Problem:


Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999



Sample input:
3 100 100
10 1000 100


Sample output:
900

Submission status: Time Limit Exceeded

Solution code:
n, x0, y0 = list(map(int, input().split()))

cards = [y0] + list(map(int, input().split()))

xs = [[-1] * (n+1) for i in range(n+1)]
ys = [[-1] * (n+1) for i in range(n+1)] 
#xs[i][j] = xの手番で、xがcards[i]を持ちyがcards[j]を持っているとき(i<j)の最善スコア
#ys[i][j] = yの手番で、xがcards[j]を持ちyがcards[i]を持っているとき(i<j)の最善スコア

########ys[i][j] = yの手番で、xがcards[i]を持ちyがcards[j]を持っているとき(i>j)の最善スコア

for i in range(n+1):
	xs[i][-1] = abs(cards[-1] - cards[i])
	ys[i][-1] = abs(cards[-1] - cards[i])

for i in range(n-1, -1, -1):

	# x[i][j] = max (y[j][j+1] , y[j][j+2] , ……, y[j][n-1] )
	for j in range(n-1, i, -1):
		# temp_max = max(ys[j][j+1:n-1])
		# xs[i][j] = max(temp_max, ys[j][n-1])
		xs[i][j] = max(ys[j][j+1:n+1])

	for j in range(n-1, i, -1):
		# temp_min = min(xs[j][j+1:n-1])
		# ys[i][j] = min(temp_min, xs[j][n-1])
		ys[i][j] = min(xs[j][j+1:n+1])

# print(max(xs[0][0], abs(cards[-1] - x0)) )

# print(xs)
# print(ys)
print(max(ys[0][1:]))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
INF = float("inf")
import sys
sys.setrecursionlimit(10000)

a = []
@lru_cache(None)
def dfs(no, i):
    global a

    ans = abs(a[-1] - a[i])
    for j in range(i + 1, len(a) - 1):
        nex = dfs(1 - no, j + 1)
        # X
        if no % 2 == 0:
            ans = max(ans, nex)
        # Y
        else:
            ans = min(ans, nex)
    return ans


def solve2(N, Z, W):
    global a
    a = [Z, W] + a
    return dfs(0, 1)


def solve(N, Z, W):
    global a
    ans = abs(a[-1] - W)
    if len(a) > 1:
        ans = max(ans, abs(a[-2] - a[-1]))
    return ans


def main():
    global a
    N, Z, W = map(int, input().split())
    a = list(map(int, input().split()))
    print(solve2(N, Z, W))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,z,w = LI()
    a = LI()
    m = {}
    def f(z,w,i,t):
        if i >= n:
            return abs(z-w)
        if t:
            k = (-1,w,i)
            if k in m:
                return m[k]
            r = -1
            for j in range(i,n):
                jr = f(a[j],w,j+1,False)
                if r < jr:
                    r = jr
            m[k] = r
        else:
            k = (z,-1,i)
            if k in m:
                return m[k]
            r = inf
            for j in range(i,n):
                jr = f(z,a[j],j+1,True)
                if r > jr:
                    r = jr
            m[k] = r
        return m[k]

    return f(z,w,-1,True)



print(main())

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,z,w = LI()
    a = LI()
    m = {}
    def f(z,w,i,t):
        k = (z,w,i,t)
        if k in m:
            return m[k]
        if i >= n:
            m[k] = abs(z-w)
            return m[k]
        if t:
            r = -1
            for j in range(i,n):
                jr = f(a[j],w,j+1,False)
                if r < jr:
                    r = jr
            m[k] = r
        else:
            r = inf
            for j in range(i,n):
                jr = f(z,a[j],j+1,True)
                if r > jr:
                    r = jr
            m[k] = r
        return m[k]

    return f(z,w,-1,True)



print(main())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, z, w = map(int, input().split())
a = list(map(int, input().split()))
score = abs(z-w)
a = np.array(a)
final_scare = np.abs(a - a[-1])
last = a[-1]
final_scare = final_scare[0:-1]
if n == 1 or abs(w-a[-1]) >= final_scare.max():
    print(abs(w-a[-1]))
    exit()

idx = 0
turn = True
while idx != n-2 :
    if turn :
        idx = final_scare[idx:].argmax()
        z = a[idx]
    else :
        idx = final_scare[idx:].argmin()
        w = a[idx]
    turn = not turn

if turn :
    z = last
else :
    w = last
print(abs(z-w))

Submission status: Time Limit Exceeded

Solution code:
# どちらか必ずa_Nのカードを持つ
import sys
from functools import lru_cache

sys.setrecursionlimit(4000)

N,Z,W = map(int,input().split())
A = list(map(int,input().split()))
A.reverse()

# turn: true->X, false->Y
BOTTOM = A[0]

@lru_cache(maxsize=2000000)
def rec_X(i,y):
  return max(abs(BOTTOM-y),max((rec_Y(j,A[j]) for j in range(1,i)), default=0))

@lru_cache(maxsize=2000000)
def rec_Y(i,x):
  return min(abs(BOTTOM-x),min((rec_X(j,A[j]) for j in range(1,i)), default=float('inf')))

print(rec_X(N,W))

Submission status: Time Limit Exceeded

Solution code:
# どちらか必ずa_Nのカードを持つ
import sys
from functools import lru_cache

sys.setrecursionlimit(4000)

N,Z,W = map(int,input().split())
A = list(map(int,input().split()))
A.reverse()

# turn: true->X, false->Y

@lru_cache(maxsize=4000000)
def rec(i,x,y,turn):
  if i == 0:
    return abs(x-y)
  if turn:
    return max(rec(j,A[j],y,False) for j in range(i))
  else:
    return min(rec(j,x,A[j],True) for j in range(i))

print(rec(N,Z,W,True))

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
A = [W]+list(map(int, input().split()))
X = [0]*N
Y = [1<<30]*N
X[0] = Y[0] = abs(A[-2]-A[-1])
for i in range(1,N):
    X[i] = max(abs(A[-2-i]-A[-1]), max(Y[j] for j in range(i)))
    Y[i] = min(abs(A[-2-i]-A[-1]), min(X[j] for j in range(i)))
print(X[-1])

Submission status: Accepted

Solution code:
N, Z, W=map(int, input().split())
if N==1:
  print(abs(W-int(input())))
else:
  a=list(map(int, input().split()))
  print(max(abs(a[-1]-W), abs(a[-2]-a[-1])))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


def main():
    import sys

    input = sys.stdin.readline

    N, Z, W = map(int, input().split())
    a = [int(x) for x in input().split()]

    if N == 1:
        print(abs(a[-1] - W))
    else:
        print(max(abs(a[-1] - W), abs(a[-2] - a[-1])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,z,w = map(int, readline().split())
a = [w]+[*map(int, readline().split())]
n += 1

INF = 1<<30
dp0 = [-INF]*n
dp1 = [INF]*n

dp0[-1] = dp1[-1] = abs(a[-1]-a[-2])
for i in range(n-2,0,-1):
    dp0[i] = max(abs(a[-1]-a[i-1]),max(dp1[i+1:]))
    dp1[i] = min(abs(a[-1]-a[i-1]),min(dp0[i+1:]))

print(dp0[1])

#print(a)
#print(dp0)
#print(dp1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF=10**18

def main():
    N,Z,W=map(int,input().split())
    a=list(map(int,input().split()))
    dic={}
    def rec(z,w,now,t):
        if dic.get((now,t),None)!=None:
            return dic[(now,t)]
        if now>=N-1:
            return abs(z-w)
        if not t:
            res=-INF
            for i in range(now+1,N):
                r=rec(a[i],w,i,1)
                res=max(res,r)
        else:
            res=INF
            for i in range(now+1,N):
                r=rec(z,a[i],i,0)
                res=min(res,r)
        dic[(now,t)]=res
        return res

    return rec(Z,W,-1,0)
    
print(main())

Submission status: Accepted

Solution code:
n, z, w = map(int, input().split())
a = list(map(int, input().split()))

if n == 1:
    print(abs(a[-1] - w))
else:
    print(max(abs(a[-1] - w), abs(a[-1] - a[-2])))

Submission status: Accepted

Solution code:
import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def S(): return sys.stdin.readline().rstrip()

N,Z,W = map(int,S().split())
a = LI()

if N == 1:
    print(abs(a[0]-W))
else:
    print(max(abs(a[-1]-W),abs(a[-1]-a[-2])))

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,z,w=map(int,input().split())
a=list(map(int,input().split()))

dp=[[-1]*n for i in range(2)]
dp[0][-1]=abs(a[-1]-w)
# dp[-1][1]=abs(a[-2]-a[-1])
# print(dp)
for i in range(n-1):
    for j in range(2):
        if j==0:
            tmp=dp[1-j][-1-i:-1][:]
            tmp.append(abs(a[-2-i]-a[-1]))
            dp[j][-2-i]=min(tmp)
        else:
            tmp=dp[1-j][-1-i:-1][:]
            tmp.append(abs(a[-2-i]-a[-1]))
            dp[j][-2-i]=max(tmp)
        # print(i,j,tmp)
print(max(dp[0]))

Submission status: Accepted

Solution code:
n, z, w = map(int, input().split())
t = tuple(map(int, input().split()))

if n == 1:
    print(abs(t[0] - w))
    exit()

# dp[i][j] := i枚引いた後，jから始めるときの最善スコア
# j=0ならxから，j=1ならyから
# 求めたいのはdp[0][0]
dp = [[0] * 2 for _ in range(n)]

# n-1枚引いた後は分かる
for j in range(2):
    dp[n-1][j] = abs(t[n-2] - t[n-1])

# dp[i+1][j]が分かっているときのdp[i][j]を求める
for i in range(n-2, -1, -1):
    for j in range(2):
        # xスタートの時
        if j == 0:
            # n-i枚残っているので，
            # 取る枚数は1, 2, ..., n-i-1, n-i枚のどれか
            # n-i-i枚までのときは，k枚引くとして以下が考えられる
            l = [dp[i+k][1] for k in range(1, n-i)]
            # n-i枚引くときは
            q = abs(t[n-1] - w) if i == 0 else abs(t[n-1] - t[i-1])
            l.append(q)
            dp[i][j] = max(l)

        else:
            l = [dp[i+k][0] for k in range(1, n-i)]
            if i != 0:
                q = abs(t[n-1] - t[i-1])
                l.append(q)
            dp[i][j] = min(l)

print(dp[0][0])

Submission status: Accepted

Solution code:
import sys

def main():
    N, Z, W = map(int, input().split())
    A = list(map(int, input().split()))

    if len(A) == 1:
        ans = abs(A[0] - W)
    else:
        ans = max(abs(A[-1]-W), abs(A[-2]-A[-1]))

    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)

Submission status: Accepted

Solution code:
n, z, w = map(int, input().split())
a = list(map(int, input().split()))

dp = [[0]*2 for _ in range(n)]

for i in range(n-1, -1, -1):
    dp[i][0] = abs((a[i-1] if i > 0 else w) - a[n-1])
    dp[i][1] = abs((a[i-1] if i > 0 else z) - a[n-1])

    for j in range(i+1, n):
        dp[i][0] = max(dp[i][0], dp[j][1])
        dp[i][1] = min(dp[i][1], dp[j][0])

print(dp[0][0])

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
A = list(map(int, input().split()))
dp = [[-1] * (N + 1) for _ in range(N + 1)]

for x in range(N, -1, -1):
    for y in range(N, -1, -1):
        if x == y:
            continue
        X = Z if x == 0 else A[x - 1]
        Y = W if y == 0 else A[y - 1]
        if x == N or y == N:
            rst = abs(X - Y)
        elif x - y == 1:
            rst = 10 ** 10
            for j in range(N, x, -1):
                rst = min(rst, dp[x][j])
        elif x - y > 1:
            rst = dp[x][y + 1]
        elif y - x == 1:
            rst = 0
            for i in range(N, y, -1):
                rst = max(rst, dp[i][y])
        elif y - x > 1:
            rst = dp[x + 1][y]
        else:
            raise Exception
        dp[x][y] = rst

ans = 0
for i in range(N):
    ans = max(ans, dp[i + 1][0])
print(ans)

Submission status: Accepted

Solution code:
#!python3

# input
N, Z, W = list(map(int, input().split()))
A = list(map(int, input().split()))


def main():
    ans = abs(A[-1] - W)
    ans = max(ans, abs(A[-1] - A[-2])) if N > 1 else ans
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,z,w=[int(x) for x in input().split()]
 
a=[int(x) for x in input().split()]
 
if(n==1):
  print(abs(a[0]-w))
else:
  print(max(abs(a[-1]-w),abs(a[-1]-a[-2])))

Submission status: Accepted

Solution code:
n,z,w = map(int,input().split())
a = list(map(int,input().split()))

# 結局最後の2枚をどうするかで決まる
# 「先手がa[n-1]、後手がwのとき」か「先手がa[n-2」、後手がa[n-1]（逆パターンもあり）」の時しかありえない
if abs(a[n-1]-w) > abs(a[n-2]-a[n-1]) :
    print(abs(a[n-1]-w))
else :
    print(abs(a[n-2]-a[n-1]))

Submission status: Accepted

Solution code:
N,Z,W=map(int,input().split())
A=list(map(int, input().split()))
if N==1:
  print(abs(A[-1]-W))
else:
  a=abs(A[-1]-W)
  b=abs(A[-2]-A[-1])
  print(max(a,b))

Submission status: Accepted

Solution code:
import sys
n,z,w=map(int,input().split())
a=list(map(int,input().split()))
if n==1: print(abs(a[0]-w));sys.exit()

x,y=[-1]*n,[-1]*n
x[n-1]=abs(a[n-1]-w)
y[n-2]=abs(a[n-1]-a[n-2])

for i in range(n-2,-1,-1):
    x[i]=abs(a[i]-a[n-1])
    for j in range(i+1,n-1):
        x[i]=min(x[i],y[j])
    y[i]=abs(a[n-1]-a[i])
    for j in range(i+1,n-1):
        y[i]=max(y[i],x[j])

print(max(x))

Submission status: Accepted

Solution code:
n,z,w = map(int, input().split( ))
*a, = map(int, input().split( ))
#添え字間違えたのでa逆
a.reverse()
ans1 = 0
if len(a)>1:
    ans1 = abs(a[1]-a[0])
ans2 = abs(a[0]-w)
print(max(ans1,ans2))

Submission status: Accepted

Solution code:
n,z,w=map(int,input().split())
lists=list(map(int,input().split()))
if n==1:
    print(abs(lists[-1]-w))
else:
    print(max(abs(lists[-1]-w),abs(lists[-1]-lists[-2])))

Submission status: Accepted

Solution code:
n,z,w=map(int,input().split())
a=list(map(int,input().split()))
dp=[[0]*2 for i in range(n)]
infi=10**10
for i in reversed(range(n)):
  if i>=1:
    y=a[i-1]
    x=a[i-1]
  else:
    y=w
    x=z
  dp[i][0]=-infi
  dp[i][0]=max(dp[i][0],abs(y-a[n-1]))
  for j in range(i+1,n):
    dp[i][0]=max(dp[i][0],dp[j][1])
  dp[i][1]=infi
  dp[i][1]=min(dp[i][1],abs(x-a[n-1]))
  for j in range(i+1,n):
    dp[i][1]=min(dp[i][1],dp[j][0])
print(dp[0][0])

Submission status: Accepted

Solution code:
N, Y, Z = map(int, input().split())
As = list(map(int, input().split()))
dpx, dpy = [0]*(N-1), [0]*(N-1)
#dpx[i] = min(dpy[i+1]...dpy[N-2], abs(As[N-1]-As[i]))
#dpy[i] = max(dpx[i+1]...dpx[N-2], abs(As[N-1]-As[i]))
for i in range(N-2, -1, -1):
    mny, mxx = 10**10, 0
    for j in range(i+1, N-1):
        mny = min(mny, dpy[j])
        mxx = max(mxx, dpx[j])
    dpx[i] = min(mny, abs(As[N-1]-As[i]))
    dpy[i] = min(mxx, abs(As[N-1]-As[i]))
dpx.append(abs(As[N-1]-Z))
print(max(dpx))

Submission status: Accepted

Solution code:
N, Y, Z = map(int, input().split())
As = list(map(int, input().split()))
dpx, dpy = [0]*(N-1), [0]*(N-1)
#dpx[i] = min(dpy[i+1]...dpy[N-2], abs(As[N-1]-As[i]))
#dpy[i] = max(dpx[i+1]...dpx[N-2], abs(As[N-1]-As[i]))
for i in range(N-2, -1, -1):
    mny, mxx = 10**10, 0
    for j in range(i+1, N-1):
        mny = min(mny, dpy[j])
        mxx = max(mxx, dpx[j])
    dpx[i] = min(mny, abs(As[N-1]-As[i]))
    dpy[i] = max(mxx, abs(As[N-1]-As[i]))
dpx.append(abs(As[N-1]-Z))
print(max(dpx))

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
A = [int(x) for x in input().split()]
if N == 1:
    print(abs(A[0] - W))
else:
    print(max(abs(A[-2] - A[-1]), abs(A[-1] - W)))

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
a = list(map(int, input().split()))

if N == 1:
    print(abs(a[0] - W))
    exit()

ans1 = abs(a[-1] - a[-2])
if abs(a[-1] - W) > ans1:
    print(abs(a[-1] - W))
else:
    print(ans1)

Submission status: Accepted

Solution code:
import sys

N, Z, W = map(int, input().split())
A = list(map(int, input().split()))

ans = abs(A[-1] - W)
if N != 1:
    ans = max(ans, abs(A[-1] - A[-2]))
print(ans)

Submission status: Accepted

Solution code:
data = input().split(" ")
n = int(data[0])
z = int(data[1])
w = int(data[2])
a = input().split(" ")
if n != 1:
	b = int(a[n-2])
	c = int(a[n-1])
	ans = abs(w-c)
	if ans <= abs(b-c):
		ans = abs(b-c)
	print(ans)
else:
	print(abs(w-int(a[0])))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

"""
・そもそもとして、ゲームが長引くことはありえない
"""

N,Z,W,*A = map(int,read().split())

A = A[::-1]

if N == 1:
    answer = abs(A[0] - W)
    print(answer)
    exit()

x = abs(A[0] - W)
y = abs(A[1] - A[0])
answer = max(x,y)
print(answer)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ARC85 D

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

"""
山札がなくなると終了かつ必ず1枚は引かなくてはいけない
⇒最後の1枚は必ずどちらかの手になる(山札が1枚の場合、必ず自分の手になる)
先攻はそれを自分の手にするか、相手の手にするかを選べる
自分の手にする場合、1ターン目で全部の手札を取る
相手の手にする場合、1ターン目で最後から2番目まで全部取る
この2通りのみを考える
"""
n,z,w = LI()
a = LI()
if n == 1:
    ans = abs(a[0] - w)
else:
    ans = max(abs(a[-2]-a[-1]),abs(a[-1]-w))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,z,w = LI()
a = LI()
if n == 1:
    ans = abs(a[0]-w)
else:
    ans = max(abs(a[-2] - a[-1]),abs(a[-1] - w))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n, z, w = LI()
    a = LI()
    if n == 1:
        print(abs(a[0] - w))
    else:
        print(max(abs(a[-1] - w), abs(a[-2] - a[-1])))
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,x,y = LI()
    a = LI()
    ans = abs(a[-1]-y)
    if n > 1:
        ans = max(ans,abs(a[-2]-a[-1]))
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N,Z,W = map(int,input().split())
A = list(map(int,input().split()))
if N == 1:
  print(abs(A[0]-W))
  exit()
  
if abs(A[-1]-A[-2]) >= abs(A[-1]-W):
  print(abs(A[-1]-A[-2]))
else:
  print(abs(A[-1]-W))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def MI(): return map(int, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def main():
    n, z, w = MI()
    aa = LI()
    if n - 2 >= 0:
        print(max(abs(aa[n - 1] - aa[n - 2]), abs(aa[n - 1] - w)))
    else:
        print(abs(aa[n - 1] - w))

main()

Submission status: Accepted

Solution code:
N,X,Y = map(int,input().split())
A = list(map(int,input().split()))

if N == 1 :
    a = -1
    b = abs(A[0] - Y)
else :
    a = abs(A[N-1] - Y)
    b = abs(A[N-2] - A[N-1])

print(max(a, b))

Submission status: Accepted

Solution code:
# ABC078D - ABS (ARC085D)
def main():
    N, Z, W, *A = map(int, open(0).read().split())
    if N == 1:
        ans = abs(A[0] - W)
    else:
        ans = max(abs(A[-1] - W), abs(A[-2] - A[-1]))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, z, w = map(int, input().split())
a = list(map(int, input().split()))
print(max(abs(a[n-1]-w), abs(a[n-1]-a[n-2])))

Submission status: Accepted

Solution code:
n,z,w=map(int,input().split())
a=list(map(int,input().split()))
if n>1:print(max(abs(a[-1]-w),abs(a[-1]-a[-2])))
else:print(abs(a[-1]-w))

Submission status: Accepted

Solution code:

import sys
def input():
	return sys.stdin.readline().strip()

N, Z, W = list(map(int, input().split()))
a = list(map(int, input().split()))

dp = [[0]*(N + 1) for i in range(N + 1)]
dp[0][N] = abs(a[N-1] - Z)
dp[N][0] = abs(a[N-1] - W)
for i in range(1, N):
	dp[i][N] = abs(a[N - 1] - a[i - 1])
	dp[N][i] = dp[i][N]

for i in range(N - 1, 0, -1):
	min_score = min(dp[i][i + 1: N + 1])
	for j in range(i - 1):
		dp[i][j] = min_score
	max_score = 0
	for j in range(i + 1, N + 1):
		max_score = max(max_score, dp[j][i])
	for j in range(i - 1):
		dp[j][i] = max_score

score = 0
for i in range(1, N + 1):
	score = max(score, dp[i][0])

print(score)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
from collections import deque

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n, ia, ib = getList()
nums = getList()

if n == 1:
    print(abs(ib - nums[0]))
    sys.exit()

ans = abs(nums[-1] - nums[-2])
if abs(nums[-1] - ib) > ans:
    ans = abs(nums[-1] - ib)

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    a = LI()
    d = defaultdict(lambda:0)
    for i in a:
        d[i] += 1
    d = list(d.values())
    d.sort()
    ans = 0
    for i in range(len(d)-k):
        ans += d[i]
    print(ans)
    return

#B
def B():
    n,z,w = LI()
    a = LI()
    if n == 1:
        print(abs(a[0]-w))
        return
    ans = max(abs(a[-1]-w),abs(a[-2]-a[-1]))
    print(ans)
    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

Solution code:
n,z,w = map(int,input().split())
a = list(map(int,input().split()))

if n == 1:
    print(abs(a[0]-w))
else:
    print(max(abs(a[-1]-w), abs(a[-1]-a[-2])))

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
*A, = map(int, input().split())
if N == 1: print(abs(A[-1] - W))
else: print(max(abs(A[-1] - A[-2]), abs(A[-1] - W)))

Submission status: Accepted

Solution code:
n, x0, y0 = list(map(int, input().split()))

cards = [y0] + list(map(int, input().split()))
# yの手持ちはゲームに関与するため、リストに加えてしまう

xs = [[-1] * (n+1) for i in range(n+1)]
ys = [[-1] * (n+1) for i in range(n+1)] 
#xs[i][j] = xの手番で、xがcards[i]を持ちyがcards[j]を持っているとき(i<j)の最善スコア
#ys[i][j] = yの手番で、xがcards[j]を持ちyがcards[i]を持っているとき(i<j)の最善スコア

for i in range(n+1):
	xs[i][-1] = abs(cards[-1] - cards[i])
	ys[i][-1] = abs(cards[-1] - cards[i])

for j in range(n-1, -1, -1):

	# x[i][j] = max (y[j][j+1] , y[j][j+2] , ……, y[j][n] )
	xs_temp = max(ys[j][j+1:n+1])
	ys_temp = min(xs[j][j+1:n+1])
	for i in range(0, j):
		xs[i][j] = xs_temp
		ys[i][j] = ys_temp

# print(xs)
# print(ys)
print(max(ys[0][1:]))

Submission status: Accepted

Solution code:
N,Z,W=map(int,input().split())
A=list(map(int,input().split()))
an=A[-1]
A=[abs(a-an) for a in A]
W=abs(W-an)
if N==1:
    print(W)
else:print(max(W,A[-2]))

Submission status: Accepted

Solution code:
n,z,w=map(int,input().split())
A=[int(i) for i in input().split()]
print(abs(w-A[-1]) if n==1 else max(abs(A[-2]-A[-1]),abs(w-A[-1])))

Submission status: Accepted

Solution code:
n,z,w = map(int,input().split())
a = [int(i) for i in input().split()]

if n == 1:
  print(abs(a[-1]-w))
else:
  print(max(abs(a[-1]-w),abs(a[-2]-a[-1])))

Submission status: Accepted

Solution code:

N,Z,W=list(map(int,input().split()))
a=list(map(int,input().split()))

print(max(abs(W-a[N-1]),abs(a[N-2]-a[N-1])))

Submission status: Accepted

Solution code:
n, z, w = map(int,input().split())
a = list(map(int,input().split()))

if n == 1:
    print(abs(a[0]-w))
else:
    print(max(abs(a[n-2]-a[n-1]),abs(a[n-1]-w)))

Submission status: Accepted

Solution code:
n, z, w = map(int, input().split())
alist = list(map(int, input().split()))

if n == 1:
  print(abs(alist[0] - w))
else:
  print(max(abs(alist[-1] - w), abs(alist[-1] - alist[-2])))

Submission status: Accepted

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(2*10**5)

# 入力
N, Z, W = map(int, input().split())
a = list(map(int, input().split()))


# バックトラック法により解を求める関数。i=1のとき、Xの手番、i=-1のときYの手番
@lru_cache(maxsize=None)
def f(i, m):
    return (
        max(
            abs(W - a[-1]) if m == N else abs(a[-(m + 1)] - a[-1]),
            max(f(-i, k) for k in range(1, m))
        ) if i == 1 and m >= 2 else
        min(
            abs(a[-(m + 1)] - a[-1]),
            min(f(-i, k) for k in range(1, m))
        ) if i == -1 and m >= 2 else
        abs(a[-2] - a[-1]) if N >= 2 else
        abs(W - a[-1])
    )


# 出力
ans = f(1, N)
print(ans)

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
a = list(map(int, input().split()))
ans = abs(a[N-1]-W)
if N > 1:
  ans = max(ans,abs(a[N-2]-a[N-1]))
print(ans)

Submission status: Accepted

Solution code:
n,z,w=map(int,input().split())
A=[int(x) for x in input().split()]
print(max(abs(A[n-2]-A[n-1]),abs(A[n-1]-w))) if n!=1 else print(abs(A[0]-w))

Submission status: Accepted

Solution code:
n,z,w = map(int,input().split())
a = tuple(map(int,input().split()))

res = abs(a[-1]-w)
for i in range(n-1):
    cand = abs(a[-1]-a[i])
    for j in range(i+1,n-1):
        cand = min(a[j]-a[-1],cand)
    res = max(res,cand)
print(res)

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
a = list(map(int, input().split()))
print(max(abs(a[N - 1] - a[N - 2]), abs(a[N - 1] - W)))

Submission status: Accepted

Solution code:
n,z,w=map(int,input().split())
A=list(map(int,input().split()))
if n==1:
    print(abs(A[0]-w))
else:
    print(max(abs(A[n-1]-w),abs(A[n-2]-A[n-1])))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def main():
    n, z, w = input().split()
    n = int(n)
    w = int(w)
    a = list(map(int, input().split()))
    a = [w] + a

    sx = s_min = s_max = abs(a[n] - a[n - 1])
    for i in range(n - 1, 0, -1):
        sx = max(s_max, abs(a[n] - a[i - 1]))
        sy = min(s_min, abs(a[n] - a[i - 1]))
        s_max = max(s_max, sy)
        s_min = min(s_min, sx)

    print(sx)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N, Z, W = map(int, input().split())
    a = list(map(int, input().split()))
    retval = abs(a[-1] - W)
    if N >= 2:
        retval = max(retval, abs(a[-1] - a[-2]))
    print(retval)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, Z, W = map(int, input().split())
A = [int(a) for a in input().split()]

if N == 1: print(abs(A[0]-W))
else: print(max(abs(A[-2]-A[-1]), abs(A[-1]-W)))

Submission status: Accepted

Solution code:
N,Z,W=map(int,input().split())
a=[int(i) for i in input().split()]
if N==1:
  print(abs(W-a[0]))
else:
  print(max(abs(W-a[-1]),abs(a[-2]-a[-1])))

Submission status: Accepted

Solution code:
import sys
fin = sys.stdin.readline

N, Z, W = [int(elem) for elem in fin().split()]
a_list = [int(elem) for elem in fin().split()]
assert len(a_list) == N

if N == 1:
    print(abs(a_list[-1] - W))
else:
    print(max(abs(a_list[-1] - W), abs(a_list[-2] - a_list[-1])))