problem_id
stringclasses
100 values
submission_id
stringlengths
10
10
status
stringclasses
2 values
code
stringlengths
6
806
p03160
s350984963
Accepted
N = int(input()) H = list(map(int, input().split())) dp = [float('inf')] * 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]) ) if i >= 2 else dp[i-1] + abs(H[i] - H[i-1]) #print(*dp) print(dp[N-1])
p03160
s569570696
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] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2])) print(dp[-1])
p03160
s400385918
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [10**18] * (10**5+10) 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] = dp[i] + abs(h[i+2] - h[i]) print(dp[n-1])
p03160
s547727170
Accepted
import numpy as np n=int(input()) h=np.array(list(map(int,input().split()))+[0]*2) dp = np.full(n+2, 10 ** 10, dtype=np.int64) dp[0] = 0 for i in range(1,n): dp[i:i+2]=np.minimum(dp[i:i+2],np.abs(h[i:i+2]-h[i-1])+dp[i-1]) print(dp[n-1])
p03160
s205340330
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
s001013931
Accepted
n = int(input()) h = tuple(map(int, input().split())) c = [0] * n c[0] = 0 c[1] = abs(h[0] - h[1]) for i in range(2, n): c[i] = min(c[i - 1] + abs(h[i - 1] - h[i]), c[i - 2] + abs(h[i - 2] - h[i])) print(c[-1])
p03160
s653740157
Accepted
n = int(input()) h = list(map(int, input().split())) h = [0] + h a = [0] * (n+1) a[1] = 0 a[2] = abs(h[1]-h[2]) for i in range(3, n+1): a[i] = min(a[i-2]+abs(h[i-2]-h[i]), a[i-1]+abs(h[i-1]-h[i])) print(a[n])
p03160
s890213660
Accepted
""" author : halo2halo date : 4, Feb, 2020 """ import sys # import itertools # import math # import numpy as np read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, *H = map(int, read().split()) dp = [0] * N dp[1] = abs(H[1] - H[0]) for i in range(2, N): ...
p03160
s713127231
Accepted
N = int(input()) h_list = list(map(int, input().split())) dp = [0] * N # DPテーブルの作成 dp[1] = abs(h_list[1] - h_list[0]) if N>= 3: for i in range(2, N): dp[i] = min(dp[i-1]+abs(h_list[i]-h_list[i-1]), dp[i-2]+abs(h_list[i]-h_list[i-2])) print(dp[N-1])
p03160
s046516301
Accepted
N = int(input()) h = list(map(int, input().split())) list = [0 for _ in range(N)] list[0] = 0 list[1] = abs(h[1]-h[0]) for i in range(2, N): list[i] = min(abs(h[i] - h[i-1]) + list[i-1], abs(h[i] - h[i-2]) + list[i-2]) print(list[N-1])
p03160
s257869174
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
s153334044
Accepted
import sys sys.setrecursionlimit(200000) n = int(input()) h = [int(i) for i in input().split()] d = {} def dp(p): if p in d: return d[p] if p == n-1: return 0 if p < n-2: m = min(dp(p+1)+abs(h[p]-h[p+1]), dp(p+2)+abs(h[p]-h[p+2])) d[p] = m return m if p == n-2: return dp(p+1)+abs...
p03160
s148253127
Accepted
def DP_A() : N = int(input()) h = [int(x) for x in input().split()] dp = [10**10]*(N+1) dp[0] = 0 dp[1] = abs(h[1] - h[0]) 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])) return dp[N-1] print(DP_A())
p03160
s550679715
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[n-1])
p03160
s190278287
Accepted
#!/usr/bin/env python3 from collections import deque, Counter from heapq import heappop, heappush from bisect import bisect_right def main(): N = int(input()) h = list(map(int, input().split())) INF = 10**12 dp = [INF] * (N+1) dp[1] = 0 for i in range(1,N): dp[i+1] = min(dp[i+1],dp[i]+a...
p03160
s138417295
Accepted
n = int(input()) H = list(map(int, input().split())) memo = [0 for _ in range(n)] memo[1] = abs(H[1] - H[0]) for i in range(2, n): memo[i] = min(memo[i-1] + abs(H[i] - H[i-1]), memo[i-2] + abs(H[i] - H[i-2])) print(memo[-1])
p03160
s043219324
Accepted
def resolve(): N = int(input()) h = [int(i) for i in input().split()] dp = [float("inf")] * 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[-1]) resolve()
p03160
s063016096
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [-1] * 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
s374912509
Accepted
N = int(input()) H = list(map(int, input().split())) cost = [0] * N for i in range(1, N): d1 = cost[i-1] + abs(H[i] - H[i-1]) d2 = cost[i-2] + abs(H[i] - H[i-2]) if i > 1 else 1e6 cost[i] = min(d1, d2) #print(cost) ans = cost[N-1] print(ans)
p03160
s771897832
Accepted
def main(): footing = int(input()) height = list(map(int, input().split())) answer = [0] * footing answer[1] = abs(height[1] - height[0]) for i in range(2, footing): answer[i] = min(answer[i - 2] + abs(height[i] - height[i - 2]), answer[i - 1] + abs(height[i] - height[i - 1])) print(answ...
p03160
s259048144
Accepted
n=int(input()) arr=[int(i) for i in input().split()] dp=[0]*(n) dp[0]=0 dp[1]=abs(arr[1]-arr[0]) for i in range(2,n): dp[i]=min(abs(arr[i]-arr[i-1])+dp[i-1],abs(arr[i]-arr[i-2])+dp[i-2]) print(dp[-1])
p03160
s341286429
Accepted
n=int(input()) a=list(map(int,input().split())) b=10**10 c=[b]*n c[0]=0 c[1]=abs(a[1]-a[0]) for i in range(n-2): for j in range(1,3): c[i+j]=min(c[i+j],c[i]+abs(a[i+j]-a[i])) c[-1]=min(c[-1],c[-2]+abs(a[-2]-a[-1])) print(c[-1])
p03160
s889576154
Accepted
n = int(input()) h = list(map(int, input().split())) INF = 10**8 dp = [INF]*n dp[0] = 0 for i in range(n-1): if i == 0: dp[i+1] = abs(h[i+1]-h[i]) else: dp[i+1] = min(dp[i]+abs(h[i+1]-h[i]), dp[i-1]+abs(h[i+1]-h[i-1])) print(dp[n-1])
p03160
s927026384
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
s967083959
Accepted
N = int(input()) h = list(map(int, input().split())) dp = [0]*N dp[N-1], dp[N-2] = 0, abs(h[N-1] - h[N-2]) for i in range(N-3,-1,-1): dp[i] = min(abs(h[i] - h[i+1]) + dp[i+1], abs(h[i] - h[i+2]) + dp[i+2]) print(dp[0])
p03160
s433306319
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(1,n-1): dp[i+1] = min(dp[i]+abs(h[i+1]-h[i]),dp[i-1]+abs(h[i+1]-h[i-1])) print(dp[n-1])
p03160
s491383738
Accepted
n = int(input()) h = [int(i) for i in input().split()] INF = float("INF") dp = [INF]*n dp[0] = 0 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
s884575417
Accepted
n = int(input()) h = list(map(int, input().split())) cnt = [float('inf')]*n cnt[0], cnt[1] = 0, abs(h[0]-h[1]) for i in range(2,n): cnt[i]=min(abs(h[i-1]-h[i])+cnt[i-1],abs(h[i-2]-h[i])+cnt[i-2]) print(cnt[n-1])
p03160
s981711262
Accepted
# A Frog 1 n = int(input()) h_list = [int(x) for x in input().split()] dp = [float('Inf')]*(n) dp[0] = 0 dp[1] = abs(h_list[1] - h_list[0]) for i in range(2,n,1): dp[i] = min(dp[i-2]+abs(h_list[i]-h_list[i-2]), dp[i-1]+abs(h_list[i]-h_list[i-1])) print(dp[n-1])
p03160
s966279859
Accepted
N = int(input()) h = [int(x) for x in input().split()] Cost = [0] * N 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[-1])
p03160
s532462604
Accepted
def main(): N = int(input()) field = list(map(int, input().split())) dpList = [0] * N dpList[1] = abs(field[0] - field[1]) for i in range(2, N): dpList[i] = min([dpList[i - 1] + abs(field[i] - field[i - 1]), dpList[i - 2] + abs(field[i] - field[i - 2])]) print(dpList[-1]) if ...
p03160
s040554217
Accepted
N = int(input()) H = list(map(int, input().split())) dp = [float("inf")] * N dp[0] = 0 for n in range(1,N): dp[n] = min(dp[n], dp[n-1]+abs(H[n]-H[n-1])) if n >= 2: dp[n] = min(dp[n], dp[n-2]+abs(H[n]-H[n-2])) print(dp[-1])
p03160
s217306878
Accepted
N = int(input()) H = [int(i) for i in input().split()] dp = [float("inf")] * N dp[0], dp[1] = 0, abs(H[1]-H[0]) for i in range(2, N): dp[i] = min(dp[i], dp[i-1]+abs(H[i]-H[i-1]), dp[i-2]+abs(H[i]-H[i-2])) print(dp[N-1])
p03160
s588286654
Accepted
def main(): n = int(input()) h = list(map(int, input().split())) dp = [0] * n def cost(i,j): return abs(h[i]-h[j]) dp[1] = cost(0,1) for i in range(2,n): dp[i] = min(dp[i-2] + cost(i-2,i), dp[i-1] + cost(i-1,i)) print(dp[n-1]) if __name__ == '__main__': main()
p03160
s025715490
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [10**7]*n dp[0] = 0 for i in range(1,n): dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1,min(3,i+1))]) print(dp[n-1])
p03160
s132825919
Accepted
def jumpCost(array): size = len(array) memo = [99999999999] * (size) memo[0] = 0 for i in range(size): j = i+1 while j < i + 2 + 1 and j < size: memo[j] = min(memo[j], memo[i] + abs(array[i] - array[j])) j += 1 return memo[size - 1] if __name__ == '__m...
p03160
s640919997
Accepted
import itertools import math import sys from collections import Counter from fractions import gcd from functools import reduce import sys sys.setrecursionlimit(4100000) INF = 1 << 60 n = int(input()) h = list(map(int, input().split())) dp = [INF] * n dp[0] = 0 for i in range(1, n): dp[i] = min(dp[i], dp[i - 1] + ...
p03160
s812948683
Accepted
import sys sample = sys.stdin.read().splitlines() n, stones = int(sample[0]), list(map(int, sample[1].split())) dp = [float('inf')] * n jumps = (1, 2) dp[0] = 0 for idx in range(len(dp)): for jump in jumps: if idx+jump < len(dp): dp[idx+jump] = min(dp[idx]+abs(stones[idx+jump]-stones[idx]), dp[idx+jump]) p...
p03160
s903639815
Accepted
N = int(input()) H = list(map(int,input().split())) dp = [float('inf') for i in range(N)] #全ノードを取り敢えずinfにしておく 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-3: dp[i+2] = min(dp[i+2], dp[i] + abs(H[i + 2]-H[i])) print(dp[N-1])
p03160
s933075384
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(n-1): 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[-1])
p03160
s220073194
Accepted
import sys sys.setrecursionlimit(10**6) N = int(input()) h = list(map(int, input().split())) dp = [-1] * (N+1) def rec(i): if i == 0: return 0 if dp[i] != -1: return dp[i] dp[i] = 2**60 # 十分大きな値 if i - 1 >= 0: dp[i] = min(dp[i], rec(i - 1) + abs(h[i - 1] - h[i])) if i - 2 >...
p03160
s509471742
Accepted
def solve(N, heights): dp = [float('inf')] * N dp[0] = 0 for i in range(N - 1): dp[i + 1] = min(dp[i + 1], dp[i] + abs(heights[i] - heights[i + 1])) if i + 2 < N: dp[i + 2] = min(dp[i + 2], dp[i] + abs(heights[i] - heights[i + 2])) print(dp[N-1]) def iin(): return int(input()) def iln(): return...
p03160
s702587535
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[-1])
p03160
s482528657
Accepted
n = int(input()) h = list(map(int,input().split())) cost = [0]*n 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
s471057810
Accepted
N=110000 dp=[0]*N n=int(input()) h=list(map(int,input().split())) now=[0,0,0] for i in range(1,n): if i==1: dp[i]=dp[i-1]+abs(h[i]-h[i-1]) else: now=[i,i-1,i-2] 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
s444763781
Accepted
N = int(input()) [*h] = 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-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2])) print(dp[N-1])
p03160
s739470849
Accepted
mod=10**9+7 import math import sys from collections import deque import heapq import copy import itertools from itertools import permutations from itertools import combinations import bisect def mi() : return map(int,sys.stdin.readline().split()) def ii() : return int(sys.stdin.readline().rstrip()) def i() : return sys...
p03160
s552995112
Accepted
_ = int(input()) stones = [int(i) for i in input().split()] cost = [0, abs(stones[1] -stones[0])] for i in range(2, len(stones)): jump_1 = cost[i-1] + abs(stones[i] - stones[i-1]) jump_2 = cost[i-2] + abs(stones[i] - stones[i-2]) cost.append(min(jump_1, jump_2)) print(cost[-1])
p03160
s485146730
Accepted
n = int(input()) li = list(map(int,input().split())) dp = [0,abs(li[1]-li[0])] for i in range(2,n): dp += [min(dp[-2]+abs(li[i]-li[i-2]),dp[-1]+abs(li[i]-li[i-1]))] print(dp[-1])
p03160
s855591084
Accepted
n = int(input()) h = list(map(int, input().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
s067129222
Accepted
def resolve(): 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-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
s394966589
Accepted
n = int(input()) h = list(map(int,input().split())) dp = [999999]*(n+1) 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-1] + abs(h[i-1]-h[i-2]), dp[i-2] +abs(h[i-1] - h[i-3])) print(dp[n])
p03160
s493997593
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [float("inf") for i in range(n)] dp[0] = 0 dp[1] = dp[0] + (h[1] - h[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
s271842718
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] - h[i-1]),dp[i-2]+abs(h[i] - h[i-2])) print(dp[n-1])
p03160
s954495334
Accepted
import sys,queue,math,copy,itertools,bisect,collections,heapq sys.setrecursionlimit(10**7) INF = 10**18 MOD = 10**9 + 7 LI = lambda : [int(x) for x in sys.stdin.readline().split()] NI = lambda : int(sys.stdin.readline()) N = NI() h = LI() dp = [INF for _ in range(N)] dp[0] = 0 for i in range(N-1): dp[i+1] = min(dp...
p03160
s561865222
Accepted
n=int(input()) h=list(map(int,input().split())) dp=[10**9]*(n+1) 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
s426456927
Accepted
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: dp[i] = min(dp[i],dp[i-1] + abs(H[i]-H[i-1])) else: dp[i] = min(dp[i],dp[i-1]+abs(H[i]-H[i-1]),dp[i-2]+abs(H[i]-H[i-2])) print(dp[N-1])
p03160
s172716210
Accepted
# Flog 1 N = int(input()) h_list = list(map(int, input().split())) cost_list = [0 for i in range(N+1)] # ステップiへたどり着くためのコスト最小値 for i in range(1,N): if i==1: cost_list[i+1] = abs(h_list[i] - h_list[i-1]) continue step_1 = cost_list[i] + abs(h_list[i]-h_list[i-1]) step_2 = cost_list[i-1] + ...
p03160
s847062725
Accepted
N = int(input()) steps = list(map(int, input().split())) costs = [0]*(N+1) for i in range(1, N): if i==1: costs[1] = abs(steps[1]-steps[0]) continue a = costs[i-1]+abs(steps[i]-steps[i-1]) b = costs[i-2]+abs(steps[i]-steps[i-2]) costs[i] = min(a, b) print(costs[N-1])
p03160
s453307152
Accepted
n=int(input()) h=list(map(int,input().split())) res=[-1]*n res[0]=0 res[1]=abs(h[0]-h[1]) for i in range(2,n): res[i]=min(abs(h[i-1]-h[i])+res[i-1],abs(h[i-2]-h[i])+res[i-2]) print(res[-1])
p03160
s737821571
Accepted
N=int(input()) # 足場の数 H=list(map(int,input().split(" "))) # 足場の高さ C=[0 for _ in range(N)] # 各足場までの最小コスト for i in range(1,N): if i==1: # 2つ目のは1つ目から移るしかない C[1]=C[0]+abs(H[1]-H[0]) else: # 3つ目以降 C[i]=min(C[i-1]+abs(H[i]-H[i-1]),C[i-2]+abs(H[i]-H[i-2])) print(C[-1]) # 最後の足場に行く最小コスト
p03160
s729870130
Accepted
import sys sys.setrecursionlimit(200000) #追加 S = [-1]*(101010) def opt_S(h,n): if(n == 0):return 0 elif(n == 1):return abs(h[1]-h[0]) else : if(S[n] != -1):return S[n] s1 = opt_S(h,n-1) + abs(h[n-1]-h[n]) s2 = opt_S(h,n-2) + abs(h[n-2]-h[n]) S[n] = min(s1,s2) return S...
p03160
s600224380
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] = min(dp[i-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2])) print(dp[N-1])
p03160
s824162929
Accepted
n=int(input()) lis=list(map(int,input().split(' '))) node=[0,abs(lis[1]-lis[0])]+[0]*(n-2) for i in range(2,n): x=abs(lis[i]-lis[i-1])+node[i-1] y=abs(lis[i]-lis[i-2])+node[i-2] node[i]=min(x,y) print(node[-1])
p03160
s038899231
Accepted
N = int(input()) *h, = 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(dp[i-2] + abs(h[i-2] - h[i]), dp[i-1] + abs(h[i-1] - h[i])) print(dp[-1])
p03160
s293558113
Accepted
N = int(input()) h = list(map(int,input().split())) inf = float("inf") dp = [inf]*N dp[0] = 0 #initial situation 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
s917961893
Accepted
n=int(input()) l=list(map(int,input().split())) 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-1]-l[i]),dp[i-2]+abs(l[i-2]-l[i])) print(dp[-1])
p03160
s303435427
Accepted
n, *h = map( int, open( 0 ).read().split() ) d = [ 0 ] * n d[ 1 ] = abs( h[ 1 ] - h[ 0 ] ) for i in range( 2, n ): d[ i ] += min( d[ i - 1 ] + abs( h[ i ] - h[ i - 1 ] ), d[ i - 2 ] + abs( h[ i ] - h[ i - 2 ] ) ) print( d[ n - 1 ] )
p03160
s437169636
Accepted
N = int(input()) h = list(map(int, input().split())) dp = [10**10 for _ in range(N)] dp[0] = 0 for i in range(N-1): if(i+2 > N-1): dp[i+1] = min(dp[i+1], dp[i]+abs(h[i+1]-h[i])) else: dp[i+2] = min(dp[i+2], dp[i]+abs(h[i+2]-h[i])) dp[i+1] = min(dp[i+1], dp[i]+abs(h[i+1]-h[i])) ...
p03160
s161361976
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) print(dp[n-1])
p03160
s990452838
Accepted
N = int(input()) H = [int(i) for i in input().split()] dp = [0] * (N+1) 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
s915099005
Accepted
h=int(input()) def bupdp(l,h): dp=[0]*(h+1) dp[0] = 0 for i in range(0,h): if i>1: dp[i] = min(dp[i-1]+abs(l[i]-l[i-1]),dp[i-2]+abs(l[i]-l[i-2])) if i==1: dp[i] = abs(l[i]-l[i-1]) if i<1: dp[i] = 0 # print(dp) return dp[-2] l = list(ma...
p03160
s010499872
Accepted
n=int(input()) a=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(a[i]-a[i-1])) if i==1: continue dp[i]=min(dp[i],dp[i-2]+abs(a[i]-a[i-2])) print(dp[n-1])
p03160
s412849661
Accepted
N = int(input()) h = list(map(int, input().split())) #dp[i]:i番目に到達するまでにはらうコストの最小値 dp = [0 for i in range(N)] dp[0] = 0 dp[1] = abs(h[1]-h[0]) for i in range(2,N): cost_1 = dp[i-1] + abs(h[i]-h[i-1]) cost_2 = dp[i-2] + abs(h[i]-h[i-2]) dp[i] = min(cost_1, cost_2) print(dp[N-1])
p03160
s803134476
Accepted
N = int(input()) h = list(map(int, input().split())) if N == 2: print(abs(h[1]-h[0])) exit() dp = [0 for _ in range(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[N-1])
p03160
s155636144
Accepted
N = int(input()) h = list(map(int, input().split())) opt = [0] * len(h) opt[1] = abs(h[0]-h[1]) for i in range(2,len(h)): A = opt[i-1] + abs(h[i-1] - h[i]) B = opt[i-2] + abs(h[i-2] - h[i]) opt[i] = min(A, B) print(opt[-1])
p03160
s981964565
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: continue dp[i]=min(dp[i],dp[i-2]+abs(a[i]-a[i-2])) print(dp[-1])
p03160
s319390939
Accepted
n = int(input()) lst = list(map(int, input().split())) cst = [float('inf')] * n cst[0] = 0 for _ in range(0, n-2): cst[_+1] = min(cst[_] + abs(lst[_+1] - lst[_]),cst[_+1]) cst[_+2] = min(cst[_] + abs(lst[_+2] - lst[_]),cst[_+2]) cst[n-1] = min(cst[n-2] + abs(lst[n-1] - lst[n-2]),cst[n-1]) print(cst[n-1])
p03160
s085853184
Accepted
inf = 10**9 def main(): N = int(input()) h = list(map(int,input().split())) memo = [inf]*N memo[0] = 0 def calcCost(start,goal,h): return abs(h[goal]-h[start]) for i in range(1,N): memo[i] = min(memo[i],memo[i-1]+calcCost(i-1,i,h)) if i > 1: memo[i] = min(memo[i],memo[i-2]+calcCost(...
p03160
s749327878
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[n-1])
p03160
s866200384
Accepted
def getval(): n = int(input()) h = list(map(int,input().split())) return n,h def main(n,h): dp = [0,abs(h[1]-h[0])] for i in range(1,n-1): x = dp[-1]+abs(h[i+1]-h[i]) y = dp[-2]+abs(h[i+1]-h[i-1]) dp.append(min(x,y)) print(dp[-1]) if __name__=="__main__": n...
p03160
s549032438
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [None]*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
s158687808
Accepted
N=int(input()) h=list(map(int,input().split())) h1=abs(h[0]-h[1]) h0=0 for i in range(1,N-1): h0,h1=h1,min( abs(h[i+1]-h[i-1])+h0, abs(h[i+1]-h[i])+h1 ) print(h1)
p03160
s510641986
Accepted
n=int(input()) h=list(map(int,input().split())) dp=[0] dp.append(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.append(min(step1,step2)) print(dp[-1])
p03160
s430057830
Accepted
n = int(input()) h_list = list(map(int, input().split())) cost_list = [0] * len(h_list) cost_list[0] = 0 cost_list[1] = abs(h_list[1] - h_list[0]) for i in range(2, n): cost_list[i] = min(abs(h_list[i-1] - h_list[i]) + cost_list[i-1], abs(h_list[i-2] - h_list[i]) + cost_list[i-2]) print(c...
p03160
s968260846
Accepted
def rec(a): dp=[0]*len(a) for i in range(len(a)): if i==0: dp[i]=0 elif i==1: dp[i]=abs(a[i]-a[i-1]) else: dp[i]=min(abs(a[i]-a[i-1])+dp[i-1],abs(a[i]-a[i-2])+dp[i-2]) return dp[len(a)-1] n=int(input()) a=[int(i) for i in input().split()] print(rec...
p03160
s529688605
Accepted
def chmin(a, b): if a > b: return b else: return a # input N = int(input()) h = list(map(int, input().split())) # dp table dp = [float('inf')]*(10**5 + 10) # 初期条件 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 > 1: dp[i] = chmin(dp[i], ...
p03160
s351387000
Accepted
N = int(input()) H = list(map(int, input().split())) dp = [0] for i, x in enumerate(H): if i == 0: continue elif i == 1: dp.append( abs(H[i] - H[i-1]) ) else: 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
s522802463
Accepted
def input_li(): return list(map(int, input().split())) def input_int(): return int(input()) N = input_int() h_li = input_li() dp = [0] * N dp[0] = 0 dp[1] = abs(h_li[1] - h_li[0]) for i in range(2, N): dp[i] = min( dp[i - 1] + abs(h_li[i] - h_li[i - 1]), dp[i - 2] + abs(h_li[i] - h_li[i - ...
p03160
s015538016
Accepted
n = int(input()) H = list(map(int, input().split())) dp = [0] * n for i in range(n): if i == 0: dp[i] = 0 elif i == 1: dp[i] = abs(H[0] - H[i]) else: 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
s545847000
Accepted
def solve(h): best_p = abs(h[0] - h[1]) best_pp = 0 n = 2 while n < len(h): cost1 = best_p + abs(h[n] - h[n-1]) cost2 = best_pp + abs(h[n] - h[n-2]) best = min(cost1, cost2) best_pp, best_p = best_p, best n = n + 1 return best_p def main(): N = int(input()) heights = list(map(int, input().split())) ...
p03160
s336297219
Accepted
n = int(input()) h = list(map(int, input().split())) dp = [float("Inf")] * (n + 1) 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
s635891124
Accepted
from math import inf n = int(input()) h = list(map(int, input().split())) costs = [inf]*n costs[0] = 0 for i in range(n): for j in (i+1, i+2): if j < n: costs[j] = min(costs[j], costs[i]+abs(h[i]-h[j])) print(costs[-1])
p03160
s338292278
Accepted
n=int(input()) li=list(map(int,input().split())) dp=[0]*(n) dp[0]=0 dp[1]=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
s584630541
Accepted
import sys INF = sys.maxsize N = int(input().strip()) h = [int(x) for x in input().split()] memo = [INF] * N for i in range(N - 1, -1, -1): if i == N -1: memo[i] = 0 else: memo[i] = min( memo[i + 1] + abs(h[i] - h[i + 1]) if i + 1 < N else INF, memo[i + 2] + abs(h[i] - h[i + 2]) if i + 2 < N...
p03160
s421551620
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
s686389187
Accepted
n=int(input()) h=list(map(int,input().split())) li=[0]*n li[1]=abs(h[1]-h[0]) for i in range(2,n): li[i]=min(li[i-1]+abs(h[i]-h[i-1]),li[i-2]+abs(h[i]-h[i-2])) ##print(li) print(li[n-1])
p03160
s129052578
Accepted
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
s826473577
Accepted
n=int(input()) cst=list(map(int,input().split())) arr=[0]*10**5 arr[0]=0 arr[1]=abs(cst[1]-cst[0]) if n==2: print(arr[1]) else: for i in range(2, n): arr[i] = min(abs(cst[i - 1] - cst[i]) + arr[i - 1], abs(cst[i - 2] - cst[i]) + arr[i - 2]) print(arr[i])
p03160
s588831450
Accepted
#Educational DP Contest #A-Frog1 n = int(input()) #n,m = map(int,input().split()) lst1 = list(map(int,input().split())) #dp table dp = [0]*n #process1 dp[1] = abs(lst1[1]-lst1[0]) for i in range(2,n): bb,b = abs(lst1[i]-lst1[i-2]),abs(lst1[i]-lst1[i-1]) dp[i] = min(dp[i-2]+bb,dp[i-1]+b) #output process pr...