# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
955973 | Nelt | Knapsack (NOI18_knapsack) | C++17 | 46 ms | 2908 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 <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
void solve()
{
ll s, n;
cin >> s >> n;
vector<ll> items[s + 1];
vector<pair<ll, ll>> vec[s + 1];
while (n--)
{
ll v, w, k;
cin >> v >> w >> k;
vec[w].push_back(make_pair(v, k));
}
for (ll i = 1; i <= s; i++)
{
sort(vec[i].begin(), vec[i].end(), greater());
for (auto j : vec[i])
while (items[i].size() * i <= s and j.second--)
items[i].push_back(j.first);
}
ll dp[s + 1];
for (ll i = 0; i <= s; i++)
dp[i] = 0;
for (ll i = 1; i <= s; i++)
for (ll j : items[i])
for (ll k = s - i; k >= 0; k--)
dp[k + i] = max(dp[k + i], dp[k] + j);
cout << *max_element(dp, dp + s + 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)
# | 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... |