Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- #define ll long long
- #define cy cout << "YES"
- #define cn cout << "NO"
- #define nl "\n"
- #define fi first
- #define se second
- #define cin(v) for (int i = 0; i < n, cin >> v[i]; i++)
- #define cout(v) for (int i = 0; i < v.size(), cout << v[i] << " "; i++)
- #define all(v) v.begin(), v.end()
- #define sz(s) s.size()
- void sherry()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- constexpr int MAXW = 1e5;
- ll n, w;
- vector < ll > val ,weight;
- vector< vector < ll > > dp;
- ll knapsack(ll idx, ll v)
- {
- if(idx == n || v <= 0 )
- return v == 0 ? 0 : 1e9;
- ll& ret = dp[idx][v];
- if(~ret)return ret;
- ret = knapsack(idx + 1, v);
- ret = min(ret, weight[idx] + knapsack(idx + 1, v - val[idx]));
- return ret;
- }
- void solve()
- {
- cin >> n >> w;
- val.resize(n), weight.resize(n);
- dp.assign(n + 10, vector < ll > (MAXW +5 , -1));
- for(int i = 0; i < n; i++)
- cin >> weight[i] >> val[i];
- for(ll i = MAXW ; i >=0 ;i--){
- if(knapsack(0,i) <= w){
- return cout << i << "\n" ,void();
- }
- }
- }
- int main()
- {
- sherry();
- int t = 1;
- // cin >> t;
- while (t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement