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 int long long
const int N = (int)2e5+10;
int n, a[N], l[N], r[N], ok[N];
priority_queue<array<int,2>> pq;
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; int ans = 0; a[0]=a[n+1]=-2e18;
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 j = 0; j < (n+1)/2; j++){
while(ok[pq.top()[1]]) pq.pop();
auto [w,i] = pq.top(); pq.pop();
ok[l[i]]=ok[r[i]]=1; cout << (ans+=w) << "\n";
a[i]=a[l[i]]+a[r[i]]-a[i], pq.push({a[i],i});
l[i] = l[l[i]], r[l[i]]=i; r[i] = r[r[i]], l[r[i]]=i;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |