#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[200010];
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 |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 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 |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
856 KB |
Output is correct |
2 |
Correct |
31 ms |
1144 KB |
Output is correct |
3 |
Correct |
28 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 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 |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
21 ms |
21084 KB |
Output is correct |
14 |
Correct |
21 ms |
21112 KB |
Output is correct |
15 |
Correct |
19 ms |
21080 KB |
Output is correct |
16 |
Correct |
22 ms |
21080 KB |
Output is correct |
17 |
Correct |
19 ms |
19036 KB |
Output is correct |
18 |
Correct |
17 ms |
16988 KB |
Output is correct |
19 |
Correct |
21 ms |
21104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 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 |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
21 ms |
21084 KB |
Output is correct |
14 |
Correct |
21 ms |
21112 KB |
Output is correct |
15 |
Correct |
19 ms |
21080 KB |
Output is correct |
16 |
Correct |
22 ms |
21080 KB |
Output is correct |
17 |
Correct |
19 ms |
19036 KB |
Output is correct |
18 |
Correct |
17 ms |
16988 KB |
Output is correct |
19 |
Correct |
21 ms |
21104 KB |
Output is correct |
20 |
Correct |
21 ms |
19036 KB |
Output is correct |
21 |
Correct |
28 ms |
21084 KB |
Output is correct |
22 |
Correct |
19 ms |
19036 KB |
Output is correct |
23 |
Correct |
14 ms |
16988 KB |
Output is correct |
24 |
Correct |
19 ms |
19032 KB |
Output is correct |
25 |
Correct |
22 ms |
16988 KB |
Output is correct |
26 |
Correct |
22 ms |
21084 KB |
Output is correct |
27 |
Correct |
22 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 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 |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
21 ms |
21084 KB |
Output is correct |
14 |
Correct |
21 ms |
21112 KB |
Output is correct |
15 |
Correct |
19 ms |
21080 KB |
Output is correct |
16 |
Correct |
22 ms |
21080 KB |
Output is correct |
17 |
Correct |
19 ms |
19036 KB |
Output is correct |
18 |
Correct |
17 ms |
16988 KB |
Output is correct |
19 |
Correct |
21 ms |
21104 KB |
Output is correct |
20 |
Correct |
21 ms |
19036 KB |
Output is correct |
21 |
Correct |
28 ms |
21084 KB |
Output is correct |
22 |
Correct |
19 ms |
19036 KB |
Output is correct |
23 |
Correct |
14 ms |
16988 KB |
Output is correct |
24 |
Correct |
19 ms |
19032 KB |
Output is correct |
25 |
Correct |
22 ms |
16988 KB |
Output is correct |
26 |
Correct |
22 ms |
21084 KB |
Output is correct |
27 |
Correct |
22 ms |
19036 KB |
Output is correct |
28 |
Correct |
216 ms |
201608 KB |
Output is correct |
29 |
Correct |
187 ms |
168628 KB |
Output is correct |
30 |
Correct |
201 ms |
197568 KB |
Output is correct |
31 |
Correct |
160 ms |
164736 KB |
Output is correct |
32 |
Correct |
177 ms |
164764 KB |
Output is correct |
33 |
Correct |
209 ms |
201556 KB |
Output is correct |
34 |
Correct |
174 ms |
197608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 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 |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Incorrect |
0 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 |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 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 |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |