Problem p03160 (python)

Fixed submission:
import sys
I = lambda:map(int,sys.stdin.readline().split())
pr = lambda x:sys.stdout.write(f'{x}\n')
sys.setrecursionlimit(2000000001)

mn = lambda w,x,y,z: min(abs(w)+x,abs(y)+z)
n = ar = dp = 0

def solve(x):
	global n,ar,dp
	if x>=n-2: return dp[x]
	if dp[x] != -1: return dp[x]
	dp[x] = mn(ar[x]-ar[x+1], solve(x+1), ar[x]-ar[x+2], solve(x+2))
	# pr(f'{x} => {dp[x]}')
	return dp[x]

def main():
	global n,ar,dp
	n, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	# pr((n,ar,dp))
	pr(solve(0))

main()

Buggy submission (verdict: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define rep(i,x) for(int i = 0; i < x; ++i)
using namespace std;

int n,ar[100001]={},dp[100001]={};

int solve(int x)
{
	if(x>=n-2) return dp[x];
	if(dp[x]==-1) dp[x]=min(abs(ar[x]-ar[x+1])+solve(x+1) , abs(ar[x]-ar[x+2])+solve(x+2));
	return dp[x];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin >> n;
	rep(i,n) cin >> ar[i];
	dp[n-2]=abs(ar[n-1]-ar[n-2]);
	rep(i,n-3) dp[i]=-1;
	cout << solve(0) <<nxt;
	return 0;
}

Buggy submission (verdict: Runtime Error):
I = sys.stdin.readline
pr = sys.stdout.write

def main():
	n=int(I())
	ar=list(map(int,I().split()))
	dp=[0]*(n-2)+[abs(ar[n-1]-ar[n-2]),0]
	def solve(x):
		if x>=n-2:return dp[x]
		if not dp[x]:dp[x]= min(abs(ar[x]-ar[x+1])+solve(x+1) , abs(ar[x]-ar[x+2])+solve(x+2))
		return dp[x]

	pr(str(solve(0)))

if __name__ == '__main__':main()

Buggy submission (verdict: Runtime Error):
I = sys.stdin.readline
pr = sys.stdout.write

def main():
	n=int(I())
	ar=list(map(int,I().split()))
	dp=[0]*(n-2)+[abs(ar[n-1]-ar[n-2]),0]
	def solve(x):
		if x>=n-2:return dp[x]
		if not dp[x]:dp[x]= min(abs(ar[x]-ar[x+1])+solve(x+1) , abs(ar[x]-ar[x+2])+solve(x+2))
		return dp[x]

	pr(str(solve(0)))

if __name__ == '__main__':main()

Buggy submission (verdict: Runtime Error):
import sys
I = sys.stdin.readline
pr = sys.stdout.write
 
def main():
	n=int(I())
	ar=list(map(int,I().split()))
 
	def solve(x):
		if x>=n-2:return abs(ar[n-1]-ar[x])
		return min(abs(ar[x]-ar[x+1])+solve(x+1) , abs(ar[x]-ar[x+2])+solve(x+2))
 
	pr(str(solve(0)))
 
if __name__ == '__main__':main()

Buggy submission (verdict: Runtime Error):
import sys
I = sys.stdin.readline
pr = sys.stdout.write

def main():
	n=int(I())
	ar=list(map(int,I().split()))

	def solve(x):
		if x>=n-2:return abs(ar[n-1]-ar[x])
		return min(abs(ar[x]-ar[x+1])+solve(x+1) , abs(ar[x]-ar[x+2])+solve(x+2))

	pr(solve(0))

if __name__ == '__main__':main()

Buggy submission (verdict: Runtime Error):
I = input
pr = print
# INF=1e9+7
def main():
	n = int(I())
	ar = list(map(int, I().split()))
	ar.append(0)
	dp = [0]*(n+1)
	# dp[n-1]=ar[-1]
	def solve(i):
		if i>=(n-1): return dp[i];
		if not dp[i]: dp[i] = min(abs(ar[i]-ar[i+1])+solve(i+1),abs(ar[i]-ar[i+2])+solve(i+2))
		return dp[i];
	pr(solve(0))

if __name__ == '__main__':main()

Buggy submission (verdict: Runtime Error):
I = input
pr = print
n = int(I())
ar = [int(x) for x in I().split()]
ar.append(0)
dp = [0]*(n+1)
dp[n-1]=ar[-1]
def solve(i):
	if i>=(n-1): return dp[i];
	if not dp[i]: dp[i] = min(abs(ar[i]-ar[i+1])+solve(i+1),abs(ar[i]-ar[i+2])+solve(i+2))
	return dp[i];
pr(solve(0))