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 int long long
const int N = 2008;
vector<pair<int, int>> val[N];
int dp[N][N];
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int s, n;
cin >> s >> n;
for (int i = 1; i <= n; i++) {
int v, w, k;
cin >> v >> w >> k;
if (w > s) continue;
val[w].push_back({v, k});
}
for (int i = 1; i <= s; i++) {
sort(val[i].begin(), val[i].end(), [] (auto x, auto y) {
return x.first > y.first;
});
int threshold = s / i;
vector<int> we;
for (int j = 0; j < val[i].size(); j++) {
int vv = val[i][j].first, sz = val[i][j].second;
bool bad = 0;
while (sz--) {
if (we.size() >= threshold) {
bad = 1;
break;
}
we.push_back(vv);
}
if (bad) break;
}
for (int j = 0; j <= s; j++) dp[i][j] = dp[i - 1][j];
for (int j = s; j >= i; j--) {
int cnt = 0;
int ps = 0;
for (auto x : we) {
cnt++;
ps += x;
if (j >= cnt * i) dp[i][j] = max(dp[i][j], dp[i - 1][j - cnt * i] + ps);
}
}
}
int mx = 0;
for (int j = 1; j <= s; j++) for (int i = 1; i <= s; i++) mx = max(mx, dp[j][i]);
cout << mx << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:26:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int j = 0; j < val[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
knapsack.cpp:30:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
30 | if (we.size() >= threshold) {
| ~~~~~~~~~~^~~~~~~~~~~~
# | 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... |