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

ans = 0
nokori = 0
for i in range(N):
    ans += min(A[i],B[i])
    nokori = max(0, B[i]-A[i])
    ans += min(A[i+1] , nokori)
    A[i+1] = max(0, A[i+1]-nokori)
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]
    elif a[i+1] >= b[i] - a[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())
monster = list(map(int,input().split()))
brave = list(map(int,input().split()))
ans = 0
for i in range(n):
  kill = min(monster[i],brave[i])
  ans += kill
  brave[i] -= kill
  kill = min(monster[i+1],brave[i])
  ans += kill
  monster[i+1] -= kill
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):
    now_kill = min(a[i], b[i])
    killed += now_kill
    a[i] -= now_kill
    b[i] -= now_kill 
    if b[i] == 0:continue
    now_kill = min(a[i+1], b[i])
    killed += now_kill
    a[i+1] -= now_kill
    b[i] -= now_kill 

print(killed)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

from collections import deque

N = int(input())
mon = deque(list(map(int, input().split())))
pla = deque(list(map(int, input().split())))
array = []
a, b, c = 0, 0, 0

for i in range(N):
    a = mon.popleft()
    b = pla.popleft()
    if a < b:
        array.append(a)
        array.append(min((b - a), mon[0]))
        mon[0] -= min((b - a), mon[0])
    else:
        array.append(b)

print(sum(array))

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
import math
N = int(input())
monsters = list(map(int, input().split()))
aaaa = list(map(int, input().split()))
totalMonsters=sum(monsters)
aliveMonsters = monsters[0]
for i in range(N):
    monsters[i], aaaa[i] = max(monsters[i]-aaaa[i], 0), max(aaaa[i] - monsters[i], 0)
    monsters[i+1] = max(monsters[i+1]-aaaa[i], 0)
print(totalMonsters-sum(monsters))

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

    atk = B[i] if B[i] <= A[i+1] else A[i+1]
    A[i+1] = A[i+1] - atk
    B[i] = B[i] - atk
    res = res + atk

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


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


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B):
    S = sum(B)

    for i in range(0, N):
        ab = min(A[i], B[i])
        A[i] -= ab
        B[i] -= ab

        ab = min(A[i+1], B[i])
        A[i+1] -= ab
        B[i] -= ab
    
    return S - sum(B)


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    print(slv(N, A, B))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

    #print(A)

ans = 0
for i in range(n + 1):
    ans = ans + (A2[i] - A[i])
    #print(ans)
    
print(ans)

Submission status: Accepted

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

defeated_monster = 0

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

Submission status: Accepted

Solution code:
n=int(input())

monst=list(map(int,input().split()))
yusya=list(map(int,input().split()))
ans=0
for i in range(n):
  if(i==0):
    kill=min(yusya[0],monst[0])
    ans+=kill
    yusya[0]-=kill
    monst[0]-=kill
  else:
    kill=min(yusya[i-1],monst[i])
    ans+=kill
    yusya[i-1]-=kill
    monst[i]-=kill
    kill=min(yusya[i],monst[i])
    ans+=kill
    yusya[i]-=kill
    monst[i]-=kill
    
ans+=min(monst[-1],yusya[-1])

print(ans)

Submission status: Accepted

Solution code:
# your code goes he
def minn(a,b):
  if a>b:
    return b
  return a
n=int(input())
a=list(map(int,input().split()))

b=list(map(int,input().split()))
sm=0
tmp=0

for i in range(n):
  tmp=minn(a[i],b[i])
  sm=sm+tmp
  a[i]=a[i]-tmp
  b[i]=b[i]-tmp
  tmp=minn(a[i+1],b[i])
  sm=sm+tmp
  a[i+1]=a[i+1]-tmp
  b[i]=b[i]-tmp
  
print(sm)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:



n = input()
A = [int(x) for x in input().split()]
B = [int(y) for y in input().split()]

sum_hit_count = 0
next_knock_down = 0

for i in range(len(A)-1):
    remaining_enemy = A[i] - next_knock_down
    if remaining_enemy >= B[i]:
        sum_hit_count += B[i]
        next_knock_down = 0
        continue
    else:
        sum_hit_count += remaining_enemy
        remaining_capacity = B[i] - remaining_enemy
        if A[i+1] >= remaining_capacity:
            sum_hit_count += remaining_capacity
            next_knock_down = remaining_capacity
        else:
            sum_hit_count += A[i+1]
            next_knock_down = A[i+1]

print(sum_hit_count)

Submission status: Accepted

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

total = 0
for i in range(N):
  defeat_i = min(A[i], B[i])
  A[i] -= defeat_i
  defeat_i1 = min(A[i+1], B[i] - defeat_i)
  A[i+1] -= defeat_i1
  total += defeat_i + defeat_i1

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)
before = a[:]
count = 0
#print(a, b)

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

for j in range(0, n+1):
    count += (before[j] - a[j])

#print(a, b)
print(count)

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):
	ni0 = min(A[i],B[i])
	ni1 = min(A[i+1],B[i]-ni0)
	A[i] -= ni0
	A[i+1] -= ni1
	total += ni0+ni1

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

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

Total = 0

# i: 0 ~ N-1
for i in range(N):
  if min(A[i], B[i]) == B[i]:
    Total += B[i];
  else:
    Total += A[i];
    B[i] -= A[i];
    Total += min(A[i+1], B[i]);
    A[i+1] -= min(A[i+1], B[i]);
    
print(Total)

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):
    d=min(a[i],b[i])
    b[i]-=d
    ans+=d
    d=min(a[i+1],b[i])
    a[i+1]-=d
    ans+=d
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):
    aa=a[i]
    c+=min(a[i],b[i])
    a[i]=max(0,a[i]-b[i])
    if a[i]==0:
        c+=min(a[i+1],b[i]-aa)
        a[i+1]=max(0,a[i+1]+aa-b[i])
print(c)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N = _I()
A = LI()
B = LI()

"""
左からみていったときの不都合
特になし
"""
all_count = sum(A)
for bidx in range(len(B)):
    diff = min(A[bidx], B[bidx])
    A[bidx] -= diff
    B[bidx] -= diff
    diff = min(A[bidx+1], B[bidx])
    A[bidx+1] -= diff
    B[bidx] -= diff
final_count = sum(A)

#  print(all_count, final_count)
print(all_count - final_count)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
from collections import deque
from functools import reduce
from operator import mul
from pprint import pprint
import bisect
import copy
import fractions
import itertools
import math
import queue
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))
def COMBINATION(n, r):
    if n < 0 or r < 0 or n < r:
        return 0
    r = min(n-r, r)
    if r == 0:
        return 1
    o = reduce(mul, range(n, n - r, -1))
    u = reduce(mul, range(1, r + 1))
    return o // u
class UnionFind():

    def __init__(self, n):
        self.n = n
        self.node = [i for i in range(n)]
        self.size = [1 for _ in range(n)]

    def same(self, x, y):
        return bool(self.root(x) == self.root(y))

    def root(self, x):
        if self.node[x] == x:
            return x
        else:
            self.node[x] = self.root(self.node[x])  # 経路圧縮
            return self.node[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x == y:
            return  # 結合不要
        if self.size[x] < self.size[y]:
            x, y = y, x
        self.node[y] = x
        self.size[x] += self.size[y]

    def return_size(self, x):
        return self.size[self.root(x)]


n = II()
a = LI()
b = LI()
cnt = 0
for i in range(len(b)):
    if a[n-i] >= b[n-1-i]:
        cnt += b[n-1-i]
        continue
    else:
        cnt += a[n-i]
        b[n-1-i] -= a[n-i]
        if a[n-1-i] >= b[n-1-i]:
            cnt += b[n-1-i]
            a[n-1-i] -= b[n-1-i]
        else:
            cnt += a[n-1-i]
            a[n-1-i] = 0
print(cnt)

Submission status: Accepted

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

import sys
import copy

sys.setrecursionlimit(1000000)

#input = sys.stdin.readline
from collections import defaultdict

# ~~~~~~~~~~~~~~~~~~~~~_(＾～＾ ｣ ∠)_~~~~~~~~~~~~~~~~~~~~~

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    ans = 0

    for i in range(n):
        beat_now = min(a[i], b[i])
        ans += beat_now
        beat_next = min(a[i + 1], b[i] - min(a[i], b[i]))
        ans += beat_next
        a[i + 1] -= beat_next

    print(ans)


resolve()

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

print(count)

Submission status: Accepted

Solution code:
def calc(N, A, B):
    before = sum(A)
    for i in range(N - 1, -1, -1):
        if (B[i] >= A[i + 1]):
            B[i] -= A[i + 1]
            A[i] -= min(A[i], B[i])
            A[i + 1] = 0
        else:
            A[i + 1] -= B[i]
            B[i] = 0
    after = sum(A)
    return before - after


N = int(input())
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')]
print(calc(N, A, B))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0) #n+1番目を作っとく
num=0
c=0
for i in range(n):
    if a[i]<=c:
        num+=a[i]
        c=b[i]
    else:
        a[i]-=c
        num+=c
        if b[i]>=a[i]:
            num+=a[i]
            c=b[i]-a[i]
        else:
            num+=b[i]
            c=0
        
if b[n]+c>=a[n]:
    num+=a[n]
else:
    num+=b[n]+c
print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split())) + [0]
sums = sum(A)
subs = 0
tmp = 0
for i in range(N+1):
    A[i] = max(0,A[i]-tmp)
    cmp = A[i] - B[i]
    if cmp > 0:
        subs += cmp
        tmp = 0
    else:
        tmp = -cmp

ans = sums - subs
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
N = int(input())
AN = list(map(int, input().split()))
BN = list(map(int, input().split()))

ans = 0

for i in range(N):
    c1 = AN[i] if BN[i] >= AN[i] else BN[i]
    BN[i] -= c1
    AN[i] -= c1

    c2 = AN[i + 1] if BN[i] >= AN[i + 1] else BN[i]
    BN[i] -= c2
    AN[i + 1] -= c2

    ans += c1 + c2

print(ans)

Submission status: Accepted

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

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

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

    defeated = min(A[i+1], B[i])
    count += defeated
    A[i+1] -= defeated
    B[i] -= defeated

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math

# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    A = inputList()
    B = inputList()
    
    ans = 0
    for i, val in enumerate(A):
        if i == N:
            break
        if B[i] <= val:
            ans += B[i]
        else:
            tmp = B[i] - val
            ans += val
            if i-1 <= N-1:
                if tmp <= A[i+1]:
                    ans += tmp
                    A[i+1] -= tmp
                else:
                    ans += A[i+1]
                    A[i+1] = 0
                    
    print(ans)
            	
if __name__ == "__main__":
	main()

Submission status: Accepted

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

count=0
for i in range(N):
    if a_list[i]<b_list[i]:
        c=b_list[i]-a_list[i]
        count+=a_list[i]
        a_list[i]=0
        if a_list[i+1]<c:
            count+=a_list[i+1]
            a_list[i+1]=0
        else:
            a_list[i+1]-=c
            count+=c
    else:
        a_list[i]-=b_list[i]
        count+=b_list[i]
print(count)

Submission status: Accepted

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

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

    taosu0 = min(a0, b)
    b -= taosu0

    taosu1 = min(a1, b)
    A[i+1] -= taosu1

    ans += taosu0 + taosu1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

total_monster = sum(A)
for n in range(N):
  A1 = A[n]
  A2 = A[n+1]
  B1 = B[n]
  if B1 < A1:
    A[n] = A1 - B1
    A[n+1] = A2
  elif A1 <= B1 < A1+A2:
    A[n] = 0
    A[n+1] = A1 + A2 - B1
  else:
    A[n] = 0
    A[n+1] = 0

remaining_monster = sum(A)
ans = total_monster - remaining_monster
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

f = [[0]*(N+1)]*(N+1)
f[0][0] = min(A[0],B[0])

sum_f = f[0][0]

for i in range(N):
    f[i][i+1] = min(A[i+1],B[i]-f[i][i])
    sum_f += f[i][i+1]
    if i == N-1:
        break
    f[i+1][i+1] = min(A[i+1]-f[i][i+1],B[i+1])
    sum_f += f[i+1][i+1]

print(sum_f)

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    if A[i]+A[i+1] >= B[i]:
        if A[i]>B[i]:
            cnt += B[i]
        else:
            A[i+1] = A[i]+A[i+1] - B[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())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

e = A.copy()
for n in range(N):
	if e[n] < B[n]:
		e[n + 1] = max(e[n + 1] - (B[n] - e[n]), 0)
		e[n] = 0
	else:
		e[n] -= B[n]
#print(A)
#print(e)
print(sum(A) - sum(e))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 29 02:16:51 2019

@author: matsui
"""

N=int(input())
A=input().split()
B=input().split()

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

Submission status: Accepted

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

xs = A[::]
last = 0
for i in range(N):
    if 0 < last:
        xs[i] = max(xs[i] - last, 0)
    if B[i] <= xs[i]:
        xs[i] -= B[i]
        last = 0
    else:
        last = B[i] - xs[i]
        xs[i] = 0
xs[N] = max(xs[N] - last, 0)
print(sum(A) - sum(xs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
xs = [int(x) for x in stdin.readline().rstrip().split()]
ys = [int(x) for x in stdin.readline().rstrip().split()]

count = 0

for i in range(n):
   r = min(xs[i], ys[i])
   xs[i] -= r
   ys[i] -= r
   count += r
   r = min(xs[i+1], ys[i])
   xs[i+1] -= r
   count += r
   
print(count)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [int(x) for x in stdin.readline().rstrip().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:
            count += b[i]
            a[i+1] -= b[i]
    else:
        count += b[i]
        a[i] -= b[i]

print(count)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(input())
    A_lists = [int(x) for x in stdin.readline().rstrip().split()]
    B_lists = [int(x) for x in stdin.readline().rstrip().split()]

    kazyou = 0
    sum_ = 0

    for A, B in zip(A_lists, B_lists):
        if (A-kazyou) <= B:
            sum_ += A
            if A-kazyou < 0:
                kazyou = B
            else:
                kazyou = abs((A-kazyou)-B)
            # print(sum_, kazyou)
        else:
            sum_ += B + kazyou
            kazyou = 0
            # print(sum_, kazyou)

    if A_lists[N] <= kazyou:
        sum_ += A_lists[N]
    else:
        sum_ += kazyou

    print(sum_)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
monster = [int(i) for i in input().split()]
brave_power = [int(i) for i in input().split()]
toubatsu_count = 0

for i in range(N):
    if brave_power[i]>monster[i]:
        toubatsu_count+=monster[i]
        nokori_brave = brave_power[i] - monster[i]
        
        if nokori_brave > monster[i+1]:
            toubatsu_count+=monster[i+1]
            monster[i+1]=0
        else:
            toubatsu_count+=nokori_brave
            monster[i+1]-=nokori_brave
    else:
        toubatsu_count+=brave_power[i]

print(toubatsu_count)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline
def intinput():return map(int,input().split())


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