problem_id
stringclasses
100 values
submission_id
stringlengths
10
10
status
stringclasses
2 values
code
stringlengths
6
806
p03160
s869496992
Accepted
N = int(input()) H = list(map(int,input().split())) dp = [10**9 for i in range(N+1)] dp[1] = 0 for i in range(2,N+1): x = H[i-2] - H[i-1] y = 100000 if i >= 3: y = H[i-3] - H[i-1] if x < 0: x = -x if y < 0: y = -y x = min(dp[i-1]+x,dp[i-2]+y) dp[i] = x print(dp[N])
p03160
s342341850
Accepted
N = int(input()) h = list(map(int, input().split())) dp = [10**9+1] * N dp[0] = 0 dp[1] = abs(h[0] - h[1]) for i in range(2, N): dp[i] = min(dp[i], dp[i - 1] + abs(h[i] - h[i-1])) dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i-2])) print(dp[-1])
p03160
s720229808
Accepted
#!/usr/bin/env python3 n = int(input()) h = list(map(int, input().split())) dp = [10001 for _ in range(n)] dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,n): dp[i] = min(dp[i-2] + abs(h[i]-h[i-2]), dp[i-1] + abs(h[i]-h[i-1])) print(dp[n-1])
p03160
s529034463
Accepted
N = int(input()) h = list(map(int,input().split())) costs = [0] * N costs[0] = 0 costs[1] = abs(h[1] - h[0]) for i in range(2,N): tmp1 = costs[i-2] + abs(h[i] - h[i-2]) tmp2 = costs[i-1] + abs(h[i] - h[i-1]) costs[i] = min(tmp1,tmp2) print(costs[N-1])
p03160
s323941303
Accepted
n=int(input()) a=list(map(int,input().split())) dp=[0]*n dp[0]=0 dp[1]=abs(a[0]-a[1]) for i in range(2,n): dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2])) print(dp[n-1])
p03160
s716946110
Accepted
n = int(input()) h = [0] for i in input().split(): h.append(int(i)) def cost(i,j): return abs(h[i]-h[j]) #dp[i]:= 足場iにたどり着く為の最小コスト #dp[0]:= 0 #dp[1]:= 0 #dp[2]:= abs(h[1]-h[2]) dp = [0 for _ in range(n+1)] dp[2] = cost(1,2) for i in range(3,n+1): dp[i] = min(dp[i-1]+cost(i,i-1),dp[i-2]+cost(i,i-2)) print(dp[n])
p03160
s106191914
Accepted
def cost(n, nums): dp = [0 for _ in range(n)] dp[0] = 0 dp[1] = abs(nums[1] - nums[0]) for i in range(2, n): dp[i] = min( dp[i-1] + abs(nums[i]-nums[i-1]), dp[i-2] + abs(nums[i]-nums[i-2])) return dp[-1] n = int(input()) nums = [int(i) for i in input().split()] prin...
p03160
s610715121
Accepted
n=int(input()) a=list(map(int,input().split())) dp_table=[] for i in range(n): if i==0:dp_table.append(0) elif i==1:dp_table.append(abs(a[i]-a[i-1])) else:dp_table.append(min(abs(a[i]-a[i-2])+dp_table[-2],abs(a[i]-a[i-1])+dp_table[-1])) print(dp_table[-1])
p03160
s981035275
Accepted
def main(): N = int(input()) h = list(map(int, input().split())) dp = [float("inf")]*N dp[0] = 0 for i in range(1, N): if i-1 >= 0: dp[i] = min(dp[i], dp[i-1]+abs(h[i]-h[i-1])) if i-2 >= 0: dp[i] = min(dp[i], dp[i-2]+abs(h[i]-h[i-2])) print(dp[-1]) if __...
p03160
s996416283
Accepted
n=int(input()) l=list(map(int,input().split())) dp=[0]*(n) dp[0]=0 dp[1]=abs(l[0]-l[1]) for i in range(2,n): dp[i]=min(abs(l[i]-l[i-2])+dp[i-2],abs(l[i]-l[i-1])+dp[i-1]) print(dp[-1])
p03160
s874996546
Accepted
n=int(input()) h=list(map(int,input().split())) dp = [-1]*n dp[0]=0 dp[1]=abs(h[1]-h[0]) if n == 2: print(dp[1]) exit() for i in range(2,n): dp[i]=min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2])) print(dp[n-1])
p03160
s994927796
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [0] dp.append(abs(h[1]-h[0])) if n > 2: for i in range(2,n): dp.append(min(abs(h[i]-h[i-2])+dp[i-2],abs(h[i]-h[i-1])+dp[i-1])) print(dp[-1])
p03160
s189467880
Accepted
n = int(input()) hl = list(map(int,input().split())) dp = [float("inf") for i in range(n)] dp[0] = 0 for idx, h in enumerate(hl): if idx + 2 < n: dp[idx+2] = min(dp[idx] + abs(hl[idx] - hl[idx+2]), dp[idx+2]) if idx+1 < n: dp[idx+1] = min(dp[idx] + abs(hl[idx] - hl[idx+1]), dp[idx+1]) print(d...
p03160
s226826142
Accepted
N = int(input()) High = list(map(int, input().split())) DP = [0]*N DP[1] = abs(High[0]-High[1]) for i in range(2, N): DP[i] = min(DP[i-2] + abs(High[i] - High[i - 2]), DP[i-1] + abs(High[i] - High[i - 1])) print(DP[N-1])
p03160
s564579710
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [float('inf')] * n dp[0] = 0 dp[1] = abs(h[1]- h[0]) for i in range(2, n): c1 = dp[i-1] + abs(h[i] - h[i-1]) c2 = dp[i-2] + abs(h[i] - h[i-2]) dp[i] = c1 if c1 < c2 else c2 print(dp[n-1])
p03160
s660183660
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [10 ** 18 for _ in range(n)] dp[0] = 0 for i in range(1, n): dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2])) print(dp[-1])
p03160
s350419842
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0]*n dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,n): dp[i] = min((dp[i-1]+abs(h[i]-h[i-1])),dp[i-2]+abs(h[i]-h[i-2])) print(dp[-1])
p03160
s018838760
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [0]*(n+1) dp[0] = 0 for i in range(1, n): if i == 1: dp[i] = dp[i-1] + abs(h[i]-h[i-1]) else: dp[i] = min(dp[i-1] + abs(h[i]-h[i-1]), dp[i-2] + abs(h[i]-h[i-2])) print(dp[n-1])
p03160
s933733571
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0]*(n) dp[1] = abs(h[1]-h[0]) for i in range(2,n): dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2])) print(dp[-1])
p03160
s988018289
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [float("inf")] * (n + 10) dp[0] = 0 for i in range(n): for j in range(1, 3): if i + j < n: dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j])) print(dp[n - 1])
p03160
s718355708
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [0]*n dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,n): dp[i] = min(dp[i-1] + abs(h[i-1] - h[i]), dp[i-2] + abs(h[i-2] - h[i])) print(dp[-1])
p03160
s026627159
Accepted
from sys import stdin n = int(stdin.readline()) h = list(map(int,stdin.readline().split())) dp = [0] for i in range(1,n): dp.append(min(dp[i-1]+abs(h[i]-h[i-1]),10**9 if i==1 else dp[i-2]+abs(h[i]-h[i-2]))) print(dp[-1])
p03160
s583081697
Accepted
N = int(input()) h = list(map(int, input().split())) x = [0] * N x[1] = x[0] + abs(h[1] - h[0]) for i in range(2, N): x[i] = min(x[i - 1] + abs(h[i] - h[i - 1]), x[i - 2] + abs(h[i] - h[i - 2])) print(x[N-1])
p03160
s702589319
Accepted
def frog(H,N): #print(H) DP = [0]*N DP[0] = 0 DP[1] = abs(H[1]-H[0]) for i in range(2,N): DP[i] = min(abs(H[i]-H[i-2])+DP[i-2],abs(H[i]-H[i-1])+DP[i-1]) #print(DP) return DP[-1] if __name__ == '__main__': N = int(input()) H = list(map(int,input().split())) ans = frog...
p03160
s481548246
Accepted
N= int(input()) h= list(map(int, input().split())) dp=[0]*(N+1) dp[0]= 0 dp[1]= abs(h[1]-h[0]) for i in range(2, N): # print((dp[i-1]+ abs(h[i]-h[i-1]))) # print((dp[i-2]+ abs(h[i]-h[i-2]))) # print() dp[i]= min( (dp[i-1]+ abs(h[i]-h[i-1])) , (dp[i-2]+ abs(h[i]-h[i-2]))) # print(dp[i]) # print(d...
p03160
s573319219
Accepted
N = int(input()) h = list(map(int, input().split())) mincost = [float("inf") for _ in range(N)] mincost[0] = 0 mincost[1] = abs(h[1] - h[0]) for i in range(2, N): mincost[i] = min(mincost[i - 2] + abs(h[i] - h[i - 2]), mincost[i - 1] + abs(h[i] - h[i - 1])) print(mincost[N - 1])
p03160
s428860964
Accepted
import sys import math from collections import deque N = int(input()) h = list(map(int, input().split())) dp = [[-1, -1] for _ in range(N)] dp[0] = [0, 0] dp[1] = [abs(h[0]-h[1]), abs(h[0]-h[1])] for i in range(2, N): dp[i][0] = min(dp[i - 1]) + abs(h[i] - h[i - 1]) dp[i][1] = min(dp[i - 2]) + abs(h[i] - h[i ...
p03160
s517465683
Accepted
n = int(input()) H = list(map(int, input().split())) DP = [10 ** 9] * n DP[0] = 0 for i in range(1, n): DP[i] = min(DP[i], DP[i-1] + abs(H[i] - H[i-1])) if i > 1: DP[i] = min(DP[i], DP[i-2] + abs(H[i] - H[i-2])) print(DP[-1])
p03160
s755613008
Accepted
import itertools,sys def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) N = I() h = [None]+LI() dp = [float('INF')]*(N+1) #1_indexed dp[1] = 0 for i,j in itertools.product(range(1,N+1),[1,2]): if i+j>=N+1: continue dp[i+j] = min(dp[i+...
p03160
s605630059
Accepted
n=int(input()) dp=[0]*(n+1) h=list(map(int,input().split())) dp[1]=abs(h[1]-h[0]) for i in range(2,n): dp[i]=min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2])) print(dp[n-1])
p03160
s240867108
Accepted
import sys N = int(input()) H = list(map(int, sys.stdin.readline().rsplit())) dp = [10 ** 18] * N dp[0] = 0 dp[1] = abs(H[0] - H[1]) for i in range(2, N): dp[i] = min(dp[i - 1] + abs(H[i - 1] - H[i]), dp[i - 2] + abs(H[i - 2] - H[i])) print(dp[N - 1])
p03160
s931469513
Accepted
n = int(input()) h = [int(i) for i in input().split()] dp = [10**10]*(n) dp[0] = 0 dp[1] = abs(h[0]-h[1]) for i in range(2,n): a = dp[i-2] + abs(h[i]-h[i-2]) b = dp[i-1] + abs(h[i]-h[i-1]) if a < b: dp[i] = a else: dp[i] = b print(dp[n-1])
p03160
s636226846
Accepted
def chmin(a, b): if a > b: a = b return a else: return a n = int(input()) ls = list(map(int, input().split())) dp = [10**9] * n dp[0] = 0 for i in range(1, n): dp[i] = chmin(dp[i], dp[i - 1] + abs(ls[i] - ls[i - 1])) if i > 1: dp[i] = chmin(dp[i], dp[i - 2] + abs(ls[i...
p03160
s589501930
Accepted
from sys import stdin import numpy as np def main(): #入力 readline=stdin.readline inf=10**10 n=int(readline()) h=np.array(list(map(int,readline().split())),dtype=np.int64) dp=np.full(n,inf,dtype=np.int64) dp[0]=0 for i in range(n): nex_end=i+2+1 dp[i+1:nex_end]=np.minimum...
p03160
s393933689
Accepted
n=int(input()) cost=list(map(int, input().split())) dp=[0]*n dp[0]=0 dp[1]=abs(cost[1]-cost[0]) for i in range(2,n): dp[i]=min(abs(cost[i]-cost[i-1]) + dp[i-1], abs(cost[i]-cost[i-2])+dp[i-2]) print(dp[n-1])
p03160
s185837472
Accepted
import sys sys.setrecursionlimit(1000000) n = int(input()) h = list(map(int, input().split())) cost = [-1] * n def ans(n): if cost[n] != -1: return cost[n] cost[n] = min(ans(n-1) + abs(h[n-1] - h[n]), ans(n-2) + abs(h[n-2] - h[n])) return cost[n] cost[0] = 0 cost[1] = abs(h[0] - h[1]) print(ans(n-1))
p03160
s377390958
Accepted
INF = 10e9 def frog(arr, n): dp = [INF]*n dp[0] = 0 for i in range(n): for j in (i+1, i+2): if j < n: dp[j] = min(dp[j], dp[i] + abs(arr[i]-arr[j])) return(dp[-1]) if __name__ == '__main__': n = int(input()) arr = [int(x) for x in input().split()] pri...
p03160
s530013086
Accepted
import math N = int(input()) h = list(map(int, input().split())) dp = [float('INF')] * N def chmin(a, b): if a >= b: return b else: return a #初期値 dp[0] = 0 #Loop for i in range(1, N): dp[i] = chmin(dp[i], dp[i-1] + abs(h[i] - h[i-1])) if i > 1: dp[i] = chmin(dp[i], dp[i-2] + ...
p03160
s702619564
Accepted
N = int( input() ) h = [int(x) for x in input().split()] dp = [0]*( len(h) ) dp[0] = 0 dp[1] = abs( h[0] - h[1] ) for ii in range( 2, len(dp) ): from_1 = abs(h[ii] - h[ii-1]) from_2 = abs(h[ii] - h[ii-2]) dp[ii] = min( dp[ii-1] + from_1, dp[ii-2] + from_2 ) print(dp[-1])
p03160
s110736990
Accepted
n=int(input()) h=list(map(int,input().split(' '))) dp=[0] * n dp[0]=0 dp[1]=abs(h[0]-h[1]) for i in range(2,n): dp[i] = min(abs(h[i]-h[i-1])+dp[i-1],abs(h[i]-h[i-2])+dp[i-2]) print(dp[n-1])
p03160
s405914345
Accepted
n = int(input()) h = list(map(int,input().split())) INF = 100100100100100 dp = [INF]*n dp[0] = 0 for i in range(1,n): dp[i] = min(dp[i],dp[i-1]+abs(h[i]-h[i-1])) if i > 1: dp[i] = min(dp[i],dp[i-2]+abs(h[i]-h[i-2])) print(dp[n-1])
p03160
s045824704
Accepted
N = int(input()) h = list(map(int,input().split())) ans = [0]*N ans[1] = abs(h[1] - h[0]) for i in range(N - 2): p1 = ans[i] + abs(h[i] - h[i + 2]) p2 = ans[i + 1] + abs(h[i+1] - h[i+2]) ans[i + 2] = min([p1,p2]) print(ans[N-1])
p03160
s365297379
Accepted
#!/usr/bin/env python # -*- coding: utf-8 -*- def main(): N = int(input()) h = [int(i) for i in input().split()] dp = [0] * N for i in range(1, N): if i < 2: dp[i] = abs(h[i-1] - h[i]) else: dp[i] = min(dp[i-1] + abs(h[i-1] - h[i]), dp[i-2] + abs(h[i-2] - h[i])) ...
p03160
s431464550
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0]*(n) for i in range(n-1): if i == 0: dp[1] = abs(h[1]-h[0]) else: dp[i+1] = min(abs(h[i+1] - h[i]) + dp[i],abs(h[i+1] - h[i-1]) + dp[i-1]) print(dp[-1])
p03160
s985533208
Accepted
N = int(input()) H = list(map(int, input().split())) dp = [0 for n in range(N)] dp[1] = abs(H[1] - H[0]) for i in range(2, N): dp[i] = min(dp[i-1] + abs(H[i]-H[i-1]), dp[i-2] + abs(H[i]-H[i-2])) print(dp[N-1])
p03160
s281484970
Accepted
n = int(input()) L = list(map(int,input().split())) inf = float("inf") dp = [inf]*(n) dp[0] = 0 dp[1] = abs(L[1] -L[0]) for i in range(n-2): dp[i+2] = min(dp[i+1] + abs(L[i+2] -L[i+1]) , dp[i] + abs(L[i+2] - L[i])) #print(dp) print(dp[n-1])
p03160
s535356973
Accepted
import math N = int(input()) h = list(map(int, input().split())) dp = [float('INF')] * N def chmin(a, b): if a >= b: return b else: return a #初期値 dp[0] = 0 #Loop for i in range(1, N): dp[i] = chmin(dp[i], dp[i-1] + abs(h[i] - h[i-1])) if i > 1: dp[i] = chmin(dp[i], dp[i-2] + ...
p03160
s574586455
Accepted
f_inf = float('inf') N = int(input()) h = list(map(int, input().split(' '))) h.append(f_inf) h.append(f_inf) dp = [f_inf for _ in range(N + 100)] dp[0] = 0 for i in range(N-1): dp[i+1] = min(dp[i] + abs(h[i+1] - h[i]), dp[i+1]) dp[i+2] = min(dp[i] + abs(h[i+2] - h[i]), dp[i+2]) print(dp[N-1])
p03160
s684736116
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [0 for _ in range(n)] dp[1] = abs(h[0] - h[1]) for i in range(2, n): dp[i] = min(dp[i - 2] + abs(h[i - 2] - h[i]), dp[i - 1] + abs(h[i - 1] - h[i])) print(dp[n - 1])
p03160
s594566877
Accepted
N = int(input()) H = list(map(int, input().split())) DP = [0]*N DP[1] = abs(H[1]-H[0]) for i in range(2, N): two = abs(H[i]-H[i-2]) one = abs(H[i]-H[i-1]) if DP[i-2]+two <= DP[i-1]+one: DP[i] = DP[i-2]+two else: DP[i] = DP[i-1]+one print(DP[-1])
p03160
s322247080
Accepted
# coding: utf-8 # Your code here! import math n=int(input()) h=list(map(int,input().split())) dp=[math.inf for _ in range(n)] dp[0]=0 for i in range(1,n): if i==1: dp[i]=dp[i-1]+abs(h[i]-h[i-1]) else: dp[i]=min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2])) print(dp[n-1])
p03160
s396276993
Accepted
INF = float('inf') N = int(input()) h = list(map(int, input().split())) dp = [INF for i in range(N)] dp[0] = 0 for i in range(N): if i - 1 >= 0: dp[i] = min(dp[i], dp[i - 1] + abs(h[i] - h[i - 1])) if i - 2 >= 0: dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2])) print(dp[N - 1])
p03160
s861456722
Accepted
import numpy as np def chmin(a,b): a = a if a < b else b return a def chmax(a, b): a = a if a > b else b return a N = int(input().strip()) h_point = [] temp = input().split() for i in range(N): h_point.append(int(temp[i])) costs = [0, abs(h_point[1]-h_point[0])] for i in range(2, N): ans...
p03160
s841075135
Accepted
n = int(input()) h = list(map(int, input().split())) sol = [0 for i in range(n)] sol[0] = 0 sol[1] = abs(h[1]-h[0]) for i in range(2, n): sol[i] = min(sol[i-2] + abs(h[i-2]-h[i]), sol[i-1] + abs(h[i-1]-h[i])) print(sol[n-1])
p03160
s772882598
Accepted
def chmin(a, b): if a > b: return b return a N = int(input()) H = list(map(int, input().split())) dp = [float('inf')]*N dp[0] = 0 dp[1] = abs(H[1] - H[0]) for i in range(2, N): dp[i] = chmin(dp[i], dp[i - 1] + abs(H[i] - H[i - 1])) dp[i] = chmin(dp[i], dp[i - 2] + abs(H[i] - H[i - 2])) print...
p03160
s586224631
Accepted
N = int(input()) h_array = list(map(int, input().split())) c_array = [0] * N c_array[0] = 0 c_array[1] = abs(h_array[0] - h_array[1]) for i in range(2, N): c_array[i] = min(c_array[i - 1] + abs(h_array[i - 1] - h_array[i]), c_array[i - 2] + abs(h_array[i - 2] - h_array[i])) print(c_array[-1]...
p03160
s112390629
Accepted
import sys input = sys.stdin.readline def main(): n = int(input()) h = list(map(int, input().split())) dp = [0]*n dp[1] = abs(h[0]-h[1]) for i in range(2, n): dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2])) print(dp[-1]) if __name__ == "__main__": main()
p03160
s560187624
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [0] * n dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,n): dp[i] = dp[i-1] + abs(h[i]-h[i-1]) dp[i] = min(dp[i],dp[i-2] + abs(h[i] - h[i-2])) print (dp[n-1])
p03160
s690343811
Accepted
n=int(input()) H=list(map(int,input().split())) dp=[10**9]*n dp[0]=0 dp[1]=abs(H[1]-H[0]) for i in range(2,n): step1=dp[i-1]+abs(H[i]-H[i-1]) step2=dp[i-2]+abs(H[i]-H[i-2]) dp[i]=min(step1,step2) print(dp[-1])
p03160
s859421616
Accepted
n = int(input()) h = list(map(int,input().split())) inf = float("inf") dp =[inf for i in range(n)] dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,n): dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2])) print(dp[-1])
p03160
s716637267
Accepted
N = int(input()) H = list(map(int, input().split())) P = [0, abs(H[0]-H[1])] for i in range(1, N-1): P.append(min(P[i-1]+abs(H[i-1]-H[i+1]), P[i]+abs(H[i]-H[i+1]))) print(P[-1])
p03160
s819271703
Accepted
#!/usr/bin/env python3 def main(): N = int(input()) h = list(map(int, input().split())) INF = 1 << 60 dp = [INF] * 100010 dp[0] = 0 for i in range(N): if i + 1 < N: dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])) if i + 2 < N: dp[i + 2] = min...
p03160
s128765610
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0] * n dp[0] = 0 dp[1] = abs(h[1] - h[0]) for i in range(2,n): dp[i] = min(dp[i-2] + abs(h[i-2] - h[i]), dp[i-1] + abs(h[i-1] - h[i])) print(dp[n-1])
p03160
s271322663
Accepted
N = int(input()) h_list = list(map(int, input().split())) dp = [float('INF') for i in range(N)] dp[0] = 0 def calc_cost(now, next): global h_list global dp global N if next > N - 1: return if dp[next] > dp[now] + abs(h_list[next] - h_list[now]): dp[next] = dp[now] + abs(h_list[next] - h_list[now]) for i...
p03160
s925941399
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0] * n dp[1] = abs(h[0] - h[1]) for i in range(2,n): p = abs(h[i] - h[i-2]) + dp[i-2] q = abs(h[i] - h[i-1]) + dp[i-1] if p <= q: dp[i] = p else: dp[i] = q print(dp[-1])
p03160
s093228946
Accepted
n=int(input()) a=list(map(int,input().split())) # dp には 足場(i)へ至る最小コストが入る。 dp=[float("inf")]*(n) dp[0]=0 for i in range(n-1): dp[i+1]=min(dp[i+1],dp[i]+abs(a[i]-a[i+1])) if i+2 < n: dp[i+2]=min(dp[i+2],dp[i]+abs(a[i]-a[i+2])) print(dp[-1])
p03160
s742835519
Accepted
import sys N=int(input()) A=[int(i) for i in input().split()] if N==1: print(0) sys.exit() dp=[0]*N dp[0]=0 dp[1]=abs(A[0]-A[1]) for i in range(2,N): dp[i]=min(abs(A[i]-A[i-2])+dp[i-2],abs(A[i]-A[i-1])+dp[i-1]) print(dp[-1])
p03160
s837356862
Accepted
n = int(input()) l = list(map(int,input().split())) dp = [0 for i in range(n)] dp[1] = abs(l[0] - l[1]) for i in range(2, n): dp[i] = min(abs(l[i]-l[i-2])+dp[i-2], abs(l[i]-l[i-1])+dp[i-1]) print(dp[-1])
p03160
s716645288
Accepted
n=int(input()) ab=abs hr=list(map(int,input().split())) dp=[-1]*(n+1) dp[0]=0 dp[1]=ab(hr[1]-hr[0]) for i in range(2,n): x=ab(hr[i]-hr[i-2])+dp[i-2] y=ab(hr[i]-hr[i-1])+dp[i-1] dp[i]=min(x,y) print(dp[n-1])
p03160
s567438029
Accepted
N = int(input()) H = list(map(int, input().split())) INF = 10 ** 10 dp = [INF] * N dp[0] = 0 for i in range(1, N): dp[i] = min(dp[i], dp[i-1] + abs(H[i] - H[i-1])) if i > 1: dp[i] = min(dp[i], dp[i-2] + abs(H[i] - H[i-2])) print(dp[N-1])
p03160
s413522676
Accepted
N = int(input()) arr = list(map(int,input().rstrip().split())) dparr= [-1 for i in range(N)] dparr[0] = 0 dparr[1] = abs(arr[0]-arr[1]) for num in range(2,N): dparr[num] = min(dparr[num-2]+ abs(arr[num]-arr[num-2]), dparr[num-1]+ abs(arr[num]-arr[num-1])) print(dparr[-1])
p03160
s002920695
Accepted
t = int(input()) a = list(map(int, input().split())) if t == 1: print(0) elif t == 2: print(abs(a[0] - a[1])) else: dp = [-1] * (t) dp[0] = 0 dp[1] = abs(a[0] - a[1]) for i in range(2, t): dp[i] = min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - ...
p03160
s670311504
Accepted
# import sys # sys.setrecursionlimit(10**5) # from collections import defaultdict geta = lambda fn: list(map(fn, input().split())) gete = lambda fn: fn(input()) N = gete(int) h = geta(int) INF = 1 << 60 dp = [INF] * N dp[0], dp[1] = 0, abs(h[0] - h[1]) for i in range(2, N): dp[i] = min(dp[i - 2] + abs(h[i] - h[i...
p03160
s646649816
Accepted
n = int(input()) hs = tuple(map(int, input().split())) a, b = 0, abs(hs[1] - hs[0]) for i in range(2, n): c = hs[i] a, b = b, min(a + abs(c - hs[i-2]), b + abs(c - hs[i-1])) print(b)
p03160
s168037598
Accepted
N = int(input()) H = list(map(int,input().split())) #sub problem: min cost from 0 to i memo = [] for i, num in enumerate(H): if i == 0: memo.append(0) elif i == 1: memo.append(abs(num - H[i-1])) else: memo.append(min(abs(num - H[i-2])+ memo[i-2], abs(num - H[i-1]) + memo[i-1]) ) print(memo[-1])
p03160
s626129648
Accepted
n = int(input()) h = [int(i) for i in input().split()] inf = 10**7 dp = [inf]*100005 dp[0] = 0 for i in range(1,n): dp[i] = min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1])) print(dp[n-1])
p03160
s687492014
Accepted
n=int(input()) arr=list(map(int, input().split())) INF=1e9+5 dp=[INF for i in range(n)] dp[0]=0 for i in range(len(arr)): for j in (i+1,i+2): if(j<n): dp[j]=min(dp[j], dp[i]+abs(arr[j]-arr[i])) print(dp[n-1])
p03160
s063397498
Accepted
N = int(input()) h = list(map(int, input().split())) cost = [0]*(N+1) for i in range(1,N): if(i==1): cost[1] =abs(h[1]-h[0]) else: cost[i] = min(abs(h[i-2]-h[i])+cost[i-2],abs(h[i-1]-h[i])+cost[i-1]) print(cost[N-1])
p03160
s984458012
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [10**18] * n dp[0] = 0 for i in range(n-1): if i == n-2: dp[i+1] = min(dp[i] + abs(h[i+1] - h[i]), dp[i+1]) else: dp[i+1] = min(dp[i] + abs(h[i+1] - h[i]), dp[i+1]) dp[i+2] = min(dp[i] + abs(h[i+2] - h[i]), dp[i+2]) prin...
p03160
s515046951
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [float("inf") for _ in range(n)] dp[0] = 0 for i in range(n-1): dp[i+1] = min(dp[i+1], dp[i]+abs(h[i+1]-h[i])) if i<n-2: dp[i+2] = min(dp[i+2], dp[i]+abs(h[i+2]-h[i])) print(dp[n-1])
p03160
s175110347
Accepted
N = int(input()) h = list(map(int, input().split())) DP = [10000]*N DP[0] = 0 DP[1] = abs(h[1]-h[0]) for i in range(N-1): DP[i+1] = abs(h[i+1]-h[i])+DP[i] if i > 0: DP[i+1] = min(abs(h[i+1]-h[i])+DP[i], abs(h[i+1]-h[i-1])+DP[i-1]) print(DP[N-1])
p03160
s001868458
Accepted
N = int(input()) h = [0] + list(map(int, input().split())) INF = float("inf") dp = [INF for _ in range(N + 1)] dp[1] = 0 for i in range(2, N + 1): if i == 2: dp[i] = abs(h[i] - h[i - 1]) else: dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])) print(dp[N])
p03160
s246610048
Accepted
def findmin(arr,n): dp = [0]*n dp[1] = abs(arr[0]-arr[1]) for i in range(2,n): dp[i] = min(dp[i-1]+abs(arr[i-1]-arr[i]), dp[i-2]+abs(arr[i-2]-arr[i])) return dp[-1] n = int(input()) arr = list(map(int,input().split())) print(findmin(arr,n))
p03160
s293885493
Accepted
N=int(input()) hlist=list(map(int,input().split())) dp=[10*5]*N dp[0]=0 dp[1]=abs(hlist[1]-hlist[0]) if N>2: for i in range (2,N): c1=abs(hlist[i]-hlist[i-2])+dp[i-2] c2=abs(hlist[i]-hlist[i-1])+dp[i-1] dp[i]=min(c1,c2) print(dp[-1])
p03160
s375936191
Accepted
N = int(input()) [*h] = map(int, input().split()) dp = [float('inf') for _ in range(N)] dp[0] = 0 dp[1] = abs(h[0]-h[1]) for i in range(2, N): dp[i] = dp[i-1]+abs(h[i]-h[i-1]) dp[i] = min(dp[i], dp[i-2]+abs(h[i]-h[i-2])) print(dp[N-1])
p03160
s150116480
Accepted
N=int(input()) h=list(map(int,input().split())) dp=[0]*N for i in range(1,N): if i==1: dp[i]=abs(h[i]-h[i-1]) else: dp[i]=min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2])) print(dp[N-1])
p03160
s107026864
Accepted
n = int(input()) h = [int(i) for i in input().split()] dp = [0]*n dp[1] = abs(h[1]-h[0]) for i in range(2, n): dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]), \ dp[i-2]+abs(h[i]-h[i-2])) print(dp[n-1])
p03160
s318089362
Accepted
N = int(input()) H = list(map(int,input().split())) dp = [0] * N for i in range(1,N): if i == 1: dp[i] = dp[i-1] + abs(H[i] - H[i-1]) else: dp[i] = min(dp[i-1] + abs(H[i] - H[i-1]),dp[i-2] + abs(H[i] - H[i-2])) print(dp[-1])
p03160
s385504456
Accepted
N = int(input()) h = list(map(int,input().split())) INF = 10*10 dp = [INF]*N dp[0] = 0 dp[1] = abs(h[1] - h[0]) if N > 3: for i in range(2,N): dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]),dp[i-2] + abs(h[i] - h[i-2])) print(dp[N-1])
p03160
s751601643
Accepted
N = int(input()) h = list(map(int,input().split())) cost = [10**9]*N cost[0] = 0 cost[1] = abs(h[1]-h[0]) for i in range(2, N): cost[i] = min((abs(h[i-1] - h[i]) + cost[i-1]),cost[i]) cost[i] = min((abs(h[i-2] - h[i]) + cost[i-2]), cost[i]) print(cost[-1])
p03160
s685982293
Accepted
n = int(input()) a = list(map(int, input().split())) dp = [float('inf')]*n dp[0] = 0 for i in range(1, n): dp[i] = min(dp[i], dp[i-1] + abs(a[i] - a[i-1])) if i > 1: dp[i] = min(dp[i], dp[i-2] + abs(a[i] - a[i-2])) print(dp[-1])
p03160
s611318933
Accepted
n = int(input()) heights = [int(x) for x in input().split()] dp = [float("inf")] * n dp[0] = 0 for i in range(1, n): if i - 2 >= 0: val1 = abs(heights[i] - heights[i - 1]) + dp[i - 1] val2 = abs(heights[i] - heights[i - 2]) + dp[i - 2] dp[i] = min(val1, val2) else: dp[i] = abs(h...
p03160
s955027387
Accepted
n = int(input()) h = [int(i) for i in input().split()] #dp[i] = そこに辿り着くまでのコスト #初期条件 dp[0] = 0, dp[1] = |h[1] - h[0]|, dp[2以上] = 10 ** 9 + 7 dp = [10**9+7] * n dp[0] = 0 dp[1] = abs(h[1] - h[0]) if n == 2: print(dp[1]) else: for i in range(2,n): dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]),dp[i-2] + abs(h[i...
p03160
s019101292
Accepted
import sys INF = 10**18 if __name__ == "__main__": n = int(input()) h = list(map(int,input().split())) dp = [INF]*(n) dp[0] = 0 for i in range(1,n): d1 = abs(h[i] - h[i-1]) d2 = INF if i>=2: d2 = abs(h[i] - h[i-2]) dp[i] = min(d1+dp[i-1],d2+dp[i-2]) p...
p03160
s795338254
Accepted
import sys stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().rstrip() # ignore trailing spaces n = ni() h = na() dp = [0]*n dp[0] = 0 dp[1] = abs(h[1]-h[0]) count = 1 for i in range(2,n): dp[i] = min(abs(h[i-1]-h[i])+dp[i-1], abs(h[i-2]-h...
p03160
s073017691
Accepted
N = int(input()) h = list(map(int , input().split(' '))) #N = 6 #h = [30,10,60 , 10, 60,50] dp =[-1]*(N) dp[N-1] = 0 dp[N-2] = abs(h[N-2] - h[N-1]) m=N-3 while m>=0: diff = 0 index = 0 diff = min(abs(h[m]-h[m+2]) + dp[m+2] , abs(h[m]-h[m+1])+dp[m+1]) dp[m] = diff m -=1 print(dp[m+1])
p03160
s081724626
Accepted
def frog1(h): dp = [float('inf')]*3 dp[0] = 0 for i in range(1,len(h)): dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)]) return dp[(len(h)-1)%3] n = input() vals = list(map(int, input().split())) print(frog1(vals))
p03160
s660739465
Accepted
import sys input=sys.stdin.readline n=int(input()) h=list(map(int,input().split())) dp=[0]*n dp[1]=abs(h[1]-h[0]) for i in range(2,n): dp[i]=min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1])) print(dp[-1])
p03160
s430949988
Accepted
n = int(input()) h = list(map(int,input().split())) inf = 10**9 dp = [inf]*n dp[0]=0 dp[1]=abs(h[1]-h[0]) for i in range(2,n): dp[i]=min((dp[i-1] +abs(h[i]-h[i-1])),(dp[i-2]+abs(h[i]-h[i-2]))) print(dp[n-1])
p03160
s233663713
Accepted
import sys n = int(input()) l = list(map(int, input().split())) dp = [sys.maxsize] *n dp[0] = 0 for i in range(n): for j in range(i+1,i+3): if(j<n): dp[j]=min(dp[j],dp[i]+abs(l[i]-l[j])) print(dp[n-1])