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>
using namespace std;
#define ll long long
const int nx=2e5+5;
ll n, a[nx], used[nx], l[nx], r[nx], res;
priority_queue<pair<ll, ll>> pq;
void erase(int u)
{
used[u]=1;
if (l[u]!=0) r[l[u]]=r[u];
if (r[u]!=n+1) l[r[u]]=l[u];
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i], pq.push({a[i], i}), l[i]=i-1, r[i]=i+1;
for (int i=1; i<=(n+1)/2; i++)
{
while (!pq.empty()&&used[pq.top().second]) pq.pop();
if (pq.empty()) break;
auto [cur, u]=pq.top();
//cout<<cur<<' '<<u<<'\n';
pq.pop();
res+=cur;
if (l[u]!=0&&r[u]!=n+1) pq.push({a[l[u]]+a[r[u]]-cur, u}), a[u]=a[l[u]]+a[r[u]]-cur;
else erase(u);
if (l[u]!=0) erase(l[u]);
if (r[u]!=n+1) erase(r[u]);
cout<<res<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |