Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define MOD 1000000007
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define oa(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";                                                       \
  nl
#define cn(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
  cin >> a[i]
#define ov(a)                                                                  \
  for (ll i = 0; i < a.size(); i++)                                            \
    cout << a[i] << " ";                                                       \
  nl
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("outpu.txt", "w", stdout);
#endif
  ll N, k;
  cin >> N >> k;
  ll ar[N];
  cn(ar, N);
  ll g[N];
  g[0] = 0;
  for (ll i = 1; i < N; i++) {
    ll m = abs(ar[i] - ar[i - 1]) + g[i - 1];
    for (int j = 2; j <= min(k, i); j++) {
      m = min(m, abs(ar[i] - ar[i - j]) + g[i - j]);
    }
    g[i] = m;
  }
  cout << g[N - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define MOD 1000000007
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define oa(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";                                                       \
  nl
#define cn(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
  cin >> a[i]
#define ov(a)                                                                  \
  for (ll i = 0; i < a.size(); i++)                                            \
    cout << a[i] << " ";                                                       \
  nl
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
int main() {
  fastio();
  ll N, k;
  cin >> N >> k;
  ll ar[N];
  cn(ar, N);
  ll g[N];
  g[0] = 0;
  for (ll i = 1; i < N; i++) {
    ll m = abs(ar[i] - ar[i - 1]) + g[i - 1];
    for (int j = 2; j <= min(k, i); j++) {
      m = min(m, abs(ar[i] - ar[i - j]) + g[i - j]);
    }
    g[i] = m;
  }
  cout << g[N - 1] << endl;
}