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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N, *A = map(int, open(0).read().split())
A = [A[-1] - K] + A

max_gap = 0
for a1, a2 in zip(A, A[1:]):
    max_gap = max(max_gap, a2 - a1)
print(K - max_gap)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-d)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

K, N = MI()
A = LI()
A_dif = []

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

print(K - max(A_dif))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

a.append(a[0]+k)

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

print(k-l)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
distance = [0] * N
for i in range(N):
    if not i == N-1:
        distance[i] =  A[i+1] - A[i]
    else:
        distance[N-1] = A[0] + (K - A[N-1])
distance.sort()
distance.pop()
print(sum(distance))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
li = []
for i in range(N):
    if i == 1:
        cadA = A[N-1] - A[0]
    else:
        cadA = K - A[i] + A[i-1]
    if i == N-1:
        cadB = A[N-1] - A[0]
        cadB = 100000000000
    else:
        cadB = A[i] + (K - A[i+1])
        cadB = 100000000000
    li.append(min(cadA, cadB))
print(min(li))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k,n = map(int, input().split())
arr = list(map(int, input().split()))
prev = 0
m = 0
for i in arr:
    m = max(m, abs(prev-i))
    prev = i

m = max(m, k+arr[0]-arr[n-1])
ans = k-m
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K,N = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(N):
    a.append(a[i] + K)
minimum = math.inf 
for i in range(N):
    minimum = min(minimum,a[i+N-1]-a[i])
print(minimum)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

dif = [A[i+1] - A[i] for i in range(N-1)]

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

print(K - max(dif))

Submission status: Accepted

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

Submission status: Accepted

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

for j in range(n,2*n):
    aa[j] = aa[j] + k

dis = k

for i in range((2*n)-1):
    tonari = aa[i+1]-aa[i]
    dis = min(dis,k-tonari)

print(dis)

Submission status: Accepted

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

A.sort()

dist = []

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

print(K - max(dist))

Submission status: Accepted

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

    max_dist = 0
    for i in range(1, N):
        dist_tmp = A[i] - A[i-1]
        if(dist_tmp > max_dist):
            max_dist = dist_tmp
    dist_over_zero = K - (A[N-1] - A[0])
    if(dist_over_zero > max_dist):
        ans = K - dist_over_zero
    else:
        ans = K - max_dist
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

def main():
    k, n = map(int, input().split())
    A_array = list(map(int, input().split()))
    A_array.sort()
    # print(A_array, A_array[-1])
    
    
    ans = float("inf")
    for i in range(n):
        if i == 0:
            d_r = A_array[-1] - A_array[0]
            d_l = k - (A_array[1] - A_array[0])
            d = min(d_r, d_l)
        elif i == n - 1:
            d_r = k - (A_array[-1] - A_array[-2])
            d_l = A_array[-1] - A_array[0]
            d = min(d_r, d_l)
        else:
            d_r = k - (A_array[i] - A_array[i-1])
            d_l = A_array[i] + (k - A_array[i+1])
            d = min(d_l, d_r)
        # print(d)
        ans = min(ans, d)
    print(ans)
main()

Submission status: Accepted

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

b.sort()
b[-1]=0
print(sum(b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    K, N = map(int, input().split())
    A = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

D = [A[i + 1] - A[i] if i < len(A) - 1 else K - A[i] + A[0] for i in range(len(A))]
D.sort()
print(K - D[-1])

Submission status: Accepted

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

Submission status: Accepted

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

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

if len(A) == 2:
    dist = A[1] - A[0]
    if 2 * dist > K:
        dist = K - dist

minDist = float("inf")
for i in range(N-1):
    if K - (A[i+1] - A[i]) < minDist:
        minDist = K - (A[i+1] - A[i])

minDist = min(minDist, A[N-1]-A[0])

print(minDist)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

#0と最後
max_ = [0, K+A[0]-A[-1]]

#iとi+1
for i in range(1, N):
    tmp = A[i] - A[i-1]
    if tmp > max_[1]:
        max_[0] = i
        max_[1] = tmp
print(K-max_[1])

Submission status: Accepted

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

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

l = [] # 各家間の距離リスト（i=1,2～）

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

print(k-max(l))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    solve()

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)
D = np.diff(a)
D = np.append(D, a[0] + K - a[-1])

print(K - max(D))

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)
D = np.diff(a)
D = np.append(D, a[0] + K - a[-1])
ind = np.argmax(D)

if N == 2:
    print(a[ind+1]-a[ind])
elif ind == len(a)-1:
    ans =  -a[0] + a[ind]
    print(ans)
else:
    ans = K - (a[ind+1]-a[ind])
    print(ans)

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):
    M.append(A[i]-A[i-1])
M=sorted(M)
print(K-M[N-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

dl.append(k - l[n-1] + l[0])
print(k - max(dl))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

B=sorted(B)

ans = sum(B[0:-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

max_kyori = 0
a.append(k + a[0])

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

print(k - max_kyori)

Submission status: Accepted

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

max_l = 0
for i in range(n):
  if i != n - 1:
    l = lst[i + 1] - lst[i]
  else:
    l = k + lst[0] - lst[i]
  if l > max_l:
    max_l = l
print(k - max_l)

Submission status: Accepted

Solution code:
#C - Traveling Salesman around Lake WA(ヒント)
K,N = map(int,input().split())
A = list(map(int, input().split()))

A[N+1:] = [A[0] + K]#Aを二回繰り返して円を表現

distance_list = []
for i in range(N):
    distance = abs(A[i] - A[i+1])
    distance_list.append(distance)
distance_list.remove(max(distance_list))
print(sum(distance_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(K - mm)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N,*A = map(int, open(0).read().split())

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

ans.append(A[0] + K - A[-1])
ans.sort()
ans.pop()

print(sum(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

dist=sorted(dist)

ans=0
for i in range(N-1):
    ans+=dist[i]

print(ans)

Submission status: Accepted

Solution code:
import math


def resolve():
    import sys
    input = sys.stdin.readline
    # row = [int(x) for x in input().rstrip().split(" ")]
    kn = [int(x) for x in input().rstrip().split(" ")]
    k = kn[0]
    a = [int(x) for x in input().rstrip().split(" ")]

    max_dist = max([a[i+1] - a[i] for i in range(len(a)-1)])
    max_dist = max_dist if max_dist > a[0] + k - a[len(a)-1] else a[0] + k - a[len(a)-1]

    print(k - max_dist)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

B=[]
t=0
for a in A:
    B.append(a-t)
    t=a
B[0] +=k-t

print(k-max(B) )

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
l1 = list(map(int,input().split()))
l2 = [l1[i]-l1[i-1] for i in range(len(l1))]
l2[0] = l1[0] + K - l1[-1] 
l2.sort()
ans = 0
for i in range(N-1):
  ans += l2[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
score = list(map(int,input().split()))
kenl = list(map(int,input().split()))

syoki = 0
syoki = kenl[0]+(score[0]-kenl[score[1]-1])
length = [0]*score[1]
length[0] = syoki
for i in range(score[1]-1):
    length[i+1] = kenl[i+1]-kenl[i]

length.sort()
answer = length[score[1]-1]
print(score[0]-answer)

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=[]
p = A[-1]-K
for a in A:
  l.append(a-p)
  p=a
print(sum(l)-max(l))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
h=list(map(int,input().split()))
l=[]
for i in range(n-1):
  l.append(h[i+1]-h[i])
l.append(h[0]+k-h[n-1])
ll=sorted(l,key=int,reverse=True)
print(k-ll[0])

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# import numpy as np

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
K, N = map(int, input().split())
A = list(map(int, input().split()))
# Q = list(map(int, input().split()))
# S = input()

# d = sorted(d.items(), key=lambda x:x[0]) # keyでsort

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

print(K - max(interbals))

Submission status: Accepted

Solution code:


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
def listinput(): return input().split()
#template
K,N = inputlist()
A = inputlist()
dis = [0]*(N)
for i in range(N):
    if i == N-1:
        dis[i] = K+A[0] - A[i]
        continue
    dis[i] = A[i+1] - A[i]
dis.sort()
print(sum(dis[:-1]))

Submission status: Accepted

Solution code:
line1 = input()
line2 = input()

aryLine1 = line1.split()
arystrLine2 = line2.split()

K = int(aryLine1[0]);
N = int(aryLine1[1]);

aryLine2 = [int(s) for s in arystrLine2]
#print(aryLine2)

aryLine2.sort()
#print(aryLine2)

ans = K

for i in range(0, N):
    temp1 = int(aryLine2[i])
    if i == N - 1:
        temp2 = int(aryLine2[0]) + K
    else:
        temp2 = int(aryLine2[i + 1])

    temp = temp1 - temp2 + K

    if temp < ans:
        ans = temp

print(ans)

Submission status: Accepted

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

ans = 10**6 + 1
for i in range(n):
    ans = min(ans, a[-1] - a[i])
    a.append(a[i]+k)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(N):
  if i == 0:
    distance = A[N-1] - A[0]
  elif i == N-1:
    distance = (K-A[i])+A[i-1]
  else:
    distance = (K-A[i+1])+A[i]
  if distance < min_distance:
    min_distance = distance
print(min_distance)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

k,n = inpm()
a = inpl()

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

print(k - d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(b)-max(b))

Submission status: Accepted

Solution code:
# 改善1：sys入力
# 改善2：ifで分岐させるより、あとから個別対応した方が分岐を毎回やらないですむ？


import sys
input=sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main(k,n,a):
    dmax=0
    for i in range(n-1):
        dmax=max(dmax,a[i+1]-a[i])
    dmax=max(dmax,k+a[0]-a[n-1])
    return k-dmax

if __name__=='__main__':
    k,n,a=readinput()
    ans=main(k,n,a)
    print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

max_index = diff_list.index(max(diff_list))
del diff_list[max_index]

print(sum(diff_list))

Submission status: Accepted

Solution code:
K,N=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ls_sub=[ls[i]-ls[i-1] for i in range(N)]
ls_sub[0]+=K
print(K-max(ls_sub))

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[n-1] + a[0])
b = sorted(b)

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

Submission status: Accepted

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

Submission status: Accepted

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

L = [] #各家の間の距離

for n in range(1, N):
  L.append(A[n]-A[n-1])

L.append(A[0]+K-A[N-1]) #最初の家と最後の家の距離（起点から最初の家の距離に、最後の家から起点までの距離を足す）

ans = K - max(L)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

k,n = [int(x) for x in stdin.readline().strip().split()]
a_lst = [int(x) for x in stdin.readline().strip().split()]
diff_lst = [a_lst[i+1] - a_lst[i] for i in range(len(a_lst)-1)] + [k - a_lst[-1] + a_lst[0]]

print(k - max(diff_lst))

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[n-1]+a[0])

b = sorted(b)

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

Submission status: Accepted

Solution code:
import sys
import itertools
k, n = map(int, sys.stdin.buffer.readline().split())
a = list(map(int, sys.stdin.buffer.readline().split()))
a.append(a[0] + k)
b, c = itertools.tee(a)
next(c)
ans = k - max(y - x for x, y in zip(b, c))
print(ans)

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[n-1] + a[0])

b = sorted(b)
print(sum(b[:-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=[int(i) for i in input().split()]
B=A+[i+K for i in A]
ans=K
for i in range(N):
    tmp=B[i+N-1]-B[i]
    ans=min(ans,tmp)
print(ans)