Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*https://atcoder.jp/contests/dp/tasks/dp_b*/
#include <bits/stdc++.h>
#include <sstream>
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl "\n"
#define pi 2 * acos(0.0)
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define repe(i, a, b) for (ll i = a; i <= b; ++i)
#define CPrint(c)                                                              \
  for (auto i : (c)) {                                                         \
    cout << i << " ";                                                          \
  }                                                                            \
  nl
#define eb emplace_back
#define ef emplace_front
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define mp make_pair
#define sz(a) int((a).size())
#define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
// Allows to traverse any type of macro
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
// debuggers
#define trace2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl
#define trace3(x, y, z)                                                        \
  cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z     \
       << endl
#define trace4(w, x, y, z)                                                     \
  cout << #w << "=" << w << " " << #x << "=" << x << " " << #y << "=" << y     \
       << " " << #z << "=" << z << endl
typedef long long ll;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
const int INF = 1e9 + 5;
int main() {
  FAST_IO;
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n, INF);
  dp[0] = 0; // base case
  vector<ll> h(n);
  rep(i, 0, n) { cin >> h[i]; }
  /* i+1.. i+k abs(h[i]-h[j])
   */
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= i + k; j++) {
      dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
/*https://atcoder.jp/contests/dp/tasks/dp_b*/
#include <bits/stdc++.h>
#include <sstream>
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl "\n"
#define pi 2 * acos(0.0)
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define repe(i, a, b) for (ll i = a; i <= b; ++i)
#define CPrint(c)                                                              \
  for (auto i : (c)) {                                                         \
    cout << i << " ";                                                          \
  }                                                                            \
  nl
#define eb emplace_back
#define ef emplace_front
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define mp make_pair
#define sz(a) int((a).size())
#define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
// Allows to traverse any type of macro
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
// debuggers
#define trace2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl
#define trace3(x, y, z)                                                        \
  cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z     \
       << endl
#define trace4(w, x, y, z)                                                     \
  cout << #w << "=" << w << " " << #x << "=" << x << " " << #y << "=" << y     \
       << " " << #z << "=" << z << endl
typedef long long ll;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
const int INF = 1e9 + 5;
int main() {
  FAST_IO;
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n, INF);
  dp[0] = 0; // base case
  vector<ll> h(n);
  rep(i, 0, n) { cin >> h[i]; }
  /* i+1.. i+k abs(h[i]-h[j])
   */
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= i + k && j < n; j++) {
      dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;
}