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:
#!/usr/bin/env python3

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
a,b=map(int,input().split())
#print(a)
n_l=list(map(int,input().split()))
n_l=list(sorted(n_l))
# 一次元配列を作成
arr = np.array(n_l)

# 隣り合う要素同士の差分を計算
diff = np.diff(arr)
#print(diff)
#print(max(diff))
print(a-max(diff))

Submission status: Wrong Answer

Solution code:
k,n=map(int, input().split())
a=list(map(int,input().split()))
t=k/2
min1=t
max1=0
min2=k
max2=t
for i in range(n):
    if a[i]<=t:
        if a[i]<min1:
            min1=a[i]
        if a[i]>max1:
            max1=a[i]
    else:
        if a[i]<min2:
            min2=a[i]
        if a[i]>max2:
            max2=a[i]
print(int(min(k-min2+max1,max2-min1)))

Submission status: Wrong Answer

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

max_a = max(A)
min_a = min(A)
diff_min = K

if max_a - min_a <= K/2:
    print(max_a-min_a)
else:
    if K - A[1] - min_a < max_a - min_a:
        print(K - A[1] - min_a)
    else:
        print(max_a - min_a)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
points = list(map(int, input().split()))
max_index = -1
max_value = -1

for index in range(1, N):
  d = points[index] - points[index-1] 
  if d > max_value:
    max_index = index
    max_value = d

start = K - points[max_index]
end   = points[max_index-1]

print(start + end)

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

if sub_2 >= sub:
    print(sub * (N - 1))
else:
    print(sub * (N - 2) + sub_2)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
list = list(map(int, input().split()))
ans = 10000
for i in range(n):
    list.append(list[i] + k)
for i in range(n):
    m = list[i + n - 1] - list[i]
    ans = min(ans, m)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k =map(int,input().split())
l=list(map(int,input().split()))
l.append(n)
d=[0 for i in range(k+1)]

for i in range(1,k+1):
  d[i]=l[i]-l[i-1]

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

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 arr[i] - arr[0] > ans:
            ans = arr[i] - arr[0]
print(ans)

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(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()))
ans = A[n-1] - A[0]
if ans > k // 2:
  ans = k - A[n-1] + A[n-2] - A[0]
print(ans)

Submission status: Wrong Answer

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

An = [0] + An + [K]

Bn = [0] * (N + 2)
for i in range(N+1):
    Bn[i] = abs(An[i+1] - An[i])


Bn = Bn[1:-2] + [Bn[0] + Bn[-1]]

print(sum(Bn) - max(Bn))

Submission status: Wrong Answer

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

ans = min(a[-1] - a[0], K - a[-1] + a[-2])
for i in range(1, N - 1):
    ans = min(ans, K + a[i - 1] + (a[-2] - a[i]))

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(N-1):
    if(A[i+1]-A[i] < K-A[i+1]):
        ans += A[i+1]-A[i]    
    else:
        ans += K-A[i+1]
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    K, N = map(int,input().split())
    A = list(map(int,input().split()))
    A.append(K)
    B = []
    for i in range(N):
        B.append(A[i+1] - A[i]) # 内包表記
    # Maxを抜く
    tmp = max(B)
    print(sum(B) - tmp)

main()

Submission status: Wrong Answer

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

distance = [0] * N

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

distance[-1] = K - A[-1]

print(K - max(distance))

Submission status: Wrong Answer

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

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] + 20 - a[n-1])
max_length = max(length)
ans = 20- max_length

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

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

Submission status: Wrong Answer

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

a = input().split()
if int(a[0]) == 0:
    point = k - int(a[-1])
else:
    point = int(a[-1]) - int(a[0])
for i in range(n-1):
    b = int(a[i+1]) - int(a[i])
    if point < b:
        point = b
sum_a = k - point
print(sum_a)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
li=[0]
for i in range(1,len(a)):
    li.append(a[i]-a[0])
#print(li)
#print(k,n)
#print(a)
for i  in range(len(li)):
    if li[i]>k/2:
        li[i]-=k
#print(li)
print(max(li)-min(li))

Submission status: Wrong Answer

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

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

print(sum(lst) - max(lst))

Submission status: Wrong Answer

Solution code:
from collections import deque
K, N = map(int, input().split())
S=deque(list(map(int, input().split())))

Ssum=0
for i in range(N-1):
    Ssum += S[i+1] - S[i]
s = max(S) - K
del S[-1]
S.appendleft(s)
S_sum = 0
for i in range(N-1):
    S_sum += S[i+1] - S[i]


print(min(Ssum, S_sum))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
li = list(map(int, input().split()))
li2 = []
for i in li:
    if i < k//2:
        i += k
    li2.append(i)

li.sort()
li2.sort()
ans = min(li[-1] - li[0], li2[-1] - li2[0])
print(ans)

Submission status: Wrong Answer

Solution code:
def calc(K, N, A):
    B = [A[i + 1] - A[i] for i in range(N - 1)] + [A[0] + K - A[N - 1]]
    print(B)
    return K - max(B)


(K, N) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
print(calc(K, N, A))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sa = abs(A[0] - A[-1])
if sa > K/2:
    sa = K - sa

for i in range(0,N-1):
    tmp = abs(A[i] - A[i+1])
    if tmp <= K/2:
        sa = max(sa,tmp)
    else:
        tmp = K - tmp
        sa = max(sa,tmp)

if sa <= K/2:
    print(sa)
else:
    print(K-sa)

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

print('sub  : ' + str(sub))
print('sub2 : ' + str(sub_2))

if sub_2 >= sub:
    print(sub * (N - 1))
else:
    print(sub * (N - 2) + sub_2)

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

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)



def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

K,N=acinput()

A=acinput()
A.append(A[0]+K)
B=np.array(A)+K
#A.extend(B)
a=np.array(A)

print(K-np.max(a[1:]-a[:-1]))



print(2*np.pi*K-np.max(a[1:]-a[:-1]))

print(a)
print(a[1:]-a[:-1])

Submission status: Wrong Answer

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

print(max(b) - min(b))

Submission status: Wrong Answer

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

if A[0] == 0:
  for i in range(2, N):
    sum += abs(A[i-1] - A[i])
  print(sum)
else:
  for i in range(1, N):
    sum += abs(A[i-1] - A[i])
  print(sum)

Submission status: Wrong Answer

Solution code:
K,N = list(map(int , input().split()))
A = list(map(int , input().split()))
A.sort()
Q = []
for i in range(N-2):
    q = abs(A[i+1]-A[i])
    Q.append(q)
p = K - A[N-1]+ A[i]
Q.append(p)
Ans = K - max(Q)
print(Ans)

Submission status: Wrong Answer

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

def main():
    n, m = map(int, input().split())
    lis = list(map(int, input().split()))
    su = 0
    for i in range(m - 1):
        su += lis[i + 1] - lis[i]
    ou = lis[0] - lis[-1] + n
    for i in range(m - 2):
        ou += lis[i + 1] - lis[i]
    print(min(ou,su))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = [int(x) for x in input().split(" ")]
a = [int(x) for x in input().split(" ")]
a.sort()
a.insert(0,a[-1])
max_d = 0
for i in range(0,N-1):
  if abs(a[i+1] - a[i]) >= max_d:
    max_d = abs(a[i+1] - a[i])

print(K-max_d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

M = 0
for i in range(N):
    if M <= A[i] - A[i-1] :
        M = A[i] - A[i-1]
ans = K - M
print(ans)

Submission status: Wrong Answer

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

for i in range(1, 2*n):
    right.append(right[-1] + a[i] - a[0])

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

Submission status: Wrong Answer

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

Rj = []
for i in range(len(Aj)-1):
	Rj.append(Aj[i+1]-Aj[i])
Rj.sort()

ans = 0
for i in range(len(Rj)-1):
	ans += Rj[i]
print(ans)

Submission status: Wrong Answer

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

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

Adist.pop(Adist.index(max(Adist)))

print(sum(Adist))

Submission status: Wrong Answer

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


A = sorted(A)
a = A[-1] - A[0]
b = (K - A[-1]) + A[-2]

print(min(a, b))

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)]=a[a>int(tmp[0]//2)]-int(tmp[0]//2)
print(sum(a))

Submission status: Wrong Answer

Solution code:
k,n=map(int, input().split())
al=list(map(int,input().split()))
last=al[-1]
al.append(al[0])
delta=[0]*n
if al[0]==0:
    for i in range(n):
        delta[i]=al[i+1]-al[i]
    print(k-max(delta))
else:
    for i in range(n):
        delta[i]=al[i+1]-al[i]
    delta[-1]=al[0]+(k-last)
    print(k-max(delta))

Submission status: Wrong Answer

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

distanceList = []

start = 0
for index, dist in enumerate(As):
  distanceList.append(abs(start - dist))
  start = dist
distanceList.append(abs(As[-1] - K))


total_dist_list = []
for index, dist in enumerate(distanceList):
  total_dist = 0
  for i in range(N):
    total_dist += distanceList[index - i]
  total_dist_list.append(total_dist)

print(min(total_dist_list))

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(sum(l)-l[-1]))
g=sum(ne)
for i  in ne:
    ma=min(ma,g-i)
print(ma)

Submission status: Wrong Answer

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

dA = [A[i+1] - A[i] for i in range(N-1)]
dA.append(K-A[-1])
sumdA = sum(dA)
maxdA = max(dA)
print(sumdA - maxdA)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
x=list(map(int,input().split()))
xx=list(set(x))
y=[]
for i in x:
    if i*2>=k:
        y.append(i-k)
    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())
house_list = list(map(int, input().split()))

saidai_kyori = 0
sou_kyori = 0
for i, position in enumerate(house_list):
    kyori = 0
    if i == 0:
        kyori = K - house_list[-1]
    else:
        kyori = house_list[i] - house_list[i-1]

    if kyori > saidai_kyori:
        saidai_kyori = kyori
    
    sou_kyori += kyori

print(sou_kyori - saidai_kyori)

Submission status: Wrong Answer

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

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-max(a))
if(x<=y):
  print(x)
else:
  print(y)

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

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:
# coding:utf-8

K, N = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
dist = []

for i in range(N):
    if i != N - 1:
        dist.append(A[i + 1] - A[i])
    else:
        dist.append(K - A[N - 1])

s = sum(dist)
minimum = K + 1
for i in range(N):
    tmp = s - dist[i]
    if tmp < minimum:
        minimum = tmp

print(minimum)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
sum = sum(a)//(len(a)- a.count(0))
print(sum)

Submission status: Wrong Answer

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

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.append(k)

ans_list = np.diff(a_list, n= 1)

print(sum(ans_list) - max(ans_list))

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    if i == 0:
        continue
    d = lst[i] - lst[i - 1]
    r = k - lst[i]
    if r < d:
        ans += r
    else:
        ans += d

print(ans)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input('').split())
a=input('')
a=a.split(' ')
for b in range(n):
    a[b]=int(a[b])

m=0
for b in range(n-1):
    a[b+1]=a[b+1]-a[b]
    if a[b+1]>=m:
        m=a[b+1]

k=k-m
print(k)

Submission status: Wrong Answer

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

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]))
    if base < comp:
        ans.append(base)
    else:
        ans.append(comp)

print(sum(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


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

    min_a = min(a)
    max_a = max(a)

    print(min(max_a - min_a, k - max_a + max(filter(lambda x: x < (k / 2), a))))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

distance.remove(max(distance))
print(sum(distance))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
d = (abs(a[n - 1] - 20) if a[0] == 0 else abs(a[n - 1] - a[0]))
for i in range(1, n):
    d = max(d, abs(a[i - 1] - a[i]))
print(k - d)

Submission status: Wrong Answer

Solution code:
K, N = [int(x) for x in input().split(" ")]
a = [int(x) for x in input().split(" ")]
a.sort()
a.insert(0,a[-1])
max_d = 0
for i in range(0,N-1):
  if abs(a[i+1] - a[i]) >= max_d:
    max_d = abs(a[i+1] - a[i])

print(max_d,K-max_d)

Submission status: Wrong Answer

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

ans1 = 0

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

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

a.sort()

ans2 = 0

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

print(min(ans1, ans2))

Submission status: Wrong Answer

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

KK = [False]*K
for a in AA:
  KK[a] = True

dist = 0
dist_max = 0
for i in range(2*K):
  if KK[int(i%K)] == 1:
    dist = 1
  else:
    dist += 1
  if dist > dist_max:
    dist_max = dist
print(dist_max)

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 = 0
flag = 0
for i in range(n):
    if a[i] <= (k / 2):
        continue
    else:
        if flag == 0:
            ans2 += a[i - 1]
            flag = 1
        ans2 += (k - a[i])

print(min(ans1, ans2))

Submission status: Wrong Answer

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

a = list(map(int,input().split()))
ans = 0
gr = 0
for i in range(1,n):
    if(a[i] - a[i-1] > gr):
        gr = a[i] - a[i-1]

if(k - a[n-1] > gr):
    gr = k - a[n-1] 
# print(gr)

print(k - gr)

Submission status: Wrong Answer

Solution code:
s1 = str(input())
s = s1.split(' ')
k = int(s[0])
n = int(s[1])
l=[]

ls = str(input())
l = ls.split(' ')
l1 = [int(x) for x in l]
print(k-(max(l1)-min(l1)))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a_array = [int(x) for x in input().split()]
diff = []
tmp = 0
for i in range(n - 1):
    diff.append(abs(a_array[i + 1] - a_array[i]))
diff.append(k - a_array[-1])
print(sum(diff) - max(diff))

Submission status: Wrong Answer

Solution code:
import math

(k, n) = [int(s) for s in input().split()]

d = [int(s) for s in input().split()]
d.append(k)
e = [d[i+1] - d[i] for i in range(0, len(d) - 1)]
e = sorted(e)
ans = sum(e[:-1])

print(ans)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=[0]+list(map(int,input().split()))+[k]
b=[]
for i in range(n+1):
      b.append(a[i+1]-a[i])
b.sort(reverse=True)
if min(b)==0:
      print(sum(b)-b[0])
else:
    print(sum(b)-b[0]-b[1])

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-1):
    B[i] = A[i+1] - A[i]
B[-1] = abs(B[0]-B[-1])
B.sort()
print(sum(B[:-1]))

Submission status: Wrong Answer

Solution code:
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])

if max(d) < k/2:
    print(max(d))
else:
    print(k-max(d))

Submission status: Wrong Answer

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

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

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

l_max = max(l)

ans = sum(l)-l_max

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n= [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
lake = n[0]
num_ie = n[1]
kyori = []
for i in range(num_ie-1):
    kyori.append(l[i+1]-l[i])
kyori.append(lake - l[num_ie-1] + l[0])
output = 0
output += sum(kyori) 
output -= max(kyori)

print(kyori)
print(output)

Submission status: Wrong Answer

Solution code:
k,n = map(lambda x: int(x),input().split())
a = list(map(lambda x: int(x),input().split()))
a.append(k)
sabun = [a[i+1] -a[i] for i in range(n)]

print(sum(sorted(sabun)[:n-1]))

Submission status: Wrong Answer

Solution code:

K,N = input().split()
K = int(K)
N= int(N)
A = [int(x) for x in input().split()]

dis=[]
now = 0

for i in range(N):
    dis.append(A[i] - now)
    now = A[i]

dis.append(K - A[N -1])

distance = K - max(dis)

print(distance)

Submission status: Wrong Answer

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

mx = K - A[N-1] +A[0]
for i in range(N-1):
  if A[i+1] - A[i] > mx:
    mx = A[i+1] - A[i]
    
ans = sum(A) - mx
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

K, N = map(int, input().split())
A = list(map(int, input().split()))
# print(K, N)
# print(A)
ans = A[0]
for i in range(N - 1):
    ans = abs(ans - A[i + 1])
print(ans)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
li=[0]
for i in range(1,len(a)):
    li.append(a[i]-a[0])
#print(li)
#print(k,n)
#print(a)
for i  in range(len(li)):
    if li[i]>k//2:
        li[i]-=k
#print(li)
print(max(li)-min(li))

Submission status: Wrong Answer

Solution code:
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]
dist1 = max(li)-min(li)
dist2 = max(re_li)-min(re_li)
print(dist1 if dist1 < dist2 else dist2)

Submission status: Wrong Answer

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

ma = 0

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

if ma < abs(A[0] - A[-1] + K):
    ma = abs(A[0] - A[-1])

print(K - ma)

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.append(K)
    diff_dis = [lst_A[i+1] - lst_A[i] for i in range(N)]

    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 numpy as np

K, N = map(int, input().split(" "))
loc = list(map(int, input().split(" ")))
loc = np.array(loc)

dist = np.diff(loc)
np.append(dist,K - loc[N-1] + loc[0])

most_far = np.max(dist)
print(K-most_far)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
lst_a = list(map(int,input().split()))
dis_max = 0
lo_last = lst_a[0]
for i in range(1,n):
    ilo = lst_a[i]
    dis_max = max(dis_max, ilo-lo_last)
    lo_last = ilo
ilo = lst_a[0]
dis_max = max(dis_max, ilo-lo_last)
print(k-dis_max)

Submission status: Wrong Answer

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

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

soto= kn[0]-a[-1]+a[kn[1]-2]
uti=a[-1]-a[0]

if soto>uti:
    print(uti)
else:
    print(soto)

Submission status: Wrong Answer

Solution code:
import sys
K,N=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

m=float(K)/2

AA=[]
for a in A:
    if a<=m:
        AA.append(a)
    else:
        AA.append(a-K)
AA.sort()
print AA[-1]-AA[0]

Submission status: Wrong Answer

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

dp = [0] * n
dp[0] = 0

l = r = A[0]
for i in range(1, n):
  to = A[i]
  if A[i]<l:
  	dis_l = l - A[i]
  else:
    dis_l = l + k - A[i]
    
  if A[i]>r:
    dis_r = A[i] - r
  else:
    dis_r = A[i] + k - r
  
  if dis_r<dis_l:
    r = A[i]
  else:
    l = A[i]
    
ans = 0
if r>l:
  ans = r-l
else:
  ans = r + k - l
  
print(ans)

Submission status: Wrong Answer

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

res = 0
for i in range(N-1):
    if(A[i] == 0):
        res += 0
    else:
        res += abs(A[i] - A[i+1])
print(res)

Submission status: Wrong Answer

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

if A[0] == 0:
  for i in range(2, N):
    sum += abs(A[i-i] - A[i])
  print(sum)
else:
  for i in range(1, N):
    sum += abs(A[i-1] - A[i])
  print(sum)

Submission status: Wrong Answer

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

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()
    ret = max(A) - min(A)

    for n in range(N-1):
        if A[n] > K // 2:
            break
        else:
            t = A[n]
            tmp = t + (K - A[n+1])
            ret = min(ret, tmp)

    print(ret)


if __name__ == '__main__':
    main()

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(sum(b)-max(b))

Submission status: Wrong Answer

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

    shiftA = A[1:] + [K]

    subA = [shiftA[i] - A[i] for i in range(N)]
    maxA = max(subA)
    subA.remove(maxA)

    print(sum(subA))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
if sum(a) > k:
    print(sum(a) - k)
else:
    for i in range(n):
        a[i] = min(a[i], k - a[i])
    print(k - sum(a))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
m1 = A[-1] - A[0]
m2 = 0
for i in A:
    m2 += min(i, abs(K-i))
print(min(m1, m2))

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 = A[N-2] - (A[N-1]-K)

if front < rear:
    print(front)
else:
    print(rear)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K+A[0] - A[N-1]
	B = A[1:]-A[:N-1]
	np.sort(B)
	print(K-max(B[N-2],A_LAST))

if __name__ == '__main__':
	main()