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>
#define N 100000
#define S 2000
#define pb push_back
#define int long long
#define mp make_pair
#define fi first
#define se second
#define rep(i, l, r) for(int i = l; i <= r; i++)
using namespace std;
typedef pair<int,int> ii;
int n, s;
int dp[S+5];
vector<int> val[S+5];
vector<ii> ares;
struct mondo {
int v, w, k;
} a[N+5];
bool cmp(mondo x, mondo y){
if (x.w == y.w) return x.v > y.v;
return x.w < y.w;
}
void solve(){
cin >> s >> n;
rep(i, 1, n) {
cin >> a[i].v >> a[i].w >> a[i].k;
}
sort(a+1, a+1+n, cmp);
rep(i, 1, n) {
// cout << a[i].v << " " << a[i].w << " " << a[i].k << endl;
}
rep(i, 1, n){
int maxsize = s / a[i].w;
if (a[i].w > s) continue;
while (a[i].k && val[a[i].w].size() < maxsize){
a[i].k--;
val[a[i].w].pb(a[i].v);
}
}
rep(i, 1, s){
for(auto x: val[i]){
ares.pb(mp(i, x));
}
}
int res = 0;
for(auto x: ares) {
int w = x.fi, v = x.se;
for(int i = s; i >= w; i--){
dp[i] = max(dp[i], dp[i-w] + v);
}
}
rep(i, 0, s) res = max(res, dp[i]);
cout << res;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("bt.in", "r", stdin);
// freopen("bt.out", "w", stdout);
// #endif
solve();
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:40:45: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
40 | while (a[i].k && val[a[i].w].size() < maxsize){
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
# | 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... |