Problem:
Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.

Solution in C# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

namespace C_ABC172
{
    class MainClass
    {

        static long[] s, a, b;

        public static void Main(string[] args)
        {
            s = Array.ConvertAll(ReadLine().Split().ToArray(), long.Parse);
            a = Array.ConvertAll(ReadLine().Split().ToArray(), long.Parse);
            b = Array.ConvertAll(ReadLine().Split().ToArray(), long.Parse);
            long n = s[0];
            long m = s[1];
            long k = s[2];

            var sa = new List<long>();
            var sb = new List<long>();


            long atmp = 0;
            long btmp = 0;

            bool aover = false;
            bool bover = false;

            for (long i = 0; i < Max(m, n); i++)
            {
                if (!aover && !bover)
                {
                    atmp += a[i];
                    if (atmp <= k)
                    {
                        sa.Add(atmp);
                        if (i == n - 1) aover = true;
                    }
                    else
                    {
                        aover = true;
                    }
                    btmp += b[i];
                    if (btmp <= k)
                    {
                        sb.Add(btmp);
                        if (i == m - 1) bover = true;
                    }
                    else
                    {
                        bover = true;
                    }
                }
                else if (!aover)
                {
                    atmp += a[i];
                    if (atmp <= k)
                    {
                        sa.Add(atmp);
                        if (i == n - 1) aover = true;
                    }
                    else
                    {
                        aover = true;
                    }
                }
                else if (!bover)
                {
                    btmp += b[i];
                    if (btmp <= k)
                    {
                        sb.Add(btmp);
                        if (i == m - 1) bover = true;
                    }
                    else
                    {
                        bover = true;
                    }
                }
                else
                {
                    break;
                }
            }

            long ans = 0;

            var sbb = sb.ToArray();


            for (int i = 0; i < sa.Count; i++)
            {
                var t = LowerBound(sbb, (k - sa[i]));
                // WriteLine("t: {0} i: {1}", t, i);
                if (t < sb.Count && sbb[t] == k - sa[i])
                {
                    // WriteLine("here");
                    t++;

                }
                else if (t == sb.Count)
                {
                    // WriteLine("here2");
                    t--;
                }
                ans = Max(ans, t  + (i + 1));
            }

            WriteLine(ans);






        }
        public static int LowerBound<T>(T[] a, T v)
        {
            return LowerBound(a, v, Comparer<T>.Default);
        }

        public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Length - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res == -1) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }
    }
}