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;
typedef long long ll;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define ckmin(a,b) a = min(a,b)
#define ckmax(a,b) a = max(a,b)
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
int N, S;
cin >> S >> N;
vll V(N);
vi W(N);
vll K(N);
for (int i=0;i<N;i++) cin >> V[i] >> W[i] >> K[i];
vector<vector<pair<ll,ll>>> items_by_weight(S+1);
for (int i=0;i<N;i++) {
items_by_weight[W[i]].push_back({V[i], K[i]});
}
vector<tuple<ll, int, ll>> items;
for (int i=1;i<=S;i++) {
sort(all(items_by_weight[i]));
for (int j=0;j < items_by_weight[i].size() && j*i<=S;j++) {
auto [v, k] = items_by_weight[i][j];
items.push_back({i, v, k});
}
}
vll dp(S+1, 0);
for (auto [w, v, k] : items) {
vll ndp(S+1, 0);
for (int j=0;j<=w && j<=S;j++) {
priority_queue<pair<ll, int>> maxes;
for (int jp=0;j+jp*w<=S;jp++) {
maxes.push({dp[j + jp*w] - jp*v, jp});
while(maxes.top().second < jp - k) maxes.pop();
ll curmax = maxes.top().first;
ckmax(ndp[j+jp*w], curmax + jp*v);
}
}
swap(ndp, dp);
}
cout << dp[S] << endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:36:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int j=0;j < items_by_weight[i].size() && j*i<=S;j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |