Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*      ###########################
        #  Author : Pranay Garg   #
        #  College : SGSITS       #
        ###########################
*/
#include <bits/stdc++.h>
#define ll long long int
#define ironman                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define MOD 1000000007
#define endl '\n'
using namespace std;
void fastio() {
  ironman
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
ll parent[100005];
ll find(ll a) {
  std::vector<ll> v;
  while (parent[a] > 0) {
    v.push_back(a);
    a = parent[a];
  }
  for (ll i = 0; i < v.size(); i++)
    parent[v[i]] = a;
  return a;
}
bool prime[100005];
void SieveOfEratosthenes(int n) {
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
void union1(ll a, ll b) {
  a = find(a);
  b = find(b);
  if (a != b) {
    parent[a] = min(parent[a], parent[b]);
    parent[b] = a;
  }
}
ll n, k;
ll arr[100002];
ll dp[100002];
int main() {
  memset(dp, -1, sizeof(dp));
  fastio();
  // ll n;
  cin >> n >> k;
  //    ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  dp[0] = 0;
  for (ll i = 1; i < n; i++) {
    if (i - 1 >= 0)
      dp[i] = dp[i - 1] + abs(arr[i] - arr[i - 1]);
    for (ll j = i - 2; j >= 0 && i - j <= k; j--) {
      dp[i] = min(dp[i], dp[j] + abs(arr[i] - arr[j]));
    }
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
/*      ###########################
        #  Author : Pranay Garg   #
        #  College : SGSITS       #
        ###########################
*/
#include <bits/stdc++.h>
#define ll long long int
#define ironman                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define MOD 1000000007
#define endl '\n'
using namespace std;
void fastio() {
  ironman
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
ll parent[100005];
ll find(ll a) {
  std::vector<ll> v;
  while (parent[a] > 0) {
    v.push_back(a);
    a = parent[a];
  }
  for (ll i = 0; i < v.size(); i++)
    parent[v[i]] = a;
  return a;
}
bool prime[100005];
void SieveOfEratosthenes(int n) {
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
void union1(ll a, ll b) {
  a = find(a);
  b = find(b);
  if (a != b) {
    parent[a] = min(parent[a], parent[b]);
    parent[b] = a;
  }
}
ll n, k;
ll arr[100002];
ll dp[100002];
int main() {
  memset(dp, -1, sizeof(dp));
  // fastio();
  // ll n;
  cin >> n >> k;
  //    ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  dp[0] = 0;
  for (ll i = 1; i < n; i++) {
    if (i - 1 >= 0)
      dp[i] = dp[i - 1] + abs(arr[i] - arr[i - 1]);
    for (ll j = i - 2; j >= 0 && i - j <= k; j--) {
      dp[i] = min(dp[i], dp[j] + abs(arr[i] - arr[j]));
    }
  }
  cout << dp[n - 1] << endl;
}