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>
#define ll long long
#define endl "\n"
using namespace std;
void solve()
{
ll m, n;
cin >> m >> n;
vector<pair<ll, ll>> el[m + 1];
vector<ll> v[m + 1];
while (n--)
{
ll p, w, k;
cin >> p >> w >> k;
el[w].push_back(make_pair(p, k));
}
for (ll i = 1; i <= m; i++)
{
sort(el[i].begin(), el[i].end(), greater());
for (auto [j, k] : el[i])
while (k-- > 0 and v[i].size() < m / i)
v[i].push_back(j);
}
ll dp[m + 1];
for (ll i = 0; i <= m; i++)
dp[i] = 0;
for (ll i = 1; i <= m; i++)
for (ll j : v[i])
for (ll k = m - i; k >= 0; k--)
dp[k + i] = max(dp[k + i], dp[k] + j);
cout << *max_element(dp, dp + m + 1) << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// precomp();
// cin >> t;
for (ll i = 1; i <= t; i++)
solve();
//cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:22:48: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
22 | while (k-- > 0 and v[i].size() < m / 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... |