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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
constexpr int INF = 1000000000000000000LL;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return b < a ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}

signed main() {
  int N, W;
  cin >> N >> W;
  vector<int> v(N), w(N);
  rep(i, N) cin >> v[i] >> w[i];
  int mid = N / 2;
  vector<pair<int, int>> left;
  rep(i, 1 << mid) {
    int sv = 0, sw = 0;
    rep(j, mid) if (i >> j & 1) sv += v[j], sw += w[j];
    left.emplace_back(sw, -sv);
  }
  sort(all(left));
  loop(i, 1, left.size()) cmin(left[i].second, left[i - 1].second);
  int maxi = 0;
  rep(i, 1 << (N - mid)) {
    int sv = 0, sw = 0;
    rep(j, N - mid) if (i >> j & 1) sv += v[j + mid], sw += w[j + mid];
    if (sw > W)
      continue;
    auto p = lower_bound(all(left), make_pair(W - sw, -INF));
    if (p == left.end())
      --p;
    if (p->first + sw > W)
      --p;
    cmax(maxi, -p->second + sv);
  }
  cout << maxi << endl;
}


Error: -6
3fa6378e-8d66-43bf-9db7-7d19830d3af3.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02321/C++/s557820890.cpp:52: int main(): Assertion `0 == maxi' failed.


Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

main {
  int N, W;
  cin >> N >> W;
  vector<int> v(N), w(N);
  rep(i, N) cin >> v[i] >> w[i];
  int mid = N / 2;
  vector<pair<int, int>> left;
  rep(i, 1 << mid) {
    int sv = 0, sw = 0;
    rep(j, N / 2) if (i >> j & 1) sv += v[j], sw += w[j];
    left.emplace_back(sw, -sv);
  }
  sort(all(left));
  loop(i, 1, left.size()) cmin(left[i].second, left[i - 1].second);
  int maxi = 0;
  rep(i, 1 << (N - mid)) {
    int sv = 0, sw = 0;
    rep(j, N - mid) if (i >> j & 1) sv += v[j + N / 2], sw += w[j + N / 2];
    if (sw > W)
      continue;
    auto p = lower_bound(all(left), make_pair(W - sw, -INF));
    if (p->first + sw > W)
      --p;
    cmax(maxi, -p->second + sv);
  }
  assert(0 == maxi);
  cout << maxi << endl;
}