AtCoder Beginner Contest 135 - City Savers

There are N+1 towns. The i-th town is being attacked by A_i monsters.
We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.
What is the maximum total number of monsters the heroes can cooperate to defeat?

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 10^5
 - 1 \leq A_i \leq 10^9
 - 1 \leq B_i \leq 10^9

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

-----Output-----
Print the maximum total number of monsters the heroes can defeat.

-----Sample Input-----
2
3 5 2
4 5

-----Sample Output-----
9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.
 - The first hero defeats two monsters attacking the first town and two monsters attacking the second town.
 - The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Source: atcoder | Difficulty: EASY


Solutions:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
	ans += min(a[i], b[i]) + min(a[i + 1], max(b[i] - a[i], 0))
	a[i + 1] = max(a[i + 1] - max(b[i] - a[i], 0), 0)
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
for i in range(n):
	(a[i], b[i]) = (max(a[i] - b[i], 0), max(b[i] - a[i], 0))
	a[i + 1] = max(a[i + 1] - b[i], 0)
s2 = sum(a)
print(s - s2)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
for (i, b) in enumerate(B):
	c = min(b, A[i] + A[i + 1])
	cnt += c
	A[i + 1] -= max(0, c - A[i])
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
for i in range(n):
	if a[i] < b[i]:
		c += min(a[i] + a[i + 1], b[i])
		a[i + 1] = max(a[i + 1] + a[i] - b[i], 0)
	else:
		c += b[i]
print(c)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
	if A[i] >= B[i]:
		ans += B[i]
		A[i] -= B[i]
	else:
		temp = B[i] - A[i]
		ans += A[i]
		if A[i + 1] >= temp:
			ans += temp
			A[i + 1] -= temp
		else:
			ans += A[i + 1]
			A[i + 1] = 0
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
	p = min(A[i], B[i])
	ans += p
	B[i] -= p
	p = min(A[i + 1], B[i])
	ans += p
	A[i + 1] -= p
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ret = 0
for (i, b) in enumerate(B):
	k1 = min(A[i], b)
	k2 = min(A[i + 1], b - k1)
	A[i + 1] -= k2
	ret += k1 + k2
print(ret)


R = lambda : map(int, input().split())
input()
(a, b) = (list(R()), R())
r = y = 0
for (u, v, w) in zip(b, a, a[1:]):
	x = min(v - y, u)
	y = min(w, u - x)
	r += x + y
print(r)


n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
ans = 0
for i in range(n):
	t1 = min(aa[i], bb[i])
	aa[i] -= t1
	bb[i] -= t1
	ans += t1
	t2 = min(aa[i + 1], bb[i])
	aa[i + 1] -= t2
	ans += t2
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
	if a[i] >= b[i]:
		ans += b[i]
		a[i] -= b[i]
		b[i] = 0
	else:
		ans += a[i]
		b[i] -= a[i]
		a[i] = 0
		if a[i + 1] >= b[i]:
			ans += b[i]
			a[i + 1] -= b[i]
			b[i] = 0
		else:
			ans += a[i + 1]
			b[i] -= a[i + 1]
			a[i + 1] = 0
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
for n in range(N):
	b = min(A[n], B[n])
	B[n] -= b
	b2 = min(A[n + 1], B[n])
	A[n + 1] -= b2
	res += b + b2
print(res)


f = lambda : list(map(int, input().split()))
n = f()[0]
l = f()
(c, t) = (0, l[0])
for (a, b) in zip(l[1:], f()):
	s = min(t + a, b)
	t = min(max(t + a - s, 0), a)
	c += s
print(c)


N = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans = 0
r = 0
for i in range(N):
	if q[i] + r > p[i]:
		ans += p[i]
		r = min(q[i] + r - p[i], q[i])
	else:
		ans += q[i] + r
		r = 0
ans += min(p[N], r)
print(ans)


N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
ans = 0
for i in range(N):
	l = min(A[i], B[i])
	ans += l
	r = min(A[i + 1], B[i] - l)
	ans += r
	A[i + 1] -= r
print(ans)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(n):
	rem = max(B[i] - A[i], 0)
	ans += min(A[i], B[i]) + min(rem, A[i + 1])
	A[i + 1] = max(A[i + 1] - rem, 0)
print(ans)


N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = sum(a)
for i in range(N):
	if a[i] <= b[i]:
		b[i] = b[i] - a[i]
		a[i] = 0
		if a[i + 1] <= b[i]:
			a[i + 1] = 0
		elif a[i + 1] > b[i]:
			a[i + 1] = a[i + 1] - b[i]
	elif a[i] > b[i]:
		a[i] = a[i] - b[i]
num = num - sum(a)
print(num)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
for i in range(0, N):
	if B[i] <= A[i]:
		cnt += B[i]
	elif B[i] > A[i] and B[i] <= A[i] + A[i + 1]:
		cnt += B[i]
		A[i + 1] = A[i + 1] - B[i] + A[i]
	else:
		cnt += A[i] + A[i + 1]
		A[i + 1] = 0
print(cnt)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
s = sum(A)
for i in range(N):
	t1 = max(A[i] - B[i], 0)
	t2 = max(B[i] - A[i], 0)
	A[i] = t1
	A[i + 1] = max(A[i + 1] - t2, 0)
print(s - sum(A))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
S = 0
for i in range(N):
	Y = min(A[i], B[i])
	S += Y
	B[i] -= Y
	Z = min(A[i + 1], B[i])
	A[i + 1] -= Z
	S += Z
print(S)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
res = 0
for i in range(N):
	enm = min(A[i] + A[i + 1] - res, B[i])
	res = max(res + enm - A[i], 0)
	cnt += enm
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for (i, j) in enumerate(b):
	c = min(j, a[i] + a[i + 1])
	cnt += c
	a[i + 1] -= max(0, c - a[i])
print(cnt)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
count = 0
for i in range(N):
	if A[i] <= B[i] <= A[i] + A[i + 1]:
		count += B[i]
		(A[i], A[i + 1]) = (0, A[i + 1] - (B[i] - A[i]))
	elif B[i] > A[i] + A[i + 1]:
		count += A[i] + A[i + 1]
		(A[i], A[i + 1]) = (0, 0)
	elif B[i] < A[i]:
		count += B[i]
		A[i] = A[i] - B[i]
print(count)


N = int(input())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
ans = 0
for i in range(N):
	k = min(A[i], B[i])
	l = min(A[i + 1], B[i] - k)
	ans += k + l
	A[i + 1] -= l
print(ans)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
s = A[0]
ans = 0
for i in range(N):
	t = s if B[i] >= s else B[i]
	u = A[i + 1] if A[i + 1] <= B[i] - t else B[i] - t
	ans += t + u
	s = A[i + 1] - u
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
monster = []
for i in range(N):
	monster.append(min(A[i] + A[i + 1], B[i]))
	A[i + 1] = min(A[i] + A[i + 1] - monster[i], A[i + 1])
print(sum(monster))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
M = sum(A)
for n in range(N):
	if A[n] >= B[n]:
		A[n] -= B[n]
	else:
		A[n + 1] = max(0, A[n + 1] + A[n] - B[n])
		A[n] = 0
print(M - sum(A))


N = int(input())
m = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
for i in range(N):
	l = min(m[i], b[i])
	s += l
	m[i] -= l
	b[i] -= l
	r = min(m[i + 1], b[i])
	s += r
	m[i + 1] -= r
print(s)


N = int(input())
A_ls = list(map(int, input().split(' ')))
B_ls = list(map(int, input().split(' ')))
rst = 0
for (i, val) in enumerate(B_ls):
	if A_ls[i] + A_ls[i + 1] >= val:
		rst += val
		if A_ls[i] >= val:
			A_ls[i] -= val
		else:
			A_ls[i + 1] -= val - A_ls[i]
	else:
		rst += A_ls[i] + A_ls[i + 1]
		(A_ls[i], A_ls[i + 1]) = (0, 0)
print(rst)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split())) + [0]
ans = c = 0
for i in range(N + 1):
	a = A[i]
	b = B[i]
	ans += min(a, c)
	a = max(a - c, 0)
	ans += min(a, b)
	c = max(b - a, 0)
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
(ans, num) = (0, 0)
for i in range(N):
	ans += min(B[i], A[i] + A[i + 1])
	d = B[i] - A[i]
	if d > 0:
		A[i + 1] -= min(d, A[i + 1])
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Sum_1 = sum(A)
for i in range(N):
	A[i + 1] = max(A[i + 1] - max(B[i] - A[i], 0), 0)
	A[i] = max(A[i] - B[i], 0)
Sum_2 = sum(A)
print(Sum_1 - Sum_2)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
M = 0
for i in range(N):
	M += min(A[i], B[i])
	B[i] -= min(A[i], B[i])
	M += min(A[i + 1], B[i])
	A[i + 1] -= min(A[i + 1], B[i])
print(M)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tmp = sum(a)
for i in range(n):
	if a[i] < b[i]:
		b[i] -= a[i]
		a[i] = 0
		a[i + 1] -= min(a[i + 1], b[i])
	else:
		a[i] -= b[i]
print(tmp - sum(a))


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = 0
for i in range(n):
	c += min(A[i], B[i])
	if A[i] < B[i]:
		c += min(A[i + 1], B[i] - A[i])
		A[i + 1] -= min(A[i + 1], B[i] - A[i])
print(c)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
e = 0
for i in range(1, n + 1):
	t = min(a[-i], b[-i])
	u = min(a[--~i], b[-i] - t)
	e += t + u
	a[--~i] -= u
print(e)


n = int(input())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
cnt = 0
for i in range(n):
	cnt += min(a[i] + a[i + 1], b[i])
	if b[i] < a[i] + a[i + 1]:
		a[i + 1] = min(a[i] + a[i + 1] - b[i], a[i + 1])
	else:
		a[i + 1] = 0
print(cnt)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
count = 0
for i in range(N):
	for j in range(2):
		tmp = min(A[i + j], B[i])
		count += tmp
		A[i + j] -= tmp
		B[i] -= tmp
print(count)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = A[0]
d = 0
for i in range(N):
	d += max(0, c - B[i])
	c = max(0, A[i + 1] + min(0, c - B[i]))
print(sum(A) - (d + c))


n = int(input())
aaa = list(map(int, input().split()))
sa = sum(aaa)
bbb = list(map(int, input().split()))
for (i, b) in enumerate(bbb):
	c = min(aaa[i], b)
	aaa[i] -= c
	aaa[i + 1] -= min(aaa[i + 1], b - c)
print(sa - sum(aaa))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
(c, d, x) = (0, 0, 0)
for i in range(n):
	d = min(int(a[i] - c), b[i])
	c = min(a[i + 1], b[i] - min(int(a[i] - c), b[i]))
	x += c + d
print(x)


n = int(input())
ali = list(map(int, input().split()))
bli = list(map(int, input().split()))
ans = 0
for i in range(n):
	ans += min(ali[i] + ali[i + 1], bli[i])
	ali[i + 1] = max(0, ali[i + 1] + min(ali[i] - bli[i], 0))
print(ans)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
B.append(0)
for i in reversed(list(range(n))):
	if B[i] <= A[i] + A[i + 1]:
		ans += B[i]
		if B[i] < A[i + 1]:
			A[i] = A[i]
		else:
			A[i] = A[i] - (B[i] - A[i + 1])
	else:
		ans += A[i] + A[i + 1]
		A[i] = 0
print(ans)


N = int(input())
As = input().split(' ')
As = [int(As[i]) for i in range(N + 1)]
Bs = input().split(' ')
Bs = [int(Bs[i]) for i in range(N)]
WIN = 0
for i in range(N):
	if Bs[i] > As[i]:
		WIN += As[i]
		Bs[i] -= As[i]
		As[i] = 0
		if Bs[i] > As[i + 1]:
			WIN += As[i + 1]
			Bs[i] -= As[i + 1]
			As[i + 1] = 0
		elif Bs[i] == As[i + 1]:
			WIN += As[i + 1]
			Bs[i] = 0
			As[i + 1] = 0
		else:
			WIN += Bs[i]
			As[i + 1] -= Bs[i]
			Bs[i] = 0
	elif Bs[i] == As[i]:
		WIN += As[i]
		Bs[i] = 0
		As[i] = 0
	else:
		WIN += Bs[i]
		As[i] -= Bs[i]
		Bs[i] = 0
print(WIN)


n = int(input())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))
count = 0
for i in range(n):
	count += min(la[i], lb[i])
	if la[i] < lb[i]:
		if la[i + 1] >= lb[i] - la[i]:
			count += lb[i] - la[i]
			la[i + 1] -= lb[i] - la[i]
		else:
			count += la[i + 1]
			la[i + 1] = 0
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
q = sum(a)
for i in range(n):
	if a[i] <= b[i]:
		a[i + 1] -= b[i] - a[i]
		a[i] = 0
		if a[i + 1] < 0:
			a[i + 1] = 0
	elif a[i] > b[i]:
		a[i] -= b[i]
print(q - sum(a))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
r = 0
for (i, j) in enumerate(B):
	c = min(j, A[i] + A[i + 1])
	r += c
	A[i + 1] -= max(0, c - A[i])
print(r)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0] * (N + 1)
cnt = 0
C[0] = A[0]
for i in range(1, N + 1):
	if C[i - 1] > B[i - 1]:
		C[i] = A[i]
		cnt += A[i - 1] + B[i - 1] - C[i - 1]
	elif C[i - 1] <= B[i - 1]:
		if A[i] - B[i - 1] + C[i - 1] >= 0:
			C[i] = A[i] - B[i - 1] + C[i - 1]
			cnt += A[i - 1]
		else:
			C[i] = 0
			cnt += A[i - 1]
if B[N - 1] - C[N - 1] > 0:
	ans = cnt + min(A[N], B[N - 1] - C[N - 1])
else:
	ans = cnt
print(ans)


n = int(input())
l = list(map(int, input().split()))
la = list(map(int, input().split()))
ans = 0
for (m, i) in enumerate(la):
	ans += min(i, l[m]) + l[m + 1] - max(0, l[m + 1] - i + min(i, l[m]))
	l[m + 1] = max(0, l[m + 1] - i + min(i, l[m]))
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
	c = b[i] - a[i]
	if c <= 0:
		cnt += b[i]
	else:
		d = b[i] - a[i + 1] - a[i]
		if d <= 0:
			cnt += b[i]
			a[i + 1] -= c
		else:
			cnt += a[i] + a[i + 1]
			a[i + 1] = 0
print(cnt)


from collections import deque
from bisect import bisect_left
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
for i in range(N):
	if A[i] >= B[i]:
		cnt += B[i]
	else:
		cnt += A[i]
		d = B[i] - A[i]
		if d <= A[i + 1]:
			cnt += d
			A[i + 1] -= d
		else:
			cnt += A[i + 1]
			A[i + 1] = 0
print(cnt)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
ans = 0
for (i, d) in enumerate(B):
	l = min(A[i], d)
	r = min(A[i + 1], d - l)
	A[i + 1] -= r
	ans += l + r
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
first = min(a[0], b[0])
more = min(a[1], b[0] - first)
count = first + more
for i in range(1, n):
	first = min(a[i] - more, b[i])
	more = min(a[i + 1], b[i] - first)
	count += first + more
print(count)


(n, *a) = map(int, open(0).read().split())
c = 0
for i in range(1, n + 1):
	c += min(a[i - 1] + a[i], a[n + i])
	a[i] -= max(min(a[n + i] - a[i - 1], a[i]), 0)
print(c)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Ans = 0
for i in range(N):
	Ans += min(A[i] + A[i + 1], B[i])
	A[i + 1] = max(0, A[i + 1] + min(A[i] - B[i], 0))
print(Ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pre = a[0]
count = 0
for i in range(n):
	atack = min(a[i + 1] + a[i], b[i])
	count += atack
	if atack > a[i]:
		a[i + 1] -= atack - a[i]
print(count)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()] + [0]
ans = 0
j = 0
for i in range(n + 1):
	ans += min(j + b[i], a[i])
	j = max(0, b[i] - max(0, a[i] - j))
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
s = sum(a)
for i in range(n):
	x = a[i]
	y = b[i]
	a[i] -= min(x, y)
	b[i] -= min(x, y)
	if b[i] > 0:
		a[i + 1] -= min(a[i + 1], b[i])
print(s - sum(a))


def main():
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = 0
	for i in range(N):
		tmp = A[i] - B[i]
		if tmp < 0:
			ans += A[i]
			if A[i + 1] < -tmp:
				ans += A[i + 1]
				A[i + 1] = 0
			else:
				ans += -tmp
				A[i + 1] += tmp
		else:
			ans += B[i]
	print(ans)

def __starting_point():
	main()
__starting_point()


from numpy import cumsum
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
	ans += min(b[i], a[i] + a[i + 1])
	a[i + 1] -= max(0, min(b[i] - a[i], a[i + 1]))
print(ans)


R = lambda : map(int, input().split())
input()
(a, b) = (list(R()), R())
r = 0
for (i, u) in enumerate(b):
	x = min(a[i], u)
	y = min(a[i + 1], u - x)
	a[i + 1] -= y
	r += x + y
print(r)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = sum(A)
for i in range(n):
	if B[i] > A[i]:
		A[i + 1] = max(0, A[i + 1] - (B[i] - A[i]))
		A[i] = 0
	else:
		A[i] -= B[i]
		B[i] = 0
print(ans - sum(A))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
for i in range(N):
	c1 = min(A[i], B[i])
	c2 = min(A[i + 1], max(0, B[i] - A[i]))
	A[i + 1] -= c2
	count += c1 + c2
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
br = 0
for i in range(len(b)):
	x = min(a[i], b[i])
	a[i] -= x
	b[i] -= x
	br += x
	x = min(a[i + 1], b[i])
	a[i + 1] -= x
	b[i] -= x
	br += x
print(br)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
for i in range(N):
	if A[i] < B[i]:
		res += min(A[i + 1] + A[i], B[i])
		A[i + 1] = max(A[i + 1] + A[i] - B[i], 0)
	else:
		res += B[i]
print(res)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
pre = sum(A)
for i in range(N):
	if B[i] <= A[i]:
		A[i] -= B[i]
	else:
		A[i + 1] = max(0, A[i + 1] - B[i] + A[i])
		A[i] = 0
print(pre - sum(A))


R = lambda : list(map(int, input().split()))
n = int(input())
(a, b) = (R(), R())
r = 0
for i in range(n):
	x = min(a[i], b[i])
	y = min(a[i + 1], b[i] - x)
	a[i + 1] -= y
	r += x + y
print(r)


N = int(input())
(*A,) = map(int, input().split())
(*B,) = map(int, input().split())
ans = 0
for i in range(N):
	m1 = min(B[i], A[i])
	m2 = min(A[i + 1], B[i] - m1)
	A[i + 1] -= m2
	ans += m1 + m2
print(ans)


n = int(input())
a = [int(n) for n in input().split()]
b = [int(n) for n in input().split()]
b.append(0)
r = 0
t = 0
x = 0
for i in range(n + 1):
	t = b[i] + x - a[i]
	if t >= 0:
		r = r + a[i]
		x = min(b[i], t)
	else:
		r = r + b[i] + x
		x = 0
print(r)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
count = 0
for x in range(n):
	if a[x] >= b[x]:
		count += b[x]
	if a[x] < b[x]:
		count += a[x]
		if b[x] - a[x] <= a[x + 1]:
			count += b[x] - a[x]
			a[x + 1] = a[x + 1] - (b[x] - a[x])
		else:
			count += a[x + 1]
			a[x + 1] = 0
print(count)


N = int(input())
M_List = list(map(int, input().split()))
N_List = list(map(int, input().split()))
ans = 0
for i in range(N):
	if N_List[i] >= M_List[i]:
		ans += M_List[i]
		ans += (M_List[i + 1], N_List[i] - M_List[i])[N_List[i] - M_List[i] <= M_List[i + 1]]
		M_List[i + 1] = max(0, M_List[i + 1] - (N_List[i] - M_List[i]))
	else:
		ans += N_List[i]
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
kill = 0
for i in range(N):
	x1 = min(B[i], A[i])
	x2 = min(B[i] - x1, A[i + 1])
	A[i + 1] -= x2
	kill += x1 + x2
print(kill)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
result = 0
for n in range(N):
	result += min(B[n], A[n] + A[n + 1])
	if B[n] > A[n]:
		A[n + 1] -= min(B[n] - A[n], A[n + 1])
print(result)


R = lambda : map(int, input().split())
input()
a = list(R())

def f():
	s = 0
	for (x, y, z) in zip(R(), a, a[1:]):
		t = min(x, y - s)
		s = min(z, x - t)
		yield (s + t)
print(sum(f()))


(n, p, *a) = map(int, open(0).read().split())
c = 0
for (a, b) in zip(a, a[n:]):
	c += min(p + a, b)
	p = a - max(min(b - p, a), 0)
print(c)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = 0
for i in range(N):
	ni0 = min(A[i], B[i])
	ni1 = min(A[i + 1], B[i] - ni0)
	A[i] -= ni0
	A[i + 1] -= ni1
	total += ni0 + ni1
print(total)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
sa = sum(a)
for (i, b) in enumerate(b):
	c = min(a[i], b)
	a[i] -= c
	a[i + 1] -= min(a[i + 1], b - c)
print(sa - sum(a))


n = int(input())
T = list(map(int, input().split()))
AB = list(map(int, input().split()))
ans = 0
for i in range(n):
	ans += min(T[i] + T[i + 1], AB[i])
	T[i + 1] = max(0, T[i + 1] - max(0, AB[i] - T[i]))
print(ans)


n = int(input())
m = list(map(int, input().split()))
h = list(map(int, input().split()))
a = 0
for i in range(n):
	if h[i] < m[i]:
		a += h[i]
	else:
		a += m[i]
		r = h[i] - m[i]
		s = min(m[i + 1], r)
		a += s
		m[i + 1] -= s
print(a)


import sys
input = sys.stdin.readline
import numpy as np

def main():
	n = int(input())
	mob = np.array(list(map(int, input().split())), dtype=int)
	bra = np.array(list(map(int, input().split())), dtype=int)
	ans = 0
	for i in range(n):
		ans += min(mob[i], bra[i])
		ans += min(mob[i + 1], bra[i] - min(mob[i], bra[i]))
		mob[i + 1] -= min(mob[i + 1], bra[i] - min(mob[i], bra[i]))
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
for (i, b) in enumerate(B):
	count += min(A[i], b)
	b -= A[i]
	if b > 0:
		x = min(A[i + 1], b)
		A[i + 1] -= x
		count += x
print(count)


import copy
N = int(input())
A = list(map(int, input().split()))
A_copy = copy.deepcopy(A)
B = list(map(int, input().split()))
ans = 0
for i in range(N):
	if A[i] <= B[i]:
		B[i] -= A[i]
		A[i] = 0
		if B[i] > 0:
			if A[i + 1] <= B[i]:
				A[i + 1] = 0
				B[i] -= A[i + 1]
			elif A[i + 1] > B[i]:
				A[i + 1] -= B[i]
				B[i] = 0
	elif A[i] > B[i]:
		A[i] -= B[i]
		B[i] = 0
ans = sum(A_copy) - sum(A)
print(ans)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
for i in range(n):
	d = min(A[i], B[i])
	cnt += d
	A[i] -= d
	B[i] -= d
	d = min(A[i + 1], B[i])
	cnt += d
	A[i + 1] -= d
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
	cnt += a[i] + a[i + 1] - max(a[i] + a[i + 1] - b[i], 0)
	a[i + 1] = max(0, a[i + 1] - max(0, b[i] - a[i]))
print(cnt)


n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
bef = sum(aa)
for i in range(n):
	aa[i] -= bb[i]
	if aa[i] >= 0:
		bb[i] = 0
	else:
		bb[i] = 0 - aa[i]
		aa[i] = 0
	aa[i + 1] -= bb[i]
	if aa[i + 1] >= 0:
		bb[i] = 0
	else:
		bb[i] = 0 - aa[i + 1]
		aa[i + 1] = 0
print(bef - sum(aa))


from itertools import tee
R = lambda : map(int, input().split())
input()
((a, b), c) = (tee(R()), R())
next(b)
r = y = 0
for (u, v, w) in zip(c, a, b):
	x = min(v - y, u)
	y = min(w, u - x)
	r += x + y
print(r)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
cnt = 0
for i in range(N):
	cnt += min(A[i] + A[i + 1], B[i])
	if A[i] < B[i]:
		A[i + 1] = max(A[i + 1] + A[i] - B[i], 0)
print(cnt)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for j in range(N):
	tmp = min(A[j], B[j])
	ans += tmp
	B[j] -= tmp
	tmp = min(A[j + 1], B[j])
	ans += tmp
	A[j + 1] -= tmp
print(ans)


INT = lambda : int(input())
INTM = lambda : map(int, input().split())
STRM = lambda : map(str, input().split())
STR = lambda : str(input())
LIST = lambda : list(map(int, input().split()))
LISTS = lambda : list(map(str, input().split()))

def do():
	n = INT()
	A = LIST()
	B = LIST()
	ans = 0
	ta = A[0]
	for i in range(n):
		tb = B[i]
		ans += min(ta, tb)
		(ta, tb) = (max(0, ta - tb), max(0, tb - ta))
		ta += A[i + 1]
		ans += min(ta, tb)
		ta = max(0, ta - tb)
		ta = min(ta, A[i + 1])
	print(ans)

def __starting_point():
	do()
__starting_point()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
for i in range(n):
	for j in range(2):
		xi = min(a[i + j], b[i])
		a[i + j] -= xi
		b[i] -= xi
		x += xi
print(x)


N = int(input())
(A, B) = (list(map(int, input().split())), list(map(int, input().split())))
ans = 0
for (i, b) in enumerate(B):
	temp = min(b, A[i] + A[i + 1])
	ans += temp
	A[i + 1] -= max(0, temp - A[i])
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = 0
for i in range(N):
	d = min(A[i], B[i])
	D += d
	B[i] -= d
	d = min(A[i + 1], B[i])
	D += d
	A[i + 1] -= d
print(D)


import copy
N = int(input())
An = [int(x) for x in input().split()]
Bn = [int(x) for x in input().split()]
maxMon = sum(An)
for (i, B) in enumerate(Bn):
	An[i + 1] = max(0, An[i + 1] - max(0, B - An[i]))
	An[i] = max(0, An[i] - B)
print(maxMon - sum(An))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
save = 0
for i in range(N):
	count += min(A[i], B[i] + save)
	save = max(0, B[i] - max(0, A[i] - save))
count += min(A[N], save)
print(count)


f = lambda : list(map(int, input().split()))
n = int(input())
la = f()
lb = f()
tmp = 0
ans = 0
for i in range(n):
	m = min(la[i], tmp + lb[i])
	tmp = min(tmp + lb[i] - m, lb[i])
	ans += m
print(ans + min(la[n], tmp))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
for (i, b) in enumerate(B):
	attack = min(b, A[i] + A[i + 1])
	count += attack
	A[i + 1] -= max(0, attack - A[i])
print(count)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
S = sum(A)
for i in range(N):
	if B[i] <= A[i]:
		A[i] -= B[i]
	elif A[i] < B[i] <= A[i] + A[i + 1]:
		B[i] -= A[i]
		A[i] = 0
		A[i + 1] -= B[i]
	else:
		A[i] = 0
		A[i + 1] = 0
S_ = sum(A)
print(S - S_)


N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = 0
ans = 0
for i in range(N):
	cnt = min(a[i], b[i] + m)
	ans += cnt
	m = min(b[i] + m - cnt, b[i])
ans += min(a[N], m)
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
count = 0
for i in range(n):
	count += min(a[i] + a[i + 1], b[i])
	a[i + 1] = max(a[i + 1] + min(a[i] - b[i], 0), 0)
print(count)


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return [float(x) for x in input().split()]

def LSS():
	return input().split()

def resolve():
	N = I()
	A = LI()
	B = LI()
	ans = 0
	for i in range(N):
		ans += min(A[i], B[i])
		B[i] -= min(A[i], B[i])
		ans += min(A[i + 1], B[i])
		A[i + 1] -= min(A[i + 1], B[i])
	print(ans)

def __starting_point():
	resolve()
__starting_point()


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split())) + [0]
(res, p) = (0, 0)
for i in range(N + 1):
	pp = B[i] + p
	res += min(A[i], pp)
	p = max(min(pp - A[i], B[i]), 0)
print(res)


N = int(input())
(*A,) = map(int, input().split())
(*B,) = map(int, input().split())
ans = 0
for i in range(N):
	a0 = min(A[i], B[i])
	B[i] -= a0
	a1 = min(A[i + 1], B[i])
	ans += a0 + a1
	A[i + 1] -= a1
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for (i, bb) in enumerate(b):
	ans += min(a[i], bb)
	sub = max(bb - a[i], 0)
	ans += min(a[i + 1], sub)
	a[i + 1] = max(a[i + 1] - sub, 0)
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
suma = sum(a)
for i in range(n):
	c = min(a[i], b[i])
	a[i] -= c
	a[i + 1] -= min(a[i + 1], b[i] - c)
print(suma - sum(a))


N = int(input())
A = map(int, input().split())
B = list(map(int, input().split()))
B.append(0)
z = 0
ans = 0
for (a, b) in zip(A, B):
	ans += min(a, b + z)
	z = max(0, b - max(0, a - z))
print(ans)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
ans = 0
for i in range(N):
	p = A[i] + A[i + 1]
	if p - B[i] >= 0:
		ans += B[i]
		if A[i] - B[i] < 0:
			A[i + 1] = p - B[i]
	else:
		ans += p
		A[i + 1] = 0
print(ans)


f = lambda : map(int, input().split())
input()
(p, *A) = f()
B = f()
monster = 0
for (a, b) in zip(A, B):
	t = min(b, p + a)
	p = a - max(0, t - p)
	monster += t
print(monster)


n = int(input())
a = list(map(int, input().split()))
k = sum(a)
b = list(map(int, input().split()))
for i in range(n):
	r = b[i] - a[i]
	a[i] = max(a[i] - b[i], 0)
	if r > 0:
		a[i + 1] = max(a[i + 1] - r, 0)
print(k - sum(a))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
s = sum(A)
for i in range(N):
	m = min(A[i], B[i])
	A[i] -= m
	A[i + 1] -= min(A[i + 1], B[i] - m)
print(s - sum(A))


input()
a = map(int, input().split())
b = map(int, input().split())
i = next(a)
r = 0
for (j, k) in zip(a, b):
	s = min(i, k)
	k -= s
	t = max(min(j, k), 0)
	j -= t
	i = j
	r += s + t
print(r)


n = input()
n = int(n)
c = 0
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = 0
for i in range(n):
	kill = min(a[i], b[i] + m)
	c += kill
	m = min(b[i] + m - kill, b[i])
c += min(m, a[n])
print(c)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
sm = sum(a)
for i in range(n):
	c = min(a[i], b[i])
	a[i] -= c
	a[i + 1] -= min(a[i + 1], b[i] - c)
print(sm - sum(a))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for (i, b) in enumerate(B):
	x = min(A[i], b)
	ans += x
	b -= x
	y = min(A[i + 1], b)
	ans += y
	A[i + 1] -= y
print(ans)


(_, a, b) = open(0)
a = list(map(int, a.split()))
b = list(map(int, b.split()))
c = sum(a)
for i in range(len(b)):
	if a[i] > b[i]:
		a[i] -= b[i]
	else:
		a[i + 1] -= b[i] - a[i]
		a[i + 1] = 0 if a[i + 1] < 0 else a[i + 1]
		a[i] = 0
print(c - sum(a))


N = int(input())
An = list(map(int, input().split()))
Bn = list(map(int, input().split()))
sum = 0
for i in range(N):
	if Bn[i] <= An[i]:
		sum += Bn[i]
	elif Bn[i] <= An[i] + An[i + 1]:
		sum += Bn[i]
		An[i + 1] += An[i] - Bn[i]
	else:
		sum += An[i + 1] + An[i]
		An[i + 1] = 0
print(sum)


n = int(input())
l = list(map(int, input().split()))
d = list(map(int, input().split()))
s = 0
for i in range(n):
	if l[i] >= d[i]:
		s += d[i]
	else:
		s += min(l[i] + l[i + 1], d[i])
		l[i + 1] = max(0, l[i + 1] - (d[i] - l[i]))
print(s)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sA = sum(A)
for i in range(N):
	m = A[i]
	A[i] = max(0, A[i] - B[i])
	B[i] = B[i] - m + A[i]
	A[i + 1] = max(0, A[i + 1] - B[i])
print(sA - sum(A))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
monster = sum(a)
for i in range(n):
	a[i + 1] -= min(b[i] - min(b[i], a[i]), a[i + 1])
	a[i] -= min(b[i], a[i])
print(monster - sum(a))


def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	f = [0] * (n + 1)
	f[0] += min(a[0], b[0])
	f[1] += min(a[1], b[0] - f[0])
	a[1] -= f[1]
	for i in range(1, n):
		f[i] += min(a[i], b[i])
		f[i + 1] += min(a[i + 1], b[i] - min(a[i], b[i]))
		a[i + 1] -= f[i + 1]
	print(sum(f))
resolve()


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = sum(A)
for i in range(n):
	b = max(B[i] - A[i], 0)
	A[i] = max(A[i] - B[i], 0)
	A[i + 1] = max(A[i + 1] - b, 0)
print(total - sum(A))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
	x = min(a[i], b[i])
	a[i] -= x
	b[i] -= x
	y = min(a[i + 1], b[i])
	a[i + 1] -= y
	b[i] -= y
	cnt += x + y
print(cnt)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
res = 0
for i in range(n):
	res += min(a[i] + a[i + 1], b[i])
	if a[i] < b[i]:
		a[i + 1] -= min(a[i + 1], b[i] - a[i])
print(res)


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

def MI():
	return map(int, input().split())

def MF():
	return map(float, input().split())

def LI():
	return list(map(int, input().split()))

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	N = I()
	L = LI()
	L2 = LI()
	ans = 0
	for (index, num) in enumerate(L2):
		if L[index] >= num:
			ans += num
			L[index] -= num
		elif L[index] + L[index + 1] >= num:
			ans += num
			L[index + 1] -= num - L[index]
			L[index] = 0
		else:
			ans += L[index] + L[index + 1]
			L[index] = 0
			L[index + 1] = 0
	print(ans)

def __starting_point():
	main()
__starting_point()


N = int(input())
f = lambda : map(int, input().split())
(*A,) = f()
(*B,) = f()
cnt = 0
for i in range(len(B)):
	a = min(A[i], B[i])
	cnt += a
	a = min(A[i + 1], B[i] - a)
	cnt += a
	A[i + 1] -= a
print(cnt)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = sum(a)
for i in range(n):
	k = min(a[i], b[i])
	a[i] -= k
	b[i] -= k
	a[i + 1] -= min(a[i + 1], b[i])
print(m - sum(a))


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
ans = 0
for i in range(N):
	ans += min(B[i], A[i] + A[i + 1])
	if B[i] > A[i]:
		A[i + 1] -= B[i] - A[i]
	if A[i + 1] < 0:
		A[i + 1] = 0
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
for i in range(N):
	fir = min(A[i], B[i])
	count += fir
	sec = min(A[i + 1], max(0, B[i] - fir))
	count += sec
	A[i + 1] -= sec
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = 0
for i in range(n):
	min_ab = min(a[i], b[i])
	num += min_ab
	a[i] -= min_ab
	b[i] -= min_ab
	if b[i] != 0:
		min_ab = min(a[i + 1], b[i])
		num += min_ab
		a[i + 1] -= min_ab
		b[i] -= min_ab
print(num)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
pool = 0
target = 0
for i in range(len(A)):
	if i == len(A) - 1:
		if pool >= A[i]:
			ans += A[i]
		else:
			ans += pool
		break
	if pool >= A[i]:
		ans += A[i]
		target = 0
	else:
		ans += pool
		target = A[i] - pool
	ans += min(target, B[i])
	pool = max(0, B[i] - target)
print(ans)


N = int(input())
lsA = list(map(int, input().split()))
lsB = list(map(int, input().split()))
stA = sum(lsA)
for i in range(N):
	if lsA[i] >= lsB[i]:
		lsA[i] -= lsB[i]
		lsB[i] = 0
	else:
		lsB[i] -= lsA[i]
		lsA[i] = 0
		if lsA[i + 1] >= lsB[i]:
			lsA[i + 1] -= lsB[i]
			lsB[i] = 0
		else:
			lsA[i + 1] = 0
			lsB[i] -= lsA[i + 1]
enA = sum(lsA)
print(stA - enA)


def main():
	N = int(input())
	A = [int(i) for i in input().split()]
	B = [int(i) for i in input().split()]
	ans = 0
	for i in range(len(B) - 1, -1, -1):
		m = min(A[i + 1], B[i])
		ans += m
		A[i + 1] -= m
		B[i] -= m
		m = min(A[i], B[i])
		ans += m
		A[i] -= m
		B[i] -= m
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
(a, b) = [list(map(int, input().split())) for _ in range(2)]
(tmp, cnt) = (0, 0)
for i in range(n):
	tmp = min(a[i] - tmp, b[i])
	cnt += tmp
	tmp = min(a[i + 1], b[i] - tmp)
	cnt += tmp
print(cnt)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
count = 0
for i in range(n):
	m = min(a[i], b[i])
	b[i] -= m
	r = min(a[i + 1], b[i])
	a[i + 1] -= r
	count += m + r
print(count)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(n):
	if a[i] >= b[i]:
		cnt += b[i]
	elif a[i] < b[i] and a[i] + a[i + 1] >= b[i]:
		cnt += b[i]
		a[i + 1] = a[i + 1] - (b[i] - a[i])
	else:
		cnt += a[i] + a[i + 1]
		a[i + 1] = 0
print(cnt)


n = int(input())
monster_list = list(map(int, input().split()))
kill_mon = list(map(int, input().split()))
kill_mon.append(0)
killed_monster = 0
carry_power = 0
for (mons, kill) in zip(monster_list, kill_mon):
	if mons >= kill + carry_power:
		killed_monster += kill + carry_power
		carry_power = 0
	else:
		killed_monster += mons
		carry_power = kill - max(0, mons - carry_power)
print(killed_monster)


n = int(input())
aa = list(map(int, input().split()))
num = sum(aa)
bb = list(map(int, input().split()))
for i in range(n):
	c = min(aa[i], bb[i])
	aa[i] -= c
	aa[i + 1] -= min(aa[i + 1], bb[i] - c)
print(num - sum(aa))


n = int(input())
a_l = list(map(int, input().split()))
b_l = list(map(int, input().split()))
ans = 0
c = 0
for i in range(1, n + 1):
	a = a_l[-i]
	if b_l[-i] + c >= a:
		ans += a
		d = c - a
		if d >= 0:
			d = 0
		c = b_l[-i] - abs(d)
	else:
		ans += b_l[-i] + c
		c = 0
if a_l[0] >= c:
	ans += c
else:
	ans += a_l[0]
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ct = 0
for i in range(n):
	if a[i] >= b[i]:
		ct += b[i]
	else:
		ct += a[i]
		x = a[i + 1]
		a[i + 1] = max(a[i + 1] - b[i] + a[i], 0)
		ct += x - a[i + 1]
print(ct)


N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
ans = 0
x = 0
for i in range(N):
	(a1, a2, b) = (A[i] - x, A[i + 1], B[i])
	ans += min(b, a1 + a2)
	x = max(min(b - a1, a2), 0)
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SUM = sum(A)
for i in range(N):
	res = B[i]
	fir = min(res, A[i])
	A[i] = A[i] - fir
	res -= fir
	sec = min(res, A[i + 1])
	A[i + 1] -= sec
print(SUM - sum(A))


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
tmp = 0
for i in range(N):
	tmp = min(A[i], B[i])
	count += tmp
	tmp = min(A[i + 1], B[i] - tmp)
	A[i + 1] -= tmp
	count += tmp
print(count)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(n):
	left = min(A[i], B[i])
	ans += left
	A[i] -= left
	B[i] -= left
	right = min(A[i + 1], B[i])
	ans += right
	A[i + 1] -= right
print(ans)


from math import gcd
from itertools import combinations, permutations, accumulate, product
from collections import deque, defaultdict, Counter
import decimal
import re
import math
import bisect
import heapq
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
INF = float('inf')
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())
n = I()
A = readInts()
B = readInts()
ans = 0
for i in range(n):
	v = B[i]
	nya = min(v, A[i])
	A[i] -= nya
	v -= nya
	ans += nya
	nya = min(A[i + 1], v)
	ans += nya
	A[i + 1] -= nya
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
tmp = 0
for i in range(N):
	res += min(A[i], B[i] + tmp)
	tmp = max(B[i] - max(A[i] - tmp, 0), 0)
res += min(tmp, A[N])
print(res)


R = lambda : map(int, input().split())
input()
a = list(R())

def f():
	y = 0
	for (u, v, w) in zip(R(), a, a[1:]):
		x = min(v - y, u)
		y = min(w, u - x)
		yield (x + y)
print(sum(f()))


import os
import sys

def main():
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if A[i] >= B[i]:
			ans += B[i]
			A[i] -= B[i]
		elif A[i] < B[i] and A[i + 1] < B[i] - A[i]:
			ans += A[i + 1] + A[i]
			A[i + 1] = 0
			A[i] = 0
		elif A[i] < B[i] and A[i + 1] >= B[i] - A[i]:
			ans += B[i]
			A[i + 1] -= B[i] - A[i]
			A[i] = 0
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
s = sum(a)
for i in range(n):
	c = min(a[i], b[i])
	a[i] -= c
	b[i] -= c
	a[i + 1] = max(0, a[i + 1] - b[i])
print(s - sum(a))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = sum(a)
for i in range(n):
	if a[i] < b[i]:
		a[i + 1] = max(a[i + 1] - b[i] + a[i], 0)
		a[i] = 0
	else:
		a[i] -= b[i]
print(t - sum(a))


n = int(input())
mons = list(map(int, input().split()))
kill = list(map(int, input().split()))
cnt = 0
for i in range(n - 1, -1, -1):
	if mons[i + 1] < kill[i]:
		cnt += mons[i + 1]
		kill[i] -= mons[i + 1]
		mons[i + 1] = 0
	else:
		cnt += kill[i]
		mons[i + 1] -= kill[i]
		kill[i] = 0
	if mons[i] < kill[i]:
		cnt += mons[i]
		kill[i] -= mons[i]
		mons[i] = 0
	else:
		cnt += kill[i]
		mons[i] -= kill[i]
		kill[i] = 0
print(cnt)


LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
I = lambda : input()
ans = 0
n = int(I())
(a, b) = (LI(), LI())
for i in range(n):
	c = min(a[i], b[i])
	ans += c
	b[i] -= c
	c = min(a[i + 1], b[i])
	ans += c
	a[i + 1] -= c
print(ans)


N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
B.append(0)
ans = 0
for i in range(N + 1):
	taosukazu1 = min(A[i], B[i])
	B[i] -= taosukazu1
	A[i] -= taosukazu1
	ans += taosukazu1
	if B[i] > 0:
		taosukazu2 = min(A[i + 1], B[i])
		B[i] -= taosukazu2
		A[i + 1] -= taosukazu2
		ans += taosukazu2
print(ans)


N = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
c = sum(list_a)
for i in range(1, len(list_a)):
	temp = list_a[i] - min(list_a[i - 1] - list_b[i - 1], 0) * -1
	if list_a[i - 1] - list_b[i - 1] <= 0:
		list_a[i - 1] = 0
	else:
		list_a[i - 1] -= list_b[i - 1]
	if temp != list_a[i] and temp >= 0:
		list_a[i] = temp
	elif temp <= 0:
		list_a[i] = 0
del_c = sum(list_a)
ans = c - del_c
print(ans)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
n = 0
for i in range(N):
	if A[i] >= B[i]:
		n += B[i]
	else:
		l = min(B[i], A[i] + A[i + 1])
		n += l
		A[i + 1] += A[i] - l
print(n)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = 0
for i in range(n):
	t = min(b[i], a[i])
	d += t
	b[i] -= t
	t2 = min(a[i + 1], b[i])
	d += t2
	a[i + 1] -= t2
print(d)


(n, *a) = map(int, open(0).read().split())
c = 0
for i in range(2 * n):
	j = i * -n % len(a)
	m = min(a[j], a[j - n])
	c += m
	a[j - n] -= m
print(c)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sumA = sum(A)
for (i, b) in enumerate(B):
	c = min(A[i], b)
	A[i] -= c
	A[i + 1] -= min(A[i + 1], b - c)
print(sumA - sum(A))


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
m = sum(B)
for i in range(n):
	c = min(A[i], B[i])
	A[i] -= c
	B[i] -= c
	d = min(A[i + 1], B[i])
	A[i + 1] -= d
	B[i] -= d
m -= sum(B)
print(m)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
for i in range(n):
	if a[i] >= b[i]:
		ans += b[i]
	else:
		ans += min(b[i], a[i] + a[i + 1])
		a[i + 1] = max(0, a[i] + a[i + 1] - b[i])
print(ans)


n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split())) + [0]
(ans, tmp_b) = (0, 0)
for i in range(n + 1):
	ans += min(al[i], tmp_b + bl[i])
	tmp_b = max(0, bl[i] - max(0, al[i] - tmp_b))
print(ans)