Submission #966327

#TimeUsernameProblemLanguageResultExecution timeMemory
966327vjudge1Knapsack (NOI18_knapsack)C++17
73 / 100
1043 ms1368 KiB
#include <bits/stdc++.h>
#define ll long long

using namespace std;
 
int s, n, k, v, w, cw, cv;
ll dp[2010], ans;
priority_queue<int, vector<int>, greater<int>> pq[2010];
vector<pair<int, int>> item;
 
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> s >> n;
    for(int i=0; i<n; i++) {
        cin >> v >> w >> k;
        k = min(k, s/w);
        while(k--) {
            pq[w].push(v);
            while(pq[w].size() > s/w) pq[w].pop();
        }
    }
    for(int i=1; i<=s; i++) {
        while(!pq[i].empty()) {
            item.push_back({i, pq[i].top()});
            pq[i].pop();
        }
    }
    for(auto i: item) {
        cw = i.first, cv = i.second;
        for(int j=s; j>=0; j--) {
            if(dp[j] == 0 && j != 0) continue;
            if(cw + j <= s) dp[cw+j] = max(dp[cw+j], dp[j]+cv);
        }
    }
    for(int i=0; i<=s; i++) ans = max(ans, dp[i]);
    cout << ans;
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:19:32: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   19 |             while(pq[w].size() > s/w) pq[w].pop();
      |                   ~~~~~~~~~~~~~^~~~~
#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...