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

#include <bits/stdc++.h>
#define ld long double
#define ll long long
using namespace std;

int main() {

  ll n, m, k, i, c, j, mx = 0;
  map<ll, ll> A;
  map<ll, ll> B;
  cin >> n >> m >> k;
  for (i = 0; i < n; i++) {
    cin >> c;
    A[i] = c;
  }
  for (i = 0; i < m; i++) {
    cin >> c;
    B[i] = c;
  }
  for (i = 1; i < n; i++) {
    A[i] += A[i - 1];
  }
  for (i = 1; i < m; i++) {
    B[i] += B[i - 1];
  }

  ll note = n, in = 0;
  for (i = 0; i < n; i++) {
    if (A[i] > k) {
      note = i;
      break;
    }
  }

  if (note == 0) {
    ll u = m;
    for (i = 0; i < m; i++) {
      if (B[i] > k) {
        u = i;
        break;
      }
    }
    mx = max(mx, u);
  }

  else {
    while (note--) {
      ll f = 0;
      for (j = in; j < m; j++) {
        if (A[note] + B[j] > k) {
          mx = max(mx, note + 1 + j);
          in = j;
          f = 1;
          break;
        }
      }
      if (f == 0) {
        mx = max(mx, note + 1 + m);
        break;
      }
    }
  }

  note = m;
  in = 0;
  for (i = 0; i < m; i++) {
    if (B[i] > k) {
      note = i;
      break;
    }
  }

  if (note == 0) {
    ll u = n;
    for (i = 0; i < n; i++) {
      if (A[i] > k) {
        u = i;
        break;
      }
    }
    mx = max(mx, u);
  }

  else {
    while (note--) {
      ll f = 0;
      for (j = in; j < n; j++) {
        if (B[note] + A[j] > k) {
          mx = max(mx, note + 1 + j);
          in = j;
          f = 1;
          break;
        }
      }
      if (f == 0) {
        mx = max(mx, note + 1 + n);
        break;
      }
    }
  }

  cout << mx;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ld long double
#define ll long long
using namespace std;

int main() {

  ll n, m, k, i, c, j, mx = 0;
  map<ll, ll> A;
  map<ll, ll> B;
  cin >> n >> m >> k;
  for (i = 0; i < n; i++) {
    cin >> c;
    A[i] = c;
  }
  for (i = 0; i < m; i++) {
    cin >> c;
    B[i] = c;
  }
  for (i = 1; i < n; i++) {
    A[i] += A[i - 1];
  }
  for (i = 1; i < m; i++) {
    B[i] += B[i - 1];
  }

  ll note = n, in = 0;
  for (i = 0; i < n; i++) {
    if (A[i] > k) {
      note = i;
      break;
    }
  }

  if (note == 0) {
    ll u = m;
    for (i = 0; i < m; i++) {
      if (B[i] > k) {
        u = i;
        break;
      }
    }
    mx = max(mx, u);
  }

  else {
    while (note--) {
      ll f = 0;
      for (j = in; j < m; j++) {
        if (A[note] + B[j] > k) {
          mx = max(mx, note + 1 + j);
          in = j;
          f = 1;
          break;
        }
      }
      if (f == 0)
        mx = max(mx, note + 1 + m);
    }
  }

  note = m;
  in = 0;
  for (i = 0; i < m; i++) {
    if (B[i] > k) {
      note = i;
      break;
    }
  }

  if (note == 0) {
    ll u = n;
    for (i = 0; i < n; i++) {
      if (A[i] > k) {
        u = i;
        break;
      }
    }
    mx = max(mx, u);
  }

  else {
    while (note--) {
      ll f = 0;
      for (j = in; j < n; j++) {
        if (B[note] + A[j] > k) {
          mx = max(mx, note + 1 + j);
          in = j;
          f = 1;
          break;
        }
      }
      if (f == 0)
        mx = max(mx, note + 1 + n);
    }
  }

  cout << mx;
  return 0;
}