Problem p03161 (C++)

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

#define mod 1000000000
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vs vector<string>
#define psi pair<string, ll>
#define pii pair<ll, ll>
#define vsi vector<psi>
#define vii vector<pii>
#define mp make_pair
#define pb push_back
#define rep(n) for (long i = 0; i < n; i++)
#define repr(n) for (long i = (n - 1); i >= 0; i--)
#define fori(i, a, b) for (auto i = a; i <= b; i++)
#define ford(i, b, a) for (auto i = b; i >= a; i--)
#define mset(a, i) memset(a, i, sizeof(a))
#define minv(a) min_element(a.begin(), a.end())
#define maxv(a) max_element(a.begin(), a.end())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define sotit(a) sort(a.begin(), a.end());
#define sot(a) sort(a, a + n);
#define ff first
#define maxheap(v) priority_queue<v>
#define minheap(v) priority_queue<v, vector<v>, greater<v>>
#define ss second
#define endl "\n"
#define NL cout << endl;
#define printmat(a, m, n)                                                      \
  for (int i = 0; i < m; i++) {                                                \
    for (int j = 0; j < n; j++) {                                              \
      cout << a[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }
#define dbgn(x) cout << " $ " << x << " $ " << endl;
#define dbg(x) cout << " $ " << x << " $ ";
#define all(v) v.begin(), v.end()
#define len(s) (ll)(s.size())
#define show(a, n)                                                             \
  for (int i = 0; i < n; i++) {                                                \
    cout << a[i] << " ";                                                       \
  }                                                                            \
  NL
#define fast_io()                                                              \
  cin.sync_with_stdio(false);                                                  \
  cout.sync_with_stdio(false);                                                 \
  cin.tie(NULL);

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val

int main() {
  fast_io()

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    ll h[n], dp[n];
    rep(n) { cin >> h[i]; }
    mset(dp, 0);
    dp[1] = abs(h[1] - h[0]);
    ll ans;
    fori(i, 2, n - 1) {
      ans = LLONG_MAX;
      fori(j, 1, min((ll)i, k)) {
        if (i - j >= 0)
          ans = min(ans, dp[i - j] + abs(h[i] - h[i - j]));
      }
      dp[i] = ans;
    }
    cout << dp[n - 1];
  }
  return 0;
}

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

#define mod 1000000000
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vs vector<string>
#define psi pair<string, ll>
#define pii pair<ll, ll>
#define vsi vector<psi>
#define vii vector<pii>
#define mp make_pair
#define pb push_back
#define rep(n) for (long i = 0; i < n; i++)
#define repr(n) for (long i = (n - 1); i >= 0; i--)
#define fori(i, a, b) for (auto i = a; i <= b; i++)
#define ford(i, b, a) for (auto i = b; i >= a; i--)
#define mset(a, i) memset(a, i, sizeof(a))
#define minv(a) min_element(a.begin(), a.end())
#define maxv(a) max_element(a.begin(), a.end())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define sotit(a) sort(a.begin(), a.end());
#define sot(a) sort(a, a + n);
#define ff first
#define maxheap(v) priority_queue<v>
#define minheap(v) priority_queue<v, vector<v>, greater<v>>
#define ss second
#define endl "\n"
#define NL cout << endl;
#define printmat(a, m, n)                                                      \
  for (int i = 0; i < m; i++) {                                                \
    for (int j = 0; j < n; j++) {                                              \
      cout << a[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }
#define dbgn(x) cout << " $ " << x << " $ " << endl;
#define dbg(x) cout << " $ " << x << " $ ";
#define all(v) v.begin(), v.end()
#define len(s) (ll)(s.size())
#define show(a, n)                                                             \
  for (int i = 0; i < n; i++) {                                                \
    cout << a[i] << " ";                                                       \
  }                                                                            \
  NL
#define fast_io()                                                              \
  cin.sync_with_stdio(false);                                                  \
  cout.sync_with_stdio(false);                                                 \
  cin.tie(NULL);

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val

int main() {
  fast_io()

      ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    ll h[n], dp[n];
    rep(n) { cin >> h[i]; }
    mset(dp, 0);
    dp[1] = abs(h[1] - h[0]);
    ll ans;
    fori(i, 2, n - 1) {
      ans = LLONG_MAX;
      fori(j, 1, min((ll)i, k)) {
        if (i - j >= 0)
          ans = min(ans, dp[i - j] + abs(h[i] - h[i - j]));
      }
      dp[i] = ans;
    }
    cout << dp[n - 1];
  }
  return 0;
}