#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 5e3 + 100;
const ll INF = (ll)1e18 + 100;
const int inf = 1e9 + 100;
const int MOD = 1e9 + 7;
const int maxl = 14;
int n, h;
int a[maxn];
ll dp[maxn][maxn];
void test(){
cin >> n >> h;
int mx = 0;
for(int i = 1; i <= n ;i++){
cin >> a[i];
mx = max(mx, a[i]);
}
if(max(mx, n) <= 5000){
for(int x = 0; x <= 5000; x++){
dp[1][x] = abs(x - a[1]);
}
for(int i = 2; i <= n; i++){
deque<ll> d;
for(int x = 0, l = 0, r = -1; x <= 5000; x++){
while(abs(r - x + 1) <= h && r < 5000){
r++;
while(d.size() && d.back() > dp[i-1][r]){
d.pop_back();
}
d.push_back(dp[i-1][r]);
}
while(abs(l - x) > h){
if(d.front() == dp[i-1][l]){
d.pop_front();
}
l++;
}
dp[i][x] = d.front() + abs(x - a[i]);
}
}
sort(dp[n], dp[n] + 5000 + 1);
cout << dp[n][0];
} else if(!h){
sort(a + 1, a + n + 1);
ll x = 0, y = 0;
for(int i = 1; i <= n; i++){
y += a[i];
}
ll ans = INF;
for(int i = 1; i <= n; i++){
x += a[i];
ans = min(ans, 1ll * a[i] * i - x + y - x - 1ll * a[i] * (n - i));
}
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; t = 1;
while(t--) test();
cout << ent;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2528 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2528 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
24 ms |
21072 KB |
Output is correct |
14 |
Correct |
22 ms |
21084 KB |
Output is correct |
15 |
Correct |
20 ms |
21148 KB |
Output is correct |
16 |
Correct |
22 ms |
21096 KB |
Output is correct |
17 |
Correct |
25 ms |
19068 KB |
Output is correct |
18 |
Correct |
17 ms |
17024 KB |
Output is correct |
19 |
Correct |
21 ms |
21120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2528 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
24 ms |
21072 KB |
Output is correct |
14 |
Correct |
22 ms |
21084 KB |
Output is correct |
15 |
Correct |
20 ms |
21148 KB |
Output is correct |
16 |
Correct |
22 ms |
21096 KB |
Output is correct |
17 |
Correct |
25 ms |
19068 KB |
Output is correct |
18 |
Correct |
17 ms |
17024 KB |
Output is correct |
19 |
Correct |
21 ms |
21120 KB |
Output is correct |
20 |
Correct |
21 ms |
19036 KB |
Output is correct |
21 |
Correct |
35 ms |
21108 KB |
Output is correct |
22 |
Correct |
20 ms |
19056 KB |
Output is correct |
23 |
Correct |
18 ms |
17140 KB |
Output is correct |
24 |
Correct |
20 ms |
19096 KB |
Output is correct |
25 |
Correct |
16 ms |
16988 KB |
Output is correct |
26 |
Correct |
21 ms |
20868 KB |
Output is correct |
27 |
Correct |
20 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2528 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
24 ms |
21072 KB |
Output is correct |
14 |
Correct |
22 ms |
21084 KB |
Output is correct |
15 |
Correct |
20 ms |
21148 KB |
Output is correct |
16 |
Correct |
22 ms |
21096 KB |
Output is correct |
17 |
Correct |
25 ms |
19068 KB |
Output is correct |
18 |
Correct |
17 ms |
17024 KB |
Output is correct |
19 |
Correct |
21 ms |
21120 KB |
Output is correct |
20 |
Correct |
21 ms |
19036 KB |
Output is correct |
21 |
Correct |
35 ms |
21108 KB |
Output is correct |
22 |
Correct |
20 ms |
19056 KB |
Output is correct |
23 |
Correct |
18 ms |
17140 KB |
Output is correct |
24 |
Correct |
20 ms |
19096 KB |
Output is correct |
25 |
Correct |
16 ms |
16988 KB |
Output is correct |
26 |
Correct |
21 ms |
20868 KB |
Output is correct |
27 |
Correct |
20 ms |
19032 KB |
Output is correct |
28 |
Correct |
243 ms |
201448 KB |
Output is correct |
29 |
Correct |
202 ms |
168704 KB |
Output is correct |
30 |
Correct |
232 ms |
197572 KB |
Output is correct |
31 |
Correct |
171 ms |
164784 KB |
Output is correct |
32 |
Correct |
200 ms |
164704 KB |
Output is correct |
33 |
Correct |
248 ms |
201700 KB |
Output is correct |
34 |
Correct |
231 ms |
197588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2528 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2528 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |