Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i, x, n) for (ll i = x; i < n; i++)
#define fl(i, x, n) for (ll i = n; i >= x; i--)
#define mod (ll)1e9 + 7
#define print(x) cout << x << endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) ::iterator it
#define MAX (ll)1e16
#define MIN (ll) - 1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
///////////   ///     ///   ///      ///   ////////////  ///      ///
///           ///     ///   ///      ///   ///      ///  ///      ///
///           ///     ///   ///      ///   ///      ///  ///      ///
///////////   ///////////   ///      ///   ////////////  ////////////
///   ///     ///   ///      ///   ///      ///  ///      ///
///   ///     ///   ///      ///   ///      ///  ///      ///
///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn(ll a, ll b) {
  if (a > b)
    return b;
  return a;
}
ll maxx2(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll maxx(ll a, ll b, ll c) { return maxx2(a, maxx2(b, c)); }
ll gcdd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcdd(b, a % b);
}
ll dsum(ll n) {
  ll sum = 0;
  while (n > 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
ll poww(ll a, ll b) {
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  else
    return poww(a, b / 2) * poww(a, b / 2) * poww(a, b % 2);
}
ll sumsq(ll x, ll y, ll z) {
  return (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x);
}
ll n, k, a[200001], dp[200001] = {0};
ll solve(ll i) {
  if (dp[i] != 1000000000)
    return dp[i];
  if (i <= 0) {
    return 0;
  }
  f(j, 1, k + 1) {
    if ((i - j) >= 0) {
      dp[i] = minn(dp[i], solve(i - j) + abs(a[i] - a[i - j]));
    }
  }
  return dp[i];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> k;
  f(i, 0, n) { cin >> a[i]; }
  f(i, 0, n) { dp[i] = 1000000000; }
  ll ans = solve(n - 1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i, x, n) for (ll i = x; i < n; i++)
#define fl(i, x, n) for (ll i = n; i >= x; i--)
#define mod (ll)1e9 + 7
#define print(x) cout << x << endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) ::iterator it
#define MAX (ll)1e16
#define MIN (ll) - 1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
///////////   ///     ///   ///      ///   ////////////  ///      ///
///           ///     ///   ///      ///   ///      ///  ///      ///
///           ///     ///   ///      ///   ///      ///  ///      ///
///////////   ///////////   ///      ///   ////////////  ////////////
///   ///     ///   ///      ///   ///      ///  ///      ///
///   ///     ///   ///      ///   ///      ///  ///      ///
///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn(ll a, ll b) {
  if (a > b)
    return b;
  return a;
}
ll maxx2(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll maxx(ll a, ll b, ll c) { return maxx2(a, maxx2(b, c)); }
ll gcdd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcdd(b, a % b);
}
ll dsum(ll n) {
  ll sum = 0;
  while (n > 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
ll poww(ll a, ll b) {
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  else
    return poww(a, b / 2) * poww(a, b / 2) * poww(a, b % 2);
}
ll sumsq(ll x, ll y, ll z) {
  return (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x);
}
ll n, k, a[200001], dp[200001] = {0};
ll solve(ll i) {
  if (dp[i] != 1000000000)
    return dp[i];
  if (i <= 0) {
    return 0;
  }
  f(j, 1, k + 1) {
    if ((i - j) >= 0) {
      dp[i] = minn(dp[i], solve(i - j) + abs(a[i] - a[i - j]));
    }
  }
  return dp[i];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  IOS;

  cin >> n >> k;
  f(i, 0, n) { cin >> a[i]; }
  f(i, 0, n) { dp[i] = 1000000000; }
  ll ans = solve(n - 1);
  cout << ans << endl;
  return 0;
}