# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
983760 | Peter2017 | Knapsack (NOI18_knapsack) | C++17 | 1067 ms | 2908 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>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define name "test"
using namespace std;
const int N = 1e5 + 5;
const int S = 2e3 + 5;
const int mod = 1e9 + 7;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
struct item{
int v, w, k;
};
int n;
int s;
ll f[2][S];
item a[N];
ll Max[S];
deque<ll> dq[S];
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void input(){
cin >> s >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].v >> a[i].w >> a[i].k;
}
void solve(){
for (int i = 1; i <= n; i++){
bool cur = (i & 1);
bool pre = (cur ^ 1);
for (int j = 0; j < a[i].w; j++){
while (dq[j].size()) dq[j].pop_back();
dq[j].push_back(j);
maxi(f[cur][j], f[pre][j]);
}
for (int j = a[i].w; j <= s; j++){
int p = j % a[i].w;
while (dq[p].size() && (j - dq[p].front()) / a[i].w > a[i].k) dq[p].pop_front();
maxi(f[cur][j], f[pre][dq[p].front()] + 1ll * ((j - dq[p].front()) / a[i].w) * a[i].v);
maxi(f[cur][j], f[cur][j - 1]);
maxi(f[cur][j], f[pre][j]);
while (dq[p].size()){
int id = dq[p].back();
if (f[pre][id] - 1ll * ((id - p) / a[i].w) * a[i].v <= f[pre][j] - 1ll * ((j - p) / a[i].w) * a[i].v) dq[p].pop_back();
else break;
}
dq[p].push_back(j);
}
}
cout << f[n & 1][s];
}
int main(){
load();
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... |