Problem:


Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.



Sample input:
20 3
5 10 15


Sample output:
10

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
listA = list(map(int,input().split()))
listB = []

for i in listA:
  if i > K/2:
    temp = i - K
    listB.append(temp)
  else:
    listB.append(i)
out1 = max(listA) - min(listA)
out2 = max(listB) - min(listB)
if out1 >= out2:
  print(out2)
else:
  print(out1)

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = [int(i) for i in input().split()]
a = np.array([int(i) - k if k - int(i) < int(i) else int(i) for i in input().split()])
r = 0
s = 0
for _ in range(1,n):
    m = np.mean(np.abs(s - a))
    r += a[np.argmin(abs(m - a))]
    np.delete(a,np.argmin(abs(m - a)))
print(r + a[0])

Submission status: Wrong Answer

Solution code:
import numpy as np

K, N = [int(element) for element in input().split(" ")]
AList = np.array([int(element) for element in input().split(" ")])
distance = np.diff(AList)
start_end_distance = abs((20 - AList[-1]) + AList[0])
distance = np.append(distance, start_end_distance)

distanceVal = K

for index, n in enumerate(distance):
    if distanceVal > (np.sum(distance) - n):
        distanceVal = np.sum(distance) - n

print(distanceVal)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
t = 0
id = 0
l = 0
for i in range(n):
    if i < n - 1:
        d = abs(a[i] - a[i + 1])
    else:
        d = k - a[n - 1] + a[0]
    if t < d:
        t = d
        id = i + 1 if i < n - 1 else 0
for i in range(n - 1):
    l += abs(a[(i + id) % n] - a [(i + id + 1) % n]) if i + id + 1 < n - 1 else k - a[n - 1] + a[0]
print(l)

Submission status: Wrong Answer

Solution code:
def main():
    H , W = map(int, input().split())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    value_temp = 0
    value_list = []
    i = 0
    #AA_list = []
    #for A in A_list:
    #    if A < H :
    #        AA_list.append(A)
    #    else :
    #        A = A - H
    #        AA_list.append(A)
    #AA_list.sort()
    #print(str(AA_list))
    for A in A_list :
        if i == 0 :
            A0 = A
            i = i + 1
            continue
        else :
            value_temp = A - A0
            A0 = A
            value_list.append(value_temp)
            #print(str(value_temp))
            #print(str(A))
    if W > 2 :
        value_temp = H - A0
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    else :
        ans = sum(value_list)

    #print(str(value_list))

    print (str(ans))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
N = N-1
A = list(map(int, input().split()))
A_d = []
[A[i+1]-A[i] for i in range(0, len(A)-1)]
A_d.append(K-A[-1]+A[0])
A_d2 = (A_d+A_d[1:])*2
res = K
for i in range(len(A_d2)-1):
    if res > sum(A_d2[i:i+N]):
        res = sum(A_d2[i:i+N])
print(res)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=0
s=[0]*n
#間隔を求める
for i in range(n-1):
    s[i]=a[i+1]-a[i]
#間隔を足す
for i in range(n):
  if s[i]<k/3:
    b=b+s[i]
  else:
    con=k-s[i]
    print(con)
    exit()
  
print(b)

Submission status: Wrong Answer

Solution code:
from collections import deque
K, N = map(int, input().split())
S=deque(list(map(int, input().split())))
ans = [S[-1] - S[0]]
for i in range(N-1):
    ans.append(S[i]- S[i+1] + 20)
print(min(ans))

Submission status: Wrong Answer

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

if a[0] == 0 and len(a) <= 3:
    print(max(a[1:])-min(a[1:]))
    exit()
elif a[0] == 0 and len(a) > 3:
    print(max(a))
    exit()
else:
    print(max(a) - min(a))

Submission status: Wrong Answer

Solution code:
kn = list(map(int,input().split()))
a = list(map(int,input().split()))

k = kn[0]
n = kn[1]


dis = []

for i in range(n):
    if i == 0:
        dis.append(min((a[n-1] - a[0]), (k - a[n-1] + a[0])))
    else:
        dis.append(min((a[i] - a[i-1]), (k - a[i] + a[i-1])))

print(dis)
del dis[dis.index(max(dis))]

print(sum(dis))

'''
dis = [[0]*n]

for i in range(n):
    for j in range(n):
        if i+j+1 >= n-1:
            dis[i][j] = k - a[i+j+1-(n-1)] + a[i+j+1-(n-1)-1]
        else:
            dis[i][j] = a[i+j+1] - a[i+j]

    dis.append([0]*n)
    print(dis)

print(dis)
'''

Submission status: Wrong Answer

Solution code:
K, N = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
if N==2:
    print(A[1]-A[0])
    exit()
A = [0] + A + [K]
dx = []
for i in range(N+1):
    dx.append(A[i+1] - A[i])
m = max(dx)
print(K-m if dx.index(m) != 0 else sum(dx[1:-1]))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
listA = list(map(int,input().split()))
listB = []

for i in listA:
  if i > K/2:
    temp = i - K
    listB.append(temp)
  else:
    listB.append(i)
print(listB)
out1 = max(listA) - min(listA)
out2 = max(listB) - min(listB)
if out1 >= out2:
  print(out2)
else:
  print(out1)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

zero = False
if 0 in a:
    a.remove(0)
    n = n - 1
    zero = True
    #print("contain 0.")

l1 = a[n-1] - a[0]  # 0を通らない側
l2 = a[0] + k - a[n-1]  # 0を通る側
if zero == True:
    ret = l2
else:
    ret = l1 if l1 < l2 else l2
print("{}".format(ret))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
if a[-1]-a[0]<=(k-a[-1])+a[0]:
    print(a[-1]-a[0])
else:
    for i in range(n-1):
        if a[-1]-a[i]>(k-a[-1])+a[i]:
            start=i
        else:
            print(((k-a[-1])+a[start])+(a[-1]-a[start+1]))
            break

Submission status: Wrong Answer

Solution code:
def samax_index(A):
    samax = 0
    max_i = 0
    prev_v = 0
    for i, v in enumerate(A):
        if samax <= (v - prev_v):
            max_i = i
            samax = v - prev_v
        prev_v = v
    return max_i


def distance(A, start_i, N):
    total = 0
    if A[1] == 0:
        total = A[start_i]
    else:
        total = A[start_i] - A[1]

    if start_i == N:
        print(total)
        return
    else:
        total += A[-1] - A[start_i + 1]

    print(total)


K, N = map(int, input().split())

A = list(map(int, input().split()))
A.append(K)
A.insert(0, 0)
start_i = samax_index(A) - 1


distance(A, start_i, N)

Submission status: Wrong Answer

Solution code:
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
dist=[0]*(n)

for i in range(n-1):
    dist[i]=a[i+1]-a[i]
dist[n-1]=k-a[n-1]+a[0]

right=a[n-1]-a[0]
left=k-dist[0]

ret=min(left,right)

for i in range(1,n):
    right+=dist[(i+n-2)%n]-dist[i-1]
    left+=dist[i-1]-dist[(i+n-2)%n]
    ret=min(left,right)

print(ret)

Submission status: Wrong Answer

Solution code:
from collections import deque
k,n = map(int,input().split())
a = deque(list(map(int,input().split())))
b = []
for i in range(n):
	b.append(a[i]-k)
b = deque(reversed(sorted(b)))
for i in range(n):
	a.appendleft(abs(b[i]))
a = list(a)
x = []
sa = []
#print(a)
sb = []
for i in range(len(a)):
	sa.append(sum(a[:i+1]))
for i in range(len(a)-n):
	sb.append(sa[i+n]-sa[i])

print(min(sb))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
if l[0]==0:
    l=l[1:]
t=l[0]
p=0
for val in l:
    p+=(val-t)
    t=val

l=l[::-1]
t=l[0]
r=0
for val in l:
    r+=(t-val)
    t=val
print(min(r,p))

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
l= list(map(int,input().split()))
li = []

for i in range(n):
  if i != n-1:
    li.append(l[n-i-1]-l[n-i-2])
  else:
    li.append(k-l[n-1]-l[0])

li.sort(reverse=True)
print(k-li[0])

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
A = [int(N) for N in input().split()]
#print(A[0])
max1 = 0
max2 = 0
for i in range(0,N-1):
    max1 = A[i+1]-A[i]
    if max1>max2:
        max2=max1
print(20-max2)

Submission status: Wrong Answer

Solution code:
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.append(k)

m = 0
ans = 0
for i in range(1, n + 1):
    x = a[i] - a[i - 1]
    ans += x
    m = max(m, x)
print(ans - m)

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
A = list(map(int, input().split()))
ans1 = A[N-1]-A[0]
ans2 = K//2
if ans1 < ans2:
    print(ans2)
else:
    print(ans1)

Submission status: Wrong Answer

Solution code:
K, N = input().split()
A = list(map(int, input().split()))
if 0 in A:
   A.remove(0)
x = max(A) - min(A)
print(x)

Submission status: Wrong Answer

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

print(min(K - A[-1] + A[-2] - min(A),max(A)-min(A)))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
for i in range(0, N):
    A[i] -= K
ans = 0
for a in A:
    ans = a-min(A)
print(ans)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
diff=0
for i in range(len(a)-1):
  diff_a=a[i+1]-a[i]
  if diff<diff_a:
    diff=diff_a
if a[n-1]-a[0]>diff:
  diff=a[n-1]-a[0]
print(k-diff)

Submission status: Wrong Answer

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

A.insert(0, 0)
A.append(K)

start = A[1] - A[0]
goal = A[-1] - A[-2]

mixi = 0
sum_len = 0

longdis = max(start, goal)

for i in range(len(A) - 1):
    if A[i+1] - A[i] >= mixi:
        mixi = A[i+1] - A[i]
    sum_len += A[i+1] - A[i]

if max(longdis, mixi) == longdis or longdis == mixi:
    result = sum_len - start - goal
elif max(longdis, mixi) == mixi:
    result = sum_len - mixi

print(result)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

zero = False
if 0 in a:
    a.remove(0)
    n = n - 1
    zero = True
    print("contain 0.")

l1 = a[n-1] - a[0]  # 0を通らない側
l2 = a[0] + k - a[n-1]  # 0を通る側
if zero == True:
    ret = l2
else:
    ret = l1 if l1 < l2 else l2
print("{}".format(ret))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
x=[]
y=[]
for i in A:
  if i>10:
    a=20-i
    y.append(a)
  else:
    x.append(i)
if len(x)==0:
  ans=max(y)-min(y)
elif len(y)==0:
  ans=max(x)-min(x)
elif (10-min(x))+(10-min(y))>=max(x)+max(y):
  ans=max(x)+max(y)
elif (10-min(x))+(10-min(y))<=max(x)+max(y):
  ans=(10-min(x))+(10-min(y))
print(ans)

Submission status: Wrong Answer

Solution code:
# 初期入力
K,N = (int(x) for x in input().split(" "))
A = [0]
for i in input().split():
    if i !="0":
        A.append( int(i) )

# 1軒ずつ距離のリスト
x =[A[i+1] - A[i] for i,v in enumerate(A[:-1]) ]
x.append(K - A[-1] +A[1])

#　求めるのは　K　－　max(隣と離れた家の距離)
kyori_min = K - max(x)
print(kyori_min)

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))

    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])
    sum_d = sum(d)
    d = tuple(d)
    
    most_min_path = 10000

    for i in range(n):
        if i == 0:
            normal_ck = sum_d - d[i] - d[-1]
            reverse_ck = sum_d - d[i+1]
        elif i == n - 1:
            normal_ck = sum_d - d[i]
            reverse_ck = sum_d - d[i+1] - d[0]
        else:
            normal_ck = sum_d - d[i]
            reverse_ck = sum_d - d[i+1]

        min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
        
        if most_min_path > min_ck:
            most_min_path = min_ck

    print(most_min_path)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
house = list(map(int,input().split()))
if house[0] == 0:
    for i in house[1:]:
        if i > 0:
            print(house[-1] - i)
            break
else:
    print(house[-1] - house[0])

Submission status: Wrong Answer

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

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    sorted(A)
    d = [A[i]-A[i-1] for i in range(1,N)]
    d.append(K - max(A) + min(A))
    ans = min(K-max(d), max(d))
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
mini=0
for i in range (N):
  if(i==0 or i%2==0):
    mini=mini+A[i]
  else : 
    mini=mini-A[i]
print(mini)

Submission status: Wrong Answer

Solution code:

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

for i in range (0, N-1):
    if A[i] > K/2:
        A[i]=A[i]-K
    else:
        A[i]=A[i]

A_sorted=sorted(A)
a=A_sorted[N-1]-A_sorted[0]
b=K-a

print(min(a, b))

Submission status: Wrong Answer

Solution code:
K,N = input().split()
A = input().split()
 
a_list = list(A)

dist = 0
dist_list = []
max = 0
sum = 0
 
for i in range(len(a_list)):
  if i == len(a_list) - 1:
    dist = int(K) - abs(int(a_list[i]) - int(a_list[0]))
  else:
    dist = abs(int(a_list[i]) - int(a_list[i + 1]))
  if i != 0: 
    if max < dist:
        sum += max
        max = dist
    else:
        sum += dist
  else:
      max = dist
  print(sum,dist)
    
print(sum)

Submission status: Wrong Answer

Solution code:
line1 = input()
k, n = line1.split(" ")
line2 = input()
num_list = line2.split(" ")
num_list = [int(num) for num in num_list]
k = int(k)
n = int(n)

ans1 = num_list[-1] - num_list[0]
ans2 = k-num_list[-1]+num_list[-2]

if ans1 <= ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

Solution code:
kn = list(map(int, input().split()))
a = list(map(int, input().split()))

k = kn[0]
n = kn[1]

list = []
for i in range(n):
    if i == 0:
        list.append(a[i])
    else:
        list.append(a[i] - a[i-1])

    if i == n-1:
        list.append(k - a[i])

ans = 0
flag = False
for ele in list:
    ans += ele

max = max(list)
if a[n-1] > k/2:
    secondMax = sorted(list)[-2]
    ans -= max+secondMax
else:
    ans -= max
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
result = 0
mx = -float("inf")
for i in range(N-1):
    mx = max(mx, A[i+1] - A[i])
mx = max(mx, K - A[-1])

result = K - mx + A[0]
print(result)

Submission status: Wrong Answer

Solution code:
k,m = map(int,input().split())
a = input().split()
a =[int(s) for s in a]
a.sort(reverse=True)
a.insert(0,k)

dis = []

for i in range(m):
  part = int(a[i]) - int(a[i+1])
  dis.append(part)
  del part

dis.append(int(a[m-1]))
dis.sort()

print(sum(dis[:m-1]))

Submission status: Wrong Answer

Solution code:
#k,n
k,n = (int(x) for x in input().split())

#a
a = list(map(int, input().split()))

ans1 = 0
ans2 = 0
max = 0

a.sort()
"""
if k - a[n-1] < a[0] - 0:
"""
a.append(k)

for i in range(n):
    ans1 += a[i+1] - a[i]
    if a[i+1] - a[i] > max:
        max = a[i+1] - a[i]

ans1 -= max
print(ans1)

Submission status: Wrong Answer

Solution code:
k, n = map(int,input().split())
an = list(map(int, input().split()))

#print(an)

an.append(k)

diff = list()
for ii in range(0,n):
  diff.append(an[ii+1] - an[ii])
  
allsum = sum(diff)
cand = list()
for ii in range(0,len(diff)):
  cand.append(allsum - diff[ii])
  
print(min(cand))

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))

    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])

    d = tuple(d)
    sum_d = sum(d)
    
    most_min_path = 10000

    for i in range(n):
        if i == 0:
            normal_ck = sum_d - d[i] - d[-1]
            reverse_ck = sum_d - d[i+1]
        elif i == n - 1:
            normal_ck = sum_d - d[i]
            reverse_ck = sum_d - d[i+1] - d[0]
        else:
            normal_ck = sum_d - d[i]
            reverse_ck = sum_d - d[i+1]

        min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
        if most_min_path > min_ck:
            most_min_path = min_ck

    print(most_min_path)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
c= map(int,input().split())
c_list = list(c)
cc = len(c_list)
if c_list[cc-1] - c_list[cc-2] < a - c_list[cc-1] + c_list[0] :
  d = c_list[cc-1] - c_list[0]
else:
  d = a - c_list[cc-1] + c_list[0] + c_list[cc-2] - c_list[0]
print(d)

Submission status: Wrong Answer

Solution code:
from collections import deque
A = deque()
K, N = map(int, input().split())
A = list(map(int, input().split()))
max = 0
for i in range(N-1):
    if abs(A[i]-A[i+1]) > max:
        max = abs(A[i]-A[i+1])
if abs(A[0]-(K-A[N-1])) > max:
    max = abs(A[0]-A[N-1])
print(K - max)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split()) 
l=list(map(int,input().split()))
ans=[]
for i in range(len(l)):
  ans.append(sum(min(abs(l[j]-l[i]),abs(k-l[j])) for j in range(len(l)) if i!=j)) 
  print(min(ans))

Submission status: Wrong Answer

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

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    d = [A[i]-A[i-1] for i in range(1,N)]
    d.append(K - max(A) + min(A))
    ans = min(K-max(d), max(d))
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
K, N = map(int, input().split())
S=deque(list(map(int, input().split())))
ans = [S[-1] - S[0]]
for i in range(N-1):
    S.appendleft(S[-(i+1)] - K)
ans = 0
for i in range(N):
    if i == 0:
        ans = S[i+2] - S[i]
    else:
        if S[i+2] - S[i] < ans:
            ans = S[i+2] - S[i]

print(ans)

Submission status: Wrong Answer

Solution code:
l, num = input().split()
point = input().split()

l = int(l)
num = int(num)

point0 = int(point[0])

rpoint = -1
for p in point:
  p = int(p)
  if l / 2 > p:
  	rpoint = p
    
lpoint = int(point[-1])

if(rpoint == -1):
  print(lpoint - rpoint)
elif rpoint == lpoint:
  print(lpoint - point0)
else:
  if rpoint == lpoint:
    print(rpoint - point0)
  elif rpoint < lpoint:
    if rpoint == point0:
      if(lpoint - rpoint < rpoint + l - lpoint):
        print(lpoint - rpoint)
      else:
        print(rpoint + l - lpoint)
    else:
      ltop = rpoint - point0 + point0 + l - lpoint
      lunder = rpoint - point0 + lpoint - rpoint
      if ltop > lunder:
        print(lunder)
      else:
        print(ltop)

Submission status: Wrong Answer

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

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    d = [A[i]-A[i-1] for i in range(1,N)]
    d.append(K - max(A) + min(A))
    ans = min(K-max(d), max(d))
    print('d:',d)
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def resolve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    max_distance = 0
    distance = []

    for i in range(1, len(A)):
        tmp = A[i] - A[i - 1]
        distance.append(tmp)
        if max_distance < tmp:
            max_distance = tmp
            start_index = i
            goal_index = i - 1

    ans = sum(distance[start_index:]) + sum(distance[: goal_index])

    if (start_index > goal_index):
        ans += K - A[-1]
    print(ans)


resolve()

Submission status: Wrong Answer

Solution code:
syu,n=map(int,input().split())
a=[int(i) for i in input().split()]
clock=a[n-1]-a[0]
han_clock=(a[n-2]-a[0])+(a[0]+syu-a[n-1])
print(min(clock,han_clock))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=0
for i in range(N-2):
    if (A[i+2]-A[i+1])>=(A[i+1]-A[i]):
        l=A[i+2]-A[i+1]
if (K-A[N-1]+A[0])>=l:
    l=K-A[N-1]+A[0]
print(K-l)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
if len(a) == n:
    if 0 in a:
        a.remove(0)
    a.sort()
    ans = 0
    n = 1
    for i in a:
        try:
            ans += a[n] - a[n-1]
            n += 1
        except:
            pass
    print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
if 0 in A:
    A.remove(0)
num = (N-2)-N
l = []
for i in range(1,len(A)+1):
    if i == len(A):
        l.append(A[-i])
        break
    ans = A[-i] - A[num]
    num -= 1
    l.append(ans)
a = sum(l)
ans = (K - a) + A[0]
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = input().split()
A = list(map(int, input().split()))
x = max(A) - min(A)
print(x)

Submission status: Wrong Answer

Solution code:
n, m = input().split()
A = sorted([int(x) for x in input().split()])

print(max(A)-min(A))

Submission status: Wrong Answer

Solution code:
k, n = map(int,input().split())

a = list(map(int, input().split()))

# x = distance between the first and last house
x = (a[-1] - a[0])
# y = k - x (the "other" distance) 
y = (k - x)

if x < y:
  print (x)
elif y < x:
  print (y)
elif y == x:
  print (x)
else:
  print (x)

Submission status: Wrong Answer

Solution code:
k,n=input().split()
k=int(k)
n=int(n)
l=list(map(int, input().split()))
ans=[0]
num=l[0]
for i in range(1,len(l)):
    ans.append(min(abs((k-l[i])+num),abs(l[i]-l[i-1])))
print(sum(ans))

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
li = list(map(int,input().split()))
maxm=max(li)
minm=min(li)
m1=maxm-li[0]
m2=maxm-minm
ttl=x-maxm
ttk=maxm-minm
if ttl > ttk:
  print(m2)
else:
  print(maxm-ttl)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
# if 10 >= a[0] > 0:
#     sub_value = a[0]
#     for x in range(n):
#         a[x] -= sub_value
#         if a[x] > 10:
#             a[x] = k - a[x]
#     a.sort()
# elif a[0] > 10:
#     a[0] = k - a[0]
#     sub_value = a[0]
#     a.sort()
#     for x in range(n):
#         a[x] -= sub_value
#         if a[x] > 10:
#             a[x] = k - a[x]
# else:
for x in range(n):
    if a[x] > 10:
        a[x] = k - a[x]
# ans = sum(a)
# print(k - ans)

if 0 in a:
    rem = a.index(0)
    del a[rem]
    print(a[0] + a[-1])
    exit()
else:
    print(a[0] + a[-1])

Submission status: Wrong Answer

Solution code:
import copy

K, N = map(int, input().split())
li = list(map(int, input().split()))
half = K/2
re_li = [v-K if v > half else v for v in li]
li2 = copy.copy(li)
if 0 in li2:
    del li2[0]
    li2.append(K)
dist1 = max(li)-min(li)
dist2 = max(re_li)-min(re_li)
dist3 = max(li2)-min(li2)
if dist1 < dist2 and dist1 < dist3:
    print(dist1)
elif dist2 < dist1 and dist2 < dist3:
    print(dist2)
else:
    print(dist3)

Submission status: Wrong Answer

Solution code:
s = input().split(" ")
k = int(s[0])
n = int(s[1])

d = input().split(" ")
dist = []

for i in range(0,n):
    dist.append(int(d[i]))

res = dist[n-1] - dist[0]

for i in range(i, n):
    diff = dist[i] - dist[i-1]
    aux = k - diff
    if aux < res:
        res = aux

print(str(res))

Submission status: Wrong Answer

Solution code:
'''

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

'''

'''o=input().rstrip().split(' ')
x=int(o[0])
y=int(o[1])
a=int(o[2])
b=int(o[3])
c=int(o[4])
H=0;
R=input().rstrip().split(' ')
G=input().rstrip().split(' ')
ANY=input().rstrip().split(' ')
R.sort(key=int,reverse=True)
G.sort(key=int,reverse=True)
ANY.sort(key=int,reverse=True)
while(x>0):
    if int(R[0]) > int(ANY[0]):
        H+=int(R[0])
        del(R[0])
        x-=1;
    else:
        break;
while(y>0):
    if int(G[0]) > int(ANY[0]):
        H+=int(G[0])
        del(G[0])
        y-=1;
    else:
        break;
if(x>0 && y==0):
    for i in range(0,x):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(R[0])
            del(R[0])
elif(y>0 && x==0):
    for i in range(0,y):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(G[0])
            del(G[0])
else:
    FF=x+y;
    if FF<=len(ANY):
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
    else:
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
        XX=0;
        YY=0;
        for i in range(0,x):
            '''
            
            
'''

20 3
0 5 15

'''


o=input().rstrip().split(' ')
p=input().rstrip().split(' ')
l=[]
for i in range(1,len(p)):
    l.append(int(p[i]) - int(p[i-1]))
l.append(int(o[0])-int(p[len(p)-1]))
q=[]
C=list(p)
A=list(C[0])
B=C[1:len(C)];
B.reverse();
C=A+B;
#print(C)
for i in range(1,len(C)):
    q.append(int(o[0]) - int(C[i]))
q.append(int(o[0]) - int(C[0]))
#print(l,q)
S=0
D=0
for i in range(0,len(l)):
    S+=int(l[i]);
    D+=int(q[i])
W=[]
minu = 10000000000;
for i in range(0,len(l)):
    if i==0:
        F=S-int(l[len(l)-1])
        minu = min(minu,F)
    else:
        F=S-int(l[i-1])
        minu = min(minu,F);
for i in range(0,len(q)):
    if i==0:
        F=D-int(q[len(l)-1])
        minu = min(minu,F)
    else:
        F=D-int(q[i-1])
        minu = min(minu,F);
print(minu)

Submission status: Wrong Answer

Solution code:
def main():
  K,N = map(int, input().split())
  A_list = list(map(int, input().split()))

  space_max = 0

  if A_list[N-1] - A_list[0] <= K/2:
    print(A_list[N-1] - A_list[0])
    exit()
  else:
    pass

  for i in range(N-1):
    space =  A_list[i+1] - A_list[i]
    if space > space_max:
      space_max = space
    else:
      pass
  
  if space_max >= K/2:
    print(K-space_max)
  else:
    print(A_list[N-1] - A_list[0])


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

A.insert(0, 0)
A.append(K)

start = A[1] - A[0]
goal = A[-1] - A[-2]

mixi = 0
sum_len = 0

for i in range(len(A) - 1):
    if A[i+1] - A[i] >= mixi:
        mixi = A[i+1] - A[i]

    sum_len += A[i+1] - A[i]

if max(start, goal, mixi) == start or max(start, goal, mixi) == goal:
    result = sum_len - start - goal
else:
    result = sum_len - mixi

print(result)

Submission status: Wrong Answer

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


l_s = []
check = 0
for i in range(0,N):
    if(A[i] == 0):
        check =1
if(check == 1):
    for i in range(1,N):
          l_s.append(A[i]-A[i-1])
    l_s.append(K-A[N-1])
    num = 0
    ans = 99999999999999999
    for i in range(0,N):
        num = K - l_s[i]
        ans = min(ans,num)
    print(ans)
    
else: 
    m1 = 0
    m2 = 0
    m1 = max(A)
    m2 = min(A)
    ans = m1 -m2
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(len(a)):
  if i == len(a) - 1:
    distance.append(k - a[i])
  else:
  	distance.append(a[i+1] - a[i])
distance.sort()
print(sum(distance[:-1]))

Submission status: Wrong Answer

Solution code:
k, n = [int(x) for x in input().split()] 
myl=list(map(int,input().split()))

myl.sort()
dis=0
for x in range(0,n-1):
	temp=myl[x+1]-myl[x]
	if(temp>=0 and temp<=k):
		dis+=temp

print(dis)

Submission status: Wrong Answer

Solution code:
N = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

a = 0
for i in range(N[1] - 1):
    if M[i+1] - M[i] >= N[0]/2:
        a = a + abs(M[i + 1] - N[0])
    else:
        a = a + M[i + 1] - M[i]

print(a)

Submission status: Wrong Answer

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

A.sort()

reverse = None

for i in range(len(A)-1):
    j = i+1
    if j > N-1:
        j -= N
    if (K-A[j]) + A[0] < A[j] - A[i]:
        reverse = j
        break
if reverse is not None:
    A = [A[i] if i < reverse else A[i] - K  for i in range(len(A))]
A.sort()
print(max(A) - min(A))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
x=list(map(int,input().split()))
xx=list(set(x))[::-1]
y=[]
for j in range(n-1):
    y.append(xx[j]-xx[j+1])
y.append(k-xx[0])
print(k-max(y))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.append(n)

s=0
check=0
f=0
save=0
for i in range(1,k):
	s=s+l[i]-l[i-1]
	if(f==0 and l[i] > n//2):
		check=i
		f=1
	else:
		if(f==0):
			save=s

s1=0
#print(save,check)
for i in range(check+1,k+1):
	s1=s1+abs(l[i]-l[(i-1)])

#print(s1,save)
s1=s1+save
#print(s,s1)

save1=0
s00=0
check1=0
f1=0
for i in range(1,k):
	s00=s00+l[i]-l[i-1]
	if(f1==0 and l[i] >= n//2):
		check1=i
		f1=1
	else:
		if(f1==0):
			save1=s00

s11=0
for i in range(check1+1,k+1):
	s11=s11+abs(l[i]-l[(i-1)])
s11=s11+save1
print(min(s,min(s1,s11)))

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
c=list(map(int,input().split()))
c.sort()
d=[0]*1000000
for i in range(b-1):
  d[i]+=abs(c[i]-c[i+1])

d.append(a-c[-1])

print(a-max(d))

Submission status: Wrong Answer

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

# 時計回り
if a[0] + a[n-1] >= k:
    print(a[n-1] - a[0])

# 反時計回り
else:
    max_n = 0
    for i in range(n-1):
        diff = abs(a[i] - a[i+1])
        max_n = max(diff, max_n)
    dif = abs(a[0] - a[n-1])
    max_n = max(diff, max_n)
    print(k - max_n)

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))

    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])

    d = tuple(d)
    sum_distance = sum(d)
    
    most_min_path = 10000

    for i in range(n):
        if i == 0:
            normal_ck = sum_distance - d[i] - d[-1]
            reverse_ck = sum_distance - d[i+1]
        elif i == n - 1:
            normal_ck = sum_distance - d[i]
            reverse_ck = sum_distance - d[i+1] - d[0]
        else:
            normal_ck = sum_distance - d[i]
            reverse_ck = sum_distance - d[i+1]

        min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
        if most_min_path > min_ck:
            most_min_path = min_ck

    print(most_min_path)


if __name__ == '__main__':
    main()