Problem:


Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.



Sample input:
20 3
5 10 15


Sample output:
10

Submission status: Accepted

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

Submission status: Accepted

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

a = sorted(a)
q = [an - ap for an, ap in zip(a[1:], a[:-1])]

nomal = max(a) - min(a)
abnomal = k - max(q)

print(min(nomal, abnomal))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

k, n = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
d = max(max([abs(a[x] - a[x - 1]) for x in range(len(a)) if x > 0]), abs(a[0] + k - a[len(a)-1]))

print(k - d)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

s=sum(b)
b.append(k-a[n-1]+a[0])
c =[]
for j in range(0,n):
  h=s-max(b[j+1],b[j])
  c.append(h)
print(min(c))

Submission status: Accepted

Solution code:
pro_list = input().split()
circle_distance = int(pro_list[0])
house_num = int(pro_list[1])

house_distance_list = [int(i) for i in input().split()]
max = -1
max_index = 0
for i in range(house_num):
  if i == house_num-1:
    distance_gap = (circle_distance - house_distance_list[i]) + house_distance_list[0]
  else:
    distance_gap = house_distance_list[i+1] - house_distance_list[i]
  if max < distance_gap:
    max = distance_gap
    max_index = i
print(circle_distance - max)

Submission status: Accepted

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

Submission status: Accepted

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

distance = list()

for a in range(0, N):
    if a == N - 1:
        num = K - coordinate[a] + coordinate[0]
    else:
        num = coordinate[a+1] - coordinate[a]
    distance.append(num)

print(K - max(distance))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n = map(int,raw_input().split(' '))
hs = map(int ,raw_input().split(' '))
hs.sort()
distances = [abs(u-v) for u,v in zip(hs,hs[1:])]
distances.append(k - hs[-1] + hs[0])
s = sum(distances)
print s - max(distances)

Submission status: Accepted

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

A = list(map(int,input().split()))
A.append(0)
A.append(K)
A = sorted(A)

S = [0]*N
S[0] = A[0]

#一番広い隙間を通らない方法を考える
#A[0]-A[N-1]
for i in range(1,N):
    S[i-1] = A[i+1] - A[i]
S[N-1] = K-A[N]+A[1]
print(K-max(S))

Submission status: Accepted

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

Distance = []
for i in range(N-1):
    Distance.append(A_list[i+1]-A_list[i])
Distance.append(A_list[0]+K-A_list[N-1])
Distance.sort(reverse=True)

print(K-Distance[0])

Submission status: Accepted

Solution code:
a,b=list(map(int,input().split()))
l=list(map(int,input().split()))
maxll=0
for i in range(b-1):
    if maxll<l[i+1]-l[i]:
        maxll=l[i+1]-l[i]
if maxll<l[0]+a-l[b-1]:
    maxll=l[0]+a-l[b-1]
print(a-maxll)

Submission status: Accepted

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

Submission status: Accepted

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

B = []

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

print(sum(B)-max(B))

Submission status: Accepted

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

assumption = 0
a = 0
b = 0


for i in range(N - 1):
  a += A[i +1]
 
  b += A[i]
 
  
  if assumption > A[i +1] - A[i]:
    assumption = assumption
  else:
    assumption = A[i +1] - A[i]
  

if assumption > (K - A[-1] + A[0]):
  result = a - b - assumption +  (K - A[-1] + A[0])

else:
  result = a - b 
 
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

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

ans = K - max(B)

print(ans)

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
A = list(map(int,input().split()))
d = []
for i in range(N-1):
    d.append(A[i+1]-A[i])
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()))
a.append(a[0]+k)
dist=[0]*n
for i in range(1,len(a)):
	dist[i-1]=a[i]-a[i-1]
print(sum(dist)-max(dist))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

D = [0] * n
for i in range(len(A)-1):
    D[i] += A[i+1]-A[i]
D[n-1] += A[0] + k - A[n-1]

D.remove(max(D))
print(sum(D))

Submission status: Accepted

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


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

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

ans = k - m

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

max = max(length)
sum1 = sum(length) #内側の合計

length.append(K-A[N-1]+A[0])

sum2 = sum(length)-max #外側を通るときの合計

if sum1 >= sum2:
    print(sum2)
else:
    print(sum1)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-temp)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
#s= list(input())
a = list(map(int, input().split()))
maxd = a[0] -a[n-1] + k

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

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

ans = K - max(B)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

m.sort(reverse=True)
kyori = []
for x in range(n[1]-1):
    kyori.append(m[x]-m[x+1])

kyori.append((m[len(m)-1]+n[0])-m[0])
print(sum(kyori)-max(kyori))

Submission status: Accepted

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

e=k-L[n-1]+L[0]
c.append(e)

print(k-max(c))

Submission status: Accepted

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

print(k-max(s))

Submission status: Accepted

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

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

print(K-max)

Submission status: Accepted

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

Submission status: Accepted

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

max=0
b=0
for i in range(N-1):
    b=a[i+1]-a[i]
    if(b>=max):
        max=b
    
m=K-a[i+1]+a[0]

if(m>max):
    max=m

kyori=K-max

print(kyori)

Submission status: Accepted

Solution code:
K,N=map(int, input().split())
Alist=list(map(int, input().split()))
diflist = []
def whichside(K,A1,A2):
    diff = A2-A1
    diff = K-diff if K-diff<diff else diff
    return diff
diflist.append(whichside(K,Alist[0],Alist[-1]))
for i in range(1,len(Alist)):
    diff = whichside(K,Alist[i-1],Alist[i])
    diflist.append(diff)
print(K-max(diflist))

Submission status: Accepted

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

    for i in range(N):
        if i == N - 1:
            d = A[0] + K - A[-1]
            dlist.append(d)
        else:
            d = abs(A[i] - A[i+1])
            dlist.append(d)
    dlist.sort()
    print(sum(dlist[:N-1]))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

d_max = K-A[N-1]+A[0]
sum = d_max

for i in range(N-1):
    d = abs(A[i]-A[i+1])
    d_max = max(d,d_max)
    sum += d

print(sum-d_max)

Submission status: Accepted

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

    temp = A[i+1]-A[i]
    ans  = max(ans, temp)

print(K-ans)

Submission status: Accepted

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

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

print(K - max_dist)

Submission status: Accepted

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

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

print(K - max(A_dist))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    dis = [0] * n
    for i in range(n):
        if i == 0:
            dis[i] = a[-1] - a[0]
        else:
            dis[i] = k - a[i] + a[i - 1]
    print(min(dis))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a_list = list(map(int, input().split()))
sa_list = []
for i in range(n-1):
    sa_list.append(a_list[i+1]-a_list[i]) 

sa_list.append(k-a_list[-1]+a_list[0])



print(k - max(sa_list))

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 max < a[i+1]-a[i]:
        max = a[i+1]-a[i]
if max < k-a[n-1]+a[0]:
    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()))
dis = [0]*n
for i in range(n):
    if i == 0:
        dis[i] = a[-1] - a[0]
    else:
        dis[i] = k - a[i] + a[i-1]
print(min(dis))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-maxi)

Submission status: Accepted

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

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

dist = []
A = [0] + A + [K+A[0]]

max_d = 0
for i in range(1, N+2):
    d = A[i]-A[i-1]
    dist.append(d)

    if d > max_d:
        max_d = d

print(K-max_d)

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = input().split()
s = 0
t = 0
while t < n:
  x = int(a[t-1])
  y = int(a[t])
  if y-x > s:
    s = y-x
  t += 1
l = int(a[0])+k - int(a[-1])

print(k-max(s,l))

Submission status: Accepted

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

Submission status: Accepted

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


c=k-li[n-1]+li[0]
dis=[c]
i=0
for i in range(n-1):
  a=li[i+1]-li[i]
  dis+=[a]

max_dis=max(dis)
ans=k-max_dis

print(ans)

Submission status: Accepted

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

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

d.append((K-A[N-1])+A[0])
print(K-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split()))
import math;floor,ceil=math.floor,math.ceil
from collections import deque
Yes=lambda b:print('Yes')if b else print('No');YES=lambda b:print('YES')if b else print('NO')
is_even=lambda x:True if x%2==0 else False

def main():
    k,n=ints();a=ints()
    a.append(a[0]+k);diff=[a[i+1]-a[i]for i in range(n)]
    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()))

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

d0=K-A[N-1]+A[0]
if max(d)>d0:
  print(K-max(d))
else:
  print(K-d0)

Submission status: Accepted

Solution code:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))
A_dist = [0]*N

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


A_dist_max = max(A_dist)
print(K-A_dist_max)

Submission status: Accepted

Solution code:
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
ans = 0
if(nums[-1] > N[0]//2):
    Max = (N[0]-nums[-1]) + nums[0]
    t = 0
else:
    Max = nums[-1] - nums[0]
    t = 0
for i in range(N[1]-1):
    if(nums[i+1] - nums[i])>Max:
        Max = nums[i+1] - nums[i]
        t = i
if(t==0):
    for i in range(N[1]-1):
        ans += nums[i+1] - nums[i]
else:
    for k  in range(0,t):
        ans += nums[k+1] - nums[k]
    for j in range(t+1,N[1]-1):
        ans += nums[j+1] - nums[j]
    if(nums[-1] > N[0]//2):
        ans += (N[0]-nums[-1]) + nums[0]
    else:
        ans += nums[-1] - nums[0]
print(ans)

Submission status: Accepted

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

x=0
for i in range(len(a)-1):
    dis=abs(a[i+1]-a[i])
    x=max(x,dis)

print(k-x)

Submission status: Accepted

Solution code:

def main():
  k, n = map(int, input().split())
  dis = tuple(map(int, input().split()))

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

  max_dis = 0
  max_idx = 0
  for i, d in enumerate(diff):
    if d > max_dis:
      max_dis = d
      max_idx = i

  diff[max_idx] = 0

  print(sum(diff))

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(k-dis[-1])

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 = np.concatenate([a, [k + a[0]]])
diff_a = np.diff(a)
diff_a = sorted(diff_a)
print(np.sum(diff_a[:-1]))

Submission status: Accepted

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

distance = []

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

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

print(sum(distance)- max(distance))

Submission status: Accepted

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

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

print(sum(x) - max(x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
メモ

時計回りに、隣接する家までの距離を計算する。
　移動先の家の方が、原点からの距離が大きい場合、
 　差を計算する
 小さい場合、
 1周の距離を用いて、差を計算する
 
上記で計算した距離から最大距離を除いた和を出力する

'''

K, N = map(int, input().split())
distance_from_north = [int(i) for i in input().split()]

distance_between_house = []

for i, distance in enumerate(distance_from_north):
  if i == len(distance_from_north) - 1:
    distance_between_house.append(distance_from_north[0] + (K - distance))
    break
  
  distance_between_house.append(distance_from_north[i + 1] - distance_from_north[i])
  
distance_between_house = sorted(distance_between_house)
print(sum(distance_between_house[:-1]))

Submission status: Accepted

Solution code:
def solve(a, k):
    a.sort()
    min_dist = k
    for i in range(len(a)):
        start = a[i]
        end = a[(i + len(a) - 1) % len(a)]
        if start > end:
            end += k
        min_dist = min(min_dist, end - start)
    return min_dist


if __name__ == "__main__":
    k, n = map(int, input().split())
    a = [int(x) for x in input().split()]
    print(solve(a, k))

Submission status: Accepted

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

Submission status: Accepted

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

dist = []

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

print(k-max(dist))

Submission status: Accepted

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

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

ad_max = max(ad)
ad_max_ind = ad.index(ad_max)

if ad_max_ind == (n-1):
    ans = a[n-1] - a[0]
if ad_max_ind != (n-1):
    ans = k - a[ad_max_ind+1] + a[ad_max_ind]
    
print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

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

m = max(dist)

print(K-m)

Submission status: Accepted

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

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

print(K - max(dist))

Submission status: Accepted

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

Submission status: Accepted

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

l = []

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

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

print(k-max(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0] * (2 * N)
    for i in range(2 * N):
        if i < N:
            B[i] += A[i]
        else:
            B[i] = A[i-N] + K

    ans = float('inf')

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

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

l = list(map(int, input().split()))
m = [0]*n
count = 0

for i in range(n-1):
    m[count] = l[i+1] - l[i]
    count+=1
m[n-1] = k - l[n-1]+ l[0]

print(k - max(m))

Submission status: Accepted

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

b_l = [a_l[i]-a_l[i-1] for i in range(n)]
b_l[0] += k
b_l.sort()
print(k-b_l[-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import math

N, K = list(map(int, stdin.readline().rstrip().split()))
A = list(map(int, stdin.readline().rstrip().split()))


A.append(N + A[0])

max_diff = 0

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

print(N - max_diff)

Submission status: Accepted

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

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

#print(A)
#print(d)
print(sum(d) - max(d))

Submission status: Accepted

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

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

for i in range(len(A)):
    if i+1 == len(A):
        break
    dis.append(A[i+1] - A[i])


        
print(K - max(dis))

Submission status: Accepted

Solution code:
m,n=map(int,input().split())
li=list(map(int,input().split()))
M=0
li.append(li[0]+m)
for i in range(n):
    if abs(li[i+1]-li[i])>M:
        M=abs(li[i+1]-li[i])
print(m-M)

Submission status: Accepted

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

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(len(a)-1)]
diff.append(K - a[len(a)- 1] + a[0])
diff.sort()
diff.pop(-1)
print(sum(diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = [int(s) for s in input().split()]
arr = [int(s) for s in input().split()]
arr.sort()
maxi = 0
for i in range(1,len(arr)):
    maxi = max(maxi,arr[i]-arr[i-1])
maxi = max(maxi,n-arr[k-1]+arr[0])
print(n-maxi)

Submission status: Accepted

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

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

print(min(b))