#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int n, h;
vector <ll> s;
vector <vector <ll>> dp;
ll solve(int i, int prev) {
if (i > n)
return 0;
if (dp[i][prev] != -1)
return dp[i][prev];
ll ans = 1e10;
for (int j = max(0, prev - h); j <= min(prev + h, 800); j++)
ans = min(ans, solve(i+1, j) + abs(j - s[i]));
dp[i][prev] = ans;
return ans;
}
int main()
{
cin >> n >> h;
s = vector <ll> (n+1);
for (int i = 1; i <= n; i++)
cin >> s[i];
int maxs = *max_element(s.begin(), s.end());
dp = vector <vector <ll>> (n+1, vector <ll> (800 + 1, -1));
ll ans = 1e10;
for (int i = 0; i <= 800; i++) {
dp = vector <vector <ll>> (n+1, vector <ll> (800 + 1, -1));
ans = min(ans, solve(1, i));
}
cout << ans << '\n';
}
Compilation message
safety.cpp: In function 'int main()':
safety.cpp:26:9: warning: unused variable 'maxs' [-Wunused-variable]
26 | int maxs = *max_element(s.begin(), s.end());
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
138 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2058 ms |
348 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |