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 fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (ull i = start; i < end; i++)
#define N 100001
#define all(v) v.begin(), v.end()
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  nl
int main() {
  fastio();

  ll n, k;
  cin >> n >> k;
  vector<ll> v(n), dp(n + 1, 0);
  loop(i, 0, n) cin >> v[i];

  for (int i = 1; i < n; i++) {
    dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]);
    for (int j = 2; i >= j && j <= k; j++)
      dp[i] = min(dp[i], dp[i - k] + abs(v[i] - v[i - k]));
  }
  cout << dp[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (ull i = start; i < end; i++)
#define N 100001
#define all(v) v.begin(), v.end()
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  nl
int main() {
  fastio();

  ll n, k;
  cin >> n >> k;
  vector<ll> v(n), dp(n + 1, 0);
  loop(i, 0, n) cin >> v[i];

  for (int i = 1; i < n; i++) {
    dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]);
    for (int j = 2; i >= j && j <= k; j++)
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
  }
  cout << dp[n - 1];

  return 0;
}