Submission #648355

#TimeUsernameProblemLanguageResultExecution timeMemory
648355thienbao1602Knapsack (NOI18_knapsack)C++17
100 / 100
125 ms37152 KiB
#include    <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x.size())
#define pi pair<ll, ll>
#define fi first
#define se second
using namespace std;

const int N = 2e3 + 55;
const ll inf = -1e16;

int n, S;
ll best[N][N];
map<ll, vector<pi>> weight;

void solve()
{
    cin >> S >> n;
    for(int i=0; i<n; i++)
    {
        ll val, w, sl;
        cin >> val >> w >> sl;
        weight[w].push_back({val, sl});
    }

    memset(best, inf, sizeof(best));
    best[0][0] = 0;
    int id = 1;
    for(auto [w, items] : weight)
    {
        sort(items.begin(), items.end(), greater<pi>());
        for(int j=0; j<=S; j++)
        {
            best[id][j] = best[id - 1][j];
            ll profit = 0, type_at = 0, sl = 0, used = 0;
            while((sl + 1) * w <= j && type_at < sz(items))
            {
                sl++;
                profit += items[type_at].fi;
                if (best[id - 1][j - w * sl] != inf)
                {
                    best[id][j] = max(best[id][j], best[id - 1][j - w * sl] + profit);
                }

                used++;
                if (used == (ll)items[type_at].second)
                {
                    used = 0;
                    type_at++;
                }
            }
        }
        id++;
    }

    ll ret = 0;
    for(int j=0; j<=S; j++)
    {
        ret = max(ret, best[id - 1][j]);
    }
    cout << ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void solve()':
knapsack.cpp:26:18: warning: overflow in conversion from 'long long int' to 'int' changes value from '-10000000000000000' to '-1874919424' [-Woverflow]
   26 |     memset(best, inf, sizeof(best));
      |                  ^~~
#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...