Problem:


Score : 300 points

Problem Statement

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 1

2
3 5 2
4 5

Sample Output 1

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.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

def solve(N,A,B):
    ans = 0
    for i in range(N):
        battle = min(A[i],B[i])
        B[i] -= battle
        ans += battle
        nex = min(A[i+1],B[i])
        A[i+1] -= nex
        ans += nex
    return ans
print(solve(N,A,B))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

        a[i]=0

print(s-sum(a))

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    A=list(map(int,readline().split()))
    B=list(map(int,readline().split()))

    res=0
    for i in range(N):
        if A[i]>=B[i]:
            res+=B[i]
        elif A[i]<B[i]:
            res+=A[i]
            p=B[i]-A[i]
            res+=min(p,A[i+1])
            A[i+1]=max(A[i+1]-p,0)
    print(res)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(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))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
B = lr()
remain = 0
answer = 0
for i in range(N):
    attack = min(A[i], remain)
    answer += attack
    A[i] -= attack
    if A[i] > 0:
        attack = min(A[i], B[i])
        answer += attack
        B[i] -= attack
    remain = B[i]

answer += min(A[N], remain)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    Ai = sum(A)
    B = list(map(int, input().split()))
    for i in range(n):
        b = B[i]
        if b > A[i]:
            b -= A[i]
            A[i] = 0
            if b > A[i + 1]:
                b -= A[i + 1]
                A[i + 1] = 0
            else:
                A[i + 1] -= b
        else:
            A[i] -= b
    Af = sum(A)
    print(Ai - Af)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
res = 0

for i in range(N):
    if b[i] >= a[i]:
        res += a[i]
        b[i] -= a[i]
        if b[i] >= a[i+1]:
            res += a[i+1]
            a[i+1] = 0
        else:
            tmp = b[i]
            res += tmp
            a[i+1] -= tmp
    else:
        res += b[i]
print(res)

Submission status: Accepted

Solution code:
def resolve():
  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]
    else:
      ans += a[i]
      b[i] -= a[i]
      if a[i+1] >= b[i]:
        ans += b[i]
        a[i+1] -= b[i]
      else:
        ans += a[i+1]
        a[i+1] = 0
  print(ans)
  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = 0
for i in range(N):
    a = A[i]
    if a >= B[i]:
        ans += B[i]
        a -= B[i]
    else:
        ans += a
        a = 0
    if a >= B[i + 1]:
        ans += B[i + 1]
        B[i + 1] = 0
    else:
        ans += a
        B[i + 1] -= a

print (ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
for i in range(N):
  if b[i] >= a[i]:
    b[i] -= a[i]
    res += a[i]
  else:
    res += b[i]
    b[i] = 0
  res += min(a[i + 1], b[i])
  a[i + 1] = max(0, a[i + 1] - b[i])
print(res)

Submission status: Accepted

Solution code:
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(N):
        if A[i] <= B[i]:
            B[i] -= A[i]
            ans += A[i]
            A[i] = 0
            if A[i+1] <= B[i]:
                B[i] -= A[i+1]
                ans += A[i+1]
                A[i+1] = 0
            else:
                A[i+1] -= B[i]
                ans += B[i]
                B[i] = 0
        else:
            A[i] -= B[i]
            ans += B[i]
            B[i] = 0
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
res=0

for i in range(n):
	if B[i]>A[i]:
		res+=A[i]
		if (B[i]-A[i])>A[i+1]:
			res+=A[i+1]
			A[i+1]=0
		else:
			res+=B[i]-A[i]
			A[i+1]-=(B[i]-A[i])
	else:
		res+=B[i]
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]:
    B[i] -= A[i]
    A[i] = 0
    
    if B[i] >= A[i+1]:
      B[i] -= A[i+1]
      A[i+1] = 0
      
    else:
      A[i+1] -= B[i]
      B[i] = 0
      
  else:
    A[i] -= B[i]
    B[i] = 0

print(ans-sum(A))

Submission status: Accepted

Solution code:
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])
    
    if B[i] > A[i]:
        x = B[i] - A[i]
        ans += min(A[i+1],x)
        A[i+1] = max(0,A[i+1]-x)
        
print(ans)

Submission status: Accepted

Solution code:
ma = lambda : map(int,input().split())
n = int(input())
A = list(ma())
B = list(ma())
ans = 0
tmp = 0
for i in range(n):
    for j in range(2):
        tmp = B[i]
        ans += min(B[i],A[i+j])
        B[i] =max(B[i]-A[i+j],0)
        A[i+j] = max(0,A[i+j]-tmp)
print(ans)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
monsters = sum(A)

for i in range(N):
  if B[N-1-i] > A[N-i]:
    B[N-1-i] -= A[N-i]
    A[N-i] = 0
  else:
    A[N-i] -= B[N-1-i]
    B[N-1-i] = 0
  if B[N-1-i] > A[N-1-i]:
    B[N-1-i] -= A[N-1-i]
    A[N-1-i] = 0
  else:
    A[N-1-i] -= B[N-1-i]
    B[N-1-i] = 0
print(monsters -sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A =list(map(int,input().split()))
B =list(map(int,input().split()))
res = 0
ans = 0 
for i in range(n):
    if A[i]-B[i]>=0:
        ans += B[i]
        res = 0
    else:
        ans += A[i]
        res =abs (A[i]-B[i])
    if res != 0:
        if A[i+1]-res>0:
            ans += res
            A[i+1] = A[1+i]-res 
            res = 0
        else:
            ans += A[i+1]
            A[i+1] =0
            res =0

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

num = 0

for i, b in enumerate(b):
    if b > a[i]:
        b -= a[i]
        num += a[i]
        if b > a[i+1]:
            num += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= b
            num += b
    else:
        num += b

print(num)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
cnt = 0
tmp = 0
tmp2 = 0

for i in range(N):
    if A[i] >= B[i]:
        cnt += B[i]
    else:
        tmp = B[i] - A[i]
        cnt += A[i]
        if A[i+1] >= tmp:
            cnt += tmp
            A[i+1] = A[i+1] - tmp
        else:
            cnt += A[i+1]
            A[i+1] = 0

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

ans = 0

for i in range(n):
    b = B[i]
    if A[i] >= b:
        ans += b
    elif A[i] < b:
        ans += A[i]
        b -= A[i]
        if A[i+1] >= b:
            ans += b
            A[i+1] -= b
        elif A[i+1] < b:
            ans += A[i+1]
            A[i+1] = 0 

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

ans = 0

for i in range(n):
    b = B[i]
    if A[i] >= b:
        ans += b
    elif A[i] < b:
        ans += A[i]
        b -= A[i]
        if A[i+1] >= b:
            ans += b
            A[i+1] -= b
        elif A[i+1] < b:
            ans += A[i+1]
            A[i+1] = 0 

print(ans)

Submission status: Accepted

Solution code:
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]:
        m = min(a[i + 1], b[i] - a[i])
        ans += a[i] + m
        a[i + 1] -= m
    else:
        ans += b[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=sum(a)

for i in range(n):
  a[i],a[i+1]=max(0,a[i]-b[i]),max(a[i+1]+min(0,a[i]-b[i]),0)
  
  
print(c-sum(a))

Submission status: Accepted

Solution code:
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])
    B[i] -= left
    ans += left
    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    ans += right
 
print(ans)

Submission status: Accepted

Solution code:
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])
    B[i] -= left
    ans += left
    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    B[i] -= right
    ans += right
 
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = sum(A)

for i in range(N):
    
    if A[i] >= B[i]:
        A[i] -= B[i]
    
    else:
        A[i + 1] -= abs(A[i] - B[i])
        A[i + 1] *= int(A[i + 1] > 0)
        A[i] = 0 

print(ans - sum(A))

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
# map(int, input().split())
# int(input())
# input().split()
# mojiretu = ','.join(str_list)
# sorted(input())

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]
    else:
        ans += a[i]
        b[i] -= a[i]
        if a[i+1] > b[i]:
            ans += b[i]
            a[i+1] -= b[i]
        else:
            ans += a[i+1]
            a[i+1] = 0
print(ans)

Submission status: Accepted

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

print(sum_a - sum(a))

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    ori = sum(a)

    for i in range(n):
        bt = b[i]
        if a[i] < bt:

            a[i+1] = max(a[i+1]-(bt-a[i]), 0)
            a[i] = 0
        else:
            a[i] -= bt
    end = sum(a)
    print(ori-end)
resolve()

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	arr = list(map(int,input().split()))
	arr2 = list(map(int,input().split()))
	cnt = 0
	if arr2[0]>arr[0]:
		cnt+=arr[0]
		arr2[0]-=arr[0]
	else:
		cnt+=arr2[0]
		arr2[0]=0
	for i in range(1,len(arr)-1):
		if arr[i]<(arr2[i]+arr2[i-1]):
			cnt+=arr[i]
			arr[i]-=arr2[i-1]
			if arr[i]>0:
				arr2[i]-=arr[i]
		else:
			cnt+=(arr2[i]+arr2[i-1])
			arr2[i]=0
	if arr2[-1]>arr[-1]:
		cnt+=arr[-1]
		arr2[-1]-=arr[-1]
	else:
		cnt+=arr2[-1]
		arr2[-1]=0
	print(cnt)

main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0

for i in range(len(B)):
    if (A[i] - B[i] == 0) or  (A[i] - B[i] > 0):
        ans = ans + (B[i])
    elif (A[i] - B[i] < 0):
        if (A[i] + A[i+1] <= B[i]):
            ans = ans + (A[i] + A[i+1])
            A[i] = 0
            A[i+1] = 0 
        else:
            RestHP = B[i] - A[i]
            A[i+1] = max(0,(A[i+1] - RestHP))
            ans = ans + B[i]
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]

    cnt = 0
    for i in range(N):
        if B[i] < A[i]:
            cnt += B[i]
            A[i] -= B[i]

        elif A[i] <= B[i] <= A[i] + A[i+1]:
            cnt += B[i]
            A[i+1] -= B[i] - A[i]
            A[i] = 0

        elif A[i] + A[i+1] < B[i]:
            cnt += A[i] + A[i+1]
            A[i+1] = 0
            A[i] = 0

    print(cnt)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, A, B = int(input()), list(map(int, input().split())), list(map(int, input().split()))
result = 0
for i in range(N):
    a, a_next, b = A[i], A[i+1], B[i]
    if a >= b: result += b
    elif a + a_next < b:
        result += a + a_next
        A[i+1] = 0
    else:
        A[i+1] -= b - a
        result += b
print(result)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

s = str(sum(a))

for i in range(n):
    if a[i] >= b[i]:
        a[i] -= b[i]
    elif a[i] < b[i]:
        if a[i] + a[i+1] <= b[i]:
            a[i] = 0
            a[i+1] = 0
        elif a[i] + a[i+1] > b[i]: 
            a[i+1] -= (b[i]-a[i])
            a[i] = 0

print(int(s)-sum(a))

Submission status: Accepted

Solution code:
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]:
    b[i] -= a[i]
    ans += a[i]
    if a[i+1] < b[i]:
      ans += a[i+1]
      a[i+1] = 0
    else:
      ans += b[i]
      a[i+1] -= b[i]
  else:
    ans += b[i]
    
print(ans)

Submission status: Accepted

Solution code:
# Problem C - City Savers

# input
N = int(input())
a_nums = list(map(int, input().split()))
b_nums = list(map(int, input().split()))

# initialization
ans_monsters = 0

# count
for i in range(len(b_nums)):
    # round 1
    if b_nums[i]>=a_nums[i]:
        ans_monsters += a_nums[i]
        b_nums[i] -= a_nums[i]
    else:
        ans_monsters += b_nums[i]
        b_nums[i] = 0

    # round 2
    if b_nums[i]>0:
        if b_nums[i]>=a_nums[i+1]:
            ans_monsters += a_nums[i+1]
            a_nums[i+1] = 0
        else:
            ans_monsters += b_nums[i]
            a_nums[i+1] -= b_nums[i]

# output
print(ans_monsters)

Submission status: Accepted

Solution code:
n = int(input())
AL = list(map(int, input().split()))
BL = list(map(int, input().split()))
ans = 0
for i in range(n):
    if BL[i] <= AL[i]:
        ans += BL[i]
    elif BL[i] > AL[i] and BL[i] <= AL[i]+AL[i+1]:
        ans += BL[i]
        AL[i+1] = AL[i+1] - BL[i] + AL[i]
    else:
        ans += AL[i]+AL[i+1]
        AL[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
ans = 0
for itr, b in enumerate(B):
  rem = b
  for i in range(2):
    dam = min(rem, A[itr + i])
    A[itr + i] -= dam
    ans += dam
    rem -= dam
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(N):
        if A[i] >= B[i]:
            ans += B[i]
            A[i] -= B[i]
            B[i] = 0
        else:
            ans += A[i]
            B[i] -= A[i]
            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)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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(N)[::-1]:
        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
            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
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#create date: 2020-04-26 08:06

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    b = na()
    ans = 0
    for i in range(n):
        res = min(b[i], a[i])
        b[i] -= res
        ans += res
        res = min(a[i+1], b[i])
        a[i+1] -= res
        ans += res
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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 += min(b[i], a[i]+a[i+1])
        a[i+1] -= min(a[i+1], b[i]-a[i])
    else:
        ans += b[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    cnt=0
    for i in range(N):
        val=min(A[i],B[i])
        A[i]-=val
        B[i]-=val
        cnt+=val

        val=min(A[i+1],B[i])
        A[i+1]-=val
        cnt+=val

    print(cnt)




resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A.reverse()
B.reverse()

ans = 0
for i in range(n):
    #print(ans)
    if A[i] <= B[i]:
        ans += A[i]
        B[i] -= A[i]
        if A[i+1] >= B[i]:
            ans += B[i]
            A[i+1] -= B[i]
            B[i] = 0
        else:
            ans += A[i+1]
            A[i+1] = 0
    else:
        ans += B[i]
        B[i] = 0
        A[i] -= B[i]
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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 += A[i]
        B[i] -= A[i]
        A[i] = 0
        if A[i + 1] <= B[i]:
            ans += A[i + 1]
            B[i] -= A[i + 1]
            A[i + 1] = 0
        else:
            ans += B[i]
            A[i + 1] -= B[i]
    else:
        ans += B[i]
        B[i] = 0
        A[i] -= B[i]

print (ans)

Submission status: Accepted

Solution code:
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])
    A[i] -= left
    B[i] -= left
    ans += left
    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    B[i] -= right
    ans += right

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))
ans1 = sum(list_a)
dum1 = 0
for i in range(n-1,-1,-1):
    dum1 = list_a[i+1] - list_b[i]
    list_a[i+1] = dum1
    if list_a[i+1] < 0:
        list_a[i+1] = 0
    if dum1 < 0:
        list_a[i] = list_a[i] + dum1
        if list_a[i] < 0:
            list_a[i] = 0
ans2 = sum(list_a)
#print(list_a)
print(ans1-ans2)

Submission status: Accepted

Solution code:
n=int(input())
a =list(map(int,input().split()))
b =list(map(int,input().split()))

res=0
for i in range(n):

    if b[i] >= a[i]:
        res+=a[i]
        t = b[i] - a[i]
        res+=min(t,a[i+1])
        a[i+1] = max(0,a[i+1] - t)

    else:
        res+=b[i]

print(res)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

count=0
for i in range(0,N):
  y=B[i]-A[i]
  if y>=0:
    count+=A[i]
    z=y-A[i+1]
    if z>=0:
      count+=A[i+1]
      A[i+1]=0
    else:
      count+=y
      A[i+1]-=y
  else:
    p=A[i]-B[i]
    count+=B[i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0

for i in range(n):
	if b[i] > a[i]:
		c += a[i]
		b[i] -= a[i]
		if b[i] >= a[i+1]:
			c += a[i+1]
			a[i+1] = 0
		else:
			c += b[i]
			a[i+1] -= b[i]
	else:
		c += b[i]
print (c)

Submission status: Accepted

Solution code:
def elip():
    return map(int,input().split())
N = int(input())
A = list(elip())
B = list(elip())
kill = 0
for i in range(len(B)):
    if A[i] > 0 and B[i] >= A[i]:
        B[i] -= A[i]
        kill += A[i]
        A[i] = 0
    else:
        if A[i] > 0:
            A[i] -= B[i]
            kill += B[i]
            B[i] = 0
            continue
    if A[i+1] > 0 and B[i] >= A[i+1]:
        B[i] -= A[i+1]
        kill += A[i+1]
        A[i+1] = 0
    else:
        if A[i+1] > 0:
            A[i+1] -= B[i]
            kill += B[i]
            B[i] = 0
print(kill)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
c=sum(a)

for i in range(N):
    a[i]-=b[i]

    if  a[i]<=0:
        a[i+1]+=a[i]
        if a[i+1]<=0:
            a[i+1]=0
        a[i]=0

print(c-sum(a))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
    x = min(a[i], b[i])
    ans += x
    a[i] -= x
    b[i] -= x

    ans += min(b[i], a[i + 1])
    a[i + 1] = max(0, a[i + 1] - b[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

killed = 0

for i in range(n):
    now_kill = min(a[i], b[i])
    killed += now_kill
    #a[i] -= now_kill
    b[i] -= now_kill
    if b[i] == 0:continue
    now_kill = min(a[i+1], b[i])
    killed += now_kill
    a[i+1] -= now_kill
    #b[i] -= now_kill

print(killed)

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(x) for x in input().split()]
b_list = [int(x) for x in input().split()]

ans = 0
for i in range(n):
    for j in range(2):
        temp = min(a_list[i + j], b_list[i])
        ans += temp
        a_list[i + j] -= temp
        b_list[i] -= temp
print(ans)

Submission status: Accepted

Solution code:
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 += min(A[i]+A[i+1],B[i])
    A[i+1] = max(0,A[i+1] - (B[i] -A[i]))
    A[i] = 0
  else:
    ans += B[i]
    A[i] -= B[i]
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

r = 0
result = 0

for i in range(n):
    if a[i] < r + b[i]:
        result += a[i]
        r = min(b[i] + r - a[i], b[i])
    else:
        result += r + b[i]
        r = 0

if a[n] < r:
    result += a[n]
else:
    result += r
        
print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
death = 0
tmp = 0
for i,x in enumerate(b):
    if i == 0:
        death += min(a[i]+a[i+1],b[i])
        if (a[i]+a[i+1])<=b[i]:
            tmp = 0
        else:
            if a[i]>=b[i]:
                tmp = a[i+1]
            else:
                tmp = a[i]+a[i+1]-b[i]
    else:
        death += min(tmp+a[i+1],b[i])
        if (tmp+a[i+1])<=b[i]:
            tmp = 0
        else:
            if tmp>=b[i]:
                tmp = a[i+1]
            else:
                tmp = tmp+a[i+1]-b[i]
    
print(death)

Submission status: Accepted

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