#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
multiset<int> local_minimum(multiset<int> s, int h) {
assert((int) s.size() % 2 == 0);
multiset<int> s2;
int slope = -(int) s.size() / 2;
for (auto &x : s) {
if (slope < 0) {
s2.insert(x - h);
}
if (slope >= 0) {
s2.insert(x + h);
}
slope++;
}
return s2;
}
multiset<int> form(multiset<int> s1, multiset<int> s2, int add1, int add2) {
multiset<int> s;
for (auto &x : s1) {
s.insert(x + add1);
}
for (auto &x : s2) {
s.insert(x + add2);
}
return s;
}
void print(multiset<int> s) {
cout << " : ";
for (auto &x : s) {
cout << x << " ";
}
cout << "\n";
}
int get(multiset<int> s, int a, int b) {
int n = (int) s.size();
vector<int> xs;
for (auto &it : s) {
xs.push_back(it);
}
for (int i = n - 1; i >= 0; i--) {
b += xs[i];
a--;
if (a == 0) {
return b;
}
}
assert(0);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
multiset<int> s, s1, s2;
int n, h, x, b = 0, add1 = 0, add2 = 0;
cin >> n >> h >> x;
s.insert(x);
s.insert(x);
s1.insert(x);
s2.insert(x);
assert(s == form(s1, s2, add1, add2));
b = -x;
b -= h * (n - 1) * n / 2;
for (int i = 2; i <= n; i++) {
s = local_minimum(s, h);
add1 -= h;
add2 += h;
assert(s == form(s1, s2, add1, add2));
int cut = *s2.begin() + add2;
int x;
cin >> x;
b -= x;
s.insert(x);
s.insert(x);
if (x < cut) {
s1.insert(x - add1);
s1.insert(x - add1);
auto it = s1.end();
it--;
int val = *it + add1;
s1.erase(it);
s2.insert(val - add2);
} else {
s2.insert(x - add2);
s2.insert(x - add2);
auto it = s2.begin();
int val = *it + add2;
s2.erase(it);
s1.insert(val - add1);
}
assert(s == form(s1, s2, add1, add2));
}
cout << get(s, (int) s.size() / 2, b) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
392 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2076 ms |
1424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
104 ms |
492 KB |
Output is correct |
14 |
Correct |
97 ms |
492 KB |
Output is correct |
15 |
Correct |
93 ms |
492 KB |
Output is correct |
16 |
Correct |
107 ms |
492 KB |
Output is correct |
17 |
Correct |
80 ms |
492 KB |
Output is correct |
18 |
Correct |
55 ms |
492 KB |
Output is correct |
19 |
Correct |
95 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
104 ms |
492 KB |
Output is correct |
14 |
Correct |
97 ms |
492 KB |
Output is correct |
15 |
Correct |
93 ms |
492 KB |
Output is correct |
16 |
Correct |
107 ms |
492 KB |
Output is correct |
17 |
Correct |
80 ms |
492 KB |
Output is correct |
18 |
Correct |
55 ms |
492 KB |
Output is correct |
19 |
Correct |
95 ms |
492 KB |
Output is correct |
20 |
Correct |
88 ms |
492 KB |
Output is correct |
21 |
Correct |
102 ms |
660 KB |
Output is correct |
22 |
Correct |
72 ms |
620 KB |
Output is correct |
23 |
Correct |
57 ms |
492 KB |
Output is correct |
24 |
Correct |
86 ms |
492 KB |
Output is correct |
25 |
Correct |
60 ms |
492 KB |
Output is correct |
26 |
Correct |
103 ms |
492 KB |
Output is correct |
27 |
Correct |
90 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
104 ms |
492 KB |
Output is correct |
14 |
Correct |
97 ms |
492 KB |
Output is correct |
15 |
Correct |
93 ms |
492 KB |
Output is correct |
16 |
Correct |
107 ms |
492 KB |
Output is correct |
17 |
Correct |
80 ms |
492 KB |
Output is correct |
18 |
Correct |
55 ms |
492 KB |
Output is correct |
19 |
Correct |
95 ms |
492 KB |
Output is correct |
20 |
Correct |
88 ms |
492 KB |
Output is correct |
21 |
Correct |
102 ms |
660 KB |
Output is correct |
22 |
Correct |
72 ms |
620 KB |
Output is correct |
23 |
Correct |
57 ms |
492 KB |
Output is correct |
24 |
Correct |
86 ms |
492 KB |
Output is correct |
25 |
Correct |
60 ms |
492 KB |
Output is correct |
26 |
Correct |
103 ms |
492 KB |
Output is correct |
27 |
Correct |
90 ms |
492 KB |
Output is correct |
28 |
Execution timed out |
2062 ms |
1132 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
392 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
104 ms |
492 KB |
Output is correct |
20 |
Correct |
97 ms |
492 KB |
Output is correct |
21 |
Correct |
93 ms |
492 KB |
Output is correct |
22 |
Correct |
107 ms |
492 KB |
Output is correct |
23 |
Correct |
80 ms |
492 KB |
Output is correct |
24 |
Correct |
55 ms |
492 KB |
Output is correct |
25 |
Correct |
95 ms |
492 KB |
Output is correct |
26 |
Correct |
88 ms |
492 KB |
Output is correct |
27 |
Correct |
102 ms |
660 KB |
Output is correct |
28 |
Correct |
72 ms |
620 KB |
Output is correct |
29 |
Correct |
57 ms |
492 KB |
Output is correct |
30 |
Correct |
86 ms |
492 KB |
Output is correct |
31 |
Correct |
60 ms |
492 KB |
Output is correct |
32 |
Correct |
103 ms |
492 KB |
Output is correct |
33 |
Correct |
90 ms |
492 KB |
Output is correct |
34 |
Execution timed out |
2062 ms |
1132 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
392 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Execution timed out |
2076 ms |
1424 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |