fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4.  
  5. int main() {
  6. // your code goes here
  7.  
  8. ll n;
  9. cin>>n;
  10. ll arr1[n+1]={0},arr2[n+1]={0};
  11. ll i=1;
  12. while(i<=n)
  13. {
  14. cin>>arr1[i];
  15. i++;
  16. }
  17. i=1;
  18. while(i<=n)
  19. {
  20. cin>>arr2[i];
  21. i++;
  22. }
  23.  
  24. ll dp[n+1]={0};
  25. ll k=0;
  26. dp[1]=max(arr1[1],max(arr2[1],k));
  27. dp[2]=max(dp[1],max(arr1[2],arr2[2]));
  28.  
  29. i=3;
  30. while(i<=n)
  31. {
  32. ll x= arr1[i]+dp[i-2];
  33. ll y = arr2[i]+dp[i-2];
  34. ll z = dp[i-1];
  35. dp[i]=max(x,max(y,z)); //trick to find max of 3 numbers
  36. // cout<<i<<" "<<dp[i]<<"\n";
  37. i++;
  38. }
  39. cout<<dp[n];
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5220KB
stdin
5
2 3 4 -8 2 
-5 8 3 1 -4
stdout
3 8
4 9
5 10
10