#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 |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2532 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2532 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
73 ms |
14020 KB |
Output is correct |
22 |
Correct |
85 ms |
14120 KB |
Output is correct |
23 |
Correct |
76 ms |
13248 KB |
Output is correct |
24 |
Correct |
60 ms |
13996 KB |
Output is correct |
25 |
Correct |
64 ms |
14184 KB |
Output is correct |
26 |
Correct |
60 ms |
13500 KB |
Output is correct |
27 |
Correct |
65 ms |
14208 KB |
Output is correct |
28 |
Correct |
64 ms |
13504 KB |
Output is correct |
29 |
Correct |
79 ms |
13240 KB |
Output is correct |
30 |
Correct |
65 ms |
14012 KB |
Output is correct |
31 |
Correct |
70 ms |
13596 KB |
Output is correct |
32 |
Correct |
68 ms |
14272 KB |
Output is correct |
33 |
Correct |
67 ms |
13200 KB |
Output is correct |
34 |
Correct |
68 ms |
13016 KB |
Output is correct |
35 |
Correct |
66 ms |
13072 KB |
Output is correct |
36 |
Correct |
78 ms |
13716 KB |
Output is correct |
37 |
Correct |
71 ms |
13244 KB |
Output is correct |
38 |
Correct |
69 ms |
13248 KB |
Output is correct |
39 |
Correct |
56 ms |
13244 KB |
Output is correct |
40 |
Correct |
57 ms |
13248 KB |
Output is correct |
41 |
Correct |
58 ms |
13096 KB |
Output is correct |
42 |
Correct |
63 ms |
13412 KB |
Output is correct |
43 |
Correct |
63 ms |
13252 KB |
Output is correct |
44 |
Correct |
63 ms |
14016 KB |
Output is correct |
45 |
Correct |
65 ms |
14192 KB |
Output is correct |
46 |
Correct |
69 ms |
13404 KB |
Output is correct |
47 |
Correct |
65 ms |
14276 KB |
Output is correct |
48 |
Correct |
66 ms |
12992 KB |
Output is correct |
49 |
Correct |
72 ms |
13240 KB |
Output is correct |
50 |
Correct |
67 ms |
13200 KB |
Output is correct |