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:
# -*- coding: utf-8 -*-
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
#一番遠いところを回らない
b = a+[a[0]+k]
longest_distance = 0
for bi,ai in zip(b[1::],a):
    longest_distance=max(longest_distance,bi-ai)
print(k-longest_distance)

Submission status: Accepted

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

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

answer = K - max_length
print(answer)

Submission status: Accepted

Solution code:
kp = list(map(int, input().rsplit()))
A = list(map(int, input().rsplit()))
max_range = kp[0]-A[-1]+A[0]
for idx in range(len(A)-1):
  dif = A[idx+1]-A[idx] 
  if dif >max_range:
    max_range = dif
 
print(kp[0] - max_range)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
li_a = list(map(int, input().split()))
li_d = list()
for idx, a in enumerate(li_a):
    if idx == (n - 1):
        li_d.append(abs(k - li_a[idx] + li_a[0]))
    else:
        li_d.append(abs(li_a[idx] - li_a[idx + 1]))
print(sum(li_d) - max(li_d))

Submission status: Accepted

Solution code:
K, N = tuple(int(v) for v in input().split())

A_list = list(int(a) for a in input().split())

# # 各点の距離
# x_iter = iter(A_list)
# y_iter = iter(A_list)
# next(y_iter)
# 
# d_list = [y - x for x, y in zip(x_iter, y_iter)]
# d_list.append(K - A_list[-1] + A_list[0])
# d_list += d_list
# 
# l = len(d_list) // 2
# # sum_d = [sum(d_list[i:i+l-1]) for i in range(l)]
# min_d = min(sum(d_list[i:i+l-1]) for i in range(l))
# 
# print(min_d)

A_list += [a + K for a in A_list[:-1]]
print(min(A_list[i+N-1] - A_list[i] for i in range(N)))

Submission status: Accepted

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

# %%
ma = 0
for i in range(N-1):
    ma = max(ma, A[i+1]- A[i])

# %%
print(min(K-ma,A[N-1]-A[0]))

# %%

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().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:
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(k-max)

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

Submission status: Accepted

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

max_val = 0

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

    if lg > max_val:
        max_val = lg
print(k - max_val)

Submission status: Accepted

Solution code:
circum_total = [int(n) for n in input().split()]

circum = circum_total[0]
total = circum_total[1]

distances = [int(n) for n in input().split()]

mindist = circum
for index in range(len(distances)):
    if index == len(distances) - 1:
        rightdist = distances[0] + circum - distances[index]
    else:
        rightdist = distances[index+1] - distances[index]
    if index == 0:
        leftdist = distances[index] - distances[index-1] + circum
    else:
        leftdist = distances[index] - distances[index-1]
    maxdist = max(rightdist,leftdist)
    mindist = min(mindist, circum - maxdist)

print(mindist)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N = list(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[N - 1])
print(K - m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = input().split()
k, n = int(k), int(n)
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 (k - a[n - 1]) + a[0] >= max:
  max = (k - a[n - 1]) + a[0]

print(k - max)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(K - m)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import *
K, N = map(int, input().split())
A, B = tee(map(int, input().split()), 2)

first = next(B)
M = max(b-a for a, b in zip_longest(A, B, fillvalue=first+K))

print(K-M)

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=[int(e) for e in input().split()]
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])
dis.sort()
ans=0
for i in range(N-1):
    ans+=dis[i]
print(ans)

Submission status: Accepted

Solution code:
input1=list(map(int,input().split()))
input2=list(map(int, input().split()))

distance=[]
for order,value in enumerate(input2):
    if order==0:
        pass
    else:
        distance.append(value-input2[order-1])
distance.append(input1[0]-sum(distance))

print(input1[0]-max(distance))

Submission status: Accepted

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

Submission status: Accepted

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

result = a[0] + (k - a[-1])

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

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

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

print(K-diff)

Submission status: Accepted

Solution code:
def solve(string):
    k, n, *a = map(int, string.split())
    b = [a[-1]] + [_a + k for _a in a]
    return str(min(a1 - a0 for a0, a1 in zip(a, b)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N =(int(x) for x in input().split())
array = list(map(int, input().strip().split()))
between = [0] * N
for i in range(N-1):
    between[i] = array[i+1] -array[i]
between[N-1] = K- array[N-1]  + array[0]

print(K-max(between))

Submission status: Accepted

Solution code:
dist_lake, num_home = map(int, input().split())
dist_start_to_home = tuple(map(int, input().split()))

max_length = 0

for index, dist in enumerate(dist_start_to_home):
  	
    if index == len(dist_start_to_home) - 1:
      	length = dist_lake - (dist_start_to_home[index] - dist_start_to_home[0])
    else:
      	length = dist_start_to_home[index + 1] - dist_start_to_home[index]
    if max_length < length:
    	max_length = length
print('{}'.format(dist_lake - max_length))

Submission status: Accepted

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

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

print(k - l)

Submission status: Accepted

Solution code:
def C():
    kn=list(map(int,input().split()))
    A=list(map(int,input().split()))
    d=[]
    sum_ans=[]
   
    for i in range(kn[1]):
        
        if i==kn[1]-1:
            d.append(kn[0]-A[i])
        else:
            d.append(A[i+1]-A[i])
        
        
    sum_ans.append(A[kn[1]-1]-A[0])
    for i in range(1,kn[1]):
        sum_ans.append(min(kn[0]-d[i],kn[0]-d[i-1]))
    
    print(min(sum_ans))    
        
    
if __name__=='__main__':
    C()

Submission status: Accepted

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

    if len > max:
        max = len

print(K - max)

Submission status: Accepted

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

Submission status: Accepted

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

l = k+a[0]-a[n-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())
v=list(map(int,input().split()))
a=0
for i in range(1,n):
   a=max(a,v[i]-v[i-1])
print(k-max(a,k-v[n-1]+v[0]))

Submission status: Accepted

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

print(k - max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = [int(i) for i in input().split()]
a = [int(i) for i in 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, *A = map(int, open(0).read().split())

A.append(K + A[0])

B = [b - a for a, b in zip(A, A[1:])]
print(sum(B) - max(B))

Submission status: Accepted

Solution code:
import numpy as np

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

a_np = np.append(a, a[0] + k)
print(k - max(np.diff(a_np)))

Submission status: Accepted

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

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())
a = list(map(int, input().split()))

b = [a[i+1]-a[i] for i in range(n-1)]
c = b.append(int(a[0]+k-a[-1]))
d = max(b)

print(int(k-d))

Submission status: Accepted

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

Submission status: Accepted

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

dist = 0

for i in range(len(arr)):
    if i is 0:
        if k - arr[i-1] + arr[i] > dist:
            dist = k - arr[i-1] + arr[i]
    
    if arr[i] - arr[i-1] > dist:
        dist = arr[i] - arr[i-1]

print(k - dist)

Submission status: Accepted

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

distance = []

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

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

index = distance.index(max(distance))

print(K - distance[index])

Submission status: Accepted

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

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

print(K - max_distance)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

total = 0
srtd = sorted(s)
for i in range(len(srtd) - 1):
  total += srtd[i]

print(total)

Submission status: Accepted

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

L = [0] + L + [K]

# print(L)

max_distance = 0

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

max_distance = max(max_distance,(L[-1]-L[-2])+(L[1]-L[0]))

print(K - max_distance)

Submission status: Accepted

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

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

print(k-wk)

Submission status: Accepted

Solution code:
import numpy as np
import statistics

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

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


b = np.array(a)


b = b%k
b.sort()



dis = 0

for i in range(1,n):
    dis = max(dis,abs(b[i]- b[i-1]))

dis = max(dis,(b[0]+k-b[n-1]))


print(k - dis)

Submission status: Accepted

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

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

print(K-max(d))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 20:19:25 2020

@author: black
"""


K, N = map(int, input().split())
li = list(map(int, input().split()))
li.append(K + li[0])
sub = [li[i+1]-li[i] for i in range(N)]
MAX = max(sub)
total = sum(sub)
print(total - MAX)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    print('{}'.format(K - max_dt))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.append(k+A[0])
m = max(y-x for x,y in zip(A,A[1:]))
ans = k-m
print(ans)

Submission status: Accepted

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

print(K-max(dis))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
A=sorted(list(map(int,input().split())))
A+=[A[0]+k]
B=max(y-x for x,y in zip(A,A[1:]))
print(k-B)

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

k, n = map(int, input().split())
a = readints()
# 1→N特殊
tokusyu = (k-(k-a[n-1]+a[0]))
# 2→1（2-1)
# 3→2（3-2）

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

Submission status: Accepted

Solution code:
n = [int(e) for e in input().split(" ")]
a = [int(e) for e in input().split(" ")]
a.append(n[0] + a[0])
m = 0
for i in range(len(a) - 1):
    d = a[i + 1] - a[i]
    if(m < d):
        m = d
else:
    print(n[0] - m)

Submission status: Accepted

Solution code:
import numpy as np

N, K = map(int, input().split())
# print(K, N)

locations = input().split()
dists = []
prev_location = None

for location in locations:
  if prev_location is None:
    prev_location = int(location)
  else:
	  dists.append(int(location) - prev_location)  
	  prev_location = int(location)
dists.append(N - int(locations[-1]) + int(locations[0]))

# print(dists)
print(N - np.array(dists).max())

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[i] = a[i]
  elif i == n-1:
    d[i] = a[i] - a[i-1]
    d[0] += k - a[i]
  else:
    d[i] = a[i] - a[i-1]
#print(d)
d2 = sorted(d.items(),key=lambda x:x[1], reverse=True)
print(k - d2[0][1])

Submission status: Accepted

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

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

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

print(k - r)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(min(dist))

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]+(k-arr[(i+1)%n])%k
  ans=min(ans,dist1,dist2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

maxL = (A[0]+K)-A[-1]
maxID =0

for x in range(1,N):
    dis = A[x]-A[x-1]
    if maxL <dis:
        maxL = dis
        maxID =x
print(K-maxL)

Submission status: Accepted

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

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

print(k - max(DISTANCE))

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.append(k+a_list[0])
diff = list(np.diff(a_list))
diff.remove(max(diff))
print(sum(diff))

Submission status: Accepted

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

c=sorted(b,reverse=True)
for i in range(1,n):
  s+=c[i]
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N = map(int, input().split())
A = list(map(int, input().split()))
# K, N = 20, 3
# A = [5, 10, 15]
l = []

l.append(K - A[-1] + A[0])
for i in range(1, N):
  l.append(A[i] - A[i - 1])
l.pop(l.index(max(l)))
print(sum(l))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(seg) - max(seg))

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])
ans = k - l
print(ans)

Submission status: Accepted

Solution code:
#abc160c
k,n=map(int,raw_input().split())
a=map(int,raw_input().split())
temp=0
for i in range(1,n):
 temp=max(a[i]-a[i-1],temp)
temp=max(k-a[n-1]+a[0],temp)
print k-temp

Submission status: Accepted

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


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

print(min(B))

Submission status: Accepted

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

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

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

print(K-s)

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

Submission status: Accepted

Solution code:
import numpy as np

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

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


A=np.array(A)

lin=np.array(A[:-1])
le=np.array(A[1:])

length=abs(lin-le)

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

A=np.append(length,rem)

M=np.max(A)

print(K-M)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K + A[0])
    B = np.array(A[1:])
    A = np.array(A[:-1])
    Diff = B - A
    max_diff = max(Diff)
    answer = K - max_diff
    return answer


if __name__ == "__main__":
    answer = main()
    print(answer)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K + A[0])
    Diff = [y - x for x, y in zip(A, A[1:])]
    answer = K - max(Diff)
    return answer


answer = main()
print(answer)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K + A[0])
    Diff = [y - x for x, y in zip(A, A[1:])]
    answer = K - max(Diff)
    return answer


if __name__ == "__main__":
    answer = main()
    print(answer)

Submission status: Accepted

Solution code:
import numpy as np
from collections import deque
k,n=map(int,input().split())
lst=np.fromstring(input(),int,sep=' ')
dis=[]
for i in range(1,n):
    dis.append(lst[i]-lst[i-1])
print(k-max(k-lst[-1]+lst[0],max(dis)))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

Submission status: Accepted

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

a0 = a[-1]
ans = [a[0] - (a0 -k)]
a0 = a[0]
for i in a[1:]:
    ans.append(i - a0)
    a0 = i

print(k - max(ans))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

# 入力はstringで入る．
# intにしたければ int(input())
# floatで受け取ると 1.0 になる．

# 入力 : K N
# 入力 : A1 A2 ... AN
K,N = (int(x) for x in input().split())
A = list(map(int, input().split()))

# AN と A1 の距離
MaxLen = K-A[-1]+A[0]

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

print(K-MaxLen)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(k-max(d))