#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define derr if(1) cerr
typedef vector<int> vi;
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, pii> state;
int l[300000];
int torhs[300000];
int tolhs[300000];
int n, k;
set<state> cands;
map<pii, ll> rev;
void ins(pii link, ll cost) {
rev[link] = cost;
cands.insert(state(cost, link));
}
ll erase(pii link) {
if(!rev.count(link)) return -1e18;
ll cost = rev[link];
if(cands.erase(state(cost, link)) == 1) {
return cost;
}
return -1e18;
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) cin >> l[i];
for(int i = 1; i <= n; i++) {
ins({i-1, i}, -l[i]);
torhs[i-1] = i;
tolhs[i] = i-1;
}
k = (n+1)/2;
ll ret = 0;
while(k--) {
state cand = *cands.begin();
ret -= cand.first;
cands.erase(cand);
int nlhs = tolhs[cand.second.first];
int nrhs = torhs[cand.second.second];
vector<ll> v;
v.push_back(erase({nlhs, cand.second.first}));
v.push_back(erase({cand.second.second, nrhs}));
if(min(v[0], v[1]) > -1e18) {
ins({nlhs, nrhs}, (v[0] - (cand.first) + v[1]));
torhs[nlhs] = nrhs;
tolhs[nrhs] = nlhs;
}
cout << ret << "\n"; cout << flush;
}
}
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
640 KB |
Output is correct |
2 |
Correct |
9 ms |
640 KB |
Output is correct |
3 |
Correct |
9 ms |
768 KB |
Output is correct |
4 |
Correct |
9 ms |
768 KB |
Output is correct |
5 |
Correct |
9 ms |
640 KB |
Output is correct |
6 |
Correct |
9 ms |
640 KB |
Output is correct |
7 |
Correct |
9 ms |
640 KB |
Output is correct |
8 |
Correct |
9 ms |
640 KB |
Output is correct |
9 |
Correct |
9 ms |
640 KB |
Output is correct |
10 |
Correct |
9 ms |
768 KB |
Output is correct |
11 |
Correct |
9 ms |
768 KB |
Output is correct |
12 |
Correct |
9 ms |
768 KB |
Output is correct |
13 |
Correct |
9 ms |
640 KB |
Output is correct |
14 |
Correct |
9 ms |
640 KB |
Output is correct |
15 |
Correct |
9 ms |
640 KB |
Output is correct |
16 |
Correct |
9 ms |
768 KB |
Output is correct |
17 |
Correct |
9 ms |
640 KB |
Output is correct |
18 |
Correct |
9 ms |
640 KB |
Output is correct |
19 |
Correct |
11 ms |
768 KB |
Output is correct |
20 |
Correct |
9 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
640 KB |
Output is correct |
2 |
Correct |
9 ms |
640 KB |
Output is correct |
3 |
Correct |
9 ms |
768 KB |
Output is correct |
4 |
Correct |
9 ms |
768 KB |
Output is correct |
5 |
Correct |
9 ms |
640 KB |
Output is correct |
6 |
Correct |
9 ms |
640 KB |
Output is correct |
7 |
Correct |
9 ms |
640 KB |
Output is correct |
8 |
Correct |
9 ms |
640 KB |
Output is correct |
9 |
Correct |
9 ms |
640 KB |
Output is correct |
10 |
Correct |
9 ms |
768 KB |
Output is correct |
11 |
Correct |
9 ms |
768 KB |
Output is correct |
12 |
Correct |
9 ms |
768 KB |
Output is correct |
13 |
Correct |
9 ms |
640 KB |
Output is correct |
14 |
Correct |
9 ms |
640 KB |
Output is correct |
15 |
Correct |
9 ms |
640 KB |
Output is correct |
16 |
Correct |
9 ms |
768 KB |
Output is correct |
17 |
Correct |
9 ms |
640 KB |
Output is correct |
18 |
Correct |
9 ms |
640 KB |
Output is correct |
19 |
Correct |
11 ms |
768 KB |
Output is correct |
20 |
Correct |
9 ms |
640 KB |
Output is correct |
21 |
Correct |
951 ms |
31224 KB |
Output is correct |
22 |
Correct |
901 ms |
31204 KB |
Output is correct |
23 |
Correct |
953 ms |
31424 KB |
Output is correct |
24 |
Correct |
550 ms |
30972 KB |
Output is correct |
25 |
Correct |
565 ms |
31224 KB |
Output is correct |
26 |
Correct |
555 ms |
31072 KB |
Output is correct |
27 |
Correct |
551 ms |
31228 KB |
Output is correct |
28 |
Correct |
554 ms |
31352 KB |
Output is correct |
29 |
Correct |
584 ms |
31352 KB |
Output is correct |
30 |
Correct |
580 ms |
31156 KB |
Output is correct |
31 |
Correct |
580 ms |
31224 KB |
Output is correct |
32 |
Correct |
598 ms |
31224 KB |
Output is correct |
33 |
Correct |
714 ms |
31096 KB |
Output is correct |
34 |
Correct |
740 ms |
30936 KB |
Output is correct |
35 |
Correct |
767 ms |
31096 KB |
Output is correct |
36 |
Correct |
929 ms |
31484 KB |
Output is correct |
37 |
Correct |
983 ms |
31300 KB |
Output is correct |
38 |
Correct |
916 ms |
31352 KB |
Output is correct |
39 |
Correct |
534 ms |
31132 KB |
Output is correct |
40 |
Correct |
573 ms |
31096 KB |
Output is correct |
41 |
Correct |
566 ms |
31096 KB |
Output is correct |
42 |
Correct |
553 ms |
31300 KB |
Output is correct |
43 |
Correct |
597 ms |
31248 KB |
Output is correct |
44 |
Correct |
571 ms |
31356 KB |
Output is correct |
45 |
Correct |
569 ms |
31260 KB |
Output is correct |
46 |
Correct |
581 ms |
31352 KB |
Output is correct |
47 |
Correct |
591 ms |
31204 KB |
Output is correct |
48 |
Correct |
732 ms |
31224 KB |
Output is correct |
49 |
Correct |
698 ms |
31096 KB |
Output is correct |
50 |
Correct |
716 ms |
31100 KB |
Output is correct |