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.  
  16. // 1-based indexing
  17. int [] arr1=new int [N+1];
  18. int [] arr2=new int [N+1];
  19.  
  20. for(int i=1;i<=N;i++){
  21. arr1[i]=sc.nextInt();
  22. }
  23.  
  24. for(int i=1;i<=N;i++){
  25. arr2[i]=sc.nextInt();
  26. }
  27.  
  28. int [] dp=new int[N+1];
  29.  
  30. if(arr1[1]>0 || arr2[1]>0){
  31. dp[1]=Math.max(arr1[1],arr2[1]);
  32. }else{
  33. dp[1]=0;
  34. }
  35.  
  36. dp[2]=Math.max(arr1[2],dp[1]);
  37. dp[2]=Math.max(dp[2],arr2[2]);
  38.  
  39. for(int i=3;i<=N;i++){
  40. int lmx=Math.max(arr1[i],arr2[i]);
  41. // no consecutive elements should be considered....
  42. dp[i]=Math.max(lmx+dp[i-2],dp[i-1]);
  43. }
  44. System.out.println("Answer : "+dp[N]);
  45. sc.close();
  46. }
  47. }
Success #stdin #stdout 0.14s 58976KB
stdin
5
2 3 4 -8 2
-5 8 3 1 -4
stdout
Answer : 10