Advertisement
sherry_ahmos

Untitled

Nov 29th, 2022
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. #define ll long long
  8. #define cy cout << "YES"
  9. #define cn cout << "NO"
  10. #define nl "\n"
  11. #define fi first
  12. #define se second
  13. #define cin(v) for (int i = 0; i < n, cin >> v[i]; i++)
  14. #define cout(v) for (int i = 0; i < v.size(), cout << v[i] << " "; i++)
  15. #define all(v) v.begin(), v.end()
  16. #define sz(s) s.size()
  17.  
  18. void sherry()
  19. {
  20.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  21. #ifndef ONLINE_JUDGE
  22.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  23. #endif
  24. }
  25. ll  n , w;
  26. vector < pair < ll, ll> >v;
  27. map < ll , ll >mp;
  28. ll rec(ll i , ll j){
  29.     if(j == 0 || i == n - 1) return 0;
  30.     if(j < 0 || i >= n) return 1e9;
  31.  
  32.     ll& ret = mp[i];
  33.     if(mp.find(ret) != mp.end()) return ret;
  34.  
  35.     ret = min(rec(i + 1 , j) , rec(i + 1 , j - v[i].se) + v[i].fi);
  36.     return ret;
  37. }
  38. void solve(){
  39.     cin >> n >> w;
  40.     v.resize(n);
  41.     for(int i = 0 ; i < n ; i++)
  42.         cin >> v[i].fi >> v[i].se;
  43.     for(int i = 1e5 ; i >= 0 ; i--){
  44.         if(rec(0 , i) <= w){
  45.             return void(cout << i << nl);
  46.         }
  47.     }
  48. }
  49. int main()
  50. {
  51.     sherry();
  52.     int t = 1;
  53.     // cin >> t;
  54.     while (t--)
  55.         solve();
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement