#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 200005;
int n, a[MN], vis[MN], ans, take, pre[MN], nxt[MN], len[MN];
struct edge {
int u, v, cost;
};
bool cmp(edge p, edge q) {
return p.cost < q.cost;
}
int32_t main() {
boost();
cin >> n;
n++;
for (int i = 2; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
pre[i] = i - 1, nxt[i] = i + 1, len[i] = a[i] - a[i - 1];
}
priority_queue<edge, vector<edge>, decltype(&cmp)> q(cmp);
for (int i = 1; i < n; i++) q.push({i, i + 1, a[i + 1] - a[i]});
while (take < n / 2) {
edge cur = q.top();
q.pop();
if (vis[cur.u] || vis[cur.v]) continue;
vis[cur.u] = vis[cur.v] = 1;
ans += cur.cost;
nxt[pre[cur.u]] = nxt[cur.v];
pre[nxt[cur.v]] = pre[cur.u];
len[nxt[cur.v]] += len[cur.u] - len[cur.v];
take++;
if (pre[cur.u] > 0 && nxt[cur.v] <= n) q.push({pre[cur.u], nxt[cur.v], len[nxt[cur.v]]});
printf("%lld\n", ans);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
174 ms |
15208 KB |
Output is correct |
22 |
Correct |
156 ms |
16388 KB |
Output is correct |
23 |
Correct |
153 ms |
16360 KB |
Output is correct |
24 |
Correct |
137 ms |
16164 KB |
Output is correct |
25 |
Correct |
105 ms |
16172 KB |
Output is correct |
26 |
Correct |
115 ms |
16212 KB |
Output is correct |
27 |
Correct |
120 ms |
16300 KB |
Output is correct |
28 |
Correct |
112 ms |
16420 KB |
Output is correct |
29 |
Correct |
101 ms |
16384 KB |
Output is correct |
30 |
Correct |
111 ms |
16388 KB |
Output is correct |
31 |
Correct |
139 ms |
16312 KB |
Output is correct |
32 |
Correct |
101 ms |
16388 KB |
Output is correct |
33 |
Correct |
125 ms |
16168 KB |
Output is correct |
34 |
Correct |
136 ms |
16072 KB |
Output is correct |
35 |
Correct |
130 ms |
16200 KB |
Output is correct |
36 |
Correct |
150 ms |
16380 KB |
Output is correct |
37 |
Correct |
153 ms |
16428 KB |
Output is correct |
38 |
Correct |
183 ms |
16324 KB |
Output is correct |
39 |
Correct |
109 ms |
16120 KB |
Output is correct |
40 |
Correct |
110 ms |
16208 KB |
Output is correct |
41 |
Correct |
132 ms |
16200 KB |
Output is correct |
42 |
Correct |
119 ms |
16300 KB |
Output is correct |
43 |
Correct |
109 ms |
16404 KB |
Output is correct |
44 |
Correct |
103 ms |
16352 KB |
Output is correct |
45 |
Correct |
135 ms |
16340 KB |
Output is correct |
46 |
Correct |
106 ms |
16300 KB |
Output is correct |
47 |
Correct |
107 ms |
16356 KB |
Output is correct |
48 |
Correct |
158 ms |
16240 KB |
Output is correct |
49 |
Correct |
119 ms |
16116 KB |
Output is correct |
50 |
Correct |
121 ms |
16120 KB |
Output is correct |