This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5000 + 7;
const int INF = (int) 1e18 + 7;
int n, dmax, a[N], dp[N], ndp[N];
signed main()
{
#ifdef ONPC
freopen ("input.txt", "r", stdin);
#endif // ONPC
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
cin >> n >> dmax;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
int big = *max_element(a + 1, a + n + 1);
if (*max_element(a + 1, a + n + 1) <= dmax)
{
cout << "0\n";
return 0;
}
assert(n < N && dmax < N);
for (int x = 0; x <= big; x++)
{
dp[x] = abs(x - a[1]);
}
for (int step = 2; step <= n; step++)
{
for (int x = 0; x <= big; x++)
{
ndp[x] = INF;
for (int y = 0; y <= big; y++)
{
if (abs(x - y) <= dmax)
{
ndp[x] = min(ndp[x], dp[y] + abs(a[step] - x));
}
}
}
for (int x = 0; x <= big; x++)
{
dp[x] = ndp[x];
}
}
int best = dp[0];
for (int x = 0; x <= big; x++)
{
best = min(best, dp[x]);
}
cout << best << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |