Showing:
Handle Score
@Ibrahim Nash 6560
@mb1973 5777
@Quandray 5275
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4398
@sanjay05 3762
@mantu_singh 3654
@gfgaccount 3601
@marius_valentin_dragoi 3525
Geek collects the balls
Medium Accuracy: 45.64% Submissions: 2067 Points: 4

There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls. The balls in first road are given in an array a and balls in the second road in an array b. 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).
Geek can change the road only at a point of intersection ie- a point where buckets have the same number of balls on two roads. Help Geek collect the maximum number of balls.

Example 1:

Input:
N = 5, M = 5
a[] = {1, 4, 5, 6, 8}
b[] = {2, 3, 4, 6, 9}
Output: 29
Explanation: The optimal way to get the
maximum number of balls is to start from
road 2. Get 2+3. Then switch at intersection
point 4. Get 4+5+6. Then switch at intersection
point 6. Get 9. Total = 2+3+4+5+6+9 = 29.

Example 2:

Input:
N = 3, M = 3
a[] = {1, 2, 3}
b[] = {4, 5, 6}
Output: 15


You do not need to read input or print anything. Your task is to complete the function maxBalls() which takes N, M, a[] and b[] as input parameters and returns the maximum number of balls that can be collected.

Expected Time Complexity: O(N+M)
Expected Auxililary Space: O(1)

Constraints:
1 ≤ N, M ≤ 103
1 ≤ a[i], b[i] ≤ 106