Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define hell 998244353
#define hell1 1000000006
#define pb push_back
#define x first
#define y second
#define MAXL 100005
#define pll pair<ll, ll>
#define pii pair<int, int>
#define L LLONG_MAX
#define all(v) (v).begin(), (v).end()
#define tcFile                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
using namespace std;
// vector<int>prime(15000005,0);
ll mM(ll a, ll b, ll p = hell) {
  ll res = 0;
  if (a >= p)
    a %= p;
  while (b) {
    if (b & 1ll)
      res = (res + a);
    if (res >= p)
      res %= p;
    a = (2 * a);
    if (a >= p)
      a %= p;
    b >>= 1ll;
  }
  return res;
}
ll fpow(ll x, ll y, ll p = hell) {
  if (x >= p)
    x = x % p;
  ll res = 1;
  while (y) {
    if (y & 1ll)
      res *= x;
    if (res < 0)
      res += p;
    if (res >= p)
      res %= p;
    y = y >> 1ll;
    x *= x;
    if (x < 0)
      x += p;
    if (x >= p)
      x %= p;
  }
  return res;
}
ll inv(ll n, ll p = hell) { return fpow(n, p - 2, p); }
ll modmul(ll x, ll y, ll p = hell) {
  if (x < 0)
    x += p;
  if (x >= p)
    x %= p;
  if (y < 0)
    y += p;
  if (y >= p)
    y %= p;
  return (x * y) % p;
}
ll modadd(ll x, ll y, ll p = hell) {
  if (x < 0)
    x += p;
  if (x >= p)
    x %= p;
  if (y < 0)
    y += p;
  if (y >= p)
    y %= p;
  ll temp = x + y;
  if (temp >= p)
    temp %= p;
  return temp;
}
/*void sieve(){
    prime[0] = -1;
    prime[1] = 0;
    for(int i=2;i<=15000000;i++)
        if(!prime[i])
            for(int j=i;j<=15000000;j+=i)
                if(!prime[j])
                        prime[j] = i;
}*/
int main() {
  ios int t = 1, tc = 0;
  // cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n), dp(n, INT_MAX);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    dp[0] = 0;
    for (int i = 1; i < n; i++)
      for (int j = 1; j <= k; j++)
        dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
    cout << dp[n - 1] << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define hell 998244353
#define hell1 1000000006
#define pb push_back
#define x first
#define y second
#define MAXL 100005
#define pll pair<ll, ll>
#define pii pair<int, int>
#define L LLONG_MAX
#define all(v) (v).begin(), (v).end()
#define tcFile                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
using namespace std;
// vector<int>prime(15000005,0);
ll mM(ll a, ll b, ll p = hell) {
  ll res = 0;
  if (a >= p)
    a %= p;
  while (b) {
    if (b & 1ll)
      res = (res + a);
    if (res >= p)
      res %= p;
    a = (2 * a);
    if (a >= p)
      a %= p;
    b >>= 1ll;
  }
  return res;
}
ll fpow(ll x, ll y, ll p = hell) {
  if (x >= p)
    x = x % p;
  ll res = 1;
  while (y) {
    if (y & 1ll)
      res *= x;
    if (res < 0)
      res += p;
    if (res >= p)
      res %= p;
    y = y >> 1ll;
    x *= x;
    if (x < 0)
      x += p;
    if (x >= p)
      x %= p;
  }
  return res;
}
ll inv(ll n, ll p = hell) { return fpow(n, p - 2, p); }
ll modmul(ll x, ll y, ll p = hell) {
  if (x < 0)
    x += p;
  if (x >= p)
    x %= p;
  if (y < 0)
    y += p;
  if (y >= p)
    y %= p;
  return (x * y) % p;
}
ll modadd(ll x, ll y, ll p = hell) {
  if (x < 0)
    x += p;
  if (x >= p)
    x %= p;
  if (y < 0)
    y += p;
  if (y >= p)
    y %= p;
  ll temp = x + y;
  if (temp >= p)
    temp %= p;
  return temp;
}
/*void sieve(){
    prime[0] = -1;
    prime[1] = 0;
    for(int i=2;i<=15000000;i++)
        if(!prime[i])
            for(int j=i;j<=15000000;j+=i)
                if(!prime[j])
                        prime[j] = i;
}*/
int main() {
  ios int t = 1, tc = 0;
  // cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n), dp(n, INT_MAX);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    dp[0] = 0;
    for (int i = 1; i < n; i++)
      for (int j = 1; j <= k; j++)
        if (i >= j)
          dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
    cout << dp[n - 1] << "\n";
  }
}