# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
551307 | AA_Surely | Candies (JOI18_candies) | C++17 | 0 ms | 0 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 FOR(i,x,n) for(int i=x; i<n; i++)
#define F0R(i,n) FOR(i,0,n)
#define ROF(i,x,n) for(int i=n-1; i>=x; i--)
#define R0F(i,n) ROF(i,0,n)
#define WTF cout << "WTF" << endl
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 2e5 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
#define lc now << 1
#define rc now << 1 | 1
struct Node {
VLL v[2][2];
} tree[N << 2];
LL n;
LL ns[N];
inline void merge(Node &a, Node b, Node c) {
F0R(i, 2) F0R(j, 2) {
a.v[i][j].resize(b.v[1][1].size() + c.v[1][1].size());
F0R(k, a.v[i][j].size()) a.v[i][j][k] = -INF;
}
F0R(l, 2) F0R(r, 2) F0R(i, b.v[1][1].size()) F0R(j, c.v[1][1].size())
a.v[l][r][i + j] = max({a.v[l][r][i + j],
b.v[l][0][i] + max(c.v[0][r][j], c.v[1][r][j]),
max(b.v[l][0][i], b.v[l][1][i]) + c.v[0][r][j]});
return;
}
void build(int now = 1, int ls = 0, int rs = n - 1) {
if (ls == rs) {
F0R(i, 2) F0R(j, 2) {
tree[now].v[i][j].pb(0);
tree[now].v[i][j].pb(-INF);
}
tree[now].v[1][1].pop_back();
tree[now].v[1][1].pb(ns[ls]);
return;
}
int mid = (ls + rs) >> 1;
build(lc, ls, mid); build(rc, mid + 1, rs);
merge(tree[now], tree[lc], tree[rc]);
return;
}
int main() {
IOS;
cin >> n;
F0R(i, n) cin >> ns[i];
build();
FOR(i, 1, (n >> 1) + (n & 1) + 1) {
LL mx = 0;
F0R(x, 2) F0R(y, 2) mx = max(mx, tree[1].v[x][y][i]);
cout << mx << '\n'
}
return 0;
}