제출 #1192460

#제출 시각아이디문제언어결과실행 시간메모리
1192460dprtoKnapsack (NOI18_knapsack)C++20
37 / 100
1 ms328 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){
        cin >> v[i] >> w[i] >> k[i];
        if(w[i] > S) continue;
        adj[w[i]].push_back({v[i], k[i]});
    }

    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 [val, cnt] : adj[w]){
            int num = 1;
            while(cnt > 0){
                int take = min(num, cnt);
                int total_w = w * take;
                int total_v = val * take;
                for(int i = S; i >= 0; --i){
                    if(i + total_w <= S && dp[i] != -1e9){
                        dp[i + total_w] = max(dp[i + total_w], dp[i] + total_v);
                    }
                }
                cnt -= take;
                num <<= 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...