problem_id
stringclasses
100 values
submission_id
stringlengths
10
10
status
stringclasses
2 values
code
stringlengths
6
806
p03160
s400639253
Accepted
input() a = list(map(int, input().split())) dp = [float('inf')]*len(a) dp[0] = 0 for i in range(len(a)-1): dp[i+1] = min(dp[i+1], dp[i] + abs(a[i+1] - a[i])) if i < len(a)-2: dp[i+2] = dp[i] + abs(a[i+2] - a[i]) print(dp[-1])
p03160
s057749728
Accepted
n = int(input()) lis = list(map(int, input().split())) dp = [] for k in range(n+1): dp.append(0) for i in range(2, n+1): if i == 2: dp[i] = abs(lis[i-1]-lis[i-2]) #print(i, dp[i]) else: #print(i) dp[i] = min(abs(lis[i-1]-lis[i-2])+dp[i-1], abs(lis[i-1]-lis[i-3])+dp[i-2]) #print(i, dp[i]) print(dp[n])
p03160
s039802859
Accepted
def chmin(a,b): if a>b: a = b return a def chmax(a,b): if a<b: a = b return a N = int(input()) h = list(map(int,input().rstrip().split(' '))) dp = [float('INF')]*N dp[0] = 0 for i in range(1,N): dp[i] = chmin(dp[i],dp[i-1]+abs(h[i]-h[i-1]))#1個前から移動 if i>1 : dp[i] = chmin(dp[i],dp[i-2]+abs(h[i]-h[i-2]))#2個前から移動 print(dp[-1])
p03160
s022863521
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [int(1e+10) for _ in range(n)] dp[0],dp[1] = 0,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
s360668392
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [float('inf')]*n dp[0] = 0 for i in range(n): if i+1 < n: dp[i+1] = min(dp[i+1], abs(h[i] - h[i+1]) + dp[i]) if i+2 < n: dp[i+2] = min(dp[i+2], abs(h[i] - h[i+2]) + dp[i]) print(dp[-1])
p03160
s427095705
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [0]*n for i in range(n): if i == 0: continue if i == 1: dp[1] = abs(h[0] - h[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
s203262140
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(abs(h[i]-h[i-1])+dp[i-1],abs(h[i]-h[i-2])+dp[i-2]) print(dp[N-1])
p03160
s049117193
Accepted
# Educational_DP_A_Frogs.py N = int(input()) H = list(map(int, input().split())) # dp[i]:カエルがiに行くまでのコスト最小値 # dp[i] = min(dp[i-2]+abs(H[i]-H[i-2]), dp[i-1]+abs(H[i]-H[i-1])) dp = [0 for i 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] - H[i-2]), dp[i-1] + abs(H[i] - H[i-1])) # print(dp) print(dp[N-1])
p03160
s666711036
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): 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
s412840985
Accepted
INF = float('inf') n = int(input()) h = [int(i) for i in input().split()] dp = [0, abs(h[1] - h[0])] for i in range(2, n): dp += [min(dp[-1] + abs(h[i] - h[i - 1]), dp[-2] + abs(h[i] - h[i - 2]))] print(dp[-1])
p03160
s519576239
Accepted
#Frog1 n=int(input()) h=list(map(int,input().split())) Min=0 dp={} for val in range(n): if(val==0): dp[val]=0 if(val==1): dp[val]=abs(h[0]-h[1]) elif(val>=2): dp[val]=min(abs(h[val]-h[val-1])+dp[val-1],abs(h[val]-h[val-2])+dp[val-2]) print(dp[n-1])
p03160
s186192350
Accepted
N = int(input()) H = [int(x) for x in 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[N-1])
p03160
s410122330
Accepted
# A - Frog1 import numpy as np N = int(input()) h = list(map(int, input().split())) dp = [10**9]*(N+2) dp[0] = 0 for i in range(N): if i+1<N: dp[i+1] = min(dp[i+1], dp[i]+abs(h[i+1]-h[i])) if i+2<N: dp[i+2] = min(dp[i+2], dp[i]+abs(h[i+2]-h[i])) print(dp[N-1])
p03160
s962392837
Accepted
import sys input = sys.stdin.readline import numpy as np N = int(input()) hight_list = np.array([int(item) for item in input().split()]) dp = np.full(N, hight_list.sum(), dtype='int64') dp[0] = 0 dp[1] = abs(hight_list[1] - hight_list[0]) for i in range(2, N): cost1 = abs(hight_list[i-1] - hight_list[i]) cost2 = abs(hight_list[i-2] - hight_list[i]) dp[i] = min(dp[i-1] + cost1, dp[i-2] + cost2) # print(dp) print(dp[N-1])
p03160
s802079162
Accepted
n = int(input()) h = [int(i) for i in input().split()] dp = [10 ** 5 for _ in range(n)] dp[0] = 0 for i in range(1, n): if i == 1: dp[i] = abs(h[1] - h[0]) else: 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
s989178969
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): DP[i] = min(DP[i],DP[i-2]+abs(h[i]-h[i-2])) DP[i] = min(DP[i],DP[i-1]+abs(h[i]-h[i-1])) print(DP[-1])
p03160
s683990137
Accepted
import sys import io f = sys.stdin.buffer n = int(f.readline()) h = list(map(int, f.readline().split())) p0, p1 = 0, abs(h[1] - h[0]) for i in range(2, n): p2 = min(p1 + abs(h[i] - h[i - 1]), p0 + abs(h[i] - h[i - 2])) p0, p1 = p1, p2 print(p1)
p03160
s831270572
Accepted
import math n = int(input()) arr = [int(x) for x in input().split()] dp = [0]*n for i in range(n): if i>=2: dp[i] = min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i-2]-arr[i])) elif i==1: dp[i] = abs(arr[i]-arr[i-1]) else: dp[i] = 0 print(dp[n-1])
p03160
s908363696
Accepted
N = int(input()) h = list(map(int,input().split())) cost = [0]*N cost[0] = 0 cost[1] = abs(h[1]-h[0]) for i in range(2,N): cost[i] = min((cost[i-2]+abs(h[i]-h[i-2])), (cost[i-1]+abs(h[i]-h[i-1]))) print(cost[-1])
p03160
s324572898
Accepted
N = int(input()) h = list(map(int, input().split())) dp = [0]*N for i in range(N): if i == 0: dp[0] = 0 continue if i == 1: dp[1] = abs(h[1]-h[0]) continue 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
s373867052
Accepted
def solve(): n = int(input()) cost = list(map(int, input().split(' '))) dp = [-1 for _ in range(n)] dp[0] = 0 dp[1] = abs(cost[1] - cost[0]) for i in range(2, n): dp[i] = min(dp[i-2] + abs(cost[i] - cost[i-2]), dp[i-1] + abs(cost[i] - cost[i-1])) print(dp[-1]) solve()
p03160
s401620492
Accepted
def solve(h): dp = [0, abs(h[0]-h[1])] for i in range(2, len(h)): one = dp[i-1] + abs(h[i-1]-h[i]) two = dp[i-2] + abs(h[i-2]-h[i]) dp.append(min(one, two)) return dp[-1] if __name__ == "__main__": n = int(input()) h = [int(x) for x in input().split()] print(solve(h))
p03160
s511061826
Accepted
import sys input = sys.stdin.readline n = int(input()) h = [int(x) for x in input().split()] dp = [float('inf')] * (n + 1) dp[1] = 0 dp[2] = abs(h[1] - h[0]) for i in range(3, n + 1): dp[i] = min(dp[i - 1] + abs(h[i - 1] - h[i - 2]), dp[i - 2] + abs(h[i - 1] - h[i - 3])) print(dp[-1])
p03160
s552018246
Accepted
N = int(input()) h = input().split() h = [int(i) for i in h] dp = [0] * N dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(1, N-1): dp[i+1] = min(dp[i] + abs(h[i]-h[i+1]), dp[i-1] + abs(h[i-1]-h[i+1])) print(dp[N-1])
p03160
s031631248
Accepted
import numpy as np N = int(input()) h = list(map(int, input().split())) memo = np.full(N, 10**4) memo[0] = 0 memo[1] = abs(h[0] - h[1]) for i in range(2, N): memo[i] = min([memo[i-1] + abs(h[i-1] - h[i]), memo[i-2] + abs(h[i-2] - h[i])]) print(memo[N-1])
p03160
s131455956
Accepted
n = int(input()) h = [int(x) for x in input().split()] h.insert(0, -1) dp = [-1] * (n+1) dp[1] = 0 dp[2] = abs(h[1] - h[2]) for i in range(3, n+1): 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
s066106907
Accepted
II = lambda: int(input()) SI = lambda: input() LI = lambda: list(map(int,input().split())) MI = lambda: map(int,input().split()) n = II() a = LI() d = [0]*n d[1] = abs(a[1]-a[0]) for i in range(2,n): d[i] = min(d[i-1]+abs(a[i-1]-a[i]),d[i-2]+abs(a[i-2]-a[i])) print(d[-1])
p03160
s727865808
Accepted
n = int(input()) hn = list(map(int, input().split())) dp = [float('inf') for _ in range(n)] dp[0] = 0 dp[1] = abs(hn[1] - hn[0]) for i in range(2, n): dp[i] = min( dp[i], dp[i-1] + abs(hn[i-1] - hn[i]), dp[i-2] + abs(hn[i-2] - hn[i]) ) print(dp[n-1])
p03160
s037997322
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [10**4] * (n+1) 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) print(dp[n-1])
p03160
s259727800
Accepted
n = int(input()) n_list = list(map(int, input().split())) dp = [-1]*n dp[0] = 0 dp[1] = abs(n_list[1]-n_list[0]) for i in range(2,n): dp[i] = min(abs(n_list[i]-n_list[i-1])+dp[i-1], abs(n_list[i]-n_list[i-2])+dp[i-2]) print(dp[n-1])
p03160
s847281658
Accepted
n = int(input()) li = [int(x) for x in input().split()] dp = [float("inf")]*n dp[0] = 0 dp[1] = abs(li[1]-li[0]) for i in range(2,n): dp[i] = min(dp[i-1]+abs(li[i]-li[i-1]),dp[i-2]+abs(li[i]-li[i-2])) print(dp[n-1])
p03160
s297575259
Accepted
n = int(input()) h = list(map(int, input().split())) dp =[] for i in range(n): if i == 0: dp.append(0) elif i == 1: dp.append(abs(h[1]-h[0])) else: dp.append(min(abs(h[i]-h[i-2])+dp[i-2],abs(h[i]-h[i-1])+dp[i-1])) print('{}'.format(dp[n-1]))
p03160
s904441000
Accepted
INF = 10e5 n = int(input()) h = list(map(int,input().split())) dp = [INF for _ in range(n)] dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,n): cost1 = abs(h[i]-h[i-1]) cost2 = abs(h[i]-h[i-2]) dp[i] = min(dp[i-1] + cost1, dp[i-2] + cost2) print(dp[n-1])
p03160
s207637272
Accepted
n=int(input()) import sys sys.setrecursionlimit(10 ** 6) h=list(map(int,input().split())) cos={0:0,1:abs(h[1]-h[0])} def cost(x): if x not in cos: d1=cost(x-1)+abs(h[x]-h[x-1]) d2=cost(x-2)+abs(h[x]-h[x-2]) cos[x]=min(d1,d2) return cos[x] else: return cos[x] print(cost(n-1))
p03160
s065640980
Accepted
n = int(input()) a = list(map(int, input().split())) dp = [0]*n for i in range(n): if i == 0: pass elif i == 1: dp[i] = abs(a[i]-a[i-1]) + dp[i-1] else: dp[i] = min(abs(a[i]-a[i-1])+dp[i-1],abs(a[i]-a[i-2])+dp[i-2]) print(dp[-1])
p03160
s825337002
Accepted
N=int(input()) H=list(map(int,input().split())) dp=[0]*N dp[1]=abs(H[1]-H[0]) for i in range(N-2): dp[i+2]=min(dp[i+1]+abs(H[i+2]-H[i+1]),dp[i]+abs(H[i+2]-H[i])) print(str(dp[N-1]))
p03160
s371386633
Accepted
import sys import io def main(): f = sys.stdin.buffer n = int(f.readline()) h = list(map(int, f.readline().split())) p0, p1 = 0, abs(h[1] - h[0]) for i in range(2, n): p2 = min(p1 + abs(h[i] - h[i - 1]), p0 + abs(h[i] - h[i - 2])) p0 = p1 p1 = p2 print(p1) if __name__ == "__main__": main()
p03160
s107767061
Accepted
n = int(input()) h = list(map(int,input().split())) dp=[10**10]*(n+2) dp[0]=0 dp[1]=0 dp[2]=abs(h[1]-h[0]) for i in range(3,n+1): dp[i]=min(dp[i],dp[i-1]+abs(h[i-1]-h[i-2]),dp[i-2]+abs(h[i-1]-h[i-3])) print(dp[n])
p03160
s747431980
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0,abs(h[0]-h[1])] for i in range(2,n): dp.append(min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1]))) print(dp[-1])
p03160
s509866570
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(1,N-1): DP[i+1]=min(DP[i]+abs(h[i]-h[i+1]),DP[i-1]+abs(h[i-1]-h[i+1])) print(DP[N-1])
p03160
s364719778
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): dp[i] = min(abs(H[i]-H[i-1]) + dp[i-1], abs(H[i]-H[i-2]) + dp[i-2]) print(dp[-1])
p03160
s194176007
Accepted
N = int(input()) *h, = 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
s956602199
Accepted
N=int(input()) h=list(map(int,input().split())) dp=[0] cost=[] for i in range(1,N): cost=[] cost.append(abs(h[i]-h[i-1])+dp[i-1]) if i>=2: cost.append(abs(h[i]-h[i-2])+dp[i-2]) dp.append(min(cost)) print(dp[N-1])
p03160
s913500056
Accepted
def chmin(a, b): if a > b: return b return a def main(): N = int(input()) hs = [i for i in map(int, input().split())] INF = 1e12 dp = [INF]*N dp[0] = 0 for i in range(1, N): dp[i] = chmin(dp[i], dp[i-1] + abs(hs[i]-hs[i-1])) if i > 1: dp[i] = chmin(dp[i], dp[i-2] + abs(hs[i]-hs[i-2])) print(dp[N-1]) return if __name__ == '__main__': main()
p03160
s337058640
Accepted
def main(): N = int(input()) H = list(map(int, input().split())) dp = [float('inf')] * (N + 5) dp[0] = 0 for i in range(N): if i + 1 <= N - 1: dp[i+1] = min(dp[i+1], dp[i] + abs(H[i+1] - H[i])) if i + 2 <= N - 1: dp[i+2] = min(dp[i+2], dp[i] + abs(H[i+2] - H[i])) print(dp[N-1]) if __name__ == "__main__": main()
p03160
s565168144
Accepted
n = int(input()) h = [0] + list(map(int, input().split())) dp = [10**10 for i in range(n+1)] dp[0] = dp[1] = 0 for i in range(1, n+1): 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(dp[i+2], dp[i]+abs(h[i] - h[i+2])) print(dp[n])
p03160
s888030513
Accepted
n=int(input()) h=list(map(int,input().split())) ans=0 temp=[0]*n temp[0]=0 temp[1]=abs(h[1]-h[0]) for i in range(2,n): temp[i]=min(temp[i-1]+abs(h[i]-h[i-1]),temp[i-2]+abs(h[i]-h[i-2])) print(temp[n-1])
p03160
s864091021
Accepted
N = int(input()) h = list(map(int, input().split())) tmp = [int(1e9)] * (N+10) tmp[0] = 0 tmp[1] = abs(h[1] - h[0]) for i in range(2, N): tmp[i] = min(tmp[i-2]+abs(h[i]-h[i-2]), tmp[i-1]+abs(h[i]-h[i-1])) print(tmp[N-1])
p03160
s782612608
Accepted
# DP practice # https://atcoder.jp/contests/dp/tasks/dp_a # 入力方法としてはこれが手軽 N = int(input()) h = list(map(int, input().split())) dp = list() # N = 0, 1 の時の処理も必要。だが省く dp.append(0) dp.append(dp[0] + abs(h[1] - h[0])) for i in range(2, N): dp.append(min(dp[i-2] + abs(h[i] - h[i-2]), dp[i-1] + abs(h[i] - h[i-1]))) print(dp[N-1])
p03160
s904480415
Accepted
N = int(input()) h = list(map(int,input().split())) ml = [10**9]* N ml[0] = 0 ml[1] = abs(h[1]-h[0]) for i in range(2,N): ml[i] = min(abs(h[i]-h[i-1]) + ml[i-1], abs(h[i]-h[i-2]) + ml[i-2]) print(ml[N-1])
p03160
s868130477
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(abs(h[i]-h[i-1])+dp[i-1],abs(h[i]-h[i-2])+dp[i-2]) print(dp[n-1])
p03160
s345543756
Accepted
import math N = int(input()) h = list(map(int, input().split())) dp = [float('INF')] * (10**5 + 10) def chmin(a, b): if a >= b: return b else: return a dp[0] = 0 #初期値 = 0 for i in range(N-1): dp[i+1] = chmin(dp[i+1], dp[i] + abs(h[i+1] - h[i])) if i < N-2: dp[i+2] = chmin(dp[i+2], dp[i] + abs(h[i+2] - h[i])) print(dp[N-1])
p03160
s156497432
Accepted
import sys n=int(input()) k=2 l=list(map(int,input().split())) ans=[0,abs(l[1]-l[0])] for i in range(2,n): m=sys.maxsize c=1 while c<min(k+1,i+1): m=min(abs(l[i]-l[i-c])+ans[-c],m) c+=1 ans.append(m) # print(ans) print(ans[-1])
p03160
s213473728
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): 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
s222136737
Accepted
import math def better(l, ans): for i in range(2, n): ans[i] = min(ans[i - 1] + abs(l[i]-l[i - 1]), ans[i - 2] + abs(l[i]-l[i - 2])) return ans n = int(input()) l = list(map(int, input().split())) ans = [0] * n ans[1] = abs(l[1]-l[0]) print(better(l, ans)[len(ans)-1])
p03160
s320370062
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
s386153319
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [0]*n for i in range(1,n): dp_0 = dp[i-1] + abs(h[i]-h[i-1]) if i -2 < 0: dp_1 = 10**10 else: dp_1 = dp[i-2] + abs(h[i]-h[i-2]) dp[i] = min(dp_0,dp_1) print(dp[-1])
p03160
s920893847
Accepted
n = int(input()) h = [int(i) for i in input().split(" ")] dp = [float("inf")] * n dp[0] = 0 dp[1] = dp[0] + abs(h[0] - h[1]) for i in range(1, n-1, 1): dp[i+1] = min(dp[i-1] + abs(h[i-1] - h[i+1]), dp[i] + abs(h[i] - h[i+1])) print(dp[-1])
p03160
s732160579
Accepted
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) INF = 10 ** 10 n = int(input()) h = [0] + list(map(int, input().split())) dp = [INF] + [INF] * n dp[1] = 0 def chmin(dp_table, index, value): if dp_table[index] > value: dp_table[index] = value for i in range(2, n+1): chmin(dp, i, dp[i-1] + abs(h[i] - h[i-1])) chmin(dp, i, dp[i-2] + abs(h[i] - h[i-2])) print(dp[n])
p03160
s949899543
Accepted
import sys n=int(input()) h=list(map(int,input().split())) if n==2: print(abs(h[1]-h[0])) sys.exit() c=[float('inf')]*n c[0]=0 c[1]=abs(h[1]-h[0]) for i in range(n-2): c[i+2]=min(c[i]+abs(h[i+2]-h[i]),c[i+1]+abs(h[i+2]-h[i+1])) print(c[n-1])
p03160
s274704520
Accepted
N = int(input()) heights = tuple(map(int, input().split(' '))) dp = [0] * (N + 1) dp[1] = 0 dp[2] = abs(heights[0] - heights[1]) for hi, i in enumerate(range(3, N + 1), start=2): dp[i] = min( dp[i - 1] + abs(heights[hi] - heights[hi - 1]), dp[i - 2] + abs(heights[hi] - heights[hi - 2]), ) print(dp[N])
p03160
s406261865
Accepted
N = int(input()) h = list(map(int, input().split())) dp_table = [float("inf")]*N # 0~N-1まで dp_table[0] = 0 for i in range(N-1): # 0~N-1までだが+1して扱う(1~N) dp_table[i+1] = min(dp_table[i]+abs(h[i]-h[i+1]), dp_table[i+1]) if i-1>=0: dp_table[i+1] = min(dp_table[i-1]+abs(h[i-1]-h[i+1]), dp_table[i+1]) print(dp_table[N-1])
p03160
s471070399
Accepted
INT_MAX= 100000000 n=int(input()) dp = [INT_MAX]*(100001) h=[int(X) for X in input().split()] 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-2]-h[i])) print(dp[n-1])
p03160
s752335878
Accepted
n = int(input()) h = [int(x) for x in 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[n-1])
p03160
s595929820
Accepted
N = int(input()) H = list(map(int,input().split())) dp = [float('inf') for i in range(N)] dp[0]=0 for i in range(N): for j in range(1,3): if i+j<N: dp[i+j] = min(dp[i]+abs(H[i]-H[i+j]),dp[i+j]) print(dp[N-1])
p03160
s487917624
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): dp[i] = min(dp[i-2] + abs(h[i-2] - h[i]), dp[i-1] + abs(h[i-1] - h[i])) print(dp[-1])
p03160
s286385452
Accepted
N = int(input()) h = list(map(int, input().split())) ans = [0] * N ans[0] = 0 ans[1] = abs(h[0]-h[1]) for i in range(2, N): ans[i] = min(ans[i-1]+abs(h[i-1]-h[i]), ans[i-2]+abs(h[i-2]-h[i])) print(ans[-1])
p03160
s245675120
Accepted
times = int(input()) line = input() list1 = [] if times <= 1: print("%s" % 0) else: for i in line.split(): if i: list1.append(int(i)) mem = [0] * times mem[-2] = abs(list1[-1] - list1[-2]) for i in range(len(list1)-3, -1, -1): step1 = abs(list1[i] - list1[i+1]) + mem[i+1] step2 = abs(list1[i] - list1[i+2]) + mem[i+2] mem[i] = min(step1,step2) print("%s" % mem[0])
p03160
s568468057
Accepted
n = int(input()) l = [int(x) for x in input().split()] dp = [float('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(l[i]-l[j])) print(dp[n-1])
p03160
s874006908
Accepted
# DPをとく inf=10**10 N=int(input()) H=list(map(int,input().split())) dp = [inf for _ in range(N)] # 足場Nまでにたどり着くときの最小コストとする dp[0]=0 for i in range(N): if i+1<N: dp[i+1] = min(dp[i+1],dp[i]+abs(H[i+1]-H[i])) if i+2<N: dp[i+2] = min(dp[i+2],dp[i]+abs(H[i+2]-H[i])) print(dp[N-1])
p03160
s104763838
Accepted
N = int(input()) H = list(map(int, input().split())) def solve(H): 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]-H[i-2]),dp[i-1]+abs(H[i]-H[i-1])) ans = dp[-1] return ans print(solve(H))
p03160
s112148411
Accepted
def frog(l,n): dp=[0]*n dp[0]=0 dp[1]=abs(l[1]-l[0]) for i in range(2,n): dp[i]=min(dp[i-1]+ abs(l[i]-l[i-1]),dp[i-2]+abs(l[i]-l[i-2])) return dp[n-1] n=int(input()) l=list(map(int,input().split())) print(frog(l,n))
p03160
s532527721
Accepted
N = int(input()) h = [int(val) for val in input().split(' ')] dp = [0, abs(h[0]-h[1])] for i in range(len(h)-2): step1_cost = dp[i+1] + abs(h[i+1]-h[i+2]) step2_cost = dp[i] + abs(h[i]-h[i+2]) dp.append(min(step1_cost, step2_cost)) print(dp[-1])
p03160
s625377808
Accepted
n = int(input()) h = list(map(int, input().split())) dp=[0] * n for i in range(1, n): if i - 2 < 0: dp[i] = dp[i-1] + abs(h[i] - h[i-1]) else: 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
s403385580
Accepted
import sys input=sys.stdin.readline n=int(input()) h=tuple(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
s345769441
Accepted
n = int(input()) h = list(map(int,input().split())) cost = [0]*(n) cost[1] = abs(h[0]-h[1]) for i in range(2,n): cost[i] = min(cost[i-2]+abs(h[i]-h[i-2]),cost[i-1]+abs(h[i]-h[i-1])) print(cost[-1])
p03160
s105303366
Accepted
N = int(input()) h = list(map(int, input().split())) + [0]*100010 dp = [float("inf")]*100010 dp[0] = 0 for i in range(N): dp[i+1] = min(dp[i+1], dp[i] + abs(h[i+1] - h[i])) dp[i+2] = min(dp[i+2], dp[i] + abs(h[i+2] - h[i])) # print(dp) print(dp[N-1])
p03160
s224385334
Accepted
def cal_cost(co, h, i, j): return co + abs(h[i]-h[j]) n = int(input()) h = [int(i) for i in input().split(" ")] lis = [0 for i in range(n)] lis[1] = cal_cost(lis[0], h, 0, 1) for i in range(2, n): temp1 = cal_cost(lis[i-2], h, i-2, i) temp2 = cal_cost(lis[i-1], h, i-1, i) lis[i] = min(temp1, temp2) print(lis[-1])
p03160
s828603591
Accepted
N = int(input()) H=list(map(int,input().split())) #print(H) INF=1000000000 dp=[INF]*(N+1) #print(dp) dp[0]=0 #print(dp[0]) #print(H[1],H[0]) dp[1]=dp[0]+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
s427330669
Accepted
N = int(input()) H = [int(i) for i in input().split()] dp = [0]*N dp[1] = abs(H[0] - H[1]) if N == 2: print(dp[-1]) else: 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
s750884111
Accepted
n = int(input()) hlist = list(map(int, input().split())) # The minimum cost to reach step i (1->n) dp = [0]*(n+1) dp[1] = 0 # 1->1 for i in range(2, n+1): dp[i] = min(dp[i-1] + abs(hlist[i-2] - hlist[i-1]), (dp[i-2] + abs(hlist[i-3] - hlist[i-1]) if i>2 else 10**9)) print(dp[n])
p03160
s926760616
Accepted
N = int(input()) h = list(map(int, input().split())) #1-index dp = [float('inf')]*(N) def chmin(a,b): if a >= b: return b else: return a dp[0] = 0 for i in range(1,N): dp[i] = chmin(dp[i], dp[i-1] + abs(h[i] - h[i-1])) if i >= 2: dp[i] = chmin(dp[i], dp[i-2] + abs(h[i] - h[i-2])) print(dp[N-1])
p03160
s823498019
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] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1])) print(dp[-1])
p03160
s089505080
Accepted
N = int(input()) h = list(map(int,input().split())) INF = float('inf') 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[-1])
p03160
s631473317
Accepted
a=int(input()) b=list(map(int,input().split())) dp=[0] dp.append(abs(b[0]-b[1])) for i in range(2,len(b)): h1=abs(b[i]-b[i-1])+dp[i-1] h2=abs(b[i-2]-b[i])+dp[i-2] dp.append(min(h1,h2)) print(dp[a-1])
p03160
s648126716
Accepted
n = int(input()) height = list(map(int,input().split())) cost = [] for i in range(n): if i == 0: cost.append(0) elif i == 1: cost.append(abs(height[i]-height[0])) else: a = cost[-1] + abs(height[i]-height[i-1]) b = cost[-2] + abs(height[i]-height[i-2]) cost.append(min([a,b])) print(cost[-1])
p03160
s101328507
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[1] - h[0]) else: dp[i] = min(abs(h[i] - h[i-1]) + dp[i-1], abs(h[i] - h[i-2]) + dp[i-2]) print(dp[-1])
p03160
s572008625
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-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1])) print(dp[-1])
p03160
s994799870
Accepted
n = int(input()) l = list(map(int,input().split())) dp = [0]*n dp[1] = abs(l[1] - l[0]) for i in range(2,n): dp[i] = min(dp[i-2]+abs(l[i]-l[i-2]),dp[i-1]+abs(l[i] - l[i-1])) print(dp[n-1])
p03160
s874934734
Accepted
import sys N=int(input()) l=[int(x) for x in input().split()] l1=[sys.maxsize]*(N) l1[0]=0 for i in range(N): for j in range(i+1,i+3): if j<N: l1[j]=min(l1[j],l1[i]+abs(l[j]-l[i])) print(l1[N-1])
p03160
s505600614
Accepted
n=int(input()) arr=[int(i) for i in input().split()] dp=[0]*(n+1) dp[2]=abs(arr[0]-arr[1]) for i in range(3,n+1): dp[i]=min(dp[i-1]+abs(arr[i-1]-arr[i-2]),dp[i-2]+abs(arr[i-1]-arr[i-3])) print(dp[n])
p03160
s816620492
Accepted
# 参考: # https://qiita.com/drken/items/dc53c683d6de8aeacf5a # ver 2,振り返る DP n=int(input()) a=list(map(int,input().split())) # dp には 足場(i)へ至る最小コストが入る。 dp=[float("inf")]*(n) # 振り返り起点(i==2)までのdp値は埋めておく。 dp[0]=0 dp[1]=abs(a[1]-a[0]) 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[-1])
p03160
s306114809
Accepted
n = int(input()) h = [0] + list(map(int, input().split())) dp = [0] * (n + 1) dp[1] = 0 dp[2] = dp[1] + abs(h[2] - h[1]) for i in range(3, n + 1): dp[i] = min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1])) print(dp[n])
p03160
s658139837
Accepted
import math from decimal import * n = int(input()) nos = list(map(int, input().split())) arr = [0 for i in range(n)] if(n>1): arr[1] = abs(nos[1]-nos[0]) for i in range(2, n): a ,b = abs(nos[i]-nos[i-1])+arr[i-1], abs(nos[i]-nos[i-2])+arr[i-2] if(a>b): arr[i]=b else: arr[i] = a print(arr[n-1])
p03160
s193327878
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): t1=dp[i-2]+abs(h[i]-h[i-2]) t2=dp[i-1]+abs(h[i]-h[i-1]) dp[i]=min(t1,t2) print(dp[n-1])
p03160
s528452377
Accepted
import sys #import numpy as np #from collections import defaultdict #import math #from collections import deque input = sys.stdin.readline def main(): N = int(input()) H = list(map(int,input().split())) H = [10**6] + H dp= [0] *(N+1) for i in range(2,N+1): dp[i]= min(dp[i-2]+abs(H[i]-H[i-2]),dp[i-1]+abs(H[i]-H[i-1])) print(dp[N]) if __name__ == "__main__": main()
p03160
s726212134
Accepted
N = int(input()) l = list(map(int,input().split())) m = {0:0,1:abs(l[1]-l[0])} for i in range(2,N): m[i] = min(m[i-1]+abs(l[i]-l[i-1]),m[i-2]+abs(l[i]-l[i-2])) print (m[N-1])
p03160
s179545025
Accepted
N = int(input()) steps = list(map(int, input().split(" "))) costs = [None] * N costs[0] = 0 costs[1] = abs(steps[1] - steps[0]) for i in range(2, N): cost1 = costs[i-1] + abs(steps[i-1] - steps[i]) cost2 = costs[i-2] + abs(steps[i-2] - steps[i]) costs[i] = min(cost1, cost2) print(costs[N-1])
p03160
s028005751
Accepted
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**8) # from functools import lru_cache # @lru_cache(maxsize=None) n = int(input()) h = list(map(int, input().split())) cost = [0]*n cost[0] = 0 cost[1] = abs(h[1]-h[0]) for i in range(2,n): cost[i] = min(cost[i-1]+abs(h[i]-h[i-1]), cost[i-2]+abs(h[i]-h[i-2])) print(cost[n-1])
p03160
s651022165
Accepted
n = int(input()) arr = list(map(int,input().strip().split(" "))) dp = [0]*n dp[0] = 0 dp[1] = abs(arr[0]-arr[1]) for i in range(2,n): p = abs(arr[i]-arr[i-1]) q = abs(arr[i]-arr[i-2]) dp[i] = min(p+dp[i-1],q+dp[i-2]) print(dp[n-1])