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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
// #include <random>
#include <map>
// #include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
// #include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
// #include <type_traits>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
double pi = 3.141592653589793;
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), sa(n), sb(m);
  ;
  rep(i, n) {
    cin >> a[i];
    if (i == 0)
      sa[i] = a[i];
    else
      sa[i] = sa[i - 1] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i == 0)
      sb[i] = b[i];
    else
      sb[i] = sb[i - 1] + b[i];
  }

  ll ans = 0;
  ll ca = 0;
  ll cb = 0;

  while (1) {
    cb++;

    if (cb == m + 1) {
      cb--;
      int time = sb[m - 1];

      while (1) {
        ca++;
        if (ca == n + 1) {
          ans = max(ans, n + m);
          break;
        }
        if (time + sa[ca - 1] <= k) {
          ans = max(ans, m + ca);
        } else {
          break;
        }
      }
      break;
    }

    if (sb[cb - 1] <= k) {
      ans = max(ans, cb);
    } else {
      break;
    }
  }

  ca = 0;
  int flg = 1;

  while (1) {
    ca++;
    if (ca >= n + 1 || cb == 0) {
      break;
    }

    while (sa[ca - 1] + sb[cb - 1] > k) {
      cb--;
      if (cb == 0) {
        flg = 0;
        break;
      }
    }

    if (flg == 0) {
      break;
    }

    ans = max(ans, ca + cb);
  }

  rep(i, n) {
    if (sa[i] <= k) {
      ans = max(ans, (ll)i + 1);
    }
  }

  ////////////////////////////////
  ca = 0;
  cb = 0;
  while (1) {
    ca++;

    if (ca == n + 1) {
      ca--;
      int time = sa[n - 1];

      while (1) {
        cb++;
        if (cb == m + 1) {
          ans = max(ans, n + m);
          break;
        }
        if (time + sb[cb - 1] <= k) {
          ans = max(ans, n + cb);
        } else {
          break;
        }
      }
      break;
    }

    if (sa[ca - 1] <= k) {
      ans = max(ca, ans);
    } else {
      break;
    }
  }

  cb = 0;
  flg = 1;

  while (1) {
    cb++;
    if (cb >= m + 1 || ca == 0) {
      break;
    }

    while (sb[cb - 1] + sa[ca - 1] > k) {
      ca--;
      if (ca == 0) {
        flg = 0;
        break;
      }
    }

    if (flg == 0) {
      break;
    }

    ans = max(ans, ca + cb);
  }

  rep(i, m) {
    if (sb[i] <= k) {
      ans = max(ans, (ll)i + 1);
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
// #include <random>
#include <map>
// #include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
// #include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
// #include <type_traits>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
double pi = 3.141592653589793;
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), sa(n), sb(m);
  ;
  rep(i, n) {
    cin >> a[i];
    if (i == 0)
      sa[i] = a[i];
    else
      sa[i] = sa[i - 1] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i == 0)
      sb[i] = b[i];
    else
      sb[i] = sb[i - 1] + b[i];
  }

  ll ans = 0;
  ll ca = 0;
  ll cb = 0;

  while (1) {
    cb++;

    if (cb == m + 1) {
      int time = sb[m - 1];

      while (1) {
        ca++;
        if (ca == n + 1) {
          cout << n + m << endl;
          return 0;
        }
        if (time + sa[ca - 1] <= k) {
          ans = m + ca;
        } else {
          break;
        }
      }
      break;
    }

    if (sb[cb - 1] <= k) {
      ans = cb;
    } else {
      break;
    }
  }

  ca = 0;
  int flg = 1;

  while (1) {
    ca++;
    if (ca >= n + 1 || cb == 0) {
      break;
    }

    while (sa[ca - 1] + sb[cb - 1] > k) {
      cb--;
      if (cb == 0) {
        flg = 0;
        break;
      }
    }

    if (flg == 0) {
      break;
    }

    ans = max(ans, ca + cb);
  }

  ca == 0;
  while (1) {
    ca++;
    if (ca == n + 1) {
      break;
    }
    if (sa[ca - 1] <= k) {
      ans = max(ans, ca);
      ;
    }
  }

  cout << ans << endl;
}