제출 #1193029

#제출 시각아이디문제언어결과실행 시간메모리
1193029euclidKnapsack (NOI18_knapsack)C++20
49 / 100
24 ms31816 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vl vector<ll>

int n, s;
const int MN = 1e5 + 3, MS = 2003;
ll v[MN], w[MN], k[MN];
vector<pll> ww[MS];
vector<ll> weight[MS]; //weight[i] - all (up to 2000) items with weight i sorted in decreasing value
ll dp[MS][MS], pref[MS][MS];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> s >> n;
    for(int i = 1; i <= n; i++) cin >> v[i] >> w[i] >> k[i];
    for(int i = 1; i <= n; i++) ww[w[i]].pb({v[i], k[i]});
    for(int i = 1; i < MS; i++) {
        sort(ww[i].begin(), ww[i].end(), greater<>());
        for(pll p : ww[i]) {
            if(weight[i].size() == 2001) break;
            for(int j = 1; j <= min(2001-(ll)weight[i].size(), p.se); j++)
                weight[i].pb(p.fi);
        }
    }
//    for(int i = 1; i < MS; i++) {
//        for(int j = 1; j <= s; j++) {
//            dp[i][j] = max(dp[i][j], dp[i-1][j]);
//            ll sum = 0;
//            int cur = 0, tot = 0;
//            for(int x = 1; x*i <= j && cur < ww[i].size(); x++) {
//                sum += ww[i][cur].fi;
//                tot++;
//                if(tot==ww[i][cur].se) {
//                    tot = 0;
//                    cur++;
//                }
//                dp[i][j] = max(dp[i][j], dp[i-1][j-x*i]+sum);
//            }
//        }
//    }
    for(int i = 1; i < MS; i++) {
        for(int j = 1; j <= s; j++) {
            dp[i][j] = max(dp[i][j], dp[i-1][j]);
            ll sum = 0;
            for(int x = 1; x*i <= j && x <= (int)weight[i].size(); x++) {
                sum += weight[i][x-1];
                dp[i][j] = max(dp[i][j], dp[i-1][j-x*i]+sum);
            }
        }
    }
    cout << dp[MS-1][s] << '\n';

//    20 3
//    5000 15 1
//    100 1 3
//    50 1 4

}

#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...