Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fr(i, s, e) for (i = s; i < e; i++)
#define rf(i, s, e) for (i = s - 1; i >= e; i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
// #define int   long long
#define ll long long
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define vl vector<long long>
#define inf (1LL << 61)
#define vi vector<int>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define viii vector<vector<vector<int>>>
typedef pair<pair<int, int>, int> P;
#define cd complex<double>
#define PI 4 * atan(1)
#define mod % 1000000007
#define pi 3.14159265358979323846 // 20 decimals
#define ld long double

int main() {
  FAST;
  ll n, i, k, j;
  cin >> n >> k;
  ll h[n];
  fr(i, 0, n) cin >> h[i];
  ll dp[n];
  fr(i, 0, n) dp[i] = 1000000000000000;
  dp[0] = 0;
  fr(i, 1, k) dp[i] = abs(h[i] - h[0]);
  fr(i, k, n) {
    fr(j, 1, k + 1) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
  }
  cout << dp[n - 1];
}


Fixed submission:
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fr(i, s, e) for (i = s; i < e; i++)
#define rf(i, s, e) for (i = s - 1; i >= e; i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
// #define int   long long
#define ll long long
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define vl vector<long long>
#define inf (1LL << 61)
#define vi vector<int>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define viii vector<vector<vector<int>>>
typedef pair<pair<int, int>, int> P;
#define cd complex<double>
#define PI 4 * atan(1)
#define mod % 1000000007
#define pi 3.14159265358979323846 // 20 decimals
#define ld long double

int main() {
  FAST;
  ll n, i, k, j;
  cin >> n >> k;
  ll h[n];
  fr(i, 0, n) cin >> h[i];
  ll dp[n];
  fr(i, 0, n) dp[i] = 1000000000000000;
  dp[0] = 0;
  fr(i, 1, min(n, k + 1)) dp[i] = abs(h[i] - h[0]);
  fr(i, k + 1, n) {
    if (i >= n)
      break;
    fr(j, 1, k + 1) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
  }
  cout << dp[n - 1];
}