public class Pioneers { static int[][] input = {{215}, {193, 124}, {117, 237, 442}, {218, 935, 347, 235}, {320, 804, 522, 417, 345}, {229, 601, 723, 835, 133, 124}, {248, 202, 277, 433, 207, 263, 257}, {359, 464, 504, 528, 516, 716, 871, 182}, {461, 441, 426, 656, 863, 560, 380, 171, 923}, {381, 348, 573, 533, 447, 632, 387, 176, 975, 449}, {223, 711, 445, 645, 245, 543, 931, 532, 937, 541, 444}, {330, 131, 333, 928, 377, 733, 17, 778, 839, 168, 197, 197}, {131, 171, 522, 137, 217, 224, 291, 413, 528, 520, 227, 229, 928}, {223, 626, 34, 683, 839, 53, 627, 310, 713, 999, 629, 817, 410, 121}, {924, 622, 911, 233, 325, 139, 721, 218, 253, 223, 107, 233, 230, 124, 233}}; static boolean isPrime(int n) { if(n == 2) return true; // if n is even or equals to 1, return false if(n % 2 == 0 || n == 1) return false; // else check for odd dividers for(int i = 3; i <= Math.sqrt(n); i+=2) { if(n % i == 0) return false; } return true; } static int increment(int i, int j, int rowC) { //check for primality if(isPrime(input[i][j])) return 0; //check for the end of the array if(i+1 == rowC) return input[i][j]; int max = 0; //check down left if(j != 0) max = Math.max(max, increment(i+1, j-1, rowC)); //check down max = Math.max(max, increment(i+1, j, rowC)); //check down right max = Math.max(max, increment(i+1, j+1, rowC)); return max + input[i][j]; } public static void main(String[] args) { System.out.println(increment(0, 0, input.length)); } }