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 <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <queue>
#include <stack>
#include <cassert>
#include <cstring>
#include <climits>
#include <functional>
#include<cstdlib>
//#include "arc.h"
//#include "dreaming.h"
using namespace std;
const int MAXN = 2e3 + 11;
using ll = long long;
const int P = 31;
const ll mod1 = 1e9 + 7;
const ll mod2 = 1000000021;
using ld = long double;
int s, n;
vector<pair<int,int>> pre[MAXN];
vector<int> vals[MAXN];
ll dp[MAXN];
int main()
{
cin.tie(0);
cout.tie(0);
//freopen("248.in", "r", stdin);
//freopen("248.out", "w", stdout);
cin >> s >> n;
for (int i = 0; i < n; i++)
{
int v, w, k;
cin >> v >> w >> k;
pre[w].push_back({ v,k });
}
for (int i = 1; i <= s; i++)
sort(pre[i].rbegin(), pre[i].rend());
for (int i = 1; i <= s; i++)
{
int mx = s / i;
int cur = 0;
if (pre[i].size() == 0)
continue;
for (int j = 0; j < mx; j++)
{
if (pre[i][cur].second == 0)
{
cur++;
}
if (cur == pre[i].size())
break;
pre[i][cur].second--;
vals[i].push_back(pre[i][cur].first);
}
}
for (int j = 1; j <= s; j++)
{
if (vals[j].empty())
continue;
for (int i = s; i >= 0; i--)
{
if (i == 0 || dp[i] > 0)
{
ll Sum = 0;
for (int c = 0; c < vals[j].size(); c++)
{
if (i + (c + 1) * j > s)
break;
Sum += vals[j][c];
//cout << i + (c + 1) * j << '\n';
dp[i + (c + 1) * j] = max(dp[i + (c + 1) * j],
dp[i] + Sum);
}
}
}
}
ll ans = 0;
for (int i = 1; i <= s; i++)
ans = max(ans, dp[i]);
//cout << dp[i] << " ";
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:72:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if (cur == pre[i].size())
| ~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int c = 0; c < vals[j].size(); c++)
| ~~^~~~~~~~~~~~~~~~
# | 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... |