Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

struct _ {
  ios_base::Init i;
  _() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
  }
} ___;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#define oset                                                                   \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define pii pair<int, int>
#define ff first
#define ss second
#define endll '\n'
#define rep(i, n) for (int i = 0; i++ < n;)
#define scl(i) scanf("%lld", &i);
#define int long long int

int dp[100005];
vector<int> arr(100005);
int k, n;
int call(int i) {
  if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];

  int mini = 1e10;

  for (int j = i + 1; j <= min(n, i + k); j++) {
    mini = min(mini, abs(arr[i] - arr[j]) + call(j));
  }

  return dp[i] = mini;
}

signed main() {

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

  cin >> n >> k;

  rep(i, n) cin >> arr[i];

  memset(dp, -1, sizeof dp);
  cout << call(1);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

struct _ {
  ios_base::Init i;
  _() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
  }
} ___;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#define oset                                                                   \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define pii pair<int, int>
#define ff first
#define ss second
#define endll '\n'
#define rep(i, n) for (int i = 0; i++ < n;)
#define scl(i) scanf("%lld", &i);
#define int long long int

int dp[100005];
vector<int> arr(100005);
int k, n;
int call(int i) {
  if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];

  int mini = 1e10;

  for (int j = i + 1; j <= min(n, i + k); j++) {
    mini = min(mini, abs(arr[i] - arr[j]) + call(j));
  }

  return dp[i] = mini;
}

signed main() {

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

  cin >> n >> k;

  rep(i, n) cin >> arr[i];

  memset(dp, -1, sizeof dp);
  cout << call(1);
}