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())
#K: 湖1周の長さ, N:湖の周りにある家の数

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

ans = 10**10
for i in range(N):
    for j in range(2):
        if j == 0: #時計周り
            if i == 0:
                dif = A[0]+(K-A[-1])
            else:
                dif = A[i] - A[i-1]
            ans = min(ans, K-dif)
        else: #半時計周り
            if i == N-1:
                dif = A[0]+(K-A[-1])
            else:
                dif = A[i+1] - A[i]
            ans = min(ans, K-dif)

print(ans)

Submission status: Accepted

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

print(sum(length)-max(length))

Submission status: Accepted

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

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

for i in range(n-1):
    sans.append(a[i+1]-a[i])
# print(sans)
print(sum(sans) - max(sans))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
tm = a[0]
a.append(tm)
m = -9878789894545656
 
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 = int(input())
# N = int(input())
# A = [int(input()) for i in range(N)]
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))
 
D = []
for i in range(N):
	D.append((K + A[i] - A[i - 1]) % K)
    
print(K - max(D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
cnt = 0
point = []
Min = 999999999
for i in range(n-1):
    point.append(abs(a[i+1] - a[i]))
point.append(k - a[-1] + a[0])
Max_index = point.index(max(point))
for i in range(n):
    if i != Max_index:
        cnt += point[i]
print(cnt)

Submission status: Accepted

Solution code:
i = list(map(int, input().split()))
K = i[0]
N = i[1]

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

m = K

for i in range(N - 1):
  t = max(abs(s[i] - s[i + 1]), K - abs(s[i] - s[i + 1]))
  if t <= m:
     m = t

if max(abs(s[0] - s[N - 1]), K - abs(s[0] - s[N - 1])) <= m:
  m = max(abs(s[0] - s[N - 1]), K - abs(s[0] - s[N - 1]))

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

    diffs = [0]*n
    for i in range(n):
        if i == n-1:
            diffs[i] = k+As[0] - As[i]
        else:
            diffs[i] = As[i+1] - As[i]
    diffs.remove(max(diffs))
    ans = sum(diffs)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

i = 0
length = []
while(i < n):
    if(i == (n-1)):
        length.append(abs((k-a[i]) + a[0]))
    else:
        length.append(abs(a[i+1]-a[i]))
    i += 1

length_sort = sorted(length)
min = sum(length_sort[:n-1])

print(min)

Submission status: Accepted

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

a.append(a[0] + K)
print(K - max([a_next - a_current for a_next, a_current in zip(a[1:], a[:-1])]))

Submission status: Accepted

Solution code:
from operator import sub

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

a.append(a[0] + K)
print(K - max(list(map(sub, a[1:], a[:-1]))))

Submission status: Accepted

Solution code:
import numpy as np

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

print(K - max(np.diff(np.append(a, a[0] + K))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from operator import add
from operator import sub

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

a = a + list(map(add, a, [K] * N))
print(K - max(list(map(sub, a[1:], a[:-1]))))

Submission status: Accepted

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

a = a + [ai + K for ai in a]
print(K - max([a_next - a_current for a_next, a_current in zip(a[1:], a[:-1])]))

Submission status: Accepted

Solution code:
def main():
    K,N = map(int, input().split())
    A = list(map(int, input().split()))
    # start:i->end:i-1 (clockwise)
    ans = K
    for i in range(N):
        if i==0:
            tmp = A[i]+(K-A[i-1])
        else:
            tmp = A[i]-A[i-1]
        ans = min(ans,K-tmp)
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
        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[N-1]+A[0])

        ans = sum(dist) - max(dist)

        print("{}".format(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(k-a[-1]+a[0])
b = sorted(b)
b1 = b[:-1]
print(sum(b1))

Submission status: Accepted

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

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

        dist = []

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

        ans = sum(dist)-max(dist)

        print("{}".format(ans))

if __name__=="__main__":
        main()

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

Submission status: Accepted

Solution code:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
#      .      '                    Udit Gupta @luctivud         ,              
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                            ##     ##  #######  # #  ######
                            ##     ##  ##   ##  ###    ##
                            ##     ##  ##    #  # #    ##
                            #########  #######  # #    ##
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
import math
# sys.setrecursionlimit(10**6)

def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end="tst, ")

MODPRIME = int(1e9+7); BABYMOD = 998244353;
# ################################ HELPER PROGRAMS USED ###################################
# ################################## MAIN STARTS HERE #####################################
# for _testcases_ in range(int(input())):
k, n = get_ints()
a = get_array()
a.sort()
maxdiff = - float('inf')
for i in range(n-1):
    maxdiff = max(maxdiff, abs(a[i] - a[(i+1)%n]))
maxdiff = max(maxdiff, k - a[-1] + a[0])
print(k - maxdiff)

# #########################################################################################
'''
THE LOGIC AND APPROACH IS BY ME @luctivud.
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
(I Own the code if no link is provided here or I may have missed mentioning it)
: DO NOT PLAGIARISE.
'''

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
D = 0
for i in range(N):
    if i == N - 1:
        d = K - (A[N - 1] - A[0])
    else:
        d = A[(i + 1) % N] - A[i]
    if d > D:
        D = d
print(K - D)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

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

if A[N-1] - A[0] <= K / 2:
    res = A[N-1] - A[0]
else:
    res = K - max(dist)

print(res)

Submission status: Accepted

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

l = [0]*n
l[-1] = k+a[0] - a[-1]
for i in range(n-1):
  l[i] = a[i+1]-a[i]
print(sum(sorted(l)[:-1]))

Submission status: Accepted

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

d = 0
for i in range(0,len(a)-1):
  d = max(d,a[i+1]-a[i])
  
s = a[0]+k-a[-1]
d = max(s,d)
print(k-d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
IN = list(map(int, input().split()))
K = IN[0]
N = IN[1]
IN = list(map(int, input().split()))

interval_max = K - IN[N-1] + IN[0]

i = 0
while i < N-1:
    interval = IN[i+1] - IN[i]

    if interval_max < interval:
        interval_max = interval

    i += 1

print(K - interval_max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    max = 0
    tmp = a[-1] - k
    for i in range(n):
        if a[i] - tmp > max:
            max = a[i] - tmp
        tmp = a[i]
    print(k - max)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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

Submission status: Accepted

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

l = list(map(int, input().split()))
ldiff =[l[i] -l[i-1] for i in range(2, N)]
ldiff.append(K - l[-1] + l[0])

lseg = max(ldiff)

print(K - lseg)

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(1,n):
    n_a.append(a[i]-a[i-1])
print(sum(sorted(n_a)[:-1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import sys
def input():
    return sys.stdin.readline()[:-1]

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

    tmp = max(tmp, (K-A[N-1]+A[0]))
    print(K-tmp)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

k,n=nm()
a=nl()

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

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

Submission status: Accepted

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

dif_list = []  # 家の間の距離を格納する配列
for i, l in enumerate(house_list):
    if i == 0:  # 0番目はスキップ
        continue
    dif_list.append(house_list[i] - house_list[i - 1])  # 配列に押し込む

dif_last_start = K - house_list[-1] + house_list[0]  # 最後の家と最初の家の距離

if dif_last_start < max(dif_list):  
    ans = sum(dif_list) - max(dif_list) + K - house_list[-1] + house_list[0]
else:
    ans = sum(dif_list)

print(ans)

Submission status: Accepted

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

gap = 0
for i in range(N-1):
	gap = max(As[i+1] - As[i], gap)

gap = max(As[0]+K-As[-1], gap)

print(K - gap)

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

print(sum(b))

Submission status: Accepted

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

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):
      
    if i==N-1:
        S[i] = K + A[0] -A[i]
    else:
        S[i] = A[i+1] - A[i]

Smax = S.index(max(S))

ans = sum(S)-S[Smax]
print(ans)

Submission status: Accepted

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

length = []

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

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

M = max(list(length))
print(K - M)

Submission status: Accepted

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

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

tonari_dif = 0
for i in range(1,n):
    tonari_dif = max(tonari_dif, a_vector[i] - a_vector[i-1])
#    print(tonari_dif)

true_tonari_dif = max(tonari_dif, a_vector[0]+(k-a_vector[-1]))

print(k-true_tonari_dif)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

K,N = map(int, (input().split()))
A = np.array(list(map(int, input().split())))
B = [0]*N
sortA=np.sort(A)
for i in range(N):
    B[i]=sortA[(N-1+i)%N]-sortA[i]
    if i>0:
        B[i]=K+sortA[(N-1+i)%N]-sortA[i]


print("{}".format(min(B)))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K, N = lr()
A = np.array(lr())
A = np.append(A, A[0] + K)
Adiff = np.diff(A)
answer = K - Adiff.max()
print(answer)

Submission status: Accepted

Solution code:
def main():
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    
    d = []
    for i in range(len(a)):
        try:
            d.append(abs(a[i] - a[i+1]))
        except:
            break
    
    d_max = max(d)
    d_max2 = a[0] + (k-a[-1])
    print(k - max(d_max, d_max2))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(K - max(lst))

Submission status: Accepted

Solution code:
K, N = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
#print(K,N,A)
max_dis = 0
total_dis = 0
for i in range(len(A)):
  if i == len(A)-1:
  	dis = A[0] + (K - A[i])
  else:
  	dis = A[i+1] - A[i]
  total_dis +=dis
  if dis > max_dis:
    max_dis = dis
print(total_dis - max_dis)

Submission status: Accepted

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

D = []
for i in range(1,List[1]):
    D.append(List[0]-(A[i]-A[i-1]))

D.append(List[0]-(A[0]+List[0]-A[List[1]-1]))

print(min(D))

Submission status: Accepted

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

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

D.append(A[0]+List[0]-A[List[1]-1])

print(List[0]-max(D))

Submission status: Accepted

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

for i in range(0,List[1]):
    A.append(A[i]+List[0])

D = []
for i in range(0,List[1]):
    D.append(A[i+List[1]-1]-A[i])

print(min(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
for i in range(len(a)-1):
    tmp=abs(a[i]-a[i+1])
    dist=max(tmp,dist)
print(k-dist)

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
A = list(map(int,input().split()))
B = [] # 逆向き
for i in range(N-1):
  tmp = A[i+1]-K
  B.append(tmp)
B.extend(A)


tmp_sum = 0
j = 0
#print(B)
dist = []
for i in range(1,len(B)):
  d = abs(B[i]-B[i-1])
  tmp_sum += d
  dist.append(d)
  if(i == N-1):
    ans = tmp_sum 
    #print('ans :',ans)
  elif(i >= N):
    tmp_sum -= dist[j]
    j += 1
    ans = min(ans,tmp_sum)
    #print('ans :',ans)
print(ans)

Submission status: Accepted

Solution code:
# Traveling Salesman around Lake https://atcoder.jp/contests/abc160/tasks/abc160_c
N,K= map(int, input().split())

Travel_List=list(map(int,input().split()))
for i in range(K):
    Travel_List.append(Travel_List[i]+N)

Answer =0 
for i in range(K):
    Temp = Travel_List[i+K-1]-Travel_List[i]
    if (i==0) or (Temp<Answer):
        Answer=Temp
    


    
print(Answer)

Submission status: Accepted

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

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

ans = A[-1] - A[0] # K - ((K - A[-1]) + A[0])

for i in range(1,len(A)):
  if K - (A[i] - A[i - 1]) < ans:
    ans = K - (A[i] - A[i - 1])

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

m = max(m, a[0] + k - a[n-1])

print(k - m)

Submission status: Accepted

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

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

print(K-max_dis)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    K, N = [int(i) for i in input().split()]
    AA = [int(i) for i in input().split()]
    maxa = AA[0] + K - AA[-1]
    for i in range(N - 1):
        maxa = max(maxa, AA[i + 1] - AA[i])
    print(K - maxa)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """20 3
5 10 15"""
        output = """10"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """20 3
0 5 15"""
        output = """10"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

b = [0] * (n)
for i in range(n-1):
    b[i] = a[i+1] - a[i]
b[n-1] = k - a[n-1] + a[0]

b.sort()
print(sum(b[:-1]))

Submission status: Accepted

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

m = a[-1] - a[0]

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

Submission status: Accepted

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

a_list = [int(x) for x in input().split()]
a_list.append(k + a_list[0])

d_list = [a_list[i + 1] - a_list[i] for i in range(n)]
print(k - max(d_list))

Submission status: Accepted

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

dmx = K - A[N-1] + A[0]

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

print(K - dmx)

Submission status: Accepted

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

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

ans = K - max(distance)
print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
#B = A + A[0]
ans = 10**20
for i in range(N):
    if i == 0:
        ans = min(ans, A[-1]-A[0], K - (A[1] - A[0]))
    elif i == N-1:
        ans = min(ans, K - (A[N-1] - A[N-2]), A[N-1] - A[0])
    else:
        ans = min(ans, K - (A[i] - A[i-1]), K - (A[i+1] - A[i]))
print(ans)

Submission status: Accepted

Solution code:
K, N = list(map(int, input().rstrip().split()))
l = [int(i) for i in input().rstrip().split()]
dist = [0 for i in range(N)]

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

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

Submission status: Accepted

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

Submission status: Accepted

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


print(k - max(distance))

Submission status: Accepted

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


distance = []

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

print(K-max(distance))

Submission status: Accepted

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

Submission status: Accepted

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

maxDistance = 0

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

maxDistance = max(maxDistance, arr[0]+(k-arr[-1]))

print(k-maxDistance)

Submission status: Accepted

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

maxgap = 0
for i in range(n):
    gap = dis[i+1] - dis[i]
    maxgap = max(maxgap, gap)
print(k-maxgap)

Submission status: Accepted

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

Submission status: Accepted

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

a = list(map(int,input().split()))
a.append(k+a[0])
maxgap = 0
al = a[-1] - a[0]

for l in range(n):
    gap = a[l+1] - a[l]
    maxgap = max(maxgap,gap)
print(al - maxgap)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()

def main():
    K,N = map(int,input().split())
    A_list = list(map(int,input().split()))
    A_list = [i-A_list[0] for i in A_list]
    dist_list = []

    A_list.append(K)
    for i in range(1,len(A_list)):
        d = (A_list[i]-A_list[i-1])
        dist_list.append(d)
    d_sorted = sorted(dist_list)
    print(int(sum(d_sorted[0:N-1])))

main()

Submission status: Accepted

Solution code:
def resolve():
  K, N = [int(i) for i in input().split()]
  A = tuple(int(i) for i in input().split())
  
  distance = [A[i+1] - A[i] for i in range(len(A)-1)]
  distance.append(K - A[-1] + A[0])
  print(K - max(distance))
resolve()

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

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

print(k - max(b))

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
k, n = map(int, input().split())
a = list(map(int, input().split()))

sub = [0] * n
for i in range(n-1):
    sub[i] = a[i+1]-a[i]

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

print(k-max(sub))

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=sorted(map(int,input().split()))
distance=[0]*n

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

ans=k-max(distance)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


# 20 3 20メートルで3件の家
# 0 5 15

# [5, 10] + [0 + (20 -15)]
# [5, 10, 5]
# max([5, 10, 5]) => 10
# 20 - 10

D_i = []
D_i.append(A_i[0] + K - A_i[-1])


for i in range(N-1):
    D_i.append(A_i[i+1] - A_i[i])

print(K - max(D_i))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0
k,n = map(int,input().split())
a = [int(x) for x in input().split()]
a.sort()
a.append(a[0]+k)
for i in range(n):
    if neko < a[i+1] - a[i]:
        neko = a[i+1] - a[i]
print(k-neko)

Submission status: Accepted

Solution code:
K, N = [int(_) for _ in input().split()]
# X = int(input())
A = [int(_) for _ in input().split()]
A.sort()
diff = [A[i+1] - A[i] for i in range(N-1)]
diff.append(A[0] + K - A[-1])
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]
for i in range(N-1):
    tmp = A[i+1] - A[i]
    if tmp > diff:
        diff = tmp
print(K-diff)

Submission status: Accepted

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

Submission status: Accepted

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

dif.sort()
print(k-dif[-1])

Submission status: Accepted

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

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

Submission status: Accepted

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

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

diff_a = [add]
for i in range(n-1):
    d = a[i+1]-a[i]
    diff_a.append(d)
print(sum(diff_a)-max(diff_a))

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(abs(a[i]-a[i+1]))
l.append(k-a[-1]+a[0])

print(sum(l)-max(l))