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

#include <bits/stdc++.h>
#define Abra_Ka_Dabra                                                          \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;

const long long mod = 1e9 + 7;
const double PI = acos(-1);
const double EPS = 1e-9;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  Abra_Ka_Dabra

      int n,
      m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> x(n), y(m);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> y[i];
  }
  for (int i = 1; i < n; i++) {
    x[i] += x[i - 1];
  }
  for (int i = 1; i < m; i++) {
    y[i] += y[i - 1];
  }
  int res = 0;
  for (int i = 0; i < n && x[i] <= k; i++) {
    int l = 0, r = m - 1, maxi = i + 1;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      ll tmp = x[i] + y[mid];
      if (tmp <= k) {
        maxi = i + mid + 2;
        l = mid + 1;
      } else
        r = mid - 1;
    }
    res = max(res, maxi);
  }
  for (int i = 0; i < m && y[i] <= k; i++) {
    int l = 0, r = n - 1, maxi = i + 1;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      ll tmp = y[i] + x[mid];
      if (tmp <= k) {
        maxi = i + mid + 2;
        l = mid + 1;
      } else
        r = mid - 1;
    }
    res = max(res, maxi);
  }
  cout << res << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
unsigned long long int *readStack(int length) {
  unsigned long long int *stack = new unsigned long long int[length];
  for (int i = 0; i < length; i++) {
    cin >> stack[i];
    // cout << stack[i] << " ";
  }
  // cout << endl;
  return stack;
}
unsigned long long int getSum(unsigned long long int *s, int l) {
  unsigned long long int sum = 0;
  for (int i = 0; i < l; i++)
    sum += s[i];
  return sum;
}
int main() {
  int n, m;
  unsigned long long int k;
  cin >> n >> m >> k;
  unsigned long long int *aStack = readStack(n);
  unsigned long long int *bStack = readStack(m);
  // check if time sum of stack is <= k
  if (getSum(aStack, n) + getSum(bStack, m) <= k)
    cout << m + n;
  else {
    int numOfBooks = 0;
    bool overFlowFlag = false;
    for (int i = -1; i < n; i++) {
      for (int j = -1; j < m; j++) {
        unsigned long long int time = 0;
        // cout << i << " " << j << endl;
        if (i == -1 && j == -1)
          continue;
        if (i >= 0)
          time += getSum(aStack, i + 1);
        // cout << time << endl;
        if (time > k) {
          overFlowFlag = true;
          break;
        }
        if (j >= 0)
          time += getSum(bStack, j + 1);
        // cout << time << endl;
        if (time > k) {
          break;
        }
        if (time <= k && i + j + 2 > numOfBooks)
          numOfBooks = i + j + 2;
      }
      if (overFlowFlag) {
        // cout << "bye" << endl;
        break;
      }
    }
    cout << numOfBooks;
  }
}