#include <bits/stdc++.h>
#define eb emplace_back
#define em emplace
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int MAX = 303030;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
struct Node {
priority_queue <ll> L;
priority_queue <ll, vector <ll>, greater <ll> > R;
ll ans = 0, s = 0;
void init(int x) {L.em(x), R.em(x);}
int size() {return (int)(L.size() + R.size());}
void merge(Node &X) {
ans += X.ans;
while(!X.L.empty()) {
ll temp = X.L.top() - X.s;
X.L.pop();
if(temp >= R.top() + s){
ans += temp - R.top() - s;
L.em(R.top() + 2 * s); R.pop();
R.em(temp - s);
}
else L.em(temp + s);
}
while(!X.R.empty()) {
ll temp = X.R.top() + X.s;
X.R.pop();
if(temp <= L.top() - s){
ans += L.top() - temp - s;
R.em(L.top() - 2 * s); L.pop();
L.em(temp + s);
}
else R.em(temp - s);
}
}
} dp[MAX];
int p[MAX], idx[MAX];
ll l[MAX];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n;
ll h;
cin >> n >> h;
for(int i = 1; i <= n; i++) {
cin >> l[i];
dp[i].init(l[i]);
idx[i] = i;
}
for(int i = 2; i <= n; i++) {
p[i] = i - 1;
}
for(int i = n; i > 1; i--) {
dp[idx[i]].s += h;
if(dp[idx[i]].size() < dp[idx[p[i]]].size()) {
dp[idx[p[i]]].merge(dp[idx[i]]);
}
else {
dp[idx[i]].merge(dp[idx[p[i]]]);
idx[p[i]] = idx[i];
}
}
cout << dp[idx[1]].ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24060 KB |
Output is correct |
6 |
Correct |
25 ms |
24056 KB |
Output is correct |
7 |
Correct |
24 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
25 ms |
24120 KB |
Output is correct |
3 |
Correct |
25 ms |
24084 KB |
Output is correct |
4 |
Correct |
24 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
25 ms |
24056 KB |
Output is correct |
3 |
Correct |
25 ms |
24056 KB |
Output is correct |
4 |
Correct |
26 ms |
24188 KB |
Output is correct |
5 |
Correct |
25 ms |
24056 KB |
Output is correct |
6 |
Correct |
26 ms |
24060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
38884 KB |
Output is correct |
2 |
Correct |
127 ms |
43844 KB |
Output is correct |
3 |
Correct |
131 ms |
44496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24060 KB |
Output is correct |
6 |
Correct |
25 ms |
24056 KB |
Output is correct |
7 |
Correct |
24 ms |
24056 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24120 KB |
Output is correct |
10 |
Correct |
25 ms |
24084 KB |
Output is correct |
11 |
Correct |
24 ms |
24056 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
26 ms |
24056 KB |
Output is correct |
14 |
Correct |
25 ms |
24156 KB |
Output is correct |
15 |
Correct |
25 ms |
24184 KB |
Output is correct |
16 |
Correct |
26 ms |
24184 KB |
Output is correct |
17 |
Correct |
26 ms |
24184 KB |
Output is correct |
18 |
Correct |
25 ms |
24184 KB |
Output is correct |
19 |
Correct |
25 ms |
24084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24060 KB |
Output is correct |
6 |
Correct |
25 ms |
24056 KB |
Output is correct |
7 |
Correct |
24 ms |
24056 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24120 KB |
Output is correct |
10 |
Correct |
25 ms |
24084 KB |
Output is correct |
11 |
Correct |
24 ms |
24056 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
26 ms |
24056 KB |
Output is correct |
14 |
Correct |
25 ms |
24156 KB |
Output is correct |
15 |
Correct |
25 ms |
24184 KB |
Output is correct |
16 |
Correct |
26 ms |
24184 KB |
Output is correct |
17 |
Correct |
26 ms |
24184 KB |
Output is correct |
18 |
Correct |
25 ms |
24184 KB |
Output is correct |
19 |
Correct |
25 ms |
24084 KB |
Output is correct |
20 |
Correct |
26 ms |
24184 KB |
Output is correct |
21 |
Correct |
26 ms |
24172 KB |
Output is correct |
22 |
Correct |
26 ms |
24184 KB |
Output is correct |
23 |
Correct |
25 ms |
24184 KB |
Output is correct |
24 |
Correct |
25 ms |
24184 KB |
Output is correct |
25 |
Correct |
25 ms |
24184 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
25 ms |
24156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24060 KB |
Output is correct |
6 |
Correct |
25 ms |
24056 KB |
Output is correct |
7 |
Correct |
24 ms |
24056 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24120 KB |
Output is correct |
10 |
Correct |
25 ms |
24084 KB |
Output is correct |
11 |
Correct |
24 ms |
24056 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
26 ms |
24056 KB |
Output is correct |
14 |
Correct |
25 ms |
24156 KB |
Output is correct |
15 |
Correct |
25 ms |
24184 KB |
Output is correct |
16 |
Correct |
26 ms |
24184 KB |
Output is correct |
17 |
Correct |
26 ms |
24184 KB |
Output is correct |
18 |
Correct |
25 ms |
24184 KB |
Output is correct |
19 |
Correct |
25 ms |
24084 KB |
Output is correct |
20 |
Correct |
26 ms |
24184 KB |
Output is correct |
21 |
Correct |
26 ms |
24172 KB |
Output is correct |
22 |
Correct |
26 ms |
24184 KB |
Output is correct |
23 |
Correct |
25 ms |
24184 KB |
Output is correct |
24 |
Correct |
25 ms |
24184 KB |
Output is correct |
25 |
Correct |
25 ms |
24184 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
25 ms |
24156 KB |
Output is correct |
28 |
Correct |
28 ms |
24696 KB |
Output is correct |
29 |
Correct |
28 ms |
24672 KB |
Output is correct |
30 |
Correct |
29 ms |
24696 KB |
Output is correct |
31 |
Correct |
27 ms |
24568 KB |
Output is correct |
32 |
Correct |
27 ms |
24568 KB |
Output is correct |
33 |
Correct |
28 ms |
24676 KB |
Output is correct |
34 |
Correct |
28 ms |
24696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24060 KB |
Output is correct |
6 |
Correct |
25 ms |
24056 KB |
Output is correct |
7 |
Correct |
24 ms |
24056 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24120 KB |
Output is correct |
10 |
Correct |
25 ms |
24084 KB |
Output is correct |
11 |
Correct |
24 ms |
24056 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
25 ms |
24056 KB |
Output is correct |
14 |
Correct |
25 ms |
24056 KB |
Output is correct |
15 |
Correct |
25 ms |
24056 KB |
Output is correct |
16 |
Correct |
26 ms |
24188 KB |
Output is correct |
17 |
Correct |
25 ms |
24056 KB |
Output is correct |
18 |
Correct |
26 ms |
24060 KB |
Output is correct |
19 |
Correct |
26 ms |
24056 KB |
Output is correct |
20 |
Correct |
25 ms |
24156 KB |
Output is correct |
21 |
Correct |
25 ms |
24184 KB |
Output is correct |
22 |
Correct |
26 ms |
24184 KB |
Output is correct |
23 |
Correct |
26 ms |
24184 KB |
Output is correct |
24 |
Correct |
25 ms |
24184 KB |
Output is correct |
25 |
Correct |
25 ms |
24084 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
26 ms |
24172 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
25 ms |
24184 KB |
Output is correct |
30 |
Correct |
25 ms |
24184 KB |
Output is correct |
31 |
Correct |
25 ms |
24184 KB |
Output is correct |
32 |
Correct |
26 ms |
24184 KB |
Output is correct |
33 |
Correct |
25 ms |
24156 KB |
Output is correct |
34 |
Correct |
28 ms |
24696 KB |
Output is correct |
35 |
Correct |
28 ms |
24672 KB |
Output is correct |
36 |
Correct |
29 ms |
24696 KB |
Output is correct |
37 |
Correct |
27 ms |
24568 KB |
Output is correct |
38 |
Correct |
27 ms |
24568 KB |
Output is correct |
39 |
Correct |
28 ms |
24676 KB |
Output is correct |
40 |
Correct |
28 ms |
24696 KB |
Output is correct |
41 |
Correct |
28 ms |
24652 KB |
Output is correct |
42 |
Correct |
27 ms |
24440 KB |
Output is correct |
43 |
Correct |
28 ms |
24596 KB |
Output is correct |
44 |
Correct |
27 ms |
24696 KB |
Output is correct |
45 |
Correct |
28 ms |
24668 KB |
Output is correct |
46 |
Correct |
27 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
24 ms |
24056 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
5 |
Correct |
25 ms |
24060 KB |
Output is correct |
6 |
Correct |
25 ms |
24056 KB |
Output is correct |
7 |
Correct |
24 ms |
24056 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24120 KB |
Output is correct |
10 |
Correct |
25 ms |
24084 KB |
Output is correct |
11 |
Correct |
24 ms |
24056 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
25 ms |
24056 KB |
Output is correct |
14 |
Correct |
25 ms |
24056 KB |
Output is correct |
15 |
Correct |
25 ms |
24056 KB |
Output is correct |
16 |
Correct |
26 ms |
24188 KB |
Output is correct |
17 |
Correct |
25 ms |
24056 KB |
Output is correct |
18 |
Correct |
26 ms |
24060 KB |
Output is correct |
19 |
Correct |
97 ms |
38884 KB |
Output is correct |
20 |
Correct |
127 ms |
43844 KB |
Output is correct |
21 |
Correct |
131 ms |
44496 KB |
Output is correct |
22 |
Correct |
26 ms |
24056 KB |
Output is correct |
23 |
Correct |
25 ms |
24156 KB |
Output is correct |
24 |
Correct |
25 ms |
24184 KB |
Output is correct |
25 |
Correct |
26 ms |
24184 KB |
Output is correct |
26 |
Correct |
26 ms |
24184 KB |
Output is correct |
27 |
Correct |
25 ms |
24184 KB |
Output is correct |
28 |
Correct |
25 ms |
24084 KB |
Output is correct |
29 |
Correct |
26 ms |
24184 KB |
Output is correct |
30 |
Correct |
26 ms |
24172 KB |
Output is correct |
31 |
Correct |
26 ms |
24184 KB |
Output is correct |
32 |
Correct |
25 ms |
24184 KB |
Output is correct |
33 |
Correct |
25 ms |
24184 KB |
Output is correct |
34 |
Correct |
25 ms |
24184 KB |
Output is correct |
35 |
Correct |
26 ms |
24184 KB |
Output is correct |
36 |
Correct |
25 ms |
24156 KB |
Output is correct |
37 |
Correct |
28 ms |
24696 KB |
Output is correct |
38 |
Correct |
28 ms |
24672 KB |
Output is correct |
39 |
Correct |
29 ms |
24696 KB |
Output is correct |
40 |
Correct |
27 ms |
24568 KB |
Output is correct |
41 |
Correct |
27 ms |
24568 KB |
Output is correct |
42 |
Correct |
28 ms |
24676 KB |
Output is correct |
43 |
Correct |
28 ms |
24696 KB |
Output is correct |
44 |
Correct |
28 ms |
24652 KB |
Output is correct |
45 |
Correct |
27 ms |
24440 KB |
Output is correct |
46 |
Correct |
28 ms |
24596 KB |
Output is correct |
47 |
Correct |
27 ms |
24696 KB |
Output is correct |
48 |
Correct |
28 ms |
24668 KB |
Output is correct |
49 |
Correct |
27 ms |
24568 KB |
Output is correct |
50 |
Correct |
133 ms |
45060 KB |
Output is correct |
51 |
Correct |
114 ms |
42140 KB |
Output is correct |
52 |
Correct |
112 ms |
41312 KB |
Output is correct |
53 |
Correct |
127 ms |
43712 KB |
Output is correct |
54 |
Correct |
133 ms |
44256 KB |
Output is correct |
55 |
Correct |
116 ms |
41668 KB |
Output is correct |
56 |
Correct |
124 ms |
43168 KB |
Output is correct |
57 |
Correct |
114 ms |
41756 KB |
Output is correct |
58 |
Correct |
107 ms |
39392 KB |
Output is correct |
59 |
Correct |
125 ms |
43360 KB |
Output is correct |