路上的球

Description
There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls. The buckets on both roads are kept in such a way that they are sorted according to the number of balls in them. Geek starts from the end of the road which has the bucket with a lower number of balls(i.e. if buckets are sorted in increasing order, then geek will start from the left side of the road). The geek can change the road only at the point of intersection(which means, buckets with the same number of balls on two roads). Now you need to help Geek to collect the maximum number of balls.

Input
The first line of input contains T denoting the number of test cases. The first line of each test case contains two integers N and M, denoting the number of buckets on road1 and road2 respectively. 2nd line of each test case contains N integers, number of balls in buckets on the first road. 3rd line of each test case contains M integers, number of balls in buckets on the second road.

Constraints:1<= T <= 1000,1<= N <= 10^3,1<= M <=10^3,0<= A[i],B[i]<=10^6

Output
For each test case output a single line containing the maximum possible balls that Geek can collect.

Sample Input 1
1
5 5
1 4 5 6 8
2 3 4 6 9

Sample Output 1
29

def solution(l2, l3, m ,n):
    i = 0
    j = 0
    start2 = 0
    start3 = 0
    res = 0
    while i < m and j < n:
        if l2[i] < l3[j]:
            i += 1
        elif l2[i] > l3[j]:
            j += 1
        else:
            res += max(sum(l2[start2: i+1]), sum(l3[start3: j+1]))
            i += 1
            j += 1
            start2 = i
            start3 = j
    if len(l2[start2:]) != 0 and len(l3[start3:]) != 0:
        res += max(sum(l2[start2:]), sum(l3[start3:]))
    elif len(l2[start2:]) != 0:
        res += sum(l2[start2:])
    elif len(l3[start3:]) != 0:
        res += sum(l3[start3:])
    return res


if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        l1 = list(map(int, input().strip().split()))
        m, n = l1[0], l1[1]
        l2 = list(map(int, input().strip().split()))
        l3 = list(map(int, input().strip().split()))
        result = solution(l2, l3, m, n)
        print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值