#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
ll n,a[200005],ans;
set<pair<ll,ll> >st;
set<ll>ind;
set<pair<ll,ll> >::iterator it;
set<ll>::iterator itt;
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
ind.insert(i);
st.insert(mp(a[i] , i));
}
for(int i=1; i<=(n + 1) / 2; i++){
it = st.end();
it--;
ll val = (*it).f , cur = (*it).s;
st.erase(it);
ans += val;
ind.erase(ind.find(cur));
itt = ind.upper_bound(cur);
a[cur] = -a[cur];
if(itt != ind.end()){
a[cur] += a[(*itt)];
st.erase(st.find(mp(a[(*itt)] , (*itt))));
ind.erase(itt);
}
itt = ind.upper_bound(cur);
if(ind.size() && itt != ind.begin()){
itt--;
a[cur] += a[(*itt)];
st.erase(st.find(mp(a[(*itt)] , (*itt))));
ind.erase(itt);
}
st.insert(mp(a[cur] , cur));
ind.insert(cur);
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |