# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1085911 | vjudge1 | Knapsack (NOI18_knapsack) | C++14 | 66 ms | 33372 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.
/**
。∠(*・ω・)っ ⌒ 由 ~ (( ,,・з・,, ))
_Π_____。
/______/~\
| 田田|門|
why im so dumb man >.<
**/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define el "\n"
typedef long long ll;
typedef pair<int,int> pii;
// var dec
const int maxn = 2e3 + 3;
int n, s;
// ds dec
//
void solve()
{
cin >> s >> n;
map <int, vector<pii>> mp;
for (int i=1; i<=n; ++i)
{
int v, w, k; cin >> v >> w >> k;
mp[w].pb({v, k});
}
vector <vector<ll>> dp(sz(mp)+3, vector<ll>(s+3, -1));
int i = 1;
dp[0][0] = 0;
for (auto x: mp)
{
int w = x.fi;
vector <pii> v = x.se;
sort(v.begin(), v.end(), greater<pii>());
for (int j=0; j<=s; ++j)
{
dp[i][j] = dp[i-1][j];
int items = 0;
int cur = 0;
int copies = v[cur].se;
ll profit = 0;
while ((items+1)*w <= j && cur < sz(v))
{
profit += v[cur].fi;
++items;
if (dp[i-1][j-items*w] != -1) dp[i][j] = max(dp[i][j], dp[i-1][j-items*w] + profit);
--copies;
if (copies == 0)
{
++cur;
copies = v[cur].se;
}
}
}
++i;
}
ll ans = 0;
for (int j=0; j<=s; ++j)
ans = max(ans, dp[sz(mp)][j]);
cout << ans << el;
}
//
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("lmao.inp", "r"))
{
freopen("lmao.inp", "r", stdin);
freopen("lmao.out", "w", stdout);
}
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
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... |