Problem p02864 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
//__int128

long long dp[305][305][305];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif

  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  int n, K;
  cin >> n >> K;
  vi a(n + 1), b(n + 1);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(all(b));
  vi p(n + 1);
  for (int i = 1; i <= n; ++i) {
    p[i] = lower_bound(all(b), a[i]) - b.begin();
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n; ++j) {
      for (int k = 0; k <= K; ++k) {
        if (b[j] != a[i + 1]) {
          dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]);
          if (b[j] >= a[i + 1]) {
            dp[i + 1][p[i + 1]][k] = min(dp[i + 1][p[i + 1]][k], dp[i][j][k]);
          } else {
            dp[i + 1][p[i + 1]][k] =
                min(dp[i + 1][p[i + 1]][k], dp[i][j][k] + a[i + 1] - b[j]);
          }
        } else {
          dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        }
      }
    }
  }
  long long res = INF;
  for (int i = 0; i <= n; ++i) {
    res = min(dp[n][i][K], res);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
//__int128

long long dp[305][305][305];
signed main() {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  int n, K;
  cin >> n >> K;
  vi a(n + 1), b(n + 1);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(all(b));
  vi p(n + 1);
  for (int i = 1; i <= n; ++i) {
    p[i] = lower_bound(all(b), a[i]) - b.begin();
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n; ++j) {
      for (int k = 0; k <= K; ++k) {
        if (b[j] != a[i + 1]) {
          dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k]);
          if (b[j] >= a[i + 1]) {
            dp[i + 1][p[i + 1]][k] = min(dp[i + 1][p[i + 1]][k], dp[i][j][k]);
          } else {
            dp[i + 1][p[i + 1]][k] =
                min(dp[i + 1][p[i + 1]][k], dp[i][j][k] + a[i + 1] - b[j]);
          }
        } else {
          dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);
        }
      }
    }
  }
  long long res = INF;
  for (int i = 0; i <= n; ++i) {
    res = min(dp[n][i][K], res);
  }
  cout << res << endl;
  return 0;
}