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

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
long long L, M, R;
pair<long long, long long> maintain(long long l, long long r) {
  if (l >= 0)
    return make_pair(l, min(M, r));
  else if (r <= 0)
    return make_pair(-r, min(M, -l));
  else
    return make_pair(0, min(M, max(r, -l)));
}
vector<pair<long long, long long>>
maintain(vector<pair<long long, long long>> b) {
  vector<pair<long long, long long>> ret, a;
  int n = b.size();
  for (int i = 0; i < n; ++i)
    if (b[i].first <= b[i].second)
      a.push_back(b[i]);
  if (a.empty())
    return a;
  sort(a.begin(), a.end());
  n = a.size();
  ret.push_back(a.front());
  for (int i = 1; i < n; ++i)
    if (a[i].first <= ret.back().second)
      ret.back().second = max(ret.back().second, a[i].second);
    else
      ret.push_back(a[i]);
  return ret;
}
vector<pair<long long, long long>> add(vector<pair<long long, long long>> a,
                                       long long x) {
  vector<pair<long long, long long>> ret;
  int n = a.size();
  for (int i = 0; i < n; ++i) {
    long long l = a[i].first, r = a[i].second;
    ret.push_back(maintain(l + x * R, r + x * L));
    ret.push_back(maintain(l - x * L, r - x * R));
  }
  return maintain(ret);
}
int main() {
  int n;
  scanf("%d%lld%lld%lld", &n, &L, &M, &R);
  R *= 2;
  M *= 2;
  vector<pair<long long, long long>> now;
  now.push_back(make_pair(0, 0));
  for (int i = 0; i < n; ++i) {
    long long x;
    scanf("%lld", &x);
    now = add(now, x);
    if (now.empty()) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define eps 1e-9
#define ll long long
#define ld long double
#define pii pair<int, int>
#define x first
#define y second
#define sf scanf
#define pf printf
#define vec vector
#define pb push_back
#define mp make_pair
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define bin_cnt(x) __builtin_popcount(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define irep(i, a) for (__typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#pragma comment(linker, "/STACK:32505856")

template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline void gmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void gmax(T &a, T b) {
  if (a < b)
    a = b;
}
inline int sign(const double &a) { return a > eps ? 1 : (a < -eps ? -1 : 0); }
struct Initializer {
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  ~Initializer() {}
} initializer;

#define M 100000
#define f(x, y) F[x & 1][y + M]
int F[2][M + 1 << 1];

int main() {
  //  freopen("out","w",stdout);
  int n, l, m, r;
  cin >> n >> l >> m >> r;

  int a = r * 2, b = l;
  f(0, 0) = 1;

  rep(i, 1, n) {
    int w;
    cin >> w;
    memset(F[i & 1], 0, sizeof(F[i & 1]));
    rep(j, -m * 2, m * 2) if (f(i - 1, j)) {
      if (j + a * w <= m * 2) {
        f(i, j + a * w) = 1;
        f(i, min(j + b * w, m * 2)) = 1;
      }
      if (j - a * w >= -m * 2) {
        f(i, j - a * w) = 1;
        f(i, max(j - b * w, -m * 2)) = 1;
      }
    }
  }

  rep(i, -m * 2, m * 2) if (f(n, i)) return puts("Yes"), 0;

  puts("No");
  return 0;
}