Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*------U Have To DO It------*/
/* BY-> RicoProg */
/*        ___
           __|__|
          |  |___       */
#include <bits/stdc++.h>
using namespace std;

//---------------------------------------------------MACROS----------------------------------------------------------
#define ll long long
#define beg(i, n) for (ll i = 0; i < n; i++)
#define beg1(i, n) for (ll i = 1; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define nl "\n"
#define vec(v, n) vector<ll> v(n)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msi map<string, int>
#define mci map<char, int>
#define f1 first
#define f2 second

//---------------------------------------------------GLOBAL----------------------------------------------------------
const ll MOD = 998244353;
const ll MAX = 1e5 + 7;
//---------------------------------------------------FUNCTIONS-------------------------------------------------------

void solve() {
  ll n, k;
  vector<ll> dp(100001, 1e18);
  cin >> n >> k;
  vector<ll> ar(n);
  beg(i, n) cin >> ar[i];
  dp[0] = 0;
  beg1(i, n) {
    beg1(j, k + 1) dp[i] = min(dp[i], dp[i - j] + abs(ar[i] - ar[i - j]));
  }
  cout << dp[n - 1];
}

//---------------------------------------------------DRIVER----------------------------------------------------------
int main() {
  fast;
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
/*------U Have To DO It------*/
/* BY-> RicoProg */
/*        ___
           __|__|
          |  |___       */
#include <bits/stdc++.h>
using namespace std;

//---------------------------------------------------MACROS----------------------------------------------------------
#define ll long long
#define beg(i, n) for (ll i = 0; i < n; i++)
#define beg1(i, n) for (ll i = 1; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define nl "\n"
#define vec(v, n) vector<ll> v(n)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msi map<string, int>
#define mci map<char, int>
#define f1 first
#define f2 second

//---------------------------------------------------GLOBAL----------------------------------------------------------
const ll MOD = 998244353;
const ll MAX = 1e5 + 7;
//---------------------------------------------------FUNCTIONS-------------------------------------------------------

void solve() {
  ll n, k;
  vector<ll> dp(100001, 1e18);
  cin >> n >> k;
  vector<ll> ar(n);
  beg(i, n) cin >> ar[i];
  dp[0] = 0;
  beg1(i, n) {
    beg1(j, k + 1) {
      if (i - j >= 0)
        dp[i] = min(dp[i], dp[i - j] + abs(ar[i] - ar[i - j]));
    }
  }
  cout << dp[n - 1];
}

//---------------------------------------------------DRIVER----------------------------------------------------------
int main() {
  fast;
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}