#include<bits/stdc++.h>
using namespace std;
//#include"nuts.h"
#define ll long long
#define x first
#define y second
#define pi pair<ll,ll>
vector<long long> Nuts(int n, vector<int> vec){
vector<ll> v(n); for(int i =0;i<n;i++) v[i]=vec[i];
priority_queue<pi> pq;
ll l[n],r[n];
for(ll i=0;i<n;i++){
pq.push({v[i],i});
l[i]=i-1;r[i]=i+1;
}
vector<bool> vis(n,0);
ll res=0;
vector<ll> ans;
for(ll i=0; i< (n+1)/2;i++){
while(vis[pq.top().y]) pq.pop();
pi p = pq.top(); pq.pop();
res += p.x;
ans.push_back(res);
ll cur = p.y;
if(r[cur]==n){
vis[cur]=1;
if(l[cur]!=-1)vis[l[cur]]=1;
if(l[l[cur]] > -1) r[l[l[cur]]]=n;
}
else if(l[cur]==-1){
vis[cur]=1;
if(r[cur]!=n)vis[r[cur]]=1;
if(r[r[cur]] < n) l[r[r[cur]]]=-1;
}
else{
v[cur] = v[r[cur]]+v[l[cur]] - v[cur];
vis[r[cur]]=1;
r[cur] = r[r[cur]];
if(r[cur] != n) l[r[cur]]=cur;
vis[l[cur]]=1;
l[cur] = l[l[cur]];
if(l[cur] != -1) r[l[cur]]=cur;
pq.push({v[cur],cur});
}
}
return ans;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
vector<int> nuts(n);
for(int i=0;i<n;i++) cin>>nuts[i];
vector<long long> ans = Nuts(n, nuts);
for(long long c : ans) cout<<c<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
1012 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
1012 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
174 ms |
13528 KB |
Output is correct |
22 |
Correct |
174 ms |
13508 KB |
Output is correct |
23 |
Correct |
172 ms |
13504 KB |
Output is correct |
24 |
Correct |
163 ms |
13780 KB |
Output is correct |
25 |
Correct |
161 ms |
14036 KB |
Output is correct |
26 |
Correct |
167 ms |
13580 KB |
Output is correct |
27 |
Correct |
168 ms |
14024 KB |
Output is correct |
28 |
Correct |
166 ms |
14536 KB |
Output is correct |
29 |
Correct |
174 ms |
14028 KB |
Output is correct |
30 |
Correct |
167 ms |
14536 KB |
Output is correct |
31 |
Correct |
167 ms |
14292 KB |
Output is correct |
32 |
Correct |
177 ms |
13764 KB |
Output is correct |
33 |
Correct |
170 ms |
14036 KB |
Output is correct |
34 |
Correct |
168 ms |
14536 KB |
Output is correct |
35 |
Correct |
176 ms |
13524 KB |
Output is correct |
36 |
Correct |
176 ms |
14788 KB |
Output is correct |
37 |
Correct |
174 ms |
13768 KB |
Output is correct |
38 |
Correct |
177 ms |
14784 KB |
Output is correct |
39 |
Correct |
163 ms |
13652 KB |
Output is correct |
40 |
Correct |
161 ms |
13512 KB |
Output is correct |
41 |
Correct |
162 ms |
13780 KB |
Output is correct |
42 |
Correct |
167 ms |
14536 KB |
Output is correct |
43 |
Correct |
169 ms |
13504 KB |
Output is correct |
44 |
Correct |
171 ms |
14024 KB |
Output is correct |
45 |
Correct |
167 ms |
13512 KB |
Output is correct |
46 |
Correct |
167 ms |
13448 KB |
Output is correct |
47 |
Correct |
175 ms |
14292 KB |
Output is correct |
48 |
Correct |
169 ms |
13512 KB |
Output is correct |
49 |
Correct |
168 ms |
13252 KB |
Output is correct |
50 |
Correct |
168 ms |
13776 KB |
Output is correct |