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

#include <iostream>
using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  long an[200001], am[200001];
  an[0] = 0;
  am[0] = 0;
  for (int i = 1; i <= n; i++) {
    int nn;
    cin >> nn;
    an[i] = an[i - 1] + nn;
    // cout << an[i] << " ";
  }
  // cout << endl;
  for (int i = 1; i <= m; i++) {
    int nm;
    cin >> nm;
    am[i] = am[i - 1] + nm;
    // cout << am[i] << " ";
  }
  // cout << endl;
  int max{0};
  int bestj = m;
  for (int i = 0; i <= n; i++) {
    int sum{0}, result{0};
    /*
    for(int j = 0; j < i; j++)
    {
        sum += an[j+1];
    }
    */
    if (an[i] > k)
      break;
    result += i;

    for (int j = bestj; j > 0; j--) {
      if (am[j] + an[i] <= k) {
        bestj = j;
        result += j;
        // cout << result << " " << am[j] << endl;
        break;
      }
      // cout << result << endl;
    }
    // cout << "result: " << result << " sum: " << sum << endl;
    /*
    for(int j = 0; j < m; j++)
    {
        if(sum + am[j+1] <= k)
        {
            sum += am[j+1];
            result++;
        }
        else
            break;

    }
    */
    // cout << result << endl;
    if (result > max)
      max = result;
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

/*
int min(int n, int m)
{
    if(n < m)
        return n;
    return m;
}
int main()
{
    int n, m, k; cin >> n >> m >> k;
    int an[200000], am[200000];
    for(int i = 0; i < n; i++)
        cin >> an[i];
    for(int i = 0; i < m; i++)
        cin >> am[i];
    int nn = 0, nm = 0;
    int sum{0}, result{0};
    for(int i = 0; i < n + m; i++)
    {

        if(nm >= m)
        {
            if(an[nn] + sum <= k)
            {
                sum += an[nn];
                nn++;
                result++;
            }
            continue;
        }
        if(nn >= n)
        {
            if(am[nm] + sum <= k)
            {
                sum += am[nm];
                nm++;
                result++;
            }
            continue;
        }
        if(an[nn] < am[nm])
        {
            if(an[nn] + sum <= k)
            {
                sum += an[nn];
                nn++;
                result++;
                //cout << sum << endl;
            }
            continue;
        }
        else if(an[nn] > am[nm])
        {
            if(am[nm] + sum <= k)
            {
                sum += am[nm];
                nm++;
                result++;
                //cout << sum << endl;
            }
            continue;
        }
        else
        {
            int a{0};
            for(int j = 0; j < min(n, m) && a == 0; j++)
            {
                if(an[nn + j] > am[nm + j])
                {
                    if(am[nm + j] + sum <= k)
                    {
                        sum += am[nm + j];
                        nm++;
                        result++;
                        //cout << sum << endl;
                        a = 1;
                        break;
                    }
                }
                else if(an[nn + j] < am[nm + j])
                {
                    if(an[nn + j] + sum <= k)
                    {
                        sum += an[nn + j];
                        nn++;
                        result++;
                        //cout << sum << endl;
                        a = 1;
                        break;
                    }
                }
            }
            if(a == 0)
            {
                if(n > m)
                {
                    if(an[nn] + sum <= k)
                    {
                        sum += an[nn];
                        nn++;
                        result++;
                        //cout << sum << endl;
                    }
                }
                else
                {
                    if(am[nm] + sum <= k)
                    {
                        sum += am[nm];
                        nm++;
                        result++;
                        //cout << sum << endl;
                    }
                }

            }

        }



    }
    cout << result;
    return 0;
}
*/

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int an[200001], am[200001];
  an[0] = 0;
  am[0] = 0;
  for (int i = 1; i <= n; i++)
    cin >> an[i];
  for (int i = 1; i <= m; i++)
    cin >> am[i];
  int max{0};
  for (int i = 0; i <= n; i++) {
    int sum{0}, result{0};
    for (int j = 0; j < i; j++) {
      sum += an[j + 1];
    }
    if (sum > k)
      break;
    result += i;
    // cout << "result: " << result << " sum: " << sum << endl;
    for (int j = 0; j < m; j++) {
      if (sum + am[j + 1] <= k) {
        sum += am[j + 1];
        result++;
      } else
        break;
    }
    // cout << result << endl;
    if (result > max)
      max = result;
  }
  cout << max << endl;
  return 0;
}