Given three stacks S1, S2 & S3 of size N1, N2 & N3 respectively, having only Positive Integers. The task is to find the possible equal maximum sum of the stacks with the removal of top elements allowed. Stacks are represented as an array, and the first index of the array represents the top element of the stack.
Example 1:
Input:
N1 = 3, N2 = 4, N3 = 2
S1 = {4,2,3}
S2 = {1,1,2,3}
S3= {1,4}
Output:
5
Explanation:
We can pop 1 element from the 1st stack, and 2
elements from the 2nd stack. Now remaining elements
yield the equal sum of the three stacks, that is 5.
Example 2:
Input:
N1 =2, N2 = 1, N3 = 3
S1 = {4,7}
S2 = {10}
S3 = {1,2,3}
Output:
0
Explanation:
We will never get an equal sum after popping
some elements, so the answer will be 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxEqualSum() which takes the arrays S1[], S2[], and S3[] and their sizes N1, N2, and N3 as inputs and returns the maximum equal sum we can obtain.
Expected Time Complexity: O(N1+N2+N3)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N1, N2, N3 <= 10^{5}
1 <= S1[i], S2[i], S3[i] <= 10^{3}
The sum, N1+N2+N3 doesn't exceed 10^{6}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Greedy algorithms

Starter code: from typing import List





class Solution:

    def maxEqualSum(self, N1:int,N2:int,N3:int, S1 : List[int], S2 : List[int], S3 : List[int]) -> int:

        # code here

        



Solutions:
from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = sum(S1)
		s2 = sum(S2)
		s3 = sum(S3)
		ans = min(s1, s2, s3)
		(t1, t2, t3) = (0, 0, 0)
		while ans != 0 and (s1 != s2 or s1 != s3):
			if s1 > ans:
				s1 -= S1[t1]
				t1 += 1
			if s1 < ans:
				ans = s1
			if ans == 0:
				return 0
			if s1 == s2 and s1 == s3 and (s1 == ans):
				return ans
			if s2 > ans:
				s2 -= S2[t2]
				t2 += 1
			if s2 < ans:
				ans = s2
			if ans == 0:
				return 0
			if s1 == s2 and s1 == s3 and (s1 == ans):
				return ans
			if s3 > ans:
				s3 -= S3[t3]
				t3 += 1
			if s3 < ans:
				ans = s3
			if s1 == s2 and s1 == s3 and (s1 == ans):
				return ans
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum_one = sum(S1)
		sum_two = sum(S2)
		sum_three = sum(S3)
		i = 0
		j = 0
		k = 0
		while sum_one > 0 and sum_two > 0 and (sum_three > 0):
			if sum_one == sum_two == sum_three:
				return sum_one
			elif sum_one >= sum_two and sum_one >= sum_three:
				sum_one -= S1[i]
				i += 1
			elif sum_two >= sum_one and sum_two >= sum_three:
				sum_two -= S2[j]
				j += 1
			else:
				sum_three -= S3[k]
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		ans = 0
		a = sum(S1)
		b = sum(S2)
		c = sum(S3)
		x = 0
		y = 0
		z = 0
		while True:
			if x == N1 or y == N2 or z == N3:
				ans = 0
				break
			elif a == b == c:
				ans = a
				break
			elif a >= b and a >= c:
				a = a - S1[x]
				x = x + 1
			elif a >= b and a >= c:
				a = a - S1[x]
				x = x + 1
			elif b >= a and b >= c:
				b = b - S2[y]
				y = y + 1
			elif c >= a and c >= b:
				c = c - S3[z]
				z = z + 1
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		a = sum(S1)
		b = sum(S2)
		c = sum(S3)
		(i, j, k) = (0, 0, 0)
		Flag = 0
		while True:
			if i == N1 and j == N2 and (k == N3):
				Flag = a
				break
			elif a == b == c:
				return a
				break
			elif a >= b and a >= c:
				a -= S1[i]
				i += 1
			elif b >= c and b >= a:
				b -= S2[j]
				j += 1
			elif c >= a and c >= b:
				c -= S3[k]
				k += 1
		return Flag


from typing import List

class Solution:

	def maxEqualSum(self, n1: int, n2: int, n3: int, s1: List[int], s2: List[int], s3: List[int]) -> int:
		(a, b, c) = (sum(s1), sum(s2), sum(s3))
		(p1, p2, p3) = (0, 0, 0)
		while a > 0 and b > 0 and (c > 0):
			if a == b and a == c:
				return a
			if a >= b and a >= c:
				a -= s1[p1]
				p1 += 1
			elif b >= a and b >= c:
				b -= s2[p2]
				p2 += 1
			elif c >= b and c >= a:
				c -= s3[p3]
				p3 += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		(s1, s2, s3) = (sum(S1), sum(S2), sum(S3))
		(i, j, k) = (0, 0, 0)
		while i < N1 and j < N2 and (k < N3):
			if s1 == s2 == s3:
				return s1
			mi = min(s1, s2, s3)
			if s1 > mi:
				s1 -= S1[i]
				i += 1
			elif s2 > mi:
				s2 -= S2[j]
				j += 1
			elif s3 > mi:
				s3 -= S3[k]
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		(sum1, sum2, sum3) = (sum(S1), sum(S2), sum(S3))
		(i, j, k) = (0, 0, 0)
		while i < N1 and j < N2 and (k < N3):
			if sum1 == sum2 == sum3:
				return sum1
			minsum = min(sum1, sum2, sum3)
			if sum1 != minsum:
				sum1 -= S1[i]
				i += 1
			if sum2 != minsum:
				sum2 -= S2[j]
				j += 1
			if sum3 != minsum:
				sum3 -= S3[k]
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		i = N1 - 1
		j = N2 - 1
		k = N3 - 1
		s1 = 0
		s2 = 0
		s3 = 0
		maxsum = 0
		while True:
			if i >= 0 and s1 <= s2 and (s1 <= s3):
				s1 += S1[i]
				i -= 1
			elif j >= 0 and s2 <= s1 and (s2 <= s3):
				s2 += S2[j]
				j -= 1
			elif k >= 0 and s3 <= s1 and (s3 <= s2):
				s3 += S3[k]
				k -= 1
			else:
				break
			if s1 == s2 and s2 == s3 and (s3 == s1):
				maxsum = max(maxsum, s1)
		return maxsum


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		i1 = 0
		i2 = 0
		i3 = 0
		while not (sum1 == sum2 and sum2 == sum3):
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[i1]
				i1 += 1
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= S2[i2]
				i2 += 1
			elif sum3 >= sum2 and sum3 >= sum1:
				sum3 -= S3[i3]
				i3 += 1
		return sum1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, s1: List[int], s2: List[int], s3: List[int]) -> int:
		(a, b, c) = (sum(s1), sum(s2), sum(s3))
		i = j = k = 0
		while a != b or b != c or c != a:
			v = [i != min(a, b, c) for i in (a, b, c)]
			(a, b, c) = (a - s1[i] * v[0], b - s2[j] * v[1], c - s3[k] * v[2])
			(i, j, k) = (i + v[0], j + v[1], k + v[2])
			if i == N1 or j == N2 or k == N3:
				return 0
		return a


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		top1 = top2 = top3 = 0
		while True:
			if top1 == N1 or top2 == N2 or top3 == N3:
				return 0
			if sum1 == sum2 and sum2 == sum3:
				break
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[top1]
				top1 += 1
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= S2[top2]
				top2 += 1
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= S3[top3]
				top3 += 1
		return sum1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		from collections import deque
		S1 = deque(S1)
		S2 = deque(S2)
		S3 = deque(S3)
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		while S1 and S2 and S3:
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[0]
				S1.popleft()
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= S2[0]
				S2.popleft()
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= S3[0]
				S3.popleft()
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = S1
		s2 = S2
		s3 = S3
		sum1 = sum(s1)
		sum2 = sum(s2)
		sum3 = sum(s3)
		while True:
			if sum1 < 0 or sum2 < 0 or sum3 < 0:
				return 0
			elif sum1 == sum2 and sum2 == sum3:
				return sum1
			elif sum1 > sum2 and sum1 > sum3:
				sum1 -= s1.pop(0)
			elif sum2 > sum3 and sum2 > sum1:
				sum2 -= s2.pop(0)
			elif sum3 > sum2 and sum3 > sum1:
				sum3 -= s3.pop(0)
			elif sum1 == sum2:
				sum1 -= s1.pop(0)
			elif sum2 == sum3:
				sum2 -= s2.pop(0)
			elif sum1 == sum3:
				sum1 -= s1.pop(0)


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		i = 0
		j = 0
		t = 0
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		while 1:
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if i == N1 or j == N2 or t == N3:
				return 0
			if sum3 >= sum2 and sum3 >= sum1:
				sum3 -= S3[i]
				i += 1
			elif sum2 >= sum3 and sum2 >= sum1:
				sum2 -= S2[j]
				j += 1
			elif sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[t]
				t += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		from collections import deque
		s1 = 0
		s2 = 0
		s3 = 0
		for i in S1:
			s1 = s1 + i
		for i in S2:
			s2 = s2 + i
		for i in S3:
			s3 = s3 + i
		min_s = min(s1, s2, s3)
		S1 = deque(S1)
		S2 = deque(S2)
		S3 = deque(S3)
		while len(S1) > 0 and len(S2) > 0 and (len(S3) > 0):
			if s1 == s2 == s3:
				return s1
			if s1 > min_s:
				x = S1.popleft()
				s1 = s1 - x
			if s2 > min_s:
				x = S2.popleft()
				s2 = s2 - x
			if s3 > min_s:
				x = S3.popleft()
				s3 = s3 - x
			min_s = min(s1, s2, s3)
		return 0


from typing import List
import heapq

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = sum(S1)
		s2 = sum(S2)
		s3 = sum(S3)
		top = None
		while S1 and S2 and S3:
			if s1 == s2 and s2 == s3:
				return s1
			elif s1 >= s2 and s1 >= s3:
				s1 -= S1.pop(0)
			elif s2 >= s1 and s2 >= s3:
				s2 -= S2.pop(0)
			else:
				s3 -= S3.pop(0)
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		(i, j, k) = (0, 0, 0)
		su1 = sum(S1)
		su2 = sum(S2)
		su3 = sum(S3)
		while i < N1 and j < N2 and (k < N3):
			if su1 == su2 and su2 == su3:
				return su1
			elif su1 >= su2 and su1 >= su3:
				su1 -= S1[i]
				i += 1
			elif su2 >= su1 and su2 >= su3:
				su2 -= S2[j]
				j += 1
			elif su3 >= su2 and su3 >= su1:
				su3 -= S3[k]
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		Sum1 = Sum2 = Sum3 = 0
		for i in range(N1):
			Sum1 += S1[i]
		for i in range(N2):
			Sum2 += S2[i]
		for i in range(N3):
			Sum3 += S3[i]
		while Sum1 != Sum2 or Sum2 != Sum3 or Sum3 != Sum1:
			if Sum1 > Sum2:
				Sum1 -= S1.pop(0)
			elif Sum2 > Sum3:
				Sum2 -= S2.pop(0)
			elif Sum3 > Sum1:
				Sum3 -= S3.pop(0)
		return Sum1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		p1 = 0
		p2 = 0
		p3 = 0
		while True:
			if p1 == N1 or p2 == N2 or p3 == N3:
				return 0
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			else:
				if min(sum1, sum2, sum3) < sum1:
					sum1 = sum1 - S1[p1]
					p1 += 1
				if min(sum1, sum2, sum3) < sum2:
					sum2 = sum2 - S2[p2]
					p2 += 1
				if min(sum1, sum2, sum3) < sum3:
					sum3 = sum3 - S3[p3]
					p3 += 1


from typing import List

class Solution:

	def Summator(self, N, S):
		sum = 0
		for i in range(N - 1, -1, -1):
			sum += S[i]
			S[i] = sum

	def Conditional(self, mini, S):
		if S[-1] != mini:
			S.pop()

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		self.Summator(N1, S1)
		self.Summator(N2, S2)
		self.Summator(N3, S3)
		S1.reverse()
		S2.reverse()
		S3.reverse()
		while len(S1) > 0 and len(S2) > 0 and (len(S3) > 0):
			if S1[-1] == S2[-1] and S2[-1] == S3[-1]:
				return S1[-1]
			else:
				mini = min(S1[-1], S2[-1], S3[-1])
				self.Conditional(mini, S1)
				self.Conditional(mini, S2)
				self.Conditional(mini, S3)
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, n1: int, n2: int, n3: int, stack1: List[int], stack2: List[int], stack3: List[int]) -> int:
		(sum1, sum2, sum3) = (0, 0, 0)
		for i in range(n1):
			sum1 += stack1[i]
		for i in range(n2):
			sum2 += stack2[i]
		for i in range(n3):
			sum3 += stack3[i]
		(top1, top2, top3) = (0, 0, 0)
		ans = 0
		while 1:
			if top1 == n1 or top2 == n2 or top3 == n3:
				return 0
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= stack1[top1]
				top1 = top1 + 1
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= stack2[top2]
				top2 = top2 + 1
			elif sum3 >= sum2 and sum3 >= sum1:
				sum3 -= stack3[top3]
				top3 = top3 + 1


from typing import List

class Solution:

	def getMax(self, s1, s2, s3):
		if s1 > s2 and s1 > s3:
			return 1
		elif s3 > s1 and s3 > s2:
			return 3
		else:
			return 2

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		(top1, top2, top3) = (0, 0, 0)
		ans = 0
		while 1:
			if top1 == N1 or top2 == N2 or top3 == N3:
				return 0
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[top1]
				top1 += 1
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= S2[top2]
				top2 += 1
			elif sum3 >= sum2 and sum3 >= sum1:
				sum3 -= S3[top3]
				top3 += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		S1 = S1[::-1]
		S2 = S2[::-1]
		S3 = S3[::-1]
		s1 = sum(S1)
		s2 = sum(S2)
		s3 = sum(S3)
		while s1 != s2 or s2 != s3:
			if s1 >= s2 and s1 >= s3:
				s1 -= S1.pop()
			elif s2 >= s1 and s2 >= s3:
				s2 -= S2.pop()
			elif s3 >= s1 and s3 >= s2:
				s3 -= S3.pop()
			if not (S1 and S2 and S3):
				break
		if s1 == s2 and s2 == s3:
			return s1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, l: int, m: int, n: int, s1: List[int], s2: List[int], s3: List[int]) -> int:
		maxs = 0
		i = l - 1
		j = m - 1
		k = n - 1
		c1 = 0
		c2 = 0
		c3 = 0
		while i > -1 or j > -1 or k > -1:
			temp = max(c1, c2, c3)
			while i > -1 and c1 < temp:
				c1 += s1[i]
				i -= 1
			while j > -1 and c2 < temp:
				c2 += s2[j]
				j -= 1
			while k > -1 and c3 < temp:
				c3 += s3[k]
				k -= 1
			if c1 == c2 == c3:
				maxs = c1
			if c1 == temp:
				c1 += s1[i]
				i -= 1
			if c2 == temp:
				c2 += s2[j]
				j -= 1
			if c3 == temp:
				c3 += s3[k]
				k -= 1
		return maxs


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, s1: List[int], s2: List[int], s3: List[int]) -> int:
		sum1 = sum(s1)
		sum2 = sum(s2)
		sum3 = sum(s3)
		mx = []
		p1 = 0
		p2 = 0
		p3 = 0
		ans = -1
		while True:
			if p1 == len(s1) or p2 == len(s2) or p3 == len(s3):
				ans = 0
				break
			if sum1 == sum2 == sum3:
				return sum1
				break
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= s1[p1]
				p1 += 1
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= s2[p2]
				p2 += 1
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= s3[p3]
				p3 += 1
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		minSum = min(sum1, sum2, sum3)
		S1 = S1[::-1]
		S2 = S2[::-1]
		S3 = S3[::-1]
		while sum1 > 0 and sum2 > 0 and (sum3 > 0):
			if sum1 > minSum:
				sum1 -= S1.pop()
			if sum2 > minSum:
				sum2 -= S2.pop()
			if sum3 > minSum:
				sum3 -= S3.pop()
			minSum = min(sum1, sum2, sum3)
			if sum1 == sum2 == sum3:
				return sum1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, n1: int, n2: int, n3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		i = j = k = 0
		while i < n1 and j < n2 and (k < n3):
			if sum1 == sum2 == sum3:
				return sum1
			if sum1 > sum2 and sum1 > sum3:
				sum1 -= S1[i]
				i += 1
			elif sum2 > sum3:
				sum2 -= S2[j]
				j += 1
			else:
				sum3 -= S3[k]
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = 0
		sum2 = 0
		sum3 = 0
		top1 = 0
		top2 = 0
		top3 = 0
		for i in range(N1):
			sum1 += S1[i]
		for i in range(N2):
			sum2 += S2[i]
		for i in range(N3):
			sum3 += S3[i]
		while True:
			if top1 == N1 or top2 == N2 or top3 == N3:
				return 0
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[top1]
				top1 += 1
			elif sum2 >= sum3 and sum2 >= sum3:
				sum2 -= S2[top2]
				top2 += 1
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= S3[top3]
				top3 += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = sum(S1)
		s2 = sum(S2)
		s3 = sum(S3)
		(i, j, z) = (0, 0, 0)
		while True:
			if i == N1 or j == N2 or z == N3:
				return 0
			if s1 == s2 and s2 == s3:
				return s1
			if s1 >= s2 and s1 >= s3:
				s1 = s1 - S1[i]
				i += 1
			elif s2 >= s3 and s2 >= s1:
				s2 = s2 - S2[j]
				j += 1
			elif s3 >= s2 and s3 >= s1:
				s3 = s3 - S3[z]
				z += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = sum(S1)
		s2 = sum(S2)
		s3 = sum(S3)
		n = min(N1, N2, N3)
		S1 = S1[::-1]
		S2 = S2[::-1]
		S3 = S3[::-1]
		while S1 and S2 and S3:
			if s1 == s2 and s1 == s3:
				return s1
			if s1 >= s2 and s1 >= s3:
				s1 -= S1.pop()
			elif s2 >= s1 and s2 >= s3:
				s2 -= S2.pop()
			else:
				s3 -= S3.pop()
		return 0


from typing import List
from collections import deque

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = deque(S1)
		s2 = deque(S2)
		s3 = deque(S3)
		sum1 = sum(s1)
		sum2 = sum(s2)
		sum3 = sum(s3)
		ans = 0
		while ans == 0 and s1 and s2 and s3:
			if sum1 == sum2 == sum3:
				ans = sum1
				return ans
			if sum1 == max(sum1, sum2, sum3):
				sum1 -= s1.popleft()
			elif sum2 == max(sum1, sum2, sum3):
				sum2 -= s2.popleft()
			else:
				sum3 -= s3.popleft()
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		S1.reverse()
		S2.reverse()
		S3.reverse()
		summa1 = sum(S1)
		summa2 = sum(S2)
		summa3 = sum(S3)
		while True:
			if summa1 == summa2 and summa1 == summa3:
				return summa1
			if summa1 >= summa2 and summa1 >= summa3:
				if not S1:
					return 0
				summa1 -= S1.pop()
			elif summa2 >= summa1 and summa2 >= summa3:
				if not S2:
					return 0
				summa2 -= S2.pop()
			elif summa3 >= summa2 and summa3 >= summa1:
				if not S3:
					return 0
				summa3 -= S3.pop()


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		temp = 0
		for i in range(N1 - 1, -1, -1):
			S1[i] = temp + S1[i]
			temp = S1[i]
		temp = 0
		for i in range(N2 - 1, -1, -1):
			S2[i] = temp + S2[i]
			temp = S2[i]
		temp = 0
		for i in range(N3 - 1, -1, -1):
			S3[i] = temp + S3[i]
			temp = S3[i]
		(i, j, k) = (0, 0, 0)
		while i < N1 and j < N2 and (k < N3):
			if S1[i] == S2[j] == S3[k]:
				return S1[i]
			if S1[i] >= max(S2[j], S3[k]):
				i += 1
			elif S2[j] >= max(S1[i], S3[k]):
				j += 1
			else:
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		(s1, s2, s3) = (S1, S2, S3)
		(t1, t2, t3) = (0, 0, 0)
		(su1, su2, su3) = (sum(s1), sum(s2), sum(s3))
		ans = 0
		while True:
			if t1 == N1 or t2 == N2 or t3 == N3:
				ans = 0
				break
			if su1 == su2 and su2 == su3:
				ans = su1
				break
			if su1 >= su2 and su1 >= su3:
				su1 -= s1[t1]
				t1 += 1
			elif su2 >= su3 and su2 >= su1:
				su2 -= s2[t2]
				t2 += 1
			elif su3 >= su2 and su3 >= su1:
				su3 -= s3[t3]
				t3 += 1
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sm1 = sum(S1)
		sm2 = sum(S2)
		sm3 = sum(S3)
		(i, j, k) = (0, 0, 0)
		while i < N1 and j < N2 and (k < N3):
			if sm1 == sm2 == sm3:
				return sm1
			if sm1 >= sm2 and sm1 >= sm3:
				sm1 -= S1[i]
				i += 1
			elif sm2 >= sm1 and sm2 >= sm3:
				sm2 -= S2[j]
				j += 1
			else:
				sm3 -= S3[k]
				k += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		a = sum(S1)
		b = sum(S2)
		c = sum(S3)
		a_index = 0
		b_index = 0
		c_index = 0
		ans = None
		while a > 0 and b > 0 and (c > 0):
			m = min(a, b, c)
			while a > m:
				a -= S1[a_index]
				a_index += 1
			while b > m:
				b -= S2[b_index]
				b_index += 1
			while c > m:
				c -= S3[c_index]
				c_index += 1
			if a == 0 or b == 0 or c == 0:
				return 0
			if a == b and b == c:
				ans = a
				break
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		(stack1, stack2, stack3) = (S1, S2, S3)
		(n1, n2, n3) = (N1, N2, N3)
		(sum1, sum2, sum3) = (sum(stack1), sum(stack2), sum(stack3))
		top1 = top2 = top3 = 0
		while 1:
			if n1 * n2 * n3 == 0:
				return 0
			if sum1 == sum2 == sum3:
				return sum1
			if sum1 == max(sum1, sum2, sum3):
				sum1 -= stack1[top1]
				top1 += 1
			elif sum2 == max(sum1, sum2, sum3):
				sum2 -= stack2[top2]
				top2 += 1
			elif sum3 == max(sum1, sum2, sum3):
				sum3 -= stack3[top3]
				top3 += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		a = b = c = 0
		while True:
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if a == N1 or b == N2 or c == N3:
				return 0
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[a]
				a += 1
			elif sum2 >= sum3 and sum2 >= sum1:
				sum2 -= S2[b]
				b += 1
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= S3[c]
				c += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		l1 = sum(S1)
		l2 = sum(S2)
		l3 = sum(S3)
		i1 = 0
		i2 = 0
		i3 = 0
		eqs = 0
		while i1 < N1 and i2 < N2 and (i3 < N3):
			if l1 == l2 == l3:
				return l1
			elif l1 == l2 and l1 > l3:
				l1 -= S1[i1]
				i1 += 1
				l2 -= S2[i2]
				i2 += 1
			elif l1 == l3 and l1 > l2:
				l1 -= S1[i1]
				i1 += 1
				l3 -= S3[i3]
				i3 += 1
			elif l2 == l3 and l2 > l1:
				l2 -= S2[i2]
				i2 += 1
				l3 -= S3[i3]
				i3 += 1
			elif l1 > l2 and l1 > l3:
				l1 -= S1[i1]
				i1 += 1
			elif l2 > l3 and l2 > l1:
				l2 -= S2[i2]
				i2 += 1
			elif l3 > l1 and l3 > l2:
				l3 -= S3[i3]
				i3 += 1
		return eqs


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		s1 = s2 = s3 = 0
		for i in range(N1):
			s1 += S1[i]
		for i in range(N2):
			s2 += S2[i]
		for i in range(N3):
			s3 += S3[i]
		top1 = top2 = top3 = 0
		while top1 < N1 and top2 < N2 and (top3 < N3):
			if s1 == s2 and s2 == s3:
				return s1
			if s1 >= s2 and s1 >= s3:
				s1 -= S1[top1]
				top1 += 1
			elif s2 >= s1 and s2 >= s3:
				s2 -= S2[top2]
				top2 += 1
			elif s3 >= s1 and s3 >= s2:
				s3 -= S3[top3]
				top3 += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		(sum1, sum2, sum3) = (sum(S1), sum(S2), sum(S3))
		(t1, t2, t3) = (0, 0, 0)
		ans = 0
		while True:
			if t1 >= N1 or t2 >= N2 or t3 >= N3:
				ans = 0
				break
			elif sum1 == sum2 and sum1 == sum3:
				ans = sum1
				break
			elif sum1 >= sum2 and sum1 >= sum3:
				sum1 -= S1[t1]
				t1 += 1
			elif sum2 >= sum1 and sum2 >= sum3:
				sum2 -= S2[t2]
				t2 += 1
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= S3[t3]
				t3 += 1
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sm1 = 0
		sm2 = 0
		sm3 = 0
		top1 = 0
		top2 = 0
		top3 = 0
		for ele in S1:
			sm1 += ele
		for ele in S2:
			sm2 += ele
		for ele in S3:
			sm3 += ele
		if sm1 == sm2 and sm2 == sm3:
			return sm1
		while True:
			if sm1 >= sm2 and sm1 >= sm3:
				sm1 -= S1[top1]
				top1 += 1
			elif sm2 >= sm1 and sm2 >= sm3:
				sm2 -= S2[top2]
				top2 += 1
			elif sm3 >= sm1 and sm3 >= sm2:
				sm3 -= S3[top3]
				top3 += 1
			if top1 == N1 or top2 == N2 or top3 == N3:
				break
			if sm1 == sm2 and sm2 == sm3:
				return sm1
		return 0


from typing import List
from collections import deque

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		Sum1 = sum(S1)
		Sum2 = sum(S2)
		Sum3 = sum(S3)
		S1 = deque(S1)
		S2 = deque(S2)
		S3 = deque(S3)
		while S1 and S2 and S3:
			if Sum1 == Sum2 == Sum3:
				return Sum1
			if Sum1 == max(Sum1, Sum2, Sum3):
				val = S1.popleft()
				Sum1 -= val
			elif Sum2 == max(Sum1, Sum2, Sum3):
				val = S2.popleft()
				Sum2 -= val
			elif Sum3 == max(Sum1, Sum2, Sum3):
				val = S3.popleft()
				Sum3 -= val
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		(i, j, k) = (0, 0, 0)
		(m, n, o) = (len(S1), len(S2), len(S3))
		while i < m and j < n and (k < o):
			if sum1 == sum2 and sum1 == sum3 and (sum1 != 0):
				return sum1
			elif sum1 == max(sum1, sum2, sum3):
				sum1 -= S1[i]
				i += 1
			elif sum2 == max(sum1, sum2, sum3):
				sum2 -= S2[j]
				j += 1
			else:
				sum3 -= S3[k]
				k += 1
		return 0


from typing import List
from bisect import bisect_left as bl

class Solution:

	def getSuffix(self, n, arr):
		for i in range(n - 2, -1, -1):
			arr[i] += arr[i + 1]

	def maxEqualSum(self, n1: int, n2: int, n3: int, s1: List[int], s2: List[int], s3: List[int]) -> int:
		self.getSuffix(n1, s1)
		self.getSuffix(n2, s2)
		self.getSuffix(n3, s3)
		s2.reverse()
		s3.reverse()
		for v in s1:
			x = bl(s2, v)
			y = bl(s3, v)
			if x == n2 or y == n3:
				continue
			if s2[x] != v or s3[y] != v:
				continue
			return v
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		summation1 = sum(S1)
		summation2 = sum(S2)
		summation3 = sum(S3)
		i = 0
		j = 0
		k = 0
		while i < N1 and j < N2 and (k < N3):
			if summation1 == summation2 and summation2 == summation3:
				return summation1
			elif summation1 >= summation2 and summation1 >= summation3:
				summation1 -= S1[i]
				i += 1
			elif summation2 >= summation1 and summation2 >= summation3:
				summation2 -= S2[j]
				j += 1
			else:
				summation3 -= S3[k]
				k += 1
		if summation1 == summation2 and summation2 == summation3:
			return summation1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, n1: int, n2: int, n3: int, s1: List[int], s2: List[int], s3: List[int]) -> int:
		sum1 = sum(s1)
		sum2 = sum(s2)
		sum3 = sum(s3)
		(t1, t2, t3) = (0, 0, 0)
		while 1:
			if t1 == n1 or t2 == n2 or t3 == n3:
				return 0
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			if sum1 >= sum2 and sum1 >= sum3:
				sum1 -= s1[t1]
				t1 += 1
			elif sum2 >= sum3 and sum2 >= sum1:
				sum2 -= s2[t2]
				t2 += 1
			elif sum3 >= sum1 and sum3 >= sum2:
				sum3 -= s3[t3]
				t3 += 1


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		a = sum(S1)
		b = sum(S2)
		c = sum(S3)
		p1 = 0
		p2 = 0
		p3 = 0
		while a != 0 and b != 0 and (c != 0):
			if a == b == c:
				return a
			elif a >= b and a >= c:
				a = a - S1[p1]
				p1 += 1
			elif b >= a and b >= c:
				b = b - S2[p2]
				p2 += 1
			elif c >= a and c >= b:
				c = c - S3[p3]
				p3 += 1
		return 0


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		S1.reverse()
		S2.reverse()
		S3.reverse()
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		ans = 0
		while S1 and S2 and S3:
			if sum1 == sum2 and sum2 == sum3:
				return sum1
			min1 = min(sum1, sum2, sum3)
			if sum1 != min1:
				sum1 -= S1.pop()
			if sum2 != min1:
				sum2 -= S2.pop()
			if sum3 != min1:
				sum3 -= S3.pop()
		return ans


from typing import List

class Solution:

	def maxEqualSum(self, N1: int, N2: int, N3: int, S1: List[int], S2: List[int], S3: List[int]) -> int:
		sum1 = sum(S1)
		sum2 = sum(S2)
		sum3 = sum(S3)
		(i, j, k) = (0, 0, 0)
		while True:
			if i == N1 and j == N2 and (k == N3):
				return 0
			else:
				if sum1 == sum2 == sum3:
					return sum1
				if sum1 >= max(sum2, sum3):
					temp = S1[i]
					i += 1
					sum1 -= temp
				elif sum2 >= max(sum1, sum3):
					temp = S2[j]
					j += 1
					sum2 -= temp
				else:
					temp = S3[k]
					k += 1
					sum3 -= temp