Problem p01887 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3
#define mod 1000000007

int w, h, k, n, m;
vector<int> ps[10010];
int dp[5005];

int main() {
  cin >> w >> h >> k;
  cin >> n;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ps[b].push_back(a);
  }
  rep(i, h) sort(all(ps[i]));
  m = (w - 1) / 2 + 1;
  if (k < m) {
    cout << -1 << endl;
    return 0;
  }
  k -= m;
  int res = 0;
  int cnt = 0;
  for (int i = 1; i < h; i += 2) {
    rep(j, m + 1) dp[j] = INF;
    dp[0] = 0;
    rep(j, m) {
      int idx1 = lower_bound(all(ps[i]), j * 2) - ps[i].begin();
      int idx2 = lower_bound(all(ps[i]), j * 2 + 1) - ps[i].begin();
      int v1 = (idx1 < ps[i].size() && ps[i][idx1] == (j * 2) ? 1 : 0);
      int v2 = (idx2 < ps[i].size() && ps[i][idx2] == (j * 2 + 1) ? 1 : 0);
      minch(dp[j + 1], dp[j] + v1);
      if (j + 2 <= m)
        minch(dp[j + 2], dp[j] + v2 * 2);
    }
    cnt += dp[m];
    res += dp[m] * 2 + (m - dp[m]) * 1;
  }
  if (cnt > k) {
    res -= 2 * k;
  } else {
    res -= 2 * cnt + (k - cnt);
  }
  cout << max(0, res) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3
#define mod 1000000007

int w, h, k, n, m;
vector<int> ps[10010];
int dp[5005];

int main() {
  cin >> w >> h >> k;
  cin >> n;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ps[b].push_back(a);
  }
  m = (w - 1) / 2 + 1;
  if (k < m) {
    cout << -1 << endl;
    return 0;
  }
  k -= m;
  int res = 0;
  int cnt = 0;
  for (int i = 1; i < h; i += 2) {
    rep(j, m + 1) dp[j] = INF;
    dp[0] = 0;
    rep(j, m) {
      int v1 = (((*lower_bound(all(ps[i]), j * 2))) == j * 2);
      int v2 = (((*lower_bound(all(ps[i]), j * 2 + 1))) == j * 2 + 1);
      minch(dp[j + 1], dp[j] + v1);
      minch(dp[j + 2], dp[j] + v2 * 2);
    }
    cnt += dp[m];
    res += dp[m] * 2 + (m - dp[m]) * 1;
  }
  if (cnt > k) {
    res -= 2 * k;
  } else {
    res -= 2 * cnt + (k - cnt);
  }
  cout << max(0, res) << endl;
  return 0;
}