Submission #1210396

#TimeUsernameProblemLanguageResultExecution timeMemory
1210396nvc2k8Knapsack (NOI18_knapsack)C++20
100 / 100
62 ms42308 KiB
#include <bits/stdc++.h>
#define TASK "askdjaskjd"
#define INT_LIM (int) 2147483647
#define LL_LIM (long long) 9223372036854775807
#define endl '\n'
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define BIT(i,x) (((i)>>(x))&1)
#define FOR(i,a,b) for(int i = (a); i<=(b); i++)
#define FORD(i,a,b) for(int i = (a); i>=(b); i--)
#define ll long long
#define pii pair<int,int>
using namespace std;
///------------------------------------------///
int n,s;
vector<pair<ll,int>> f[2001];
int d[2001];
ll pf[2001][2001];

void inp()
{
    cin >> s >> n;
    FOR(i, 1, n)
    {
        ll val; int weight, cnt;
        cin >> val >> weight >> cnt;
        f[weight].pb(mp(val,cnt));
    }

    FOR(i, 1, s)
    {
        sort(f[i].begin(), f[i].end());
        d[i] = s/i;
        FOR(j, 1, s/i)
        {
            if (f[i].empty())
            {
                d[i] = j-1; break;
            }
            pf[i][j] = f[i].back().fi+pf[i][j-1];
            f[i].back().se--;
            if (f[i].back().se==0) f[i].pop_back();
        }
    }
}

ll dp[2001][2001];

void solve()
{
    FOR(i, 0, s) FOR(j, 0, s) dp[i][j] = -LL_LIM;
    dp[0][0] = 0;

    FOR(i, 1, s) FOR(j, 0, s)
    {
        FOR(t, 0, d[i])
        {
            if (j-t*i<0) break;
            if (dp[i-1][j-t*i]!=-LL_LIM) dp[i][j] = max(dp[i][j], dp[i-1][j-t*i]+pf[i][t]);
        }
    }


    ll ans = 0;
    FOR(j, 0, s) ans = max(ans, dp[s][j]);
    cout << ans;
}

signed main()
{
    ///--------------------------///
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    if (fopen(TASK".INP","r")!=NULL)
    {
        freopen(TASK".INP","r",stdin);
        freopen(TASK".OUT","w",stdout);
    }
    ///--------------------------///

    int NTEST = 1;
    //cin >> NTEST;

    while (NTEST--)
    {
        inp();
        solve();
    }

    return 0;
}

///------------------------------------------///

Compilation message (stderr)

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