Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):

// Problem : C - Tsundoku
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*

WINNERS NEVER QUIT AND QUITTERS NEVER WIN!!

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<long, long> pl;
typedef pair<ll, ll> pll;
typedef vector<long> vl;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;

#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define REV(i, a, b) for (long long i = a; i >= b; i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define tc                                                                     \
  ll tests;                                                                    \
  cin >> tests;                                                                \
  while (tests--)
#define io                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define coutv(v)                                                               \
  for (auto it : (v))                                                          \
    cout << it << " ";                                                         \
  newl;
#define cout2d(v)                                                              \
  for (auto it : (v)) {                                                        \
    for (auto j : it)                                                          \
      cout << j << " ";                                                        \
    newl;                                                                      \
  }
#define cinv(v, n)                                                             \
  vll(v)(n);                                                                   \
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cinvg(v, n)                                                            \
  (v).resize(n);                                                               \
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cin2d(v, n, m)                                                         \
  vvll(v)(n, vll(m, 0));                                                       \
  FOR(i, 0, n) {                                                               \
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \
  }
#define cin2dg(v, n, m)                                                        \
  (v).resize(n, vll(m));                                                       \
  FOR(i, 0, n) {                                                               \
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \
  }
#define newl cout << "\n"
#define mod 1000000007
#define INF LLONG_MAX / 2

int main() {
  io;
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  cinv(a, n) cinv(b, m) vll pre_a(n + 3), pre_b(m + 3);
  pre_a[0] = 0;
  pre_b[0] = 0;
  FOR(i, 1, n + 1) { pre_a[i] = pre_a[i - 1] + a[i - 1]; }

  FOR(i, 1, m + 1) { pre_b[i] = pre_b[i - 1] + b[i - 1]; }

  ll ind = 0;
  REV(i, n, 0) // start with max index in a and decrease
  {
    FOR(j, ind, m + 1) // increase index of B
    {
      if (pre_a[i] + pre_b[j] <= k) {
        ans = max(ans, i + j);
      } else {
        ind = j;
        break;
      }
    }
  }

  cout << ans;
  return 0;
}


Fixed submission:

// Problem : C - Tsundoku
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*

WINNERS NEVER QUIT AND QUITTERS NEVER WIN!!

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<long, long> pl;
typedef pair<ll, ll> pll;
typedef vector<long> vl;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;

#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define REV(i, a, b) for (long long i = a; i >= b; i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define tc                                                                     \
  ll tests;                                                                    \
  cin >> tests;                                                                \
  while (tests--)
#define io                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define coutv(v)                                                               \
  for (auto it : (v))                                                          \
    cout << it << " ";                                                         \
  newl;
#define cout2d(v)                                                              \
  for (auto it : (v)) {                                                        \
    for (auto j : it)                                                          \
      cout << j << " ";                                                        \
    newl;                                                                      \
  }
#define cinv(v, n)                                                             \
  vll(v)(n);                                                                   \
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cinvg(v, n)                                                            \
  (v).resize(n);                                                               \
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cin2d(v, n, m)                                                         \
  vvll(v)(n, vll(m, 0));                                                       \
  FOR(i, 0, n) {                                                               \
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \
  }
#define cin2dg(v, n, m)                                                        \
  (v).resize(n, vll(m));                                                       \
  FOR(i, 0, n) {                                                               \
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \
  }
#define newl cout << "\n"
#define mod 1000000007
#define INF LLONG_MAX / 2

int main() {
  io;
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  cinv(a, n) cinv(b, m) vll pre_a(n + 3), pre_b(m + 3);
  pre_a[0] = 0;
  pre_b[0] = 0;
  FOR(i, 1, n + 1) { pre_a[i] = pre_a[i - 1] + a[i - 1]; }

  FOR(i, 1, m + 1) { pre_b[i] = pre_b[i - 1] + b[i - 1]; }

  ll ind = 0;
  REV(i, n, 0) // start with max index in a and decrease
  {
    FOR(j, ind, m + 1) // increase index of B
    {
      if (pre_a[i] + pre_b[j] <= k) {
        ans = max(ans, i + j);
      } else {
        ind = j;
        break;
      }
    }
    if (ans == n + m) {
      break;
    }
  }

  cout << ans;
  return 0;
}