Submission #668394

#TimeUsernameProblemLanguageResultExecution timeMemory
668394600MihneaSafety (NOI18_safety)C++17
13 / 100
2076 ms724 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...