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:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

max_diff = 0
for i in range(1, N + 1):
    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_d = 0
max_idx = 0
D = []
for i in range(N - 1):
    D.append(min(abs(A[i + 1] - A[i]), abs(A[i] + (K - A[i + 1]))))
    if D[i] > max_d:
        max_d = D[i]
        max_idx = i
D.append(min(abs(A[N - 1] - A[0]), abs(A[0] + (K - A[N - 1]))))
if D[N - 1] > max_d:
    max_idx = N - 1

res = 0
for i in range(N):
    if i != max_idx:
        res += D[i]
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

(k, n), s = map(int, input().split()), sorted(list(map(int, input().split())))
print(k - sorted([s[i] - s[i - 1] for i in range(1, n)] + [k - s[-1] + s[0]])[-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a.append(k + a[0])
diff = [a1 - a0 for a0, a1 in zip(a, a[1:])]

print(k - max(diff))

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

print(k - max(diff))

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[N - 1])
print(sum(d) - max(d))

Submission status: Accepted

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


min_=99999999999

for i in range(n):
	if i==0:
		x=arr[-1]-arr[0]
	else:
		x=k-(arr[i]-arr[i-1])
	if x<min_:
		min_=x


print(min_)

Submission status: Accepted

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

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

min_=99999999999

for i in range(n):
	if arr[i+n-1]-arr[i]<min_:
		min_=arr[i+n-1]-arr[i]


print(min_)

Submission status: Accepted

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

Submission status: Accepted

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

[k-a[-1]+a[0]]
d = []

for i in range(n):
    if i>0:
        d.append(a[i]-a[i-1])
d.append(k-a[-1]+a[0])
print(k-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

min_ = float('inf')
for i in range(len(A)):
    if B[i+N-1]-B[i] < min_:
        min_ = B[i+N-1]-B[i]

print(min_)

Submission status: Accepted

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

b.append(k - a[-1] + a[0])
ans = k - max(b)
print(ans)

Submission status: Accepted

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

print(sum(b)-max(b))

Submission status: Accepted

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

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

Submission status: Accepted

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

A_dis = [a - A[i-1] if i > 0 else 0 for i, a in enumerate(A)]

A_dis.append(k - A[-1] + A[0])

A_dis.remove(max(A_dis))

print(sum(A_dis))

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

k,n = MAP()
a = LIST()
a.sort()

mx = k-a[-1] + a[0]

for i in range(1, n):
    mx = max(mx, a[i]-a[i-1])

print(k-mx)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int, input().split()))
a.sort()
#print(a)
#さが一番大きいところを見つける
dif=0
for i in range(n-1):
    dif=max(dif,a[i+1]-a[i])
dif=max(dif,k-a[n-1]+a[0])
print(k-dif)

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

Submission status: Accepted

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

A = list([int(x) for x in input().split()])

r_list = []

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

    else:
        r_list.append(A[i + 1] - A[i])

r_list.sort()

print(sum(r_list[:-1]))

Submission status: Accepted

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

saityou = K - A[N-1] + A[0]
start = N-1
for x in range(N-1):
    if saityou < A[x+1] - A[x]:
        saityou = A[x+1] - A[x]
        start = x
print (K-saityou)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-m)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
#aを1直線状のリストと考える
a.append(k+a[0])
#k+a[0]はkを超えた後のa[0]
d = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k, n, *aa = map(int, open(0).read().split())

aa.append(k+aa[0])

print(k - max(a - b for a, b in zip(aa[1:], aa[:-1])))

Submission status: Accepted

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

print(sum(dist) - max(dist))

Submission status: Accepted

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

Submission status: Accepted

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

dtlst = [al[0]]
for i in range(1,n):
  dtlst.append(al[i]-al[i-1])
dtlst.append(k - al[-1]+al[0])

print(k - max(dtlst))

Submission status: Accepted

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

A.append(K + A[0])

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

l.remove(max(l))
print(sum(l))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
max = 0
b = []
for i in range(n):
  if i ==0:
    b.append(a[0]+k-a[n-1])
  elif not i==0:
    b.append(a[i]-a[i-1])
for i in range(n):
  if max < b[i]:
    max = b[i]
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[-1] + A[0])
for i in range(len(A) - 1):
	B.append(A[i + 1] - A[i])
print(sum(B) - max(B))

Submission status: Accepted

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

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

D = [A[0]+K-A[-1]]

for i in range(1,N):
    d = A[i] - A[i-1]
    D.append(d)
print(K-max(D))

Submission status: Accepted

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

l=list(map(int, input().split(' ')))

diffs=[]
for i in range(1,n):
  diffs.append(l[i]-l[i-1])
diffs.append(c-l[-1]+l[0])

print(sum(diffs)-max(diffs))

Submission status: Accepted

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

a.sort()
a.append(a[0])

max_d = max([min(abs(a[i+1]-a[i]), K-a[i]+a[i+1]) for i in range(len(a)-1)])
result = K-max_d

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

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

max_len = a_l[0] + (K - a_l[-1])

past = a_l[0]
for a in a_l[1:]:
    length = a - past
    if length > max_len:
        max_len = length
    past = a
print(K-max_len)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a[0] = a[0] + k
b = [a[0] - a[-1]]
for i in range(1, n):
  b.append(a[i] - a[i-1])
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)
print(K-max(abs(b-a) for a, b in zip(A, A[1:])))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
k, n = [int(w) for w in input().split()]
a_li = [int(w) for w in input().split()]

dist = k - a_li[-1] + a_li[0]

for a1, a2 in zip(a_li[:-1], a_li[1:]):
    dist = max(dist, a2-a1)

print(k - dist)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
k, n = [int(w) for w in input().split()]
a_li = [int(w) for w in input().split()]

dist = k - a_li[-1] + a_li[0]

for i in range(1, n):
    dist = max(dist, a_li[i] - a_li[i - 1])

print(k - dist)

Submission status: Accepted

Solution code:
import numpy as np

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

b = np.array(a[1:] + [a[0] + k])
c = np.array(a)

print(k - max(b - c))

Submission status: Accepted

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

sumA = A[-1] - A[0]
Ai = sumA

for i in range(1, N):
  sumA = min(sumA, (A[i-1] + K) - A[i])

print(sumA)

Submission status: Accepted

Solution code:
ans = 10**12
K, N = map(int, input().split())
*A, = map(int, input().split())
for i in range(N):
    v = abs((A[i]+K)-A[i-1])%K
    ans = min(ans, K-v)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
k, n = map(int, sys.stdin.readline().split())
an = list(map(int, sys.stdin.readline().split()))

dai = an[0]+k-an[-1]

for i in range(1, n):
    if an[i]-an[i-1]>dai:
        dai = an[i]-an[i-1]

print(k-dai)

Submission status: Accepted

Solution code:
import sys
k, n = map(int, sys.stdin.readline().split())
an = list(map(int, sys.stdin.readline().split()))

dai = an[0]+k-an[-1]

for i in range(1, n):
    dai = max(dai, an[i]-an[i-1])

print(k-dai)

Submission status: Accepted

Solution code:
K,N = (int(a) for a in 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[N-1] = A[0] + K - A[N-1] 
print(sum(B) - max(B))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
Nlist = list(map(int, input().split()))
ans = min(Nlist[N-1]-Nlist[0], Nlist[0]+K-Nlist[N-1])
sa = Nlist[0]
for i in range(1, N):
    if Nlist[i]-sa > ans:
        ans = Nlist[i]-sa
    sa = Nlist[i]

print(K-ans)

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()))
    mind=K-(A[0]-A[-1]+K)
    for i in range(1,N):
        mind=min(mind,K-(A[i]-A[i-1]))
    print(mind)
    
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
Alist = list(map(int, input().split()))
  
#for each house, traverse clock/anti-clock wise
Alen = len(Alist)
min_distance = 10000000
for s in range(Alen):
  distance = 0
  current_p = s
  #clock-wise distance
  if (current_p == (Alen-1)):
    next_p = 0
    distance = Alist[current_p]-Alist[next_p]
  else:
    next_p = current_p+1
    distance = K-(Alist[next_p]-Alist[current_p])
  if (distance < min_distance):
    min_distance = distance
  #anti clock-wise distance
  if (current_p == 0):
    next_p = Alen-1
    distance = Alist[next_p]-Alist[current_p]
  else:
    next_p = current_p-1
    distance = K-(Alist[next_p]-Alist[current_p])
  if (distance < min_distance):
    min_distance = distance
print(min_distance)

Submission status: Accepted

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

a.sort()
# print(a)

dist = []
for i in range(n):
    if i!=n-1:
        dist.append(min(a[i+1]-a[i], a[i]+(k-a[i+1])))
    else:
        dist.append(min(a[i]-a[0], a[0]+(k-a[i])))
# print(dist)
print(k-max(dist))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
K,N = map(int,readline().split())
A = list(map(int,readline().split()))
A = sorted(A)
A.append(A[0]+K)
max_dist = 0
ans = 0
for i in range(N):
    dist = A[i+1] - A[i]
    max_dist = max(max_dist,dist)
    ans += dist
print(ans-max_dist)

Submission status: Accepted

Solution code:
import itertools
import functools
import math
from collections import Counter



K,N = map(int,input().split())
A = list(map(int,input().split()))
for i in range(N):
    A.append(K+A[i])

sum = 0
chk = -1

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


print(K-sum)

Submission status: Accepted

Solution code:
#n , m , l = map(int , input().split())

# list_n = list(map(int , inpuut().split()))<Paste>

#n = input()
# list = [input() for i in range(N)

#list = [[i for i in range(N)] for _ in range(M)]


K, N = map(int, input().split())
List_A = list(map(int, input().split()))
List_AA = List_A[1:]+List_A[:1]
List_d = List_AA

for i in range(N):
    d = abs(List_AA[i]-List_A[i])
    List_d[i] = min(d, K-d)
ans = K - max(List_d)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-1):
  ans=max(ans, l[i+1]-l[i])
print(k-ans)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=sorted(list(map(int,input().split())))
dist=2*10e+12
for i in range(n):
  p1=i
  p2=i+1
  if i+1!=n:
    dist=min(k-(a[p2]-a[p1]), dist)
  else:
    p2=0
    dist=min(a[p1]-a[p2],dist)
    print(dist)

Submission status: Accepted

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

max_diff = k + a[0] - a[n-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:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    ans = INF
    for i in range(N - 1):
        ans = min(ans, K - (A[i + 1] - A[i]))

    ans = min(ans, K - (K - A[-1] + A[0]))
    return ans


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(K-max_dis)

Submission status: Accepted

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

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

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

print(sum(distance))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = sorted(a)

d = -1

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

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

ans = k - d
print(ans)

Submission status: Accepted

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

B = []
for i in range(len(A) - 1):
    B.append(A[i + 1] - A[i])
B.append(A[0] + K - A[-1])

print(sum(sorted(B)[:-1]))

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,K-A[-1]+A[0])
print(K-ans)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
d=[]
d.append(k-a[-1]+a[0])
for i in range(1,n):
  d.append(a[i]-a[i-1])
ans=10**18
for di in d:
  t=k-di
  ans=min(ans,t)
print(ans)

Submission status: Accepted

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

An_list.append(An_list[0]+K)
#print(An_list)

distance=[0]*N

for i in range(0,N):
    distance[int(i)]=An_list[int(i+1)]-An_list[int(i)]

distance.sort()
del distance[-1]
#print(distance)
print(sum(distance))

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
Alist = ["_"]
Alist += list(map(int,input().split()))
maxway = 0
for i in range(1,N+1):
    if i == N:
        temp = (K-Alist[N]) + Alist[1]
    else:
        temp = Alist[i+1] - Alist[i]
    
    maxway = max(maxway,temp)

print(K-maxway)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
def main():
    k, n = map(int, input().split())
    a = tuple(map(int, input().split()))
    dis_max = (k - a[-1]) + a[0]
    for i1 in range(1, n):
        dis = a[i1] - a[i1-1]
        dis_max = max(dis_max, dis)
    print(k - dis_max)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

M = 0
for i in range(-1, N-1):
  dis = abs(A[i] - A[i + 1])
  if 2*dis > K:
    dis = K - dis
  #print(dis)
  if dis > M:
    M = dis
    
print(K-M)

Submission status: Accepted

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

a.append(a[0] + k)
max_dist = 0

for i in range(len(a)-1):
    max_dist = max(max_dist,a[i+1]-a[i])

print(k-max_dist)

Submission status: Accepted

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

A = [j for j in map(int, input().split())]

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

print(K-max(max(d), K-A[N-1]+A[0]))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    m = 0
    for i in range(n):
        if i<n-1:
            m = max(m,a[i+1]-a[i])
        else:
            m = max(m,k-a[i]+a[0])
    print(k-m)
    return

if __name__=='__main__':
    Main()

Submission status: Accepted

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i<n-1:
            dist.append(a[i+1]-a[i])
        else:
            dist.append(k-a[i]+a[0])
    x = max(dist)
    print(k-x)
    return

if __name__=='__main__':
    Main()

Submission status: Accepted

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

longest = k - (A[-1] - A[0])

for i in range(n-1):
    if A[i+1] - A[i] > longest:
        longest = A[i+1] -A[i]
print(k-longest)

Submission status: Accepted

Solution code:

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

ans = 10**18
for start_i in range(n):

    if start_i == 0:
        clock = al[-1] - al[0]
    else:
        clock = k - al[start_i] + al[start_i-1]

    if start_i == n-1:
        c_clock = al[-1] - al[0]
    else:
        goal = start_i+1
        c_clock = al[start_i] + (k-al[goal]) 

    ans = min(ans, clock, c_clock)
    # print(clock)
    # print(c_clock)

print(ans)

Submission status: Accepted

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

for i in range(n):
  if i == 0:
    sa = a[i + 1] - a[i]
  elif i == (n - 1):
    if sa < (k + a[0]) - a[i]:
      sa = (k + a[0]) - a[i]
  else:
    if sa < a[i + 1] - a[i]:
      sa = a[i + 1] - a[i]

min = k - sa
print(min)

Submission status: Accepted

Solution code:
k,n,*l=map(int,open(0).read().split())
print(min((l[i-1]-l[i])%k for i in range(n)))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(B))

Submission status: Accepted

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

Submission status: Accepted

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

L =[]
for i in range(1,n):
    L.append(A[i]-A[i-1]) #A[0],A[-1]以外のの道
L.append(k-A[-1] + A[0])#A[0],A[-1]の道
print(k - max(L))#一つだけ通らなくてもいい

Submission status: Accepted

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

d = []
for i in range(1, n):
    d.append(A[i] - A[i - 1])
d.append(k - A[-1] + A[0])

print(k - max(d))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(sum(dp) - max(dp))

Submission status: Accepted

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

#a_lstの要素の隣同士の距離で最大のものを求め、kから減算したものが最短距離？
tmp_kyori = k - abs(a_lst[-1]-a_lst[0])#最初だけ反時計回りの距離を算出
for i in range(len(a_lst)-1):
    sa_kyori = a_lst[i+1] - a_lst[i] 
    if sa_kyori > tmp_kyori:
        tmp_kyori = sa_kyori

print(k-tmp_kyori)

Submission status: Accepted

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

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

diff.sort()
ans = K - diff[-1]
print(ans)

Submission status: Accepted

Solution code:
K,N=map(int, input().split()) 
c=list(map(int, input().split())) 
a = 0
b = 0
for i in range(N):
  if i == 0:
    a = min((c[N-1]-c[0]),K-(c[i+1]-c[0]))
  elif i == N-1:
    a = min(a,K-(c[i-1]-c[i]))
    a = min(a,K-(c[0]-c[i]))
    
  else:
    a = min(a,K-(c[i-1]-c[i]))
    a = min(a,K-(c[i+1]-c[i]))
    
print(a)

Submission status: Accepted

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


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

    l.insert(0, 0)
    l.append(k)
    dist_list = []
    for i in range(n+1):
        dist = l[i+1]-l[i]
        dist_list.append(dist)
    
    dist_list[0] = dist_list[0]+dist_list[-1]
    del dist_list[-1]
    print(sum(dist_list)-max(dist_list))
    

if __name__ == '__main__':
    main()