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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define llu unsigned long long
#define U unsigned int
#define ff first
#define ss second
#define SIZE 100005
#define pie 3.14159265358979323
#define minuss 1e-6
#define pair pair<ll, ll>
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define BIG 1000000000000
#define INF 1000000000000005
#define N 10000
vector<ll> vec;
map<string, ll> mps;
map<ll, string> mpss;
set<ll> s1;
set<string> s2;
map<string, ll>::iterator str;
set<ll>::iterator itr;
/*
std::cout << std::fixed;
std::cout << std::setprecision(1);
std::cout << VALUE NAME<<endl;
*/
ll tra[100006], tra1[100006];

int main() {
  fast_cin;
  ll t;

  map<ll, ll> mp;
  map<ll, ll>::iterator it;
  string s, s1;
  ll lock = 0, sum = 0, co = 0, ans;
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1], aa[n + 1], bb[m + 1];
  aa[0] = 0;
  bb[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    aa[i] = aa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    bb[i] = bb[i - 1] + b[i];
  }

  sum = n + m;
  while (co <= sum) {
    ll mid = (co + sum) / 2;
    lock = BIG;
    for (int i = 0; i <= mid; i++) {
      if (i <= n && (mid - i) <= m) {
        lock = min(lock, aa[i] + bb[mid - i]);
      }
    }

    if (lock == k) {
      ans = mid;
      break;
    } else if (lock < k) {
      ans = mid;
      co = mid + 1;
    } else if (lock > k)
      sum = mid - 1;
  }
  cout << ans;

  s1.clear();
  vec.clear();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define llu unsigned long long
#define U unsigned int
#define ff first
#define ss second
#define SIZE 100005
#define pie 3.14159265358979323
#define minuss 1e-6
#define pair pair<ll, ll>
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define BIG 1000000000
#define INF 1000000000000005
#define N 10000
vector<ll> vec;
map<string, ll> mps;
map<ll, string> mpss;
set<ll> s1;
set<string> s2;
map<string, ll>::iterator str;
set<ll>::iterator itr;
/*
std::cout << std::fixed;
std::cout << std::setprecision(1);
std::cout << VALUE NAME<<endl;
*/
ll tra[100006], tra1[100006];

int main() {
  fast_cin;
  ll t;
  cin >> t;
  while (t--) {
    map<ll, ll> mp;
    map<ll, ll>::iterator it;
    string s, s1;
    ll lock = 0, sum = 0, co = 0, ans;
    ll n, m, k;
    cin >> n >> m >> k;
    ll a[n + 1], b[m + 1], aa[n + 1], bb[m + 1];
    aa[0] = 0;
    bb[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      aa[i] = aa[i - 1] + a[i];
    }
    for (int i = 1; i <= m; i++) {
      cin >> b[i];
      bb[i] = bb[i - 1] + b[i];
    }
    ll ma = BIG;
    sum = n + m;
    while (co <= sum) {
      ll mid = (co + sum) / 2;
      t = ma;
      for (int i = 0; i <= mid; i++) {
        if (i <= n && (mid - i) <= m) {
          lock = min(lock, aa[i] + bb[mid - i]);
        }
      }

      if (t == k) {
        ans = mid;
        break;
      } else if (lock < k) {
        ans = mid;
        co = mid + 1;
      } else if (lock > k) {
        sum = mid - 1;
      }
    }
    cout << ans;

    s1.clear();
    vec.clear();
  }
}