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