Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- int n, W;
- int w[105], v[105];
- long long dp[105][100005];
- long long rek(int idx, int value) {
- if(value == 0) {
- return 0;
- }
- if(idx == n) {
- return 2e9;
- }
- if(dp[idx][value] != -1) {
- return dp[idx][value];
- }
- long long result = 2e9;
- result = min(result, rek(idx + 1, value));
- if(value >= v[idx]) {
- result = min(result, rek(idx + 1, value - v[idx]) + w[idx]);
- }
- dp[idx][value] = result;
- return result;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cout.tie(0);
- cin.tie(0);
- cin >> n >> W;
- for(int i = 0; i < n; i++) {
- cin >> w[i] >> v[i];
- }
- for(int i = 0; i < n; i++) {
- for(int j = 0; j <= 100000; j++) {
- dp[i][j] = -1;
- }
- }
- for(int i = 100000; i >= 0; i--) {
- if(rek(0, i) <= W) {
- cout << i << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement