Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <queue>
- #include <vector>
- using namespace std;
- typedef long long ll;
- ll dp[105][100005];
- int main() {
- int n, W;
- cin >> n >> W;
- int sum = 0;
- vector<int> weights(n), values(n);
- for(int i = 0; i < n; i++) {
- cin >> weights[i] >> values[i];
- sum += values[i];
- }
- for(int i = 0; i < 105; i++) {
- for(int j = 0; j < 100005; j++) {
- dp[i][j] = 2e18;
- }
- }
- dp[0][0] = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j <= sum; j++) {
- dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
- if(j + values[i] <= sum) {
- dp[i + 1][j + values[i]] = min(dp[i + 1][j + values[i]], dp[i][j] + weights[i]);
- }
- }
- }
- for(int i = sum; i >= 0; i--) {
- if(dp[n][i] <= W) {
- cout << i << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement