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 System.Numerics;

public class ABC172_C
{
    public static void Main()
    {
        Inline(out int n, out int m, out long k);
        Inline(out long[] ints1);
        Inline(out long[] ints2);
        int a = 0, b = 0, res = 0;
        bool fin = false;
        while (true)
        {
            if(a == -1)
            {
                k -= ints2[b];
                b++;
                if (b == m) fin = true;
            }
            else if(b == -1)
            {
                k -= ints1[a];
                a++;
                if (a == n) fin = true;
            }
            else if (ints1[a] > ints2[b])
            {
                k -= ints2[b];
                b++;
                if (b == m) b = -1;
            }
            else
            {
                k -= ints1[a];
                a++;
                if (a == n) a = -1;
            }

            if (k < 0) break;
            else res++;
            if (fin) break;
        }
        Out(res);
    }

    /* Template */
    //入力:1入力
    public static void Inline<A>(out A var)
    {
        Inline(out var, out object _, out object _, out object _, out object _);
    }
    //入力:2入力
    public static void Inline<A, B>(out A var1, out B var2)
    {
        Inline(out var1, out var2, out object _, out object _, out object _);
    }
    //入力:3入力
    public static void Inline<A, B, C>(out A var1, out B var2, out C var3)
    {
        Inline(out var1, out var2, out var3, out object _, out object _);
    }
    //入力:4入力
    public static void Inline<A, B, C, D>(out A var1, out B var2, out C var3, out D var4)
    {
        Inline(out var1, out var2, out var3, out var4, out object _);
    }
    //入力:5入力
    public static void Inline<A, B, C, D, E>(out A var1, out B var2, out C var3, out D var4, out E var5)
    {
        var o = new object[5];
        var t = new Type[] { typeof(A), typeof(B), typeof(C), typeof(D), typeof(E) };
        string[] s = Console.ReadLine().Split(' ');
        for (int i = 0; i < s.Length; i++)
        {
            if (typeof(int) == t[i]) o[i] = int.Parse(s[i]);
            else if (typeof(long) == t[i]) o[i] = long.Parse(s[i]);
            else if (typeof(double) == t[i]) o[i] = double.Parse(s[i]);
            else if (typeof(decimal) == t[i]) o[i] = decimal.Parse(s[i]);
            else o[i] = s[i];
        }
        var1 = (A)o[0];
        var2 = (B)o[1];
        var3 = (C)o[2];
        var4 = (D)o[3];
        var5 = (E)o[4];
    }

    //入力:1行配列
    public static void Inline<A>(out A[] var)
    {
        InlineArg(out var);
    }
    //入力:1行配列
    public static void InlineArg<A>(out A[] arg)
    {
        object obj = null;
        Type t = typeof(A);
        string[] line = Console.ReadLine().Split(' ');
        if (typeof(int) == t) obj = line.Select(int.Parse).ToArray();
        else if (typeof(long) == t) obj = line.Select(long.Parse).ToArray();
        else if (typeof(double) == t) obj = line.Select(double.Parse).ToArray();
        else if (typeof(decimal) == t) obj = line.Select(decimal.Parse).ToArray();
        else obj = line;
        arg = (A[])obj;
    }

    //文字列結合
    public static string JoinString(object obj, string sep)
    {
        return obj.GetType().IsArray ?
            string.Join(sep, ((IEnumerable<object>)obj).Select(i => i.ToString()).ToArray()) : obj.ToString();
    }

    //出力
    public static void Out(object o)
    {
        Console.WriteLine(o);
    }
    //出力:Yes
    public static void OutYes()
    {
        Console.WriteLine("Yes");
    }
    //出力:No
    public static void OutNo()
    {
        Console.WriteLine("No");
    }
    //出力:YES
    public static void OutYES()
    {
        Console.WriteLine("YES");
    }
    //出力:NO
    public static void OutNO()
    {
        Console.WriteLine("NO");
    }

    //最大
    public static int[] Max(int[] array, int count)
    {
        int[] result = new int[count];
        for (int c = 0; c < count; c++)
        {
            int max = -1;
            int plus = Vector<int>.Count;
            var vmax = new Vector<int>(max);
            for (int i = 0; i < array.Length - plus; i += plus)
            {
                vmax = Vector.Max(vmax, new Vector<int>(array, i));
            }
            for (int i = 0; i < plus; i++)
            {
                if (max < vmax[i]) max = vmax[i];
            }
            for (int i = array.Length - plus; i < array.Length; i++)
            {
                if (i < 0) i = 0;
                if (max < array[i]) max = array[i];
            }

            result[c] = max;
            if (c != count - 1)
            {
                var list = new List<int>(array);
                list.Remove(max);
                array = list.ToArray();
            }
        }
        return result;
    }

    //最小
    public static int[] Min(int[] array, int count)
    {
        int[] result = new int[count];
        for (int c = 0; c < count; c++)
        {
            int min = int.MaxValue;
            int plus = Vector<int>.Count;
            var vmax = new Vector<int>(min);
            for (int i = 0; i < array.Length - plus; i += plus)
            {
                vmax = Vector.Min(vmax, new Vector<int>(array, i));
            }
            for (int i = 0; i < plus; i++)
            {
                if (min > vmax[i]) min = vmax[i];
            }
            for (int i = array.Length - plus; i < array.Length; i++)
            {
                if (i < 0) i = 0;
                if (min > array[i]) min = array[i];
            }

            result[c] = min;
            if (c != count - 1)
            {
                var list = new List<int>(array);
                list.Remove(min);
                array = list.ToArray();
            }
        }
        return result;
    }

    //約数
    public static int[] Divisor(int num)
    {
        var list = new List<int>();
        int count = 0;

        for (int i = 0; i * i <= num; i++)
        {
            if (num % i == 0)
            {
                var n = num / i;
                list.Insert(count, i);
                if (i != n) list.Insert(count + 1, n);
                else break;
                count++;
            }
        }
        return list.ToArray();
    }
    public static decimal[] Divisor(decimal num)
    {
        var list = new List<decimal>();
        int count = 0;

        for (decimal d = 0; d * d <= num; d++)
        {
            if (num % d == 0)
            {
                var n = num / d;
                list.Insert(count, d);
                if (d != n) list.Insert(count + 1, n);
                else break;
                count++;
            }
        }
        return list.ToArray();
    }

    //素因数分解
    public static int[] PrimeFactorization(int num)
    {
        int l = 2, n = num;
        var result = new int[0];
        while (l * l <= num)
        {
            if (n % l == 0)
            {
                n /= l;
                Array.Resize(ref result, result.Length + 1);
                result[^1] = l;
            }
            else l++;
        }
        if (n != 1)
        {
            Array.Resize(ref result, result.Length + 1);
            result[^1] = n;
        }
        return result;
    }
    public static decimal[] PrimeFactorization(decimal num)
    {
        decimal l = 2, n = num;
        var result = new decimal[0];
        while (l * l <= num)
        {
            if (n % l == 0)
            {
                n /= l;
                Array.Resize(ref result, result.Length + 1);
                result[^1] = l;
            }
            else l++;
        }
        if (n != 1)
        {
            Array.Resize(ref result, result.Length + 1);
            result[^1] = n;
        }
        return result;
    }

    //大小比較
    public static bool Compare(bool lequal, decimal a, decimal x, decimal b, bool requal)
    {
        if (a <= x && x <= b)
        {
            if ((!lequal && x == a) || (!requal && x == b)) return false;
            else return true;
        }
        return false;
    }

    //互いに素
    public static bool IsRelativelyPrime(decimal a, decimal b)
    {
        return a > 1 && b > 1 ? Divisor(a).Intersect(Divisor(b)).ToArray().Length == 0 : false;
    }

    //素数判定
    public static bool IsPrimeNumber(decimal num)
    {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false;

        for (decimal i = 3; i * i <= num; i += 2)
        {
            if (num % i == 0) return false;
        }
        return true;
    }

    //最大公約数
    public static int GCD(int a, int b)
    {
        return (int)GCD((decimal)a, b);
    }
    public static decimal GCD(decimal a, decimal b)
    {
        decimal[] acd = Divisor(a).ToArray(), bcd = Divisor(b).ToArray();
        Dictionary<decimal, int> dic1 = new Dictionary<decimal, int>(), dic2 = new Dictionary<decimal, int>();
        int count = 0;
        decimal n = 0, result = 1;
        for (int i = 0; i < acd.Length; i++)
        {
            if (n == acd[i]) count++;
            else
            {
                if (n != 0) dic1.Add(n, count);
                n = acd[i];
                count = 1;
            }
        }
        dic1.Add(n, count);
        n = 0;
        count = 0;
        for (int i = 0; i < bcd.Length; i++)
        {
            if (n == bcd[i]) count++;
            else
            {
                if (dic1.ContainsKey(n)) dic2[n] = Math.Max(count, dic1[n]);
                n = bcd[i];
                count = 1;
            }
        }
        if (dic1.ContainsKey(n)) dic2[n] = Math.Max(count, dic1[n]);
        foreach (decimal d in dic2.Keys)
        {
            result *= (decimal)Math.Pow((double)d, dic2[d]);
        }
        return result;
    }

    //最小公倍数
    public static decimal LCM(decimal a, decimal b)
    {
        decimal[] acd = Divisor(a).ToArray(), bcd = Divisor(b).ToArray();
        var dic = new Dictionary<decimal, int>();
        int count = 0;
        decimal n = 0, result = 1;
        for (int i = 0; i < acd.Length; i++)
        {
            if (n == acd[i]) count++;
            else
            {
                if (n != 0) dic.Add(n, count);
                n = acd[i];
                count = 1;
            }
        }
        dic.Add(n, count);
        n = 0;
        count = 0;
        for (int i = 0; i < bcd.Length; i++)
        {
            if (n == bcd[i]) count++;
            else
            {
                if (dic.ContainsKey(n)) dic[n] = Math.Max(count, dic[n]);
                else if (n != 0) dic.Add(n, count);
                n = bcd[i];
                count = 1;
            }
        }
        if (dic.ContainsKey(n)) dic[n] = Math.Max(count, dic[n]);
        else dic.Add(n, count);
        foreach (decimal l in dic.Keys)
            result *= (decimal)Math.Pow((double)l, dic[l]);
        return result;
    }

    //組合せ数(nCr)
    public static decimal Combination(int n, int r)
    {
        if (n / 2 < r) r = n - r;
        return TotalPower(n, r + 1) / TotalPower(n - r);
    }

    //組合せ数総和(Σ(k=1,n,nCk))
    public static double CombinationSum(int n)
    {
        return Math.Pow(2, n);
    }

    //重複組合せ数(nHr)
    public static decimal RepetitionCombination(int n, int r)
    {
        return Combination(n + r - 1, r);
    }

    //重複組合せ数総和(Σ(k=1,n,nHk))
    public static decimal RepetitionCombinationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += RepetitionCombination(n, i);
        }
        return result;
    }

    //順列数(nPr)
    public static decimal Permutation(int n, int r)
    {
        return TotalPower(n, r + 1);
    }

    //順列数総和(Σ(k=1,n,nPk))
    public static decimal PermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += Permutation(n, i);
        }
        return result;
    }

    //円順列数
    public static decimal CircularPermutation(int n)
    {
        return TotalPower(n - 1);
    }

    //円順列数総和
    public static decimal CircularPermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += CircularPermutation(i);
        }
        return result;
    }

    //数珠順列数
    public static decimal NecklacePermutation(int n)
    {
        return CircularPermutation(n - 1) / 2;
    }

    //数珠順列数総和
    public static decimal NecklacePermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += NecklacePermutation(i);
        }
        return result;
    }

    //重複順列数(nΠr)
    public static decimal RepetitionPermutation(int n, int r)
    {
        return (decimal)Math.Pow(n, r);
    }

    //重複順列数総和(Σ(k=1,n,nΠk))
    public static decimal RepetitionPermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += RepetitionPermutation(n, i);
        }
        return result;
    }

    //総乗
    public static decimal TotalPower(int max)
    {
        return TotalPower(max, 1);
    }
    public static decimal TotalPower(int max, int min)
    {
        decimal result = 1;
        for (int i = min; i <= max; i++) result *= i;
        return result;
    }
    public static decimal TotalPower(int[] ints)
    {
        decimal result = 1;
        foreach (int i in ints) result *= i;
        return result;
    }

    //総和
    public static decimal TotalSum(int max)
    {
        return TotalSum(max, 1);
    }
    public static decimal TotalSum(int max, int min)
    {
        decimal result = 1;
        for (int i = min; i <= max; i++) result += i;
        return result;
    }
    public static decimal TotalSum(int[] ints)
    {
        decimal result = 1;
        foreach (int i in ints) result += i;
        return result;
    }
}