Advertisement
Guest User

Cable Merchant

a guest
Aug 26th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.IOException;
  5.  
  6. public class CableMerchant {
  7.  
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
  10.         String[] temp = sc.readLine().split(" ");
  11.         int[] cables = new int[temp.length];
  12.         for (int i = 0; i < temp.length; i++) {
  13.             cables[i] = Integer.parseInt(temp[i]);
  14.            
  15.         }
  16.         int cPrice = Integer.parseInt(sc.readLine());
  17.         int connectPrice = cPrice * 2;
  18.  
  19.         for (int i = 1; i < cables.length; i++) {
  20.             int splits = (i + 1) / 2;
  21.  
  22.             int start = 0;
  23.             int end = i - 1;
  24.             while (splits > 0) {
  25.                 cables[i] = Math.max(cables[i], cables[start++] + cables[end--] - connectPrice);
  26.                 splits--;
  27.             }
  28.         }
  29.  
  30.         System.out.println(Arrays.toString(cables).replaceAll("[\\[\\],]", ""));
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement