Advertisement
ogv

Untitled

ogv
Jan 2nd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. // Java 616 ms  94016 KB
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         int N;
  8.         int[] a, b, c;
  9.         try (Scanner scanner = new Scanner(System.in)) {
  10.             N = scanner.nextInt();
  11.  
  12.             a = new int[N];
  13.             b = new int[N];
  14.             c = new int[N];
  15.  
  16.             for (int i = 0; i < N; i++) {
  17.                 a[i] = scanner.nextInt();
  18.                 b[i] = scanner.nextInt();
  19.                 c[i] = scanner.nextInt();
  20.             }
  21.         }
  22.  
  23.         int result = solve(a, b, c, N);
  24.         System.out.println(result);
  25.  
  26.         //test();
  27.     }
  28.  
  29.     private static int solve(int[] a, int[] b, int[] c, int N) {
  30.         int sa = 0;
  31.         int sb = 0;
  32.         int sc = 0;
  33.         for (int i = 0; i < N; i++) {
  34.             int newSa = Math.max(sb, sc) + a[i];
  35.             int newSb = Math.max(sa, sc) + b[i];
  36.             int newSc = Math.max(sa, sb) + c[i];
  37.             sa = newSa;
  38.             sb = newSb;
  39.             sc = newSc;
  40.         }
  41.         return Math.max(sa, Math.max(sb, sc));
  42.     }
  43.  
  44.     private static void test() {
  45.         testCase(new int[] { 10, 20, 30}, new int[] {40, 50, 60},  new int[] {70, 80, 90}, 210);
  46.         testCase(new int[] { 100 }, new int[] {10},  new int[] {1}, 100);
  47.         testCase(new int[] { 6,8,2,7,4,2,7}, new int[] {7,8,5,8,6,3,5},  new int[] {8,3,2,6,8,4,1}, 46);
  48.         System.out.println("DONE");
  49.     }
  50.  
  51.     private static void testCase(int[] a, int[] b, int[] c, int expected) {
  52.         int result = solve(a, b, c, a.length);
  53.         if (result != expected) {
  54.             System.out.println("FAILED a=" + Arrays.toString(a) + " b=" + Arrays.toString(b) + " c=" + Arrays.toString(c)
  55.                     + ", was " + result + " expected " + expected);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement