제출 #1330971

#제출 시각아이디문제언어결과실행 시간메모리
1330971silver_bulletKnapsack (NOI18_knapsack)C++17
49 / 100
3 ms348 KiB
#include<bits/stdc++.h>
using namespace std;
const int maxW = (int)2e3 + 2;
priority_queue<int,vector<int>,greater<int>> pq[maxW];
long long dp[maxW];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int W,n;
    cin >> W >> n;
    for(int i=1;i<=n;++i)
    {
        int v,w,k;
        cin >> v >> w >> k;
        while(pq[w].size() < W / w && k > 0)
        {
            --k;
            pq[w].push(v);
        }
        if(pq[w].size() == W / w && k)
        {
            while(pq[w].top() < v)
            {
                pq[w].pop();
                pq[w].push(v);
            }
        }
    }
    vector<array<int,2>> a;
    a.clear();
    for(int w=1;w<=W;++w) 
    {
        while(pq[w].size())
        {
            a.push_back({w,pq[w].top()});
            pq[w].pop();
        }
    }
    for(auto x : a)
    {
        for(int i=W;i>=x[0];--i)
        dp[i] = max(dp[i],dp[i-x[0]] + x[1]);
    }
    cout << dp[W];
}
#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...