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

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

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  Int w, h, k, n;
  cin >> w >> h >> k >> n;
  k -= (w + 1) / 2;
  if (k < 0) {
    cout << -1 << endl;
    return 0;
  }
  Int ans = ((w + 1) / 2) * (h / 2);
  Int cnt = 0;
  vector<vector<Int>> vp(h + 1);
  for (Int i = 0; i < n; i++) {
    Int x, y;
    cin >> x >> y;
    if (x & 1)
      ans++;
    if (y & 1)
      continue;
    vp[y].emplace_back(x);
  }
  const Int INF = 1e9;
  for (Int i = 0; i < h + 1; i++) {
    if (vp[i].empty())
      continue;
    vector<Int> dg(w + 1, 0);
    for (Int x : vp[i])
      dg[x] = 1;
    auto dp = make_v<Int>(2, w + 1);
    fill_v(dp, -INF);
    dp[0][1] = 0;
    for (Int j = 1; j < w; j += 2) {
      chmax(dp[0][j + 2], dp[0][j]);
      chmax(dp[0][j + 2], dp[1][j]);
      if (dg[j + 1])
        continue;
      chmax(dp[1][j + 2], dp[0][j] + dg[j] + dg[j + 2]);
    }
    ans -= max(dp[0][w], dp[1][w]);
    Int sum = 0;
    for (Int x : vp[i])
      if (x & 1)
        sum++;
    cnt += sum - max(dp[0][w], dp[1][w]);
  }
  chmin(cnt, k);
  ans -= min(ans, cnt * 2);
  k -= cnt;
  ans -= min(ans, k);
  cout << ans << endl;
  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  Int w, h, k, n;
  cin >> w >> h >> k >> n;
  k -= (w + 1) / 2;
  if (k < 0) {
    cout << -1 << endl;
    return 0;
  }
  Int ans = ((w + 1) / 2) * (h / 2);
  Int cnt = 0;
  vector<vector<Int>> vp(h + 1);
  for (Int i = 0; i < n; i++) {
    Int x, y;
    cin >> x >> y;
    if (x & 1)
      ans++;
    if (y & 1)
      continue;
    vp[y].emplace_back(x);
  }

  const Int INF = 1e9;
  for (Int i = 0; i < h + 1; i++) {
    if (vp[i].empty())
      continue;
    vector<Int> dg(w, 0);
    for (Int x : vp[i])
      dg[x] = 1;
    auto dp = make_v<Int>(2, w + 1);
    fill_v(dp, -INF);
    dp[0][1] = 0;
    for (Int j = 1; j < w; j += 2) {
      chmax(dp[0][j + 2], dp[0][j]);
      chmax(dp[0][j + 2], dp[1][j]);
      if (dg[j + 1])
        continue;
      chmax(dp[1][j + 2], dp[0][j] + dg[j] + dg[j + 2]);
    }
    ans -= max(dp[0][w], dp[1][w]);
    cnt += vp[i].size() - max(dp[0][w], dp[1][w]);
  }
  chmin(cnt, k);
  ans -= min(ans, cnt * 2);
  k -= cnt;
  ans -= min(ans, k);
  cout << ans << endl;
  return 0;
}