# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
985233 | Marcus | Knapsack (NOI18_knapsack) | C++17 | 125 ms | 36432 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>
using namespace std;
#define ll long long
void read() {freopen("input.txt", "r", stdin);}
int main() {
//read();
ll s, n; cin>>s>>n;
map<ll, vector<pair<ll, ll>>> p;
for (ll i=0; i<n; i++)
{
int v, w, c; cin>>v>>w>>c;
p[w].push_back({v, c});
}
vector<vector<ll>> dp((ll)p.size()+1, vector<ll>(s+1, 0));
ll ite = 1;
for (auto &[w, items]: p)
{
sort(items.begin(), items.end(), greater<pair<ll, ll>>());
for (ll x=1; x<=s; x++)
{
dp[ite][x] = dp[ite-1][x];
ll copy = 0;
ll item = 0;
ll item_copy = 0;
ll profit = 0;
while((copy+1)*w <= x && item < items.size())
{
copy++;
profit += items[item].first;
dp[ite][x] = max(dp[ite][x], dp[ite-1][x-copy*w] + profit);
item_copy++;
if (item_copy == items[item].second) {item_copy = 0; item++;}
}
}
ite++;
}
//for (auto u: dp[(ll)p.size()]) {cout<<u<<" ";}
cout<<dp[(ll)p.size()][s];
}
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... |