You have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. 

Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of the three stacks until they are all the same height, then return the height.  

Example  

$h1=[1,2,1,1]$ 

$h2=[1,1,2]$ 

$h3=[1,1]$  

There are $4,3$ and $2$ cylinders in the three stacks, with their heights in the three arrays.  Remove the top 2 cylinders from $\mbox{h1}$ (heights = [1, 2]) and from $h2$ (heights = [1, 1]) so that the three stacks all are 2 units tall.  Return $2$ as the answer.  

Note: An empty stack is still a stack.  

Function Description  

Complete the equalStacks function in the editor below.

equalStacks has the following parameters:  

int h1[n1]: the first array of heights  
int h2[n2]: the second array of heights  
int h3[n3]: the third array of heights  

Returns  

int: the height of the stacks when they are equalized  

Input Format

The first line contains three space-separated integers, $n\mbox{1}$, $n2$, and $n3$, the numbers of cylinders in stacks $1$, $2$, and $3$. The subsequent lines describe the respective heights of each cylinder in a stack from top to bottom:      

The second line contains $n1$ space-separated integers, the cylinder heights in stack $1$. The first element is the top cylinder of the stack.  
The third line contains $n2$ space-separated integers, the cylinder heights in stack $2$. The first element is the top cylinder of the stack.     
The fourth line contains $n3$ space-separated integers, the cylinder heights in stack $3$.    The first element is the top cylinder of the stack.  

Constraints

$0<n1,n2,n3\leq10^5$
$0<\:\textit{height of any cylinder}\leq100$

Sample Input
STDIN       Function
-----       --------
5 3 4       h1[] size n1 = 5, h2[] size n2 = 3, h3[] size n3 = 4  
3 2 1 1 1   h1 = [3, 2, 1, 1, 1]
4 3 2       h2 = [4, 3, 2]
1 1 4 1     h3 = [1, 1, 4, 1]

Sample Output
5

Explanation

Initially, the stacks look like this:

To equalize thier heights, remove the first cylinder from stacks $1$ and $2$, and then remove the top two cylinders from stack $3$ (shown below).

The stack heights are reduced as follows:

$8-3=5$
$9-4=5$
$7-1-1=5$

All three stacks now have $height=5$, the value to return.

Source: hackerrank | Difficulty: EASY
Tags: Data structures


Solutions:
def read_stack():
	stack = [int(x) for x in input().split(' ')]
	stack = list(reversed(stack))
	sum_stack = set()
	psum = 0
	for i in range(len(stack)):
		psum += stack[i]
		sum_stack.add(psum)
	return sum_stack
input()
ans = read_stack()
ans &= read_stack()
ans &= read_stack()
if len(ans) > 0:
	print(max(ans))
else:
	print(0)


n = [int(x) for x in input().split()]
arr = []
hei = []
ind = [0, 0, 0]
for i in range(3):
	if n[i] == 0:
		arr.append([])
		hei.append(0)
		continue
	arr.append([int(x) for x in input().split()])
	hei.append(sum(arr[i]))
while hei[0] != hei[1] or hei[1] != hei[2] or hei[0] != hei[2]:
	if hei[0] >= hei[1] and hei[0] >= hei[2]:
		hei[0] -= arr[0][ind[0]]
		ind[0] += 1
	elif hei[1] >= hei[0] and hei[1] >= hei[2]:
		hei[1] -= arr[1][ind[1]]
		ind[1] += 1
	else:
		hei[2] -= arr[2][ind[2]]
		ind[2] += 1
print(hei[0])


_ = input()
stacks = [[int(c) for c in input().split()] for _ in range(3)]
lens = [sum(s) for s in stacks]
while not lens[0] == lens[1] == lens[2] and max([len(stack) for stack in stacks]) > 0:
	max_i = lens.index(max(lens))
	c = stacks[max_i].pop(0)
	lens[max_i] -= c
print(lens[0])


(n1, n2, n3) = map(int, input().split())
c = [0] * 10000001
for i in range(3):
	s = 0
	for h in reversed(list(map(int, input().split()))):
		s += h
		c[s] += 1
h = 10000000
while h > 0 and c[h] < 3:
	h -= 1
print(h)


input()
(A, B, C) = (list(map(int, input().split())), list(map(int, input().split())), list(map(int, input().split())))
A = A[::-1]
B = B[::-1]
C = C[::-1]
a = sum(A)
b = sum(B)
c = sum(C)
while a != b or b != c:
	if b > a:
		(a, b) = (b, a)
		(A, B) = (B, A)
	if c > a:
		(a, c) = (c, a)
		(A, C) = (C, A)
	a -= A.pop()
print(a)


from sys import stderr

def read(f):
	return f(input())

def readlist(f):
	return [f(x) for x in input().split()]
(N1, N2, N3) = readlist(int)
H1 = readlist(int)
H1.reverse()
H2 = readlist(int)
H2.reverse()
H3 = readlist(int)
H3.reverse()
h1 = sum(H1)
h2 = sum(H2)
h3 = sum(H3)
while not h1 == h2 == h3:
	if h1 >= h2 and h1 >= h3:
		h1 -= H1.pop()
	elif h2 >= h1 and h2 >= h3:
		h2 -= H2.pop()
	elif h3 >= h1 and h3 >= h2:
		h3 -= H3.pop()
print(h1)


[N1, N2, N3] = [int(r) for r in input().split(' ')]
H1 = [int(r) for r in input().split(' ')]
H2 = [int(r) for r in input().split(' ')]
H3 = [int(r) for r in input().split(' ')]
H1.reverse()
H2.reverse()
H3.reverse()
import itertools
Total1 = set()
Total2 = set()
Total3 = set()
for i in itertools.accumulate(H1):
	Total1.add(i)
for i in itertools.accumulate(H2):
	Total2.add(i)
for i in itertools.accumulate(H3):
	Total3.add(i)
p = Total1.intersection(Total2)
q = Total2.intersection(Total3)
h = p.intersection(q)
if h:
	print(max(h))
else:
	print(0)


(n1, n2, n3) = map(int, input().split())
s1 = [i for i in list(map(int, input().split()))[::-1]]
sum1 = sum(s1)
s2 = [i for i in list(map(int, input().split()))[::-1]]
sum2 = sum(s2)
s3 = [i for i in list(map(int, input().split()))[::-1]]
sum3 = sum(s3)
while n1 > 0 and n2 > 0 and (n3 > 0):
	if sum1 == sum2 == sum3:
		print(sum1)
		break
	min_sum = min(sum1, sum2, sum3)
	if sum1 > min_sum:
		sum1 -= s1[-1]
		s1.pop()
		if sum1 == 0:
			print(0)
			break
	if sum2 > min_sum:
		sum2 -= s2[-1]
		s2.pop()
		if sum2 == 0:
			print(0)
			break
	if sum3 > min_sum:
		sum3 -= s3[-1]
		s3.pop()
		if sum3 == 0:
			print(0)
			break


input()
stack_a = [int(s) for s in input().split()][::-1]
stack_b = [int(s) for s in input().split()][::-1]
stack_c = [int(s) for s in input().split()][::-1]
a_sum = sum(stack_a)
b_sum = sum(stack_b)
c_sum = sum(stack_c)
least = min(a_sum, b_sum, c_sum)
most = max(a_sum, b_sum, c_sum)
while most > least:
	if most == a_sum:
		a_sum -= stack_a.pop()
	elif most == b_sum:
		b_sum -= stack_b.pop()
	else:
		c_sum -= stack_c.pop()
	least = min(a_sum, b_sum, c_sum)
	most = max(a_sum, b_sum, c_sum)
print(most)


numStacks = len(input().split())
stacks = []
heights = []
for stack in range(numStacks):
	stacks.append(list(map(int, input().split())))
	stacks[-1].reverse()
	heights.append(sum(stacks[-1]))
while len(set(heights)) > 1:
	idx = heights.index(max(heights))
	removed = stacks[idx].pop()
	heights[idx] -= removed
print(heights[0])


(n1, n2, n3) = [int(x) for x in input().split()]
h1 = [int(x) for x in input().split()]
h2 = [int(x) for x in input().split()]
h3 = [int(x) for x in input().split()]
s1 = set()
s2 = set()
s3 = set()
s = 0
for i in range(n1 - 1, -1, -1):
	s += h1[i]
	s1.add(s)
s = 0
for i in range(n2 - 1, -1, -1):
	s += h2[i]
	s2.add(s)
s = 0
for i in range(n3 - 1, -1, -1):
	s += h3[i]
	s3.add(s)
ss = s1 & s2 & s3
if ss:
	print(max(ss))
else:
	print(0)


import collections

class Solution:

	def __init__(self):
		self.size = get_int_list(input())
		self.array1 = collections.deque(get_int_list(input()))
		self.array2 = collections.deque(get_int_list(input()))
		self.array3 = collections.deque(get_int_list(input()))

	def calculate(self):
		height1 = sum(self.array1)
		height2 = sum(self.array2)
		height3 = sum(self.array3)
		while height1 != height2 or height2 != height3:
			if len(self.array1) == 0 or len(self.array2) == 0 or len(self.array3) == 0:
				return 0
			next1 = height1 - self.array1[0]
			next2 = height2 - self.array2[0]
			next3 = height3 - self.array3[0]
			if next1 >= next2 and next1 >= next3:
				self.array1.popleft()
				height1 = next1
			elif next2 >= next1 and next2 >= next3:
				self.array2.popleft()
				height2 = next2
			else:
				self.array3.popleft()
				height3 = next3
		return height1

def main():
	my_object = Solution()
	print(my_object.calculate())

def get_int_list(in_str):
	return [int(i) for i in in_str.strip().split()]
main()


stacks = [int(i) for i in input().split()]
stack1 = [int(i) for i in input().split()][::-1]
stack2 = [int(i) for i in input().split()][::-1]
stack3 = [int(i) for i in input().split()][::-1]
sum1 = sum(stack1)
sum2 = sum(stack2)
sum3 = sum(stack3)
while not (sum1 == sum2 and sum2 == sum3):
	heights = [sum1, sum2, sum3]
	next = heights.index(max(heights))
	if next == 0:
		sum1 -= stack1.pop()
	elif next == 1:
		sum2 -= stack2.pop()
	else:
		sum3 -= stack3.pop()
print(sum1)


nh = [int(x) for x in input().strip().split(' ')]
data = [[int(x) for x in input().strip().split(' ')] for i in range(len(nh))]

def cumsum(l):
	l = l[::-1]
	s = [0, l[0]]
	for a in l[1:]:
		s.append(s[-1] + a)
	return s
cdata = [cumsum(x) for x in data]
plist = []
for n in nh:
	plist.append(n)
while True:
	eq = sum([abs(cdata[i][p] - cdata[0][plist[0]]) for (i, p) in enumerate(plist)])
	if eq == 0:
		break
	mp = -1
	s = -1
	for (i, p) in enumerate(plist):
		if cdata[i][p] > s:
			s = cdata[i][p]
			mp = i
	plist[mp] -= 1
print(cdata[0][plist[0]])


n = input()
first = input()
second = input()
third = input()
f = first.split()
s = second.split()
th = third.split()

def getSum(arr):
	sum = 0
	for i in range(len(arr)):
		sum += int(arr[i])
	return sum
fsum = getSum(f)
fi = 0
ssum = getSum(s)
si = 0
thsum = getSum(th)
thi = 0
while not (fsum == ssum and ssum == thsum):
	if fsum == 0 or ssum == 0 or thsum == 0:
		fsum = 0
		break
	if fsum >= ssum and fsum >= thsum:
		fsum -= int(f[fi])
		fi += 1
	elif ssum >= fsum and ssum >= thsum:
		ssum -= int(s[si])
		si += 1
	else:
		thsum -= int(th[thi])
		thi += 1
print(fsum)


from itertools import count
import sys

def max_height(stacks):
	arr_len = len(stacks)
	sums = [sum(stack) for stack in stacks]
	pos = [0 for i in range(arr_len)]
	while True:
		max_pos = 0
		has_max = False
		for i in range(1, arr_len):
			if sums[i] > sums[max_pos]:
				max_pos = i
				has_max = True
			elif sums[i] < sums[max_pos]:
				has_max = True
		if not has_max:
			return sums[0]
		sums[max_pos] -= stacks[max_pos][pos[max_pos]]
		pos[max_pos] += 1
lengths = [int(tok) for tok in input().split()]
A = [[int(tok) for tok in input().split()] for i in range(len(lengths))]
print(max_height(A))


def putInSet(setN, listN):
	count = 0
	for i in range(len(listN) - 1, -1, -1):
		count += listN[i]
		setN.add(count)
(n1, n2, n3) = map(int, input().split())
setN1 = set()
setN2 = set()
setN3 = set()
n1List = list(map(int, input().split()))
n2List = list(map(int, input().split()))
n3List = list(map(int, input().split()))
putInSet(setN1, n1List)
putInSet(setN2, n2List)
putInSet(setN3, n3List)
setN1N2 = setN1.intersection(setN2)
setN1N2N3 = setN1N2.intersection(setN3)
maxHeight = 0
try:
	maxHeight = max(setN1N2N3)
except ValueError:
	maxHeight = 0
print(maxHeight)


import sys
sz = [int(i) for i in input().strip().split(' ')]
n1 = [int(A_temp) for A_temp in input().strip().split(' ')]
n2 = [int(A_temp) for A_temp in input().strip().split(' ')]
n3 = [int(A_temp) for A_temp in input().strip().split(' ')]
n = [n1, n2, n3]
h = [sum(i) for i in n]
while h[0] != h[1] or h[1] != h[2]:
	i = h.index(max(h))
	h[i] -= n[i].pop(0)
print(h[0])


from bisect import bisect_left
(a, b, c) = map(int, input().split())
A = list(reversed([int(x) for x in input().split()]))
B = list(reversed([int(x) for x in input().split()]))
C = list(reversed([int(x) for x in input().split()]))
SA = [0 for _ in range(a)]
SA[0] = A[0]
for i in range(1, a):
	SA[i] = A[i] + SA[i - 1]
SB = [0 for _ in range(b)]
SB[0] = B[0]
for i in range(1, b):
	SB[i] = B[i] + SB[i - 1]
SC = [0 for _ in range(c)]
SC[0] = C[0]
for i in range(1, c):
	SC[i] = C[i] + SC[i - 1]
res = 0
for x in SA:
	p = bisect_left(SB, x)
	q = bisect_left(SC, x)
	if p < b and q < c and (SB[p] == x) and (SC[q] == x):
		res = max(res, x)
print(res)


(x, y, z) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
a.reverse()
a1 = [0] * x
a1[0] = a[0]
for i in range(1, x):
	a1[i] = a[i] + a1[i - 1]
b = list(map(int, input().strip().split()))
b.reverse()
b1 = [0] * y
b1[0] = b[0]
for i in range(1, y):
	b1[i] = b[i] + b1[i - 1]
c = list(map(int, input().strip().split()))
c.reverse()
c1 = [0] * z
c1[0] = c[0]
for i in range(1, z):
	c1[i] = c[i] + c1[i - 1]
s = set(a1).intersection(b1).intersection(c1)
if len(s) != 0:
	print(max(s))
else:
	print(0)


input()
A = [int(x) for x in input().strip().split()]
Apartial = [sum(A)]
for x in A:
	Apartial.append(Apartial[-1] - x)
B = [int(x) for x in input().strip().split()]
Bpartial = [sum(B)]
for x in B:
	Bpartial.append(Bpartial[-1] - x)
C = [int(x) for x in input().strip().split()]
Cpartial = [sum(C)]
for x in C:
	Cpartial.append(Cpartial[-1] - x)
intersect = set(Apartial) & set(Bpartial) & set(Cpartial)
print(max(intersect))


(n1, n2, n3) = map(int, input().split())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
a3 = list(map(int, input().split()))
s1 = sum(a1)
s2 = sum(a2)
s3 = sum(a3)
a1 = a1[::-1]
a2 = a2[::-1]
a3 = a3[::-1]
while 1:
	if s1 == s2 == s3:
		break
	m = max(s1, s2, s3)
	if m == s1:
		x = a1.pop()
		s1 -= x
	elif m == s2:
		x = a2.pop()
		s2 -= x
	else:
		x = a3.pop()
		s3 -= x
print(s1)


(n1, n2, n3) = [int(i) for i in input().strip().split(' ')]
A1 = [int(i) for i in input().strip().split(' ')]
A2 = [int(i) for i in input().strip().split(' ')]
A3 = [int(i) for i in input().strip().split(' ')]
s1 = 0
s2 = 0
s3 = 0
S1 = {0}
S2 = {0}
S3 = {0}
for i in range(n1 - 1, -1, -1):
	s1 = s1 + A1[i]
	S1.add(s1)
for i in range(n2 - 1, -1, -1):
	s2 = s2 + A2[i]
	S2.add(s2)
for i in range(n3 - 1, -1, -1):
	s3 = s3 + A3[i]
	S3.add(s3)
se = S1 & S2 & S3
print(max(se))


from collections import *
(d, ans) = (Counter(), 0)
input()
for i in range(3):
	c = 0
	for x in list(map(int, input().split()))[::-1]:
		c += x
		d[c] += 1
		if d[c] == 3:
			ans = c
print(ans)


sizes = input().strip().split()
s1 = list(map(int, input().strip().split()[::-1]))
s2 = list(map(int, input().strip().split()[::-1]))
s3 = list(map(int, input().strip().split()[::-1]))
h1 = sum(s1)
h2 = sum(s2)
h3 = sum(s3)
while h1 != h2 or h2 != h3 or h1 != h3:
	if h1 > h2:
		h1 -= s1.pop()
	elif h1 > h3:
		h1 -= s1.pop()
	elif h2 > h1:
		h2 -= s2.pop()
	elif h2 > h3:
		h2 -= s2.pop()
	elif h3 > h2:
		h3 -= s3.pop()
	elif h3 > h1:
		h3 -= s3.pop()
print(h1)


(n1, n2, n3) = [int(x) for x in input().split()]
l = []
for i in range(3):
	l.append([int(x) for x in input().split()][::-1])
h = [sum(l[0]), sum(l[1]), sum(l[2])]
m = 0
while not h[0] == h[1] == h[2]:
	m = min(h[0], h[1], h[2])
	for i in range(3):
		if h[i] > m:
			while h[i] > m:
				h[i] -= l[i].pop()
print(h[0])


input()
n0 = [int(a) for a in input().split(' ')]
n1 = [int(a) for a in input().split(' ')]
n2 = [int(a) for a in input().split(' ')]
s0 = sum(n0)
s1 = sum(n1)
s2 = sum(n2)
s = [s0, s1, s2]
while not s[0] == s[1] == s[2]:
	najgolem = s.index(max(s))
	if najgolem == 0:
		s0 -= n0[0]
		del n0[0]
	elif najgolem == 1:
		s1 -= n1[0]
		del n1[0]
	else:
		s2 -= n2[0]
		del n2[0]
	s = [s0, s1, s2]
print(s1)


import sys
(n1, n2, n3) = [int(c) for c in next(sys.stdin).rstrip('\n').split(' ')]
l1 = list(reversed([int(c) for c in next(sys.stdin).rstrip('\n').split(' ')]))
l2 = list(reversed([int(c) for c in next(sys.stdin).rstrip('\n').split(' ')]))
l3 = list(reversed([int(c) for c in next(sys.stdin).rstrip('\n').split(' ')]))
l = [l1, l2, l3]
for l_ in l:
	for index in range(len(l_) - 1):
		l_[index + 1] += l_[index]

def pop_max():
	(ind_max, _) = max([(ind, l[ind][-1]) for ind in range(len(l))], key=lambda a: a[1])
	l[ind_max].pop()
	if not l[ind_max]:
		l.pop(ind_max)

def is_equal():
	return l[0][-1] == l[1][-1] and l[1][-1] == l[2][-1]
while True:
	if is_equal():
		print(l[0][-1])
		break
	pop_max()
	if len(l) < 3:
		print(0)
		break


(n1, n2, n3) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l3 = list(map(int, input().split()))
h1 = sum(l1)
h2 = sum(l2)
h3 = sum(l3)
i = 0
j = 0
k = 0
while not (h1 == h2 and h2 == h3):
	h = min(h1, h2, h3)
	if h1 > h:
		h1 -= l1[i]
		i += 1
	if h2 > h:
		h2 -= l2[j]
		j += 1
	if h3 > h:
		h3 -= l3[k]
		k += 1
print(h1)


import sys
import operator
stacks = []
for i in range(len([x for x in input().split(' ')])):
	stacks.append([int(x) for x in input().split(' ')])
sizes = [sum(x) for x in stacks]
while not all((x == sizes[0] for x in sizes)):
	(index, stack_size) = max(enumerate(sizes), key=operator.itemgetter(1))
	stack = stacks[index]
	block_size = stack.pop(0)
	stacks[index] = stack
	sizes[index] = stack_size - block_size
print(sizes[0])


(n1, n2, n3) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
l3 = list(map(int, input().split()))
h1 = sum(l1)
h2 = sum(l2)
h3 = sum(l3)
i = 0
j = 0
k = 0
while not (h1 == h2 and h2 == h3):
	h = min(h1, h2, h3)
	if h1 > h:
		h1 -= l1[i]
		i += 1
	if h2 > h:
		h2 -= l2[j]
		j += 1
	if h3 > h:
		h3 -= l3[k]
		k += 1
print(h1)


n = list(map(int, input().split()))
line1 = list(map(int, input().split()))
line2 = list(map(int, input().split()))
line3 = list(map(int, input().split()))
for i in reversed(range(n[0] - 1)):
	line1[i] = line1[i] + line1[i + 1]
for i in reversed(range(n[1] - 1)):
	line2[i] = line2[i] + line2[i + 1]
for i in reversed(range(n[2] - 1)):
	line3[i] = line3[i] + line3[i + 1]
line1.append(0)
line2.append(0)
line3.append(0)
i = 0
j = 0
k = 0
while line1[i] != line2[j] or line1[i] != line3[k] or line3[k] != line2[j]:
	l1 = line1[i]
	l2 = line2[j]
	l3 = line3[k]
	if l1 > l2 or l1 > l3:
		i = i + 1
	if l2 > l1 or l2 > l3:
		j = j + 1
	if l3 > l2 or l3 > l1:
		k = k + 1
print(line1[i])


import sys
args = []
for line in sys.stdin:
	args.append(list(map(int, line.strip().split(' '))))
args = list(map(lambda arg: arg[::-1], args))
stacks = args[1:]
heights = [sum(stacks[0]), sum(stacks[1]), sum(stacks[2])]
min_height = min(heights)
max_height = max(heights)
while min_height != max_height:
	max_idx = heights.index(max_height)
	heights[max_idx] -= stacks[max_idx].pop()
	min_height = min(heights)
	max_height = max(heights)
print(min_height)


input()
s1 = list(map(int, input().strip().split()))
s2 = list(map(int, input().strip().split()))
s3 = list(map(int, input().strip().split()))
h1 = sum(s1)
h2 = sum(s2)
h3 = sum(s3)
while not h1 == h2 == h3:
	m = max(h1, h2, h3)
	if h1 == m:
		t = s1.pop(0)
		h1 -= t
	elif h2 == m:
		t = s2.pop(0)
		h2 -= t
	elif h3 == m:
		t = s3.pop(0)
		h3 -= t
print(h1)


n = [int(_) for _ in input().split()]
a = [list(reversed([int(_) for _ in input().split()])) for i in range(3)]
s = [sum(_) for _ in a]
while s[0] != s[1] or s[0] != s[2]:
	for i in range(3):
		if s[i] == max(s):
			s[i] -= a[i].pop()
			break
print(s[0])


from itertools import accumulate
(n1, n2, n3) = map(int, input().split())
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
s3 = list(map(int, input().split()))
s1 = list(accumulate(s1[::-1]))
s2 = list(accumulate(s2[::-1]))
s3 = list(accumulate(s3[::-1]))
se1 = set(s1)
se2 = set(s2)
se3 = set(s3)
se4 = se1 & se2 & se3
ans = 0 if len(se4) == 0 else max(se4)
print(ans)


def check_equal_heights(heights):
	h_0 = heights[0]
	for h_i in heights[1:]:
		if h_i != h_0:
			return False
	return True
num_blocks = [int(num) for num in input().strip().split()]
num_stacks = len(num_blocks)
stacks = []
for __ in range(num_stacks):
	stack = [int(h) for h in input().strip().split()]
	stacks.append(stack[::-1])
goal = 1
best_height = 0
heights = [0] * num_stacks
positions = [0] * num_stacks
total_blocks = sum(num_blocks)
while sum(positions) < total_blocks:
	for (i, stack) in enumerate(stacks):
		if positions[i] == num_blocks[i]:
			continue
		if heights[i] == goal:
			heights[i] += stack[positions[i]]
			positions[i] += 1
		at_end = False
		while heights[i] < goal:
			if positions[i] == num_blocks[i]:
				at_end = True
				break
			heights[i] += stack[positions[i]]
			positions[i] += 1
		goal = heights[i]
		if at_end:
			continue
		if check_equal_heights(heights):
			best_height = heights[i]
print(best_height)


(n1, n2, n3) = input().split()
s1 = [int(h) for h in input().split(' ')]
s2 = [int(h) for h in input().split(' ')]
s3 = [int(h) for h in input().split(' ')]
index1 = 0
index2 = 0
index3 = 0
h1 = sum(s1)
h2 = sum(s2)
h3 = sum(s3)
while not h1 == h2 == h3 and (h1 > 0 or h2 > 0 or h3 > 0):
	op = False
	maxH = max(h1, h2, h3)
	if not op and maxH == h1:
		h1 -= s1[index1]
		index1 += 1
		op = True
	if not op and maxH == h2:
		h2 -= s2[index2]
		index2 += 1
		op = True
	if not op and maxH == h3:
		h3 -= s3[index3]
		index3 += 1
		op = True
print(h1)


def read_stack(inp):
	return list(reversed([int(c) for c in inp.split()]))

def height_array(stack):
	height = 0
	yield height
	for cyl in stack:
		height += cyl
		yield height
garbage = input()
cyl1 = set(height_array(read_stack(input())))
cyl2 = set(height_array(read_stack(input())))
cyl3 = set(height_array(read_stack(input())))
print(max(cyl1.intersection(cyl2, cyl3)))


input()
s1 = list(map(int, input().split(' ')))
s2 = list(map(int, input().split(' ')))
s3 = list(map(int, input().split(' ')))
si1 = 0
si2 = 0
si3 = 0
sum1 = sum(s1)
sum2 = sum(s2)
sum3 = sum(s3)
while sum1 != sum2 or sum2 != sum3:
	if sum1 >= sum2 and sum1 >= sum3:
		sum1 -= s1[si1]
		si1 += 1
	elif sum2 >= sum1 and sum2 >= sum3:
		sum2 -= s2[si2]
		si2 += 1
	elif sum3 >= sum2 and sum1 <= sum3:
		sum3 -= s3[si3]
		si3 += 1
	if sum1 == 0 or sum2 == 0 or sum3 == 0:
		sum1 = 0
		sum2 = 0
		sum3 = 0
print(sum1)


def binsearch(array, target):
	lower = 0
	upper = len(array)
	while lower < upper:
		x = lower + (upper - lower) // 2
		val = array[x]
		if target == val:
			return x
		elif target >= val:
			if lower == x:
				break
			lower = x
		elif target < val:
			upper = x
(N1, N2, N3) = [int(n) for n in input().split()]
stack1 = [int(n) for n in input().split()][::-1]
stack2 = [int(n) for n in input().split()][::-1]
stack3 = [int(n) for n in input().split()][::-1]
h1List = [None] * N1
h2List = [None] * N2
h3List = [None] * N3
tot = 0
for n in range(N1):
	tot += stack1[n]
	h1List[n] = tot
tot = 0
for n in range(N2):
	tot += stack2[n]
	h2List[n] = tot
tot = 0
for n in range(N3):
	tot += stack3[n]
	h3List[n] = tot
found = False
if N1 > 100:
	for n in range(N1 - 1, -1, -1):
		if binsearch(h2List, h1List[n]) and binsearch(h3List, h1List[n]):
			print(h1List[n])
			found = True
			break
else:
	for n in range(N1 - 1, -1, -1):
		if h1List[n] in h2List and h1List[n] in h3List:
			print(h1List[n])
			found = True
			break
if not found:
	print(0)


(a, b, c) = [int(x) for x in input().split()]
m = [int(x) for x in input().split()]
m.reverse()
for i in range(1, a):
	m[i] = m[i - 1] + m[i]
n = [int(x) for x in input().split()]
n.reverse()
for i in range(1, b):
	n[i] = n[i - 1] + n[i]
o = [int(x) for x in input().split()]
o.reverse()
for i in range(1, c):
	o[i] = o[i - 1] + o[i]
l = []
for i in set.intersection(set(m), set(n), set(o)):
	l.append(i)
if l != []:
	print(max(l))
else:
	print('0')


input()
l = []
l.append([int(y) for y in input().split()])
l.append([int(y) for y in input().split()])
l.append([int(y) for y in input().split()])
sums = [sum(l[0]), sum(l[1]), sum(l[2])]
d = max(sums)
diffs = [d - i for i in sums]
n = [0, 0, 0]
p = 1
while not (diffs[0] == diffs[1] and diffs[1] == diffs[2]):
	try:
		temp = [diffs[x] + l[x][n[x]] for x in range(3)]
	except:
		p = 0
		break
	i = temp.index(min(temp))
	diffs[i] = temp[i]
	n[i] += 1
if p:
	print(sum(l[0][n[0]:]))
else:
	print(0)


(a, b, c) = list(map(int, input().split()))
aa = list(map(int, input().split()))[::-1]
bb = list(map(int, input().split()))[::-1]
cc = list(map(int, input().split()))[::-1]
sa = [0] * a
sb = [0] * b
sc = [0] * c
s = 0
for i in range(a):
	s += aa[i]
	sa[i] = s
s = 0
for i in range(b):
	s += bb[i]
	sb[i] = s
s = 0
for i in range(c):
	s += cc[i]
	sc[i] = s
a -= 1
b -= 1
c -= 1
if a < 0:
	a = 0
if b < 0:
	b = 0
if c < 0:
	c = 0
while True:
	if sa[a] == sb[b] and sa[a] == sc[c]:
		print(sa[a])
		break
	s = max(sa[a], sb[b], sc[c])
	if s == sa[a]:
		a -= 1
	if s == sb[b]:
		b -= 1
	if s == sc[c]:
		c -= 1
	if a < 0:
		a = 0
		sa[0] = 0
	if b < 0:
		b = 0
		sb[0] = 0
	if c < 0:
		c = 0
		sc[0] = 0


def sums(items):
	return list(map(sum, items))

def equal(items):
	return len(set(items)) == 1
_ = input()
stacks = []
for i in range(3):
	stacks.append(list((int(i) for i in input().strip().split(' '))))
heights = sums(stacks)
while not equal(heights):
	highest = heights.index(max(heights))
	heights[highest] -= stacks[highest].pop(0)
print(sums(stacks)[0])


from collections import defaultdict
A = [int(i) for i in input().split(' ')]
S1 = [int(i) for i in input().split(' ')]
S2 = [int(i) for i in input().split(' ')]
S3 = [int(i) for i in input().split(' ')]
S1.reverse()
S2.reverse()
S3.reverse()

def height():
	height1 = defaultdict(lambda : 0)
	counter = 0
	for i in S1:
		height1[counter + i] += 1
		counter = counter + i
	counter = 0
	for i in S2:
		height1[counter + i] += 1
		counter = counter + i
	counter = 0
	for i in S3:
		height1[counter + i] += 1
		counter = counter + i
	max = 0
	for (key, value) in height1.items():
		if value == 3:
			if key > max:
				max = key
	print(max)
height()


(n1, n2, n3) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a_sum = sum(a)
b_sum = sum(b)
c_sum = sum(c)
a_curr = 0
b_curr = 0
c_curr = 0
while True:
	if a_sum == b_sum == c_sum:
		break
	if a_sum >= b_sum and a_sum >= c_sum:
		a_sum -= a[a_curr]
		a_curr += 1
	elif b_sum >= a_sum and b_sum >= c_sum:
		b_sum -= b[b_curr]
		b_curr += 1
	else:
		c_sum -= c[c_curr]
		c_curr += 1
print(a_sum)


(n1, n2, n3) = [int(x) for x in input().strip().split(' ')]
A = [int(x) for x in input().strip().split(' ')]
B = [int(x) for x in input().strip().split(' ')]
C = [int(x) for x in input().strip().split(' ')]
i = n1 - 2
while i >= 0:
	A[i] = A[i] + A[i + 1]
	i -= 1
i = n2 - 2
while i >= 0:
	B[i] = B[i] + B[i + 1]
	i -= 1
i = n3 - 2
while i >= 0:
	C[i] = C[i] + C[i + 1]
	i -= 1
i1 = 0
i2 = 0
i3 = 0
ans = 0
while i1 < n1 and i2 < n2 and (i3 < n3):
	if A[i1] == B[i2] and B[i2] == C[i3]:
		ans = A[i1]
		break
	elif A[i1] >= B[i2] and A[i1] >= C[i3]:
		i1 += 1
	elif B[i2] >= A[i1] and B[i2] >= C[i3]:
		i2 += 1
	else:
		i3 += 1
print(ans)


(m, n, p) = map(int, input().split())
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
z = [int(x) for x in input().split()]
x = x[::-1]
y = y[::-1]
z = z[::-1]
a = b = c = 0
for v in x:
	a += v
for v in y:
	b += v
for v in z:
	c += v
while len(x) > 0:
	while b > a:
		b -= y.pop()
	while c > a:
		c -= z.pop()
	if a == b and a == c:
		print(a)
		quit()
	a -= x.pop()
print(0)


(n1, n2, n3) = map(int, input().strip().split())
MAX = 1 + 10 ** 7
a = [False] * MAX
b = [False] * MAX
c = [False] * MAX
a[0] = True
b[0] = True
c[0] = True
cur = 0
N = list(map(int, input().strip().split()))[::-1]
for n in N:
	cur += n
	a[cur] = True
cur = 0
M = list(map(int, input().strip().split()))[::-1]
for n in M:
	cur += n
	b[cur] = True
cur = 0
L = list(map(int, input().strip().split()))[::-1]
for n in L:
	cur += n
	c[cur] = True
for i in range(MAX - 1, -1, -1):
	if a[i] and b[i] and c[i]:
		print(i)
		break


(x, y, z) = [int(a) for a in input().split()]
h = []
for a0 in range(3):
	h.append([int(a) for a in input().split()[::-1]])
tot = [[], [], []]
for a1 in range(3):
	c = 0
	for a2 in range(len(h[a1])):
		c += h[a1][a2]
		tot[a1].append(c)
p = set(tot[0]) & set(tot[1]) & set(tot[2])
if list(p) == []:
	print(0)
else:
	print(max(p))


(len_a, len_b, len_c) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

def sum_heights(heights):
	result = []
	total = 0
	for (i, h) in enumerate(heights):
		total += h
		result.append(total)
	return result
heights_A = sum_heights(reversed(A))
heights_B = sum_heights(reversed(B))
heights_C = sum_heights(reversed(C))
max_height = 0
while heights_A and heights_B and heights_C:
	if heights_A[-1] == heights_B[-1] == heights_C[-1]:
		max_height = heights_A[-1]
		break
	if heights_A[-1] >= heights_B[-1] and heights_A[-1] >= heights_C[-1]:
		heights_A.pop()
	elif heights_B[-1] >= heights_A[-1] and heights_B[-1] >= heights_C[-1]:
		heights_B.pop()
	elif heights_C[-1] >= heights_B[-1] and heights_C[-1] >= heights_A[-1]:
		heights_C.pop()
print(max_height)


from collections import deque
from operator import itemgetter

def main():
	num_cylinders = [int(i) for i in input().split()]
	stacks = [deque() for _ in range(3)]
	heights = [0] * 3
	for i in range(3):
		stacks[i].extend(map(int, input().split()))
		heights[i] = sum(stacks[i])
	while len(set(heights)) > 1:
		i = max(enumerate(heights), key=itemgetter(1))[0]
		heights[i] -= stacks[i].popleft()
	print(heights[0])
main()


import sys
import bisect
(h1, h2, h3) = input().strip().split()
stack1 = input().strip().split()
cumulativestack1 = []
counter = len(stack1) - 1
total = 0
for i in reversed(stack1):
	stack1[counter] = int(i)
	cumulativestack1.append(total + int(i))
	total += int(i)
	counter -= 1
stack2 = input().strip().split()
cumulativestack2 = []
counter = len(stack2) - 1
total = 0
for i in reversed(stack2):
	stack2[counter] = int(i)
	cumulativestack2.append(total + int(i))
	total += int(i)
	counter -= 1
stack3 = input().strip().split()
cumulativestack3 = []
counter = len(stack3) - 1
total = 0
for i in reversed(stack3):
	stack3[counter] = int(i)
	cumulativestack3.append(total + int(i))
	total += int(i)
	counter -= 1
finalanswer = 0
l2 = len(cumulativestack2)
l3 = len(cumulativestack3)
for i in cumulativestack1:
	ind2 = bisect.bisect_left(cumulativestack2, i)
	ind3 = bisect.bisect_left(cumulativestack3, i)
	if ind2 != l2 and cumulativestack2[ind2] == i and (ind3 != l3) and (cumulativestack3[ind3] == i):
		finalanswer = i
print(finalanswer)


height = [int(i) for i in input().split()]
s1 = reversed([int(i) for i in input().split()])
s2 = reversed([int(i) for i in input().split()])
s3 = reversed([int(i) for i in input().split()])

def get_height_set(blocks):
	heights = set()
	height = 0
	for block in blocks:
		height += block
		heights.add(height)
	return heights
heights1 = get_height_set(s1)
heights2 = get_height_set(s2)
heights3 = get_height_set(s3)
matching_heights = heights1 & heights2 & heights3
if not matching_heights:
	print('0')
else:
	print(max(matching_heights))


import sys
n = [int(n) for n in input().strip().split(' ')]
stack = []
for i in range(3):
	stack.append([int(e) for e in input().strip().split(' ')])
height = [sum(s) for s in stack]
index = [0, 0, 0]
while not (height[0] == height[1] and height[1] == height[2]):
	max_height = max(height)
	i = height.index(max_height)
	height[i] -= stack[i][index[i]]
	index[i] += 1
print(height[0])


H = [int(a) for a in input().strip().split(' ')]
S = [0, 0, 0]
S[0] = [int(a) for a in input().strip().split(' ')][::-1]
S[1] = [int(a) for a in input().strip().split(' ')][::-1]
S[2] = [int(a) for a in input().strip().split(' ')][::-1]
ans = 0
i = [-1, -1, -1]
h = [0, 0, 0]
while True:
	newi = i[:]
	if all(map(lambda x: x == h[0], h)):
		ans = h[0]
		newi[0] = i[0] + 1
		if newi[0] >= H[0]:
			newi[0] = i[0]
		else:
			h[0] += S[0][newi[0]]
		newi[1] = i[1] + 1
		if newi[1] >= H[1]:
			newi[1] = i[1]
		else:
			h[1] += S[1][newi[1]]
		newi[2] = i[2] + 1
		if newi[2] >= H[2]:
			newi[2] = i[2]
		else:
			h[2] += S[2][newi[2]]
	else:
		maxh = max(h)
		if h[0] < maxh:
			newi[0] = i[0] + 1
			if newi[0] >= H[0]:
				newi[0] = i[0]
			else:
				h[0] += S[0][newi[0]]
		if h[1] < maxh:
			newi[1] = i[1] + 1
			if newi[1] >= H[1]:
				newi[1] = i[1]
			else:
				h[1] += S[1][newi[1]]
		if h[2] < maxh:
			newi[2] = i[2] + 1
			if newi[2] >= H[2]:
				newi[2] = i[2]
			else:
				h[2] += S[2][newi[2]]
	if i == newi:
		break
	else:
		i = newi[:]
print(ans)


(n1, n2, n3) = map(int, input().split())
h1 = list(map(int, input().split()))[::-1]
h2 = list(map(int, input().split()))[::-1]
h3 = list(map(int, input().split()))[::-1]
s1 = sum(h1)
s2 = sum(h2)
s3 = sum(h3)
while not s1 == s2 == s3:
	tallest = max(s1 - h1[-1], s2 - h2[-1], s3 - h3[-1])
	if s1 - h1[-1] == tallest:
		s1 -= h1.pop()
	if s2 - h2[-1] == tallest:
		s2 -= h2.pop()
	if s3 - h3[-1] == tallest:
		s3 -= h3.pop()
print(s1)


import sys
stackSizes = [int(A_temp) for A_temp in input().strip().split(' ')]
sums = []
stacks = []
for i in range(0, len(stackSizes)):
	if stackSizes[i] > 0:
		stacks.append([int(A_temp) for A_temp in input().strip().split(' ')])
	else:
		stacks.append([])
	sums.append(sum(stacks[i]))
while sums[0] != sums[1] or sums[0] != sums[2] or sums[1] != sums[2]:
	indexOfMax = sums.index(max(sums))
	val = stacks[indexOfMax].pop(0)
	sums[indexOfMax] = sums[indexOfMax] - val
print(sums[0])


(n1, n2, n3) = [int(i) for i in input().strip().split(' ')]
a = [int(i) for i in input().strip().split(' ')]
b = [int(i) for i in input().strip().split(' ')]
c = [int(i) for i in input().strip().split(' ')]
alar = sum(a)
bler = sum(b)
cler = sum(c)
a1 = 0
b1 = 0
c1 = 0
m = min([alar, bler, cler])
while alar != bler or bler != cler:
	if alar > m:
		alar -= a[a1]
		a1 += 1
		m = min([alar, bler, cler])
	elif bler > m:
		bler -= b[b1]
		b1 += 1
		m = min([alar, bler, cler])
	elif cler > m:
		cler -= c[c1]
		c1 += 1
		m = min([alar, bler, cler])
print(m)


input()
n0 = [int(x) for x in input().strip().split()]
n1 = [int(x) for x in input().strip().split()]
n2 = [int(x) for x in input().strip().split()]
s0 = sum(n0)
s1 = sum(n1)
s2 = sum(n2)
c0 = 0
c1 = 0
c2 = 0
while s0 != s1 or s0 != s2:
	if s0 > s1:
		s0 -= n0[c0]
		c0 += 1
	if s0 > s2:
		s0 -= n0[c0]
		c0 += 1
	if s1 > s0:
		s1 -= n1[c1]
		c1 += 1
	if s1 > s2:
		s1 -= n1[c1]
		c1 += 1
	if s2 > s0:
		s2 -= n2[c2]
		c2 += 1
	if s2 > s1:
		s2 -= n2[c2]
		c2 += 1
print(s0)


def read_input():
	return [int(x) for x in input().strip().split(' ')]

def calculate_absolute_heights(cylinder_heights):
	running_total = 0
	returned = []
	for height in cylinder_heights:
		returned.append(height + running_total)
		running_total += height
	return returned
(n1, n2, n3) = read_input()
cylinA = read_input()
cylinB = read_input()
cylinC = read_input()
cylinA = set(calculate_absolute_heights(reversed(cylinA)))
cylinB = set(calculate_absolute_heights(reversed(cylinB)))
cylinC = set(calculate_absolute_heights(reversed(cylinC)))
common_heights = cylinA.intersection(cylinB).intersection(cylinC)
if common_heights:
	print(max(common_heights))
else:
	print(0)


import sys
A = [int(A_temp) for A_temp in input().strip().split(' ')]
stacks = []
for i in range(0, len(A)):
	stack = list(reversed([int(s_temp) for s_temp in input().strip().split(' ')]))
	stacks.append(stack)

def chk_eql(values):
	return len(set(values)) == 1

def check_empty(values):
	for value in values:
		if len(value) == 0:
			return True
	return False

def max_common_height(stacks):
	height = 0
	total_heights = list(map(sum, stacks))
	while not chk_eql(total_heights) and (not check_empty(stacks)):
		current_tallest = total_heights.index(max(total_heights))
		c_height = stacks[current_tallest].pop()
		total_heights[current_tallest] -= c_height
	if chk_eql(total_heights):
		height = total_heights[0]
	return height
print(max_common_height(stacks))


(a, b, c) = input().strip().split()
(a, b, c) = (int(a), int(b), int(c))
sA = [int(tmp) for tmp in input().strip().split()]
sB = [int(tmp) for tmp in input().strip().split()]
sC = [int(tmp) for tmp in input().strip().split()]
sA.reverse()
sB.reverse()
sC.reverse()
stacks = [sA, sB, sC]
for s in stacks:
	height = 0
	for i in range(len(s)):
		height += s[i]
		s[i] = height
s = 0
s1 = 1
s2 = 2
if stacks[1][len(stacks[1]) - 1] < stacks[0][len(stacks[0]) - 1] and stacks[1][len(stacks[1]) - 1] < stacks[2][len(stacks[2]) - 1]:
	s = 1
	s1 = 0
	s2 = 2
if stacks[2][len(stacks[2]) - 1] < stacks[0][len(stacks[0]) - 1] and stacks[2][len(stacks[2]) - 1] < stacks[1][len(stacks[1]) - 1]:
	s = 2
	s1 = 0
	s2 = 1
set1 = set(stacks[0])
set2 = set(stacks[1])
set3 = set(stacks[2])
set4 = set1.intersection(set2)
set5 = set4.intersection(set3)
if set5:
	print(max(set5))
else:
	print(0)


import sys
sys.setrecursionlimit(1000)
[n1, n2, n3] = [int(x) for x in input().strip().split(' ')]
s1 = [int(x) for x in input().strip().split(' ')]
s2 = [int(x) for x in input().strip().split(' ')]
s3 = [int(x) for x in input().strip().split(' ')]
h1 = sum(s1)
h2 = sum(s2)
h3 = sum(s3)
i1 = i2 = i3 = 0
while not h1 == h2 == h3:
	if h1 >= h3 and h1 >= h2:
		h1 -= s1[i1]
		i1 += 1
	elif h2 >= h1 and h2 >= h3:
		h2 -= s2[i2]
		i2 += 1
	else:
		h3 -= s3[i3]
		i3 += 1
print(h1)


import sys
(n1, n2, n3) = map(int, input().split(' '))
stacks = []
stacks.append(list(map(int, input().split(' '))))
stacks.append(list(map(int, input().split(' '))))
stacks.append(list(map(int, input().split(' '))))
heights = list(map(sum, stacks))
maxH = 0
while len(stacks[0]) or (len(stacks[1]) and len(stacks[2])):
	if heights[0] == heights[1] and heights[1] == heights[2]:
		maxH = heights[0]
		break
	i = 2
	if heights[0] >= heights[1] and heights[0] >= heights[2]:
		i = 0
	elif heights[1] >= heights[0] and heights[1] >= heights[2]:
		i = 1
	aux = stacks[i].pop(0)
	heights[i] -= aux
print(maxH)


(n1, n2, n3) = map(int, input().strip().split())
s1 = [int(i) for i in input().strip().split()]
s2 = [int(i) for i in input().strip().split()]
s3 = [int(i) for i in input().strip().split()]
sum1 = sum(s1)
sum2 = sum(s2)
sum3 = sum(s3)
t1 = [sum1]
t2 = [sum2]
t3 = [sum3]
for i in range(n1):
	sum1 = sum1 - s1.pop(0)
	t1.append(sum1)
for i in range(n2):
	sum2 = sum2 - s2.pop(0)
	t2.append(sum2)
for i in range(n3):
	sum3 = sum3 - s3.pop(0)
	t3.append(sum3)
t = set(t1) & set(t2) & set(t3)
n = max(t)
if n:
	print(n)
else:
	print(0)


input()
tk = lambda : [int(i) for i in input().split()]
heights = {}

def f(L):
	s = 0
	for i in reversed(L):
		s += i
		heights[s] = heights.setdefault(s, 0) + 1
for _ in range(3):
	f(tk())
ans = 0
for k in heights:
	if heights[k] == 3:
		ans = max(ans, k)
print(ans)


import sys

def height(blocks):
	n = len(blocks)
	a = 0
	for i in range(n):
		a += blocks[i]
	return a

def findmax(a, b, c):
	m = a
	if b > m:
		m = b
	if c > m:
		m = c
	if m == a:
		return 1
	elif m == b:
		return 2
	else:
		return 3
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
height1 = input().strip().split()
height2 = input().strip().split()
height3 = input().strip().split()
for i in range(n1):
	height1[i] = int(height1[i])
for i in range(n2):
	height2[i] = int(height2[i])
for i in range(n3):
	height3[i] = int(height3[i])
h1 = height(height1)
h2 = height(height2)
h3 = height(height3)
while not (h1 == h2 and h2 == h3):
	x = findmax(h1, h2, h3)
	if x == 1:
		h1 -= height1[0]
		height1.pop(0)
	elif x == 2:
		h2 -= height2[0]
		height2.pop(0)
	else:
		h3 -= height3[0]
		height3.pop(0)
print(h1)


def stacks_equal(stacks):
	return heights[0] == heights[1] == heights[2]
lens = [int(x) for x in input().rstrip().split()]
heights = []
stacks = []
for i in range(0, 3):
	stacks.append([int(x) for x in reversed(input().rstrip().split())])
	heights.append(sum(stacks[i]))
while not stacks_equal(heights):
	max_len = heights.index(max(heights))
	heights[max_len] -= stacks[max_len][lens[max_len] - 1]
	lens[max_len] -= 1
print(heights[0])


from collections import deque
import sys

def isEqual(alist):
	return len(set(alist)) == 1
(n1, n2, n3) = input().split()
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
(s, h) = ([], [])
for i in range(3):
	new = deque()
	for num in input().split():
		new.append(int(num))
	s.append(new)
	h.append(sum(new))
while not isEqual(h):
	maxIndex = h.index(max(h))
	toRemove = s[maxIndex].popleft()
	h[maxIndex] -= toRemove
print(h[0])


import sys

def same(items):
	return all((x == items[0] for x in items))

def parse():
	input()
	arrs = [[], [], []]
	for i in range(3):
		arrs[i] = [int(a) for a in input().split(' ')]
		arrs[i].reverse()
	heights = [sum(arr) for arr in arrs]
	return (arrs, heights)

def solve(data):
	arrs = data[0]
	heights = data[1]
	if same(heights):
		return heights[0]
	while all(heights):
		idx = heights.index(max(heights))
		popped = arrs[idx].pop()
		heights[idx] -= popped
		if same(heights):
			return heights[0]
	return 0
print(solve(parse()))


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
n1_a = []
n2_a = []
n3_a = []
n1_h = n2_h = n3_h = 0
for height in (int(n) for n in input().split(' ')):
	n1_a.append(height)
	n1_h += height
n1_a = list(reversed(n1_a))
for height in (int(n) for n in input().split(' ')):
	n2_a.append(height)
	n2_h += height
n2_a = list(reversed(n2_a))
for height in (int(n) for n in input().split(' ')):
	n3_a.append(height)
	n3_h += height
n3_a = list(reversed(n3_a))
while not n1_h == n2_h == n3_h:
	if n1_h >= n2_h and n1_h >= n3_h:
		n1_h -= n1_a.pop()
	elif n2_h >= n1_h and n2_h >= n3_h:
		n2_h -= n2_a.pop()
	elif n3_h >= n1_h and n3_h >= n2_h:
		n3_h -= n3_a.pop()
print(n1_h)


import sys
from collections import deque
(size_1, size_2, size_3) = map(int, input().split())
(a1, a2, a3) = (deque(map(int, input().split())), deque(map(int, input().split())), deque(map(int, input().split())))
(s1, s2, s3) = (sum(a1), sum(a2), sum(a3))
while s1 != s2 or s2 != s3:
	if s1 >= s2 and s2 >= s3:
		while s1 > s3:
			s1 -= a1.popleft()
	elif s1 >= s3 and s3 >= s2:
		while s1 > s2:
			s1 -= a1.popleft()
	elif s2 >= s1 and s1 >= s3:
		while s2 > s3:
			s2 -= a2.popleft()
	elif s2 >= s3 and s3 >= s1:
		while s2 > s1:
			s2 -= a2.popleft()
	elif s3 >= s1 and s1 >= s2:
		while s3 > s2:
			s3 -= a3.popleft()
	else:
		while s3 > s1:
			s3 -= a3.popleft()
print(s1)


import sys
(A, B, C) = map(int, input().strip().split(' '))
StackA = [int(A_temp) for A_temp in input().strip().split(' ')]
StackB = [int(A_temp) for A_temp in input().strip().split(' ')]
StackC = [int(A_temp) for A_temp in input().strip().split(' ')]
flag = False
for i in range(A):
	if i != 0 and i != 1:
		StackA[-i] += StackA[-i + 1]
if A > 1:
	StackA[0] += StackA[1]
for i in range(B):
	if i != 0 and i != 1:
		StackB[-i] += StackB[-i + 1]
if B > 1:
	StackB[0] += StackB[1]
for i in range(C):
	if i != 0 and i != 1:
		StackC[-i] += StackC[-i + 1]
if C > 1:
	StackC[0] += StackC[1]
i = 0
j = 0
k = 0
while not flag and i < A and (j < B) and (k < C):
	if StackA[i] == StackB[j] and StackB[j] == StackC[k]:
		flag = True
		print(StackA[i])
	else:
		maximum = max(StackA[i], StackB[j], StackC[k])
		if maximum == StackA[i]:
			i += 1
		if maximum == StackB[j]:
			j += 1
		if maximum == StackC[k]:
			k += 1
if not flag:
	print(0)


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
d = {}
total = 0
for h in reversed(h1):
	total += h
	d[total] = 1
d2 = {}
total = 0
for h in reversed(h2):
	total += h
	if total in d:
		d2[total] = 1
total = 0
highest = 0
for h in reversed(h3):
	total += h
	if total in d2:
		if total > highest:
			highest = total
print(highest)


ans = 0
x0 = [0] * 100005
y0 = [0] * 100005
z0 = [0] * 100005
(a, b, c) = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
y = list(map(int, input().split(' ')))
z = list(map(int, input().split(' ')))
x.reverse()
y.reverse()
z.reverse()
x0[0] = x[0]
y0[0] = y[0]
z0[0] = z[0]
for i in range(1, a):
	x0[i] = x0[i - 1] + x[i]
for i in range(1, b):
	y0[i] = y0[i - 1] + y[i]
for i in range(1, c):
	z0[i] = z0[i - 1] + z[i]
k = {}
for i in range(a):
	if x0[i] in k:
		k[x0[i]] += 1
	else:
		k[x0[i]] = 1
for i in range(b):
	if y0[i] in k:
		k[y0[i]] += 1
	else:
		k[y0[i]] = 1
for i in range(c):
	if z0[i] in k:
		k[z0[i]] += 1
		if k[z0[i]] == 3:
			ans = z0[i]
	else:
		k[z0[i]] = 1
print(ans)


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
h1.append(0)
h2.append(0)
h3.append(0)
stacks = list([h1, h2, h3])
sum_stacks = [sum(i) for i in stacks]
while len(set(sum_stacks)) > 1:
	i = sum_stacks.index(max(sum_stacks))
	sum_stacks[i] = sum_stacks[i] - stacks[i][0]
	del stacks[i][0]
print(sum_stacks[0])


import sys
n = input().strip().split(' ')
stacks = []
for i in range(len(n)):
	A = input().strip().split(' ')
	stacks.append(A)
sums = [0] * len(n)
maxheight = 0
for i in range(len(n)):
	for j in range(len(stacks[i])):
		sums[i] += int(stacks[i][j])
iter = 0
for i in range(len(n)):
	iter += int(n[i])
for k in range(iter):
	minindex = sums.index(min(sums))
	for i in range(len(n)):
		if sums[i] > sums[minindex]:
			sums[i] -= int(stacks[i][0])
			stacks[i].remove(stacks[i][0])
	ifequal = all((x == sums[0] for x in sums))
	if ifequal == True:
		print(sums[minindex])
		break
	if any((stacks[i] == [] for i in range(len(n)))):
		print(0)
		break
	k += 1


import sys

def get_stack():
	stack = []
	parts = input().strip().split()
	for el in parts:
		stack.append(int(el))
	stack.reverse()
	return stack
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
st1 = get_stack()
st2 = get_stack()
st3 = get_stack()
h1 = sum(st1)
h2 = sum(st2)
h3 = sum(st3)
heights = [h1, h2, h3]
min_height = min(heights)
total_h = sum(heights)
while min_height * 3 != total_h:
	if heights[0] > min_height:
		el = st1.pop()
		heights[0] -= el
	if heights[1] > min_height:
		el = st2.pop()
		heights[1] -= el
	if heights[2] > min_height:
		el = st3.pop()
		heights[2] -= el
	min_height = min(heights)
	total_h = sum(heights)
print(min_height)


import sys

def argmax(l):
	return max(enumerate(l), key=lambda x: x[1])
(n1, n2, n3) = map(int, input().strip().split(' '))
heights = [list(map(int, input().split())) for i in range(3)]
for el in heights:
	el.reverse()
s = [sum(el) for el in heights]
while s != [0, 0, 0]:
	(a, m) = argmax(s)
	c = s.count(m)
	if c == 3:
		break
	if c == 1:
		s[a] -= heights[a][-1]
		heights[a].pop()
	else:
		el = min(filter(lambda x: x[1][1] == m, enumerate(zip(heights, s))), key=lambda x: x[1][0][-1])[0]
		s[el] -= heights[el][-1]
		heights[el].pop()
print(s[0])


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
s1 = set([])
h1 = 0
s2 = set([])
h2 = 0
s3 = set([])
h3 = 0
nums1 = input().strip().split(' ')
nums2 = input().strip().split(' ')
nums3 = input().strip().split(' ')
for num in reversed(nums1):
	h1 += int(num)
	s1.add(h1)
for num in reversed(nums2):
	h2 += int(num)
	s2.add(h2)
for num in reversed(nums3):
	h3 += int(num)
	s3.add(h3)
answer = 0
for h in s1:
	if h in s2 and h in s3:
		if h > answer:
			answer = h
print(str(answer))


def find_height(s0, s1, s2):
	heights = [sum(s0), sum(s1), sum(s2)]
	if min(heights) == max(heights):
		return heights[0]
	while s0 and s1 and s2 and (min(heights) != max(heights)):
		if heights[0] != min(heights):
			heights[0] -= s0.pop()
			continue
		if heights[1] != min(heights):
			heights[1] -= s1.pop()
			continue
		if heights[2] != min(heights):
			heights[2] -= s2.pop()
	if min(heights) == max(heights):
		return heights[0]
	else:
		return 0
_ = input()
s0 = [int(x) for x in input().split()]
s1 = [int(x) for x in input().split()]
s2 = [int(x) for x in input().split()]
s0.reverse()
s1.reverse()
s2.reverse()
print(find_height(s0, s1, s2))


import sys
import math
n = [int(i) for i in input().strip().split(' ')]
c1 = [int(i) for i in input().strip().split(' ')]
c2 = [int(i) for i in input().strip().split(' ')]
c3 = [int(i) for i in input().strip().split(' ')]
s1 = sum(c1)
s2 = sum(c2)
s3 = sum(c3)
while True:
	if len(c1) == 0 and len(c2) == 0 and (len(c3) == 0):
		break
	if s1 == s2 and s2 == s3:
		break
	m = max(s1, s2, s3)
	if s1 == m:
		s1 -= c1[0]
		c1.pop(0)
	elif s2 == m:
		s2 -= c2[0]
		c2.pop(0)
	elif s3 == m:
		s3 -= c3[0]
		c3.pop(0)
print(s1)


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
(s1, s2, s3) = (sum(h1), sum(h2), sum(h3))
while not (s1 == s2 and s2 == s3):
	if s1 > s2:
		s1 -= h1.pop(0)
	elif s3 > s2:
		s3 -= h3.pop(0)
	else:
		s2 -= h2.pop(0)
print(s1)


(a, b, c) = input().split()
a = int(a)
b = int(b)
c = int(c)
a1 = [int(i) for i in input().strip().split(' ')]
b1 = [int(i) for i in input().strip().split(' ')]
c1 = [int(i) for i in input().strip().split(' ')]
sum1 = [sum(a1), sum(b1), sum(c1)]
x = 0
y = 0
z = 0
while 1:
	if sum1[0] == sum1[1] and sum1[0] == sum1[2]:
		print(sum1[0])
		break
	elif sum1[0] > sum1[1] or sum1[0] > sum1[2]:
		sum1[0] -= a1[x]
		x += 1
		continue
	elif sum1[1] > sum1[0] or sum1[0] > sum1[2]:
		sum1[1] -= b1[y]
		y += 1
		continue
	elif sum1[2] > sum1[1] or sum1[2] > sum1[0]:
		sum1[2] -= c1[z]
		z += 1
		continue


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
l1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
l2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
l3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
s1 = sum(l1)
s2 = sum(l2)
s3 = sum(l3)
m = max(s1, s2, s3)
while True:
	mi = min(s1, s2, s3)
	m = max(s1, s2, s3)
	if s1 == s2 and s1 == s3 or mi == 0:
		break
	if s1 == m:
		while s1 > mi:
			s1 -= l1[0]
			del l1[0]
	elif s2 == m:
		while s2 > mi:
			s2 -= l2[0]
			del l2[0]
	elif s3 == m:
		while s3 > mi:
			s3 -= l3[0]
			del l3[0]
print(mi)


tot = input()
first = [int(i) for i in input().split()]
first_sum = sum(first)
second = [int(i) for i in input().split()]
second_sum = sum(second)
third = [int(i) for i in input().split()]
third_sum = sum(third)
while not first_sum == second_sum == third_sum:
	if first_sum > second_sum:
		if first_sum > third_sum:
			first_sum -= first.pop(0)
		else:
			third_sum -= third.pop(0)
	elif second_sum > third_sum:
		second_sum -= second.pop(0)
	else:
		third_sum -= third.pop(0)
print(first_sum)


import sys
n = list(map(int, input().split()))
s = [list(map(int, input().split())) for x in range(3)]
for st in s:
	st.reverse()
h = [sum(st) for st in s]
while not h[0] == h[1] == h[2]:
	h[0] -= s[0].pop()
	for i in range(1, 3):
		while h[i] > h[0]:
			h[i] -= s[i].pop()
print(h[0])


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
height1 = sum(h1)
height2 = sum(h2)
height3 = sum(h3)
while height1 != height2 or height1 != height3 or height2 != height3:
	if height1 >= height2 and height1 >= height3:
		height1 -= h1.pop(0)
	elif height2 >= height1 and height2 >= height3:
		height2 -= h2.pop(0)
	elif height3 >= height1 and height3 >= height2:
		height3 -= h3.pop(0)
print(height1)


from operator import itemgetter
(n1, n2, n3) = map(int, input().strip().split(' '))
s1 = [int(a) for a in reversed(input().strip().split(' '))]
s2 = [int(a) for a in reversed(input().strip().split(' '))]
s3 = [int(a) for a in reversed(input().strip().split(' '))]
stacks = tuple(({'stack': s, 'height': sum(s)} for s in (s1, s2, s3)))
while not stacks[0]['height'] == stacks[1]['height'] == stacks[2]['height']:
	s = max(stacks, key=itemgetter('height'))
	s['height'] -= s['stack'].pop()
print(stacks[0]['height'])


import sys

def max_index(array):
	max_val = array[0]
	max_index = 0
	for index in range(len(array)):
		if array[index] > max_val:
			max_index = index
	return max_index
(n0, n1, n2) = input().strip().split(' ')
(n0, n1, n2) = [int(n0), int(n1), int(n2)]
h0 = [int(h0_temp) for h0_temp in input().strip().split(' ')]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
stacks = [h0, h1, h2]
sums = [sum(h0), sum(h1), sum(h2)]
while not sums[0] == sums[1] == sums[2]:
	tallest = max_index(sums)
	top_of_tallest = stacks[tallest].pop(0)
	sums[tallest] -= top_of_tallest
print(sums[0])


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
sum = {0: 0, 1: 0, 2: 0}
for i in h1:
	sum[0] += i
for i in h2:
	sum[1] += i
for i in h3:
	sum[2] += i
dict = {0: h1, 1: h2, 2: h3}
ch = 0
while True:
	min_sum = min(sum.values())
	max_sum = max(sum.values())
	if max_sum == min_sum:
		break
	if min_sum == 0:
		ch = 1
		print('0')
		break
	for (key, val) in sum.items():
		if val == max_sum:
			x = dict[key]
			sum[key] -= x[0]
			del x[0]
			break
if ch == 0:
	print(min_sum)


def eqStacks(s1, s2, s3):
	h1 = sum(s1)
	h2 = sum(s2)
	h3 = sum(s3)
	while not (h1 == h2 and h2 == h3):
		if h1 >= h2 and h1 >= h3:
			h1 = h1 - s1.pop(0)
		elif h2 >= h3:
			h2 = h2 - s2.pop(0)
		else:
			h3 = h3 - s3.pop(0)
	return h1
ignore = input()
s1 = [int(i) for i in input().split(' ')]
s2 = [int(i) for i in input().split(' ')]
s3 = [int(i) for i in input().split(' ')]
print(eqStacks(s1, s2, s3))


import sys

def stackSum(stack):
	height = 0
	for i in stack:
		height += int(i)
	return height
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
stack1 = stackSum(h1)
index1 = 0
stack2 = stackSum(h2)
index2 = 0
stack3 = stackSum(h3)
index3 = 0
while stack1 != stack3 or stack1 != stack2:
	while stack1 != stack2:
		if stack1 > stack2:
			stack1 -= h1[index1]
			index1 += 1
		else:
			stack2 -= h2[index2]
			index2 += 1
	if stack1 > stack3:
		stack1 -= h1[index1]
		index1 += 1
	elif stack1 < stack3:
		stack3 -= h3[index3]
		index3 += 1
print(stack1)


import sys
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
h2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
h3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
h1.reverse()
h2.reverse()
h3.reverse()
for i in range(1, n1):
	h1[i] += h1[i - 1]
for i in range(1, n2):
	h2[i] += h2[i - 1]
for i in range(1, n3):
	h3[i] += h3[i - 1]
(i, j, k) = (n1 - 1, n2 - 1, n3 - 1)
ans = 0
while True:
	if i < 0 or j < 0 or k < 0:
		break
	if h1[i] == h2[j] == h3[k]:
		ans = h1[i]
		break
	mx = max(h1[i], h2[j], h3[k])
	if mx == h1[i]:
		h1.pop()
		i -= 1
	elif mx == h2[j]:
		h2.pop()
		j -= 1
	else:
		h3.pop()
		k -= 1
print(ans)


import sys
from itertools import accumulate
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
c1 = [int(h1_temp) for h1_temp in input().strip().split(' ')]
c2 = [int(h2_temp) for h2_temp in input().strip().split(' ')]
c3 = [int(h3_temp) for h3_temp in input().strip().split(' ')]
c1.reverse()
c2.reverse()
c3.reverse()
c1_hts = list(accumulate(c1))
c2_hts = list(accumulate(c2))
c3_hts = list(accumulate(c3))
c1_hts = set(c1_hts)
c2_hts = set(c2_hts)
c3_hts = set(c3_hts)
try:
	print(max(c1_hts.intersection(c2_hts).intersection(c3_hts)))
except:
	print('0')


import sys
from collections import deque
(n1, n2, n3) = input().strip().split(' ')
(n1, n2, n3) = [int(n1), int(n2), int(n3)]
h1 = deque([int(h1_temp) for h1_temp in input().strip().split(' ')])
h2 = deque([int(h2_temp) for h2_temp in input().strip().split(' ')])
h3 = deque([int(h3_temp) for h3_temp in input().strip().split(' ')])
sumh1 = sum(h1)
sumh2 = sum(h2)
sumh3 = sum(h3)

def getEqual():
	if sumh1 == sumh2 == sumh3:
		return False
	else:
		return True
while getEqual():
	curmin = min(sumh1, sumh2, sumh3)
	if curmin != sumh1:
		sumh1 = sumh1 - h1.popleft()
	if curmin != sumh2:
		sumh2 = sumh2 - h2.popleft()
	if curmin != sumh3:
		sumh3 = sumh3 - h3.popleft()
print(sumh3)