Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out)                                                         \
  stdi(in);                                                                    \
  stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 1000007
#define MOD 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
// typedef int ittt;
// #define int ll

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

const ll inf = 1e18;

ll hit[MAX];
ll dp[MAX];
int n, k;

ll solve(int i) {
  if (i <= 1) {
    return 0;
  }

  if (dp[i] != -1)
    return dp[i];

  ll ans = solve(i - 1) + abs(hit[i] - hit[i - 1]);

  for (int j = 2; j <= k; j++) {
    if (i - j > 0) {
      ans = min(ans, solve(i - j) + abs(hit[i] - hit[i - j]));
    }
  }

  return dp[i] = ans;
}

int main() {
#ifndef ONLINE_JUDGE
  stdio("in", "out");
#endif
  fast;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> hit[i];
    dp[i] = -1;
  }

  ll ans = solve(n);
  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out)                                                         \
  stdi(in);                                                                    \
  stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 1000007
#define MOD 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
// typedef int ittt;
// #define int ll

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

const ll inf = 1e18;

ll hit[MAX];
ll dp[MAX];
int n, k;

ll solve(int i) {
  if (i <= 1) {
    return 0;
  }

  if (dp[i] != -1)
    return dp[i];

  ll ans = solve(i - 1) + abs(hit[i] - hit[i - 1]);

  for (int j = 2; j <= k; j++) {
    if (i - j > 0) {
      ans = min(ans, solve(i - j) + abs(hit[i] - hit[i - j]));
    }
  }

  return dp[i] = ans;
}

int main() {
  /*#ifndef ONLINE_JUDGE
      stdio("in", "out");
  #endif*/
  fast;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> hit[i];
    dp[i] = -1;
  }

  ll ans = solve(n);
  cout << ans << '\n';

  return 0;
}