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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import itertools as ite
import math
import random
import sys
import codecs
from collections import defaultdict

INF = 10 ** 18
MOD = 10 ** 9 + 7

sys.setrecursionlimit(1000000)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    else:
        ans += b[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0

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

    c += e + d

print(c)

Submission status: Accepted

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

res = 0
for i in range(N):
  x = min(heroes[i], towns[i])
  towns[i] -= x
  heroes[i] -= x
  res += x
  x = min(heroes[i], towns[i + 1])
  towns[i + 1] -= x
  heroes[i] -= x
  res += x

print(res)

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

Submission status: Accepted

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

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

ANS=0

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

    x=min(A[i+1],B[i])
    ANS+=x
    A[i+1]-=x
    B[i]-=x

print(ANS)

Submission status: Accepted

Solution code:
N = int(input())

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

cnt = 0
for i,b in enumerate(B):
    c = min(b,A[i]+A[i+1])
    cnt += c
    A[i+1] -= max(0, c-A[i])
print(cnt)

Submission status: Accepted

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

ans = 0
for i in reversed(range(N)):
    m1 = min(B[i], A[i + 1])
    ans += m1
    m0 = min(B[i] - m1, A[i])
    A[i] -= m0
    ans += m0
print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    # s = input()
    # a, b = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    # a = [int(input()) for i in range(N)]

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
M=list(map(int,input().strip().split(' ')))
H=list(map(int,input().strip().split(' ')))
count=0
for i in range(len(H)):
    temp=min(M[i],H[i])
    H[i]-=temp
    M[i]-=temp
    count+=temp
    temp2=min(M[i+1],H[i])
    H[i]-=temp2
    M[i+1]-=temp2
    count+=temp2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
li = list(map(int,input().split()))
ans = 0
for i in range(n):
    num = min(lis[i],li[i])
    ans += num
    lis[i] -= num
    li[i] -= num
    nu = min(lis[i+1],li[i])
    ans += nu
    lis[i+1] -= nu
    li[i] -= nu
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict

def main():
    num = int(input())
    data_teki = list(map(int, input().split()))
    data_mikata = list(map(int, input().split()))

    ans = min(data_mikata[-1], data_teki[-1])
    nokori = data_mikata[-1] - ans
    for i in range(num)[::-1]:
        if i == 0:
            taoseru = nokori
        else:
            taoseru = nokori + data_mikata[i - 1]
        taosu  = min(data_teki[i], taoseru)
        ans += taosu
        nokori = min(data_mikata[i - 1], taoseru - taosu)
        # print(ans, nokori)

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    N = II()
    A = LMIIS()
    B = LMIIS()
    count = 0
    for i in range(N-1,-1,-1):
        if A[i+1] <= B[i]:
            count += A[i+1]
            B[i] -= A[i+1]
            if A[i] <= B[i]:
                count += A[i]
                A[i] = 0
            else:
                count += B[i]
                A[i] -= B[i]
        else:
            count += B[i]
    print(count)





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

print(ans)

Submission status: Accepted

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

prv = 0
ans = 0
for i in range(N):
    s = min(prv, A[i])
    A[i] -= s
    ans += s
    s = min(A[i], B[i])
    A[i] -= s
    B[i] -= s
    ans += s
    prv = B[i]
s = min(prv, A[N])
A[N] -= s
ans += s
print(ans)

Submission status: Accepted

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

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

print(ans)

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

    if B[i] > 0:
        c = min(A[i + 1], B[i])
        A[i + 1] -= c
        res += c

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]"):
    count = 0
    for i in range(N):
        b = min(A[i], B[i])
        count += b
        if A[i] < B[i]:
            c = min(B[i]-A[i], A[i+1])
            count += c
            A[i+1] -= c
    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N+1)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    n = int(input())
    arr = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    ans = 0
    for i in range(n):
        if arr[i] <= b[i]:
            ans += arr[i]
            b[i] -= arr[i]
            if arr[i + 1] <= b[i]:
                ans += arr[i + 1]
                arr[i + 1] = 0
            else:
                ans += b[i]
                arr[i + 1] -= b[i]
        else:
            ans += b[i] 
    
    print(ans)
    
    return 0

main()

Submission status: Accepted

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

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

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

    an_ = min(b, A[i+1])
    A[i+1] -= an_
    ans += an_
print(ans)

Submission status: Accepted

Solution code:
import queue

"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
T = 0 
for i in range(N):
	if B[i] > A[i]:
		B[i] -= A[i]
		T += A[i]
		T += min(B[i],A[i+1])
		A[i+1] -= min(B[i],A[i+1])
	else:
		T += B[i]
print(T)

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

Submission status: Runtime Error

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]:
        cnt += b[i]
    else:
        cnt += a[i]
        b[i+1] += b[i] - a[i]

if b[-1] <= a[-2]+a[-1]:
    cnt += b[-1]
else:
    cnt += a[-1]+a[-2]
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8


def main():
    N = int(input())
    A = list(map(int, input().split()))

    for i in range(N):
        b = int(input())
        tmp = A[i] - b
        if tmp < 0:
            ans += A[i]
            if A[i + 1] < -tmp:
                ans += A[i + 1]
                A[i + 1] = 0
            else:
                ans += -tmp
                A[i + 1] += tmp
        else:
            ans += b

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(sum(ans))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
int main(void){
    long long int n,a[100000],b[100000],i=1,j,saidai=0,m=0,L=0;
    string s;
    cin >> n;
    for(i=0;i<n+1;i++)
    {
        cin >> a[i];
        saidai+=a[i];
    }
    for(i=0;i<n;i++)
    {
        cin >> b[i];
        L+=b[i];
    }
    for(i=0;i<n;i++)
    {
        if(a[i]<b[i])
        {
            a[i+1]-=b[i]-a[i];
            if(a[i+1]<0)
            {
                a[i+1]=0;
            }
            a[i]=0;
        }
        else
        {
            a[i]=a[i]-b[i];
        }
    }
        for(i=0;i<n+1;i++)
        {
            m+=a[i];
        }
    if(saidai-m>L)
    {
        cout << L << endl;
    }
    else
    {
        cout << saidai-m << endl;
    }
    
    
    return 0;

}

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

    for i in range(n):
        x = A[i] + A[i+1]
        ans += min(x,B[i])
        A[i+1] = min(A[i+1],max(x-B[i+1],0))
    return print(ans)

solv()

Submission status: Runtime Error

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

    for i in range(n+1):
        x = A[i] + A[i+1]
        ans += min(x,B[i])
        A[i+1] = min(A[i+1],max(x-B[i+1],0))
    return print(ans)

solv()

Submission status: Runtime Error

Solution code:

def solv():
    n = int(input())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    ans = 0

    for i in range(n+1):
        x = A[i] + A[i+1]
        ans += min(x,B[i])
        A[i+1] = min(A[i+1],max(x-B[i+1],0))
    return print(ans)

solve()

Submission status: Runtime Error

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

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

solve()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

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

ans=0

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

print(ans)

Submission status: Runtime Error

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


print(ans)

Submission status: Runtime Error

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


print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[26]:


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


# In[27]:


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


# In[ ]:

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math
import re
import copy
import sys
import numpy as np

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

for i in range(n):
    c += min(a[i],b[i])
    if a[i] < b[i]:
        a[i+1] -= b[i]-a[i]
        c += b[i]-a[i]

print(c)

Submission status: Runtime Error

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

ans = 0

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

Submission status: Runtime Error

Solution code:
def main():
    num_monster = 0
    res = 0  # 余力
    for i in range(N):
        a = A[i] - res  # 前の人が倒しといてくれた分を引く
        b = B[i]
        if b <= a:  # 目の前の敵を倒し切れるかあやしい
            num_monster += b
            res = 0
        elif b <= a+A[i+1]:  # 次の敵の一部まで倒せる
            num_monster += b
            res = b - a
        else:  # 次の敵まで全部倒せる
            num_monster += a+A[i+1]
            res = A[i+1]
    print(num_monster)
main()

Submission status: Runtime Error

Solution code:
N = int(input())

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

ai = A[0]
ai2 = A[1]

for i in range(N):

  bi = B[i]
  
  if ai <= bi:
    cnt += ai
    
    if ai2 <= (bi - ai):
      cnt += ai2
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
    else:
      cnt += (bi - ai)
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
      
  else:
    cnt += bi
    ai = A[i+1]
    ai2 = A[i+2]
  
print(int(cnt))

Submission status: Runtime Error

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

sa = sum(A)

for i, b in enumerate(B):
    tmp = min(A[i], b)  # 削り取れる分
    A[i] -= tmp　　# 削り取る
    A[i + 1] -= min(A[i + 1], b - tmp)　　# i+1から削れる分はここで削っておく。b-tmpがi時点でのA[i+1]への攻撃力

print(sa - sum(A))　　# 全量から削れなかった分を引く

Submission status: Runtime Error

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

Submission status: Runtime Error

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

    cnt = 0
    res = 0 #余力
    for i in range(n):

        ch = min(a[i],res)
        cnt += ch
        a[i] -= ch
        if b[i] > a[i]:
            cnt += a[i]
            res = b[i] - a[i]
        else:
            cnt += b[i]
            res = 0
    
    cnt += min(a[n],res)

    print(cnt)

Submission status: Runtime Error

Solution code:
def check(al,bl,cnt,i,j):

    if(i==len(al) or j==len(bl)):
        return cnt

    elif(j>i):
        c = check(al,bl,cnt,i+1,j)
        
    elif(al[i] > bl[j]) :
        al[i] = al[i]-bl[j]
        cnt += bl[j]
        c = check(al,bl,cnt,i,j+1)
    elif(al[i] < bl[j]):
        bl[j] = bl[j] - al[i]
        cnt += al[i]
        c= check(al,bl,cnt,i+1,j)
    elif(al[i] == bl[j]):
        cnt += al[i]
        c = check(al,bl,cnt,i+1,j+1)

    return c

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

cnt = 0

print(check(a,b,cnt,0,0))

Submission status: Runtime Error

Solution code:
def check(al,bl,cnt,i,j):


    if(i==-1 or j==-1):
        return cnt

    elif(j >i):
        c = check(al,bl,cnt,i,j-1)
        
    elif(al[i] > bl[j]) :
        al[i] = al[i]-bl[j]
        cnt += bl[j]
        c = check(al,bl,cnt,i,j-1)
    elif(al[i] < bl[j]):
        bl[j] = bl[j] - al[i]
        cnt += al[i]
        c= check(al,bl,cnt,i-1,j)
    elif(al[i] == bl[j]):
        cnt += al[i]
        c = check(al,bl,cnt,i-1,j-1)

    return c

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

cnt = 0

print(check(a,b,cnt,len(a)-1,len(b)-1))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

dp = [0]*N

for i in range(N):
  dp[i] = min(A[i], B[i])
  A[i], B[i] -= dp[i]
  if rem==0:
    tmp = min(A[i+1], B[i])
    dp[i] += tmp
    A[i+1] -= tmp
print(sum(dp))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
mons = list(map(int, input().split()))
yush = list(map(int, input().split()))

temp = sum(mons)

for i, yu in enumerate(yush):
    mo = mons[i]
    if (yu >= mo):
        mons[i] = 0
        yush[i] = yu - mo
        
        if (mons[i + 1] > yush[i]):
            mons[i + 1] -= yush[i]
        else:
            monx[i + 1] = 0
        
    else:
        mons[i] = mo - yu
        yush[i] = 0
ans = temp - sum(mons)
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

print(ans)