Submission #524138

#TimeUsernameProblemLanguageResultExecution timeMemory
524138shubham20_03Knapsack (NOI18_knapsack)C++17
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define deb(x) cout<<#x<<'='<<x<<'\n';
#define deb2(x,y) cout<<#x<<'='<<x<<", "<<#y<<'='<<y<<'\n';
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
const long double PI = acos(-1);
const int mod = 1e9 + 7;

// usaco guide
signed main() {
    fastio

    freopen("../input1.txt", "r", stdin);
    freopen("../output1.txt", "w", stdout);

    int S, N; cin >> S >> N;
    map<int, vector<pii>> bywt;
    for (int i = 0; i < N; i++) {
        int v, w, k; cin >> v >> w >> k;
        bywt[w].pb({v, k});
    }

    int dp[sz(bywt) + 1][S + 1];
    memset (dp, -1, sizeof(dp));

    dp[0][0] = 0;
    int at = 1;
    for (auto& pp : bywt) {
        int w = pp.f;
        vector<pii>& itms = pp.s;
        sort(all(itms));
        reverse(all(itms));

        for (int j = 0; j <= S; j++) {
            dp[at][j] = dp[at - 1][j];

            int copies = 0;
            int type_at = 0;
            int prof = 0;
            int cur_used = 0;
            while ((copies + 1) * w <= j and type_at < sz(itms)) {
                copies++;
                prof += itms[type_at].f;
                if (dp[at - 1][j - w * copies] != -1)
                    dp[at][j] = max(dp[at][j], dp[at - 1][j - w * copies] + prof);

                cur_used++;
                if (cur_used == itms[type_at].s) {
                    cur_used = 0;
                    type_at++;
                }
            }
        }

        at++;
    }

    cout << dp[n-1][S] << '\n';

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:64:16: error: 'n' was not declared in this scope
   64 |     cout << dp[n-1][S] << '\n';
      |                ^
knapsack.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen("../input1.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen("../output1.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~