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 <map>
#include <set>
#include<algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include<iomanip>
#include <array>
using namespace std;
const int MAXN = 1e6+ 11;
const int MOD = 1e9+7;
using ll = long long;
using ld = long double;
const int K = 26;
ll n,S;
ll v[MAXN];
ll weight[MAXN];
ll k[MAXN];
vector<pair<ll, int>> obj[4000];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("cowjog.in", "r", stdin);
//freopen("cowjog.out", "w", stdout);
cin >> S >> n;
for (int i = 0; i < n; i++)
cin >> v[i] >> weight[i] >> k[i];
ll mx = *max_element(k, k + n);
if (n == 1)
cout << v[0] * min(k[0],S/weight[0]) << '\n';
else if (n <= 100 && mx <= 10)
{
ll dp[2300];
memset(dp, 0, sizeof dp);
for (int i = 0; i < n; i++)
{
for (int j = 1; j <= k[i]; j++)
{
for (int w = S; w >= 0; w--)
{
if ((w == 0 || dp[w] > 0) && w + weight[i] <= S)
{
dp[w + weight[i]] = max(dp[w + weight[i]], dp[w] + v[i]);
}
}
}
}
ll ans = 0;
for (int i = 1; i <= S; i++)
ans = max(ans, dp[i]);
cout << ans << '\n';
}
else
{
ll dp[2300];
memset(dp, 0, sizeof dp);
for (int i = 0; i < n; i++)
obj[weight[i]].push_back({ v[i],k[i] });
for (int i = 1; i <= S; i++)
{
if (obj[i].size() == 0)
continue;
sort(obj[i].begin(), obj[i].end(),greater<pair<ll,ll>>());
int id = 0;
for (int j = 1; j*i <= S; j++)
{
if (id >= obj[i].size())
break;
for (int w = S; w >= 0; w--)
{
if ((w == 0 || dp[w] > 0) && w + i <= S)
{
dp[w + i] = max(dp[w + i], dp[w] + obj[i][id].first);
}
}
obj[i][id].second--;
if (obj[i][id].second == 0)
id++;
}
}
ll ans = 0;
for (int i = 1; i <= S; i++)
ans = max(ans, dp[i]);
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:115:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | if (id >= obj[i].size())
| ~~~^~~~~~~~~~~~~~~~
# | 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... |