#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int &x : a)
cin >> x;
vector<long long> edge(n);
multiset<pair<long long, int>> pq;
set<int> st;
for (int i=0; i<n; i++) {
edge[i] = a[i];
pq.emplace(a[i], i);
st.insert(i);
}
long long ret = 0;
for (int rep=0; rep<(n+1)/2; rep++) {
auto [x, i] = *pq.rbegin();
pq.erase({x, i});
ret += x;
auto it = st.erase(st.find(i));
if (it == st.begin()) {
if (it != st.end()) {
pq.erase({edge[*it], *it});
st.erase(it);
}
} else if (it == st.end()) {
pq.erase({edge[*prev(it)], *prev(it)});
st.erase(prev(it));
} else {
pq.erase({edge[*it], *it});
pq.erase({edge[*prev(it)], *prev(it)});
edge[*prev(it)] += edge[*it] - x;
pq.emplace(edge[*prev(it)], *prev(it));
st.erase(it);
}
cout << ret << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
584 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
476 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
480 KB |
Output is correct |
11 |
Correct |
2 ms |
480 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
584 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
584 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
476 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
480 KB |
Output is correct |
11 |
Correct |
2 ms |
480 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
584 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
463 ms |
26168 KB |
Output is correct |
22 |
Correct |
505 ms |
26212 KB |
Output is correct |
23 |
Correct |
526 ms |
26192 KB |
Output is correct |
24 |
Correct |
204 ms |
26188 KB |
Output is correct |
25 |
Correct |
262 ms |
26176 KB |
Output is correct |
26 |
Correct |
224 ms |
26188 KB |
Output is correct |
27 |
Correct |
214 ms |
26212 KB |
Output is correct |
28 |
Correct |
210 ms |
26156 KB |
Output is correct |
29 |
Correct |
203 ms |
26200 KB |
Output is correct |
30 |
Correct |
232 ms |
26164 KB |
Output is correct |
31 |
Correct |
218 ms |
26244 KB |
Output is correct |
32 |
Correct |
225 ms |
26244 KB |
Output is correct |
33 |
Correct |
353 ms |
26168 KB |
Output is correct |
34 |
Correct |
403 ms |
26256 KB |
Output is correct |
35 |
Correct |
412 ms |
26196 KB |
Output is correct |
36 |
Correct |
638 ms |
26288 KB |
Output is correct |
37 |
Correct |
525 ms |
26212 KB |
Output is correct |
38 |
Correct |
482 ms |
26216 KB |
Output is correct |
39 |
Correct |
223 ms |
26248 KB |
Output is correct |
40 |
Correct |
204 ms |
26252 KB |
Output is correct |
41 |
Correct |
232 ms |
26212 KB |
Output is correct |
42 |
Correct |
202 ms |
26208 KB |
Output is correct |
43 |
Correct |
241 ms |
26208 KB |
Output is correct |
44 |
Correct |
202 ms |
26328 KB |
Output is correct |
45 |
Correct |
255 ms |
26188 KB |
Output is correct |
46 |
Correct |
217 ms |
26220 KB |
Output is correct |
47 |
Correct |
225 ms |
26276 KB |
Output is correct |
48 |
Correct |
318 ms |
26188 KB |
Output is correct |
49 |
Correct |
330 ms |
26248 KB |
Output is correct |
50 |
Correct |
312 ms |
26248 KB |
Output is correct |