Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <fstream>
- #include <array>
- using namespace std;
- typedef long long ll;
- vector<int> palindromes;
- int n, weight_max;
- int w[105], v[105];
- ll dp[105][100005];
- ll knapsack(int at, int value) {
- if(value == 0) {
- return 0;
- }
- if(at == n) {
- return 1e9;
- }
- if(dp[at][value] != -1) {
- return dp[at][value];
- }
- ll result = 1e9;
- result = min(result, knapsack(at + 1, value));
- if(value - v[at] >= 0)
- result = min(result, knapsack(at + 1, value - v[at]) + w[at]);
- return dp[at][value] = result;
- }
- int main()
- {
- cin >> n >> weight_max;
- int sum = 0;
- for(int i = 0; i < n; i++) {
- cin >> w[i] >> v[i];
- sum += v[i];
- }
- for(int i = 0; i <= n; i++) {
- for(int j = 0; j <= sum; j++) {
- dp[i][j] = -1;
- }
- }
- for(int i = sum; i >= 0; i--) {
- if(knapsack(0, i) <= weight_max) {
- cout << i << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement