Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
I_Love_Trueno
Author- darklight13
Smoke_Weed_Everyday
*/
#define _CRT_SECURE_NO_DEPRECATE
#define V__0_0__V
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define SYNC                                                                   \
  std::ios_base::sync_with_stdio(0);                                           \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define FRE                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define view(x) cout << (#x) << " is " << (x) << endl
#define view2(x, y)                                                            \
  cout << (#x) << " " << (#y) << " are " << (x) << " " << (y) << endl
#define pb push_back
#define F first
#define S second
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
#define all(v) (v).begin(), (v).end()
#define sz(a) (ll)((a).size())
#define sq(x) ((x) * (x))
#define pii pair<ll, ll>
#define endl '\n'
#define pow (ll) pow

int32_t main() {
  // FRE
  SYNC int n, k;
  cin >> n >> k;
  ll h[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  ll dp[n + 1];
  for (int i = 1; i <= k; i++) {
    dp[i] = abs(h[i] - h[1]);
  }
  for (int i = k + 1; i <= n; i++) {
    dp[i] = LLINF;
    for (int j = 1; j <= k; j++) {
      if (j >= i)
        break;
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
    if (dp[i] == LLINF)
      dp[i] = 0;
  }
  cout << dp[n];
  return 0;
}

Fixed submission:
/*
I_Love_Trueno
Author- darklight13
Smoke_Weed_Everyday
*/
#define _CRT_SECURE_NO_DEPRECATE
#define V__0_0__V
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define SYNC                                                                   \
  std::ios_base::sync_with_stdio(0);                                           \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define FRE                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define view(x) cout << (#x) << " is " << (x) << endl
#define view2(x, y)                                                            \
  cout << (#x) << " " << (#y) << " are " << (x) << " " << (y) << endl
#define pb push_back
#define F first
#define S second
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
#define all(v) (v).begin(), (v).end()
#define sz(a) (ll)((a).size())
#define sq(x) ((x) * (x))
#define pii pair<ll, ll>
#define endl '\n'
#define pow (ll) pow

int32_t main() {
  // FRE
  SYNC int n, k;
  cin >> n >> k;
  ll h[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  ll dp[n + 1];
  if (k >= n) {
    cout << abs(h[n] - h[1]);
    exit(0);
  }
  for (int i = 1; i <= k; i++) {
    dp[i] = abs(h[i] - h[1]);
  }
  for (int i = k + 1; i <= n; i++) {
    dp[i] = LLINF;
    for (int j = 1; j <= k; j++) {
      if (j >= i)
        break;
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
    if (dp[i] == LLINF)
      dp[i] = 0;
  }
  cout << dp[n];
  return 0;
}