Problem: 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?

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if a[0]-b[0]<0:
  totl=a[0]-b[0]
for i in range(n):
  if b[i]+b[i+1]>a[i+1]:
    totl+=a[i+1]-b[i]-b[i+1]
    
print(sum(b)+totl)
  


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


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)
  


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)
  


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)
  


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
totl=0
for i in range(n):
  m=min(b[i],max(a[i],a[i+1]))
  totl+=m
  del a[a.index(m)]
print(totl)
  


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)
    


  


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)
  


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)
  


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)
  


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


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


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


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Accepted