View Code of Problem 65

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int t = scanner.nextInt();
		for(int i = 0;i < t;i++) {
			int n = scanner.nextInt();
			int[][] a = new int[n][n];
			for(int j = 0;j < n;j++) {
				for(int k = 0;k <= j;k++) {
					a[j][k] = scanner.nextInt();
				}
			}
			for(int j = n - 1; j > 0;j--) {
				for(int k = 0;k < j;k++) {
					if(a[j][k] > a[j][k + 1]) {
						a[j - 1][k] += a[j][k];
					}
					else {
						a[j - 1][k] += a[j][k + 1];
					}
				}
			}
			System.out.println(a[0][0]);
		}
	}
}

Double click to view unformatted code.


Back to problem 65