Sample Input 0

1
5 4 10
4 2 4 6 1
2 1 8 5

Sample Output 0

4

Explanation 0

The two stacks initially look like this:

The image below depicts the integers Nick should choose to remove from the stacks. We printas our answer, because that is the maximum number of integers that can be removed from the two stacks without the sum exceeding.

(There can be multiple ways to remove the integers from the stack, the image shows just one of them.)

#!/bin/python

import sys


g = int(raw_input().strip())
for a0 in xrange(g):
    n,m,x = raw_input().strip().split(' ')
    n,m,x = [int(n),int(m),int(x)]
    a = map(int, raw_input().strip().split(' '))
    b = map(int, raw_input().strip().split(' '))
    c=0
    # your code goes here

    i, j, sum = 0,0,0
    while i<n and (sum+a[i])<=x:
        sum+=a[i]
        i+=1
    ans=i
    while j<m and i>=0:
        sum+=b[j]
        j+=1
        while sum>x and i>0:
            i-=1
            sum-=a[i]
        if sum<=x and i+j>ans:
            ans=i+j
    print(ans)

results matching ""

    No results matching ""