Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int INF = INT_MAX; // 2147483647
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
#ifdef LOCAL_ENV
#define debug(var) std::cout << #var " = " << var << std::endl
#else
#define debug(var)
#endif
#define p(var) std::cout << var << std::endl
#define PI (acos(-1))
#define rep(i, n) for (int i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (int i = 1, i##_length = (n); i <= i##_length; ++i)
#define all(a) (a).begin(), (a).end()
#define pb push_back
inline double isnatural(double a) { return a >= 0 && ceil(a) == floor(a); }
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (int i = 0, len = v.size(); i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  for (int i = 0, len = vv.size(); i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

/*-----8<-----8<-----*/

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> h(n + 1, 0);
  repeq(i, n) { cin >> h[i]; }

  vector<int> dp(n + 1, 0);
  dp[2] = abs(h[1] - h[2]);
  for (int i = 3; i <= n; i++) {
    int minval = 99999999;
    for (int j = 1; j <= k; j++) {
      chmin(minval, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = minval;
  }
  p(dp[n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int INF = INT_MAX; // 2147483647
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
#ifdef LOCAL_ENV
#define debug(var) std::cout << #var " = " << var << std::endl
#else
#define debug(var)
#endif
#define p(var) std::cout << var << std::endl
#define PI (acos(-1))
#define rep(i, n) for (int i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (int i = 1, i##_length = (n); i <= i##_length; ++i)
#define all(a) (a).begin(), (a).end()
#define pb push_back
inline double isnatural(double a) { return a >= 0 && ceil(a) == floor(a); }
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (int i = 0, len = v.size(); i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  for (int i = 0, len = vv.size(); i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

/*-----8<-----8<-----*/

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> h(n + 1, 0);
  repeq(i, n) { cin >> h[i]; }

  vector<int> dp(n + 1, 0);
  dp[2] = abs(h[1] - h[2]);
  for (int i = 3; i <= n; i++) {
    int minval = INF;
    for (int j = 1; j <= k && i - j >= 1; j++) {
      chmin(minval, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = minval;
  }
  p(dp[n]);
  return 0;
}