# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
524138 | shubham20_03 | Knapsack (NOI18_knapsack) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define deb(x) cout<<#x<<'='<<x<<'\n';
#define deb2(x,y) cout<<#x<<'='<<x<<", "<<#y<<'='<<y<<'\n';
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
const long double PI = acos(-1);
const int mod = 1e9 + 7;
// usaco guide
signed main() {
fastio
freopen("../input1.txt", "r", stdin);
freopen("../output1.txt", "w", stdout);
int S, N; cin >> S >> N;
map<int, vector<pii>> bywt;
for (int i = 0; i < N; i++) {
int v, w, k; cin >> v >> w >> k;
bywt[w].pb({v, k});
}
int dp[sz(bywt) + 1][S + 1];
memset (dp, -1, sizeof(dp));
dp[0][0] = 0;
int at = 1;
for (auto& pp : bywt) {
int w = pp.f;
vector<pii>& itms = pp.s;
sort(all(itms));
reverse(all(itms));
for (int j = 0; j <= S; j++) {
dp[at][j] = dp[at - 1][j];
int copies = 0;
int type_at = 0;
int prof = 0;
int cur_used = 0;
while ((copies + 1) * w <= j and type_at < sz(itms)) {
copies++;
prof += itms[type_at].f;
if (dp[at - 1][j - w * copies] != -1)
dp[at][j] = max(dp[at][j], dp[at - 1][j - w * copies] + prof);
cur_used++;
if (cur_used == itms[type_at].s) {
cur_used = 0;
type_at++;
}
}
}
at++;
}
cout << dp[n-1][S] << '\n';
return 0;
}