제출 #1162407

#제출 시각아이디문제언어결과실행 시간메모리
1162407AlfraganusKnapsack (NOI18_knapsack)C++20
12 / 100
9 ms328 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define str string
#define endl '\n'
#define fs first
#define ss second
#define all(a) a.begin(), a.end()

#define print(a) for(auto x : a) cout << x << ' '; cout << endl;
#define printmp(a) for(auto x : a) cout << x[0] << ' ' << x[1] << endl;

const int mod = 1e9 + 7;

struct SegTree {

    int size = 1;
    vector<int> tree;

    SegTree (vector<int> &a){
        while((int)a.size() > size)
            size <<= 1;
        tree.resize(size << 1, -1e9);
        for(int j = 0; j < (int)a.size(); j ++)
            tree[size + j - 1] = a[j];
        for(int j = (int)tree.size() - 1; j >= 0; j --)
            tree[(j - 1) / 2] = max(tree[(j - 1) / 2], tree[j]);
    }

    int get(int l, int r, int x, int lx, int rx){
        if(l <= lx and rx <= r)
            return tree[x];
        else if(r <= lx or rx <= l)
            return -1e9;
        int mid = (lx + rx) >> 1;
        return max(get(l, r, (x << 1) + 1, lx, mid), get(l, r, (x << 1) + 2, mid, rx));
    }
};

void solve(){
    int s, n;
    cin >> s >> n;
    int v[n], w[n], k[n], dp[s + 1];
    for(int i = 0; i < n; i ++)
        cin >> v[i] >> w[i] >> k[i];
    for(int i = 0; i <= s; i ++)
        dp[i] = 0;
    for(int i = 0; i < n; i ++){
        for(int j = 0; j < w[i]; j ++){
            vector<int> a;
            for(int x = 0; x * w[i] + j <= s; x ++)
                a.push_back(dp[x * w[i] + j] - x * v[i]);
            SegTree st(a);
            for(int x = 0; x * w[i] + j <= s; x ++)
                dp[x * w[i] + j] = max(dp[x * w[i] + j], st.get(max(0, x - k[i]), x + 1, 0, 0, st.size) + x * v[i]);
        }
    }
    cout << *max_element(dp, dp + s + 1);
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while(t --){
        solve();
        cout << endl;
    }
}
#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...