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 = [int(x) for x in input().split(' ')]
A = [int(x) for x in input().split(' ')]

def pairwise_distance(a, b, K):
  return b - a 
  
memo = {frozenset({i, i+1}): pairwise_distance(A[i], A[i+1], K) 
        for i in range(0, len(A) - 1)}

last_pair_distance = K - pairwise_distance(A[0], A[len(A) - 1], K)
if last_pair_distance > K / 2:
  memo[frozenset({0, len(A) - 1})] = K - last_pair_distance
else:
  memo[frozenset({0, len(A) - 1})] = last_pair_distance

min_dist = K - max(memo.values())
print(min_dist)

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

Submission status: Accepted

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

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

print(k - max(l))

Submission status: Accepted

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

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

for i in range(1,N):									#range(始まり、項数)
	dis = max(dis,A[i]-A[i-1])

dis = K - dis
print(dis)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
num_list = list(map(int, input().split())) 
num_list.sort()
num_list_sort=num_list
maxa_b=0
for i in range(len(num_list_sort)):
  a=num_list_sort[i]
  if(i==len(num_list_sort)-1):
    b=num_list_sort[0]
  else:
    b=num_list_sort[i+1]

  a_b=min(max(a,b)-min(a,b),min(a,b)+K-max(a,b))
  if(maxa_b<=a_b):
    maxa_b=a_b
print(K-maxa_b)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
array = list(map(int,input().split()))
max_length = 0
sum_length = 0
for i in range(n - 1):
  length = array[i + 1] - array[i]
  sum_length += length
  if length > max_length:
    max_length = length
last_length = k - array[n - 1] + array[0]
sum_length += last_length
max_length = max(max_length, last_length)
print(sum_length - max_length)

Submission status: Accepted

Solution code:
X = input().split()
K = int(X[0])
N = int(X[1])
Y = input().split()
Z = []
for i in range(N):
    Z.append(int(Y[i]))
result = Z[0]+ K - Z[N-1]

for i in range(N):
    if result < Z[i] - Z[i-1]:
        result = Z[i] - Z[i -1]
    i = i + 1
print(K - result)

Submission status: Accepted

Solution code:
import numpy as np


# input the data
k, n = (int(x) for x in input().split())
a = [int(i) for i in input().split()]

# culculate each distance between the houses
distance = []
for i in range(n-1):
    distance.append(a[i+1] - a[i])
# exeption (distance between 1st house and n_th house)
distance.append(k - a[n-1] + a[0])

# compare max(distance) and spesial distance
dis_max = np.max(distance)

# answer
ans = k - dis_max
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

BBB = []

for i in range(N-1):
    BBB.append(AAA[i+1]-AAA[i])
BBB.append(K-AAA[-1]+AAA[0])
#print(BBB)
print(K-max(BBB))

Submission status: Accepted

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

print(K - max(m, K - (lst[-1] - a)))

Submission status: Accepted

Solution code:
i = list(map(int, input().split()))
j = list(map(int, input().split()))
max = 0


for k in range(i[1]-1):
  buf1 = j[k+1]
  buf2 = j[k]
  if max < buf1 - buf2:
    max =  buf1 - buf2
if max < i[0] + j[0] - j[i[1]-1]:
  max =  i[0] + j[0] - j[i[1]-1]
max = i[0] - max
print(max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
a.reverse()
for i in range(n-1):
    a[i]-=a[i+1]
a[n-1] = 0
a.append(k-sum(a))
ans = k-max(a)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
a.append(k+a[0])
a.reverse()
for i in range(n):
    a[i]-=a[i+1]
ans = k-max(a)
print(ans)

Submission status: Accepted

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

a.append(k+a[0])
# print(a)
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()))
ans = 0
a.reverse()
for i in range(n-1):
    a[i]-=a[i+1]
a[n-1] = 0
a.append(k-sum(a))
ans = k-max(a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os

def main():
	k,n = map(int, input().split())
	a_list = list(map(int, input().split()))
	min_total = 1000000
	for i in range(n):
		if i == 0:
			a_total = a_list[i-1] - a_list[i]
			b_total = a_list[i] + k - a_list[i+1]	
		else:
			if i == n-1:
				a_total = k - a_list[i] + a_list[i-1]
				b_total = a_list[i] - a_list[0]
			else:
				a_total = k - a_list[i] + a_list[i-1]
				b_total = a_list[i] + k - a_list[i+1]

		if (a_total < min_total) or (b_total < min_total):
			if a_total <= b_total:
				min_total = a_total
			else:
				min_total = b_total
	print(min_total)

if __name__ == "__main__":
	main()

Submission status: Accepted

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

dist = []
for i in range(N - 1):
    dist.append(An[i + 1] - An[i])

result = 0
matagi = K - An[N - 1] + An[0]
if max(dist) >= matagi:
    result = sum(dist) + matagi - max(dist)
else:
    result = sum(dist)

print(result)

Submission status: Accepted

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

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
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	K, N = getlist()
	A = getlist()
	dif = 0
	for i in range(N - 1):
		dif = max(dif, A[i + 1] - A[i])

	dif = max(dif, A[0] + K - A[-1])

	print(K - dif)


if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Traveling Salesman around Lake
# https://atcoder.jp/contests/abc160/tasks/abc160_c

# 各家において、時計回りか反時計回りのどちらで移動した方が良いかチェックする。

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

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

Submission status: Accepted

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

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

ans = k - x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

max_space = 0

for i in range(n):
    if i == 0:
        max_space = k - aa[n - 1] + aa[0]
    else:
        max_space = max(max_space, aa[i] - aa[i - 1])
print(k - max_space)

Submission status: Accepted

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

A.append(K+A[0])

max_range = 0

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

print(K - max_range)

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc160/tasks/abc160_c"

def main():
    lake, house = list(map(int, input().split()))
    dis = list(map(int, input().split()))
    ans = []
    for i in range(1, len(dis)):
        ans.append(dis[i] - dis[i - 1])
    ans.append(lake - dis[-1] + dis[0])
    print(sum(ans) - max(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
a = np.array([int(i) for i in input().split()])
#a.insert(0, 0)
#a.append(k)
#a = np.array(a)

d = np.diff(a)
diff_k = k - a[-1] + a[0]

if diff_k >= max(d):
    s = sum(d)
else:
    s = sum(d) - max(d) + diff_k

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#12
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(int(k-a[n-1]+a[0]))

c = max(b)
b.remove(c)

print(sum(b))

Submission status: Accepted

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

for i in range(k-1):
  s=l[i+1]-l[i]
  if s>max:
    max=s

s=n-l[-1]+l[0]
if s>max:
  max=s
print(n-max)

Submission status: Accepted

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

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

print(k-longest)

Submission status: Accepted

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

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

a.append(k+a[0])

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())) 
a += [a[0]+k]
b = []
for i in range(1,n+1):
    b.append(a[i]-a[i-1])
print(sum(b)-max(b))

Submission status: Accepted

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

su_list =[k-a_list[-1]+a_list[0]]

for i in range(n-1):
    su_list.append(a_list[i+1]-a_list[i])

print(k-max(su_list))

Submission status: Accepted

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

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

print(k-max(distance))

Submission status: Accepted

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

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)]
if 0 in A:
    diff.append(K- A[-1])
else:
    diff.append(A[0] + K - A[-1])


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

Submission status: Accepted

Solution code:
k, _ = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
b = [ai + k - a[len(a) - 1] if i == 0 else ai - a[i - 1]  for i, ai in enumerate(a)]
print(k - max(b))

Submission status: Accepted

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

A_list.append(a[0] + (k - a[n-1]))
print(k - max(A_list))

Submission status: Accepted

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

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

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!=0:
        d.append(abs(a[i]-a[i-1]))
    else:
        d.append(abs(a[i]-a[i-1]+k))
d.sort()

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

Submission status: Accepted

Solution code:
k_n = list(map(int,input().split()))
a = list(map(int,input().split()))
dis = []
total_dis = 0
max_dis = 0
for i in range(k_n[1]):
  if(i < (k_n[1]-1)):
    dis.append(a[i+1] - a[i])
  else:
    dis.append(k_n[0] - a[i] + a[0])
  
  if(max_dis <= dis[i]):
    max_dis = dis[i]
    
  total_dis+= dis[i]

total_dis -= max_dis
print(total_dis)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,N = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
max = 0
for i in range(N-1):
  if (a[i+1]-a[i]) >= max:
    max = a[i+1]-a[i]
if max <  k -(a[N-1]-a[0]):
  max = k -(a[N-1]-a[0])
min = k-max
print(min)

Submission status: Accepted

Solution code:
k,n=[int(j) for j in input().split()]
a=[int(j) for j in input().split()]
l=[]
for i,j in zip(a,a[1:]):
    l.append(j-i)
l.append(k-a[-1]+a[0])
print(k-max(l))

Submission status: Accepted

Solution code:
perimeter,n = map(int, input().split())
v =list(map(int, input().split()))
diff = []
v.reverse()
for i in range(n-1):
    diff.append(v[i]-v[i+1])
diff.append(perimeter-(v[0]- v[n-1]))
print(perimeter - max(diff))

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

Submission status: Accepted

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

Submission status: Accepted

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

K = int(K);
N = int(N);

A = [int(a) for a in A];

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

#修正
B [0] = (K - A[len(A) -1]) + A[0];

max_B = max(B);
ans = K - max(B);

print(ans);

Submission status: Accepted

Solution code:
l = input().split()
L = [int(c) for c in l]
K = L[0]
N = L[1]
A = [int(b) for b in input().split()]

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

Submission status: Accepted

Solution code:
import numpy as np

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

diffs = np.diff(np.array(a_list))
diff_max = max(diffs.max(), a_list[0] + (k - a_list[-1]))

print(k - diff_max)

Submission status: Accepted

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

max_value = -1
ans = 0
for i in range(1, n+1):
    tmp_d = a[i] - a[i-1]
    ans += tmp_d
    max_value = max(max_value, tmp_d)
ans -= max_value
print(ans)

Submission status: Accepted

Solution code:
K , N = map(int,input().split())
arr = list(map(int,input().split()))
add = 0
maxDist = -1
arr.sort()
dp = [0 for  _ in range(N) ]
for i in range(N-1):
	dp[i+1] = arr[i+1]-arr[i]
dp[0] = (K-arr[N-1])+arr[0]
for i in range(N):
	add += dp[i]
	maxDist = max(maxDist,dp[i])
print(add-maxDist)

Submission status: Accepted

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

A_n.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

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):
    if i==n-1:
        dist[i]=k+a[0]-a[i]
    else:
        dist[i]=a[i+1]-a[i]
print(k-sorted(dist,reverse=True)[0])

Submission status: Accepted

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

dist = lst[-1]-lst[0]
for i in range(N-1):
    dist = min(dist, K-lst[i+1]+lst[i])
print(dist)

Submission status: Accepted

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

max_dist = 0
for i in range(N-1):
    if max_dist < lst[i+1]-lst[i]:
      max_dist = lst[i+1]-lst[i]

if max_dist < lst[0] + (K-lst[-1]):
      max_dist = lst[0] + (K-lst[-1])

print(K - max_dist)

Submission status: Accepted

Solution code:
def main():
    k, n = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    a_ = [k - a[-1] + a[0]] + [a[i] - a[i - 1] for i in range(1, n)]

    print(k - max(a_))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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


maxdis=0
for i in range(n):
    one=a[i]
    if i==n-1:
        two=a[0]
        maxdis=max(maxdis,(two+k-one))
    else:
        two=a[i+1]
        maxdis=max(maxdis,(two-one))        
print(k-maxdis)

Submission status: Accepted

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

import sys
import math

sys.setrecursionlimit(10 ** 6)

input_line = input()
K, N = map(int, input_line.split(" "))

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

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

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

print(ans)

Submission status: Accepted

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

cost = 0
maxcost = 0

# print(A)

for i in range(len(A)):
    if i == 0:
        cost += A[i]
        # print(cost)
        continue
    elif i == len(A)-1:
        tm = A[i] - A[i - 1]
        maxcost = max(maxcost, tm)
        cost += tm

        tm = KN[0] - A[i]
        maxcost = max(maxcost,tm+A[0])
        # print(maxcost)
        cost += tm

    else:
        tm = A[i] - A[i-1]
        maxcost = max(maxcost, tm)
        cost += tm
    # print(cost)

print(cost-maxcost)

Submission status: Accepted

Solution code:
# K : 湖の円周 n : 家の軒数　
k, n = map(int, input().split())
# 家の位置をリストで取得
house_point = list(map(int,input().split()))

# それぞれの家の位置を差分を取得
house_point_diff = [house_point[i+1] - house_point[i] for i in range(n-1)]
# 最初の家と最後の家に位置の差分を、Kを考慮し取得
house_point_diff.append(k + house_point[0] - house_point[n-1])

# 最も離れた家の間を通らないようにすれば最短距離を得られる。
# そのため、湖の円周(K)から、最も離れた家の間の距離(max(house_point_diff))を引く
print(k-max(house_point_diff))

Submission status: Accepted

Solution code:
# K : 湖の円周 n : 家の軒数　
k, n = map(int, input().split())
# 家の位置をリストで取得
house_point = list(map(int,input().split()))

# それぞれの家の位置を差分を取得
house_point_diff = [house_point[i+1] - house_point[i] for i in range(n-1)]
# 最初の家と最後の家に位置の差分を、Kを考慮し取得
house_point_diff.append(k + house_point[0] +  - house_point[n-1])

# 最も離れた家の間を通らないようにすれば最短距離を得られる。
# そのため、湖の円周(K)から、最も離れた家の間の距離(max(house_point_diff))を引く
print(k-max(house_point_diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

print(K-maximum)

Submission status: Accepted

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

d = []
for i in range(N-1):
    if A[i+1] - A[i] > K/2:
        d.append(K-A[i+1] + A[i])
    else:
        d.append(A[i+1] - A[i])

if K - A[N-1] + A[0] > K/2:
    d.append(A[N-1] - A[0])
else:
    d.append(K - A[N-1] + A[0])
print(sum(d)-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

li=[(k-a[n-1])+a[0]]

[li.append(-(a[x]-a[x+1])) for x in range(n-1)]


print(k-max(li))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

x = [abs(A[n] - A[n+1]) for n in range(N-1)] + [K - A[-1] + A[0]]

print(K - max(x))

Submission status: Accepted

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

dmax = K - A[N-1] + A[0]
apre = -(K - A[N-1])
for a in A:
  d = a - apre
  if d > dmax:
    dmax = d
  apre = a

print(K - dmax)

Submission status: Accepted

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

Submission status: Accepted

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

houses = [int(value) for value in input().split()]


distance = [0] * n

for i in range(n):
    distance[i] = houses[int((i+1)%n)] - houses[i]
    if distance[i] < 0:
        distance[i] = min(abs(distance[i]), distance[i]+k)

max_distance = max(distance)

sum_distance = 0
for dis in distance:
    sum_distance += dis

sum_distance -= max_distance

print(sum_distance)

Submission status: Accepted

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

Submission status: Accepted

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


for i in range(N):
  l.append((A[(i+N-1)%N]-A[i]+K)%K)
#
print(min(l))

Submission status: Accepted

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

Submission status: Accepted

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

A.insert(0, 0)
A.append(K)

# print(A)

start = A[1] - A[0]
goal = A[-1] - A[-2]

mixi = 0
sum_len = 0

# SorG = max(start, goal)

SorG = start + goal

for i in range(len(A) - 1):
    if A[i+1] - A[i] > mixi:
        mixi = A[i+1] - A[i]
    sum_len += A[i+1] - A[i]

if max(SorG, mixi) == SorG or SorG == mixi:
    result = sum_len - (start + goal)
elif max(SorG, mixi) == mixi:
    result = sum_len - mixi

print(result)

Submission status: Accepted

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

for i in range(N):
    min = 0
    if i == N-1:
        min = abs(array[0] - array[N-1])
        if min>=K/2:
            min = abs(K-min)
        array_distance.append(min)
    else:
        min = abs(array[i] - array[i+1])

        array_distance.append(min)

array_distance= sorted(array_distance, reverse = True)
# print(array_distance)
# print(array_distance[0])
# print(sum(array_distance))
print(sum(array_distance) - array_distance[0])

Submission status: Accepted

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

gap=[ ]
for i in range(N-1):
    gap.append(L[i+1]-L[i])

gap.append(K-L[N-1]+L[0])
print(K-max(gap))

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(N):
    min = 0
    if i == N-1:
        min = abs(array[0] - array[N-1])
        if min>=K/2:
            min = abs(K-min)
        array_distance.append(min)
    else:
        min = abs(array[i] - array[i+1])
        if min>=K/2:
            min = abs(K-min)
        array_distance.append(min)

array_distance= sorted(array_distance, reverse = True)
# print(array_distance)
# print(array_distance[0])
# print(sum(array_distance))
print(sum(array_distance) - array_distance[0])

Submission status: Accepted

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

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

print(k-max(an_minus))

Submission status: Accepted

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(l[0])
n = int(l[1])
x = 0
m = k-int(j[-1])+int(j[0])
for i in range(n-1):
  d = int(j[i+1]) - int(j[i])
  x = max(x,d)
x = max(x,m)
dt = k - int(x)
print(dt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
KN = input().split(' ')
K = int(KN[0])
N = int(KN[1])
M = K/2
Al = input().split(' ')
A = []

for home in Al:
  A.append(int(home))

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

print(K-m)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    k,n=map(int,input().split())
    A=[int(_) for _ in input().split()]
    max_len=(k-A[-1])+A[0]
    for i in range(n-1):
        max_len=max(max_len,A[i+1]-A[i])
    print(k-max_len)

if __name__=='__main__':
    main()

Submission status: Accepted

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

max_n=0

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

range_n=k-(a[n-1]-a[0])
if range_n > max_n:
  max_n=range_n
print(k-max_n)

Submission status: Accepted

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

for i in range(N-1):
    langth = List[i+1] - List[i]
    if langth > max:
        max = langth

langth = 0

langth = K - (List[N - 1] - List[0])
if langth > max:
    max = langth

print(K-max)

Submission status: Accepted

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

max = 0

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

    if t > max:
        max = t

print(k-max)