제출 #1192453

#제출 시각아이디문제언어결과실행 시간메모리
1192453dprtoKnapsack (NOI18_knapsack)C++20
37 / 100
1 ms400 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];

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

    vector<pair<int, int>> adj[2005];

    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>>());

        for(auto [v, k] : adj[w]){
            int cnt = 1;
            while(k > 0){
                int take = min(cnt, k);
                int total_w = w * take;
                int total_v = v * take;
                for(int i = S - total_w; i >= 0; --i){
                    if(dp[i] != -1e9){
                        dp[i + total_w] = max(dp[i + total_w], dp[i] + total_v);
                    }
                }
                k -= take;
                cnt <<= 1;
            }
        }
    }

    int res = 0;
    for(int i = 0; i <= S; ++i){
        res = max(res, dp[i]);
    }
    cout << res << "\n";

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