제출 #1192441

#제출 시각아이디문제언어결과실행 시간메모리
1192441dprtoKnapsack (NOI18_knapsack)C++20
37 / 100
1095 ms17044 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define f(i, l, r) for(int i = l; i <= r; ++i)
const int mod = 1e9 + 7;
const int ars = 2e5 + 5;
const ll infll = 1e18;


int s, n, v[ars], w[ars], k[ars], dp[2005];
vector<pair<int, int>> adj[2005];

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int S, N;
    cin >> S >> N;

    for(int i = 0; i < N; ++i){
        int v, w, k;
        cin >> v >> w >> k;
        if (w > S) continue;
        adj[w].push_back({v, k});
    }

    for(int i = 0; i <= S; ++i) dp[i] = -1e9;
    dp[0] = 0;

    for(int w = 1; w <= S; ++w){
        if (adj[w].empty()) continue;

        sort(adj[w].begin(), adj[w].end(), greater<pair<int, int>>());

        vector<int> items;

        for(auto [v, k] : adj[w]){
            while (k--) items.push_back(v);
        }

        for(int v : items){
            for(int i = S - w; i >= 0; --i){
                if (dp[i] != -1e9){
                    dp[i + w] = max(dp[i + w], dp[i] + v);
                }
            }
        }
    }
    int res = 0;
    for(int i = 0; i <= S; ++i){
        res = max(res, dp[i]);
    }
    cout << res;


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