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

print(dead_mon)

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    B=LI()
    ans = 0
    for i,b in enumerate(B):
        if A[i]<=b:
            ans += A[i]
            b -= A[i]
            ans += max(0,min(A[i+1],b))
            A[i+1] = max(0,A[i+1]-b)
        else:            
            ans += b
            b = 0
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# ABC136 C-City Savers
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0

for i in range(n):
    a_t=min(b[i],a[i])
    ans+=a_t
    b[i]-=a_t
    b_t=min(a[i+1],b[i])
    a[i+1]-=b_t
    ans+=b_t
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):
    # 正面の町を救う
    beaten = min(a[i], b[i])
    b[i] -= beaten
    # 隣町を救う
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
ans = 0
for i in range(n):
    beaten = min(a[i], b[i])
    b[i] -= beaten
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
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):
    a_m = min(a[i], b[i])
    c += a_m
    b[i] -= a_m
    b_m = min(a[i+1], b[i])
    a[i+1] -= b_m
    c += b_m

print(c)

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):
    a = min(A[i], B[i])
    ans += a
    B[i] -= a
    b = min(A[i + 1], B[i])
    A[i + 1] -= b
    ans += b
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 A[i]+A[i+1]<=B[i]:
		count+=A[i]+A[i+1]
		B[i]-=A[i]+A[i+1]
		A[i]=0
		A[i+1]=0
	elif A[i]<=B[i] and A[i]+A[i+1]>B[i]:
		count+=B[i]
		B[i]-=A[i]
		A[i+1]-=B[i]
		A[i]=0
		B[i]=0
	else:
		count+=B[i]
print(count)

Submission status: Accepted

Solution code:
# 2020/02/22
# AtCoder Beginner Contest 135 - C

# Input
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

# Calc
ans = 0
for i in range(n):
    remb = max(b[i] - a[i], 0)
    geta = min(b[i], a[i]) + min(a[i+1], remb)
    a[i+1] = max(a[i+1] - remb, 0)

    ans = ans + geta

# Output
print(ans)

Submission status: Accepted

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

for i in range(N):
    a_1, a_2 = A_list[i], A_list[i+1]
    b = B_list[i]
    if b>=a_1:
        monster_count = monster_count + a_1
        b = b - a_1
    elif b<a_1:
        monster_count = monster_count + b
        continue

    if b>=a_2:
        monster_count = monster_count + a_2
        A_list[i+1] = 0
    elif b<a_2:
        monster_count = monster_count + b
        a_2 = a_2 - b
        A_list[i+1] = a_2

print(monster_count)

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

"""
#DPを書きたいぞ
ans = [0] * n #i番目の勇者が倒すモンスターの数
if b[0] >= a[1] + a[0]:
    ans[0] = a[1] + a[0]
elif ans[0] <= b[0]:
    ans[0] = 
"""

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
ans = 0
for i in range(n):
    beat = min(a[i], b[i])
    a[i] -= beat
    b[i] -= beat
    if i != n:
        beat2 = min(a[i + 1], b[i])
        a[i + 1] -= beat2
    ans += beat + beat2
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 reversed(range(1, n+1)):
    if A[i] <= B[i-1]:
        ans += A[i]
        B[i-1] -= A[i]
        if A[i-1] >= B[i-1]:
            ans += B[i-1]
            A[i-1] -= B[i-1]
        else:
            ans += A[i-1]
            A[i-1] = 0
    else:
        ans += B[i-1]
        B[i-1] = 0
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

n = ni()
a = na()
b = na()

attack = 0
apre = a[0]
for ai, bi in zip(a[1:], b):
    attackleft = min(apre, bi)
    attack += attackleft
    bi -= attackleft
    attackright = min(ai, bi)
    attack += attackright
    apre = ai - attackright

print(attack)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

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]
            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]
            B[i] = 0
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    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]:
            if A[i+1]<=B[i]-A[i]:
                count+=A[i+1]+A[i]
                A[i+1]=0
            else:
                count+=B[i]
                A[i+1]=A[i+1]-(B[i]-A[i])
        if A[i]>B[i]:
            count+=B[i]
    print(count)
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, b in enumerate(B):
    l = min(A[i], b)
    b -= l
    r = min(A[i + 1], b)
    A[i] -= l
    A[i + 1] -= r

    ans += l + r

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a, b = [list(map(int, input().split())) for i in range(2)]

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

Submission status: Accepted

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

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

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

ans = 0
for i in range(n):
    if a[i] <= b[i]:
        if a[i]+a[i+1] > b[i]:
            all_enemy = a[i] + a[i + 1]
            ans += b[i]
            a[i+1] = a[i]+a[i+1] - b[i]
        else:
            all_enemy = a[i] + a[i + 1]
            ans += all_enemy
            a[i+1] = 0
    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()))
count =0
for i in range(N):
  if A[i] >= B[i]  and A[i] >= 0:
    count += B[i]
    A[i] = A[i] - B[i]
  elif A[i] < B[i] and A[i] >= 0:
    if A[i+1]-B[i]+A[i] > 0:
      count += B[i]
    else:
      count += A[i] +A[i+1]
    A[i+1] -=  B[i] - A[i]
    A[i] = -1
  elif A[i] < 0:
    if A[i+1] >= B[i]:
      count += B[i]
      A[i+1] = A[i+1]- B[i]
    else:
      count += A[i+1]
      A[i+1] -= B[i]
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]+list(map(int,input().split()))+[0]
B = [0]+list(map(int,input().split()))+[0]
ans = 0
for i in range(1, N+2):
    c = min(A[i], B[i-1])
    A[i] -= c
    ans += c
    c = min(A[i], B[i])
    A[i] -= c
    B[i] -= c
    ans += c
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 += A[i]
    B[i] -= A[i]
    if B[i] >= A[i+1]:
      count += A[i+1]
      A[i+1] = 0
    else:
      count += B[i]
      A[i+1] -= B[i]
  else:
     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()))
cnt = 0
for i in range(n):
    if b[i] >= a[i]:
        cnt += a[i]
        b[i] = b[i] - a[i]
        if b[i] >= a[i+1]:
            cnt += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] = a[i+1]-b[i]
            cnt += b[i]
    else:
        cnt += b[i]
print(cnt)

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

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

Submission status: Accepted

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

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 a[i] < b[i] and a[i+1] <= b[i]-a[i]:
        res += (a[i]+a[i+1])
        a[i] = 0
        a[i+1] = 0
    elif a[i] < b[i]:
        res += b[i]
        a[i+1] = a[i+1]+a[i]-b[i]
        a[i] = 0
    else:
        res += b[i]
        a[i] = a[i]-b[i]
    #print(a)

print(res)

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
*a, = map(int, sys.stdin.readline().split())
*b, = map(int, sys.stdin.readline().split())

def attack(i, j, cnt):
    if b[i] <= a[j]:
        a[j] -= b[i]
        cnt += b[i]
        b[i] = 0
    else:
        b[i] -= a[j]
        cnt += a[j]
        a[j] = 0
    return cnt

def main():
    cnt = 0
    for i in range(n):
        cnt = attack(i, i, cnt)
        cnt = attack(i, i+1, cnt)
    return cnt

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

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
*a, = map(int, sys.stdin.readline().split())
*b, = map(int, sys.stdin.readline().split())

def main():
    cnt = 0
    for i in range(n):
        if b[i] <= a[i]:
            a[i] -= b[i]
            cnt += b[i]
            b[i] = 0
        else:
            b[i] -= a[i]
            cnt += a[i]
            a[i] = 0
        
        if b[i] >= a[i+1]:
            cnt += a[i+1]
            b[i] -= a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
            cnt += b[i]
            b[i] = 0
    return cnt

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

Submission status: Accepted

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

res = 0
rem = 0

for i in range(N):
    mobs_rem = min(A[i], rem)
    A[i] -= mobs_rem
    mobs = min(A[i], B[i])
    rem = B[i] - mobs
    res += (mobs+mobs_rem)

res += min(A[N], rem)
print(res)

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int,input().split()))
init = sum(data)
datb = list(map(int,input().split()))
res = 0
#print(data)
for i in range(len(datb)):
    #print("---------------")
    #pprint(datb)
    dmg = min(datb[i], data[i])
    data[i] = data[i] - min(datb[i], data[i])
    res += dmg

    datb[i] -= dmg
    datb[i] = 0 if datb[i] < 0 else datb[i]
    #print(datb)

    res += min(datb[i], data[i+1])
    data[i+1] = data[i+1] - min(datb[i], data[i+1])
    #print(data)

print(init - sum(data))

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

Submission status: Accepted

Solution code:
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))
monster = 0

for i in range(n):
    a1 = a_lst[i]
    a2 = a_lst[i+1]
    b1 = b_lst[i]
    tmp = b1-a1

    monster += min(a1,b1)
    if tmp>0:
        if a2-tmp<=0:
            monster += a2
            a_lst[i+1] = 0
        else:
            monster += tmp
            a_lst[i+1] -= tmp

print(monster)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()][::-1]
B = [int(x) for x in input().split()][::-1]
beats = 0
carry = 0
for a, b in zip(A, B):
    beats = beats + min(b + carry, a)
    carry = max(b - max(a - carry, 0), 0)
print(beats + min(A[-1], carry))

Submission status: Accepted

Solution code:
# coidng: utf-8

def main():
  count = int(input())
  monsters = list(map(int, input().split()))
  monsters_at_start = sum(monsters)
  heroes = list(map(int, input().split()))
  
  for i, hero in enumerate(heroes):
    if monsters[i] >= hero:
      monsters[i] -= hero
      
    elif monsters[i] < hero:
      hero -= monsters[i]
      monsters[i] = 0
      if monsters[i + 1] >= hero:
        monsters[i + 1] -= hero
        
      else:
        monsters[i + 1] = 0
        
  print(monsters_at_start - sum(monsters))
  
  return 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()))
s1 = sum(A)

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

if B[0] >= A[0]:
    A[0] = 0
else:
    A[0] -= B[0]

print(s1 - sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
B.append(0)
cnt = 0

if A[0] <= B[0]:
    cnt += A[0]
    B[0] -= A[0]
else:
    cnt += B[0]
    B[0] = 0
    
for i in range(1,N+1):
    M = B[i-1]
    L = B[i]
    if A[i] <= M:
        cnt += A[i]
    elif A[i] <= L+M:
        cnt += A[i]
        B[i] -= A[i]-M
    elif A[i] > L+M:
        cnt += L+M
        B[i] = 0

print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
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]
        tmp = min(B[i]-A[i], A[i+1])
        ans += tmp
        A[i+1] -= tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8


class Monster:
    def __init__(self, hp):
        self.hp = int(hp)


class Hero:
    def __init__(self, hp):
        self.hp = int(hp)

    def attack(self, monster_1st, monster_2nd):
        if self._attack(monster_1st):
            self._attack(monster_2nd)

    def _attack(self, monster):
        if monster.hp == self.hp:
            monster.hp = 0
            self.hp = 0
            return False
        elif monster.hp > self.hp:
            monster.hp -= self.hp
            self.hp = 0
            return False
        elif monster.hp < self.hp:
            self.hp -= monster.hp
            monster.hp = 0
            return True


def main():
    _ = int(input())
    monsters = list(map(Monster, input().split(' ')))
    monsters_at_start = sum(monster.hp for monster in monsters)
    heroes = list(map(Hero, input().split(' ')))

    for i, hero in enumerate(heroes):
        hero.attack(monsters[i], monsters[i + 1])

    print(monsters_at_start - sum(monster.hp for monster in monsters))

    return 0


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8


def main():

    count = int(input())

    monsters = list(map(int, input().split(' ')))

    monsters_at_start = sum(monsters)

    heroes = list(map(int, input().split(' ')))

    for i, hero in enumerate(heroes):

        if monsters[i] >= hero:

            monsters[i] -= hero

        elif monsters[i] < hero:

            hero -= monsters[i]

            monsters[i] = 0

            if monsters[i + 1] >= hero:

                monsters[i + 1] -= hero

            else:

                monsters[i + 1] = 0


    print(monsters_at_start - sum(monsters))

    return 0


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
Monsters = deque(map(int, input().split()))
Fighters = deque(map(int, input().split()))

Knockouts = 0
prev_ftr = 0

while len(Monsters) > 0:
  mon = Monsters.popleft()
  ko1 = min(mon, prev_ftr)
  #print(mon, prev_ftr, ko1)
  Knockouts += ko1
  mon -= ko1

  if len(Fighters) > 0:
    ftr = Fighters.popleft()
    ko2 = min(mon, ftr)
    #print(mon, ftr, ko2)
    Knockouts += ko2
    mon -= ko2
    prev_ftr = ftr - ko2

print(Knockouts)

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])
  ans += left

  a[i] -= left
  b[i] -= left

  #右側
  right = min(a[i + 1], b[i])
  ans += right
  a[i + 1] -= right
  b[i] -= right

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

dest = 0
nokori = 0
for i in reversed(range(N)):
	if A[i + 1] >= nokori:
		dest += nokori
		mons_hp = A[i + 1] - nokori
	else:
		dest += A[i + 1]
		mons_hp = 0

	if mons_hp > B[i]:
		nokori = 0
		dest += B[i]
	else:
		nokori = B[i] - mons_hp
		dest += mons_hp

if A[0] > nokori:
	dest += nokori
else:
	dest += A[0] 

print(dest)

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(0,N):
    if A[i]>B[i]:
        ans+=B[i]
        A[i]-=B[i]
    else:
        ans+=A[i]
        B[i]-=A[i]
        A[i]=0
        if A[i+1]>B[i]:
            ans+=B[i]
            A[i+1]-=B[i]
        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()))
a.append(0)
b.append(0)
b.append(0)
ans = 0
for i in range(n+1):
    if a[i]-b[i] > 0:
        ans += b[i]
    elif a[i]-b[i] <= 0 and a[i] + a[i+1] >= b[i]:
        b[i+1] += b[i]-a[i]
        ans += a[i]
    else:
        b[i+1] += a[i+1]
        ans += a[i]
print(ans)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  import sys
  n = int(input())
  a = list(map(int,sys.stdin.readline().split()))
  b = list(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for c,h in zip(a,b):
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys
  n = int(input())
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for c,h in zip(a,b):
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys
  n = int(input())
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for i in range(n):
    c = a[i]
    h = b[i]
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys
  n = int(input())
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  cNow=a[0]
  cNext=a[1]
  
  for i in range(n):
    cNext=a[i+1]
    h = b[i]
    count+=min(cNow,h)  
    if h>cNow:
        count+=min(h-cNow,cNext)
        cNow=max(0,cNext-(h-cNow))
        continue
    cNow=cNext
  print(count)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
def main():
  import sys
  n = int(input())
  a = list(map(int,sys.stdin.readline().split()))
  b = list(map(int,sys.stdin.readline().split()))
  
  count=0
  
  for i in range(n):
    c1 = a[i] 
    c2 = a[i+1]
    h = b[i]
    count+=min(c1,h)  
    if h>c1:
        count+=min(h-c1,c2)
        a[i+1]=max(0,c2-(h-c1))
 
  print(count)
  
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=0
for i in range(n):
    if A[i]>B[i]:
        s+=B[i]
    
    elif A[i]<=B[i] and B[i]<=A[i+1]+A[i]:
        s+=B[i]
        A[i+1]=A[i+1]+A[i]-B[i]
    elif B[i]>A[i+1]+A[i]:
        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()))

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


print(total)

Submission status: Accepted

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


ans = 0

if B[0] >= A[0]:
    ans += A[0]
    B[0] -= A[0]
else:
    ans += B[0]
    B[0] = 0
    
A = A[1:]
B.append(0)

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

Submission status: Accepted

Solution code:
# Code for C - City Savers
# Use input() to fetch data from STDIN
n = input()
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

total = 0
for i in range(len(a) - 1):
    s = min(a[i], b[i])
    a[i] = a[i] - s
    b[i] = b[i] - s

    t = min(a[i+1], b[i])
    a[i+1] = a[i+1] - t
    b[i] = b[i] - t
    total += s + t


print(total)

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):
    if A[i] >= B[i]:
        ans += B[i]
    else:
        if A[i] + A[i+1] >= B[i]:
            ans += B[i]
            A[i+1] -= (B[i]-A[i])
        else:
            ans += A[i]+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
rem = a[0]
for i in range(n):
    ans += min(rem+a[i+1], b[i])
    if rem-b[i]<0:
        rem = max(rem+a[i+1]-b[i], 0)
    else:
        rem = a[i+1]
print(ans)

Submission status: Accepted

Solution code:
def f(a, b, pb):
    return min(a, b + pb), max(0, b - max(a - pb, 0))

def g(A, B):
    ans, pb = 0, 0
    for a, b in zip(A, B):
        k, pb = f(a, b, pb)
        ans += k
    return ans

N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split())) + (0,)
print(g(A, B))

Submission status: Accepted

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

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

if sum(a)>0:
    ans = sums-sum(a)
else:
    ans = sums
print(ans)