#include <iostream>
#include <fstream>
#include <queue>
#include <climits>
using namespace std;
const int nmax=200005;
priority_queue< pair<long long,int> > pq;
int n,i,j,poz,st,dr;
long long val,ans,v1,v2;
long long v[nmax];
int tt[nmax],rg[nmax],l[nmax],r[nmax];
int finds(int x)
{
while(x!=tt[x])
x=tt[x];
return x;
}
void unite(int A,int B)
{
if(rg[A]<rg[B]) swap(A,B);
rg[A]+=rg[B];tt[B]=A;
l[A]=min(l[A],l[B]);
r[A]=max(r[A],r[B]);
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n;
for(i=1;i<=n;i++)
{
cin>>v[i];
pq.push({v[i],i});
tt[i]=l[i]=r[i]=i;
rg[i]=1;
}
int cnt=0;
while((!pq.empty())&&cnt<(n+1)/2)
{
val=pq.top().first;
poz=pq.top().second;
pq.pop();
if(v[poz]!=val) continue;
cnt+=1;
st=l[finds(poz)]-1;dr=r[finds(poz)]+1;
v1=v2=0;
v[finds(poz)]=LLONG_MIN;
if(st)
{
st=finds(st);
v1=v[st];
v[st]=LLONG_MIN;
unite(finds(poz),st);
}
if(dr<=n)
{
dr=finds(dr);
v2=v[dr];
v[dr]=LLONG_MIN;
unite(finds(poz),dr);
}
ans+=val;
v[finds(poz)]=v1+v2-val;
pq.push({v[finds(poz)],finds(poz)});
cout<<ans<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |