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