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

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

print(min(k-max(b), sum(b)))

Submission status: Wrong Answer

Solution code:
a=input()
a,b=a.split(" ")
a=int(a)
b=int(b)
c=input().split(" ")
d=0
for i in range(1,b):
  d=max(int(c[i])-int(c[i-1]),d)
d=max(a-int(c[-1])+int(c[0]),d)
print(d)

Submission status: Wrong Answer

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

result = sum(c) - c[0]



print(result)

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
dis = abs(A_list[0]-A_list[-1])
if dis > max1:
      max_i = -1
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()))
mi = float("inf")
ma = 0

for index, tmp in enumerate(A):
    if index==0:
        left = tmp
    elif index == 1:
        right = tmp
        ans = right-left
    else:
        b1 = tmp-right
        b2 = left+K-tmp
        if b1<b2:
            ans += b1
        else:
            ans += b2
print(ans)

Submission status: Wrong Answer

Solution code:
k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0] + a + [k[0]]
sa = [b[i + 1] - b[i] for i in range(len(b) - 1)]
c = sa[1:-1]
if k[1] % 2 == 1:
  temae = (len(sa) // 2) -1
  oku = temae + 1
  d = sum(sa) - sa[temae]
  e = sum(sa) - sa[oku]
  print(min([sum(c), d, e]))
else:
  x = len(sa) // 2
  d = sum(sa) - sa[x]
  print(min([sum(c),d]))

Submission status: Wrong Answer

Solution code:
import copy
k,n = map(int,input().split())
a = [int(i) for i in input().split()]
b=[]
bf=0
c=a[0]
if c!=0:
    aa=copy.deepcopy(a)
    a=[]
    for i in aa:
        a.append(i-c)

for i in a:
    b.append(i-bf)
    bf=i
b.append(k-bf)

e = k-b[-1]
f= k-b[-2]
if e==f:
    print(e)
elif e<f:
    print(e)
elif e>f:
    print(f)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
a = [int(i) for i in input().split()]
dist1 = max(a)-min(a)
a1 = []
for i in a:
    if i <= 10:
        a1.append(-i)
    else:
        a1.append(K-i)
dist2 = max(a1)-min(a1)
a2 = []
for i in a:
    if i < 10:
        a2.append(-i)
    else:
        a2.append(K-i)
dist3 = max(a2)-min(a2)
print(min(dist1,dist2,dist3))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
num_list = list(map(int, input().split()))
min_num = num_list[0]+num_list[-1]

for i in range(0,len(num_list)):
    res = num_list[-1]-k
    #print(num_list)
    num_list.pop(-1)
    #print(num_list)
    num_list.insert(0, res)
    #print(num_list)
    sum_num = abs(num_list[0])+abs(num_list[-1])
    #print("sum_num")
    #print(sum_num)
    
    if sum_num < min_num:
        min_num = sum_num

print(min_num)

Submission status: Wrong Answer

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

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

count_zero = list_house.count(0)

ans = (sum(list_house)) // (len(list_house) - count_zero)

print(ans)

Submission status: Wrong Answer

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

# 最も大きな差が答え
new_list  = [x-K if K//2 < x else x for x in num_list]

ans = K // 2
tmp = max(new_list)-min(new_list)
ans = tmp if tmp < ans else ans

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0])

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

print(K-distance_max)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans1 = 0
for i in range(n - 1):
    ans1 += li[(i + 1)] - li[i]
#print(ans1)

chuten = int(k / 2)
for i,j in enumerate(li):
  if j > chuten:
    li[i] = j - k
li.sort()
#print(li)
ans2 = 0
for i in range(n - 1):
    ans2 += li[(i + 1)] - li[i]
#print(ans2)

print(min(ans1, ans2))

Submission status: Wrong Answer

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

A = list(map(int, input().split()))
D = []

for i in range(0,N-1):
  d = A[i + 1] - A[i]
  D.append(abs(d))
  
d2 = A[N-1] - K
D.append(abs(d2))

kankaku = max(D)
saitan = K - kankaku

print(saitan)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if 0 in a :
  a.sort()
  print((k - max(a)) + a[1])
else :
  print(max(a) - min(a))

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(k-max(c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

cA = A.copy()
for i, j in enumerate(cA):
    if j >= K / 2:
        cA[i] = j-K
cA = sorted(cA)

dcA = []
for k in range(1, N):
    dcA.append(cA[i] - cA[i-1])

print(min(sum(dA), sum(dcA)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import ceil,sqrt
def main():
    
    # for _ in range(ii()):
    
    # t=int(input())
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    b=[]
    c=[]
    x=k//2
    for i in range(n):
        if(a[i]>x):
            b.append(k-a[i])
        else:
            c.append(a[i])
    b.sort()
    c.sort()
    ans=0
    ans1=0
    ans2=0
    ans3=0
    if(len(c)>0):
        ans=c[len(c)-1]-c[0]
        ans1=c[len(c)-1]
    if(len(b)>0):
        ans3=b[len(b)-1]
        if(len(c)>0):
            ans2=(k-b[0])-c[len(c)-1]
    print(min(ans+ans2,ans1+ans3))
    
    
    
    
    
    
    
    
    
    
    
    




    
if __name__ =="__main__":
    main()

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
def main():
    kukan = []
    max_index = 0
    max_v = 0
    A.append(K- A[0])
    for i in range(N):
        if max_v < A[i+1] -A[i]:
            max_index = i
        max_v = max(A[i+1] -A[i], max_v)
    # print(max_index)
    if max_index == N-1:
        diff = A[max_index+1] - A[max_index]
    elif max_index == 0:
        diff = K -A[-1] +A[max_index]
    else:
        print(K -A[max_index+1] +A[max_index])
        return
    print(K-diff)
main()

Submission status: Wrong Answer

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

if max(mylist)-min(mylist)>K/2:
    #0より大きいminを取得
    #それにK-maxを足す
    if mylist[0]==0:
        print(mylist[1]+K-max(mylist))
    else:
        print(mylist[0]+K-max(mylist))

else:
    print(max(mylist)-min(mylist))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

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

rear = []
for i in range(N):
    if A[i] >= K // 2:
        rear.append(A[i]-K)
    else:
        rear.append(A[i])    

rear.sort()

rear_value = rear[N-1] - rear[0]
if front < rear_value:
    print(front)
else:
    print(rear_value)

Submission status: Wrong Answer

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

low = 0
high = 0

low = min(a)
high = max(a)

print(high - low)

Submission status: Wrong Answer

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

#print(k,n, l)

x = []
for i in range(1, n):
    x .append(l[i]-l[i-1])
x = x + [k - l[-1]]

print(sum(x) - max(x))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	K,N = inputMap()
	A = inputList()

	toto = 0
	tmp = 0
	for j,vol in enumerate(A):
		if j == 0:
			tmp = vol
			continue
		toto += vol - tmp
		tmp = vol

	ans = toto
	toto = toto + (K - A[-1])
	
	for j,vol in enumerate(A):
		if j == 0:
			continue

		tmpans = toto - (vol - A[j-1])

		if ans > tmpans:
			ans = tmpans

	print(ans)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
A=list(map(int,input().split()))
a=sorted(A)
try:
  for i in range(n):
    b=a[i+1]-a[i]
    c=[]
    c.append(b)
except IndexError:
  pass
d=max(c)
if d<=(a[0]+k-a[n-1]):
  d=a[0]+k-a[n-1]
print(k-d)

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(min((a[i] - a[i-1]), (k - 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
Y = 0
Z = 0
B = A.copy()
D = A.copy()
E = A.copy()
E.reverse()
flag = 0
for i in range(N):
    D[i] = -abs(K-D[i])
if B[0] == 0:
    C = [B[1],B[0]]+D[2:N]
    flag += 1
if flag == 1:
    for i in range(N-1):#正規
        X += abs(A[i+1]-A[i])
    for j in range(N-1):#逆順2通り目
        Y += abs(C[j+1]-C[j])
    for i in range(N-1):#逆順(正規)
        Z += abs(E[i+1]-E[i])
    print(min(X,Y,Z))
else:
    for i in range(N-1):#正規
        X += abs(A[i+1]-A[i])
    for i in range(N-1):#逆順(正規)
        Z += abs(E[i+1]-E[i])
    print(min(X,Z))

Submission status: Wrong Answer

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

# 上側通る場合
upper_list = [i if i <= K/2 else i - K for i in l]
# 下側を通る場合
lower_list = [0 if i == K/2 else i - K/2 for i in l]

upper_length = max(upper_list) - min(upper_list)
lower_length = max(lower_list) - min(lower_list)

print(int(min(upper_length,lower_length)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

B=[0]*N
for i in range(N):
    if A[i]<=K//2:
        B[i]=A[i]
    if A[i]>K//2:
        B[i]=-(K-A[i])
        
if A[-1]<K//2:
    print(A[-1]-A[0])
if A[-1]>=K//2:
    print(min(A[-1]-A[0],abs(min(B))+max(B)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
m=100000
for i in range(len(A)):
    x=A[i]
    a=A[:i]+A[i+1:]
    s=0
    for j in range(len(a)):
        s+=abs(a[j]-x)
    if m>s:
        m=s
print(m)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
K,N=map(int, input().split())
A=list(map(int, input().split()))

counta = A[-1]-A[0]
#print(counta)

i=0
while N>i:
    if A[i]<K/2:
        A[i]+=20
    i+=1
A = sorted(A)
#print(A)
countb = A[-1]-A[0]
#print(counta)

if counta<countb:
    print(counta)
else:
    print(countb)

Submission status: Wrong Answer

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

diff = []


for i in range(N-1):
    Diff = A[i+1] -A[i]
    diff.append(Diff)
    
diff.append(K-A[N-1]+A[0])


DIFF = sorted(diff)
print(DIFF)

del DIFF[-1]

print(sum(DIFF))

Submission status: Wrong Answer

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

if a[n-1]-a[0] <= k//2:
    print(a[n-1]-a[0])
else:
    for i in range(1,n):
        ans=max(ans,a[i]-a[i-1])
    print(k-ans)

Submission status: Wrong Answer

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


dis =[]

dis.append(an[0] + K - an[len(an)-1])

for i in range(N-1):
    dis.append(an[i+1]-an[i])

work = sorted(dis)
print(work[0]+ work[1])

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

dlst.append(k - lst[n - 1])
dlst.sort(reverse=True)
dlst.pop(0)

print(sum(dlst))

Submission status: Wrong Answer

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

for i in range(n):
  x = abs(a[i] - a[i-1])
  if x > k/2:
    x = x - int(k/2) 
  d.append(x)

print(sum(d)-max(d))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
li = list(map(int, input().split()))
x = k/2
a = 0
b = 0
for i in li:
    temp_a = min(abs(k-i), abs(i))
    a += temp_a
    temp_b = abs(x-i)
    b += temp_b

ans = min(int(a), int(b))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,M=(int(x) for x in input().split())
A=list(map(int, input().split()))
A=np.array(A)
B=A[1:]-A[:-1]
b=max(B)
c=A[-1]-A[0]
if c > N/2:
    c=int(c-N/2)
else:
    c=c
d=max(b,c)
print(N-d)

Submission status: Wrong Answer

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    K, N = il()
    A = il()
    A.sort()
    hf = K // 2
    ret = max(A) - min(A)

    for n in range(N - 1):
        if hf < A[n + 1] - A[n]:
            ret = min(ret, A[n] + K - A[n + 1])

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
dif = []
for i in range(k):
    if(i==k-1):
        dif.append(abs(n-l[i]))
    else:
        dif.append(abs(l[i]-l[i-1]))
ans = sum(dif)-max(dif)
print(ans)

Submission status: Wrong Answer

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

for i in range(n):
  x = abs(a[i] - a[i-1])
  if x > k/2:
    x = x - int(k/2) 
  d.append(x)

print(d)
print(sum(d)-max(d))

Submission status: Wrong Answer

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

diff_list.append(K-A[-1] + A[0])
for i in range(1, N):
    diff_list.append(A[i]-sum_val)
    sum_val += A[i]

print(sum(diff_list)-max(diff_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

left = 0
right = 0
for i in range(len(a)):
    if k-a[i] < a[i] and k-a[i]>left:
        left = k-a[i]
    elif k-a[i] >= a[i] and a[i]>right:
        right = a[i]
if max(a)-min(a) < left+right:
    print(max(a)-min(a))
else:
    print(left+right)

Submission status: Wrong Answer

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

d = min(k - (a[0] - (a[-1] - 20)), k - (a[1] - a[0]))
for i in range(1, n-1, 1):
  d = min(d, k - (a[i]-a[i-1]), k - (a[i+1]-a[i]) )
d = min(d, k - (a[-1] - a[-2]))

print(d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
x=max(a)-min(a)
y=min(a)+(k-a[1])
if(x<=y):
    print(x)
else:
    print(y)

Submission status: Wrong Answer

Solution code:
k,n=list(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]
dist[n-1]=k-a[n-1]+a[0]

right=k-dist[n-1]
left=k-dist[0]
ret=min(left,right)

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

print(ret)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=len(a)
diff=max(a)-min(a)
temp1=a[0]
for i in range(1,b):
    temp2=a[i]
    a[i]=a[i]-temp1
    temp1=temp2


la=k-diff
a[0]=la

print(k-min(a))

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
l = [int(x) for x in input().split()]
ans_1 = l[n-1]-l[0]
ans_2 = (k-l[n-1])+l[0]
if 0 in l:
    del l[0]
    ans = k-l[n-2]+l[0]
    print(ans)
elif ans_1*2>k:
    print(ans_2)
else:
    print(ans_1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a.sort()
if (max(a)-min(a)) <= (K //2):
    print(max(a)-min(a))
else:
    b = list()
    for i in range(N-1):
        x = a[i+1] - a[i]
        b.append(x)
    print(K-max(b))

Submission status: Wrong Answer

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

if A[N-1] - A[0] <= K//2:
    print(A[N-1] - A[0])

else:
    for i in range(N):
        if A[i] > K//2:
            A[i] -= K
    print(max(A) - min(A))

Submission status: Wrong Answer

Solution code:

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

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

n_prev=0

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

print(res)

Submission status: Wrong Answer

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

ans=[]
ans.append(A[-1]-A[0])
for i in range(n):
  A[i]=(A[i]+k//2)%k
A.sort()
ans.append(A[-1]-A[0])
print(min(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(min(amin,bmin))

Submission status: Wrong Answer

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


d = 0
for i in range(n):
    if abs(a[i]-a[i-1]) >= d:
        d = abs(a[i]-a[i-1])
    else:
        d = d


print(k-d)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
house = list(map(int,input().split()))
ans = [house[2]-house[0],(20-house[1])+house[0],(20-house[2])+house[1]]
print(min(ans))

Submission status: Wrong Answer

Solution code:
# C
K, N = map(int,input().split())
A =list(map(int,input().split()))
l = len(A)
ans = 10**7
for i in range(l):
    if (i == 0):
        num = min(A[-1] - A[0], A[0] + K - A[1])
    elif (i == l-1):
        num = min(A[-1] - A[0], A[-2] + K - A[-1])
    else:
        num = min(K - A[i] + A[i-1], A[i] + K - A[i+1])
print(num)

Submission status: Wrong Answer

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

diff = []

for i in range(1, len(A)):
  diff.append(A[i] - A[i-1])
  
diff.append(K - A[-1])

diff.remove(max(diff))

print(sum(diff))

Submission status: Wrong Answer

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

dmax = 0
for i in range(N-1):
    d = A[i+1] - A[i]
    if d > dmax:
        dmax = d
d = A[0] + (K - A[N-1])
if d > dmax:
    dmax = d

ans = K - dmax
print(dmax)

Submission status: Wrong Answer

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

A = list(map(int, input().split()))
D = []

for i in range(0,N-1):
  d = A[i + 1] - A[i]
  D.append(abs(d))
  
d2 = A[N-1] - A[0]
D.append(abs(d2))

kankaku = max(D)
saitan = K - kankaku

print(saitan)

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(int(ans))

Submission status: Wrong Answer

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

mini = 10**6
for i in range(a[1]):
  if b[i] > 0 and mini > b[i]:
    mini = b[i]

print(max(b) - mini)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    K, N = il()
    A = il()
    A.sort()
    hf = K // 2
    ret = max(A) - min(A)
    if ret > hf:
        for n in range(N - 1):
            if hf < A[n + 1] - A[n]:
                ret = min(ret, A[n] + K - A[n + 1])

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort()
a_list = np.array(a_list)

if k - a_list[-1] > a_list[0]:
    a_list = np.delete(a_list, 0)
    print(sum(np.diff(a_list)))
else:
    print(sum(np.diff(a_list)))

Submission status: Wrong Answer

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

ans = k - max_diff
print(ans)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=len(a)
diff=max(a)-min(a)
temp1=a[0]
for i in range(1,b):
    temp2=a[i]
    a[i]=a[i]-temp1
    temp1=temp2


la=k-diff
a[0]=la

print(min(a))

Submission status: Wrong Answer

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

A = list(map(int, input().split()))
D = []

for i in range(0,N-1):
  d = A[i + 1] - A[i]
  D.append(abs(d))
  
d2 = A[N-1] - A[0]
D.append(abs(d2))

kankaku = max(D)
saitan = K - kankaku

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
m1 = A[-1] - A[0]

for i in range(N-1):
    if A[i+1] - A[i] >= (K//2):
        m1 = A[i] + K - A[i+1]

print(m1)

Submission status: Wrong Answer

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

diff = []


for i in range(N-1):
    Diff = A[i+1] -A[i]
    diff.append(Diff)
    
diff.append(K-A[N-1]-A[0])


DIFF = sorted(diff)
print(DIFF)
if A[0] == 0:
    del DIFF[-1]

print(sum(DIFF))

Submission status: Wrong Answer

Solution code:
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    minn = int(10e10)
    maxx = 0
    for i in a:
        if i != 0:
            minn = min(i, minn)
            maxx = max(i, maxx)
    print(maxx-minn)


if __name__ == "__main__":
    #IOE()
    main()

Submission status: Wrong Answer

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

sub = A[1] - A[0]
max = N * sub
sub_2 = K - max

all = sub * (N - 1)

if K < all:
    print(all % K)
elif sub_2 >= sub:
    print(sub * (N - 1))
else:
    print(sub * (N - 2) + sub_2)

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(min(l2[-1] + l3[0],l2[-1] + (a//2 - l3[0])))

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
        minn = min(aa,bb)
        print(minn)
    else:
        cc = res[-1] - res[1]
        print(cc)

Submission status: Wrong Answer

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

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:
        a[0] = 1
        continue
    sum += a[i] - a[i-1]
print(sum)