Problem:
Knapsack Problem
You have
N
kinds of items that you want to put them into a knapsack. Item
i
has value
v
i
and weight
w
i
.
You want to find a subset of items to put such that:
The total value of the items is as large as possible.
The items have combined weight at most
W
, that is capacity of the knapsack.
You can select as many items as possible into a knapsack for each kind.
Find the maximum total value of items in the knapsack.
Input
N
W
v
1
w
1
v
2
w
2
:
v
N
w
N
The first line consists of the integers
N
and
W
. In the following lines, the value and weight of the
i
-th item are given.
Output
Print the maximum total values of the items in a line.
Constraints
1 ≤
N
≤ 100
1 ≤
v
i
≤ 1000
1 ≤
w
i
≤ 1000
1 ≤
W
≤ 10000
Sample Input 1
4 8
4 2
5 2
2 1
8 3
Sample Output 1
21
Sample Input 2
2 20
5 9
4 10
Sample Output 2
10
Sample Input 3
3 9
2 1
3 1
5 2
Sample Output 3
27

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(T) T.begin(), T.end()
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
void chmax(ll& x, ll y) { x = max(x, y); }
void chmin(ll& x, ll y) { x = min(x, y); }

int main() {
    // 0-1knapsack
    ll n, W;
    cin >> n >> W;
    vector<ll> w(n), v(n);
    rep(i, n) cin >> v[i] >> w[i];
    // dp[i][j]=iまでえらんで重さがjのときの価値の最大
    const ll inf = 2e18;
    vector dp(n + 1, vector<ll>(W + 1, -inf));
    dp[0][0] = 0;
    rep(i, n) {
        rep(j, W + 1) {
            chmax(dp[i + 1][j], dp[i][j]);
            //  何回も試すことができる
            for (int k = 1; k <= 1000; k++) {
                ll add = w[i] * k;
                if (add + j <= W)
                    chmax(dp[i + 1][j + add], dp[i][j] + v[i] * k);
                else
                    break;
            }
        }
    }

    ll ans = 0;
    rep(j, W + 1) chmax(ans, dp[n][j]);
    cout << ans << endl;
}
accuracy: 0.35
time_ms: 10
memory_kb: 4352
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;


void dp(int w, vector<int> &memo, const vector<pair<int, int>> &things) {
    if (w == 0) memo[0] = 0;
    else {
        int result = 0;
        for (int i = 0; i <= w; ++i) {
            result = max(result, memo[i] + memo[w-i]);
        }
        memo[w] = result;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    //重さをidxとして持つメモを使うDP

    int N, W;
    cin >> N >> W;
    vector<int> memo(W+1, 0);
    vector<pair<int, int>> things(N);
    for (int i = 0; i < N; ++i) {
        int v, w;
        cin >> v >> w;
        things[i] = {v, w};
    }
    sort(things.begin(), things.end());
    reverse(things.begin(), things.end());

    //初期埋め
    int idx = 0;
    while (idx != N) {
        auto [v, w] = things[idx];
        for (int i = w; i <= W; ++i) {
            if (!memo[i]) {
                memo[i] = v;
            }
            else break;
        }
        ++idx;
    }

    //bottom_up!
    for (int i = 0; i <=W; ++i) {
        dp(i, memo, things);
    }
    cout << memo[W] << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3504
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

template<class T> void chmax(T& a, T b) {
    if (a < b)
        a = b;
}

struct Item {
    int value;
    int weight;
    double gd;
};

bool comp(const Item& a, Item& b) {
    return a.gd > b.gd;
}

int main()
{
    int N, W;
    std::cin >> N >> W;
    std::vector<Item> item(N);
    for (int i = 0; i < N; i++) {
        std::cin >> item[i].value >> item[i].weight;
        item[i].gd = (double)item[i].value / item[i].weight;
    }
    std::sort(item.begin(), item.end(), comp);

    if (W % item[0].weight == 0) {
        std::cout << (W / item[0].weight) * item[0].value << std::endl;
    }
    else {
        std::vector<std::vector<long long>> dp(N + 1, std::vector<long long>(W + 1, 0));

        for (int i = 0; i < N; i++)
            for (int j = 0; j <= W; j++) {
                for (int k = 1; k <= W / item[i].weight; k++)
                    if (j - k * item[i].weight >= 0)
                        chmax(dp[i + 1][j],
                            dp[i][j - k * item[i].weight] + item[i].value * k);
                chmax(dp[i + 1][j], dp[i][j]);
            }

        std::cout << dp[N][W] << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 11032
final_rank: 5

code_snippet: #include<iostream>
#include<map>
#include<iomanip>
void _main();int main(){std::cin.tie(0);std::ios::sync_with_stdio(0);
std::cout<<std::fixed<<std::setprecision(12);
int _T=1;
//std::cin>>_T;
for(;_T--;)_main();}
#include<algorithm>
#include<vector>
template<class T>using V=std::vector<T>;
#define eb emplace_back
using ll=long long;
template<class S,class T>bool chmin(S&a,T b){return(a>b?a=b,1:0);}
template<class S,class T>bool chmax(S&a,T b){return(a<b?a=b,1:0);}
#define lep(a,b,c) for(int a=b;a<c;a++)
#define rep(a,b) lep(a,0,b)
using namespace std;
using pp=pair<ll,ll>;
void _main(){
	int n,limit;
	cin>>n>>limit;
	map<int,int>m;
	rep(i,n){
		int v,w;cin>>v>>w;
		chmax(m[w],v);
	}
	V<int>v,w;
	rep(i,10001)if(m[i])v.eb(m[i]),w.eb(i);
	n=v.size();
	V<V<int>>dp(n+1,V<int>(limit+1));
	rep(i,n)rep(j,limit+1)for(int k=0;j+k*w[i]<=limit;k++)chmax(dp[i+1][j+k*w[i]],dp[i][j]+k*v[i]);
	cout<<dp[n][limit]<<"\n";
}
accuracy: 1
time_ms: 60
memory_kb: 6924
final_rank: 4

code_snippet: #include <any>
#include <cassert>
#include <cfenv>
#include <cfloat>
#include <charconv>
#include <cinttypes>
#include <ciso646>
#include <clocale>
#include <codecvt>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdbool>
#include <ctgmath>
#include <forward_list>
#include <fstream>
#include <future>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <shared_mutex>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <variant>

using namespace std;

int main() {
    int N, W;
    cin >> N >> W;
    vector<int> v(N), w(N);
    map<int, int> mp;
    for (int i = 0; i < N; i++) {
        cin >> v[i] >> w[i];
        mp[w[i]] = max(mp[w[i]], v[i]);
    }

    vector<int> v2, w2;
    for (auto [weight, value] : mp) {
        v2.push_back(value);
        w2.push_back(weight);
    }

    vector<vector<int>> dp(v2.size() + 1, vector<int>(W + 1, -1));
    dp[0][0] = 0;
    for (int i = 0; i < v2.size(); i++) {
        for (int j = 0; j <= W; j++) {
            if (dp[i][j] == -1) {
                continue;
            }
            for (int k = 0; j + k * w2[i] <= W; k++) {
                dp[i + 1][j + k * w2[i]] =
                    max(dp[i + 1][j + k * w2[i]], dp[i][j] + k * v2[i]);
            }
        }
    }

    int ans = 0;
    for (int j = 0; j <= W; j++) {
        ans = max(ans, dp[v2.size()][j]);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6232
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()

using ll = long long;
using ui = unsigned int;

void _main(istream& is);
int main(void) {
#ifdef OFFLINE_JUDGE
    ifstream ifs = ifstream("input.txt");
    _main(ifs);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    _main(cin);
#endif
}

ll fact(int n) { return n <= 1 ? n : n * fact(n-1); }

void _main(istream& is) {
    int N, W;
    is >> N >> W;
    map<int, int> weightCheck;
    rep(i, 0, N) {
        int v, w;
        is >> v >> w;

        if (weightCheck.find(w) == weightCheck.end()) {
            weightCheck[w] = v;
        }
        else {
            if (weightCheck[w] < v) {
                weightCheck[w] = v;
            }
        }
    }
    vector<int> value;
    vector<int> weight;
    fore(i, weightCheck) {
        value.push_back(i.second);
        weight.push_back(i.first);
    }

    N = value.size();

    vector<vector<int>> dp(N + 1, vector<int>(W + 1, 0));
    rep(n, 1, N + 1) {
        rep(w, 0, W + 1) {
            if (weight[n - 1] > w) {
                dp[n][w] = dp[n - 1][w];
            }
            else {
                dp[n][w] = dp[n - 1][w];
                int i = 0;
                while (w - i * weight[n - 1] >= 0) {
                    dp[n][w] = max(dp[n][w], i * value[n - 1] + dp[n - 1][w - i * weight[n - 1]]);
                    i++;
                }
            }
        }
    }

    cout << dp[N][W] << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 6512
final_rank: 3