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())
A = list(map(int, input().split()))
diff = []
for i in range(N):
    if A[i-1] > A[i]:
        diff.append(A[i]-(A[i-1]-K))
    else:
        diff.append(A[i]-(A[i-1]))

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

Submission status: Accepted

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

Ai = list(map(int,input().split()))
kyori = []

for x in range(n-1):
  kyori.append(Ai[x+1]-Ai[x])
  
kyori.append(Ai[0]+k-Ai[n-1])

kyori.remove(max(kyori))

print(sum(kyori))

Submission status: Accepted

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

before = 0
max_n = 0
for num in nums:
  dist = num - before
  max_n = max(max_n, dist)
  before = num
max_n = max(max_n, K-nums[-1]+nums[0])
print(K-max_n)

Submission status: Accepted

Solution code:
# C - Traveling Salesman around Lake
K,N = map(int,input().split())
A = list(map(int,input().split()))
tmp = 0
for i in range(N):
    tmp = max(tmp, (A[i]-A[i-1])%K)
print(K-tmp)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = K
for i in range(N):
    start = A[i]
    end = K+A[i-1] if A[i-1] < A[i] else A[i-1]
    path = end-start
    ans = min(path, ans)

print(ans)

Submission status: Accepted

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

print(k-m)

Submission status: Accepted

Solution code:
# C - Traveling Salesman around Lake

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

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

print(k-max(bet))

Submission status: Accepted

Solution code:
k,n = map(int, input().split())
a = [int(x.strip()) for x in input().split()]
min_dist = k
for i in range(n):
  if i != 0 and i != n-1:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] + (k - a[i+1])
    if min_dist > min(dist_r,dist_l):
      min_dist = min(dist_r,dist_l)
  elif i == 0:
    dist_r = a[n-1] - a[i]
    dist_l = a[i] + (k - a[i+1])
    min_dist0 = min(dist_r,dist_l)
  else:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] - a[0]
    min_distn = min(dist_r,dist_l)
ans = min(min_dist,min_dist0,min_distn)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

k,n = mi()
a = li()

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

print(k-max_l)

Submission status: Accepted

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

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

ans = []

diss = []
suma = 0

for i in range(len(a)-1):
    dis = a[i+1] - a[i]
    diss.append(dis)


diss.append(k-a[-1]+a[0])
diss.pop(diss.index(max(diss)))
print(sum(diss))

Submission status: Accepted

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

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

m = max(d)
ans = sum(d) - m
print(ans)

Submission status: Accepted

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

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

A.append(k+A[0])

road = [A[i+1]-A[i] for i in range(n)]

print(sum(road)-max(road))

Submission status: Accepted

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

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

Submission status: Accepted

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

# 最も長い辺を求める
for i in range(n):
    d = abs(a[i] - a[i + 1])
    sum += d
    if max < d:
        max = d
# 最も長い辺を通らずに1周回るのが最小
print(sum - max)

Submission status: Accepted

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

for i in range(n) :
    d = abs(a[i]-a[i+1])
    sum += d
    if max<d :
        max = d
#    print(sum,d,max)

print(sum-max)

Submission status: Accepted

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

max_dist = K - A[N-1] + A[0]
s = 0
for i in range(0,N-1):
  if max_dist < A[i+1] - A[i]:
    max_dist = A[i+1] - A[i]
    s = i + 1

B = list(map(lambda x: int(copysign(x+K, x)), A[:s]))
A = A[s:] + B

ans = 0
for j in range(N):
  if j == 0:
    continue
  ans += A[j] - A[j-1]
  
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(K - l)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# N個の区間のうち通らないものは高々1つ
# N個の区間のうち最も長い区間を通らず、それ以外の区間を1度だけ通る経路が最短距離を与える
# よって、Kから最も長い区間を引いた長さが最短距離

# 実装では、北端をまたいだ区間の扱いに気を付ける
A.append(A[0]+K)
l = 0
for i in range(N):
  l = max(l, A[i+1]-A[i])

ans = K -l
print(ans)

Submission status: Accepted

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

print(K-l)

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

l.append(a[0]+(k-a[-1]))

b=max(l)

print(k-b)

Submission status: Accepted

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

a = sorted(a)

d = []
d.append(k-a[n-1]+a[0])

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

print(k-max(d))

Submission status: Accepted

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append((K-A[i]+A[0]))
    else:
        theta.append((A[i+1]-A[i]))
#print("sum",sum(theta))
theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=s
print(l_min)

Submission status: Accepted

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

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

#print("sum",sum(theta))

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=s
print(l_min)

Submission status: Accepted

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

for i in range(len(a)):
    if (i == len(a) - 1):
        distance = a[0] + (k - a[-1])

    else:
        tar2 = a[i + 1]
        tar1 = a[i]
        distance = tar2 - tar1

    diff.append(distance)

# スタート位置を見つける
index = diff.index(max(diff))
ans = sum(diff) - max(diff)

print(ans)
# print(index)
# > 2なので、15~5は避ける。
# if (index == len(diff)):

Submission status: Accepted

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

min_dist = K
for i in range(N):
	if i == 0:
		dist = houses[-1] - houses[i]
	else:
		dist = houses[i] - houses[i-1]
		dist = K - dist
			
	if dist < min_dist:
		min_dist = dist
		
print(min_dist)

Submission status: Accepted

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

dist = [0] * N

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

dist[N - 1] = (K - A[N - 1]) + A[0]

max_dist = max(dist)
print(K - max_dist)

Submission status: Accepted

Solution code:

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

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

LL = max(L)

print(K - LL)

Submission status: Accepted

Solution code:
#C
K, N=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    A.append(A[i]+K)
    Right=[]
for i in range(N):
    Right.append(A[N-1+i]-A[i])
print(min(Right))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


a=list(map(int,input().split()))
max=0
a.sort()

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

if max<k-abs(a[0]-a[-1]):
    max=k-abs(a[0]-a[-1])

print(k-max)

Submission status: Accepted

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

Submission status: Accepted

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

    d_lst = [0 for _ in range(n)]

    for i in range(n - 1):
        d_lst[i] = a_lst[i + 1] - a_lst[i]

    d_lst.append(a_lst[0] + k - a_lst[n - 1])

    ans = k - max(d_lst)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
k,n=[int(i) for i in input().strip().split(" ")]
arr=[int(i) for i in input().strip().split(" ")]
# maxDist=arr[0]+min(k-arr[-1],arr[-1]) 
maxDist=arr[0]+k-arr[-1]
# maxDist=max(arr)-min(arr)
for i in range(n-1):
    maxDist=maxDist if arr[i+1]-arr[i] <=maxDist else arr[i+1]-arr[i]
print(k-maxDist)

Submission status: Accepted

Solution code:
k,n=[int(i) for i in input().strip().split(" ")]
arr=[int(i) for i in input().strip().split(" ")]
maxDist=arr[0]+min(k-arr[-1],arr[-1]) 
# maxDist=max(arr)-min(arr)
for i in range(n-1):
    maxDist=maxDist if arr[i+1]-arr[i] <=maxDist else arr[i+1]-arr[i]
print(k-maxDist)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import exit
from math import gcd, factorial, ceil, floor, sqrt
from bisect import bisect_left, bisect_right
from copy import deepcopy
from heapq import heapify, heappop, heappush
from itertools import permutations, combinations, product, accumulate
from collections import defaultdict, deque
sys.setrecursionlimit(10**7)

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

K, N = mi()
A = li()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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


print(K - max(path))

Submission status: Accepted

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

    max_distance = 0

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

    answer = k - max_distance
    print(answer)


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(N-1):
  M = max(M, A[i+1] - A[i])
M = max(M, A[0] + K - A[-1])
print(K - M)

Submission status: Accepted

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

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

print(K-max(sp))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(K-dmax)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    k, n = map(int, input().split())
    A = [int(x) for x in input().split()]
    # AnからA0までを取り除いたものを初期値にする
    minv = A[n-1] - A[0]
    #️ A0からA1の距離...An-1からAnの距離をkから引いた値を比較してちいさいものが答え
    for i in range(n-1):
        minv = min(minv, k - (A[i+1] - A[i]))
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
S=[0 for _ in range(k)]

for i in range(1,k+1):
    if i!=k:
        S[i-1]=A[i]-A[i-1]
    else:
        S[i-1]=A[0]+n-A[k-1]

print(n-max(S))

Submission status: Accepted

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

Submission status: Accepted

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

print(k - max_len)

Submission status: Accepted

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

A_list = list(map(int, input().split()))
A_gap_max = 0
ans = 0

for i in range(N-1):
    A_gap_temp = A_list[i+1] - A_list[i]
    if A_gap_temp > A_gap_max:
        A_gap_max = A_gap_temp
    ans += A_gap_temp 
A_gap_temp = A_list[0] + K - A_list[N-1]
if A_gap_temp > A_gap_max:
    A_gap_max = A_gap_temp
ans += A_gap_temp 
    
ans -= A_gap_max
print(ans)

Submission status: Accepted

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

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

c = abs(K - A[N-1] + A[0])

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

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    dists = set([])
    for idx in range(len(A) - 1):
        dists.add(A[idx + 1] - A[idx])
    dists.add(A[0] + K - A[-1])
    print(K - max(list(dists)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
A += [A[0] + k]
far = max(y - x for x, y in zip(A, A[1:]))
print(k - far)

Submission status: Accepted

Solution code:
import sys


def main():
    read = sys.stdin.buffer.read

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def main():
    read = sys.stdin.buffer.read
    k, n, *A = map(int, read().split())
    A += [k + A[0]]
    far = max(x - y for x, y in zip(A[1:], A))
    print(k - far)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def main():
    read = sys.stdin.buffer.read
    k, n, *A = map(int, read().split())
    far = k + A[0] - A[-1]
    y = A[0]
    for x in A[1:]:
        dis = x - y
        if far < dis:
            far = dis
        y = x
    print(k - far)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
y = A[0]
for x in A[1:]:
    dis = x - y
    if far < dis:
        far = dis
    y = x
print(k - far)

Submission status: Accepted

Solution code:
import sys
from array import array
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
A += [k + A[0]]
far = max(array("l", [x - y for x, y in zip(A[1:], A)]))
print(k-far)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
y = A[0]
for x in A:
    if far < x - y:
        far = x - y
    y = x
print(k-far)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
for x, y in zip(A[1:], A):
    if far < x - y:
        far = x - y
print(k-far)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k - max(l))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
k, n, *A = read().split()
far = int(k) + int(A[0]) - int(A[-1])
for x, y in zip(A[1:], A):
    if far < int(x) - int(y):
        far = int(x) - int(y)
print(int(k)-far)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
k, n = readline().rsplit()
A = readline().rsplit()
far = int(k) + int(A[0]) - int(A[-1])
for x, y in zip(A[1:], A):
    if far < int(x) - int(y):
        far = int(x) - int(y)
print(int(k)-far)

Submission status: Accepted

Solution code:
k, n, *A = map(int, open(0).read().split())
far = k + A[0] - A[-1]
for x, y in zip(A[1:], A):
    if far < x - y:
        far = x - y
print(k-far)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print (K - max(B))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:

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

if __name__=='__main__':
  main()

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

import numpy as np

def main():
	k,n=MI()
	A=LI()
	A=np.array(A)
	A=np.append(A,A+k)

	B=A[n-1:]-A[:n+1]
	print(min(B))
















if __name__ == "__main__":
	main()

Submission status: Accepted

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

print(K-max(a))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

dis.sort
print(sum(dis) - max(dis))

Submission status: Accepted

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

max_range = 0

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

ans = K - max_range
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
k,n = readInts()
A = sorted(readInts())
A.append(A[0])
long = 0
for i in range(n):
    if i != n-1:
        long = max(long,abs(A[i+1] - A[i]))
    else:
        long = max(long, abs((k-A[i]) + A[i+1]))
print(k - long)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
k,n = readInts()
A = readInts()
max_dist = 0
for i in range(n):
    next = (i+1)%n
    dist = A[next] - A[i]
    if dist < 0:
        dist += k
    max_dist = max(max_dist, 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])
ans = 0
for i in range(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()))
D = []
for i in range(n-1):
    D.append(A[i+1]-A[i])
else:
    D.append(k-(A[n-1]-A[0]))
D.sort(reverse=True)
print(k-D[0])

Submission status: Accepted

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

Submission status: Accepted

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

result = []

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

result.sort()

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

Submission status: Accepted

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