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:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

k, n = Is()
A = LI()

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

print(k-m)

Submission status: Accepted

Solution code:

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

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

print(k-max(d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    k, n = MI()
    A = LI()
    for i in range(n):
        A.append(k+A[i])
    # A = A * 2
    # print(A)
    S = [0] * (2 * n)

    ans = INF
    for i in range(1, 2 * n):
        # print(i)
        # S[i-1] = A[i] - A[i-1]
        S[i] = S[i-1] + A[i] - A[i-1]
        if i >= (n - 1):
            # print(S[i], S[i-n+1])
            ans = min(ans, S[i] - S[i-n+1])

    # print(S)
    print(ans)


    # R
    # for i in range(k-1, 2 * n):






if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

X=max(x,k-a[n-1]+a[0])
print(k-X)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

K, N = LI()
A = LI()

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

Submission status: Accepted

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

Submission status: Accepted

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

#N個の区間の中で最大距離の区間以外の和を取ると良い
disList = []
for i in range(n):
    if i == n-1:
        dis = k - a_[i] + a_[0]
        disList.append(dis)
    else:
        dis = a_[i+1] - a_[i]
        disList.append(dis)
max = max(disList)
print(k - max)

Submission status: Accepted

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

#N個の区間の中で最大距離の区間以外の和を取ると良い
disList = []
for i in range(n):
    if i == n-1:
        dis = k - a_[i] + a_[0]
        disList.append(dis)
    else:
        dis = a_[i+1] - a_[i]
        disList.append(dis)
max = max(disList)
disList.remove(max)

print(sum(disList))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
k,n = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]

a.sort()
a.append(a[0]+k) # 1番目の家とn番目の家の間の距離

d = 0
for i in range(n): # 1番目の家と2番目の家、2番目の家と3番目の家、、、n-1番目の家とn番目の家の間の距離
    d = max(d, a[i+1]-a[i]) # 一番離れている家はどれか

print(k-d) # 一番離れている家を最初と最後に行くことにするのが最短距離

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
k,n = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]

a.sort()

d = a[0]+(k-a[n-1])
for i in range(n-1):
    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 = [None]*n

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

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

print(k - max(d))

Submission status: Accepted

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

memo = []

for i in range(len(A)-1):
    distance = A[i+1] - A[i]
    memo.append(distance)

memo.append(K - A[-1] + A[0])
ans = sum(memo)
ans -= max(memo)
print(ans)

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

def hgrn(n):
	if n>=0:
		return n
	else:
		return n+k

k,n = readInts()
a = readInts()

ans = k

dis = []

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

print(k-max(dis))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k,n = map(int,input().split()) 
a =list(map(int,input().split()))
s =[a[i]-a[i-1] for i in range (1,n)] #2点距離をリストに格納(北端は越さない)  
s.append(k-a[n-1]+a[0]) #北端を超える2点距離をリストに格納
print(k-max(s))   #最長距離となる２点を見つける→一周から引けば良い

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18 #最小値を求めるので十分大きな値で初期化
for i in range(n): #上式を元に最短距離を求める
 dist1=(arr[i-1]-arr[i])%k
 dist2=(arr[i]-arr[(i+1)%n])%k
 ans=min(ans,dist1,dist2)
print(ans)

Submission status: Accepted

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

lst2 = []
for i in range(n):
  if i != n-1:
    lst2.append(lst[i+1]- lst[i])
  else:
    lst2.append(k- lst[i] + lst[0])
    
m = max(lst2)

print(sum(lst2) - m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
alist=list(map(int,input().split()))
difflist=[]
for i in range(n):
    tmpdiff=abs(alist[i]-alist[i-1])
    tmpdiff=min(tmpdiff,k-tmpdiff)
    difflist.append(tmpdiff)

print(k-max(difflist))

Submission status: Accepted

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

betweenD = [] # 隣の家との間隔
for i in range(N):
    if i==N-1:
        betweenD.append(K - A[i] + A[0])
    else:
        betweenD.append(A[i+1] - A[i])

print(sum(betweenD) - max(betweenD)) # 間隔が最も離れている家同士の移動を避ける

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

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

max_interval = 0
for i in range( int( n ) - 1 ):
    interval = int( a[i+1] ) - int( a[i] )
    if max_interval < interval:
        max_interval = interval

interval = int( k ) - int( a[-1] ) + int( a[0] )
if max_interval < interval:
        max_interval = interval

print( int( k ) - max_interval )

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

print(k - max(d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k,n = map(int,input().split())
li = list(map(int,input().split()))
cnt=k+li[0]-li[-1]

for i in range(len(li)-1):
    if li[i+1]-li[i]>=cnt:
        cnt=li[i+1]-li[i]
print(k-cnt)

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): 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 LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
input = sys.stdin.readline

K,N = MI()
l = LI()
max_dis = 0
for i in range(N):
    if i < N-1:
        next_dis = l[i+1]-l[i]
    else:
        next_dis = l[0]+(K-l[i])
    max_dis = max(max_dis,next_dis)
print(K-max_dis)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

k,n = map(int,input().split())
a = np.array(list(map(int,input().split())))
d = a[1:] - a[:-1]
d = np.append(d,a[0]+k-a[-1])

d.sort()
print(sum(d[:-1]))

Submission status: Accepted

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

max1 = 0
for i in range(n-1):
    a1 = a_lst[i]
    a2 = a_lst[i+1]

    max1 = max(max1, a2-a1)

max2 = k - a_lst[-1] + a_lst[0]

max = max(max1, max2)

print(k-max)

Submission status: Accepted

Solution code:
K,N=map(int, input().split())
A=list(map(int, input().split()))
d=[0]*(N+1)
if A[0]!=0:
    tmp=A[0]
    A=list(map(lambda x: x-tmp, A))
for n in range(N+1):
    if n==0:
        d[n]=A[0]
    elif n==N:
        d[n]=K-A[n-1]
    else:
        d[n]=A[n]-A[n-1]
print(sum(d)-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - max(B))

Submission status: Accepted

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

A.sort()

x = 0
for i in range(N-1): #Nにi+1を入れるとはみ出るからN-1
    if x < A[i+1] - A[i]:
        x = A[i+1] - A[i]

if x < A[0] + (K - A[N-1]):
    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()))

max_result = A[0]+K-A[N-1]

for i in range (N-1):
    
    before = A[i]
    after = A[i+1]
    diff = after - before
    if diff>=max_result:
        max_result = diff

print(K-max_result)

Submission status: Accepted

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

A.sort()
X = 0
for i in range(N-1):
    X_ = A[i+1] - A[i]
    if X < X_:
        X = X_

X_ = A[0] + (K - A[N-1])
if X < X_:
    X = X_

print(K - X)

Submission status: Accepted

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

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

print(sum(D)-max(D))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
from copy import copy, deepcopy
from operator import itemgetter

from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
#dequeを使うときはpython3を使う、pypyはダメ
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds

#mod = 10**9+7
#w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

#N = int(input())
K, N = map(int, input().split())
#L = [int(input()) for i in range(N)]
A = list(map(int, input().split()))
#S = [list(map(int, input().split())) for i in range(N)]

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

print(K-max(ls))

Submission status: Accepted

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

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

max_span = max(span)
print(sum(span) - max_span)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
def circle_distances(A,K,N):
    ret = []
    for i in range(N):
        if i == N-1:
            d = K - abs(A[0] - A[i])
        else:
            d = A[i+1] - A[i]
        ret.append(d)
    return ret
distances = circle_distances(A,K,N)
print(K -max(distances))

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
a_list = [int(i) for i in input().split()]
behind_range = 0
max_distance = 0
a_list.append(K + a_list[0])
for a in a_list:
    distance = a - behind_range
    behind_range += distance
    if max_distance < distance:
        max_distance = distance

print(K - max_distance)

Submission status: Accepted

Solution code:
K, N = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [0]*N
total = 0
max_val = A[1]-A[0]
for n in range(N-1):
  B[n] = A[n+1]-A[n]
  total += B[n]
  if max_val<B[n]:
    max_val = B[n]
B[N-1]=A[0]+K-A[N-1]
total += B[N-1]
if max_val<B[N-1]:
  max_val = B[N-1]
print(total-max_val)

Submission status: Accepted

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


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

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

Submission status: Accepted

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

x=k-a[-1]+a[0] #A1とANの距離
for i in range(n-1):
    if x<=a[i+1]-a[i]:
        x=a[i+1]-a[i]

print(k-x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

_a = np.append(a,a[0]+k)
_a = np.delete(_a,0)

dis = _a -a
ans = k -np.max(dis)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N,K=map(int,input().split())
A=list(map(int,input().split()))
arr = np.array(A)
diff = np.diff(arr)

start_to_end=A[0]+N-A[K-1]

if max(diff) < start_to_end:
    print(sum(diff))
else:
    print(sum(diff)-max(diff)+start_to_end)

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())
A = list(map(int, input().split()))
cost_list = []
for i in range(len(A)-1):
  cost_list.append(A[i+1] - A[i])

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

print(sum(cost_list) - max(cost_list))

Submission status: Accepted

Solution code:
import numpy as np

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

print(K-max(dist))

Submission status: Accepted

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

print(str(int(K-max(D))))

Submission status: Accepted

Solution code:
import sys

k,n = (int(i) for i in input().split())

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



kyori = [None]*(n-1)

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

max = max(kyori)

print(k-max)

Submission status: Accepted

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

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

sum_l = sum(l)
sum_r = sum(r)

print(min(sum_l-max(l),sum_r-max(r)))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
a.append(k+a[0])
l = []
i = 0

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

print(k-max(l))

Submission status: Accepted

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a = np.append(a, a[0]+k)
print(k - max(a[1:] - a[:-1]))

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

Submission status: Accepted

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a1 = np.zeros(1, dtype=int)
a1 = np.append(a1, a)
a2 = a
a2 = np.append(a2, a[0]+k)
print(k - max(a2[1:] - a1[1:]))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = a[:]
for i in a:
    x.append(i+k)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

array=[]

for n in range(N-1):
  array.append(K-(A[n+1]-A[n]))
  
array.append(A[N-1]-A[0])

print(min(array))

Submission status: Accepted

Solution code:
#160-3 
k, n = map(int,input().split())
ans = 1e18
arr = list(map(int,input().split()))
for i in range(n): 
    dist1=(arr[i-1]-arr[i])%k
    dist2=(arr[i]-arr[(i+1)%n])%k
    ans=min(ans,dist1,dist2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    K, N = read_ints()
    A = read_ints()
    distance = K-(A[0]+(K-A[N-1]))
    for i in range(N-1):
        distance = min(distance, K-(A[i+1]-A[i]))
    return distance


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
K,N = (int(x) for x in input().split())
house = list(map(int,input().split()))

distance = []


for i in range(N-1):
    distance.append(house[i+1] - house[i])
    
distance.append(K - house[N-1] + house[0]) 



print(K-max(distance))

Submission status: Accepted

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

s=[]
for i in range(len(a)):
    if not i==len(a)-1:
        s.append(abs(a[i+1]-a[i]))
    else:
        s.append(k-a[i]+a[0])

s.remove(max(s))
print(sum(s))

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()))
    diff = [A[i+1] - A[i] for i in range(N-1)]
    diff.append(A[0]+K-A[N-1])
    print(sum(diff)-max(diff))


if __name__ == '__main__':
    main()

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()))
    diff = []
    for i in range(N-1):
        diff.append(A[i+1] - A[i])
    diff.append(A[0]+K-A[N-1])
    print(sum(diff)-max(diff))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

house=list(map(int,input().split()))
longest=K-(house[N-1]-house[0])

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


print(K-longest)

Submission status: Accepted

Solution code:
import re
K,W = map(int,input().split())
#N = str(input())
#K = int(input())
A = list(map(int,input().split()))
a=[K-A[-1]+A[0]]
for i in range(W-1):
    l = abs(A[i]-A[i+1])
    a.append(l)

a.remove(max(a))
print(sum(a))

Submission status: Accepted

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

l = list()

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

ans = K - max(l)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

a = [int(i) for i in a]

diff = list()
diff.insert(0,K-a[N-1]+a[0]) # seq 1
for i in range(2,N+1):       # seq 2 to N-1
  diff.insert(i-1,abs(a[i-1]-a[i-2]))

# print (diff)

print (sum(diff)-max(diff))

Submission status: Accepted

Solution code:

k,n = map(int,input().split())
a = list(map(int, input().split()))
a.append(a[0])
total = 0
mx = 0
for i,item in enumerate(a):
    if(i + 1 >= len(a)):
        break
    d = a[i+1] - a[i]
    if(d < 0) :
        d = k - a[i] + a[i+1]
    total += d
    mx = max([d,mx])
print(total - mx)

Submission status: Accepted

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

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

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

print(sum(ans) - max(ans))

Submission status: Accepted

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

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

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

print(min(l_min, r_min))



#r[0] = a[n-1] - a[0]
#r[1] = (k-a[1]) + a[0]
#r[2] = (k-a[2]) + a[1]
#...
#r[n-1] = (k-a[n-1]) + a[n-2]
#
#l[0] = a[0] + (k-a[1]) 
#l[1] = a[1] + (k-a[2]) 
#l[2] = a[2] + (k-a[3])
#...
#l[n-1] = a[n-1] - a[0]

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
k,n = map(int,input().split())
l = list(map(int,input().split()))
maxi = -1
for i in range(n-1):
  maxi = max(maxi,l[i+1]-l[i])
maxi = max(maxi,k-l[n-1]+l[0])
print(k-maxi)

Submission status: Accepted

Solution code:
def multi_input():
    return map(int, input().split())

N, K = multi_input()
A = list(map(int, input().split()))

d = [N-A[-1]+A[0]]
for i in range(1, K):
    d.append(A[i]-A[i-1])
    
s = sum(d)
mn = s
for i in range(K):
    mn = min(mn, s-d[i])
print(mn)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
A = [int(x) for x in input().split()]
dis = [0]*n

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

dis.append(k-sum(dis))

print(k-max(dis))

Submission status: Accepted

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(A[0] + K - A[N-1])
D.sort(reverse = True)
D[0] = 0
print(sum(D))

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a = list(map(int,input().split()))
a_l = 0
a_s = 0
a_g = 0
for i in range(n):
    if i < n -1:
        a_short = min(abs(k-abs(a[i]-a[i+1])),abs(a[i]-a[i+1]))
        if a_l < a_short:
            a_l = a_short
            a_s = i
            a_g = i+1
    else:
        a_short = min(abs(k-abs(a[0]-a[n-1])),abs(a[0]-a[n-1]))
        if a_l < a_short:
            a_l = a_short
            a_s = 0
            a_g = n-1
print(max(abs(k-abs(a[a_s]-a[a_g])),abs(a[a_s]-a[a_g])))

Submission status: Accepted

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

def main():
    *N, = map(int, open(0).read().split())
    A = N[2:]
    K = N[0]
    N = N[1]

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

main()

Submission status: Accepted

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

ans = 0
for num in range(n-1):
    distance = a_list[num+1] - a_list[num]
    if distance > ans:
        ans = distance

distance_2 = k+a_list[0]-a_list[n-1]
if distance_2 > ans:
    print(k - distance_2)
else:
    print(k - ans)

Submission status: Accepted

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

B = sorted([(A[i+1]- A[i]) for i in range(N)])
ans = K - B[-1]
print(ans)

Submission status: Accepted

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

A.sort()

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

print(K - tmp)