Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define ld long double
#define FOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define FI(i, n) for (int i = 0; i < (n); ++i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define f first
// #define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>     // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;

// I am questioning life and universe and
// everything else after looking at this

const ll MAXN = 1e5 + 5;
ll a[MAXN], dp[MAXN];
ll n, k;

ll rec(ll idx) {
  if (idx > n)
    return 1e10;
  if (idx == n)
    return 0;

  ll &ans = dp[idx];

  if (ans != -1)
    return ans;
  ans = 1e10;

  FOR(i, 1, k) {
    if (idx + i <= n)
      ans = min(ans, rec(idx + i) + abs(a[idx + i] - a[idx]));
  }

  return ans;
}

void solve() {
  cin >> n >> k;

  FOR(i, 1, n) cin >> a[i];
  memset(dp, -1, sizeof(dp));

  ll ans = rec(1);

  cout << ans << endl;
}

signed main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  FastRead;

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define ld long double
#define FOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define FI(i, n) for (int i = 0; i < (n); ++i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define f first
// #define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>     // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;

// I am questioning life and universe and
// everything else after looking at this

const ll MAXN = 1e5 + 5;
ll a[MAXN], dp[MAXN];
ll n, k;

ll rec(ll idx) {
  if (idx > n)
    return 1e10;
  if (idx == n)
    return 0;

  ll &ans = dp[idx];

  if (ans != -1)
    return ans;
  ans = 1e10;

  FOR(i, 1, k) {
    if (idx + i <= n)
      ans = min(ans, rec(idx + i) + abs(a[idx + i] - a[idx]));
  }

  return ans;
}

void solve() {
  cin >> n >> k;

  FOR(i, 1, n) cin >> a[i];
  memset(dp, -1, sizeof(dp));

  ll ans = rec(1);

  cout << ans << endl;
}

signed main() {

  FastRead;

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();
}