Advertisement
secretheaven

dp question

Jul 26th, 2022
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. int helper(int idx, int s, int n, vector<pair<int,int>> &diff, vector<vector<int>> &dp){
  5.     if(idx >= n)return 0;
  6.     if(dp[idx][s] != -1)return dp[idx][s];
  7.     int ans1 = (diff[idx].first <= s ? diff[idx].second + helper(idx + 1, s - diff[idx].first, n, diff, dp) : 0);
  8.     int ans2 = helper(idx + 1, s, n, diff, dp);
  9.     return dp[idx][s] = max(ans1,ans2);
  10. }
  11. int main(){
  12.     //Write here code...
  13.     int s; cin >> s;
  14.     int n = 5;
  15.     vector<int> curr(n), future(n);
  16.     for(auto &x : curr)cin >> x;
  17.     for(auto &x : future)cin >> x;
  18.     vector<pair<int,int>> diff(n);
  19.     vector<vector<int>> dp(n, vector<int>(s + 10,-1));
  20.     for(int i = 0; i < n; i++)diff[i] = {curr[i], future[i] - curr[i]};
  21.     sort(diff.begin(),diff.end(),[](pair<int,int> &a, pair<int,int> &b){
  22.         if(a.second == b.second)return a.first < b.first;
  23.         return a.second > b.second;
  24.     });
  25.     cout << helper(0, s, n, diff, dp);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement