Submission #831258

#TimeUsernameProblemLanguageResultExecution timeMemory
831258jmao142857Knapsack (NOI18_knapsack)C++14
100 / 100
365 ms8876 KiB
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <unordered_set>
#include <set>
#include <queue>

using namespace std;
using ll = long long;

void setIO(string s) {
	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

    int S, N; cin >> S >> N;

    map<int, vector<vector<int>>> nums;
    for (int i = 0; i < N; i++) {
        int a, b, c; cin >> a >> b >> c;
        nums[b].push_back({a, c});
    }
    for (int j = 1; j <= S; j++) sort(nums[j].begin(), nums[j].end());

    vector<ll> dp(S + 1);
    for (int j = 1; j <= S; j++) {
        int pointer = nums[j].size() - 1;
        int count = 0;
        while (pointer >= 0 && count <= (S / j)) {
            for (int i = S; i >= 0; i--) {
                if (i >= j) dp[i] = max(dp[i - j] + nums[j][pointer][0], dp[i]);
            }
            nums[j][pointer][1]--; count++;
            if (nums[j][pointer][1] == 0) pointer--;
        }
    }

    ll ans = 0;
    for (int i = 0; i <= S; i++) ans = max(ans, dp[i]);
    cout << ans;
}

Compilation message (stderr)

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