Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define EPS 1e-6
#define INF 1000000000

// MAXIMIZAR f(x) == MINIMIZAR -f(x)

#define _                                                                      \
  ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0),           \
      cout.precision(15);
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(i, a, b) for (int i = int(a); i >= int(b); i--)
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << (#x) << " = " << x << "\n";
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

clock_t t_;

using namespace std;

template <class T> void print(T s) { cout << s << "\n"; }
template <class T> void emax(T &a, T b) { a = (a < b ? b : a); };
template <class T> void emin(T &a, T b) { a = (a > b ? b : a); }
template <class T> void printC(T s) {
  for (auto &x : s) {
    cout << x << " ";
  }
  cout << "\n";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define maxn 100005
#define mod 1000000007

const int inf = 1e9 + 5;

int main(int argc, const char *argv[]) {

#ifdef input
  freopen("input.txt", "r", stdin);
#endif
#ifdef output
  freopen("output.txt", "w", stdout);
#endif
  t_ = clock();

  int n, k;
  cin >> n >> k;
  vi info(n);
  for (auto &i : info) {
    cin >> i;
  }
  vi dp(n + 1, inf);
  dp[1] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= i + k; j++) {
      dp[j] = min(dp[j], dp[i] + abs(info[i - 1] - info[j - 1]));
    }
  }

  print(dp[n]);

  t_ = clock() - t_;
  cerr << setprecision(9) << fixed << ((float)t_ / CLOCKS_PER_SEC) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define EPS 1e-6
#define INF 1000000000

// MAXIMIZAR f(x) == MINIMIZAR -f(x)

#define _                                                                      \
  ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0),           \
      cout.precision(15);
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(i, a, b) for (int i = int(a); i >= int(b); i--)
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << (#x) << " = " << x << "\n";
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

clock_t t_;

using namespace std;

template <class T> void print(T s) { cout << s << "\n"; }
template <class T> void emax(T &a, T b) { a = (a < b ? b : a); };
template <class T> void emin(T &a, T b) { a = (a > b ? b : a); }
template <class T> void printC(T s) {
  for (auto &x : s) {
    cout << x << " ";
  }
  cout << "\n";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define maxn 100005
#define mod 1000000007

const int inf = 1e9 + 5;

int main(int argc, const char *argv[]) {

#ifdef input
  freopen("input.txt", "r", stdin);
#endif
#ifdef output
  freopen("output.txt", "w", stdout);
#endif
  t_ = clock();

  int n, k;
  cin >> n >> k;
  vi info(n);
  for (auto &i : info) {
    cin >> i;
  }
  vi dp(n + 1, inf);
  dp[1] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n && j <= i + k; j++) {
      dp[j] = min(dp[j], dp[i] + abs(info[i - 1] - info[j - 1]));
    }
  }

  print(dp[n]);

  t_ = clock() - t_;
  cerr << setprecision(9) << fixed << ((float)t_ / CLOCKS_PER_SEC) << endl;

  return 0;
}