# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
489601 | vicyan1611 | Knapsack (NOI18_knapsack) | C++14 | 159 ms | 36308 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |