Submission #1321949

#TimeUsernameProblemLanguageResultExecution timeMemory
1321949jim_xKnapsack (NOI18_knapsack)C++17
100 / 100
37 ms1624 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <numeric>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;

void baseIO(string s = ""){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (s.size()){
        freopen((s + ".in").c_str(), "r", stdin);
        freopen((s + ".out").c_str(), "w", stdout);
    }
}

int main() {
    baseIO();

    int s, n;
    cin >> s >> n;


    vector<vector<pair<int, int>>> by_weight(s + 1);
    for (int i = 0; i < n; i++){
        int a, b, c;
        cin >> a >> b >> c;
        by_weight[b].push_back({a, c});
    }

    int dp[s + 1]{};

    for (int w = 1; w <= s; w++){
        sort(by_weight[w].begin(), by_weight[w].end(), greater<>());
        int cnt = 0;
        for (auto &p : by_weight[w]){
            while (p.second--){
                cnt++;
                for (int pos = s; pos >= w; pos--){
                    dp[pos] = max(dp[pos], dp[pos - w] + p.first);
                }
                if (cnt >= s / w) break;
            }
            if (cnt >= s / w) break;
        }
    }

    cout << *max_element(dp, dp + s + 1);

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void baseIO(std::string)':
knapsack.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen((s + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen((s + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...