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 fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ar3 array<int, 3>
#define nl '\n'
#define tb '\t'
#define sp ' '
using namespace std;
const int MX=100005, MOD=1e9+7, BLOCK=500, INF=2e9+7;
const ll INFF=1e18+7;
const ld ERR=1e-7, pi=3.14159265358979323846;
int S, N, DP[2][2005], cnt[2005];
ar3 A[MX];
int main(){
fastio;
cin >> S >> N;
for(int i=1, v, w, k; i<=N; i++){
cin >> v >> w >> k;
A[i]={w, v, k};
}
sort(A+1, A+N+1, greater<ar3>());
for(int i=1; i<=S; i++)
DP[0][i]=-INF;
DP[0][0]=0;
for(int i=1; i<=N; i++){
int W=A[i][0], V=A[i][1], K=A[i][2];
int now=i%2, bef=now^1;
for(int j=0; j<=S; j++){
DP[now][j]=DP[bef][j];
cnt[j]=0;
if(j>=W){
if(DP[now][j]<DP[bef][j-W]+V){
DP[now][j]=DP[bef][j-W]+V;
cnt[j]=1;
}
if(cnt[j-W]<K){
if(DP[now][j]<DP[now][j-W]+V){
DP[now][j]=DP[now][j-W]+V;
cnt[j]=cnt[j-W]+1;
}else if(DP[now][j]==DP[now][j-W]+V)
cnt[j]=min(cnt[j], cnt[j-W]+1);
}
}
}
}
N%=2;
for(int i=0; i<S; i++)
DP[N][S]=max(DP[N][S], DP[N][i]);
cout << DP[N][S] << nl;
}
# | 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... |