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"holiday.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, C = 1e9 + 7;
ll go(int n, int s, int d, int a[]) {
priority_queue<int, vi, greater<int>> pq;
ll ans = 0, sum = 0;
int cost = 0;
for(int i = s; i < n; i++) {
pq.push(a[i]);
sum += a[i];
while(SZ(pq) + cost > d) {
if(pq.empty()) return ans;
sum -= pq.top();
pq.pop();
}
ans = max(ans, sum);
cost++;
}
return ans;
}
struct node {
node *l, *r;
int cnt;
ll sum;
node() {
cnt = sum = 0;
l = r = nullptr;
}
node(node *_l, node *_r) {
l = _l, r = _r;
cnt = (l ? l -> cnt : 0) + (r ? r -> cnt : 0);
sum = (l ? l -> sum : 0LL) + (r ? r -> sum : 0LL);
}
};
int CNT(node* t) {
return t ? t -> cnt : 0;
}
ll SUM(node* t) {
return t ? t -> sum : 0LL;
}
node* lc(node* t) {
return t ? t -> l : nullptr;
}
node* rc(node* t) {
return t ? t -> r : nullptr;
}
node* upd(node* t, int pos, int tl = 0, int tr = INF) {
if(tr - tl == 1) {
node* res = new node();
res -> cnt = CNT(t) + 1;
res -> sum = SUM(t) + tl;
return res;
}
int tm = (tl + tr) / 2;
if(pos < tm) return new node(upd(lc(t), pos, tl, tm), rc(t));
else return new node(lc(t), upd(rc(t), pos, tm, tr));
}
ll qqry(node* t, int need, int tl = 0, int tr = INF) {
if(need == 0) return 0LL;
if(tr - tl == 1)
return 1LL * tl * need;
int tm = (tl + tr) / 2;
if(CNT(rc(t)) >= need)
return qqry(rc(t), need, tm, tr);
else
return qqry(lc(t), need - CNT(rc(t)), tl, tm) + SUM(rc(t));
}
struct DS {
vi a;
V<pi> stk;
int c, n;
V<node*> h;
ll test(int d, int i) {
if(i * c > d) return -1;
d -= i * c;
d = min(d, i + 1);
assert(d >= 0 && d <= CNT(h[i]));
return qqry(h[i], d);
}
DS(vi _a, int _c) {
a = _a;
c = _c;
n = SZ(a);
node* t = nullptr;
for(int i = 0; i < n; i++) {
t = upd(t, a[i]);
h.PB(t);
}
for(int i = 0; i < n; i++) {
while(SZ(stk) && test(stk.back().F, stk.back().S) <= test(stk.back().F, i))
stk.pop_back();
if(stk.empty())
stk.EB(i * c, i);
else {
int lb = stk.back().F + 1, rb = INF;
// find first x s.t. test(x, stk.back().S) <= test(x, i)
while(lb <= rb) {
int mb = (lb + rb) / 2;
if(test(mb, stk.back().S) <= test(mb, i))
rb = mb - 1;
else
lb = mb + 1;
}
stk.EB(lb, i);
}
}
}
ll qry(int d) {
int it = upper_bound(ALL(stk), pi(d, INF)) - stk.begin() - 1;
return test(d, stk[it].S);
}
};
ll solve(int n, int s, int d, int a[]) { // go left in the first step
if(s == 0) return 0;
vi pl, pr;
for(int i = s - 1; i >= 0; i--) pl.PB(a[i]);
for(int i = s; i < n; i++) pr.PB(a[i]);
DS dsl = DS(pl, 2);
DS dsr = DS(pr, 1);
ll ans = 0;
for(int i = 0; i + 2 <= d; i++) {
ans = max(ans, dsl.qry(i) + dsr.qry(d - i - 2));
}
return ans;
}
ll findMaxAttraction(int n, int s, int d, int a[]) {
ll ans = go(n, s, d, a);
ans = max(ans, solve(n, s, d, a));
reverse(a, a + n);
s = n - s - 1;
ans = max(ans, go(n, s, d, a));
ans = max(ans, solve(n, s, d, a));
return ans;
}
# | 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... |