Submission #490654

#TimeUsernameProblemLanguageResultExecution timeMemory
490654CyberSleeperKnapsack (NOI18_knapsack)C++14
29 / 100
1 ms332 KiB
#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, V[MX], W[MX], K[MX], DP[2][2005], cnt[2005];

int main(){
    fastio;
    cin >> S >> N;
    for(int i=1; i<=N; i++){
        cin >> V[i] >> W[i] >> K[i];
    }
    for(int i=1; i<=S; i++)
        DP[0][i]=-INF;
    DP[0][0]=0;
    for(int i=1; i<=N; i++){
        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[i]){
                if(DP[now][j]<DP[bef][j-W[i]]+V[i]){
                    DP[now][j]=DP[bef][j-W[i]]+V[i];
                    cnt[j]=1;
                }
                if(cnt[j-W[i]]<K[i]){
                    if(DP[now][j]<DP[now][j-W[i]]+V[i]){
                        DP[now][j]=DP[now][j-W[i]]+V[i];
                        cnt[j]=cnt[j-W[i]]+1;
                    }else if(DP[now][j]==DP[now][j-W[i]]+V[i])
                        cnt[j]=min(cnt[j], cnt[j-W[i]]+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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...