Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ans = 0;

  vector<ll> sumA(n + 1), sumB(m);
  sumA[0] = 0;
  sumB[0] = b[0];
  reps1(i, n + 1) sumA[i] = sumA[i - 1] + a[i - 1];
  reps1(i, m) sumB[i] = sumB[i - 1] + b[i];

  rep(i, n + 1) {
    ll count = sumA[i];
    if (count > k)
      break;

    // cout << i << " " << k << " " << " " << count << " " << ans << endl;

    ll pos = upper_bound(sumB.begin(), sumB.end(), k - count) - sumB.begin();
    /*
    ll pos = 0;
    rep(j, m) {
      count += b[j];
      pos++;
      if (count > k) {
        pos--;
        break;
      }
    }
    */
    chmax(ans, i + pos);
    // cout << i << " " << k << " " << pos << " " << count << " " << ans <<
    // endl;
  }

  cout << ans << endl;

  /*
    stack<ll> stackA;
    stack<ll> stackB;
    ll count;

    rep(i, n) {
      ll a;
      cin >> a;
      count += a;
      stackA.push(a);
    }

    rep(i, m) {
      ll b;
      cin >> b;
      count += b;
      stackB.push(b);
    }

    ll ans = n + m;

    while(count > k) {
      ll a = stackA.top();
      ll b = stackB.top();
      cout << a << " " << b << " " << " " << count << " " << ans << endl;

      if (a >= b) {
        stackA.pop();
        count -= a;
      }
      else {
        stackB.pop();
        count -= b;
      }

      ans--;
    }
    */

  // cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ans = 0;

  vector<ll> sumA(n), sumB(m);
  sumA[0] = a[0];
  sumB[0] = b[0];
  reps1(i, n) sumA[i] = sumA[i - 1] + a[i];
  reps1(i, m) sumB[i] = sumB[i - 1] + b[i];

  rep(i, n + 1) {
    ll count = accumulate(a.begin(), a.begin() + i, 0ll);
    if (count > k)
      break;

    // cout << i << " " << k << " " << " " << count << " " << ans << endl;

    ll pos = upper_bound(sumB.begin(), sumB.end(), k - count) - sumB.begin();
    /*
    ll pos = 0;
    rep(j, m) {
      count += b[j];
      pos++;
      if (count > k) {
        pos--;
        break;
      }
    }
    */
    chmax(ans, i + pos);
    // cout << i << " " << k << " " << pos << " " << count << " " << ans <<
    // endl;
  }

  cout << ans << endl;

  /*
    stack<ll> stackA;
    stack<ll> stackB;
    ll count;

    rep(i, n) {
      ll a;
      cin >> a;
      count += a;
      stackA.push(a);
    }

    rep(i, m) {
      ll b;
      cin >> b;
      count += b;
      stackB.push(b);
    }

    ll ans = n + m;

    while(count > k) {
      ll a = stackA.top();
      ll b = stackB.top();
      cout << a << " " << b << " " << " " << count << " " << ans << endl;

      if (a >= b) {
        stackA.pop();
        count -= a;
      }
      else {
        stackB.pop();
        count -= b;
      }

      ans--;
    }
    */

  // cout << ans << endl;

  return 0;
}