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;
using ll = long long;
using ld = long double;
#define pll pair<ll,ll>
#define pii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
ll dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
ll dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const ll INF = 1e9 + 16, M = 1e9 + 7, MaxN = 2e5 +6;
struct item {
ll v, w, c;
};
void solve(int tc) {
int s, n;
cin >> s >> n;
vector<pll > a[s + 1];
for (int i = 0; i < n; ++i) {
int v, w, c;
cin >> v >> w >> c;
a[w].push_back({v, c});
}
vector<vll > dp(s + 2, vll(s + 1, 0));
for (ll w = 0; w <= s; ++w) {
sort(a[w].rbegin(), a[w].rend());
ll copies = 0;
for (auto [v, c]: a[w]) {
if (copies > s)
break;
copies += w * c;
for (ll ct = 0; ct <= c; ++ct) {
for (ll i = s; i >= 0; --i) {
if (i - w * ct >= 0)
dp[w][i] = max(dp[w][i], dp[w][i - w * ct] + v * ct);
else
break;
}
}
}
for (ll i = s; i >= 0; --i)
dp[w + 1][i] = max(dp[w + 1][i], dp[w][i]);
}
cout << *max_element(dp[s + 1].begin(), dp[s + 1].end()) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int tt = 1;
// cin >> tt;
for (int tc = 1; tc <= tt; tc++) {
solve(tc);
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve(int)':
knapsack.cpp:28:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
28 | for (auto [v, c]: a[w]) {
| ^
# | 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... |