fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void max_score(vector<int>&dp,vector<int>&arr,vector<int>&arr2){
  6. int n=arr.size();
  7. dp[0]=max(arr[0],arr2[0]);
  8. dp[1]=max(dp[0],max(arr[1],arr2[1]));
  9. for(int i=2;i<n;i++){
  10. dp[i]=max(dp[i-1],max(arr[i]+dp[i-2],arr2[i]+dp[i-2]));
  11. }
  12.  
  13. }
  14. int main(){
  15. int n;
  16. cin>>n;
  17. vector<int>arr(n);
  18. vector<int>arr2(n);
  19. for(int i=0;i<n;i++){
  20. cin>>arr[i];
  21. }
  22. for(int i=0;i<n;i++){
  23. cin>>arr2[i];
  24. }
  25.  
  26.  
  27. vector<int>dp(n);
  28. max_score(dp,arr,arr2);
  29. cout<<dp[n-1]<<"is the max score that be obtained from the arrays"<<endl;
  30. return 0;
  31. }
Success #stdin #stdout 0s 5320KB
stdin
5
2 3 4 -8 2
-5 8 3 1 -4
stdout
10is the max score that be obtained from the arrays