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 ll long long
#define db double
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define fs first
#define sc second
#define pb push_back
const ll Mod = 1e9 + 7;
const ll k = 300;
const db PI = 3.1415;
signed main() {
//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll w, n, overallans = -1; cin >> w >> n;
vector<vector<ll>> cnt(w + 1, vector<ll>(w + 1));
vector<ll> dp(w + 1);
vector<vector<pll>> elements(w + 1);
vector<vector<ll>> pref(w + 1);
for (ll i = 0; i < n; i++) {
ll weight, cost, amount;
cin >> cost >> weight >> amount;
elements[weight].pb({cost, amount});
}
for (ll i = 1; i <= w; i++) {
sort(all(elements[i]), greater<pll>());
pref[i].resize(elements[i].size());
for (ll j = 0; j < elements[i].size(); j++) {
if (j == 0) pref[i][j] = elements[i][j].sc;
else pref[i][j] = pref[i][j - 1] + elements[i][j].sc;
}
}
for (ll i = 1; i <= w; i++) {
ll id = 0, mx = -1, used = 0;
for (ll j = 1; j <= i; j++) {
if (elements[j].empty() || cnt[i - j][j] >= pref[j][elements[j].size() - 1]) continue;
auto it = lower_bound(all(pref[j]), cnt[i - j][j] + 1) - pref[j].begin();
if (mx < dp[i - j] + elements[j][it].fs) {
mx = dp[i - j] + elements[j][it].fs;
id = i - j;
used = j;
}
}
dp[i] = mx;
cnt[i] = cnt[id];
cnt[i][used]++;
overallans = max(overallans, dp[i]);
}
cout << overallans << "\n";
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:29:26: 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]
29 | for (ll j = 0; j < elements[i].size(); 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... |