input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
h,n=list(map(int,input().split())) AB=[list(map(int,input().split())) for i in range(n)] A_max=max(AB)[0] DP=[0]+[float("inf")]*(h+A_max) for i in range(h): if DP[i]==float("inf"):continue for a,b in AB: t=DP[i]+b if t<DP[i+a]: DP[i+a]=t print((min(DP[h:])))
from sys import stdin readline=stdin.readline h,n=list(map(int,readline().split())) AB=[list(map(int,readline().split())) for i in range(n)] A_max=max(AB)[0] DP=[0]+[float("inf")]*(h+A_max) for i in range(h): if DP[i]==float("inf"):continue for a,b in AB: t=DP[i]+b if t<DP[i+a]: ...
p02787
h,n=(int(i) for i in input().split()) l=[] for i in range(n): a,b=(int(i) for i in input().split()) l.append([a,b]) pocket=[0] for i in range(h): r=[] for j in range(n): a=l[j][0] b=l[j][1] r.append(pocket[max(0,i+1-a)]+b) pocket.append(min(r)) print((pocket[h]))
h,n=(int(i) for i in input().split()) l=[] for i in range(n): a,b=(int(i) for i in input().split()) l.append([a,b]) pocket=[0] for i in range(h): mini=pocket[max(0,i+1-l[0][0])]+l[0][1] for j in range(n-1): a=l[j+1][0] b=l[j+1][1] if pocket[max(0,i+1-a)]+b<mini: mini=pocket[max(0,i...
p02787
H, N = list(map(int, input().split())) ab_list = [list(map(int, input().split())) for _ in range(N)] max_a = max(a for a,b in ab_list) W = H + 1 + max_a dp = [0] * W for i in range(W): dp[i] = 10**9 for a,b in ab_list: dp[i] = min(dp[i], dp[i-a]+b) print((min(dp[H-1:])))
H, N = list(map(int, input().split())) ab_list = [list(map(int, input().split())) for _ in range(N)] max_a = max(a for a,b in ab_list) W = H + 1 + max_a dp = [0] * W for i in range(W): dp[i] = min(dp[i-a]+b for a,b in ab_list) print((min(dp[H-1:])))
p02787
import sys import math def main(): h_and_n_s = sys.stdin.readline().strip().split(' ') h, n = [int(s) for s in h_and_n_s] a = [-1] * n b = [-1] * n for i in range(n): a[i], b[i] = [int(s) for s in sys.stdin.readline().strip().split(' ')] dp = [float('inf')] * (h+1) ...
import sys def main(): h_and_n_s = sys.stdin.readline().strip().split(' ') h, n = [int(s) for s in h_and_n_s] a = [-1] * n b = [-1] * n # max_a = 0 # ef_i = -1 for i in range(n): a[i], b[i] = [int(s) for s in sys.stdin.readline().strip().split(' ')] # max_a = ma...
p02787
H,N = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] dp = [[10**18]*(N+1) for _ in range(H+1)] dp[0][0] = 0 for h in range(H+1): for i in range(N): dp[h][i+1] = min(dp[h][i], dp[max(0,h-AB[i][0])][i+1] + AB[i][1]) print((dp[H][N]))
inf = 10**9 H, N = list(map(int, input().split())) AB = [] for n in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(reverse=True, key=lambda x: x[0]) dp = [inf] * (H + 10 ** 4) dp[0] = 0 for a, b in AB: for h in range(H): if dp[h] != inf: tmp = dp[h...
p02787
import sys INF = float("inf") def main(): input = sys.stdin.readline H, N = list(map(int, input().split())) A = [0] * N B = [0] * N for i in range(N): A[i], B[i] = list(map(int, input().split())) dp = [INF] * (H + max(A)) dp[0] = 0 for h in range(H): f...
import sys input = sys.stdin.readline INF = float("inf") def main(): H, N = list(map(int, input().split())) A = [0] * N B = [0] * N for i in range(N): A[i], B[i] = list(map(int, input().split())) dp = [INF] * (H + max(A)) dp[0] = 0 for h in range(H): for...
p02787
H,N = list(map(int,input().split())) dp = [[[0,float('INF')]for i in range(H+1)] for _ in range(N+1)] #↑[ダメージ、MP] for i in range(N+1): dp[i][0][1]=0 # dp[i][j] -> iまでの魔法が使えるとき、j以上のダメージを与えるための最小限のMP for i in range(1,N+1): a,b = list(map(int,input().split())) for j in range(1,H+1): """ ...
H,N = list(map(int,input().split())) dp = [float("INF")]*(H+1) dp[0] = 0 for i in range(N): a,b= list(map(int,input().split())) for j in range(1,H+1): dp[j] = min(dp[j],dp[max((j-a),0)]+b) print((dp[-1]))
p02787
h,n = list(map(int, input().split())) ab = [] amax = 10**5 for _ in range(n): ab.append(tuple(map(int,input().split()))) amax = max(amax,ab[-1][1]) p = [10**10]*(h+amax+1) q = [(i,j) for i,j in ab] while q: a,b = q.pop() if b<p[a]: p[a]=b else: b=p[a] if a>=h: continue for i,j in ab: if ...
h,n = list(map(int, input().split())) ab = [] amax = 10**5 for _ in range(n): ab.append(tuple(map(int,input().split()))) amax = max(a for a,b in ab) p = [0]*(h+amax+1) for i in range(1,h+amax): p[i] = min(p[i-a]+b for a,b in ab) print((p[h]))
p02787
#E re H, N = list(map(int, input().split())) A = [0] * N B = [0] * N for i in range(N): A[i], B[i] = list(map(int, input().split())) DP = [float('inf')] * (H+1) DP[0] = 0 for i in range(N): for j in range(H): nj = min(j+A[i], H) DP[nj] = min(DP[nj], DP[j]+B[i]) print((int(DP[H]))...
H, N = list(map(int, input().split())) A = [0] * N B = [0] * N for i in range(N): A[i], B[i] = list(map(int, input().split())) DP = [0] * H for i in range(H): minM = float('inf') for j in range(N): if i+1 - A[j] >= 0: M = DP[i-A[j]] + B[j] else: M = B[j]...
p02787
H,N=list(map(int,input().split())) A=[0]*N B=[0]*N for i in range(N): A[i],B[i]=list(map(int,input().split())) INF=10**9 dp=[INF]*(H+1) # dp[x]:xダメージを与えるための最小魔力 dp[0]=0 for i in range(N): for j in range(len(dp)): if dp[j]!=INF: if j+A[i]<len(dp): if dp[j+A[i]]>dp[j]+B[i]: ...
H,N=list(map(int,input().split())) A=[0]*N B=[0]*N for i in range(N): A[i],B[i]=list(map(int,input().split())) INF=10**9 dp=[INF]*(H+max(A)) # dp[x]:xダメージを与えるための最小魔力 dp[0]=0 for i in range(N): for j in range(H): if dp[j]!=INF: if dp[j+A[i]]>dp[j]+B[i]: dp[j+A[i]]=dp[j]+B[i] print((...
p02787
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 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 I(): return int(sys.stdin....
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 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 I(): return int(sys.stdin....
p02787
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=998244353 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 LF(): return [float(x)...
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=998244353 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 LF(): return [float(x)...
p02787
H,N=list(map(int,input().split())) A=[] B=[] for i in range(N): a,b=list(map(int,input().split())) A.append(a) B.append(b) DP=[[1000000000]*(H+max(A)+1) for i in range(N+1)] for i in range(N): DP[i+1][A[i]]=B[i] for i in range(N): for h in range(H+max(A)+1): DP[i+1]...
H,N=list(map(int,input().split())) A=[] B=[] for i in range(N): a,b=list(map(int,input().split())) A.append(a) B.append(b) DP=[1000000000]*(H+max(A)+1) DP[0]=0 for i in range(N): DP[A[i]]=B[i] for i in range(N): for h in range(A[i],H+max(A)+1): DP[h]=min(DP[h-A[i]]+B...
p02787
h, n = [int(i) for i in input().split()] a, b = [0]*n, [0]*n eff = 0 eff_index = 0 for i in range(n): a[i], b[i] = [int(_) for _ in input().split()] if eff < a[i]/b[i]: eff = a[i]/b[i] eff_index = i max_kaisuu = - (-h//a[eff_index]) ans = float('inf') H = max_kaisuu * b[eff_inde...
h, n = [int(i) for i in input().split()] a, b = [0]*n, [0]*n eff = 0 eff_index = 0 for i in range(n): a[i], b[i] = [int(_) for _ in input().split()] max_a = max(a) ans = float('inf') H = h + max_a dp = [float('inf')] * (H+1) # dp[i] iのダメージを与える最小消費魔力 dp[0] = 0 for i in range(H+1): for j in ran...
p02787
def e_crested_ibis_vs_monster(): H, N = [int(i) for i in input().split()] Damage, Cost = [], [] for _ in range(N): a, b = [int(i) for i in input().split()] Damage.append(a) Cost.append(b) # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値 dp = [[float('inf')] * (H ...
def e_crested_ibis_vs_monster(): H, N = [int(i) for i in input().split()] Monsters = [[int(i) for i in input().split()] for j in range(N)] # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値 # としたとき、更新順序を工夫することで添字 i をなくせる dp = [0] + [float('inf')] * H for a, b in Monsters: for j...
p02787
h, n = list(map(int, input().split())) attack = [None] * n cost = [None] * n for i in range(n): a, c = list(map(int, input().split())) attack[i] = a cost[i] = c dp = [0] + [float('inf')] * h for i in range(1, h + 1): for j in range(n): dp[i] = min(dp[i], cost[j] + dp[max(0, i - attack[...
from sys import stdin h, n = list(map(int, stdin.readline().split())) attack = [None] * n cost = [None] * n for i in range(n): a, c = list(map(int, stdin.readline().split())) attack[i] = a cost[i] = c dp = [0] + [float('inf')] * h for i in range(1, h + 1): for j in range(n): dp[i] =...
p02787
###### Importing Libraries ###### import math import operator as op from functools import reduce from fractions import Fraction as frac ################################# ##### User defined function ##### ############ NCR ############### def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, list(rang...
###### Importing Libraries ###### import math import operator as op from functools import reduce from fractions import Fraction as frac ################################# ##### User defined function ##### ############ NCR ############### def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, list(rang...
p02787
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): H, N, *AB = list(map(int, read().split())) magic = [0] * N for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)): mag...
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): H, N, *AB = list(map(int, read().split())) dp = [INF] * (H + 1) dp[0] = 0 for a, b in zip(*[iter(AB)] * 2): ...
p02787
import sys read = sys.stdin.buffer.read INF = 1 << 60 def main(): H, N, *AB = list(map(int, read().split())) dp = [INF] * (H + 1) dp[0] = 0 for a, b in zip(*[iter(AB)] * 2): for i in range(H + 1): if dp[i] > dp[max(i - a, 0)] + b: dp[i] = dp[max(i ...
import sys read = sys.stdin.read INF = 1 << 60 def main(): H, N, *AB = list(map(int, read().split())) dp = [INF] * (H + 1) dp[0] = 0 for a, b in zip(*[iter(AB)] * 2): for i in range(H + 1): if i >= a: if dp[i] > dp[i - a] + b: ...
p02787
INF = 10 ** 9 def main(): H, N = list(map(int, input().split())) dp = [INF] * (H+1) dp[0] = 0 for _ in range(N): a, b = list(map(int, input().split())) for j in range(H): nj = min(j+a, H) dp[nj] = min(dp[nj], dp[j]+b) print((dp[H])) if _...
import sys readline = sys.stdin.readline INF = 10 ** 8 def main(): H, N = list(map(int, readline().rstrip().split())) dp = [INF] * (H + 1) # HPを減らすのに必要な最小のMP dp[0] = 0 for _ in range(N): hp, mp = list(map(int, readline().rstrip().split())) for i in range(H): ...
p02787
h,n=list(map(int,input().split())) a,b=[],[] for i in range(n): aa,bb=list(map(int,input().split())) a.append(aa) b.append(bb) inf=float("inf") dp=[[inf]*(h+1) for i in range(n+1)] dp[0][0]=0 for i in range(n): for j in range(h+1): dp[i+1][j]=min(dp[i+1][j],dp[i][j]) dp[i+1][min(h,j+a[i])]=m...
h,n=list(map(int,input().split())) a,b=[],[] for i in range(n): aa,bb=list(map(int,input().split())) a.append(aa) b.append(bb) inf=float("inf") dp=[[inf]*(h+1) for i in range(n+1)] dp[0][0]=0 for i in range(n): for j in range(h+1): dp[i+1][j]=min(dp[i+1][j],dp[i][j]) dp[i+1][min(j+a[i],h...
p02787
h,n=list(map(int,input().split())) a,b=[],[] for i in range(n): aa,bb=list(map(int,input().split())) a.append(aa) b.append(bb) inf=float("inf") dp=[[inf]*(h+1) for i in range(n+1)] dp[0][0]=0 for i in range(n): for j in range(h+1): dp[i+1][j]=min(dp[i+1][j],dp[i][j]) dp[i+1][min(j+a[i],h...
import sys input = sys.stdin.readline h, n = list(map(int, input().split())) a = [0]*n b = [0]*n for i in range(n): a[i] ,b[i] = list(map(int, input().split())) inf = float("inf") # dp[i][j]:i-1番目までの魔法で体力をj減らした時の消耗魔力の最大値 dp = [[inf]*(h+2) for i in range(n+1)] dp[0][0] = 0 for i in range(n): fo...
p02787
h,n = list(map(int, input().split())) x = [list(map(int, input().split())) for i in range(n)] #dp[v]:体力をv減らす時の最小魔力 dp=[10**20 for i in range(h+1)] dp[0]=0 for i in range(h+1): for j in range(n): if i-x[j][0]<0: dp[i] = min(dp[i], dp[0] + x[j][1]) else: dp[i]=m...
h,n= list(map(int, input().split())) a= [list(map(int, input().split())) for i in range(n)] dp=[float('inf')]*(h+1) dp[0]=0 for i in range(n): x=a[i][0] y=a[i][1] for j in range(h+1): if j>=x: dp[j]=min(dp[j-x]+y,dp[j]) else: dp[j]=min(y,dp[j]) print((...
p02787
h,n=list(map(int,input().split())) a,b=[],[] for i in range(n): a_sub,b_sub=list(map(int,input().split())) a.append(a_sub) b.append(b_sub) inf=10000000000000 ma=max(a) dp=[inf]*(h+1+ma) dp[0]=0 for i in range(n): x=[a[i],b[i]] for j in range(h): if dp[j]!=inf: if j+...
h,n=list(map(int,input().split())) ab=[list(map(int,input().split())) for i in range(n)] ab.sort(reverse=True) inf=1000000000 ma=ab[0][0] dp=[inf]*(h+1+ma) dp[0]=0 for i in range(n): x=ab[i] for j in range(h): if dp[j]!=inf: if j+x[0]<=h+ma: if j+x[0]>=h: ...
p02787
def DP(): H,N,*T = list(map(int,open(0).read().split())) #dpテーブルの設定と初期化 dp = [0] + [10 ** 9] * H #各魔法について for d,c in zip(*[iter(T)] * 2): #d,cはそれぞれ魔法ダメージと魔法のコスト #dpテーブルの各HPについて for j in range(H + 1): k = max(j - d,0) #ダメージjを与える時のコストについて、 #魔法iを新たに使った場合と、既存の最小コスト...
def main(): h,n,*t=list(map(int,open(0).read().split())) d=[0]+[10**18]*h for a,b in zip(*[iter(t)]*2): for j in range(h+1): k=j-a if k<0:k=0 c=d[k]+b if c<d[j]:d[j]=c print((d[h])) main()
p02787
h, n = list(map(int, input().split())) c = [0]*n m = [0]*n mc = 0 for i in range(n): c[i], m[i] = list(map(int, input().split())) if mc < c[i]: mc = c[i] dp = [10**10]*(h+mc) dp[0] = 0 for i in range(1, h + mc): for j in range(n): dp[i] = min(dp[i-c[j]] + m[j], dp[i]) ...
h, n = list(map(int, input().split())) c = [0]*n m = [0]*n mc = 0 for i in range(n): c[i], m[i] = list(map(int, input().split())) if mc < c[i]: mc = c[i] dp = [10**10]*(h+mc) dp[0] = 0 for i in range(1, h + mc): dp[i] = min(dp[i-a] + b for a, b in zip(c, m)) print((mi...
p02787
import sys input = sys.stdin.readline inf = 10**9 H, N = list(map(int, input().split())) AB = [] for n in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(reverse=True, key=lambda x: x[0]) dp = [inf] * (H + 10 ** 4) dp[0] = 0 for a, b in AB: for h in range(H): ...
inf = 10 ** 9 def main(): H, N = list(map(int, input().split())) AB = [] for n in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(reverse=True, key=lambda x: x[0]) dp = [inf] * (H + 10 ** 4) dp[0] = 0 for a, b in AB: for h in ...
p02787
inf = 10 ** 9 def main(): H, N = list(map(int, input().split())) AB = [] for n in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(reverse=True, key=lambda x: x[0]) dp = [inf] * (H + 10 ** 4) dp[0] = 0 for a, b in AB: for h in ...
def main(): inf = 10**9 H, N = list(map(int, input().split())) AB = [] for n in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(reverse=True, key=lambda x: x[0]) dp = [inf] * (H + 10 ** 4) dp[0] = 0 for a, b in AB: for h in ra...
p02787
import sys sys.setrecursionlimit(10**9) INF = 10**20 def main(): H,N = list(map(int,input().split())) A,B = [],[] for _ in range(N): a,b = list(map(int,input().split())) A.append(a) B.append(b) tp = 3 * 10**4 dp = [INF] * tp dp[0] = 0 minim = INF ...
import sys sys.setrecursionlimit(10**9) INF = 10**20 def main(): H,N = list(map(int,input().split())) A,B = [],[] for _ in range(N): a,b = list(map(int,input().split())) A.append(a) B.append(b) tp = 3 * 10**4 dp = [INF] * tp dp[0] = 0 ans = INF f...
p02787
n=int(eval(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]>0: count+=a[i] if a[i+1]>(b[i]-a[i]): a[i+1]-=(b[i]-a[i]) count+=(b[i]-a[i]) elif a[i+1]<=b[i]-a[i]: count+=a[i+1] a[i+1]=0 elif b[i]<=a[i]:...
n=int(eval(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: if b[i]<=a[i]+a[i+1]: ans+=b[i] a[i+1]-=(b[i]-a[i]) if a[i+1]<0: a[i+1]=0 else: if b[i]>a[i]+a[i+1]: ans+=...
p02959
R = lambda: list(map(int, input().split())) n = int(eval(input())) a, b = R(), R() r = 0 for i in range(n): x = min(a[i], b[i]) b[i] -= x y = min(a[i + 1], b[i]) a[i + 1] -= y r += x + y print(r)
R = lambda: list(map(int, input().split())) n = int(eval(input())) a, b = R(), R() r = 0 for i in range(n): x = min(a[i], b[i]) y = min(a[i + 1], b[i] - x) a[i + 1] -= y r += x + y print(r)
p02959
R = lambda: list(map(int, input().split())) eval(input()) a, b = list(R()), R() r = 0 for i, u in enumerate(b): x = min(a[i], u) y = min(a[i + 1], u - x) a[i + 1] -= y r += x + y print(r)
R = lambda: list(map(int, input().split())) eval(input()) a, b = list(R()), R() r = y = 0 for u, v, w in zip(b, a, a[1:]): x = min(v - y, u) y = min(w, u - x) r += x + y print(r)
p02959
from itertools import tee R = lambda: list(map(int, input().split())) eval(input()) (a, b), c = tee(R()), R() next(b) r = y = 0 for u, v, w in zip(c, a, b): x = min(v - y, u) y = min(w, u - x) r += x + y print(r)
R = lambda: list(map(int, input().split())) eval(input()) a = list(R()) r = y = 0 for u, v, w in zip(R(), a, a[1:]): x = min(v - y, u) y = min(w, u - x) r += x + y print(r)
p02959
R = lambda: list(map(int, input().split())) eval(input()) a = list(R()) def f(): y = 0 for u, v, w in zip(R(), a, a[1:]): x = min(v - y, u) y = min(w, u - x) yield x + y print((sum(f())))
R = lambda: list(map(int, input().split())) eval(input()) a = list(R()) def f(): s = 0 for x, y, z in zip(R(), a, a[1:]): t = min(x, y - s) s = min(z, x - t) yield s + t print((sum(f())))
p02959
n = int(eval(input())) a_lis = list(map(int,input().split())) b_lis = list(map(int,input().split())) ans = 0 for i in range(n): if b_lis[i] <= a_lis[i]: ans += b_lis[i] else: ans += a_lis[i] b_lis[i] -= a_lis[i] if b_lis[i] <= a_lis[i+1]: ans += b_lis[i] ...
n = int(eval(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] x = b[i] -a[i] if x >= a[i+1]: ans += a[i+1] a[i+1] = 0 else: ans += x a[i+1] = a[i+1] -x print(ans)
p02959
# ABC135C - City Savers # greedy algorithm def main(): n = int(eval(input())) A = list(map(int, input().rstrip().split())) B = tuple(map(int, input().rstrip().split())) ans = 0 for i in range(n): cur1 = min(A[i], B[i]) cur2 = min(A[i + 1], B[i] - cur1) A[i] -= cur1 ...
# ABC135C - City Savers # greedy algorithm def main(): n = int(eval(input())) A = tuple(map(int, input().rstrip().split())) B = tuple(map(int, input().rstrip().split())) ans, a1 = 0, A[0] for a2, b in zip(A[1:], B): cnt1 = min(a1, b) cnt2 = min(a2, b - cnt1) ans += ...
p02959
# ABC135C - City Savers # greedy algorithm def main(): n = int(eval(input())) A = tuple(map(int, input().split())) B = tuple(map(int, input().split())) ans, a1 = 0, A[0] for a2, b in zip(A[1:], B): cnt1 = a1 if a1 < b else b cnt2 = a2 if a2 < b - cnt1 else b - cnt1 ...
# ABC135C - City Savers # greedy algorithm def main(): n = int(eval(input())) A = tuple(map(int, input().split())) B = tuple(map(int, input().split())) ans, a1 = 0, A[0] for a2, b in zip(A[1:], B): cnt1 = a1 if a1 < b else b x = b - cnt1 cnt2 = a2 if a2 < x else x ...
p02959
n = int(input()) a = [int(i) for i in input().split(' ')] b = [int(i) for i in input().split(' ')] ret = 0 for i in range(n): e = min(a[i], b[i]) ret += e a[i] -= e b[i] -= e e = min(a[i + 1], b[i]) ret += e a[i + 1] -= e b[i] -= e print(ret)
N = int(eval(input())) A = [int(s) for s in input().split(' ')] B = [int(s) for s in input().split(' ')] + [0] m = 0 pa = 0 for a, b in zip(A, B): m1 = 0 if pa > 0: m1 = min(pa, a) a -= m1 m2 = min(a, b) pa = b - m2 m += m1 + m2 print(m)
p02959
n=int(eval(input())) a=list(map(int, input().split())) b=list(map(int, input().split())) ans=0 tmp=0 for i in range(n-1,-1,-1): if(a[i+1] >= b[i]+tmp): ans+=b[i]+tmp tmp=0 elif(a[i+1] < tmp): ans+=a[i+1] tmp=b[i] else: ans+=a[i+1] tmp=b[i]+tmp-...
n=int(eval(input())) a=list(map(int,input().split())) b=list(map(int,input().split())) ans=0 rmn=0 for i in range(n): if a[i]<b[i]+rmn: if rmn>a[i]: rmn=b[i] else: rmn=rmn+b[i]-a[i] ans+=a[i] else: ans+=b[i]+rmn rmn=0 ans+=min(a[-1...
p02959
def main(): import sys n = int(eval(input())) a = tuple(map(int,sys.stdin.readline().split())) b = tuple(map(int,sys.stdin.readline().split())) count=0 buffer=0 for c,h in zip(a,b): count += min(buffer,c) g = max(0,c-buffer) count += min(h,g) buffer=max(0,h-g) else: ...
def main(): import sys n = int(eval(input())) a = list(map(int,sys.stdin.readline().split())) b = list(map(int,sys.stdin.readline().split())) count=0 buffer=0 for c,h in zip(a,b): count += min(buffer,c) g = max(0,c-buffer) count += min(h,g) buffer=max(0,h-g) else: ...
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = 0 for i in range(len(b)): # print('倒せる数', b) # print('倒せる街にいるモンスターの数',a) for j in range(b[i]): if a[i] > 0: b[i] -= 1 a[i] -= 1 c += 1 elif a[i] == 0 and a[i+1] > 0: b[...
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = 0 for i in range(len(b)): if b[i] > a[i]: c += a[i] b[i] = b[i] - a[i] a[i] = 0 else: c += b[i] a[i] = a[i] - b[i] b[i] = 0 if a[i] == 0: if b[i] > a[i+1]: c ...
p02959
import math import re import copy import sys n = int(eval(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]: c += min(a[i+1],b[i]-a[i]) a[i+1] -= min(a[i+1],b[i]-a[i]) print(c)
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = 0 for i in range(n-1,-1,-1): ans += min(a[i]+a[i+1],b[i]) a[i] = max(a[i]-max(b[i]-a[i+1],0),0) print(ans)
p02959
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = 0 l = A[0] for i in range(N): r = A[i + 1] cur = B[i] # print(l, r, cur) ans += min(l + r, cur) l = max(r - max(cur - l, 0), 0) # めちゃくちゃ整理するとこの形の式になる print(ans)
# https://atcoder.jp/contests/abc135/tasks/abc135_c N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = 0 l = A[0] for i in range(N): r = A[i + 1] cur = B[i] # print(l, r, cur) # ans += min(l + r, cur) # l = max(r - max(cur - l, 0), 0) #...
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 for i in reversed(list(range(n+1))): if i == n: temp = min(a[i], b[i-1]) ans += temp b[i-1] -= temp elif i == 0: ans += min(a[i], b[i]) else: temp1 = min(a[i], b[i]) ans ...
n = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = 0 for i in reversed(list(range(1, n+1))): if A[i] <= B[i-1]: ans += A[i] B[i-1] -= A[i] if A[i-1] >= B[i-1]: ans += B[i-1] A[i-1] -= B[i-1] else: ...
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 t = 0 for i in range(n): t = a[i] if t >= b[i]: t -= b[i] ans += b[i] else: ans += t b[i] -= t t = 0 if b[i] <= a[i+1]: ans += b[i] a[i+1] -= b[i] else:...
n = int(eval(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 b[i] <= a[i+1]: a[i+1] -= b[i] ans += b[i] else: ans += a[i+1] a[i+1] ...
p02959
def solve(): n = int(eval(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] + A[i + 1]) A[i + 1] = min(A[i + 1], max(A[i] + A[i + 1] - B[i], 0)) print(ans) solve()
def solve(): n = int(eval(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(B[i], x) A[i + 1] = min(A[i + 1], max(x - B[i], 0)) print(ans) solve()
p02959
n=int(eval(input())) A=list(map(int,input().split())) B=list(map(int,input().split())) sum=0 for i in range(n-1): if B[i]-A[i]>0: if A[i+1]-(B[i]-A[i])<0: # print(A[i]+A[i+1]) sum+=A[i]+A[i+1] A[i+1]=0 else: A[i+1]+=-(B[i]-A[i])...
n=int(eval(input())) A=list(map(int,input().split())) B=list(map(int,input().split())) sum=0 for i in range(n-1): x=B[i]-A[i] if x>0: if A[i+1]-(x)<0: # print(A[i]+A[i+1]) sum+=A[i]+A[i+1] A[i+1]=0 else: A[i+1]+=-(x) ...
p02959
n = int(eval(input())) a = [int(e) for e in input().split()] b = [int(e) for e in input().split()] result = 0 for i in range(n): if a[i] > b[i]: result += b[i] a[i] -= b[i] b[i] = 0 else: result += a[i] b[i] -= a[i] a[i] = 0 if a[i + 1] > b[i]: result += b[i] a...
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) result = 0 for i in range(N): if B[i] >= A[i]: result += A[i] B[i] -= A[i] A[i] = 0 if B[i] >= A[i+1]: result += A[i+1] B[i] -= A[i+1]...
p02959
N = int( eval(input()) ) A, B = list( [int(x) for x in input().split(" ")] ), list( [int(x) for x in input().split(" ")] ) #A = monsters. B = number of to be able to kill monsters.殺せる数 def kill_monsters(Ai, Bi): Ai -= Bi Bi = 0 if Ai < 0: Bi += -1*Ai Ai = 0 return (Ai...
def read(): N = int(eval(input())) A = list( map(int, input().split(" ")) ) B = list( map(int, input().split(" ")) ) return (N, A, B) def kill(A, B): if B < A: A = A - B B = 0 else: B = B - A A = 0 return (A, B) def main(N, A, B): ...
p02959
N=int(eval(input())) A=[int(x) for x in input().rstrip().split()] B=[int(x) for x in input().rstrip().split()] ans=0 now=A[0] for i in range(N): if B[i]<=A[i]: ans+=B[i] else: 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]=A[i+1]-(B[i]-A[i]) ...
n=int(eval(input())) a=[int(x) for x in input().rstrip().split()] b=[int(x) for x in input().rstrip().split()] ans=0 for i in range(len(b)): if a[i]+a[i+1]<=b[i]: ans+=a[i]+a[i+1] a[i]=0 a[i+1]=0 elif a[i]<=b[i]: ans+=a[i] ans+=(b[i]-a[i]) a[i+1]=a[i+1]-(b[i]-a[i]) else: ...
p02959
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) num = 0 for i in range(n): if a[i] >= b[i]: # print(a[i],b[i]) a[i] -= b[i] num += b[i] b[i] = 0 elif a[i] < b[i]: num += a[i] #a[i] = 0 b[...
n = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) tot = sum(A) for i in range(n): en = A[i] + A[i+1] A[i] = max(A[i] -B[i],0) if A[i] == 0: A[i+1] = max(en - B[i],0) #print(A[i],A[i+1]) print((tot-sum(A)))
p02959
N = int(eval(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 += A[i] B[i] -= A[i] A[i] = 0 if A[i + 1] <= B[i]: ans += A[i + 1] B[i] -= A[i + 1] A[i + 1...
N = int(eval(input())) B = list(map(int, input().split())) A = list(map(int, input().split())) ans = 0 for i in range(N): a = A[i] if a >= B[i]: ans += B[i] a -= B[i] else: ans += a a = 0 if a >= B[i + 1]: ans += B[i + 1] B[i + 1] = 0 ...
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) b.append(0) ans = 0 for i in range(n): beat = min(a[i], b[i]) a[i] -= beat b[i] -= beat if i != n: beat2 = min(a[i + 1], b[i]) a[i + 1] -= beat2 ans += beat + beat2 print(ans)
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) b.append(0) ans = 0 for i in range(n): beaten = min(a[i], b[i]) b[i] -= beaten beaten2 = min(a[i + 1], b[i]) a[i + 1] -= beaten2 ans += beaten + beaten2 print(ans)
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) b.append(0) ans = 0 for i in range(n): beaten = min(a[i], b[i]) b[i] -= beaten beaten2 = min(a[i + 1], b[i]) a[i + 1] -= beaten2 ans += beaten + beaten2 print(ans)
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 for i in range(n): # 正面の町を救う beaten = min(a[i], b[i]) b[i] -= beaten # 隣町を救う beaten2 = min(a[i + 1], b[i]) a[i + 1] -= beaten2 ans += beaten + beaten2 print(ans)
p02959
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] ans = 0 for i in range(N): if A[i] < B[i]: B[i] -= A[i] ans += A[i] if A[i+1] < B[i]: ans += A[i+1] A[...
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] ans = 0 for i in range(N): if A[i] < B[i]: B[i] -= A[i] ans += A[i] if A[i+1] < B[i]: ans += A[i+1] A[...
p02959
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] ans = 0 for i in range(N)[::-1]: if A[i+1] >= B[i]: ans += B[i] A[i+1] -= B[i] B[i] = 0 else: ans += A[i+1] ...
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] ans = 0 for i in range(N): if A[i] >= B[i]: ans += B[i] A[i] -= B[i] B[i] = 0 else: ans += A[i] B[i] ...
p02959
import sys, heapq, bisect, math, fractions from collections import deque N = int(eval(input())) A = list(map(int, sys.stdin.readline().rsplit())) B = list(map(int, sys.stdin.readline().rsplit())) res = 0 for i, b in enumerate(B): a = A[i] a1 = A[i + 1] if a >= b: res += b else: ...
import sys N = int(eval(input())) A = list(map(int, sys.stdin.readline().rsplit())) B = list(map(int, sys.stdin.readline().rsplit())) res = 0 for i, b in enumerate(B): a = A[i] a1 = A[i + 1] if a >= b: res += b else: res += a if a1 >= b - a: A[i + 1] ...
p02959
n=int(eval(input())) a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] ans=0 for i in range(n): m=min(a[i],b[i]) ans+=m a[i]-=m b[i]-=m m=min(a[i+1],b[i]) a[i+1]-=m b[i]-=m ans+=m print(ans)
from sys import stdin,stdout if __name__=="__main__": n=int(stdin.readline()) a=[int(x) for x in stdin.readline().split()] b=[int(x) for x in stdin.readline().split()] cnt=0 for i in range(n): if(a[i]>=b[i]): cnt+=b[i] else: cnt+=a[i] ...
p02959
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) c = 0 for i in range(n): if a[i] >= b[i]: c += b[i] else: d = b[i] -a[i] if a[i+1] <= d: c += a[i] + a[i+1] a[i+1] = 0 else: a[i+1] -= d c += b[i] print(c)
def main(): n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) c = 0 for i in range(n): if a[i] >= b[i]: c += b[i] else: d = b[i] -a[i] if a[i+1] <= d: c += a[i] + a[i+1] a[i+1] = 0 else: a[i+1] -= ...
p02959
n = int(eval(input())) enemy = list(map(int, input().split())) beat = list(map(int, input().split())) ans = 0 for i in range(n): #左を全力で倒す left = min([enemy[i], beat[i]]) ans += left enemy[i] -= left beat[i] -= left right = min([enemy[i+1], beat[i]]) ans += right enemy[i + 1] -= right ...
n = int(eval(input())) enemy = list(map(int, input().split())) beat = list(map(int, input().split())) ans = 0 for i in range(n): #左を全力で倒す left = min([enemy[i], beat[i]]) ans += left beat[i] -= left right = min([enemy[i+1], beat[i]]) ans += right enemy[i + 1] -= right beat[i] -= right ...
p02959
n = int(eval(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] : if a[i+1] >= (b[i]-a[i]) : 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...
n = int(eval(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] : if a[i+1] >= (b[i]-a[i]) : 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 +=...
p02959
N = int(eval(input())) A = list(map(int, input().split())) # N + 1 B = list(map(int, input().split())) # N # 重みがないのですべて小さい方から倒せると仮定する cnt = 0 for i in range(N): cnt += min(B[i], A[i]) diff = B[i] - A[i] if diff > 0: if A[i+1] < diff: cnt += A[i+1] A[i+1] = 0 ...
N = int(eval(input())) A = list(map(int, input().split())) # N + 1 B = list(map(int, input().split())) # N # 重みがないのですべて小さい方から倒せると仮定する cnt = 0 for i in range(N): cnt += min(B[i], A[i]) diff = B[i] - A[i] if diff > 0: if A[i+1] < diff: cnt += A[i+1] A[i+1] = 0 ...
p02959
# -*- coding: utf-8 -*- ############# # Libraries # ############# import sys input = sys.stdin.readline import math #from math import gcd import bisect from collections import defaultdict from collections import deque from functools import lru_cache ############# # Constants # ############# M...
# -*- coding: utf-8 -*- ############# # Libraries # ############# import sys input = sys.stdin.readline import math #from math import gcd import bisect from collections import defaultdict from collections import deque from functools import lru_cache ############# # Constants # ############# M...
p02959
from collections import defaultdict if __name__=="__main__": brave_n = int(eval(input())) monster_dic = defaultdict(int) brave_dic = defaultdict(int) monster_n = list(map(int, input().split())) brave_can = list(map(int, input().split())) kill_count = 0 for i, m in enumerate(mon...
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) d_sum = sum(A) for i in range(N): if A[i] >= B[i]: A[i] = A[i] - B[i] else: B[i] -= A[i] A[i] = 0 if A[i+1] >= B[i]: A[i+1] -= B[i] B[i] = 0 ...
p02959
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) temp = 0 for i, j in enumerate(A): if i == N: break if j >= B[i]: temp += B[i] B[i] = 0 continue else: temp += j B[i] = B[i] - j if A[i + 1] ...
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = 0 for i in range(N): # i番目の街のモンスターを倒す ans += min(A[i], B[i]) nb = B[i] - min(A[i], B[i]) # i+1番目の街のモンスターを倒す ans += min(A[i+1], nb) A[i+1] -= min(A[i+1], nb) print(ans)
p02959
n = int(eval(input())) a_li = [int(i) for i in input().split()] b_li = [int(i) for i in input().split()] if b_li[0]<b_li[-1]: a_li = a_li[::-1] b_li = b_li[::-1] ans = 0 for i in range(0,n): attack = min(a_li[i],b_li[i]) attack2 = min(a_li[i+1],b_li[i]-min(a_li[i],b_li[i])) a_li[i]-=at...
n = int(eval(input())) a_input = list(map(int,input().split())) b_input = list(map(int,input().split())) ans=0 for i in range(n): first_attack=0 second_attack=0 first_attack = min(b_input[i],a_input[i]) a_input[i]-=first_attack if first_attack<b_input[i]: second_attack = min(a_...
p02959
N = int(eval(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 ...
N = int(eval(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] if A[i] < B[i]: A[i+1] -= (B[i] - A[i]) else: ko += A[i] + A[i+1] A[i+1] = 0 print(ko)
p02959
a = int(eval(input())) b = list(map(int, input().split())) c = list(map(int, input().split())) d = 0 for i in range(a-1, -1, -1): e = min(b[i+1], c[i]) d += e b[i+1] -= e c[i] -= e e = min(b[i], c[i]) d += e b[i] -= e c[i] -= e print(d)
a = int(eval(input())) b = list(map(int, input().split())) c = list(map(int, input().split())) e = 0 for i in range(a): d = min(c[i], b[i]) e += d c[i] -= d b[i] -= d d = min(c[i], b[i+1]) e += d c[i] -= d b[i+1] -= d print(e)
p02959
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) N = int(eval(input())) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] before = sum(A) for i in range(N): nokori = B[i] for j in range(i,i+2): if nokori >= A[j]: nokori -= A[j] ...
N = int(eval(input())) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] cnt = 0 for i in range(N): if B[i] >= A[i]: cnt += A[i] B[i] -= A[i] else: cnt += B[i] B[i] = 0 if B[i] == 0: continue if B[i] >= A[i+1]: cnt...
p02959
n=int(eval(input())) a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] k=0 for i,j in enumerate(b): if a[i]+a[i+1]<=j: k+=a[i]+a[i+1] a[i],a[i+1]=0,0 elif a[i]<=j: k+=j a[i],a[i+1]=0,a[i]+a[i+1]-j else: k+=j a[i]-=j print(k)
n=int(eval(input())) a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] ans=0 mon1=a[0] mon2=a[0]+a[1] for i in range(n): if b[i] <= mon1: ans+=b[i] mon1=a[i+1] if i<=n-2: mon2=a[i+1]+a[i+2] elif mon1<b[i]<=mon2: ans+=b[i] mon1=mon2-b[i] #こ...
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = 0 for i in range(len(b)): c_i = 0 c_i = min(a[i], b[i]) c += c_i b[i] -= c_i c_i_1 = 0 c_i_1 = min(a[i+1], b[i]) c += c_i_1 a[i+1] -= c_i_1 print(c)
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) c = 0 for i in range(n): ci = 0 ci = min(a[i],b[i]) c += ci b[i] -= ci ci1 = 0 ci1 = min(a[i+1],b[i]) c += ci1 a[i+1] -= ci1 print(c)
p02959
n=int(eval(input())) ai=[] for i in map(int,input().split()): ai.append(i) bi=[] for i in map(int,input().split()): bi.append(i) asum=0 for i in range(n+1): asum=asum+ai[i] i=0 if ai[i]<=bi[i]: bi[i]=bi[i]-ai[i] ai[i]=0 else : ai[i]=ai[i]-bi[i] bi[i]=0 for i in range...
n=int(eval(input())) a=list(map(int,input().split())) b=list(map(int,input().split())) fii=min(a[0],b[0]) fij=min(a[1],b[0]-fii) icnt=fii+fij for i in range(1,n): fii=min(a[i]-fij,b[i]) fij=min(a[i+1],b[i]-fii) icnt+=fij+fii print(icnt)
p02959
import sys sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N = ir() A = lr() B = lr() answer = 0 for i in range(N): a, b = A[i], B[i] answer += min(a, b) if a < b: remain = b - a add = min(A[i+1], remain) a...
# coding: utf-8 import sys sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N = ir() A = lr() B = lr() remain = 0 answer = 0 for i in range(N): attack = min(A[i], remain) answer += attack A[i] -= attack if A[i] > 0: at...
p02959
N = int(eval(input())) M = list(map(int, input().split())) H = list(map(int, input().split())) sum_m = 0 killed = 0 rest = 0 for i in range(0, N): killed = min(H[i]+rest, M[i]) rest = min(H[i]+rest-killed, H[i]) sum_m += killed sum_m += min(rest, M[-1]) print(sum_m)
N = int(eval(input())) M = list(map(int, input().split())) H = list(map(int, input().split())) sum_m = 0 rest = 0 for i in range(N): if M[i] >= H[i]+rest: sum_m += H[i]+rest rest = 0 else: sum_m += M[i] rest = min(H[i], H[i]+rest-M[i]) sum_m += min(rest, M[-1]) print(sum_m)
p02959
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) s = 0 for i in range(N): if A[i]+A[i+1]<=B[i]: s = s + A[i] + A[i+1] A[i]=0 A[i+1] = 0 elif A[i] <= B[i] and A[i] + A[i+1] > B[i]: s = s + B[i] A[i+1] = A[i+1] + A[i] - B[i] A[i] = 0 ...
N = int(eval(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 += A[i] B[i] -= A[i] if A[i + 1] < B[i]: ans += A[i + 1] A[i + 1] = 0 else: ans += B[i] ...
p02959
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) cnt = 0 for i in range(N): cnt += min(A[i],B[i]) a = A[i] A[i] = max(a-B[i],0) B[i] = max(B[i]-a,0) cnt += min(A[i+1],B[i]) A[i+1] = max(A[i+1]-B[i],0) print(cnt)
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) tot = 0 c = 0 for i in range(N): a = A[i] b = B[i] if a>=c: a -= c tot += c if a>=b: tot += b c = 0 else: tot += a c = ...
p02959
# 初案のアルゴリズムが違っていた。難しかった。 n=int(eval(input())) a=list(map(int,input().split())) b=list(map(int,input().split())) b.append(0) #ダミー z = 0 c = b[0] for i in range(n+1): x = a[i] - c if i < n: if x <= 0: z += a[i] if -x >= a[i+1]: c = b[i+1] + a[i+1] ...
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) + [0] bonus = 0 x = 0 for i in range(n+1): enemy = max(0, a[i] - bonus) tmp = enemy - b[i] if tmp <= 0: x += a[i] bonus = -tmp else: x += (b[i] + bonus) bonus = 0 ...
p02959
def LI(): return [int(s) for s in input().split()] N = int(eval(input())) A_s = LI() B_s = LI() counter = 0 for i in range(N): if A_s[i] > B_s[i]: counter+=B_s[i] elif B_s[i] < A_s[i] + A_s[i+1]: counter+=B_s[i] A_s[i+1] = A_s[i+1] - (B_s[i]-A_s[i]) else: counter+=A_s[i]+A_s...
N = int(eval(input())) A = [int(s) for s in input().split()] sum_A = sum(A) B = [int(s) for s in input().split()] for i in range(N): attack1 = A[i] - B[i] if attack1 < 0: attack2 = A[i + 1] + attack1 A[i] = 0 A[i + 1] = attack2 if attack2 >= 0 else 0 else: A[i] = a...
p02959
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) start = [0]*(N+1) attack = 0 enemy = 0 for i in range(N+1): start[i] = A[i] for i in range(N): attack = min(A[i],B[i]) A[i] = A[i] - attack B[i] = B[i] - attack attack = min(A[i+1],B[i]) ...
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) ans = 0 for i in range(N): tmp = 0 if A[i] >= B[i]: A[i] -= B[i] ans += B[i] else: ans += A[i] tmp = B[i] - A[i] A[i] = 0 if A[i+1] >= tmp: ...
p02959
#C n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) max_num = 0 for i in range(n): if a[i]-b[i] >= 0: a[i] -= b[i] max_num += b[i] elif a[i]-b[i] < 0: max_num += a[i] b[i] -= a[i] if a[i+1] - b[i] < 0: ...
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = 0 for i in range(n): if a[i] - b[i] >= 0: a[i] -= b[i] ans += b[i] b[i]=0 else: ans += a[i] b[i] -= a[i] a[i] = 0 if a[i+1] - b[i] >= 0: ...
p02959
N = int(eval(input())) m = list(map(int, input().split())) b = list(map(int, input().split())) s = 0 for i in range(N): l = min(m[i],b[i]) s += l m[i] -= l b[i] -= l r = min(m[i+1],b[i]) s += r m[i+1] -= r print(s)
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) c = 0 for i in range(n): if a[i]>=b[i]: c+=int(b[i]) else: if (a[i]+a[i+1])>=b[i]: c+=int(b[i]) a[i+1]=a[i]+a[i+1]-b[i] else: c+=int(a[i]+a[i+1]) a[i+1]=0 print(c)
p02959
N = int(eval(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)
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = sum(A) for i in range(N): n = min(A[i], B[i]) A[i] -= n B[i] -= n if B[i] > 0: A[i+1] = max(0, A[i+1]-B[i]) print((ans - sum(A)))
p02959
inf = 10 ** 18 P = 10 ** 9 + 7 N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) A_all = sum(A) xi = min(A[0], B[0]) A[0] -= xi B[0] -= xi for i in range(N): yi = min(B[i], A[i + 1]) A[1 + i] -= yi B[i] -= yi # if i < N - 1: x...
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = 0 for i in range(N): left = min(A[i], B[i]) A[i] -= left B[i] -= left ans += left right = min(A[i + 1], B[i]) A[i + 1] -= right B[i] -= right ans += right print(ans)
p02959
n,*a=list(map(int,open(0).read().split())) c=0 for i in range(1,n+1):c+=min(a[i-1]+a[i],a[n+i]);a[i]-=max(min(a[n+i]-a[i-1],a[i]),0) print(c)
n,x,*a=list(map(int,open(0).read().split())) c=0 for a,b in zip(a,a[n:]):c+=min(x+a,b);x=a-max(min(b-x,a),0) print(c)
p02959
n=int(eval(input())) a=list(map(int, input().split())) b=list(map(int, input().split())) knock=0 ex=0 for i in range(n): if a[i] < b[i]: ex=b[i]-a[i] knock+=a[i] elif a[i] >= b[i]: ex=0 knock+=b[i] if ex != 0: if a[i+1] - ex <= 0: knock+=...
n = int(eval(input())) *a, = list(map(int, input().split())) *b, = list(map(int, input().split())) knock = 0 ex = 0 for i in range(n): if a[i] < b[i]: ex = b[i] - a[i] knock += a[i] else: ex = 0 knock += b[i] if ex != 0: if a[i+1] - ex <= 0: ...
p02959
n = int(eval(input())) *a, = list(map(int, input().split())) *b, = list(map(int, input().split())) knock = 0 ex = 0 for i in range(n): if a[i] < b[i]: ex = b[i] - a[i] knock += a[i] else: ex = 0 knock += b[i] if ex != 0: if a[i+1] - ex <= 0: ...
n = int(eval(input())) *a, = list(map(int, input().split())) *b, = list(map(int, input().split())) knock = 0 ex = 0 for i in range(n): if a[i] < b[i]: ex = b[i] - a[i] knock += a[i] elif a[i] >= b[i]: ex = 0 knock += b[i] if ex != 0: if a[i+1] < ex: ...
p02959
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) z = 0 for i in range(N): if B[i]<=A[i]: z+=B[i] elif A[i]<B[i]<=A[i]+A[i+1]: z+=B[i] A[i+1]-=B[i]-A[i] else: z+=A[i]+A[i+1] A[i+1]=0 print(z)
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) ans = 0 for n in range(N): if B[n]<=A[n]: ans+=B[n] elif B[n]<=A[n]+A[n+1]: ans+=B[n] A[n+1]-=B[n]-A[n] else: ans+=A[n]+A[n+1] A[n+1] = 0 print(ans)
p02959
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) ans = 0 for i in range(N): p = min(A[i], B[i]) ans += p B[i] -= p p = min(A[i+1],B[i]) ans += p A[i+1] -= p print(ans)
N = int(eval(input())) A = list(map(int,input().split())) B = list(map(int,input().split())) ans = 0 for i,b in enumerate(B): x = min(A[i], b) ans += x b -= x y = min(A[i+1], b) ans += y A[i+1] -= y print(ans)
p02959
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans, surplus = 0, 0 for i in range(N): tmp = min(A[i], surplus) ans += tmp A[i] -= tmp if B[i] > A[i]: ans += A[i] surplus = B[i] - A[i] else: ans += B[i] sur...
N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) ans, mod = 0, 0 for i in range(N): if mod > A[i]: ans += A[i] A[i] = 0 else: ans += mod A[i] -= mod mod = 0 if B[i] > A[i]: ans += A[i] ...
p02959
import math n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = 0 for i in reversed(list(range(n))): if a[i+1] - b[i] <= 0: ans += a[i+1] b[i] -= a[i+1] a[i+1] = 0 if a[i] - b[i] <= 0: ans += a[i] b[i] -= a[i] a[i] = 0 else: ans ...
n = int(eval(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] if a[i+1] < b[i] - a[i]: ans += a[i+1] a[i+1] = 0 else: ans += b[i] - a[i] a[i+1] -= b[i] - a[i] print(ans)
p02959
import sys def main(): n, *lst = list(map(int, sys.stdin.read().split())) a = lst[n::-1] b = lst[-1:n:-1] res = 0 for i in range(n): if i == 0: tmp = min(b[i], a[i]) res += tmp a[i] -= tmp b[i] -= tmp else: tmp = min(b[i] + b[i - 1], a[i]) res += tmp ...
import sys def main(): n, *lst = list(map(int, sys.stdin.read().split())) a = lst[:n+1] b = lst[n+1:] res = 0 for i in range(n): if i == 0: tmp = min(a[i], b[i]) res += tmp b[i] -= tmp else: tmp = min(a[i], b[i] + b[i - 1]) res += tmp b[i] -= min(b[i],...
p02959
from collections import deque from sys import stdin def main(): lines = stdin.readlines() A = deque(list(map(int, lines[1].split()))) B = deque(list(map(int, lines[2].split()))) cnt = 0 ans = 0 while B: a = A.pop() b = B.pop() tmp = a if a < b else b ...
from sys import stdin def main(): lines = stdin.readlines() A = tuple(map(int, lines[1].split())) B = tuple(map(int, lines[2].split())) pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 = a if a < b else b ans...
p02959
from sys import stdin readline = stdin.readline def main(): readline() A = tuple(map(int, readline().split())) B = tuple(map(int, readline().split())) pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 = a if a < b e...
from sys import stdin readline = stdin.readline def main(): eval(input()) A = tuple(map(int, readline().split())) B = tuple(map(int, readline().split())) pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 = a if a < ...
p02959
from sys import stdin def main(): eval(input()) readline = stdin.readline A = tuple(map(int, readline().split())) B = tuple(map(int, readline().split())) pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 = a if a ...
from sys import stdin def main(): readline = stdin.readline readline() A = tuple(map(int, readline().split())) B = tuple(map(int, readline().split())) pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 = a if a < b...
p02959
from sys import stdin readline = stdin.readline def read(): return list(map(int, readline().split())) def main(): eval(input()) *A, = read() *B, = read() pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 =...
from sys import stdin def main(): readline = stdin.readline readline() *A, = list(map(int, readline().split())) *B, = list(map(int, readline().split())) pre = A[0] ans = 0 for a, b in zip(A[1:], B): tmp1 = pre if pre < b else b b -= tmp1 tmp2 = a if a <...
p02959
N = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) a.reverse() b.reverse() ans = 0 capacity = 0 for i in range(N): ans += min(b[i] + capacity, a[i]) capacity = max(b[i] - max(a[i] - capacity, 0), 0) ans += min(a[-1], capacity) print(ans)
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) a = a[::-1] b = b[::-1] ans = 0 for i, bi in enumerate(b): ans += min(a[i], b[i]) remain = max(0, b[i] - a[i]) ans += min(a[i + 1], remain) a[i + 1] = max(0, a[i + 1] - remain) print(ans)
p02959
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) a = a[::-1] b = b[::-1] ans = 0 for i, bi in enumerate(b): ans += min(a[i], b[i]) remain = max(0, b[i] - a[i]) ans += min(a[i + 1], remain) a[i + 1] = max(0, a[i + 1] - remain) print(ans)
n = int(eval(input())) A = list(map(int, input().split()))[::-1] B = list(map(int, input().split()))[::-1] ans = 0 for i, b in enumerate(B): ans += min(A[i], b) remain = max(0, b - A[i]) ans += min(A[i + 1], remain) A[i + 1] = max(0, A[i + 1] - remain) print(ans)
p02959
def main(): import sys input = sys.stdin.readline #文字列をsplit()しない場合s=s[:-2]が必要 n=int(eval(input())) A=[int(_) for _ in input().split()] sum_a=sum(A) B=[int(_) for _ in input().split()] for i in range(n): s=max(B[i]-A[i],0) A[i]-=min(A[i],B[i]) A[i+1]-=min(...
from sys import stdin def main(): input=stdin.readline n=int(eval(input())) A=[int(_) for _ in input().split()] sum_a=sum(A) B=[int(_) for _ in input().split()] for i in range(n): s=max(B[i]-A[i],0) A[i]-=min(A[i],B[i]) A[i+1]-=min(A[i+1],s) print((sum_a...
p02959
n = int(eval(input())) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ans = 0 for i in range(n): if a[i] <= b[i]: if a[i]+a[i+1] > b[i]: all_enemy = a[i] + a[i + 1] ans += b[i] a[i+1] = a[i]+a[i+1] - b[i] else: ...
n = int(eval(input())) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ans = 0 for i in range(n): if a[i] <= b[i]: all_enemy = a[i] + a[i + 1] if all_enemy > b[i]: ans += b[i] a[i+1] = all_enemy - b[i] else: ans += ...
p02959
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = 0 ai = 0 bi = 0 while 1: if max(b) == 0: break if a[ai] < b[bi]: ans += a[ai] b[bi] -= a[ai] a[ai] = 0 ai += 1 elif a[ai] == b[bi]: ans += a[ai]...
n = int(eval(input())) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = 0 """ ai = 0 bi = 0 while 1: if max(b) == 0: break if a[ai] < b[bi]: ans += a[ai] b[bi] -= a[ai] a[ai] = 0 ai += 1 elif a[ai] == b[bi]: ans += ...
p02959