fork download
  1. // Max subset sum of 2 array. No 2 elem should be consecutive.
  2. // If 4th elem is from ar1 then we can't chose 3rd elem from either array and 4th elem shouldn't be from ar2.
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. main(){
  7. int n;
  8. cin>>n;
  9. vector<int>a(n),b(n),dp(n);
  10. for(auto &it:a)cin>>it;
  11. for(auto &it:b)cin>>it;
  12. dp[0]=max(0,max(a[0],b[0]));
  13. dp[1]=max(dp[0],max(a[1],b[1]));
  14. for(int i=2;i<n;i++){
  15. dp[i]=max(dp[i-2]+max(a[i],b[i]),dp[i-1]);
  16. }
  17. cout<<dp[n-1]<<"\n";
  18. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
1 5 3 21234
-4509 200 3 40
stdout
21434