Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e18;

lint N, K, H[302];
lint dp[303][301]; // dp[見た地点][使ったKの数] = 必要な手数の最小値

int main() {
  cin >> N >> K;
  H[0] = 0;
  H[N + 1] = 0;
  REP(i, N) { cin >> H[i + 1]; }
  REP(i, N + 2) {
    REP(j, K + 1) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;
  REP(i, N + 2) {
    REP(j, K + 1) {
      // 使うKの数
      REP(k, K - j + 1) {
        if (H[i] > H[i + k + 1])
          chmin(dp[i + k + 1][j + k], dp[i][j]);
        else
          chmin(dp[i + k + 1][j + k], dp[i][j] + (H[i + k + 1] - H[i]));
      }
    }
  }
  lint ans = INF;
  REP(i, K + 1) { chmin(ans, dp[N + 1][i]); }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e18;

lint N, K, H[302];
lint dp[303][301]; // dp[見た地点][使ったKの数] = 必要な手数の最小値

int main() {
  cin >> N >> K;
  H[0] = 0;
  H[N + 1] = 0;
  REP(i, N) { cin >> H[i + 1]; }
  REP(i, N + 2) {
    REP(j, K + 1) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;
  REP(i, N + 2) {
    REP(j, K + 1) {
      // 使うKの数
      REP(k, K - j + 1) {
        if (i + k + 1 > N + 1)
          continue;
        if (H[i] > H[i + k + 1])
          chmin(dp[i + k + 1][j + k], dp[i][j]);
        else
          chmin(dp[i + k + 1][j + k], dp[i][j] + (H[i + k + 1] - H[i]));
      }
    }
  }
  lint ans = INF;
  REP(i, K + 1) { chmin(ans, dp[N + 1][i]); }
  cout << ans << endl;
}