# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1220982 | longgggg | Knapsack (NOI18_knapsack) | C++17 | 6 ms | 12352 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define IN "A.in"
#define OUT "A.out"
#define DEBUG "debug.out"
const int mxN = 100005, INF = (int) 1e9+5;
const ll MOD = (ll) 1e9+7, LINF = (ll) 1e18;
void solve() {
int n, limit; cin >> limit >> n;
// (v, w) got k times
int totalItems = 0;
map <pair <int, int>, int> items;
for (int i = 1; i <= n; i++) {
int v, w, k; cin >> v >> w >> k;
items[{v, w}] = k;
totalItems += k;
}
vector <vector <ll>> dp(limit+1, vector <ll>(limit+1, 0));
// dp[i][j]: maximum val until item i with limit j
int i = 1;
for (auto &it : items) {
int v = it.first.first, w = it.first.second, k = it.second;
for (int t = 1; t <= k; t++) {
for (int j = 1; j <= limit; j++) {
dp[i][j] = dp[i-1][j];
if (j-w >= 0) dp[i][j] = max(dp[i][j], dp[i-1][j - w] + v);
}
++i;
}
}
cout << dp[totalItems][limit] << endl;
}
signed main() {
if (fopen(IN, "r")) {
freopen(IN, "r", stdin);
freopen(OUT, "w", stdout);
freopen(DEBUG, "w", stderr);
}
ios_base::sync_with_stdio(0); cin.tie(0);
// ll t; cin >> t;
// while (t--)
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |