제출 #708591

#제출 시각아이디문제언어결과실행 시간메모리
708591hafoKnapsack (NOI18_knapsack)C++17
12 / 100
1 ms340 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e17 + 69;
const int N = 2e3 + 7;

pall dp[2][N];
int n, s, v[maxn], w[maxn], k[maxn];

void cmp(pall &a, pall b) {
    if(a.fi < b.fi) a = b;
    else if(a.fi == b.fi && a.se > b.se) a.se = b.se;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>s>>n;
    for(int i = 1; i <= n; i++) cin>>v[i]>>w[i]>>k[i];
    for(int i = 0; i < 2; i++)
        for(int j = 0; j <= s; j++) dp[i][j] = make_pair(0, 0);

    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        int cur = (i & 1), last = (cur ^ 1);
        for(int j = 0; j <= s; j++) {
            dp[cur][j] = make_pair(0, 0);
            cmp(dp[cur][j], make_pair(dp[last][j].fi, 0));
            if(j - w[i] >= 0) {
                pall pre = dp[cur][j - w[i]];
                if(pre.se != k[i]) cmp(dp[cur][j], make_pair(pre.fi + v[i], pre.se + 1));
            }
            if(i == n) maxi(ans, dp[cur][j].fi);
        }
    }
    cout<<ans;
    return 0;
}
#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...