Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define pb push_back
#define digit(x) floor(log10(x)) + 1
#define mod 1000000007
#define L_MAX 1e18
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using iii = tuple<ll, ll, ll>;
using matrix = vector<vector<ll>>;
using arr = vector<ll>;
using vs = vector<string>;
using pv = vector<ii>;
using umap = unordered_map<ll, ll>;
#define in1(x) scanf("%lld", &x)
#define in2(x, y) scanf("%lld %lld", &x, &y)
#define in3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << endl;
void showArr(arr a) {
  for (auto i : a)
    cout << i << " ";
  cout << endl;
}
ll dx[] = {-1, 1, -1, 1, 0, 0, -1, 1};
ll dy[] = {-1, 1, 0, 0, -1, 1, 1, -1};
ll Solve(ll index, arr a, ll n, ll k, ll dp[]) {
  if (index >= n - 1)
    return 0;
  ll ans = L_MAX;
  if (dp[index] != -1)
    return dp[index];
  for (int i = 1; i <= k; i++) {
    if (index + i < n) {
      if (dp[index + i] == -1)
        dp[index + i] = Solve(index + i, a, n, k, dp);
      ans = min(ans, abs(a[index] - a[index + i]) + dp[index + i]);
    }
  }
  return dp[index] = ans;
}
int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  arr a;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }
  ll dp[100009];
  memset(dp, -1, sizeof(dp));
  cout << Solve(0, a, n, k, dp) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define pb push_back
#define digit(x) floor(log10(x)) + 1
#define mod 1000000007
#define L_MAX 1e18
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using iii = tuple<ll, ll, ll>;
using matrix = vector<vector<ll>>;
using arr = vector<ll>;
using vs = vector<string>;
using pv = vector<ii>;
using umap = unordered_map<ll, ll>;
#define in1(x) scanf("%lld", &x)
#define in2(x, y) scanf("%lld %lld", &x, &y)
#define in3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << endl;
void showArr(arr a) {
  for (auto i : a)
    cout << i << " ";
  cout << endl;
}
ll dx[] = {-1, 1, -1, 1, 0, 0, -1, 1};
ll dy[] = {-1, 1, 0, 0, -1, 1, 1, -1};
ll Solve(ll index, arr a, ll n, ll k, ll dp[]) {
  if (index >= n - 1)
    return 0;
  ll ans = L_MAX;
  if (dp[index] != -1)
    return dp[index];
  for (int i = 1; i <= k; i++) {
    if (index + i < n) {
      if (dp[index + i] == -1)
        dp[index + i] = Solve(index + i, a, n, k, dp);
      ans = min(ans, abs(a[index] - a[index + i]) + dp[index + i]);
    }
  }
  return dp[index] = ans;
}
int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  arr a;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }
  /* ll dp[100009];
   memset(dp,-1,sizeof(dp));
   cout<<Solve(0,a,n,k,dp)<<endl;*/
  ll dp[1000009];
  dp[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = L_MAX;
    for (int j = 1; j <= k; j++) {
      if (i + j <= n - 1) {
        dp[i] = min(dp[i], dp[i + j] + abs(a[i] - a[i + j]));
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}