Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// includes
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// macros and consts
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a) for (auto &i : a)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define accm accumulate
#define Fi first
#define Se second
#define INF 1e9
#define llINF (1LL << 60)
#define mINF -1e9
#define mllINF -(1LL << 60)
#define endl "\n"
#define CONT continue
#define BRK break
#define modulo(n, m) (((n) % (m) + (m)) % m)

// shorter types
using namespace std;
using ll = long long;
using vi = std::vector<int>;
using vc = std::vector<char>;
using vll = std::vector<long long>;
using vs = std::vector<string>;
using Mi = map<int, int>;
using Mll = map<ll, ll>;
using UMi = unordered_map<int, int>;
using UMll = unordered_map<ll, ll>;
using Pi = pair<int, int>;
using Pll = pair<ll, ll>;
using vPi = vector<Pi>;
using vPll = vector<Pll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using pqgi = priority_queue<int, vector<int>, greater<int>>;
using pqsi = priority_queue<int, vector<int>, less<int>>;
using pqgll = priority_queue<int, vector<int>, greater<int>>;
using pssll = priority_queue<int, vector<int>, less<int>>;
template <class T> using vec = vector<T>;
#define int long long

int N;
int K;
// here begins your code
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  vector<int> h;
  cin >> N;
  cin >> K;
  h.resize(N);

  REP(i, N) { cin >> h[i]; }
  vector<int> dp;
  dp.resize(N + 10, llINF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }

  cout << dp[N - 1] << endl;
  // check types
  // do not eb if you resize
  // resize before you access by idx
  return 0;
}

Fixed submission:
// includes
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// macros and consts
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a) for (auto &i : a)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define accm accumulate
#define Fi first
#define Se second
#define INF 1e9
#define llINF (1LL << 60)
#define mINF -1e9
#define mllINF -(1LL << 60)
#define endl "\n"
#define CONT continue
#define BRK break
#define modulo(n, m) (((n) % (m) + (m)) % m)

// shorter types
using namespace std;
using ll = long long;
using vi = std::vector<int>;
using vc = std::vector<char>;
using vll = std::vector<long long>;
using vs = std::vector<string>;
using Mi = map<int, int>;
using Mll = map<ll, ll>;
using UMi = unordered_map<int, int>;
using UMll = unordered_map<ll, ll>;
using Pi = pair<int, int>;
using Pll = pair<ll, ll>;
using vPi = vector<Pi>;
using vPll = vector<Pll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using pqgi = priority_queue<int, vector<int>, greater<int>>;
using pqsi = priority_queue<int, vector<int>, less<int>>;
using pqgll = priority_queue<int, vector<int>, greater<int>>;
using pssll = priority_queue<int, vector<int>, less<int>>;
template <class T> using vec = vector<T>;
#define int long long

int N;
int K;
// here begins your code
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  vector<int> h;
  cin >> N;
  cin >> K;
  h.resize(N);

  REP(i, N) { cin >> h[i]; }
  vector<int> dp;
  dp.resize(N + 110, llINF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }

  cout << dp[N - 1] << endl;
  // check types
  // do not eb if you resize
  // resize before you access by idx
  return 0;
}