#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1;
const ll oo = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
list<ll> lst;
struct el {
list<ll>::iterator it;
bool operator<(const el& o) const {
if(*it==*o.it) {
return it._M_node<o.it._M_node;
}
return *it < *o.it;
}
};
multiset<el> pq;
for(int i=0;i<n;++i) {
int a; cin >> a;
lst.push_back(a);
pq.insert({prev(lst.end())});
}
lst.push_back(-oo);
lst.push_front(-oo);
ll ans=0;
for(int i=1;i<=(n+1)/2;++i) {
auto e = *prev(pq.end());
pq.erase(prev(pq.end()));
ll v = *e.it;
ans+=v;
auto lit = prev(e.it);
auto rit = next(e.it);
pq.erase({lit});
pq.erase({rit});
lst.erase(e.it);
ll nv = *lit + *rit - v;
lst.erase(rit);
*lit=nv;
pq.insert({lit});
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
456 KB |
Output is correct |
7 |
Correct |
2 ms |
460 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 |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
508 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 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 |
2 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 |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
456 KB |
Output is correct |
7 |
Correct |
2 ms |
460 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 |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
508 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
308 ms |
19348 KB |
Output is correct |
22 |
Correct |
333 ms |
19380 KB |
Output is correct |
23 |
Correct |
315 ms |
19272 KB |
Output is correct |
24 |
Correct |
180 ms |
19220 KB |
Output is correct |
25 |
Correct |
145 ms |
19280 KB |
Output is correct |
26 |
Correct |
133 ms |
19236 KB |
Output is correct |
27 |
Correct |
184 ms |
19316 KB |
Output is correct |
28 |
Correct |
159 ms |
19384 KB |
Output is correct |
29 |
Correct |
151 ms |
19276 KB |
Output is correct |
30 |
Correct |
214 ms |
19364 KB |
Output is correct |
31 |
Correct |
168 ms |
19320 KB |
Output is correct |
32 |
Correct |
164 ms |
19356 KB |
Output is correct |
33 |
Correct |
232 ms |
19136 KB |
Output is correct |
34 |
Correct |
258 ms |
19104 KB |
Output is correct |
35 |
Correct |
225 ms |
19228 KB |
Output is correct |
36 |
Correct |
312 ms |
19352 KB |
Output is correct |
37 |
Correct |
317 ms |
19284 KB |
Output is correct |
38 |
Correct |
318 ms |
19344 KB |
Output is correct |
39 |
Correct |
138 ms |
19224 KB |
Output is correct |
40 |
Correct |
143 ms |
19268 KB |
Output is correct |
41 |
Correct |
172 ms |
19196 KB |
Output is correct |
42 |
Correct |
150 ms |
19276 KB |
Output is correct |
43 |
Correct |
149 ms |
19336 KB |
Output is correct |
44 |
Correct |
187 ms |
19376 KB |
Output is correct |
45 |
Correct |
158 ms |
19276 KB |
Output is correct |
46 |
Correct |
160 ms |
19348 KB |
Output is correct |
47 |
Correct |
164 ms |
19360 KB |
Output is correct |
48 |
Correct |
227 ms |
19060 KB |
Output is correct |
49 |
Correct |
247 ms |
19148 KB |
Output is correct |
50 |
Correct |
233 ms |
19080 KB |
Output is correct |