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: Accepted

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

Submission status: Accepted

Solution code:
K,N=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[N-1]=K-A[N-1]+A[0]

print(K-max(distance))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a2 = []
for i, d in enumerate(a):
    if i != 0:
        a2.append(d - a[i - 1])
    else:
        a2.append(d + k - a[n - 1])
# print(a, a2)
a2.sort(reverse=True)
# print(a, a2)
print(k - a2[0])

Submission status: Accepted

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

for i in range(len(a)-1):
    if diff < a[i] - a[i+1]:
        diff = a[i] - a[i+1]

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

ans = k-diff

print(ans)

Submission status: Accepted

Solution code:
KN = list(map(int, input().split()))
dis = list(map(int, input().split()))
lists = []
for i in range(KN[1]-1):
    lists.append(dis[i+1]-dis[i])

lists.append(KN[0]+dis[0]-dis[KN[1]-1])
print(KN[0]-max(lists))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# coding: utf-8
# import sys
# import pprint
# import numpy as np
# import code
import functools

if __debug__:
    debug = lambda *x: None
else:
    import logging
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    debug = logger.debug

k, n, *a = (int(x) for x in input().split())
a = [int(x) for x in input().split()]

debug(k)
debug(n)
debug(a)

aa = a.copy()
aa.pop(0)
aa.append(k + a[0])
debug(aa)
# r = functools.reduce(lambda )

d = [y - x for x, y in zip(a,aa)]
debug(d)
d.sort()
debug(d)
d.pop()
debug(d)
print(sum(d))

Submission status: Accepted

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

print(k-max(dist))

Submission status: Accepted

Solution code:
import scipy as sp

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

a_list = sp.array(a_list)
sub_a_list = (a_list - sp.roll(a_list, 1))[1:]
sub_a_list = sp.append(sub_a_list, a_list[0]+k-a_list[-1])
print(k-max(sub_a_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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[-1] + a[0])
print(k - max(b))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

A.append(A[0] + K)

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

Submission status: Accepted

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

a.append(a[0] + k)

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

print(k - max_distance)

Submission status: Accepted

Solution code:
E = [input() for i in range(2)]
E0 = E[0].split()
K = int(E0[0])
N = int(E0[1])
E12 = E[1].split()
E1 = [int(s) for s in E12]
E1.append(K + E1[0])
d = 0
M = 0
gap = []
for k in range(len(E1)-1):
    gap.append(int(E1[k+1]) - int(E1[k]))
    d += gap[k]
    M = max(M, gap[k])


d -= M

print(d)

Submission status: Accepted

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

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

out = K - max(sa)

print(out)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(k, n, a):
    a.append(a[0] + k)
    return min((k - a[i+1] + a[i]) for i in range(n))

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

Submission status: Accepted

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

for i in range(len(list_point)):
  list_point[i] = int(list_point[i])

#点と点との距離を記録
distance = []
for i in range(len(list_point)):
  if i == 0:
    distance.append(list_point[i] + N-list_point[len(list_point)-1])
    distance.append(list_point[i+1]-list_point[i])
    continue
  if i == len(list_point)-1:
    break
  distance.append(list_point[i+1]-list_point[i])


distance.sort()

distance.remove(max(distance))


print(sum(distance))

Submission status: Accepted

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

Submission status: Accepted

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

if abs(K - A[N-1] + A[0]) >= max:
    max = abs(K - A[N-1] + A[0])

print(K - max)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
K,N,*A = map(int, read().split())
A += [A[0]+K]
gap = max(y-x for x,y in zip(A, A[1:]))

print(K-gap)

Submission status: Accepted

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

Submission status: Accepted

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

maxd = 0

for i in range(n):
  if i < (n-1):
    d = alist[i+1] - alist[i]
  else:
    d = k - ( alist[i] - alist[0] )
  if maxd < d:
    maxd = d
print(k - maxd)

Submission status: Accepted

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

Submission status: Accepted

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

A_split.pop(A_split.index(max(A_split)))
print(sum(A_split))

Submission status: Accepted

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

K, N, *A = map(int, read().split())
A += [A[0] + K]
gap = max(y-x for x,y in zip(A, A[1:]))
print(K - gap)

Submission status: Accepted

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

def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    max_ = A[0] + K - A[-1]
    for i in range(1, N):
        max_ = max(max_, A[i] - A[i-1])
    print(K - max_)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

mx = 0
for i in range(n-1):
    tmp = a[i+1] - a[i]
    if mx < tmp:
        mx = tmp

tmp = k - a[n-1] + a[0] 
if tmp > mx:
    mx= tmp

print(k-mx)

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0]+k)
A = np.array(A)

A_shift = np.roll(A, -1)
A_diff = A_shift - A
ans = k - np.max(A_diff[:-1])
print(str(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A=input()
B=input()
AA=A.split()
K=int(AA[0])
N=int(AA[1])
d=(B.split())
dis=[]
for i in range(N-1):
    dis.append(int(d[i+1])-int(d[i]))

dis.append(int(d[0])+(K-int(d[N-1])))

print(K-max(dis))

Submission status: Accepted

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

distance = []
diff = 0

for i in range(0, N):
    
    if i == N-1:
        diff = K + A_list[0] - A_list[i]
        distance.append(diff)
        
    else:
        diff = A_list[i+1] - A_list[i]
        distance.append(diff)
        

print(sum(distance) - max(distance))

Submission status: Accepted

Solution code:

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right


if __name__ == "__main__":
    k,n = list((map(int, input().split(' '))))
    a = list(map(int, input().split(' ')))
    # a = quick_sort
    result = 0
    for i in range(1,n):
        length = a[i]-a[i-1]
        result = max(length, result)
        
    length = a[0] + (k-a[n-1])
    result = max(result, length)
    print(k-result)
    





      
# for i in range(10):
#     print(i)
#     if i%5==4:
#         break

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n,*a=map(int,open(0).read().split())
a+=[a[0]+k]
d=[abs(a[i]-a[i+1]) for i in range(n)]
print(k-max(d))

Submission status: Accepted

Solution code:
first = input()
first = first.split()

K = int(first[0])
N = int(first[1])

second = input()
second = second.split()

distance = []
diff = 0

for i in range(0, N):
    if i == N-1:
        diff0 = 0
        diff1 = 0
        diff0 = int(second[0])
        diff1 = K - int(second[i])
        diff = diff0 + diff1
        distance.append(diff)
    else:
        diff = int(second[i+1]) - int(second[i])
        distance.append(diff)
        
MaxIndex = distance.index(max(distance))
distance.pop(MaxIndex)
Max = sum(distance)
print(Max)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

distance = [A_list[i] - A_list[i - 1] for i in range(len(A_list))]
distance[0] = A_list[0] + K - A_list[N - 1]
index = distance.index(max(distance))

print(sum(distance) - distance[index])

Submission status: Accepted

Solution code:
k,n  = map(int,input().split())
list_a = list(map(int,input().split()))
list_kankaku = []
x = 0
for i in range(n-1):
  list_kankaku.append(list_a[i+1]-list_a[i])
list_kankaku.append(k-list_a[-1]+list_a[0])
x = max(list_kankaku)
ans = k-x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

max = k + a[0] - a[n-1]
prev = a[0]
for i in a[1:]:
    tmp = i - prev
    if tmp > max:
        max = tmp

    prev = i

print(k-max)

Submission status: Accepted

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

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

print(k-max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a)
diff = [0]*N
for i in range(N):
    diff[i] = a[i] - a[i-1]
    if i == 0: diff[i] += K

print(K-max(diff))

Submission status: Accepted

Solution code:
[k,n] = list(map(int, input().split()))
 
li = list(map(int, input().split()))
 
distances = []
for i in range(n):
    if i == 0:
        distances.append(li[n-1] - li[i])
    else:
        distances.append(k - (li[i] - li[i-1]))
 
print(min(distances))

Submission status: Accepted

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

for i in range(n):
    if i == n-1:
        b.append(k - int(a[i]) + int(a[0]))
    else:
        b.append(int(a[i+1]) - int(a[i]))
    s += b[i]

s = s - max(b)
print(s)

Submission status: Accepted

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

aaa.append(aaa[0] + a)

result = [c1 - c0 for c0,c1 in zip(aaa,aaa[1:])]

print(a - max(result))

Submission status: Accepted

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

Submission status: Accepted

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

if a[0]==0:
    a.append(k)
else:
    a.append(k+a[0])
l=[]
for i in range(len(a)-1):
    s = a[i+1]-a[i]
    if s<0:
        s=-s
    l.append(s)
max_value=max(l)
#max_index=l.index(max_value)
print(k-max_value)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
A=list(map(int,input().split()))
dis=[]
for i in range(len(A)-1):
  dis.append((A[i]+k)-A[i+1])
dis.append(max(A)-min(A))
print(min(dis))

Submission status: Accepted

Solution code:
import sys

k, n = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
a.sort()

max_len = 0
total = 0
for i in range(n):
    if a[i] < a[i-1]:
        l = a[i] + k - a[i-1]
    else:
        l = a[i] - a[i-1]
    max_len = max(max_len, l)
    total += l

print(total-max_len)

Submission status: Accepted

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

distance = []
for i in range(len(A)-1):
    distance.append(A[i]+K-A[i+1])
distance.append(max(A)-min(A))
print(min(distance))

Submission status: Accepted

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

A.append(K+A[0])

ans=0
maxdist=0
for i,a in enumerate(A):
    if i == len(A)-1:
        break
    dist = A[i+1]-a   
    ans += dist
    maxdist=max(maxdist,dist)
print(ans-maxdist)

Submission status: Accepted

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

A.append(K+A[0])

maxdist,maxindx = 0,0
for i,a in enumerate(A):
    dist = -a
    if i+1 == len(A):
        dist += K
    else:
        dist += A[i+1]
    if dist > maxdist:
        maxdist = dist
        maxindx = i
ans = 0
num=0
for x,a in enumerate(A):
    if num==len(A)-2:
        break
    if x == maxindx:
        continue
    num+=1
    if x+1 == len(A):
        ans += K-a
    else:
        ans += A[x+1]-a
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

d=[]

for i in range(1,len(x)):
    d.append(x[i]-x[i-1])
    
d.append(min(x[-1]-x[0], n-x[-1]+x[0]))
print(n-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = sorted([int(a) for a in input().split()])
D = [abs(A[i]-A[i+1]) if i != N-1 else abs(A[i]-K-A[0]) for i in range(N)]
D.remove(max(D))
print(sum(D))

Submission status: Accepted

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

b = a + [a[0]+k]
longest_distance = 0
for bi, ai in zip(b[1::],a):
    longest_distance = max(longest_distance, bi-ai)
print(k-longest_distance)

Submission status: Accepted

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

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

num = K - check
print(num)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(min(distances))

Submission status: Accepted

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

Submission status: Accepted

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

B = [A[i+1] - A[i] for i in range(N-1)]
B.append(A[0]-A[-1]+K)
B.sort()
print(sum(B[:-1]))

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff = np.diff(A)
diff = np.append(diff, A[0] + K - A[-1])
minimum = K - max(diff)
print(minimum)

Submission status: Accepted

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

q_dist = 0

for i in range(N):
    q_dist = max(A[i + 1] - A[i], q_dist)

print(K - q_dist)

Submission status: Accepted

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

relative = []
relative.append(distance[0])
for i in range(int(N)-1):
    relative.append(distance[i+1] - distance[i])
relative[0] += (int(K) -  distance[-1])
print(sum(relative) - max(relative))

Submission status: Accepted

Solution code:
K, N = [int(s) for s  in input().split()]
A = [ int(s) for s in input().split() ]

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

print(K - max(D))

Submission status: Accepted

Solution code:
inputs = [int(i) for i in input().split()]
K,N = inputs[0],inputs[1]

input_lines = [int(i) for i in input().split()]

max_range = input_lines[0]+(K-input_lines[-1])
for i in range(N):
    if i!=N-1:
        torange = input_lines[i+1]-input_lines[i]
        if torange>max_range:
            max_range = torange
print(K-max_range)

Submission status: Accepted

Solution code:
k, n = input().split()
a_list_org = list(map(int,input().split()))
a_list = []
for a in a_list_org:
    if a == a_list_org[0]:
        pre = a
        a_list.append(int(k) - a_list_org[-1] + a)
    else:
        a_list.append(a - pre)
        pre = a
a_list.remove(max(a_list))
print(sum(a_list))

Submission status: Accepted

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

max_diff = a[0] + K - a[-1]
for i in range(1, N):
    max_diff = max(max_diff, a[i] - a[i-1])
print(K-max_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main(k, n, A):
    distances = [A[0] + k-A[-1]]

    for i in range(1, n):
        distances.append(A[i] - A[i-1])

    print(k - max(distances))


if __name__ == "__main__":
    k, n = map(int, input().split())
    A = list(map(int, input().split()))

    main(k, n, A)

Submission status: Accepted

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

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

x = 10 ** 6

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

print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
k,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
i=a[0]
l=0 # 最大距離
for j in a[1:]:
  if l < j-i: # j-1とjの距離
    l = j-i
  i=j
if l < a[0]+k-a[-1]:
  l=a[0]+k-a[-1]
print(k-l)

Submission status: Accepted

Solution code:
kn_list = input().split(" ")
K = int(kn_list[0])
N = int(kn_list[1])
a_list = [int(num) for num in input().split(" ")]

max_distance = 0
for i in range(N-1):
    distance = a_list[i+1] - a_list[i]
    if max_distance < distance:
        max_distance = distance
if max_distance < K - (a_list[N-1]-a_list[0]):
    max_distance = K - (a_list[N-1]-a_list[0])
print(K - max_distance)

Submission status: Accepted

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


a = list(map(int,input().split()))	
maxdis = a[0]+K-a[-1]
alldis = a[0]+K-a[-1]
a.sort()
for i in range(1,N):
    alldis += a[i] - a[i-1]
    maxdis = max(maxdis,a[i]-a[i-1])
print(alldis-maxdis)

Submission status: Accepted

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

A = [int(a) for a in input().split()]
Abar = [0] + A
A.append(A[0]+k)
A = np.array(A)
Abar = np.array(Abar)
dis = A - Abar
print(k - max(dis))

Submission status: Accepted

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

maxV = 0
ans = 0
for i in range(N-1):
    tmp = A[i+1] - A[i]
    ans += tmp
    maxV = max(tmp, maxV)
ans += K - A[N-1]+A[0]
# print(ans)
maxV = max(maxV, K - A[N-1]+A[0])
print(ans - maxV)

Submission status: Accepted

Solution code:
"""時計回り／反時計回りのどちらで行くかを考えると終わるのでは
"""
K, N = (int(x) for x in input().split(' '))
A = [int(x) for x in input().split(' ')]
#各家の間の距離も求めてGに追加していく
G = []
for i in range(len(A)-1):
  G += [A[i+1]-A[i]]
#先頭と最後の1軒距離を求める
G += [(K - (A[N-1] - A[0]))]
#一番幅の広い距離を避けて一周回る
print(sum(G)-max(G))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

distances = []
for i in range(int(n)-1):
    distances.append(house_list[i+1] - house_list[i])
distances.append(int(k) - house_list[int(n)-1] + house_list[0])
print(int(k) - max(distances))

Submission status: Accepted

Solution code:
# ABC 160 C
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = -1*float('inf')
for i in range(n-1):
  # N番目の家とN-1番目の家の距離まで求める。
  ans = max(ans, a[i+1]-a[i])
  # N番目の家と１番目の家の距離も最後に求めて最大値をとる。
ans = max(k+a[0]-a[n-1], ans)
# 最短距離になる条件は、片道かつ二点間の家の距離が最も大きい区間を避けること、よって円周から導出した最大値を引いた値を出力して終了。
#　N番目の家と一番目の家の距離計算は工夫が必要。
print(k-ans)

Submission status: Accepted

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

Submission status: Accepted

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

D=[]

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

Dmax=0
Sum=0
for i in range(N):
  if int(D[i])>Dmax:
    Dmax=int(D[i])
  Sum=Sum+int(D[i])   
  
print(Sum-Dmax)

Submission status: Accepted

Solution code:
import sys
 
k, n = map(int, sys.stdin.readline().split())
a = [int(i) for i in sys.stdin.readline().split()]
a.sort()
max_dist = 0
for i in range(1, n):
	max_dist = max(a[i] - a[i - 1], max_dist)
    
if len(a) != 0:
	max_dist = max(a[0] + k - a[-1], max_dist)
sys.stdout.write(str(k - max_dist))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
data1 = input()
data2 = input()
circle = int(data1.split()[0])
house = int(data1.split()[1])
distance = [int(s) for s in data2.split()]
#隣接間の最大距離を省いてみる
between = []
for i,d in enumerate(distance):
  if i != house-1:
    between.append(distance[i+1]-d)
  else:
    between.append(circle-d+distance[0])
print(sum(between)-max(between))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
lake, _ = [int(e) for e in input().split()]
arr = [int(h) for h in input().split()]
length = [n2-n1 for n1, n2 in zip(arr[:-1], arr[1:])]
length.append(lake-arr[-1]+arr[0])
length.sort()
print(lake-length[-1])