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 = 1e18 + 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, item = 0, cur = 0;
while ((copies + 1) * w <= s && item < a[w].size()) {
copies++;
cur++;
for (int i = s; i >= w; --i) {
dp[w][i] = max(dp[w][i], dp[w][i - w] + a[w][item].first);
}
if (cur == a[w][item].second)
cur = 0, item++;
}
for (int 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());
}
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:46: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | while ((copies + 1) * w <= s && item < a[w].size()) {
| ~~~~~^~~~~~~~~~~~~
# | 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... |