Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;

/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; ++i)
    os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  fio;
  int tcases = 1;
  // cin>>testcases;
  for (int tcase = 1; tcase <= tcases; ++tcase) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n, mod);
    FOR(i, 0, n) cin >> a[i];
    vector<int> dp(n, mod);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < min(i + k + 1, n); ++j) {
        dp[j] = min(dp[j], abs(a[j] - a[i]) + dp[i]);
      }
    }
    cout << dp[n - 1] << endl;

    // cout<<"Case #"<<tcase<<": ";
  }
  return 0;
}

// Functions

int fpow(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}


Fixed submission:
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;

/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; ++i)
    os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);

int32_t main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt" , "r" , stdin);freopen("output.txt" , "w" ,
// stdout);freopen("error.txt" , "w" , stderr);
#endif
  fio;
  int tcases = 1;
  // cin>>testcases;
  for (int tcase = 1; tcase <= tcases; ++tcase) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n, mod);
    FOR(i, 0, n) cin >> a[i];
    vector<int> dp(n, mod);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < min(i + k + 1, n); ++j) {
        dp[j] = min(dp[j], abs(a[j] - a[i]) + dp[i]);
      }
    }
    cout << dp[n - 1] << endl;

    // cout<<"Case #"<<tcase<<": ";
  }
  return 0;
}

// Functions

int fpow(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}