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 a1[] = new int[n];
  16. int a2[] = new int[n];
  17.  
  18. for(int i=0; i<n; i++){
  19. a1[i] = sc.nextInt();
  20. }
  21.  
  22. for(int i=0; i<n; i++){
  23. a2[i] = sc.nextInt();
  24. }
  25.  
  26. int dp[] = new int[n];
  27. if(n==1){
  28. dp[0] = Math.max(a1[0],a2[0]);
  29. return;
  30. }
  31. dp[1] = Math.max(a1[1],a2[1]);
  32.  
  33. for(int i=2; i<n; i++){
  34. dp[i] = Math.max(Math.max(a1[i],a2[i])+dp[i-2], dp[i-1]);
  35. }
  36.  
  37. System.out.println(dp[n-1]);
  38.  
  39. }
  40. }
Success #stdin #stdout 0.18s 54704KB
stdin
4
1 5 3 21234
-4509 200 3 40
stdout
21434