/**
* author: fractal
* timus: 288481RF
**/
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define sz(x) (int)x.size()
#define len(x) (int)strlen(x)
#define all(x) x.begin(), x.end()
#define debug cerr << "OK\n";
#define ub upper_bound
#define lb lower_bound
#define nl printf("\n");
#define clbuff fflush(stdin);
#define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef set<int> si;
typedef set<ll> sll;
typedef set<pii> spii;
typedef set<pll> spll;
typedef multiset <int> msi;
typedef multiset <ll> msll;
typedef map <int, int> mi;
typedef map <ll, ll> mll;
const int N = 2e5 + 2;
const int M = 1e5;
const int mod = 0;
const int inf = 2e9 + 3;
const ll INF = 1e11;
const ld pi2 = 2.0 * 3.141592;
const ld pi = 3.141592;
const ld eps = 1e-12;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
void files(string s = "main") {
#ifndef PC
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
#endif
}
int add(int a, int b) {
if (a + b < 0) return a + b + mod;
if (a + b >= mod) return a + b - mod;
return a + b;
}
int mul(int a, int b) {
return a * 1LL * b % mod;
}
int binpow(int a, int n) {
int ret = 1;
while (n) {
if (n & 1) ret = mul(ret, a);
a = mul(a, a);
n >>= 1;
}
return ret;
}
ll ans = 0, n, h, a[N];
vector<pll> v, u;
int main() {
speed;
cin >> n >> h;
for (int i = 1; i <= n; ++i)
cin >> a[i];
if (h == 0) {
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i)
ans += abs(a[i] - a[(n + 1) / 2]);
cout << ans << '\n';
return 0;
}
if (n > 5000) {
cout << -1 << '\n';
return 0;
}
v.pb({-INF, -1});
v.pb({a[1], 1});
ans = 0;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j < sz(v) - 1; ++j) {
u.pb(v[j]);
if (v[j].S < 0 && v[j + 1].S > 0) {
u.pb({v[j + 1].F, 0});
}
}
u.pb(v.back());
v = u;
u.clear();
for (auto &it : v)
it.F += (it.S <= 0 ? -h : h);
for (int j = 0; j < sz(v); ++j) {
if (v[j].S == 0) {
if (a[i] < v[j].F)
ans += v[j].F - a[i];
if (a[i] > v[j + 1].F)
ans += a[i] - v[j + 1].F;
}
}
for (int j = 0; j < sz(v); ++j) {
if (v[j].F < a[i]) {
if (sz(u) == 0 || u.back().S != v[j].S - 1) {
if (sz(u) && u.back().F == v[j].F)
u.ppb();
u.pb({v[j].F, v[j].S - 1});
}
if (j + 1 < sz(v) && v[j + 1].F > a[i]) {
if (sz(u) && u.back().F == a[i])
u.ppb();
if (sz(u) == 0 || u.back().S != v[j].S + 1)
u.pb({a[i], v[j].S + 1});
}
else if (j + 1 == sz(v)) {
if (sz(u) && u.back().F == a[i])
u.ppb();
if (sz(u) == 0 || u.back().S != v[j].S + 1)
u.pb({a[i], v[j].S + 1});
}
}
else {
if (sz(u) && u.back().F == v[j].F)
u.ppb();
if (sz(u) == 0 || u.back().S != v[j].S + 1)
u.pb({v[j].F, v[j].S + 1});
}
}
v = u;
u.clear();
}
cout << ans << '\n';
}
Compilation message
safety.cpp: In function 'int mul(int, int)':
safety.cpp:74:21: warning: division by zero [-Wdiv-by-zero]
74 | return a * 1LL * b % mod;
| ~~~~~~~~~~~~^~~~~
safety.cpp: In function 'void files(std::string)':
safety.cpp:62:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:63:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
1572 KB |
Output is correct |
2 |
Correct |
40 ms |
3552 KB |
Output is correct |
3 |
Correct |
42 ms |
3720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
5 ms |
412 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
332 KB |
Output is correct |
18 |
Correct |
4 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
5 ms |
412 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
332 KB |
Output is correct |
18 |
Correct |
4 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
5 ms |
332 KB |
Output is correct |
21 |
Correct |
5 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
3 ms |
332 KB |
Output is correct |
25 |
Correct |
4 ms |
332 KB |
Output is correct |
26 |
Correct |
3 ms |
388 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
5 ms |
412 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
4 ms |
332 KB |
Output is correct |
18 |
Correct |
4 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
5 ms |
332 KB |
Output is correct |
21 |
Correct |
5 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
3 ms |
332 KB |
Output is correct |
25 |
Correct |
4 ms |
332 KB |
Output is correct |
26 |
Correct |
3 ms |
388 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
28 |
Correct |
218 ms |
656 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
283 ms |
776 KB |
Output is correct |
31 |
Correct |
340 ms |
888 KB |
Output is correct |
32 |
Correct |
337 ms |
1632 KB |
Output is correct |
33 |
Correct |
241 ms |
836 KB |
Output is correct |
34 |
Correct |
506 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
5 ms |
412 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
4 ms |
332 KB |
Output is correct |
24 |
Correct |
4 ms |
332 KB |
Output is correct |
25 |
Correct |
3 ms |
332 KB |
Output is correct |
26 |
Correct |
5 ms |
332 KB |
Output is correct |
27 |
Correct |
5 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
3 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
332 KB |
Output is correct |
31 |
Correct |
4 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
388 KB |
Output is correct |
33 |
Correct |
3 ms |
332 KB |
Output is correct |
34 |
Correct |
218 ms |
656 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
283 ms |
776 KB |
Output is correct |
37 |
Correct |
340 ms |
888 KB |
Output is correct |
38 |
Correct |
337 ms |
1632 KB |
Output is correct |
39 |
Correct |
241 ms |
836 KB |
Output is correct |
40 |
Correct |
506 ms |
1016 KB |
Output is correct |
41 |
Correct |
254 ms |
704 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
426 ms |
940 KB |
Output is correct |
44 |
Correct |
278 ms |
764 KB |
Output is correct |
45 |
Correct |
430 ms |
1024 KB |
Output is correct |
46 |
Correct |
392 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
27 ms |
1572 KB |
Output is correct |
20 |
Correct |
40 ms |
3552 KB |
Output is correct |
21 |
Correct |
42 ms |
3720 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
5 ms |
412 KB |
Output is correct |
25 |
Correct |
3 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
332 KB |
Output is correct |
27 |
Correct |
4 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
332 KB |
Output is correct |
29 |
Correct |
5 ms |
332 KB |
Output is correct |
30 |
Correct |
5 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
3 ms |
332 KB |
Output is correct |
33 |
Correct |
3 ms |
332 KB |
Output is correct |
34 |
Correct |
4 ms |
332 KB |
Output is correct |
35 |
Correct |
3 ms |
388 KB |
Output is correct |
36 |
Correct |
3 ms |
332 KB |
Output is correct |
37 |
Correct |
218 ms |
656 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
283 ms |
776 KB |
Output is correct |
40 |
Correct |
340 ms |
888 KB |
Output is correct |
41 |
Correct |
337 ms |
1632 KB |
Output is correct |
42 |
Correct |
241 ms |
836 KB |
Output is correct |
43 |
Correct |
506 ms |
1016 KB |
Output is correct |
44 |
Correct |
254 ms |
704 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
426 ms |
940 KB |
Output is correct |
47 |
Correct |
278 ms |
764 KB |
Output is correct |
48 |
Correct |
430 ms |
1024 KB |
Output is correct |
49 |
Correct |
392 ms |
896 KB |
Output is correct |
50 |
Incorrect |
25 ms |
3796 KB |
Output isn't correct |
51 |
Halted |
0 ms |
0 KB |
- |