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=list(map(int,input().split()))
d0=0
dmax=0
for i in range(n):
    d=a[i]-d0
    d0=a[i]
    if d>=dmax:
        dmax=d
d1=k-a[n-1]+a[0]
if d1>=dmax:
    print(str(k-d1))
else:
    print(str(k-dmax))

Submission status: Accepted

Solution code:
#K, N = map(int, input().split())
#As = list(map(int, input().split()))
#dmax = 0
#for i in range(N-1):
#    dmax = max(dmax, As[i+1]-As[i])
#dmax = max(dmax, As[0] + K - As[N-1])
#print(K - dmax)
K, N = map(int, input().split())
A = [int(x) for x in 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:
# coding: utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
max = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-max_dis)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
al=list(map(int,input().split()))
d=[a_-a for a,a_ in zip(al, al[1:])]+[k-al[-1]+al[0]]
print(sum(sorted(d)[:-1]))

Submission status: Accepted

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

Submission status: Accepted

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

max_distance = K + (A[0] - A[-1])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


############ ---- Input Functions ---- ############
def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


def in_ints():
    return (map(int, input().split()))


k, n  = in_ints()
a = in_list()

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

    mx = max(mx, abs(a[i]- next))
    ans  += abs(a[i]- next)


print(ans - mx)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
r = list(map(int, input().split()))
ans = 0
num_m = 0

for i in range(1, len(r)):
    k = r[i] - r[i-1]
    ans += k
    if k > num_m:
        num_m = k
k = r[0] + a - r[-1]
ans += k
if k > num_m:
    num_m = k

print(ans - num_m)

Submission status: Accepted

Solution code:
input_KN = list(map(int, input().split()))
K = input_KN[0] 
N = input_KN[1] 
ienokyori= list(map(int, input().split()))
ienokyori.append(K)
if(ienokyori[0]==0):
    max_ienokyori = 0 
else:
    max_ienokyori = ienokyori[0]+abs(ienokyori[-1]-ienokyori[-2])
for i in range(N):
    if(max_ienokyori < abs(ienokyori[i]-ienokyori[i+1])):
        max_ienokyori = abs(ienokyori[i]-ienokyori[i+1])

print(K-max_ienokyori)

Submission status: Accepted

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

# 最長の長さがどこにあるか判定して上げる
# 0 5 15
# 0-1 : data[1]     - data[0] :       5 -  0 =  5
# 1-2 : data[2]     - data[1] :      15 -  5 = 10
# 2-0 : data[0] + k - data[2] :  0 + 20 - 15 =  5

diffs = []
for i in range(n):
  if i+1 <= n-1:
    diffs.append(data[i+1] - data[i])
  
  if i == n-1:
    diffs.append(data[0] + k - data[i])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# innefficient but lowest to AC sol.
n, k = map(int, input().split())
houses = [int(x) for x in input().split()]
houses.sort()

diffList = []

for index, pos in enumerate(houses):
    diffList.append( (pos - houses[index-1]) % n)

m = max(diffList)

print(n - max(diffList))

Submission status: Accepted

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

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

print(k - ans)

Submission status: Accepted

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

Submission status: Accepted

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

A = []
for i in input().split():
  A.append(int(i))

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

print(K-mergin[-1])

Submission status: Accepted

Solution code:
date0=input().rstrip().split(" ")
date1=input().rstrip().split(" ")
list0=[]
list1=[]
for i in range(int(date0[1])):
  list0.append(int(date1[i]))
list0=sorted(list0)
for j in range(int(date0[1])-1):
  list1.append(list0[j+1]-list0[j])
list1.append(int(date0[0])-list0[int(date0[1])-1]+list0[0])
list1=sorted(list1,reverse=True)
print(int(date0[0])-list1[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A+=[A[0]+K]
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()))
A+=[A[0]+K]
print(min(K-(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()))
distance=max(A)-min(A)
for i in range(N):
    A.append(A[i]+K)
for i in range(N):
    distance=min(distance,A[i+N-1]-A[i])
print(distance)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
K, N = map(int, input().split())
A = deque(map(int, input().split()))
A.appendleft(K - A[-1] + A[0])
sa_max = A[0]
for i in range(1, N):
    sa = A[i + 1] - A[i]
    if sa_max < sa:
        sa_max = sa
print(A[-1] + A[0] - (sa_max + A[1]))

Submission status: Accepted

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

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

print(k - max(a_sub))

Submission status: Accepted

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

a = list(int(i) for i in input().split())
s = []
for i in range(n-1):
   s.append(a[i+1] - a[i])

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

print(k - max(s))

Submission status: Accepted

Solution code:
round,house_num = map(int,input().split())

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

min = distance[-1] - distance[0]

for i in range(house_num - 1):
  if round-(distance[i+1]-distance[i]) < min:
    min = round-(distance[i+1]-distance[i])
print(min)

Submission status: Accepted

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

Submission status: Accepted

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

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

distance.append(A[0] + K - A[-1])
most = max(distance)
output = K - most
print(output)

Submission status: Accepted

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

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

import numpy as np
A=np.array(A)

ans=max(np.diff(A).max(), K-A[-1]+A[0])
if ans<=K/2:
    print(K-ans)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[K,N]=[int(item) for item in input().split()]
array = [int(item) for item in input().split()]
max_=0
for i in range(N-1):
    dist=abs(array[i] - array[i + 1])
    if dist > max_:
        max_=dist

dist=array[0] + (K-array[N-1])
if dist > max_:
    max_=dist
print(K-max_)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
li=list(map(int,input().split()))
def l(m):
    x=li[m+1]-li[m]
    return(x)
list_l=[l(m) for m in range(n-1)]
list_l.append(li[0]+k-li[n-1])
list_l.sort()
list_l.reverse()
print(sum(list_l)-list_l[0])

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
c=sorted(map(int,input().split()))
x=int(1)
dif=[]
while x<b:
    dif.append(c[x]-c[x-1])
    x=x+1
dif.append(a-max(c)+min(c))
print(a-max(dif))

Submission status: Accepted

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

print(K - max(diff))

Submission status: Accepted

Solution code:
# abc160 c
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
a = [i - a[0] for i in a]
d = []
for i in range(len(a)):
    if i == len(a)-1:
        d.append(k - a[i])
    else:
        d.append(a[i+1] - a[i])
print(k - max(d))

Submission status: Accepted

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

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

Submission status: Accepted

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

a.append(kn[0]+a[0])
dist = []
for i in range(1, kn[1]+1):
    dist.append(a[i]-a[i-1])

dist[dist.index(max(dist))] = 0

print(sum(dist))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
longest = 0
prev = a[0]
for h in a[1:]:
    dif = h - prev
    prev = h
    if dif > longest:
        longest = dif
b = k - a[-1] + a[0]
if longest <= b:
    longest = b
print(k - longest)

Submission status: Accepted

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

max = 0
i = 0
while i < n-1:
  if max < a[i+1] - a[i]:
    max = a[i+1] - a[i]
  i+=1

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

print(k-max)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
d=k-a[n-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.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 = list(map(int,input().split()))
re_a = []
for x in a_list:
    re_a.append(x-k)
a_list_add_re = re_a+a_list
result = []
x = n - 1
for i in range(len(a_list_add_re)-x):
    result.append(a_list_add_re[i+x] - a_list_add_re[i])
    
print(min(result))

Submission status: Accepted

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

for i in range(N):
    if i < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)

Submission status: Accepted

Solution code:
K,N=map(int, input().split())
A=[int(i) for i in input().split()]
D=[0]*(N)
sum=0
for i in range(0, N-1):
  D[i]=A[i+1]-A[i]
  sum+=D[i]
D[N-1]=A[0]-A[N-1]+K
sum+=D[N-1]
dist=-1
for i in range(0, N):
  temp=sum-D[i]
  if(dist==-1):
    dist=temp
  else:
    dist=min(temp, dist)
print(dist)

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
As = np.array(input().split(), dtype=int)

lis = np.append(As, As[0] + K)
diff = np.diff(lis)
longest = diff.max()
print(K - longest)

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a = [a[-1] - k] + a + [k + a[0]]
    mt = 10 ** 10
    for i in range(1, len(a) - 1):
        mt = min(mt, k - abs(a[i] - a[i-1]), k - abs(a[i] - a[i+1]))
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

maximum=0
previous=0
for i in array:
    maximum=max(maximum,abs(previous-i))
    previous=i

maximum=max(maximum,k-array[-1]+array[0])

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):
  d.append(A[i+1] - A[i])
d.append((K - A[N-1]) + A[0])

d.remove(max(d))
print(sum(d))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

a_list = a_list + list(map(lambda x: x + k, a_list))

mi_len = k
for i in range(len(a_list)-(n-1)):
    mov_len = a_list[i+(n-1)] - a_list[i]
    if mi_len > mov_len:
        mi_len = mov_len

print(mi_len)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
ma = -1
mi = -1
for i in range(n-1):
    if abs(a[i] - a[i+1]) > ma:
        ma = abs(a[i] - a[i+1])
        mi = i
flag = False
if abs(a[n-1]-k-a[0]) > ma:
    ma = abs(a[n-1]-k-a[0])
    mi = n-1
    flag = True
sum = 0
if flag:
    for i in range(0, n-1):
        sum += abs(a[i] - a[i+1])
else:
    for i in range(0, mi):
        sum += abs(a[i] - a[i+1])
    for i in range(mi+1, n-1):
        sum += abs(a[i] - a[i+1])
    sum += abs(a[n-1]-k-a[0])
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

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

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


def solve():
    k, n = MI()
    A = LI()

    path = [A[i] - A[int1(i)] for i in range(1, len(A))]
    l = A[-1] - A[0]
    path.append(min(l, k - l))
    # print(path)

    ans = sum(path)
    if len(path) > 2:
        ans = ans - max(path)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

min = k
for i in range(n):
  if A[i+n-1]>=A[i]: 
    d = A[i+n-1]-A[i]
  else:
    d = A[i+n-1]+k-A[i]
  if d<min: min = d
    
print(min)

Submission status: Accepted

Solution code:
import copy
from functools import reduce
k,n=map(int,input().split())
a=list(map(int,input().split()))
c=copy.copy(a)
c.insert(0,0)
a.insert(n,(k+a[0]))
b=list(map(lambda a,b:a-b,a,c))
d=reduce(lambda a,b:b if b>a else a,b)
print(k-d)

Submission status: Accepted

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

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

kyori_sa = []

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

print(k-max(kyori_sa))

Submission status: Accepted

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

Submission status: Accepted

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

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

mx = house[0] + (K - house[-1])

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

print(K - mx)

Submission status: Accepted

Solution code:
firstInput=list(map(int,input().split()))
perimeter=firstInput[0]
numHouses=firstInput[1]
housesDistances=list(map(int,input().split()))
maxDistance=0
maxidx=1
totalDistance=0
for idx, house in enumerate(housesDistances[:-1]):
  distance=housesDistances[idx+1]-house
  totalDistance+=distance
  if distance>maxDistance:
      maxDistance= distance
      maxidx=idx+1

lastDistance= perimeter-housesDistances[-1]+housesDistances[0]
totalDistance+=lastDistance
if lastDistance>maxDistance:
  maxDistance=perimeter-housesDistances[-1]+housesDistances[0]
  maxidx=idx+1
print(totalDistance-maxDistance)

Submission status: Accepted

Solution code:
K,N = [int(i) for i in input().rstrip().split()]
A = [int(i) for i in input().rstrip().split()]
A.sort()
maxD = [A[0] + (K - A[-1]), 0]
for i, a in enumerate(A):
    if i == 0:
        continue
    D = A[i] - A[i-1]
    if D > maxD[0]:
        maxD = [D,i]
print(K - maxD[0])

Submission status: Accepted

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

A = input().split()

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

for i in range(N):
    try:
        if abs(int(A[i]) - int(A[i+1])) > maxv:
            maxv = abs(int(A[i]) - int(A[i+1]))
    except IndexError:
        break

print(K-maxv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections
sys.setrecursionlimit(10**6)

class gv:
    K = 0
    N = 0
    A = []
    max_interval = 0

def build():
    gv.K, gv.N = map(int, sys.stdin.readline().split(' '))
    *gv.A, = map(int, sys.stdin.readline().split())
    for i in range(1,gv.N):
        gv.max_interval = max(gv.max_interval, gv.A[i] - gv.A[i-1])

def solve():
    ans = 0
    ans = min(  gv.A[gv.N -1] - gv.A[0], 
                gv.K - gv.max_interval)
    
    print(ans)

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

Submission status: Accepted

Solution code:
#C
K, N = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
Ad = A + [K+A[0]]
Ad2 = []
for i in range(N):
  Ad2 += [Ad[i+1]-Ad[i]]

print(sum(Ad2)-max(Ad2))

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions

n, k = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]

w = 0
for i in range(k):
    if i != k - 1:
        temp = a[i+1] - a[i]
    else:
        temp = a[0] + n - a[i]
    if w < temp:
        w = temp
        
print(n - w)

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):
    d=a[i+1]-a[i]
    m=max(d,m)
d_last=a[0]+k-a[n-1]
m=max(d_last,m)

ans=k-m
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

k, n = map(int, input().split())
A = list(map(int, input().split()))
AA = sorted(A)
AA.append(AA[0]+k)
dif_AA = []

for a in range(len(AA)-1) : 
    tmp = AA[a+1] - AA[a]
    dif_AA.append(tmp)
    
dif_max = dif_AA.index(max(dif_AA))
ans = AA[dif_max] - AA[0] + AA[-1] - AA[dif_max+1]

print(ans)

# candidate_1 = AA[-1] - AA[1]
# candidate_2 = AA[-2] - AA[0]

# print(min(candidate_1, candidate_2))

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):
    d = a[i+1] - a[i]
    m = max(m, d)
d_last = k - a[n-1] + a[0]
m = max(m, d_last)

res = k - m
print(res)

Submission status: Accepted

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

print(K-a)

Submission status: Accepted

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

import math

m = 0

for i in range(len(lis)):
    a = abs(lis[i] - lis[i-1])
    if K-a <= a:
        a = K-a
    if a >= m:
        m = a

print(K-m)

Submission status: Accepted

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    cost = 10**10
    for i in range(n-1):
        cost = min(cost, k - abs(a[i]-a[i+1]))
    print(min(cost, k-abs(a[0]+k-a[n-1])))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K, N = map(int, input().split())
A_list = list(map(int, input().split()))
q = K - (A_list[N-1] - A_list[0])
for i in range(N-1):
    p = A_list[i+1] - A_list[i]
    if p > q:
        q = p
print(K - q)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

k,n=n1()
a=n1()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    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))
resolve()

Submission status: Accepted

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

biggestDist = 0
for i in range(len(a)):
    if i == len(a)-1:
        aDist = a[0]-(a[i]-K)
        biggestDist = max(aDist, biggestDist)
    else:
        dist = a[i+1]-a[i]
        biggestDist = max(dist, biggestDist)
print(K-biggestDist)

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

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


if __name__ == '__main__':
    main()

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):
    d = abs(a[i+1]-a[i])
    distance.append(d)
a = sum(distance)
distance.append(k-a)
print(sum(distance)-max(distance))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
i = []
for line in sys.stdin.readlines():
  i.append(line.rstrip().split(" "))

list_i1 = [int(s) for s in i[1]]

a = 0
distance = []
for list_i1[a] in list_i1:
  if a > len(list_i1)-2:
    break
  distance.append(list_i1[a+1] - list_i1[a]) 
  a += 1
c = 0
for b in distance:
  c += b
distance.append(int(i[0][0]) - c)

print(int(i[0][0]) - max(distance))

Submission status: Accepted

Solution code:
# スペース区切りの文字列の入力
K, N = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

# 最も離れた場所を求める(通常)
max_val = 0
for i in range(N-1):
  max_val = max(max_val, A[i+1] - A[i])
# 最も離れた場所を求める(先頭と終端)
max_val = max(max_val, K - A[N-1] + A[0])

print(K-max_val)

Submission status: Accepted

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

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

print(K-max(d))

Submission status: Accepted

Solution code:
# スペース区切りの文字列の入力
K, N = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

ans = 0
max = 0

for i in range(N):
  if(i == N - 1):
    A2 = K + A[0]
  else:
    A2 = A[i+1]
    
  # 距離が最大のもの以外をansに加算
  if(max < abs(A2 - A[i])):
      ans += max
      max = abs(A2 - A[i])
  else:
    ans += abs(A2 - A[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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