# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1090111 | Staheos | Knapsack (NOI18_knapsack) | C++14 | 26 ms | 1836 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 <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, s;
cin >> s >> n;
auto q = new priority_queue<pair<int, int>>[s + 1];
for (int i = 0; i <= s; i++)
{
q[i] = priority_queue<pair<int, int>>();
}
for (int i = 0; i < n; i++)
{
int v, w, k;
cin >> v >> w >> k;
q[w].push(pair<int, int>(v, k));
}
auto e = new vector<long long>[s + 1];
for (int i = 0; i <= s; i++)
{
e[i] = vector<long long>();
while (!q[i].empty() && e[i].size() < s && e[i].size() * i < s)
//while (!q[i].empty() && e[i].size() < s)
{
auto p = q[i].top();
q[i].pop();
e[i].push_back(p.first);
p.second--;
if (p.second > 0)
{
q[i].push(p);
}
}
}
delete[] q;
long long* dp = new long long[s + 1];
for (int i = 1; i <= s; i++)
{
dp[i] = -1;
}
dp[0] = 0;
for (int i = 1; i <= s; i++)
{
if (e[i].size() < 1)
{
continue;
}
for (int j = s; j >= 0; j--)
{
if (dp[j] == -1)
{
continue;
}
for (int k = 0; k < e[i].size() && j + i * (k + 1) <= s; k++)
//for (int k = 0; k < e[i].size() && j + i * (k + 1) <= s && i * k <= s; k++)
{
if (dp[j + i * (k + 1)] == -1)
{
dp[j + i * (k + 1)] = dp[j + i * k] + e[i][k];
}
else if (dp[j + i * (k + 1)] < dp[j + i * k] + e[i][k])
{
dp[j + i * (k + 1)] = dp[j + i * k] + e[i][k];
}
else
{
break;
}
}
}
}
int m = 0;
for (int i = 0; i <= s; i++)
{
//cout << "dp " << i << ' ' << dp[i] << '\n';
if (dp[i] > dp[m])
{
m = i;
}
}
cout << dp[m];
}
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... |