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()
{
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<int>[s + 1];
for (int i = 0; i <= s; i++)
{
e[i] = vector<int>();
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;
int* dp = new int[s + 1];
for (int i = 1; i <= s; i++)
{
dp[i] = -1;
}
dp[0] = 0;
int* h = new int[s + 1];
for (int i = 1; i <= s; i++)
{
if (e[i].size() < 1)
{
continue;
}
for (int j = 0; j <= s; j++)
{
h[j] = 0;
}
for (int j = 1; j <= s; j++)
{
if (j - i < 0)
{
continue;
}
if (dp[j - i] == -1)
{
continue;
}
if (e[i].size() <= h[j - i])
{
// nigdy nie powinien być mniejszy, co najwyżej większy lub równy
continue;
}
if (dp[j] == -1)
{
dp[j] = dp[j - i] + e[i][h[j - i]];
h[j] = h[j - i] + 1;
continue;
}
}
for (int j = 1; j <= s; j++)
{
if (j - i < 0)
{
continue;
}
if (dp[j - i] == -1)
{
continue;
}
if (e[i].size() <= h[j - i])
{
// nigdy nie powinien być mniejszy, co najwyżej większy lub równy
continue;
}
if (dp[j] == -1)
{
dp[j] = dp[j - i] + e[i][h[j - i]];
h[j] = h[j - i] + 1;
continue;
}
if (dp[j] > dp[j - i] + e[i][h[j - i]])
{
dp[j] = dp[j - i] + e[i][h[j - i]];
h[j] = h[j - i] + 1;
}
}
}
int m = 0;
for (int i = 0; i <= s; i++)
{
if (dp[i] > dp[m])
{
m = i;
}
}
cout << dp[m];
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:27:39: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
27 | while (!q[i].empty() && e[i].size() < s)
| ~~~~~~~~~~~~^~~
knapsack.cpp:70:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | if (e[i].size() <= h[j - i])
| ~~~~~~~~~~~~^~~~~~~~~~~
knapsack.cpp:92:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
92 | if (e[i].size() <= h[j - i])
| ~~~~~~~~~~~~^~~~~~~~~~~
# | 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... |