#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
const int N = (int)(1e6) + 5;
ll s[N];
int lf[N];
int rg[N];
ll lv[N];
ll rv[N];
int main(void)
{
int n;
cin>>n;
n += 2;
s[1] = -1e16;
s[n] = -1e16;
for (int i = 2;i < n;++i)
cin>>s[i];
for (int i = 0;i <= n;++i) {
lf[i] = rg[i] = -1;
lv[i] = rv[i] = -1e16;
if (i > 0)
lf[i] = i - 1,lv[i] = s[i];
if (i < n)
rg[i] = i + 1,rv[i] = s[i + 1];
}
priority_queue < pair < ll , pii > > Q;
for (int i = 1;i <= n;++i)
Q.push(mp(s[i],mp(i - 1,i)));
ll answer = 0;
n -= 2;
for (int cs = 1;cs * 2 <= n + 1;++cs) {
const int u = Q.top().se.fi;
const int v = Q.top().se.se;
const ll cost = Q.top().fi;
Q.pop();
if (lf[v] != u || rg[u] != v) {
--cs;
continue;
}
answer += cost;
int L = lf[u];
int R = rg[v];
lf[u] = rg[v] = -1;
if (L != -1 && R != -1) {
const ll new_cost = lv[u] + rv[v] - cost;
lf[R] = L;rg[L] = R;
rv[L] = lv[R] = new_cost;
Q.push(mp(new_cost,mp(L,R)));
}
cout << answer << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
508 KB |
Output is correct |
2 |
Correct |
3 ms |
612 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
4 ms |
732 KB |
Output is correct |
6 |
Correct |
4 ms |
892 KB |
Output is correct |
7 |
Correct |
4 ms |
892 KB |
Output is correct |
8 |
Correct |
3 ms |
892 KB |
Output is correct |
9 |
Correct |
3 ms |
908 KB |
Output is correct |
10 |
Correct |
4 ms |
908 KB |
Output is correct |
11 |
Correct |
4 ms |
908 KB |
Output is correct |
12 |
Correct |
3 ms |
908 KB |
Output is correct |
13 |
Correct |
4 ms |
908 KB |
Output is correct |
14 |
Correct |
4 ms |
908 KB |
Output is correct |
15 |
Correct |
3 ms |
908 KB |
Output is correct |
16 |
Correct |
4 ms |
908 KB |
Output is correct |
17 |
Correct |
3 ms |
908 KB |
Output is correct |
18 |
Correct |
4 ms |
908 KB |
Output is correct |
19 |
Correct |
4 ms |
908 KB |
Output is correct |
20 |
Correct |
5 ms |
908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
508 KB |
Output is correct |
2 |
Correct |
3 ms |
612 KB |
Output is correct |
3 |
Correct |
4 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
4 ms |
732 KB |
Output is correct |
6 |
Correct |
4 ms |
892 KB |
Output is correct |
7 |
Correct |
4 ms |
892 KB |
Output is correct |
8 |
Correct |
3 ms |
892 KB |
Output is correct |
9 |
Correct |
3 ms |
908 KB |
Output is correct |
10 |
Correct |
4 ms |
908 KB |
Output is correct |
11 |
Correct |
4 ms |
908 KB |
Output is correct |
12 |
Correct |
3 ms |
908 KB |
Output is correct |
13 |
Correct |
4 ms |
908 KB |
Output is correct |
14 |
Correct |
4 ms |
908 KB |
Output is correct |
15 |
Correct |
3 ms |
908 KB |
Output is correct |
16 |
Correct |
4 ms |
908 KB |
Output is correct |
17 |
Correct |
3 ms |
908 KB |
Output is correct |
18 |
Correct |
4 ms |
908 KB |
Output is correct |
19 |
Correct |
4 ms |
908 KB |
Output is correct |
20 |
Correct |
5 ms |
908 KB |
Output is correct |
21 |
Correct |
257 ms |
11924 KB |
Output is correct |
22 |
Correct |
247 ms |
11924 KB |
Output is correct |
23 |
Correct |
248 ms |
11924 KB |
Output is correct |
24 |
Correct |
198 ms |
11924 KB |
Output is correct |
25 |
Correct |
175 ms |
11924 KB |
Output is correct |
26 |
Correct |
175 ms |
11924 KB |
Output is correct |
27 |
Correct |
246 ms |
11924 KB |
Output is correct |
28 |
Correct |
196 ms |
11924 KB |
Output is correct |
29 |
Correct |
252 ms |
11924 KB |
Output is correct |
30 |
Correct |
222 ms |
11924 KB |
Output is correct |
31 |
Correct |
209 ms |
11924 KB |
Output is correct |
32 |
Correct |
212 ms |
11924 KB |
Output is correct |
33 |
Correct |
196 ms |
11924 KB |
Output is correct |
34 |
Correct |
220 ms |
11924 KB |
Output is correct |
35 |
Correct |
208 ms |
11924 KB |
Output is correct |
36 |
Correct |
235 ms |
11924 KB |
Output is correct |
37 |
Correct |
235 ms |
11924 KB |
Output is correct |
38 |
Correct |
225 ms |
11924 KB |
Output is correct |
39 |
Correct |
191 ms |
11924 KB |
Output is correct |
40 |
Correct |
203 ms |
11924 KB |
Output is correct |
41 |
Correct |
230 ms |
11924 KB |
Output is correct |
42 |
Correct |
243 ms |
11924 KB |
Output is correct |
43 |
Correct |
290 ms |
11924 KB |
Output is correct |
44 |
Correct |
213 ms |
11924 KB |
Output is correct |
45 |
Correct |
200 ms |
11924 KB |
Output is correct |
46 |
Correct |
199 ms |
11924 KB |
Output is correct |
47 |
Correct |
196 ms |
11924 KB |
Output is correct |
48 |
Correct |
197 ms |
11924 KB |
Output is correct |
49 |
Correct |
200 ms |
11924 KB |
Output is correct |
50 |
Correct |
213 ms |
11924 KB |
Output is correct |