Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Lamps {
- public static void main(String[] args) throws Exception {
- Scanner scan = new Scanner(System.in);
- String s = scan.nextLine();
- String[] sArray = s.split(" ");
- int[] lampa = new int[sArray.length];
- for (int i = 0; i < sArray.length; i++) {
- lampa[i] = Integer.valueOf(sArray[i]);
- }
- System.out.println(solve(lampa));
- }
- public static int solve(int[] pole) {
- int x = 0;
- int n = pole.length;
- if (n > 5) {
- int m = n / 2;
- int a = solve(Arrays.copyOfRange(pole, 0, (m)));
- int b = solve(Arrays.copyOfRange(pole, m, (n)));
- int c = solve(Arrays.copyOfRange(pole, 0, (m - 1)));
- int d = solve(Arrays.copyOfRange(pole, (m + 1), (n)));
- x = Math.min((a + b), Math.min((c + b), (a + d)));
- } else if (n == 5) {
- x = Math.min(pole[2], (pole[1] + pole[3])) + pole[0] + pole[4];
- } else if (n == 4) {
- x = Math.min(pole[1], pole[2]) + pole[0] + pole[3];
- } else if (n == 3) {
- x = pole[0] + pole[pole.length - 1];
- } else if (n == 2) {
- x = pole[0] + pole[pole.length - 1];
- } else if (n < 2) {
- x = pole[0];
- }
- return x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement