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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 200005
const ll inf = 1e9 + 7;
#define pb push_back
#define PI 3.14159265
#define watch(x) cerr << (#x) << " is " << (x) << endl;
void solve();
void sieve();
int main() {
  // sieve();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed;
  cout << setprecision(10);

  ll t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}

ll pr[1001];
vector<ll> prime;
void sieve() {
  pr[0] = -1;
  pr[1] = -1;
  ll i, j;
  for (i = 2; i < 1001; i++) {
    if (pr[i] != -1) {
      pr[i] = 1;
      for (j = 2; j < 1001 / i + 1; j++) {
        if (i * j < 1001)
          pr[i * j] = -1;
      }
    }
  }
  for (i = 2; i < 1001; i++) {
    if (pr[i] == 1)
      prime.pb(i);
  }
}

bool pal(string s) {
  string t = s;
  reverse(s.begin(), s.end());
  return (t == s);
}
ll countdig(ll x) {
  ll c = 0;
  if (x == 0)
    return 1;

  while (x != 0) {
    c++;
    x /= 10;
  }
  return c;
}
bool isprime(ll x) {
  bool flag = true;
  for (ll i = 2; i <= sqrt(x) + 1; i++) {
    if (x % i == 0) {
      flag = false;
      break;
    }
  }
  if (x == 1)
    flag = false;

  return flag;
}
void swap(ll &a, ll &b) {
  ll t = a;
  a = b;
  b = t;
}
class matrix {
public:
  vector<vector<ll>> v;
  ll row;
  ll col;
  matrix(vector<vector<ll>> mat) {
    v.assign(mat.begin(), mat.end());
    row = mat.size();
    col = mat[0].size();
  }
  vector<vector<ll>> I(ll n) {
    vector<vector<ll>> mat(n, vector<ll>(n, 0));
    for (ll i = 0; i < n; i++)
      mat[i][i] = 1;
    return mat;
  }
  matrix operator*(matrix &u) {
    ll r = this->row;
    ll c = u.col;
    assert(this->col == u.row);
    vector<vector<ll>> res(r, vector<ll>(c, 0));
    for (ll i = 0; i < r; i++) {
      for (ll j = 0; j < c; j++) {
        for (ll k = 0; k < this->col; k++)
          res[i][j] += v[i][k] * u.v[k][j];
      }
    }
    return matrix(res);
  }
  matrix fastexpo(matrix m, ll p) {
    assert(m.row == m.col);
    matrix res(I(m.row));

    while (p) {
      if (p & 1)
        res = res * m;
      m = m * m;
      p = p >> 1;
    }
    return res;
  }
};
bool comp(ll a, ll b) { return a > b; }
void gensparse() {
  ll n = 5, i, j;
  ll st[n][27];
  ll arr[] = {1, 2, 3, 4, 5};
  for (i = 0; i < n; i++)
    st[i][0] = (arr[i]);

  for (j = 1; j < 27; j++) {
    for (i = 0; i + (1 << j) - 1 < n; i++) {
      st[i][j] = (st[i][j - 1] + st[i + (1 << (j - 1))][j - 1]);
    }
  }
  // now query
  ll l, r;
  cin >> l >> r;
  ll ans = 0;
  for (j = 26; j >= 0; j--) {
    if ((1 << j) <= r - l + 1) {
      ans += st[l][j];
      l += (1 << j);
    }
  }
  cout << ans;
}
ll solve(ll l, ll r, ll n) {
  if (l > r)
    return 0;
  ll g = (l + r) / 2;
  if (g == 0)
    return solve(g + 1, r, n);

  if (n >= (ll)pow(26, g - 1) + 1 && n <= (ll)pow(26, g))
    return g;
  else if (n > (ll)pow(26, g))
    return solve(g + 1, r, n);
  else
    return solve(l, g - 1, n);
}
void solve() {
  ll n, m, k, i, j;
  cin >> n >> m >> k;
  ll a[n], b[m], pra[n], prb[m];
  for (i = 0; i < n; i++)
    cin >> a[i];
  pra[0] = a[0];
  for (i = 1; i < n; i++)
    pra[i] = pra[i - 1] + a[i];
  for (i = 0; i < m; i++)
    cin >> b[i];
  prb[0] = b[0];
  for (i = 1; i < m; i++)
    prb[i] = prb[i - 1] + b[i];

  ll l = 0, r = 0;
  for (r = 0; r < m; r++) {
    if (prb[r] > k)
      break;
  }
  r--;

  ll maxm = r; // at the end do r+=2
  for (l = 0; l < n; l++) {
    ll lim = k - pra[l];
    if (lim < 0)
      break;
    for (j = r; j >= 0; j--) {
      if (prb[j] <= lim)
        break;
    }
    maxm = max(l + j + 1, maxm);

    r = j;
  }
  cout << maxm + 1;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 200005
const ll inf = 1e9 + 7;
#define pb push_back
#define PI 3.14159265
#define watch(x) cerr << (#x) << " is " << (x) << endl;
void solve();
void sieve();
int main() {
  // sieve();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed;
  cout << setprecision(10);

  ll t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}

ll pr[1001];
vector<ll> prime;
void sieve() {
  pr[0] = -1;
  pr[1] = -1;
  ll i, j;
  for (i = 2; i < 1001; i++) {
    if (pr[i] != -1) {
      pr[i] = 1;
      for (j = 2; j < 1001 / i + 1; j++) {
        if (i * j < 1001)
          pr[i * j] = -1;
      }
    }
  }
  for (i = 2; i < 1001; i++) {
    if (pr[i] == 1)
      prime.pb(i);
  }
}

bool pal(string s) {
  string t = s;
  reverse(s.begin(), s.end());
  return (t == s);
}
ll countdig(ll x) {
  ll c = 0;
  if (x == 0)
    return 1;

  while (x != 0) {
    c++;
    x /= 10;
  }
  return c;
}
bool isprime(ll x) {
  bool flag = true;
  for (ll i = 2; i <= sqrt(x) + 1; i++) {
    if (x % i == 0) {
      flag = false;
      break;
    }
  }
  if (x == 1)
    flag = false;

  return flag;
}
void swap(ll &a, ll &b) {
  ll t = a;
  a = b;
  b = t;
}
class matrix {
public:
  vector<vector<ll>> v;
  ll row;
  ll col;
  matrix(vector<vector<ll>> mat) {
    v.assign(mat.begin(), mat.end());
    row = mat.size();
    col = mat[0].size();
  }
  vector<vector<ll>> I(ll n) {
    vector<vector<ll>> mat(n, vector<ll>(n, 0));
    for (ll i = 0; i < n; i++)
      mat[i][i] = 1;
    return mat;
  }
  matrix operator*(matrix &u) {
    ll r = this->row;
    ll c = u.col;
    assert(this->col == u.row);
    vector<vector<ll>> res(r, vector<ll>(c, 0));
    for (ll i = 0; i < r; i++) {
      for (ll j = 0; j < c; j++) {
        for (ll k = 0; k < this->col; k++)
          res[i][j] += v[i][k] * u.v[k][j];
      }
    }
    return matrix(res);
  }
  matrix fastexpo(matrix m, ll p) {
    assert(m.row == m.col);
    matrix res(I(m.row));

    while (p) {
      if (p & 1)
        res = res * m;
      m = m * m;
      p = p >> 1;
    }
    return res;
  }
};
bool comp(ll a, ll b) { return a > b; }
void gensparse() {
  ll n = 5, i, j;
  ll st[n][27];
  ll arr[] = {1, 2, 3, 4, 5};
  for (i = 0; i < n; i++)
    st[i][0] = (arr[i]);

  for (j = 1; j < 27; j++) {
    for (i = 0; i + (1 << j) - 1 < n; i++) {
      st[i][j] = (st[i][j - 1] + st[i + (1 << (j - 1))][j - 1]);
    }
  }
  // now query
  ll l, r;
  cin >> l >> r;
  ll ans = 0;
  for (j = 26; j >= 0; j--) {
    if ((1 << j) <= r - l + 1) {
      ans += st[l][j];
      l += (1 << j);
    }
  }
  cout << ans;
}
ll solve(ll l, ll r, ll n) {
  if (l > r)
    return 0;
  ll g = (l + r) / 2;
  if (g == 0)
    return solve(g + 1, r, n);

  if (n >= (ll)pow(26, g - 1) + 1 && n <= (ll)pow(26, g))
    return g;
  else if (n > (ll)pow(26, g))
    return solve(g + 1, r, n);
  else
    return solve(l, g - 1, n);
}
void solve() {
  ll n, m, k, i, j;
  cin >> n >> m >> k;
  ll a[n], b[m], pra[n], prb[m];
  for (i = 0; i < n; i++)
    cin >> a[i];
  pra[0] = a[0];
  for (i = 1; i < n; i++)
    pra[i] = pr[i - 1] + a[i];
  for (i = 0; i < m; i++)
    cin >> b[i];
  prb[0] = b[0];
  for (i = 1; i < m; i++)
    prb[i] = prb[i - 1] + b[i];

  ll l = 0, r = 0;
  for (r = 0; r < m; r++) {
    if (prb[r] > k)
      break;
  }
  r--;
  ll maxm = r; // at the end do r+=2
  for (l = 0; l < n; l++) {
    ll lim = k - pra[l];
    if (lim < 0)
      break;
    for (j = r; j >= 0; j--) {
      if (prb[j] <= lim)
        break;
    }
    if (j != -1)
      maxm = max(l + j, maxm);

    r = j;
  }
  cout << maxm + 2;
}