#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge {
int l, r;
ll cost;
};
bool operator<(Edge A, Edge B) { return A.l < B.l; }
bool operator>(Edge A, Edge B) { return A.cost < B.cost; }
Edge merge(Edge A, Edge B, Edge C) { return {A.l, C.r, A.cost + C.cost - B.cost}; }
bool visited[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n;
priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
set<Edge> edges;
for (int i = 1; i <= n; i++) {
ll a;
cin >> a;
Edge e = {i, i + 1, a};
pq.push(e);
edges.insert(e);
}
Edge e = {n + 1, n + 2, (ll)-1e14};
pq.push(e);
edges.insert(e);
e = {0, 1, (ll)-1e14};
pq.push(e);
edges.insert(e);
ll ans = 0;
for (int i = 0; i < (n + 1) / 2;) {
Edge curr = pq.top();
pq.pop();
if (!visited[curr.l] && !visited[curr.r]) {
i++;
ans += curr.cost;
visited[curr.l] = visited[curr.r] = true;
auto it = edges.find(curr);
Edge l = *prev(it), r = *next(it);
Edge newedge = merge(l, curr, r);
edges.erase(l); edges.erase(curr); edges.erase(r);
edges.insert(newedge);
pq.push(newedge);
cout << ans << '\n';
}
}
return 0;
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:19:12: warning: unused variable 'k' [-Wunused-variable]
int n, k;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
508 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
4 ms |
632 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
4 ms |
508 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
4 ms |
504 KB |
Output is correct |
15 |
Correct |
4 ms |
508 KB |
Output is correct |
16 |
Correct |
3 ms |
532 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
504 KB |
Output is correct |
19 |
Correct |
4 ms |
632 KB |
Output is correct |
20 |
Correct |
4 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
508 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
4 ms |
632 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
4 ms |
508 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
4 ms |
504 KB |
Output is correct |
15 |
Correct |
4 ms |
508 KB |
Output is correct |
16 |
Correct |
3 ms |
532 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
504 KB |
Output is correct |
19 |
Correct |
4 ms |
632 KB |
Output is correct |
20 |
Correct |
4 ms |
504 KB |
Output is correct |
21 |
Runtime error |
132 ms |
34164 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
22 |
Halted |
0 ms |
0 KB |
- |