fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15. int m=sc.nextInt();
  16. int arr[][]=new int[n][2];
  17. for(int i=0;i<n;i++){
  18. arr[i][0]=sc.nextInt();
  19. arr[i][1]=sc.nextInt();
  20. }
  21. int dp[][]=new int[n+1][2];
  22. dp[1][0]=arr[0][0];
  23. dp[1][1]=arr[0][1];
  24. for(int i=2;i<=n;i++){
  25. dp[i][0]=dp[i-1][0]+arr[i-1][0];
  26. dp[i][1]=Math.max(dp[i-2][0],dp[i-2][1])+arr[i-1][1];
  27. }
  28. System.out.println(Math.max(dp[n][0],dp[n][1]));
  29. }
  30. }
Success #stdin #stdout 0.12s 56524KB
stdin
4 2
1 2
4 10
20 21
2 23
stdout
33