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:
import glob


# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = 'ABC\\135\\C'

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'


class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class C(Common):

    def resolve(self):

        N = int(self.input_data())
        monster = [int(i) for i in self.input_data().split()]
        saver = [int(i) for i in self.input_data().split()]

        result = 0
        remaining = 0

        for i in range(N):
            if saver[i] + remaining > monster[i]:
                result += monster[i]
                remaining = saver[i] + remaining - monster[i]
                if remaining > saver[i]:
                    remaining = saver[i]
            else:
                result += saver[i] + remaining
                remaining = 0

        if remaining > monster[N]:
            result += monster[N]
        else:
            result += remaining

        print(str(result))


solver = C(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

Solution code:
#a,b=map(int,input().split())
n=int(input())
#m=int(input())
#r=[input() for _ in range(n)]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
total=sum(a)
for i in range(n):
    if a[i]<=b[i]:
        diff=b[i]-a[i]
        a[i]=0
        if a[i+1]<diff:
            a[i+1]=0
        else:
            a[i+1]-=diff
    else:
        a[i]-=b[i]
print(total-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]:
        if B[i] >= A[i+1] + A[i]:
            ans += A[i+1] + A[i]
            #print(ans,i)
            A[i+1] = 0
        else:
            ans += B[i]
            A[i+1] = A[i+1] - B[i] + A[i]
            #print(ans,i)
    else:
        ans += B[i]
        #print(ans,i)

print(ans)

Submission status: Accepted

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

ko = 0

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

Submission status: Accepted

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

sum_monster = 0
add = 0

def trp(l, n):
    return l[:n] + [0]*(n-len(l))
B = trp(B, len(A))

for (mons, hero) in zip(A, B):
  hero_added = hero + add
  if mons >= hero_added:
    sum_monster += hero_added
    add = 0
  else:
    sum_monster += mons
    if (hero_added - mons) >= hero:
      add = hero
    else:
      add = (hero_added - mons)
print(sum_monster)

Submission status: Accepted

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

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

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()))
    total = 0

    for i in list(reversed(range(1,n+1))):
        #一歩先を倒す処理
        if a[i] > b[i-1]:
            total += b[i-1]
            a[i] = a[i] - b[i-1]
            b[i-1] = 0
        else:
            total += a[i]
            b[i-1] = b[i-1] - a[i]
            a[i] = 0

        #自分の街を助ける処理
        if a[i-1] > b[i-1]:
            total += b[i-1]
            a[i-1] = a[i-1] - b[i-1]
            b[i-1] = 0
        else:
            total += a[i-1]
            b[i-1] = b[i-1] -a[i-1]
            a[i-1] = 0

    print(total)


if __name__ == '__main__' :
    main()

Submission status: Accepted

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

A_survive = list(reversed(A))
B_avail = list(reversed(B))
ans = 0

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

#print(*A_survive)
#print(*B_avail)
print(ans)

Submission status: Accepted

Solution code:
sin = input()
n = int(sin)
sin = input()
mons = list(map(int, sin.split()))
sin = input()
yuus = list(map(int, sin.split()))
yuus = [0] + yuus

tkill = 0
rest = 0
for mi, yi in zip(reversed(mons), reversed(yuus)):
    tkill += min(mi, yi + rest)
    rest = yi + rest - mi
    if rest < 0:
        rest = 0
    elif rest > yi:
        rest = yi

print(tkill)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lstA = list(map(int,input().split()))
lstB = list(map(int,input().split()))

ans = 0
for i in range(n):
    if lstB[i] < lstA[i]:
        ans += lstB[i]
    elif lstB[i] > (lstA[i] + lstA[i+1]):
        ans += lstA[i] + lstA[i+1]
        lstA[i+1] = 0
    else:
        ans += lstB[i]
        lstA[i+1] -= lstB[i] - lstA[i]
print(ans)

Submission status: Accepted

Solution code:

n = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split())) + [0]

count = 0
before = 0
for i in range(n+1):
    if As[i] >= before:
        count += before
        As[i] -= before
    else:
        count += As[i]
        As[i] = 0


    if As[i] > 0:
        if As[i] < Bs[i]:
            count += As[i]
            before = Bs[i] - As[i]
        else:
            before = 0
            count += Bs[i]
    else:
        before = Bs[i]

print(count)

Submission status: Accepted

Solution code:

n = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

count = 0
before = 0
for i in range(n+1):
    if As[i] >= before:
        count += before
        As[i] -= before
    else:
        count += As[i]
        As[i] = 0

    if i >= n:
        break

    if As[i] > 0:
        if As[i] < Bs[i]:
            count += As[i]
            before = Bs[i] - As[i]
        else:
            before = 0
            count += Bs[i]
    else:
        before = Bs[i]

print(count)

Submission status: Accepted

Solution code:
def C_CitySavers():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    for x in range(N):
        if A[x] >= B[x]:
            ans += B[x]
        else:
            over = B[x] - A[x]
            if A[x+1] < over:
                over = A[x + 1]
            A[x+1] -= over
            ans += A[x] + over
    print(ans)
    
if __name__ == "__main__":
    C_CitySavers()

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
a = [int(i) for i in a]
b = input().split()
b = [int(i) for i in b]

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

print(sum)

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


print(s-sum(a))

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    cs = list(map(int, input().split()))
    bs = list(map(int, input().split()))
    count = 0

    for i in range(n):
        if cs[i] >= bs[i]:
            count += bs[i]
        else:
            next_defeat = min(cs[i+1], bs[i] - cs[i])
            cs[i+1] = cs[i+1] - next_defeat
            count += cs[i]
            count += next_defeat

    print(count)
resolve()

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split(" ")))
Bs = list(map(int, input().split(" ")))


res = 0
tmp = 0

for i in range(N):
	if As[i] >= (Bs[i] + tmp):
		res += Bs[i] + tmp
		tmp = 0
	else:
		res += As[i]		
		tmp = Bs[i] + min(0, tmp - As[i])

res += tmp if (As[N] - tmp) > 0 else As[N]

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

I = A[0]
ans = 0

for i in range(N):
    if B[i] <= I:
        ans += B[i]
        I = A[i + 1]
    elif I + A[i + 1] >= B[i] > I:
        ans += B[i]
        I = I + A[i + 1] - B[i]
    elif B[i] > I + A[i + 1]:
        ans += I + A[i + 1]
        I = 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):
  ans+=min(A[i]+A[i+1],B[i])
  A[i+1]=max(0,min(A[i]+A[i+1]-B[i],A[i+1]))

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

for num in range(N):
    Ans += min(A[num], B[num])
    if A[num] >= B[num]:
        pass
    else: # A[num] < B[num]
        B[num] -= A[num]
        Ans += min(A[num+1], B[num])
        if A[num+1] >= B[num]:
            A[num+1] -= B[num]
        else:
            A[num+1] = 0
print(Ans)

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

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    monsters = list(map(int, input().split(" ")))
    peoples = list(map(int, input().split(" ")))

    score = 0
    kurikoshi = 0
    for i in range(n):
        if monsters[i] > peoples[i] + kurikoshi:
            score += peoples[i] + kurikoshi
            kurikoshi = 0
        elif monsters[i] <= peoples[i] + kurikoshi:
            score += monsters[i]
            if monsters[i] < kurikoshi:
                kurikoshi = peoples[i]
            else:
                kurikoshi = peoples[i] + (kurikoshi - monsters[i])

    if monsters[n] > kurikoshi:
        score += kurikoshi
    else:
        score += monsters[n]

    print(score)
    
main()

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

ret = 0
for idx, b in enumerate(Bs):
    if As[idx] >= b:
        ret += b
    else:
        ret += As[idx]
        b -= As[idx]
        if As[idx + 1] >= b:
            ret += b
            As[idx + 1] -= b
        else:
            ret += As[idx + 1] 
            As[idx + 1] = 0
print(ret)

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 += B[i]
  elif A[i] + A[i+1] <= B[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())
enemy_hp = [int(i) for i in input().split()]
hero_str = [int(i) for i in input().split()]
enemy_hp_sum = sum(enemy_hp)

def than_zero(x):
  if x < 0:
    y = 0
  else:
    y = x
  return y

for i in range(n):
  temp_hp = enemy_hp[i]
  enemy_hp[i] = than_zero(enemy_hp[i] - hero_str[i])
  hero_str[i] = than_zero(hero_str[i] - temp_hp)
  enemy_hp[i+1] = than_zero(enemy_hp[i+1] - hero_str[i])

print(enemy_hp_sum - sum(enemy_hp))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def remove_monster(a_rest, a, b_rest, b):
    if a_rest <= b_rest:
        removed = a_rest
        a_rest = 0
    else:
        removed = b_rest
        a_rest -= b_rest

    if a_rest <= b:
        removed += a_rest
        b_rest = b - a_rest
    else:
        removed += b
        b_rest = 0

    if a <= b_rest:
        removed += a
        b_rest -= a
        a_rest = 0
    else:
        removed += b_rest
        a_rest = a - b_rest
        b_rest = 0

    return removed, a_rest, b_rest

N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
    
a_rest = A[0]
b_rest = 0
all_removed = 0
for n in range(N):
    removed, a_rest, b_rest = \
        remove_monster(a_rest, A[n + 1], b_rest, B[n])
    # print(f'({n}):', removed, a_rest, b_rest)
    all_removed += removed

print(all_removed)

Submission status: Accepted

Solution code:
n = int(input())
li_a = list(map(int,input().split()))
li_b = list(map(int,input().split()))
ans = 0
for i in range(n):
  if li_a[i] < li_b[i]:
    if li_b[i] >= li_a[i] + li_a[i+1]:
      ans += li_a[i] + li_a[i+1]
      li_a[i+1] = 0
    else:
      ans += li_b[i]
      li_a[i+1] = li_a[i+1] - (li_b[i]-li_a[i])
  elif li_a[i] == li_b[i]:
    ans += li_a[i]
  elif li_a[i] > li_b[i]:
    ans += li_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):
  if a[i]>=b[i]:
    ans+=b[i]
  else:
    ans+=a[i]
    ans+=min(b[i]-a[i],a[i+1])
    a[i+1]=max(0,a[i+1]-b[i]+a[i])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
enemy = list(map(int, input().split()))
heroes = list(map(int, input().split()))
ans = 0
for i in range(N):
  diff = min(enemy[i], heroes[i])
  enemy[i] -= diff
  heroes[i] -= diff
  ans += diff

  diff = min(enemy[i + 1], heroes[i])
  enemy[i + 1] -= diff
  heroes[i] -= diff
  ans += diff

print(ans)

Submission status: Accepted

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

ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
mons = list(map(int,input().split()))
hero = list(map(int,input().split()))

kills=0
mons.insert(0,0)
hero.insert(0,0)
hero.append(0)

#print(mons)
#print(hero)

for i in range(1,n+2):
  if mons[i]>=hero[i-1]:
    kills+=hero[i-1]
    mons[i]-=hero[i-1]
  else:
    kills+=mons[i]
    mons[i]=0
  #print(mons)
  #print(hero)
  #print("kills="+str(kills))
  if mons[i]>=hero[i]:
    kills+=hero[i]
    mons[i]-=hero[i]
    hero[i]=0
  else:
    hero[i]-=mons[i]
    kills+=mons[i]
  #print(mons)
  #print(hero)
  #print("kills="+str(kills))
print(kills)

Submission status: Accepted

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

    d = min(b[i], a[i+1])
    b[i] -= d
    a[i+1] -= d
    total += d
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def p_c():
    N = int(input())
    A = list(map(int, input().split()))  # N+1の町にモンスターAi体
    B = list(map(int, input().split()))

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    p_c()

Submission status: Accepted

Solution code:
n = int(input())
# the num of monster in the cith A[i]
a = list(map(int, input().split()))
# the num of monster the hero can defeat
b = list(map(int, input().split()))


b_rest = [max(b[0]-a[0],0)]
a_rest = [max(a[0]-b[0],0)]


for i in range(n-1):
  b_rest.append( min( b[i+1], max( ((b[i+1]+b_rest[i])-a[i+1]), 0 ) ) )
  a_rest.append( max( a[i+1]-(b[i+1]+b_rest[i]), 0 ) ) 

a_rest.append(max(0, (a[n]-b_rest[-1]) ) )
ans = sum(a) - sum(a_rest)
                                
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 B[i] >= (A[i] + A[i+1]):
            ans += (A[i] + A[i+1])
            A[i+1] = 0
        
        elif B[i] < (A[i] + A[i+1]):
            ans += B[i]
            A[i+1] = (A[i] + A[i+1]) - B[i]
print(ans)

Submission status: Accepted

Solution code:
#32 ABC135C
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0)
res=0

if a[0]>=b[0]:
	res+=b[0]
	a[0]-=b[0]
	b[0]=0
else:
	res+=a[0]
	b[0]-=a[0]
	a[0]=0

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

Submission status: Accepted

Solution code:
n=int(input())
mons=list(map(int,input().split()))
sold=list(map(int,input().split()))

a=0
for i in range(n):
    if mons[i]<=sold[i]:
        sold[i]-=mons[i]
        a+=mons[i]
        mons[i]=0
        if mons[i+1]<=sold[i]:
            a+=mons[i+1]
            mons[i+1]=0
        else:
            a+=sold[i]
            mons[i+1]-=sold[i]
    else:
        a+=sold[i]

print(a)

Submission status: Accepted

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

Submission status: Accepted

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

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 reversed(range(0,n)):
    if b[i]<=a[i+1]:
        sum+=b[i]
    elif b[i]<=a[i+1]+a[i]:
        sum+=b[i]
        a[i]-=b[i]-a[i+1]
    elif a[i+1]+a[i]<=b[i]:
        sum+=a[i+1]+a[i]
        a[i]=0

print(sum)

Submission status: Accepted

Solution code:
import itertools

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

n=getint()
a=getints()
b=getints()

res = 0

for i in range(n):
    kill = min(a[i],b[i])
    res += kill
    can = b[i]-kill
    nxt_kill = min(a[i+1], can)
    res += nxt_kill
    a[i+1] -= nxt_kill

print(res)

Submission status: Accepted

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

print(s-sum(A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
MONSTER = sum(A)
for i in range(N):
    if A[i] > B[i]:
        A[i] -= B[i]
    else:
        USED = A[i]
        A[i] = 0
        A[i+1] = max(0, A[i+1] - (B[i] - USED))

ans = MONSTER - sum(A)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
An1 = input().split(' ')
Bn = input().split(' ')

next_add = 0
attacked = 0
for i in range(N):
    me = int(Bn[i])
    enemy = int(An1[i])
    if next_add > 0:
        diff1 = next_add - enemy
        if diff1 < 0:
            attacked = attacked + next_add
            enemy = enemy -next_add
            next_add = 0
        else:
            attacked = attacked + enemy
            next_add = me
            continue
        
    diff2 = me - enemy
    if diff2 < 0:
        attacked = attacked + me
        next_add = 0
    else:
        attacked = attacked + enemy
        next_add = diff2

if next_add > 0:
    enemy = int(An1[N])
    diff1 = next_add - enemy
    if diff1 < 0:
        attacked = attacked + next_add
    else:
        attacked = attacked + enemy
print(attacked)

Submission status: Accepted

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

cnt = 0
for ind in range(N):
    n_kill = min(a_ls[ind], b_ls[ind])
    cnt += n_kill
    if n_kill < b_ls[ind]:
        if a_ls[ind + 1] <= b_ls[ind] - n_kill:
            cnt += a_ls[ind + 1]
            a_ls[ind + 1] = 0
        else:
            a_ls[ind + 1] -= b_ls[ind] - n_kill
            cnt += b_ls[ind] - n_kill

print(cnt)

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

ans = 0

if A_list[0] > B_list[0]:
  ans += B_list[0] 
  A_list[0] -= B_list[0]
  B_list[0] = 0

if A_list[N] > B_list[N-1]:
  ans += B_list[N-1] 
  A_list[N] -= B_list[N-1] 
  B_list[N-1] = 0
  
for i in range(1, N):    
  if A_list[i] > B_list[i-1] + B_list[i]:
    ans += B_list[i-1] + B_list[i]
    A_list[i] -= B_list[i-1] + B_list[i]
    B_list[i-1] = 0
    B_list[i] = 0

#####    
if A_list[0] > B_list[0]:
  ans += B_list[0]
  B_list[0] = 0
else:
  ans += A_list[0]
  B_list[0] -= A_list[0]
  

for i in range(1, N):
  if A_list[i] > B_list[i-1] + B_list[i]:
    ans += B_list[i-1] + B_list[i]
    B_list[i-1] = 0
    B_list[i] = 0
  else:
    ans +=  A_list[i]
    if A_list[i] > B_list[i-1]:
      B_list[i] = B_list[i]  - (A_list[i] - B_list[i-1])

if A_list[N] > B_list[N-1]:
  ans += B_list[N-1]
else:
  ans += A_list[N]

print(ans)

Submission status: Accepted

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

  else:
    A[i] = A[i] - Y[i]
    Y[i] = 0

print(A_sum_1 - sum(A))

Submission status: Accepted

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

destoryed_monsters = 0
for n in range(N):
    c1 = min(A[n], B[n])
    c2 = min(A[n+1], max(0, B[n]-A[n]))
    A[n+1] -= c2
    destoryed_monsters += (c1+c2)
print(destoryed_monsters)

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):
    left = min(a[i], b[i])
    count += left
    a[i] -= left
    b[i] -= left

    right = min(a[i+1], b[i])
    count += right
    a[i+1] -= right
    b[i] -= right
    
print(count)

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,braver in enumerate(B):
    result = A[i] - braver
    count += A[i] if result <= 0 else braver
    braver = braver - A[i]
    if braver > 0:
        nresult = A[i+1] - braver
        count += A[i+1] if nresult <= 0 else braver
        A[i+1] = nresult if nresult > 0 else 0
print(count)

Submission status: Accepted

Solution code:
n=int(input())
monsters=list(map(int, input().split()))
battle=list(map(int, input().split()))
remain=0
win=0
for i in range(n):
    if monsters[i] < battle[i]:
        win += monsters[i]
        if monsters[i+1] >= battle[i] - monsters[i]:
            win += battle[i] - monsters[i]
            monsters[i+1] -= battle[i] - monsters[i]
        else:
            win += monsters[i+1]
            monsters[i+1] = 0
    else:
        win += battle[i]
print(win)

Submission status: Accepted

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

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])
    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)

Submission status: Accepted

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

def sub(al, ar, b):
    tmp = (al + ar) - b
    if tmp < 0:
        return 0, al + ar
    else:
        if al > b:
            return ar, b 
        else:
            return ar+(al-b), b

ans = 0

ext, res = sub(A[0], A[1], B[0])
ans += res
for i in range(1, N):
    ext, res = sub(ext, A[i+1], B[i])
    ans += res

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()]

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)

Submission status: Accepted

Solution code:
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):
	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())
monsters=[int(x) for x in input().split(' ')]
powers=[int(x) for x in input().split(' ')]
ans=0
for i in range(n):
	t=min(monsters[i],powers[i])
	ans+=t
	t2=min(monsters[i+1],powers[i]-t)
	ans+=t2
	monsters[i+1]-=t2
print(ans)

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