#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ss second
#define ff first
#define pb push_back
#define pii pair<int, int>
#define INF INT_MAX
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef AKIKO_DEBUG
#define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll MOD = 1e9 + 7;
const int N = 5e3 + 1;
int n, h;
vector<ll> a;
void go () {
sort(a.begin(), a.end());
vector<ll> pres(n);
for(int i = 0; i < n; i++) {
if(i == 0) {
pres[i] = a[i];
} else {
pres[i] = a[i] + pres[i - 1];
}
}
ll ans = INT64_MAX;
for(ll i = 0; i < n; i++) {
ll x = a[i];
ll temp = (x * i) - (i > 0 ? pres[i - 1] : 0);
ll sum = pres[n - 1] - (i > 0 ? pres[i - 1] : 0);
temp += sum - (n - i) * x;
ans = min(ans, temp);
}
cout << ans << "\n";
}
int main() {
cin >> n >> h;
a.resize(n);
h = min (h, N);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
if(h == 0) {
go();
return 0;
}
vector<int> prev(N), cur(N);
for(int i = 0; i < N; i++) {
prev[i] = abs(a[0] - i);
}
for(int i = 1; i < n; i++) {
set<pii> s;
for(int j = 0; j <= h; j++) {
s.insert({prev[j], j});
}
for(int j = 0; j < N; j++) {
cur[j] = (*s.begin()).ff + abs(a[i] - j);
if(j >= h)
s.erase({prev[j - h], j - h});
if(j + h + 1 < N)
s.insert({prev[j + h + 1], j + h + 1});
}
prev = cur;
}
int ans = INF;
for(int i = 0; i < N; i++) {
ans = min(ans, cur[i]);
}
cout << ans << "\n";
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
2336 KB |
Output is correct |
2 |
Correct |
84 ms |
3160 KB |
Output is correct |
3 |
Correct |
94 ms |
3224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Correct |
222 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
224 ms |
548 KB |
Output is correct |
16 |
Correct |
284 ms |
348 KB |
Output is correct |
17 |
Correct |
275 ms |
460 KB |
Output is correct |
18 |
Correct |
221 ms |
340 KB |
Output is correct |
19 |
Correct |
160 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Correct |
222 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
224 ms |
548 KB |
Output is correct |
16 |
Correct |
284 ms |
348 KB |
Output is correct |
17 |
Correct |
275 ms |
460 KB |
Output is correct |
18 |
Correct |
221 ms |
340 KB |
Output is correct |
19 |
Correct |
160 ms |
320 KB |
Output is correct |
20 |
Correct |
237 ms |
548 KB |
Output is correct |
21 |
Correct |
314 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
168 ms |
552 KB |
Output is correct |
24 |
Correct |
283 ms |
484 KB |
Output is correct |
25 |
Correct |
201 ms |
552 KB |
Output is correct |
26 |
Correct |
292 ms |
340 KB |
Output is correct |
27 |
Correct |
141 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Correct |
222 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
224 ms |
548 KB |
Output is correct |
16 |
Correct |
284 ms |
348 KB |
Output is correct |
17 |
Correct |
275 ms |
460 KB |
Output is correct |
18 |
Correct |
221 ms |
340 KB |
Output is correct |
19 |
Correct |
160 ms |
320 KB |
Output is correct |
20 |
Correct |
237 ms |
548 KB |
Output is correct |
21 |
Correct |
314 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
168 ms |
552 KB |
Output is correct |
24 |
Correct |
283 ms |
484 KB |
Output is correct |
25 |
Correct |
201 ms |
552 KB |
Output is correct |
26 |
Correct |
292 ms |
340 KB |
Output is correct |
27 |
Correct |
141 ms |
332 KB |
Output is correct |
28 |
Correct |
1500 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Execution timed out |
2065 ms |
468 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |