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())
a = list(map(int,input().split()))

hanbun = k /2
a.sort
print(a)
i = 0
while True:
    if a[i] != 0:
        end = a[i]
        break
    else:
        i += 1
start = a[n-1]


result = start - end
if result < 0:
    result = result * -1
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def rMap():
	return map(int,input().split())
def rList():
	return list(map(int,input().split()))
k,n = rMap()
a = rList()
x = min(a)
y = max(a)
first = y-x
for i in range(n):
	if a[i]>=k//2:
		a[i] -=k
x = min(a)
y = max(a)
second = y-x
print(min(first,second))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
x=list(map(int,input().split()))
xxx=[]
for i in x:
    if i==k:
        xxx.append(0)
    else:
        xxx.append(i)
xx=list(set(xxx))
y=[]
for i in x:
    if i*2>=k:
        y.append(i-k)
    elif i==k:
        y.append(0)
    else:
        y.append(i)
yy=list(set(y))
print(min(max(xx)-min(xx),max(yy)-min(yy)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    import numpy as np
    a = np.array(a)

    kouho = a[0] + (k - a[-1])

    sa = a[1:] - a[0: -1]

    kouho1 = max(sa)

    print(max(kouho,kouho1))

Submission status: Wrong Answer

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

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

dist = A[0] + (K-A[N-1])

for i in range(N-1):
    if (dist < A[i+1]-A[i]):
        dist = A[i+1]-A[i]

print(dist)

Submission status: Wrong Answer

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

# ans = []
# for i in range(len(A) - 1):
#     base = abs(A[i+1] - A[i])
#     comp = abs(A[0] - (K - A[i + 1]))
#     print(base, comp, sep=',')
#     if base < comp:
#         ans.append(base)
#     else:
#         ans.append(comp)

ans = []
for a in A:
    if a > abs(a - K):
        ans.append(a - K)
    else:
        ans.append(a)

ans.sort()
print(sum([ans[i + 1] - ans[i] for i in range(len(ans) - 1)]))

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]))

ex = dist[0] + k - dist[n-1]
res = dist[n-1] - dist[0]

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

print(str(res))

Submission status: Wrong Answer

Solution code:
inputNumber = input()
K = int(inputNumber.split(" ")[0])
N = int(inputNumber.split(" ")[1])

inputNumber = input()
A=inputNumber.split(" ")

A_Length=len(A)

for i in range(A_Length):
    A[i] = int(A[i])

result=0

for i in range(A_Length):
    if i == A_Length - 1:
        if result < (K - A[i]) + A[0]:
            result = (K - A[i]) + A[0]
    else:
        if result < A[i] - A[i-1]:
            result = A[i+1] - A[i]

result = K - result
print(result)

Submission status: Wrong Answer

Solution code:
I=lambda:list(map(int,input().split()))
p,n=I()
l=I()
ne=[]
ma=10**9+7
for i in range(n-1):
    ne.append(abs(l[i]-l[i+1]))
ne.append(abs(p-l[-1]))
g=sum(ne)
for i  in ne:
    ma=min(ma,g-i)
print(ma)

Submission status: Wrong Answer

Solution code:
k=int(input().split(" ")[0])
a = list(map(lambda x: int(x), input().split("\n")[0].split(" ")))
df = max(a) - min(a)
print(min(df, k - df))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans1=max(a)-min(a)

for i in range(n):
    if (k//2)<a[i]:
        li.append(a[i]-k)
    else:
        li.append(a[i])

ans2=max(li)-min(li)

print(min(ans1,ans2))

Submission status: Wrong Answer

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

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

A = sorted(a)

maxl = A[0] - A[N-1]

if maxl <= 0:
    maxl = -1 * maxl


for i in range(N-1):
    l = A[i] - A[i+1]

    if l <= 0:
        l = -1 * l
    
    if maxl < l:
        maxl = l

ans = K - maxl

print(ans)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [0]*n
a.sort()
amin = a[n-1] - a[0]
for i in range(n):
  if a[i] < k//2:
    b[i] = a[i] + k
  else:
    b[i] = a[i]
b.sort()
bmin = b[n-1] - b[0]
print(a,b)
print(min(amin,bmin))

Submission status: Wrong Answer

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

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

A_kyori = []
for i in range(N - 1):
    A_kyori.append(A_list[i + 1] - A_list[i])

A_kyori.append(K - A_list[-1])

print(sum(A_kyori) - max(A_kyori))

Submission status: Wrong Answer

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

for i in range(n-1):
  length.append(a[i+1] - a[i])

length.append(a[0] + k - a[n-1])

print(length)
max_length = max(length)
ans = k- max_length

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    if i == 0:
        continue
    dlst.append(lst[i] - lst[i - 1])

if dlst[len(dlst) - 1] > k - lst[n - 1]:
    dlst[len(dlst) - 1] = k - lst[n - 1]

print(sum(dlst))

Submission status: Wrong Answer

Solution code:
K,N=list(map(int,input().split(' ')))
A=list(map(int,input().split(' ')))
k=0
Anew=[]
for i in range(1,N):
    Anew.append(abs(A[i]-A[i-1]))
Anew.append(abs(K-A[-1]+A[0]))
print(Anew)
print(K-max(Anew))

Submission status: Wrong Answer

Solution code:
k,n=input().split()
k=int(k)
n=int(n)
l=list(map(int, input().split()))
s=sum(l)
k2=k//2
if s%k2==0:
    print(k2)
else:
    print(s%k2)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
mtrx=list(map(int,input().split()))
mtrx.append(K)
length = []
for i in range(len(mtrx)-1):
    length.append(mtrx[i+1]-mtrx[i])
print(K-max(length))

Submission status: Wrong Answer

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

tmp = [0] * n
for i in range(n):
    tmp[i] = li[i] - k
li = li + tmp
li.sort()
ans = 10**9
for i in range(len(li)-2):
    ans = min(ans, abs(li[i] - li[i + 2]))
print(ans)

Submission status: Wrong Answer

Solution code:
#スペース区切りの整数の入力
K, N = map(int, input().split())
#文字列の入力
A = input()
#コンマで区切る
A_list = A.split(' ')
for i in range(len(A_list)):
    A_list[i] = int(A_list[i])

    
    
amari = K-int(A_list[N-1])


aida_list = A_list
for i in range(len(A_list)-1):
  aida_list[i] = A_list[i+1] - A_list[i]
aida_list[len(A_list)-1] = amari

max_ = max(aida_list)

aida_list.remove(max_)

ans = sum(aida_list)


print(ans)

Submission status: Wrong Answer

Solution code:
info = list(map(int,input().split()))
k=info[0]
n=info[1]
a = list(map(int,input().split()))
MAX=0

for i in range(n-1):
    if a[i+1]-a[i] > MAX:
        MAX=a[i+1]-a[i]
if abs(k-a[n-1]) > MAX:
        MAX=abs(k-a[n-1])
print(str(k-MAX))

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
A = [int(x) for x in input().split()]
c=[]
for i in range(n-1):
  c.append(A[i+1]-A[i])
c.append(k-A[n-1])
print(15-min(c))

Submission status: Wrong Answer

Solution code:
K,N =map(int,input().split())
list_A =input().strip().split()
list_B=[]
list_B.append(int(list_A[0])+(K-int(list_A[-1])))
for i in range(N-1):
    list_B.append(int(list_A[i+1])-int(list_A[i]))
print(max(list_B))

Submission status: Wrong Answer

Solution code:
import numpy as np

K,N=map(int,input().split())
A=list(map(int, input().split()))
dif=[0]*N

for i in range(N-1):
    dif[i]=A[i+1]-A[i]

dif[N-1]=20-A[N-1]+A[0]

print(int(K-max(dif)))

Submission status: Wrong Answer

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

a_list_ = input().split()  # sはlist
a_list = [int(x) for x in a_list_]

if int(k/2)>=(max(a_list)-min(a_list)):
    print(max(a_list)-min(a_list))
else:
    right = [x for x in a_list if x<int(k/2)]
    left = [x for x in a_list if x>=int(k/2)]

    one = max(right)+k-min(left)
    two = k-one

    print(min([one, two]))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
A_diff = np.diff(A)
asc = np.sum(A_diff)
desc = asc - abs(A[-1] - A[-2]) + (N - A[-1] + A[0])  
print(min([asc, desc]))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff_list = []
sum_val = 0
for i in range(N):
    diff_list.append(A[i]-sum_val)
    sum_val += A[i]
diff_list.append(K-A[-1])
print(sum(diff_list)-max(diff_list))

Submission status: Wrong Answer

Solution code:
# 1行目の入力を空白で分割された数値として扱う
K, N = map(int, input().split())
# 2行目の入力を数値のリストとして扱う
A = list(map(int, input().split()))

# 答えの準備
ans = 0
max = 0

# 距離の取得
for i in range(0, N -1):
    d = A[i+1] - A[i]
    ans += d
    if d > max:
        max = d

# 一周した最後
dl = K - A[N - 1]
if dl < max:
    ans = ans - max + dl

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [abs(x - y) for x, y in zip(A[1:] + [K], A)]

print(sum(B) - max(B))

Submission status: Wrong Answer

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

hutuu = max(a) - min(a)

a_inv = []
for i in a:
    if i > (k//2):
        a_inv.append(i - k)
    else:
        a_inv.append(i)
inv = max(a_inv) - min(a_inv)

if hutuu >= inv:
    print(inv)
else:
    print(hutuu)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split(" "))
A = [int(i) for i in input().split(" ")]
a1 = [i for i in A if i<=K/2]
a2 = [j for j in A if j>K/2]

Max = 0

for i in range(len(a1)-1):
    if a1[i+1] - a1[i] > Max:
        Max = a1[i+1] - a1[i]
        
for j in range(len(a2)-1):
    if a2[j+1] - a2[j]>Max:
        Max = a2[j+1] - a2[j]
        
if a1[0]+K-a2[-1]>Max:
    Max = a1[0]+K-a2[-1]
    
if a2[0]+a1[-1]>Max:
    Max = a2[0]-a1[-1]
    
print(K-Max)

Submission status: Wrong Answer

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

ans = A[N - 1] - A[0]
for i in range(1, N):
  prior = A[i] - A[i - 1]
  if i == 1:
    next_ = A[0] + K - A[N - 1]
  else:
    next_ = A[i-1 ] - A[i -2] 
  #print(prior, next_)
  tmp = ans - prior + next_
  if ans > tmp:
    ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
inputNumber = input()
K = int(inputNumber.split(" ")[0])
N = int(inputNumber.split(" ")[1])

inputNumber = input()
A=inputNumber.split(" ")

A_Length=len(A)

for i in range(A_Length):
    A[i] = int(A[i])

result=0

for i in range(A_Length):
    if i == A_Length - 1:
        if result < (K - A[i]) + A[0]:
            result = (K - A[i]) + A[0]
    else:
        if result < A[i] - A[i-1]:
            result = A[i+1] - A[i]

print(result)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
diff = []
for a, b in zip(A, A[1:]):
    diff.append(b - a)
diff.append(A[0] - 0 + K - A[-1])
a = max(diff)
print(min(a, K - a))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split(" "))
A = list(map(int,input().split(" ")))
A.sort()
distance_list = []
for i,a in enumerate(A):
    try:
        distance = abs(a - A[i+1])
        distance_list.append(distance)
    except IndexError:
        break
distance_list.append(K-max(A))
distance_list.remove(max(distance_list))
ans =sum(distance_list)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):
    if k - a[i] <= a[i]:
        a[i] -= k

a.sort()

ans = 0

for i in range(1, n):
    ans += a[i] - a[i-1]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = list(map(int, input().split()))
an = list(map(int, input().split()))
bn = []
for i in range(n-1):
  bn.append(an[i+1]-an[i])
bn.append(k-an[n-1])
print(sum(bn)-max(bn))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = []
last = 0
saisho = 0
for i in map(int, input().split()):
  if saisho == 0:
    saisho = i
    last = i
    continue
  A.append(i - last)
  llast = last
  last = i
A.append(last - llast  + saisho)
print(sum(A) - max(A))

Submission status: Wrong Answer

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

a_list_sorted = sorted(a_list)

distance_1 = a_list_sorted[-1] - a_list_sorted[0]
distance_2 = (k -a_list_sorted[-1]) + a_list_sorted[-2]

print(min(distance_1, distance_2))

Submission status: Wrong Answer

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

hanbun = k /2
a.sort
i = 0
while True:
    if a[i] != 0:
        end = a[i]
        break
    else:
        i += 1
start = a[n-1]


result = start - end
print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np
 
tmp =list(map(int, input().split()))
a=np.array(list(map(int, input().split())))
 
a[a>int(tmp[0]/2)]=tmp[0]-a[a>int(tmp[0]/2)]
print(sum(a))

Submission status: Wrong Answer

Solution code:
def rMap():
	return map(int,input().split())
def rList():
	return list(map(int,input().split()))
k,n = rMap()
a = rList()
x = min(a)
y = max(a)
first = y-x
for i in range(n):
	if a[i]>k//2:
		a[i] -=k
x = min(a)
y = max(a)
second = y-x
print(min(first,second))

Submission status: Wrong Answer

Solution code:
info = list(map(int,input().split()))
k=info[0]
n=info[1]
a = list(map(int,input().split()))
MAX=0

for i in range(n-1):
    if a[i+1]-a[i] > MAX:
        MAX=a[i+1]-a[i]
if abs(a[0]-a[n-1]) > MAX:
        MAX=abs(a[0]-a[n-1])
print(str(k-MAX))

Submission status: Wrong Answer

Solution code:
import math
a , b = map(int,input().split())
res = list(map(int,input().split()))
if len(res) == 1:
    print(0)
else:
    if res[0] == 0:
        aa = a - res[1]
        bb = res[-1] - 0
    else:
        aa = res[1] + (a - res[-1])
        bb = res[-1] - res[1]
    minn = min(aa,bb)
    print(minn)

Submission status: Wrong Answer

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

k2 = k // 2

for i in range(n):
    if a[i] > k2:
        a[i] = a[i] - k
    if n < 4 and a[i] == k2:
        a[i] -=k2
#print("a", a)
re_a = sorted(a)
#print(re_a)
print(re_a[n-1] - re_a[0])

Submission status: Wrong Answer

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

a.append(a[0] + k)
a.insert(0, a[1] - a[-2])

li = []
for i in range(1, n+1):
    d = min(k - abs(a[i]-a[i-1]), k - abs(a[i]-a[i+1]))
    li.append(d)

ans = min(li)
print(ans)

Submission status: Wrong Answer

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

#print(a)
sa =  []
for i in range(1,n):
    sa_ = a[i]-a[i-1]
    sa.append(sa_)
#print(sa)
k_last = k
for j in range(len(sa)):
    k_last -= sa[j]
#print('k_last=',k)

ans = k_last -  max(sa)
if ans < 0:
    print(max(sa))
elif ans == 0:
    print(k)
else:
    print(ans)

Submission status: Wrong Answer

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

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

'''
l = 0

for i in range(n):
    if i == 0:
        l += a[0]
    else:
        l += a[i+1] - a[i]

print(l)
'''

dis = []

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

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

print(sum(dis))

Submission status: Wrong Answer

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

x = 0
index = 0

for i in range(n-1):
    if x < a[i+1] - a[i]:
        x = a[i+1] - a[i]
        index = i

if x < k-a[-1]+a[0]:
    x = k-a[-1]+a[0]

print(k - x,k,  x,k-a[-1]+a[0])

Submission status: Wrong Answer

Solution code:
k, n = map(int,input().split())
a = list(map(int, input().split()))
if a[0] == 0:
    print(a[-1] - a[-2])
else:
    print(a[-1] - a[0])

Submission status: Wrong Answer

Solution code:
import numpy as np
a,b=map(int,input().split())

n_l=list(map(int,input().split()))
n_l=list(sorted(n_l))

arr = np.array(n_l)

diff = np.diff(arr)

print(a-max(diff))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
fst = None
lst = None
pre = None
mx = 0
for a in sorted(map(int, input().split())):
    lst = a
    if fst is None:
        fst = pre = a
        continue
    d = a - pre
    if d > mx:
        mx = d
    pre = a
d = (K - lst) + fst
if d > mx:
    mx = d
print(mx)

Submission status: Wrong Answer

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

if A[0] == 0:
    A.append(k)

diff = []
answer = 0
start = 0

for i in range(len(A)):
    diff.append(A[i] - start)
    start = A[i]

answer = sum(diff) - max(diff)

print(answer)

Submission status: Wrong Answer

Solution code:

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

res= a[n-1]

max=0
n_prev=0

for nn in a:
    if (nn-n_prev) < (k-a[n-1]):
        res=(nn-n_prev)
    n_prev=nn

print(res)

Submission status: Wrong Answer

Solution code:
K = 20
A = [5,10,15]
A.sort()
distance_list = []
for i,a in enumerate(A):
    try:
        distance = abs(a - A[i+1])
        distance_list.append(distance)
    except IndexError:
        break
distance_list.append(K-max(A))
distance_list.remove(max(distance_list))
ans =sum(distance_list)
print(ans)

Submission status: Wrong Answer

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

b = []
for i in range(n-1):
    b.append(a[i+1]-a[i])
b.append(k-a[n-1])

print(k-max(b))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = []
last = 0
saisho = 0
for i in map(int, input().split()):
  if saisho == 0:
    saisho = i
    last = i
    continue
  A.append(i - last)
  llast = last
  last = i
A.append(last - llast  + saisho)
print(min(A))

Submission status: Wrong Answer

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

def main(K, N, lst_A):

    lst_A = [0] + lst_A
    lst_A.append(K)
    diff_dis = [lst_A[i+1] - lst_A[i] for i in range(N+1)]

    sum_dis = sum(diff_dis)
    return sum_dis - max(diff_dis)

if __name__ == "__main__":

    answer = main(K, N, lst_A)
    print(answer)

Submission status: Wrong Answer

Solution code:
import math

KN = list(map(int, input().split()))
A = list(map(int, input().split()))

path = 0
lastPosition = 0

for i in range(KN[1]):
    if (i == 0):
        path = A[0]
        lastPosition = 0
    else:
        currentPosition = A[i]
        if (path < currentPosition - lastPosition):
            path = currentPosition - lastPosition
        lastPosition = currentPosition

if (path < (KN[0] + A[0] - A[KN[1] - 1])):
    path = KN[0] + A[0] - A[KN[1] - 1]

print(KN[0] - path)

Submission status: Wrong Answer

Solution code:
inputNumber = input()
K = int(inputNumber.split(" ")[0])
N = int(inputNumber.split(" ")[1])

inputNumber = input()
A=inputNumber.split(" ")
for i in range(len(A)):
    A[i] = int(A[i])

result=0

for i in range(len(A)):
    if i == len(A) - 1:
        if result < (K - A[i]) + A[0]:
            result = (K - A[i]) + A[0]
    else:
        if result < A[i] - A[i-1]:
            result = A[i+1] - A[i]

print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = map(int, input().split())
a = list(map(int, input().split()))
b = np.zeros(n)
for i in range(0,n-1):
  b[i] = a[i+1] -a[i]
  
b[n-1] = k - a[n-1] + a[0]
print(a,b)
ans = k - max(b)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B=[]
for i in range(N-1):
    B.append(A[i+1]-A[i])
B.append(A[N-1]-A[0])
print(K-max(B))

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
sum = 0
for i in range(1,n):
    sum += a[i] - a[i-1]
print(sum)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
n_inf = list(map(int,input().split()))
n_inf.append(k)
n_inf.insert(0,0)
#print(n_inf)
kyori_list = [j for j in range(n+1)]
for i in range(n+1):
  kyori_list[i] = n_inf[i+1]-n_inf[i]
#print(kyori_list)
kyori_list.sort(reverse=1)
#print(kyori_list)
print(sum(kyori_list[1:n+1]))

Submission status: Wrong Answer

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

    D = []
    for i in range(N - 1):
        D.append(A[i + 1] - A[i])

    D.append((K - A[-1]) + A[0])

    ans = 10 ** 6
    for i in range(N - 1):
        ans = min(ans, sum(D[i : i + N - 1]))

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
k, n = [int(elem) for elem in input().split()]
a_lst = [int(elem) for elem in input().split()]

diff = []
for i, a in enumerate(a_lst[:-1]):
    diff.append(a_lst[i+1] - a)
diff.append((a_lst[0] + k) - a_lst[-1])
print(diff)

res = sum(diff) - max(diff)
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
input()
a = list(map(lambda x: int(x), input().split("\n")[0].split(" ")))
print(max(a) - min(a))

Submission status: Wrong Answer

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

for i in range(n):
  if i != n - 1:
    d.append(p[i+1] - p[i])
  else:
    d.append(k - p[i])

a = sum(d) - max(d)
print(a)

Submission status: Wrong Answer

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

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

distances = []
for i in range(N-1):
    distances.append(houses[i+1] - houses[i])

distances = [K-houses[N-1]] + distances


print(min(sum(distances[:-1]), sum(distances[1:])))

Submission status: Wrong Answer

Solution code:
a,b = [int(i) for i in input().split()]
l  = [int(i) for i in input().split()]

if l[0] != 0:
    print(l[-1] - l[0])
else:
    l2 = [i for i in l if i <= (a//2)]
    l3 = [a - i for i in l if i > (a//2)]
    print(l2[-1] + l3[0])

Submission status: Wrong Answer

Solution code:
k, n = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
kyoris = []
for i in range(len(a)-1):
    kyoris.append(abs(a[i]-a[i+1]))
lastkyori = (k - a[-1]) + a[0]
kyoris.append(lastkyori)
print(max(kyoris))

Submission status: Wrong Answer

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

if 0 in A:
    A.pop(0)

mx = max(A)
mn = min(A)

diff = mx - mn
ndiff = K - diff

print(min(diff, ndiff))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
A_list = list(map(int,input().split()))
sa = []
for i in range(1,N):
    sa.append(abs(A_list[i]-A_list[i-1]))
if A_list[0] == 0:
    sa.append(K-A_list[-1])
else:
    sa.append(A_list[-1]-A_list[0])
print(sum(sa)-max(sa))

Submission status: Wrong Answer

Solution code:
k,_,*a=map(int,open(0).read().split())
c=m=0
for x,y in zip(a,a[1:]):
  m=max(m,y-x)
  c+=y-x
print(c+k-y-max(m,k-y))

Submission status: Wrong Answer

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

distance = [0]*n

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

distance.sort(reverse=True)
print(distance)
print(sum(distance[1:]))

Submission status: Wrong Answer

Solution code:
import numpy as np
 
tmp =list(map(int, input().split()))
a=np.array(list(map(int, input().split())))

a[a>int(tmp[0]//2)]=tmp[0]-a[a>int(tmp[0]//2)]
print(sum(a))

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
a = list(map(int, input().split()))
b = [k-a[-1]+a[0]]
for i in range(len(a)-1):
  b.append(a[i+1]-a[i])
del b[b.index(min(b))-1]
print(sum(b))

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
if K % 2 == 0:
    half = K // 2
else:
    half = K // 2 + 1

for i in A:
    if i >= half:
        ans += K - i
    else:
        ans += i

print(ans)

Submission status: Wrong Answer

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

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

A = sorted(a)

minl = 0

for i in range(N-1):
    l = A[i] - A[i+1]

    if l <= 0:
        l = -1 * l
    
    if minl < l:
        minl = l

ans = K - minl

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
a_list=list(map(int,input().split()))
b_list=[]
for i in range(N-1):
    X=a_list[i+1]-a_list[i]
    b_list.append(abs(X))
b_list.append(K-a_list[-1]+a_list[0])
print(b_list)
print(K-max(b_list))

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().strip().split())
A = list(map(int, input().strip().split()))

min_=10**6
dA=np.diff(A)
np.append(dA,abs(A[0]-A[-1]))
skip_dist=max(dA)
ans=K-skip_dist

print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B=[]
for i in range(N-1):
    B.append(A[i+1]-A[i])
B.append(A[N-1]-A[0])
print(N-max(B))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A_list = list(map(int, input().split()))
#DP = [10**6 for i in range(K)]
max1 = -1
ans = 0
for i in range(N-1):
    dis = abs(A_list[i]-A_list[i+1])
    if dis > max1:
        max1 = dis
        max_i = i
for i in range(N-1):
    if max_i!=0:
        ans += abs(A_list[max_i]-A_list[max_i-1])
    else:
        ans += abs(K-A_list[max_i-1])
    max_i -= 1
print(ans)

Submission status: Wrong Answer

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

for i in A:
  if i>10:
    a=i-20
    x.append(a)
  else:
    x.append(i)
if len(x)==1:
  print(abs(x[0]))
else:
  xmax=max(x)
  xmin=min(x)
  ans=xmax-xmin
  print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
As = [int(i) for i in input().split()]

nums = [K - As[-1]]
for idx in range(len(As)-1):
    nums.append(As[idx+1] - As[idx])
print(sum(nums) - max(nums))

Submission status: Wrong Answer

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

  Max = A_list[N-1]
  Min = A_list[0]

  if Max -  Min <= K/2:
    print(Max - Min)
  else:
    ans = K - (Max - A_list[N-2])
    print(ans)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
import sys
import math
def i():return int(sys.stdin.readline().replace("\n",""))
def i2():return map(int,sys.stdin.readline().replace("\n","").split())
def s():return str(sys.stdin.readline().replace("\n",""))
def l():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))
def t():return tuple(map(int,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":pass
k,n = i2()
a = intl()
b = list(map(lambda x:int(x)-k,a))
print(min(a[n-1]-a[0],b[n-1]-b[0]))

Submission status: Wrong Answer

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

A = list(map(int, input().split()))
a = A[-1] - A[0]
b = K-A[-1] + A[-2]-A[0]
if a <= b:
  print(a)
else:
  print(b)

Submission status: Wrong Answer

Solution code:
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = sorted([n-k if n > k//2 else n for n in a])
print(min(max(a) - min(a), abs(max(b) - min(b))))

Submission status: Wrong Answer

Solution code:
K,N=list(map(int,input().split(' ')))
A=list(map(int,input().split(' ')))
k=0
Anew=[]
for i in range(1,N):
    Anew.append(abs(A[i]-A[i-1]))
print(K-max(Anew))

Submission status: Wrong Answer

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

if a[0] == 0:
	ma = k - a[-1]
else:
	ma = a[-1] - a[0]

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

print(k-ma)

Submission status: Wrong Answer

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

dist = [0] * (N-1)

for i in range(N-1):
  dist[i] = A[i+1] - A[i]
    
print(K - max(dist))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
a=input().split()
A=[int(b) for b in a]
answers=[]
for z in range(N-1):
    sa=A[z+1]-A[z]
    answers.append(sa)
if A[0]==0:
    answers.append(K-A[-1])
else:
    answers.append(A[-1]-A[0])
kotae=max(answers)
print(K-kotae)