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 Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 200'010;
ll a[N];
struct Obj {
int l, r;
ll val0, val1;
} obj[N];
int rt[N];
ll val(int x) { return obj[x].val0 - obj[x].val1; }
set<pair<ll, int>, greater<pair<ll, int>>> pq;
int main()
{
cin.tie(0) -> sync_with_stdio(false);
int n;
cin >> n;
Loop (i,0,n+2) {
if (i==0 || i==n+1)
a[i] = -1e18;
else
cin >> a[i];
obj[i].l = obj[i].r = i;
obj[i].val0 = a[i];
pq.insert({val(i), i});
rt[i] = i;
}
ll sum = 0;
Loop (i,0,(n+1)/2) {
int p = pq.begin()->second;
sum += val(p);
cout << sum << '\n';
int lrt = rt[obj[p].l-1];
int rrt = rt[obj[p].r+1];
pq.erase({val(p), p});
pq.erase({val(lrt), lrt});
pq.erase({val(rrt), rrt});
rt[obj[lrt].l] = p;
rt[obj[rrt].r] = p;
swap(obj[p].val0, obj[p].val1);
obj[p].val0 += obj[lrt].val0;
obj[p].val0 += obj[rrt].val0;
obj[p].val1 += obj[lrt].val1;
obj[p].val1 += obj[rrt].val1;
obj[p].l = obj[lrt].l;
obj[p].r = obj[rrt].r;
pq.insert({val(p), p});
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |