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
#define double long double
#define ull unsigned long long
#define pii pair<int,int>
#define tiii tuple<int,int,int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define s second
#define f first
#define pb push_back
#define oo 1000000000000000000ll
#define all(a) (a).begin(), a.end()
#define rall(a) (a).rbegin(), a.rend()
#define mp make_pair
void solve() {
int s, n; cin >> s >> n;
vector<int> v(n), w(n), k(n);
for (int i = 0; i < n; ++i) cin >> v[i] >> w[i] >> k[i];
vector< vector< pair<int, int> > > by_weight(s+1);
for (int i = 0; i < n; ++i){
by_weight[w[i]].pb(mp(v[i], k[i]));
}
for (int i = 1; i <= s; ++i){
sort(rall(by_weight[i]));
}
vector< vector<int> > dp(s+1, vector<int>(s+1, -oo)); dp[0][0] = 0;
for (int i = 1; i <= s; ++i){
for (int j = 0; j <= s; ++j){
dp[i][j] = dp[i-1][j];
int cur_idx = 0;
int cur_cnt = 0;
int smv = 0;
int smw = 0;
while (cur_idx < by_weight[i].size() && smw + i <= j){
smw += i;
smv += by_weight[i][cur_idx].f;
dp[i][j] = max(dp[i][j], dp[i-1][j - smw] + smv);
cur_cnt++;
if (cur_cnt == by_weight[i][cur_idx].s){
cur_cnt = 0;
cur_idx++;
}
}
//cerr << i << " " << j << " " << dp[i][j] << "\n";
}
//cerr << "\n";
}
int ans = 0;
for (int i = 1; i <= s; ++i) ans = max(ans, dp[s][i]);
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tst; tst = 1;
//cin >> tst;
while (tst--){
solve();
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:46:19: 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]
46 | while (cur_idx < by_weight[i].size() && smw + i <= 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... |