// In Practice, You should use the statndard input/output // in order to receive a score properly. // Do not use file input and output. Please be very careful. import java.util.ArrayList; import java.util.Scanner; import java.io.FileInputStream; class Climbing_Stairs { public static int Max(int[] data, int pos, boolean can1Step) { if (pos < 0) return 0; if (!can1Step) return (data[pos] + Max(data, pos - 2, true)); else { int one_step = data[pos] + Max(data, pos - 1, false); int two_step = data[pos] + Max(data, pos - 2, true); return (one_step >= two_step) ? one_step : two_step; } } public static void main(String args[]) throws Exception { // Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(new FileInputStream(Climbing Stairs.txt)); int T = sc.nextInt(); int N = 0; for (int tc = 0; tc < T; tc++) { N = sc.nextInt(); int[] stairs = new int[N]; for (int i = 0; i < N; i++) { stairs[i] = sc.nextInt(); //System.out.print(stairs[i] + ); } int max = 0; max = Max(stairs, stairs.length-1, true); System.out.println(max); } } }
©