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 A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a))
#define boost() ios_base::sync_with_stdio(false); cin.tie(); cout.tie()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const int mxN=2e3+2;
vector <pii> withw[mxN];
int dp[mxN][mxN]={};
int main(){
int s, n;
cin >> s >> n;
for(int i=0; i<n; i++){
int v, w, k;
cin >> v >> w >> k;
withw[w].push_back(MP(v, k));
}
for(int i=1; i<=mxN; i++){
sort(withw[i].begin(), withw[i].end());
for(int j=1; j<=s; j++){
dp[i][j] = dp[i-1][j];
int totalval=0, totalw=0;
for(auto [val, amnt]:withw[i]){
int temp=1;
while(temp <= amnt && j >= totalw){
totalval+=val;
totalw+=i;
dp[i][j] = max(dp[i][j], dp[i-1][j-totalw]+totalval);
temp++;
}
}
}
}
cout << dp[mxN][s];
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:52:19: warning: array subscript 2002 is above array bounds of 'int [2002][2002]' [-Warray-bounds]
52 | cout << dp[mxN][s];
| ~~~~~~^
knapsack.cpp:26:5: note: while referencing 'dp'
26 | int dp[mxN][mxN]={};
| ^~
# | 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... |