fork download
  1. import java.util.*;
  2.  
  3. class Farida {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int t = Integer.parseInt(sc.nextLine());
  7. for (int i = 1; i <= t; i++) {
  8. int n = Integer.parseInt(sc.nextLine());
  9. long sol = 0;
  10. if (n > 0) {
  11. String line = sc.nextLine().replaceAll("\\s+"," ").trim();
  12. if (!line.equals("")) {
  13. String[] monsters_str = line.split(" ");
  14. long[] monsters = new long[n];
  15. for (int j = 0; j < n; j++) {
  16. monsters[j] = Long.parseLong(monsters_str[j]);
  17. }
  18. //Algorithm to solve problem: I solve from the smallest to the biggest problem, from the last monster to the first
  19.  
  20. for (int k = n-1; k >= 0; k--) {
  21. long s2 = 0;
  22. long s1 = 0;
  23. if (k+2 < n) {
  24. s2 = monsters[k+2];
  25. }
  26. if (k+1 < n) {
  27. s1 = monsters[k+1];
  28. }
  29. monsters[k] = Math.max(monsters[k]+s2,s1);
  30. }
  31. // En monsters[0] queda la solucion del problema
  32. sol = monsters[0];
  33. }
  34. }
  35. System.out.println("Case "+i+": "+sol);
  36.  
  37. }
  38. }
  39. }
Success #stdin #stdout 0.13s 321088KB
stdin
2
5
1 2  3 4 5
1
10
stdout
Case 1: 9
Case 2: 10