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: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))

c_list = []

for i in range(n):
    c_list.append(min(a_list[i] + a_list[i+1], b_list[i]))
    a_list[i+1] = a_list[i+1] - (a_list[i] - c_list[i])
    
sum = 0
for i in range(n):
    sum += c_list[i]
    
print(sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

N = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))

dair = {}
dbir = {}
for i in range(N):
    dair.setdefault(i, ai[N-i])
    dbir.setdefault(i, bi[N-1-i])

dair.setdefault(N, ai[0])
#print(dair)
#print(dbir)

ans = 0
for i in range(N):
    diff = dbir[i] - dair[i]
    if diff >= 0:
        ans += dair[i]
        diff2 = dair[i+1] - diff
        if diff2 >= 0:
            dair[i+1]=diff2
            ans += diff
        else:
            ans += dair[i+1]
            dair[i+1]=0
    #print(ans)

print(ans)
#print(dair)
#print(dbir)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(totl)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
totl=0
for i in range(n):
  totl+=min(a[i],b[i])
print(totl)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a_=a.sort(reverse=True)
b_=b.sort(reverse=True)
totl=0
for i in range(n):
  totl+=min(a[i],b[i])
print(totl)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a_=a.sort(reverse=True)
b_=b.sort(reverse=True)
totl=0
for i in range(n):
  totl+=max(a[i],b[i])
print(totl)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

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

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

print(count)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

a.append(0)
b.append(0)

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

Submission status: Wrong Answer

Solution code:
n=int(input())
T=list(map(int,input().split()))
AB=list(map(int,input().split()))
ans=0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

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

beaten_monster = sum(A) - sum(C)
print(beaten_monster)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
amari = 0
point = 0
for i in range(n):
    point += min(A[i],B[i])
    amari = min(A[i+1],B[i]-min(A[i],B[i]))
    if amari > 0:
        point += amari
print(point)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
new_A = [A[i]+A[i+1] for i in range(n)]
amari = 0
point = 0
for s in range(n):
    if B[s] >= new_A[s]:
        amari = B[s]-new_A[s]
    else:
        amari = 0
    point += (B[s]-amari)
print(point)

Submission status: Wrong Answer

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

A_copy = [x for x in A]
B_copy = [y for y in B]
cnt = 0
for i in range(N-1, -1, -1):
  if A_copy[i+1] > 0:
    cnt += min(A_copy[i+1], B_copy[i])
    A_copy[i+1] = max(A_copy[i+1]-B[i], 0)
    B_copy[i] = max(B_copy[i]-A[i+1], 0)
  if B_copy[i] > 0:
    cnt += min(A_copy[i], B_copy[i])
    a = A_copy[i]
    A_copy[i] = max(A_copy[i]-B_copy[i], 0)
    B_copy[i] = max(B_copy[i]-a, 0)

n = sum(A) - sum(A_copy)
print(cnt)

Submission status: Wrong Answer

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

A_copy = [x for x in A]
B_copy = [y for y in B]
for i in range(N-1, -1, -1):
  if A_copy[i+1] > 0:
    A_copy[i+1] = max(A_copy[i+1]-B[i], 0)
    B_copy[i] = max(B_copy[i]-A[i+1], 0)
  if B_copy[i] > 0:
    a = A_copy[i]
    A_copy[i] = max(A_copy[i]-B_copy[i], 0)
    B_copy[i] = max(B_copy[i]-a, 0)

n = sum(A) - sum(A_copy)
print(n)

Submission status: Wrong Answer

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

A_copy = [x for x in A]
B_copy = [y for y in B]
for i in range(N-1, -1, -1):
  if A_copy[i+1] > 0:
    A_copy[i+1] = max(A_copy[i+1]-B[i], 0)
    B_copy[i] = max(B_copy[i]-A[i+1], 0)
  if B_copy[i] > 0:
    a = A_copy[i]
    A_copy[i] = max(A_copy[i]-B_copy[i], 0)
    B_copy[i] = max(B_copy[i]-a, 0)

n = sum(B) - sum(B_copy)
print(n)

Submission status: Wrong Answer

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

A_copy = [x for x in A]
B_copy = [y for y in B]
for i in range(N-1, -1, -1):
  if A_copy[i+1] > 0:
    A_copy[i+1] = max(A_copy[i+1]-B[i], 0)
    B_copy[i] = max(B_copy[i]-A[i+1], 0)
  if B_copy[i] > 0:
    A_copy[i] = max(A_copy[i]-B[i], 0)
    B_copy[i] = max(B_copy[i]-A[i], 0)

n = sum(B) - sum(B_copy)
print(n)

Submission status: Wrong Answer

Solution code:
n = int(input())
mnstr = list(map(int,input().split()))
yshabl = list(map(int,input().split()))
#twn = int(data[0]) + 1
#ysh = int(data[0])
#yshabl = list(map(int,data[2].split()))
#mnstr = list(map(int,data[1].split()))

cnt = 0

for i in range(-1,-1*len(yshabl)-1,-1):
	y = yshabl[i]
	m = mnstr[i]+mnstr[i-1]
	if m >= y:
		cnt += y
		mnstr[i-1] -= y - mnstr[i]
	else:
		cnt += m
		mnstr[i-1] = 0

print(cnt)

Submission status: Wrong Answer

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]
            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: Wrong Answer

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 >= hero:
      add = hero
    else:
      add = (hero_added - mons)
print(sum_monster)

Submission status: Wrong Answer

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:
    add = (hero_added - mons)
    sum_monster += mons
print(sum_monster)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A_T = list(reversed(A))
B_T = list(reversed(B))
A_survive = A_T.copy()
B_avail = B_T.copy()
ans = 0

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

print(ans)

Submission status: Wrong Answer

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

print(tkill)

Submission status: Wrong Answer

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

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]
    else:
        ans += lstB[i]
print(ans)

Submission status: Wrong Answer

Solution code:

stdin = input()
n = int(stdin)

stdin = input()
monster = list(map(int, stdin.split()))

stdin = input()
yuu = list(map(int, stdin.split()))
yuu.append(0)

total_kill = 0
rest = 0
for mi, yi in zip(monster, yuu):
    total_kill += min(mi, yi + rest)
    rest = yi - mi + rest
    if rest < 0:
        rest = 0

print(total_kill)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0)
i=0
count=0
while True:
    if i==n:
        break
    elif a[i]>b[i]:
        i+=1
        count+=min(a[i],b[i])
    else:
        count+=min(a[i],b[i])
        c=b[i]-a[i]
        count+=min(a[i+1],c)
        if c>a[i+1]:
            a[i+1]=0
            i+=1
        else:
            a[i+1]-=c
            i+=1
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=0
count=0
while True:
    if i==n:
        break
    elif a[i]>b[i]:
        count+=b[i]
        i+=1
    else:
        count+=a[i]
        c=b[i]-a[i]
        if a[i+1]>c:
            count+=c
            i+=1
        else:
            count+=a[i+1]
            i+=1
print(count)

Submission status: Wrong Answer

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
  else:
    add = (hero_added - mons)
    sum_monster += mons
print(sum_monster)

Submission status: Wrong Answer

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] + tmp - As[i]

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

print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def city_savers(n,a,b):
    terminate = 0

    for i in range(n-1,-1,-1):
        count = 0
        #i+1
#        print("i = %d, a[i] = %d, a[i+1] = %d, b[i] = %d" %(i,a[i],a[i+1],b[i]))
        if b[i] == a[i+1]:
            mons = 0
            count = b[i]
            b[i] = 0
        elif b[i] > a[i+1]:
            mons = 0
            count = a[i+1]
            b[i] = b[i] - a[i+1]
        elif b[i] < a[i+1]:
            mons = a[i+1] - b[i]
            count = b[i]
            b[i] = 0
        terminate += count
#       print("i+1 = %d, terminate = %d" %(i+1,count))
        count = 0
#       print("i = %d, a[i] = %d, a[i+1] = %d, b[i] = %d" %(i,a[i],a[i+1],b[i]))
        #i
        if b[i] == a[i]:
            mons = 0
            count = b[i]
            b[i] = 0
        elif b[i] > a[i]:
            mons = 0
            count = a[i]
            b[i] = b[i] - a[i]
        elif b[i] < a[i]:
            mons = a[i] - b[i]
            count = b[i]
            b[i] = 0
        terminate += count
#        print("i = %d, count = %d" %(i,count))
#        print("")
    return terminate
if __name__ == "__main__":
    n = int(raw_input())
    a = list(map(int,raw_input().split()))
    b = list(map(int,raw_input().split()))

    print(city_savers(n,a,b))

Submission status: Wrong Answer

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]
    a[i+1]-=b[i]-a[i]
    cnt+=min(b[i]-a[i+1], a[i+1])
    a[i+1]=max(a[i+1], 0)
print(str(cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
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(1, n-1):
  b_rest.append( min(b[i], max( ((b[i]+b_rest[i-1])-a[i]), 0 ) ) )
  a_rest.append( max( a[i]-(b[i]+b_rest[i-1]), 0 ) ) 

a_rest.append(max(0, (a[n-1]-b_rest[-1]) ) )
ans = sum(a) - sum(a_rest)
                                
print(ans)

Submission status: Wrong Answer

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

print(a - ans)

Submission status: Wrong Answer

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

print(a - ans)









#     C.append(A[i] + A[i+1])
#     D.append(C[i])
#     C[i] = C[i]-B[i]
#     if C[i] < 0:
#         C[i] = 0
# print(C)
# ans = sum(A) -sum(C)

# print(ans,sum(C))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
## 最初から
# i=0の街
if A_list[0] <= B_list[0]:
  ans += A_list[0]
  B_list[0] = B_list[0] - A_list[0]
else:
  ans += B_list[0]
  B_list[0] = 0
  
# i= 1~Nの街
for i in range(1, N):
  if A_list[i] <= B_list[i-1] + B_list[i]:
    ans += A_list[i]
    if A_list[i] > B_list[i-1]:
      B_list[i] = B_list[i-1] - (A_list[i] - B_list[i])
  else:
    ans += A_list[i] - B_list[i]
    B_list[i] = 0

# i=N+1の街
if A_list[N] <= B_list[N-1]:
  ans += A_list[N]
else:
  ans += A_list[N] - B_list[N-1]

## 最後から
ans2 = 0
# i=N+1の街
if A_list[N] <= B_list[N-1]:
  ans2 += A_list[N]
else:
  ans2 += A_list[N] - B_list[N-1]
  
# i= 1~Nの街
for i in range(N-1, 0, -1):
  if A_list[i] <= B_list[i-1] + B_list[i]:
    ans2 += A_list[i]
    if A_list[i] > B_list[i-1]:
      B_list[i] = B_list[i] - (A_list[i] - B_list[i-1]) 
  else:
    ans2 += A_list[i] - B_list[i]
    B_list[i] = 0

# i=0の街
if A_list[0] <= B_list[0]:
  ans2 += A_list[0]
  B_list[0] = B_list[0] - A_list[0]
else:
  ans2 += B_list[0]
  B_list[0] = 0

if ans > ans2:
  print(ans)
else:
  print(ans2)

Submission status: Wrong Answer

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

ans = 0
## 最初から
# i=0の街
if A_list[0] <= B_list[0]:
  ans += A_list[0]
  B_list[0] = B_list[0] - A_list[0]
else:
  ans += B_list[0]
  B_list[0] = 0
  
# i= 1~Nの街
for i in range(1, N):
  if A_list[i] <= B_list[i-1] + B_list[i]:
    ans += A_list[i]
    B_list[i] = B_list[i-1] + B_list[i] - A_list[i]
  else:
    ans += A_list[i] - B_list[i]
    B_list[i] = 0

# i=N+1の街
if A_list[N] <= B_list[N-1]:
  ans += A_list[N]
else:
  ans += A_list[N] - B_list[N-1]

## 最後から
ans2 = 0
# i=N+1の街
if A_list[N] <= B_list[N-1]:
  ans2 += A_list[N]
else:
  ans2 += A_list[N] - B_list[N-1]
  
# i= 1~Nの街
for i in range(N-1, 0, -1):
  if A_list[i] <= B_list[i-1] + B_list[i]:
    ans2 += A_list[i]
    B_list[i] = B_list[i-1] + B_list[i] - A_list[i]
  else:
    ans2 += A_list[i] - B_list[i]
    B_list[i] = 0

# i=0の街
if A_list[0] <= B_list[0]:
  ans2 += A_list[0]
  B_list[0] = B_list[0] - A_list[0]
else:
  ans2 += B_list[0]
  B_list[0] = 0

if ans > ans2:
  print(ans)
else:
  print(ans2)

Submission status: Wrong Answer

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

ans = 0
# i=0の街
if A_list[0] <= B_list[0]:
  ans += A_list[0]
  B_list[0] = B_list[0] - A_list[0]
else:
  ans += B_list[0]
  B_list[0] = 0
  
# i= 1~Nの街
for i in range(1, N):
  if A_list[i] <= B_list[i-1] + B_list[i]:
    ans += A_list[i]
    B_list[i] = B_list[i-1] + B_list[i] - A_list[i]
  else:
    ans += A_list[i] - B_list[i]
    B_list[i] = 0

# i=N+1の街
if A_list[N] <= B_list[N-1]:
  ans += A_list[N]
else:
  ans += A_list[N] - B_list[N-1]

print(ans)

Submission status: Wrong Answer

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
    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: Wrong Answer

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

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

  if Y[i] > 0:
    A[i + 1] = A[i + 1] - Y[i]

print(A_sum_1 - sum(A))

Submission status: Wrong Answer

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 remain != 0:
        win += remain
        battle[i] -= remain
        remain = 0
    if monsters[i] < battle[i]:
        win += monsters[i]
        monsters[i+1] -= battle[i] - monsters[i]
    else:
        win += battle[i]
        remain = monsters[i] - battle[i]
print(win)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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-1,-1,-1):
	if monsters[i+1]>=powers[i]:
		ans+=powers[i]
	else:
		ans+=monsters[i+1]
		powers[i]-=monsters[i+1]
		if monsters[i]>=powers[i]:
			monsters[i]-=powers[i]
			ans+=powers[i]
		else:
			ans+=monsters[i]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
for i in range(N):
  c1 = min(A[i], B[i])
  c2 = min(A[i+1], max(0, B[i]-A[i]))
  A[i+1] -= c2
  count += c1+c2

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
i = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
d = 0
e = 0
z = 0
y = 0

for f in range(i):
    d = b[f] - a[f]
    if d > 0:
        z = z + a[f]
        y = a[f+1]
        a[f+1] = a[f+1] - d
        if a[f+1] <= 0:
            a[f+1] = 0
            z = z + y
        else:
            z = z + y
    elif d < 0:
        z = z + b[f]
    else:
        z = z + a[f]
print(z)

Submission status: Wrong Answer

Solution code:
i = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
d = 0
e = 0
z = 0

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

Submission status: Wrong Answer

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

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split())) + [0]

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

    ans += min(A[i], B[i])
    if B[i] > A[i]:
        ex = B[i] - A[i]

print(ans)

Submission status: Wrong Answer

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

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split())) + [0]

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

print(ans)