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 all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back
#define sp <<' '<<
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<bool> vb;
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a) for(int i=(a);i>=0;i--)
#define contp(a) for(auto el:a) {cout<<el.fi<<'-'<<el.se<<' ';}cout<<endl
#define cont(a) for(auto el:a) {cout<<el<<' ';}cout<<endl
const int MAXN=1e5+5;
const int MAXS=2e3+5;
const int INF=1e13+3;
int dp[MAXS];
int say[MAXS][MAXN];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,mxw;
cin>>mxw>>n;
vi v(n+1);
vi w(n+1);
vi k(n+1);
FOR(i,n){
cin>>v[i+1]>>w[i+1]>>k[i+1];
}
FORE(i,1,n+1){
say[0][i]=k[i];
}
FORE(i,1,mxw+1){
int ind=0;
dp[i]=dp[i-1];
FORE(j,1,n+1) say[i][j]=say[i-1][j];
FORE(j,1,n+1){
if(i-w[j]>=0 and say[i-w[j]][j]>0 and dp[i-w[j]]+v[j]>dp[i]){ //kullan
dp[i]=dp[i-w[j]]+v[j];
ind=j;
}
}
if(ind){
FORE(j,1,n+1){
say[i][j]=say[i-w[ind]][j];
//i.weightte j.elemandan kaç tane kaldi
}
say[i][ind]--;
}
}
cout<<dp[mxw]<<endl;
}
Compilation message (stderr)
knapsack.cpp:26:19: warning: overflow in conversion from 'double' to 'int' changes value from '1.0000000000003e+13' to '2147483647' [-Woverflow]
26 | const int INF=1e13+3;
| ~~~~^~
# | 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... |