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:
def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    aa.reverse()
    bb.reverse()
    bb.append(0)

    cnt = 0
    zan = 0
    for a, b in zip(aa, bb):
        # 前回残り勇者
        cnt += min(a, zan)
        # 今回勇者
        if a > zan:
            cnt += min(a - min(a, zan), b)
            zan = b - min(a - min(a, zan), b)
        else:
            zan = b

    print(cnt)
    
resolve()

Submission status: Accepted

Solution code:
N = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
ans = 0

for i in range(N):
  if list_a[i] <= list_b[i]:
    ans += list_a[i]
    list_b[i] -= list_a[i]
    list_a[i] = 0
    #print(list_a, list_b)
    if list_a[i+1] <= list_b[i]:
      ans += list_a[i+1]
      list_a[i+1] = 0
    else:
      ans += list_b[i]
      list_a[i+1] -= list_b[i]
  else:
    ans += list_b[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
monst = 0
C = A[N]
R = B[N-1] - C
R = max(R,0)
monst += B[N-1] - R
if N > 1:
    for i in range(N-1,0,-1):
        C = A[i]
        Q = R - C
        Q = max(Q,0)
        monst += R - Q
        P = max(0,C-R+Q)
        R = B[i-1] - P
        R = max(R,0)
        monst += B[i-1] - R
    monst += min(A[0],R)
print(monst)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
counter = 0

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

Submission status: Accepted

Solution code:
def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = 0
	rest = A[0]
	for i in range(N):
		if rest >= B[i]:
			ans += B[i]
			rest = A[i+1]
		elif (rest < B[i]) & (rest + A[i+1] >= B[i]):
			ans += B[i]
			rest = rest + A[i+1] - B[i]
		else:
			ans += (rest + A[i+1])
			rest = 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()))

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

fin = sum(a)  
print(init-fin)

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

Submission status: Accepted

Solution code:
N = int(input())

AList = list(map(int, input().split()))
BList = list(map(int, input().split()))

ans = 0

for i in range(N):
    ans += min(AList[i], BList[i])
    BList[i] = BList[i] - min(AList[i], BList[i])

    ans += min(AList[i + 1], BList[i])
    AList[i + 1] = AList[i + 1] - min(AList[i + 1], BList[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(len(a)-1):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        ans += a[i]
        ans += a[i+1] - max(a[i+1] - (b[i]-a[i]),0) 
        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()))

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

Submission status: Accepted

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

count = 0

for i in range(n+1):
    city = n - i
    if (city != n):
        defeat = min(int(a[city]), int(b[city]))
        count = count + defeat
        a[city] = str(int(a[city]) - defeat)
    if (city != 0):
        defeat = min(int(a[city]), int(b[city-1]))
        count = count + defeat
        b[city-1] = str(int(b[city-1]) - defeat)

print(count)

Submission status: Accepted

Solution code:
sentence = input()
N = int(sentence)
sentence = input()
a_str = sentence.split()
a_list = [int(a) for a in a_str]
sentence = input()
b_str = sentence.split()
b_list = [int(b) for b in b_str]
a_list.reverse()
b_list.reverse()
remain = a_list.pop(0)
total_number = 0
for i, b in enumerate(b_list):
    if remain >= b:
        total_number += b
        remain = a_list[i]
    else:
        total_number += remain
        b_remain = b - remain
        monster_a = a_list[i]
        if monster_a >= b_remain:
            total_number += b_remain
            remain = monster_a - b_remain
        else:
            total_number += monster_a
            remain = 0
print(total_number)

Submission status: Accepted

Solution code:
N = int(input().strip())
A = list(map(int, input().strip().split()))[::-1]
B = list(map(int, input().strip().split()))[::-1]

total = 0
for i in range(N):
  b = B[i]
  a = A[i]

  if a >= b:
    total += b
    continue

  total += a
  b -= a

  a = A[i + 1]
  if a >= b:
    total += b
    A[i + 1] = a - b
  else:
    total += a
    A[i + 1] = 0

print(total)

Submission status: Accepted

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

t = 0
n = 0
for i in range(N):
    if a[i] < n:
        t += a[i]
        a[i] = 0
    else:
        t += n
        a[i] -= n
    if a[i] < b[i]:
        t += a[i]
        n = b[i] - a[i]
    else:
        t += b[i]
        n = 0

if a[N] < n:
    t += a[N]
else:
    t += n

print(t)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
last = 0
cnt = 0
for ind,i in enumerate(A):
    lmon = max(i-last,0)
    if(ind<N):
        lpow = B[ind]
    else:
        lpow = 0
    cnt += min(last+lpow,i)
    last = max(lpow-lmon,0)
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
AList = list(map(int, input().split()))
BList = list(map(int, input().split()))
BList.append(0)
CList = [0] #前の勇者の余力
Answer = 0
for i in range(0,N+1):
    if BList[i]+CList[i] > AList[i]:
        CList.append(min(BList[i]+CList[i]-AList[i],BList[i]))
        Answer += AList[i]
    else:
        CList.append(0)
        Answer += BList[i]+CList[i]
print(Answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
ans = 0
tmp = 0
for i in range(N):
    if a_list[i] >= b_list[i] + tmp:
        ans += b_list[i] + tmp
        tmp = 0
    else:
        ans += a_list[i]
        if (a_list[i] - tmp) <= 0:
            tmp = b_list[i]
        else:
            tmp = b_list[i] - (a_list[i] - tmp)


if tmp == 0:
    print(ans)
else:
    if tmp >= a_list[N]:
        print(ans + a_list[N])
    else:
        print(ans + tmp)

Submission status: Accepted

Solution code:
from copy import deepcopy


n = int(input())
a_ = list(map(int, input().split()))
b = list(map(int, input().split()))

a = deepcopy(a_)
bef = 0
now = 0
ans = 0
for i in range(n):
    now = b[i]
    if bef >= a[i]:
        ans += a[i]
        bef = now
    else:
        ans += bef
        a[i] -= bef
        if now >= a[i]:
            ans += a[i]
            bef = now - a[i]
        else:
            ans += now
            bef = 0
if bef >= a[n]:
    ans += a[n]
else:
    ans += bef
ans1 = ans
a = deepcopy(a_)
a.reverse()
b.reverse()
bef = 0
now = 0
ans = 0
for i in range(n):
    now = b[i]
    if bef >= a[i]:
        ans += a[i]
        bef = now
    else:
        ans += bef
        a[i] -= bef
        if now >= a[i]:
            ans += a[i]
            bef = now - a[i]
        else:
            ans += now
            bef = 0
if bef >= a[n]:
    ans += a[n]
else:
    ans += bef
print(max(ans, ans1))

Submission status: Accepted

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

sum = 0
kuriage = 0
for i in range(n):
  if int(b[i]) + kuriage > int(a[i]):
    sum += int(a[i])
    kuriage = kuriage - int(a[i])
    if kuriage > 0:
      kuriage = 0
    kuriage += int(b[i])
    #print("amaru"+a[i])
  else:
    sum += int(b[i]) + kuriage
    #print(int(b[i]) + kuriage)
    kuriage = 0

  #print(kuriage)
    
if kuriage > 0:
  sum += min(kuriage,int(a[n]))
    
print(sum)

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

Submission status: Accepted

Solution code:
num_knight = int(input())

monsters = [int(x) for x in input().split(' ')]
knights = [int(x) for x in input().split(' ')]

initial_num_monster = sum(monsters)

for pos, power in enumerate(knights):
    if monsters[pos] <= power:
        defeated = monsters[pos]
        monsters[pos] = 0
        rest_power = power - defeated
    else:
        monsters[pos] -= power
        continue

    monsters[pos+1] = max(monsters[pos+1] - rest_power, 0)

rest_monster = sum(monsters)
print(initial_num_monster - rest_monster)
exit(0)

Submission status: Accepted

Solution code:
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
ans = 0
tmp = 0
for i in range(n):
  if tmp > lia[i]:
    ans += lia[i]
    tmp = lib[i]
  elif lia[i] <= lib[i] + tmp:
    ans += lia[i]
    tmp = lib[i] - lia[i] + tmp
  else:
    ans += lib[i] + tmp
    tmp = 0
ans += min(tmp,lia[n])
print(ans)

Submission status: Accepted

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

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):
    diff = 0
    if A[i] < B[i]:
        diff = B[i] - A[i]
        tmp = min(A[i+1], diff)
        A[i+1] = A[i+1] - tmp
        ans += A[i] + tmp
    else:
        ans += B[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]

times = 0

for i in range(N):
    if As[i] - Bs[i] > 0:
        times += Bs[i]
    else:
        if Bs[i] - As[i] < As[i+1]:
            times += Bs[i]
            As[i+1] -= Bs[i] - As[i]
        else:
            times += As[i] + As[i+1]
            As[i+1] = 0

print(times)

Submission status: Accepted

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

Submission status: Accepted

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

goukei = 0
for i in range(N):
    hidari = min(A[i], B[i])
    nokori = max(0, B[i] - A[i])
    migi = min(A[i+1], nokori)
    A[i+1] = max(0, A[i+1] - nokori)
    goukei += hidari + migi


print(goukei)

Submission status: Accepted

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

for i in range(N):
  C=A[i]-B[i]
  D=A[i+1]+A[i]-B[i]
  E=A[i+1]+A[i]
  if C>=0:
    A[i]=C
    teki=teki+B[i]
  else:
    if D>0:
      A[i]=0
      A[i+1]=D
      teki=teki+B[i]
    else:
      A[i]=0
      A[i+1]=0
      teki=teki+E

print(teki)

Submission status: Accepted

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

count = 0

for i, b in enumerate(B):
    if(A[i] > b):
        count += b
    else:
        sum_monster = A[i] + A[i+1]
        killed_monster = min(b, A[i] + A[i+1])
        count += killed_monster
        A[i+1] = sum_monster - killed_monster
        
print(count)

Submission status: Accepted

Solution code:
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):
    for j in range(i,i+2):
        tmp = b[i]
        b[i] -= a[j]        
        if b[i]<=0:
            ans += tmp
            a[j] -= tmp
            break
        else:
            ans += a[j]
            a[j] = 0
            
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0

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

print(sum)

Submission status: Accepted

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

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

print(kill)

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) :
    ai = a[i]
    a[i] = max(0, ai-b[i])
    a[i+1] = max(0, a[i+1]-max(0, b[i]-ai))

print(s-sum(a))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = 0
sum_val = 0
for i in range(N):
    if res + B[i] >= A[i]:
        sum_val += A[i]
        res = min(res + B[i] - A[i], B[i])
    else:
        sum_val += res + B[i]
        res = 0

if res >= A[N]:
    sum_val += A[N]
else:
    sum_val += res

print(sum_val)

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()))
cnt = 0
for i in range(n):
    if a[i] <= 0:
        a[i] = 0
    if b[i] > a[i]:
        check = a[i+1]+a[i]
        a[i+1] -= b[i]-a[i]
    else:
        a[i] -= b[i]
    if a[i+1] <= 0:
        cnt += check
    else:
        cnt += b[i]
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
ahoge = input().split()
bhoge = input().split()
a = []
b = []
for i in range(N):
  a.append(int(ahoge[i]))
  b.append(int(bhoge[i]))

a.append(int(ahoge[N]))
count = 0

for i in range(N-1,-1,-1):
  hoge = b[i] - a[i+1]
  if hoge > 0:
    count += a[i+1]
    huga = hoge - a[i]
    if huga > 0:
      count += a[i]
      a[i] = 0
    else:
      count += hoge
      a[i] = a[i] - hoge
  else:
    count += b[i]

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0]*(N+1)
B = [0]*N
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = int(0)
maxans = int(0)

for i in range(N+1):
    maxans += A[i]

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


for i in range(N+1):
    ans += A[i]

ans = maxans - ans

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


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
towns = list(map(int, input().split()))
yusyas = list(map(int, input().split()))

ans = 0
restmons = towns[0]
for i, mons in enumerate(yusyas):
    if restmons > mons:  # 前の勇者がとり逃したほうがキャパを超える
        ans += mons
        restmons = towns[i + 1]
    else:
        if restmons + towns[i + 1] > mons:  # 次の町は狩り残す
            ans += mons
            restmons = restmons - mons + towns[i + 1]
        else:  # 次を空にする
            ans += restmons + towns[i + 1]
            restmons = 0
    # print((i, ans, restmons))

print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
ms = map(int, raw_input().split(' '))
bs = map(int, raw_input().split(' '))
msorig = ms[:]

for i in range(len(bs)):
    if bs[i] >= ms[i]:
        ms[i+1] = max(0, ms[i+1] - (bs[i] - ms[i]))
    ms[i] = max(0, ms[i] - bs[i])

print sum(msorig) - sum(ms)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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 b[i] - a[i] >= 0:
            ans += a[i]
            b[i] -= a[i]
            a[i] = 0

            if b[i] - a[i + 1] >= 0:
                ans += a[i + 1]
                b[i] -= a[i + 1]
                a[i + 1] = 0
            else:
                ans += b[i]
                a[i + 1] -= b[i]
                b[i] = 0
        else:
            ans += b[i]
            a[i] -= b[i]
            b[i] = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
a_l = list(map(int, input().split()))
b_l = list(map(int, input().split()))

cnt = 0
for i in range(N):
    if a_l[i] <= b_l[i]:
        cnt += a_l[i]
        tmp1 = b_l[i] - a_l[i]
        if a_l[i+1] >= tmp1:
            cnt += tmp1
            a_l[i+1] -= tmp1
        else:
            cnt += a_l[i+1]
            a_l[i+1] = 0
    else:
        cnt += b_l[i]

print(cnt)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = [0] + list(map(int, input().split()))

    ans = 0
    rest = A[0]
    for i in range(1, N+1):
        a = A[i]
        b = B[i]

        ans += min(a + rest, b)
        rest = max(0, min(a + rest - b, a))

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n = int(input())
city = list(map(int, input().split()))
hero = list(map(int, input().split()))
get = 0

if city[0] <= hero[0]:
    get = get + city[0]
    hero[0] = hero[0] - city[0]
else:
    get = get + hero[0]
    hero[0] = 0


for i in range(1,n):
    if city[i] <= hero[i-1]:
        get = get + city[i]
    elif (city[i] > hero[i-1]) & (city[i] <= hero[i] + hero[i-1]):
        get = get + city[i]
        hero[i] = hero[i] - (city[i] - hero[i-1])
    else:
        get = get + (hero[i-1] + hero[i])
        hero[i] = 0

if city[n] <= hero[n-1]:
    get = get + city[n]
else:
    get = get + hero[n-1]

print(get)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

defeat_sum = 0
for i, Bn in enumerate(reversed(B)):
    i = N-i-1

    defeat_num = min(Bn, A[i+1])
    Bn -= defeat_num
    A[i+1] -= defeat_num
    defeat_sum += defeat_num

    defeat_num = min(Bn, A[i])
    Bn -= defeat_num
    A[i] -= defeat_num
    defeat_sum += defeat_num

print(defeat_sum)

Submission status: Accepted

Solution code:
N = int(input())
monster_list = list(map(int, input().split()))
Hero = list(map(int, input().split()))
ans = 0
for i, monster in enumerate(monster_list):
    if i == 0:
        h = Hero[i]
        if h >= monster:
            ans += monster
            Hero[i] -= monster
        else:
            ans += h
            Hero[i] = 0
    elif i == N:
        h = Hero[i-1]
        if h >= monster:
            ans += monster
        else:
            ans += h
    else:
        h1 = Hero[i-1]
        h2 = Hero[i]

        if h1 >= monster:
            ans += monster
        else:
            ans += h1
            monster -= h1
            if h2 >= monster:
                ans += monster
                Hero[i] -= monster
            else:
                ans += h2
                Hero[i] = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    ans = sum(a)

    a[n], b[n-1] = fight(a[n], b[n-1])

    for i in range(n-1, 0, -1):
        a[i], b[i] = fight(a[i], b[i])
        a[i], b[i-1] = fight(a[i], b[i-1])

    a[0], b[0] = fight(a[0], b[0])
    print(ans-sum(a))


def fight(m, h):
    if m <= h:
        return 0, h-m
    else:
        return m-h, 0


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sA = sum(A)
for i in range(N):
    b = B[i]
    if A[i] != 0:
        x = min(b, A[i])
        A[i] -= x
        b -= x
    x = min(b, A[i+1])
    A[i+1] -= x

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a_ls=list(map(int,input().split()))
b_ls=list(map(int,input().split()))
ans=0
for i in range(n):
    first_monster=a_ls[i]
    second_monster=a_ls[i+1]
    beats=b_ls[i]
    if first_monster>=beats:
        a_ls[i]=first_monster-beats
        ans+=beats
    elif first_monster+second_monster<beats:
        a_ls[i],a_ls[i+1]=0,0
        ans+=first_monster+second_monster
    else:
        rest=beats-first_monster
        a_ls[i]=0
        a_ls[i+1]-=rest
        ans+=beats
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

ans = 0
yoryoku = 0
for i in range(N):
    if yoryoku >= A[i]:
        ans += min(yoryoku, A[i]) # Use yoryoku
        yoryoku = B[i]
    else:
        ans += min(yoryoku + B[i], A[i]) # Use yoryoku + B[i]
        yoryoku = max(B[i] - (A[i] - yoryoku), 0)

ans += min(A[N], yoryoku)
print(ans)

Submission status: Accepted

Solution code:
import sys,heapq,math,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def setrlim():
  sys.setrecursionlimit(250000)
  soft,hard = resource.getrlimit(RLIMIT_STACK)
  # setrlimit works on ubuntu (and atcoder), but not on WSL
  #resource.setrlimit(RLIMIT_STACK, (128*1024*1024,hard))

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

Submission status: Accepted

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

co = sum(a)

temp = a[0]
ans = 0
for i in range(n):
    temp = a[i] - b[i]
    a[i] = max([0,temp])
    if a[i] == 0:
        a[i+1] = max([0,a[i+1] + temp])

print(co - sum(a))

Submission status: Accepted

Solution code:
import numpy as np

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


N=int(input())
a=acinput()
b=acinput()

SA=np.sum(a)
res=0
for i in range(N):
    #print(a,b)
    if a[i]>=b[i]:
        a[i]-=b[i]
        b[i]=0
    else:
        b[i]-=a[i]
        a[i]=0
        

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

res=0
for sa in a:
    res+=sa
print(SA-res)

Submission status: Accepted

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

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

for i in range(N):
    if A[i] >= B[i]:
        ans += 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

#if A[N] >= B[N-1]:
#    ans += B[N-1]
#else:
#    ans+= A[N]

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