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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
using vi = vector<int>;
using ll = long long;
using pii = pair<ll, int>;
const int MAXN = 100000 + 7;
const int MAXM = 2023;
int V[MAXN], W[MAXN], K[MAXN];
vector<pii> item[MAXM];
ll dp[MAXM][MAXM];
int main() {
ios::sync_with_stdio(false);
int S, N;
cin >> S >> N;
//unordered_map<int, vector<pii>> item;
for (int i = 0; i < N; i++) {
cin >> V[i] >> W[i] >> K[i];
item[W[i]].push_back(make_pair(V[i], K[i]));
}
for (int i = 0; i <= S; i++) {
sort(all(item[i]));
reverse(all(item[i]));
}
memset(dp, 0xff, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= S; i++) {
for (int j = 0; j <= S; j++) {
if (dp[i - 1][j] != -1) dp[i][j] = max(dp[i][j], dp[i - 1][j]);
int cnt = 0, w = i;
ll v = 0;
for (int it = 0; it < item[i].size(); it++) {
for (int k = 0; k < item[i][it].second && cnt < (S / w); k++) {
cnt++;
v += item[i][it].first;
if (j - cnt * w >= 0 && dp[i - 1][j - cnt * w] != -1) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - cnt * w] + v);
}
}
if (cnt > (S / w)) break;
}
}
}
ll res = 0;
for (int i = 0; i <= S; i++) {
res = max(res, dp[S][i]);
}
cout << res << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:53:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int it = 0; it < item[i].size(); it++) {
| ~~~^~~~~~~~~~~~~~~~
# | 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... |