Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC Optimize("Ofast")
#pragma GCC Optimize "trapv"
#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define li long int
#define ll long long
#define ull unsigned long long
#define Vec(x) vector<x>
#define Pai(x, y) pair<x, y>
#define Map(x, y) map<x, y>
#define UMap(x, y) unordered_map<x, y>
#define Set(x) set<x>
#define Sta(x) stack<x>
#define Qu(x) queue<x>
#define DQ(x) deque(x)
#define PriorQ(x) priority_queue<x>
#define bg begin()
#define ed end()
#define sz size()
#define ct(x) count(x)
#define len length()
#define F first
#define S second
#define Makep make_pair
#define ins(x) insert(x)
#define ers(x) erase(x)
#define endl "\n";
#define MOD 1000000007
#define PI 2 * acos(0.0)
#define clr() clear()
#define fori0(n) for (int i = 0; i < n; i++)
#define foriab(a, b) for (int i = a; i < b; i++)
#define mem0(x) memset(x, 0, sizeof(x))
#define mem1(x) memset(x, 1, sizeof(x))
#define autoit(x) for (auto it = x.beg; it != x.ed; it++)
#define count0(x) __builtin_popcount(x)
#define ret return
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ll Inf = 1e9 + 5;

ll a[100005], dp[100005], n, k;

ll ans(ll i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return INT_MAX;
  if (dp[i] != -1)
    return dp[i];
  ll sol = INT_MAX;
  for (ll j = 1; j <= k; j++) {
    if (i + j < n) {
      sol = min(sol, abs(a[i] - a[j]) + ans(j));
    }
  }
  return dp[i] = sol;
}

void solve() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  cout << ans(0) << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  solve();
  ret 0;
}

Fixed submission:
#pragma GCC Optimize("Ofast")
#pragma GCC Optimize "trapv"
#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define li long int
#define ll long long
#define ull unsigned long long
#define Vec(x) vector<x>
#define Pai(x, y) pair<x, y>
#define Map(x, y) map<x, y>
#define UMap(x, y) unordered_map<x, y>
#define Set(x) set<x>
#define Sta(x) stack<x>
#define Qu(x) queue<x>
#define DQ(x) deque(x)
#define PriorQ(x) priority_queue<x>
#define bg begin()
#define ed end()
#define sz size()
#define ct(x) count(x)
#define len length()
#define F first
#define S second
#define Makep make_pair
#define ins(x) insert(x)
#define ers(x) erase(x)
#define endl "\n";
#define MOD 1000000007
#define PI 2 * acos(0.0)
#define clr() clear()
#define fori0(n) for (int i = 0; i < n; i++)
#define foriab(a, b) for (int i = a; i < b; i++)
#define mem0(x) memset(x, 0, sizeof(x))
#define mem1(x) memset(x, 1, sizeof(x))
#define autoit(x) for (auto it = x.beg; it != x.ed; it++)
#define count0(x) __builtin_popcount(x)
#define ret return
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ll Inf = 1e9 + 5;

ll a[100005], dp[100005], n, k;

ll ans(ll i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return INT_MAX;
  if (dp[i] != -1)
    return dp[i];
  ll sol = INT_MAX;
  for (ll j = 1; j <= k; j++) {
    if (i + j < n) {
      sol = min(sol, abs(a[i] - a[i + j]) + ans(i + j));
    }
  }
  return dp[i] = sol;
}

void solve() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  cout << ans(0) << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  solve();
  ret 0;
}