# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1145348 | blueviolet | Knapsack (NOI18_knapsack) | C++20 | 1095 ms | 328 KiB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Os")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define io(x) if (fopen(x".inp","r")) {freopen(x".inp","r",stdin),freopen(x".out","w",stdout);}
#define mem(c, x) memset(c, x, sizeof(c))
#define all(c) c.begin(), c.end()
#define bit(i,j) ((i >> j) & 1)
#define pb push_back
#define se second
#define fi first
#define el '\n'
using namespace std;
template<class T> bool maximize(T &a, const T &b) { return (a < b ? a = b, 1 : 0); }
template<class T> bool minimize(T &a, const T &b) { return (a > b ? a = b, 1 : 0); }
int dx[8] = {0, 1, 0,-1, 1, 1,-1,-1};
int dy[8] = {1, 0,-1, 0, 1,-1,-1, 1};
const int maxn = 2e3 + 1;
const int Inf = 2e9 + 7;
const ll Infll = 1e18 + 9;
const ll Mod = 1e9 + 7;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ll dp[maxn];
int n, s;
void solve() {
cin >> s >> n;
for (int i=1; i<=n; i++) {
int v, w, sl; cin >> v >> w >> sl;
ll sum = 0;
for (int i=0; sum + (1 << i) <= sl; i++) {
ll W = 1ll * w * (1ll << i), V = 1ll * v * (1ll << i);
for (int j=s; j>=W; j--) {
maximize(dp[j], dp[j-W] + V);
}
sum += (1 << i);
}
if (sum < sl) {
ll W = 1ll * w * (sl - sum), V = 1ll * v * (sl - sum);
for (int j=s; j>=W; j--) {
maximize(dp[j], dp[j-W] + V);
}
}
}
cout << dp[s];
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
io("task");
solve();
return (0 ^ 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... |