fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. import static java.lang.Integer.max;
  7.  
  8. /**
  9.  * Created by bugkiller on 07/02/18.
  10.  */
  11. public class CalvinsGame {
  12.  
  13. static int a[] = new int[1000005];
  14. static int dp[] = new int[1000005];
  15.  
  16. public static void main(String[] args) throws IOException {
  17. int n, k;
  18. String[] s;
  19. s = br.readLine().split("\\s");
  20. n = Integer.parseInt(s[0]);
  21. k = Integer.parseInt(s[1]);
  22. s = br.readLine().split("\\s");
  23. for (int i = 1; i <= n; i++) {
  24. a[i] = Integer.parseInt(s[i - 1]);
  25. }
  26. System.out.println(solve(a, n, k));
  27. }
  28.  
  29. private static int solve(int[] a, int n, int k) {
  30. return maxScore(a, n, k);
  31. }
  32.  
  33. private static int maxScore(int[] a, int n, int k) {
  34. for (int i = k+1; i <=n ; i++) {
  35. dp[i] = max(dp[i - 1], dp[i - 2]) + a[i];
  36. }
  37. int maxScore = 0, maxScoreIndex = k;
  38. for (int i = k; i <=n; i++) {
  39. if (dp[i] > maxScore) {
  40. maxScore = dp[i];
  41. maxScoreIndex = i;
  42. }
  43. }
  44. Arrays.fill(dp, 0);
  45. for (int i = maxScoreIndex - 1; i > 0; i--) {
  46. dp[i] = max(dp[i + 1], dp[i + 2]) + a[i];
  47. }
  48. return dp[1] + maxScore;
  49. }
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:11: error: class CalvinsGame is public, should be declared in a file named CalvinsGame.java
public class CalvinsGame {
       ^
1 error
stdout
Standard output is empty