# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
683639 | vjudge1 | Knapsack (NOI18_knapsack) | C++17 | 1064 ms | 17144 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 task "a"
#define etr "\n"
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define bg begin
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define lwb lower_bound
#define upb upper_bound
#define range(x, l, r) x+l, x+1+r
#define all(x) (x).bg(), (x).end()
#define compact(x) x.resize(unique(all(x)) - (x).bg())
#define sq(x) ((x)*(x))
void freop()
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const int N=1e5, M=2e3, mod=1e9+7;
int n, s;
ll v[N*10+5], w[N*10+5], dp[M+5];
void process()
{
cin >> s >> n;
for (int i=1; i<=n; i++)
{
ll k;
cin >> v[i] >> w[i];
cin >> k;
k = min(k, s/w[i]);
int bit = 0;
while (true)
{
if (k >= (1 << bit))
{
v[i+bit] = v[i] * (1 << bit);
w[i+bit] = w[i] * (1 << bit);
k -= (1 << bit);
if (k == 0) break;
} else
{
v[i+bit] = v[i] * k;
w[i+bit] = w[i] * k;
break;
}
bit++;
}
i += bit;
n += bit;
}
ll res=0;
for (int i=1; i<=n; i++)
{
for (int j=s; j>=w[i]; j--)
{
dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
res = max(res, dp[j]);
}
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1; //cin >> t;
while (t--) process();
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... |