Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++) // REP(i, 5) cout<<i;
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define MOD 1000000007 // 10^9+7
// #define MOD 1

ll gcd(ll a, ll b) {
  ll tmp;
  if (b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b != 0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

const int MAX = 2000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void sosuu(ll n, vector<ll> &v) {

  vector<ll> num(n, 0);

  FOR(i, 1, sqrt(n)) {
    if (num[i] == 0) {
      FOR(j, 0, n / i) { num[i + (j * i)] = 1; }
    }
  }
  REP(i, n) {
    if (num[i] == 0)
      v.push_back(i + 1);
  }
}

ll GetDigit(ll num) {
  ll digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

ll power(ll a, ll b) {
  ll ans = 1;
  if (b % 2 == 0) {
    REP(i, b / 2) {
      ans *= a;
      if (ans > 1000000000) {
        return -1;
      }
    }
    return ans * ans;
  } else {
    b = b / 2;
    REP(i, b) {
      ans *= a;
      if (ans > 1000000000) {
        return -1;
      }
    }
    return ans * ans * a;
  }
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main(void) {

  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> a(n);
  vector<ll> b(m);
  vector<ll> a_sum(n + 1);
  vector<ll> b_sum(m + 1);
  a_sum[0] = 0;
  b_sum[0] = 0;

  REP(i, n) {
    ll x;
    cin >> x;
    a[i] = x;
    // if(i!=0)
    a_sum[i + 1] = a_sum[i] + x;
    // else a_sum[i] = x;
    // cout<<a[i]<<" ";
  }
  // cout<<endl;
  REP(i, m) {
    ll x;
    cin >> x;
    b[i] = x;
    // if(i!=0)
    b_sum[i + 1] = b_sum[i] + x;
    // else b_sum[i] = x;
    // cout<<b[i]<<" ";
  }
  // cout<<endl;

  // REP(i, n+1) cout<<a_sum[i]<<" ";
  // cout<<endl;
  // REP(i, m+1) cout<<b_sum[i]<<" ";
  // cout<<endl;

  ll ans = 0;

  REP(i, n + 1) {

    ll left = 0;
    ll right = b_sum.size() - 1;
    ll mid = (left + right) / 2;

    if (a_sum[i] + b_sum[mid] < k) {
      left = mid;
    } else {
      right = mid;
    }
    // cout<<left<<" "<<mid<<" "<<right<<endl;

    for (ll j = left; j <= right; j++) {
      // cout<<i<<" "<<j<<endl;

      if (a_sum[i] + b_sum[j] <= k) {
        ans = max(ans, i + j);
        // if(i==j && i==0) ans -=2;
        // else if(i==0 || j==0) ans-=1;
        // cout<<ans<<endl;
      } else
        break;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++) // REP(i, 5) cout<<i;
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define MOD 1000000007 // 10^9+7
// #define MOD 1

ll gcd(ll a, ll b) {
  ll tmp;
  if (b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b != 0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

const int MAX = 2000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void sosuu(ll n, vector<ll> &v) {

  vector<ll> num(n, 0);

  FOR(i, 1, sqrt(n)) {
    if (num[i] == 0) {
      FOR(j, 0, n / i) { num[i + (j * i)] = 1; }
    }
  }
  REP(i, n) {
    if (num[i] == 0)
      v.push_back(i + 1);
  }
}

ll GetDigit(ll num) {
  ll digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

ll power(ll a, ll b) {
  ll ans = 1;
  if (b % 2 == 0) {
    REP(i, b / 2) {
      ans *= a;
      if (ans > 1000000000) {
        return -1;
      }
    }
    return ans * ans;
  } else {
    b = b / 2;
    REP(i, b) {
      ans *= a;
      if (ans > 1000000000) {
        return -1;
      }
    }
    return ans * ans * a;
  }
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main(void) {

  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> a(n);
  vector<ll> b(m);
  vector<ll> a_sum(n + 1);
  vector<ll> b_sum(m + 1);
  a_sum[0] = 0;
  b_sum[0] = 0;

  REP(i, n) {
    ll x;
    cin >> x;
    a[i] = x;
    // if(i!=0)
    a_sum[i + 1] = a_sum[i] + x;
    // else a_sum[i] = x;
    // cout<<a[i]<<" ";
  }
  // cout<<endl;
  REP(i, m) {
    ll x;
    cin >> x;
    b[i] = x;
    // if(i!=0)
    b_sum[i + 1] = b_sum[i] + x;
    // else b_sum[i] = x;
    // cout<<b[i]<<" ";
  }
  // cout<<endl;

  // REP(i, n+1) cout<<a_sum[i]<<" ";
  // cout<<endl;
  // REP(i, m+1) cout<<b_sum[i]<<" ";
  // cout<<endl;

  ll ans = 0;

  REP(i, n + 1) {

    ll left = 0;
    ll right = b_sum.size() - 1;
    ll mid = (left + right) / 2;

    ll count = 0;
    while (count != 1000) {
      if (a_sum[i] + b_sum[mid] < k) {
        left = mid;
      } else {
        right = mid;
      }
      mid = (left + right) / 2;
      count++;
    }
    // cout<<left<<" "<<mid<<" "<<right<<endl;

    for (ll j = left; j <= right; j++) {
      // cout<<i<<" "<<j<<endl;

      if (a_sum[i] + b_sum[j] <= k) {
        ans = max(ans, i + j);
        // if(i==j && i==0) ans -=2;
        // else if(i==0 || j==0) ans-=1;
        // cout<<ans<<endl;
      } else
        break;
    }
  }

  cout << ans << endl;

  return 0;
}