Submission #489601

#TimeUsernameProblemLanguageResultExecution timeMemory
489601vicyan1611Knapsack (NOI18_knapsack)C++14
100 / 100
159 ms36308 KiB
#include <bits/stdc++.h>

using namespace std;
const long long oo = 1e18 + 7;

long long s, n;
long long f[2002][2002];
vector <pair <long long, long long>> ite[2002];

int main()
{
    cin >> s >> n;
    for (long long i = 1; i <= n; ++i)
    {
        long long v, w, k;
        cin >> v >> w >> k;
        if (w > s || k <= 0) continue;
        ite[w].push_back(make_pair(v, k));
    }
    for (long long i = 0; i <= s; ++i)
    {
        for (long long j = 0; j <= s; ++j)
        {
            f[i][j] = -oo;
        }
    }
    f[0][0] = 0;
    long long countid = 1;
    for (long long id = 1; id <= s; ++id)
    {
        if (!ite[id].size()) continue;
        sort(ite[id].begin(), ite[id].end(), greater <pair <long long, long long>>());
        for (long long w = 0; w <= s; ++w)
        {
            f[countid][w] = f[countid-1][w];
            long long idt = 0;
            long long copies = 0;
            long long prof = 0;
            long long cnt = 0;
            while ((copies + 1) * id <= w && idt < ite[id].size())
            {
                copies++;
                prof += ite[id][idt].first;
                if (f[countid - 1][w - copies * id] != -oo)
                {
                    f[countid][w] = max(f[countid][w], f[countid - 1][w - copies * id] + prof);
                }
                cnt++;
                if (cnt == ite[id][idt].second)
                {
                    idt++;
                    cnt = 0;
                }
            }
        }
        countid++;
    }
    long long res = 0;
    for (long long i = 0 ; i <= s; ++i)
    {
        for (long long j = 0; j <= s; ++j)
        {
            res = max(res, f[i][j]);
        }
    }
    cout << res;
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:40:50: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |             while ((copies + 1) * id <= w && idt < ite[id].size())
      |                                              ~~~~^~~~~~~~~~~~~~~~
#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...