Submission #1102288

#TimeUsernameProblemLanguageResultExecution timeMemory
1102288tuannmKnapsack (NOI18_knapsack)C++17
100 / 100
53 ms3832 KiB
#include<bits/stdc++.h>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int W = 2e3 + 1;
const string NAME = "";
int n, s;
vector<ii> item[W];
long long dp[W];

template<class X, class Y> bool maximize(X &x, const Y &y) { if (x < y) { x = y; return true; } else return false; }

void inp(){
    cin >> s >> n;

    for(int i = 1; i <= n; ++i){
        int v, w, k;
        cin >> v >> w >> k;

        item[w].pb({v, k});
    }
}

void solve(){
    for(int i = 1; i <= s; ++i)
        dp[i] = -1e16, sort(item[i].begin(), item[i].end(), greater<ii>());

    for(int w = 1; w < W; ++w){
        for(int j = s; j >= 0; --j){
            int sumW = 0;
            long long sumVal = 0;

            for(auto [v, k] : item[w]){
                for(int i = 1; i <= k; ++i){
                    sumW += w, sumVal += v;

                    if(sumW > j)
                        break;

                    maximize(dp[j], dp[j - sumW] + sumVal);
                }

                if(sumW > j)
                    break;
            }
        }
    }

    long long ans = 0;
    for(int i = 0; i <= s; ++i)
        maximize(ans, dp[i]);

    cout << ans;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen((NAME + ".inp").c_str(), "r")){
        freopen((NAME + ".inp").c_str(), "r", stdin);
        freopen((NAME + ".out").c_str(), "w", stdout);
    }

    inp();
    solve();
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen((NAME + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen((NAME + ".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...