| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1308269 | tolga | Knapsack (NOI18_knapsack) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int s, n;
cin >> s >> n;
vector<int> p(n), w(n), cnt(n);
for (int i = 0; i < n; i++)
cin >> p[i] >> w[i] >> cnt[i];
vector<ll> dp(s + 1);
ll ans = 0;
for (int i = 0; i < n; i++) {
int K = cnt[i], get = 1;
while (K) {
int br = min(get, K);
K -= br, get <<= 1;
ll weight = br * w[i], price = br * p[i];
for (int j = s; j >= weight; j--) {
if (dp[j] < dp[j - weight] + price) {
dp[j] = dp[j - weight] + price;
ans = max(ans, dp[j]);
}
}
}
cout << ans << endl;
}
