# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
887783 | conthoanco | Knapsack (NOI18_knapsack) | C++14 | 76 ms | 3156 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 fi first
#define se second
#define II pair < int , int >
#define pb push_back
#define mset(a , b) memset(a , b , sizeof a)
#define all(a) (a).begin() , (a).end()
const int N = 1e5 + 5;
int s, n, v[N], w[N], k[N], dp[2][N];
vector<II> lst[2005], vc;
void input()
{
cin >> s >> n;
for(int i = 1; i <= n; ++i) {
cin >> v[i] >> w[i] >> k[i];
k[i] = min(k[i], s);
lst[w[i]].pb({v[i], k[i]});
}
}
void solve()
{
vc.pb({0, 0});
for(int i = 1; i <= s; ++i) {
sort(lst[i].rbegin(), lst[i].rend());
int lim = s / i;
for(auto j: lst[i]) {
if(lim == 0) break;
int cur = min(lim, j.se);
lim -= cur;
while(cur--) vc.pb({j.fi, i});
}
}
int res = 0;
for(int i = 1; i < vc.size(); ++i) {
for(int curS = 0; curS <= s; ++curS) {
dp[i % 2][curS] = dp[1 - i % 2][curS];
if(curS >= vc[i].se) dp[i % 2][curS] = max(dp[i % 2][curS], dp[1 - i % 2][curS - vc[i].se] + vc[i].fi);
res = max(res, dp[i % 2][curS]);
}
}
cout << res;
}
int main()
{
if(fopen("trash.inp" , "r"))
freopen("trash.inp" , "r" , stdin) , freopen("trash.out" , "w" , stdout);
// else freopen(".inp" , "r" , stdin) , freopen(".out" , "w" , stdout);
ios::sync_with_stdio(0);
cin.tie(0);
input();
solve();
}
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... |