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=int(input().split(" ")[0])
a = sorted(map(lambda x: int(x), input().split("\n")[0].split(" ")))
dfs=[k-a[len(a)-1]+a[0]]
for i in range(len(a) - 1):
    dfs.append(a[i+1]-a[i])
print(sum(dfs)-max(dfs))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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


longest_dist = (k - A[n-1]) + A[0]

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

print(k-longest_dist)

Submission status: Accepted

Solution code:
k,n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
maxdis = 0
pre = a[-1] - k
for dis in a:
    t = dis - pre
    pre = dis
    if t > maxdis:
        maxdis = t
print(k-maxdis)

Submission status: Accepted

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

for i in range(n-1):
    distances_between_houses.append(a_list[i+1]-a_list[i])

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

max_distance = max(distances_between_houses)
distances_between_houses.remove(max_distance)

print(sum(distances_between_houses))

Submission status: Accepted

Solution code:
import numpy as np

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

next_ls = [0] * n
for i in range(n-1):
    next_ls[i] = a_ls[i+1] - a_ls[i]
next_ls[n-1] = k - a_ls[-1] + a_ls[0]
argmax = np.argmax(next_ls)
next_ls.pop(argmax)
print(sum(next_ls))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
# amax=max(a)
# amin=min(a)

# amin-amax

# 2の場合
if n ==2:
    f=a[0]-a[1]
    if f > k//2:
        ans =k-f
    else:
        ans=f
else:
    a.sort
    a.append(a[0]+k)

    # print(range(len(a[:-1])))
    f=0
    for i in range(len(a[:-1])):
        if (a[i+1] - a[i]) > f:
            f=a[i+1] - a[i]

    ans = k-f
    # if f > k//2:
    #     ans = k -f
    # else:
    #     ans=f

print(ans)

Submission status: Accepted

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

o=l[-1][0]
if o==len(a)-2:
    g=a[-2]-a[0]
else:
    g=a[o]+n-a[o+1]
print(g)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

a_list.append(k+a_list[0])

ans_list = np.diff(a_list)

print(sum(ans_list) - max(ans_list))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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(sum(B) - max(B))

Submission status: Accepted

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

#print(k,n, l)

x = []
for i in range(1, n):
    x .append(l[i]-l[i-1])
#print(x)
x = x + [k - sum(x)]

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

Submission status: Accepted

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

Submission status: Accepted

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

answer = [0]

for index, i in enumerate(house):
  if i == house[-1]:
    X = (K - i) + house[0]
    answer.append(X)
  else:
    X = house[index + 1] - i
    answer.append(X)
print(K - max(answer))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    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]) # 内包表記
    # Maxを抜く
    tmp = max(B)
    print(sum(B) - tmp)
    
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 1行、1変数を受け取るとき
K, N = map(int, input().split())
ls = list(map(int, input().split()))

max_dist = 0

for i in range(len(ls) - 1):
    max_dist = max(ls[i + 1] - ls[i], max_dist)

print(K - max(max_dist, K - ls[-1] + ls[0]))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

a.sort()
b=[a[0]+k-a[n-1]]
ans = a[0]+k-a[n-1]

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

ans-=max(b)

print(ans)

Submission status: Accepted

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

ans=min(ans,K-K+abs(A[0]-A[-1]),K-abs(A[0]-A[1]))

ans=min(ans,K-abs(A[-1]-A[-2]),K-abs(K-A[-1]))

print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    k, n = na()
    mid = k // 2
    a = na()

    ma = [] #minus
    pa = [] #pls
    for i in range(n - 1):
        x = a[i+1] - a[i]
        if x < 0:
            x = k - abs(x)
        ma.append(x)
    
    for i in range(n - 1, 0, -1):
        x = a[i] - a[i - 1]
        if x < 0:
            x = k - abs(x)
        pa.append(x)

    x = a[-1] - a[0]
    if x < 0:
        x = k - abs(x)
    ma.append(x)
    x = a[0] - a[-1]
    if x < 0:
        x = k - abs(x)
    pa.append(x)
    ma.sort()
    pa.sort()
    
    print(min(sum(ma[:-1]), sum(pa[:-1])))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a_list = list(map(int,input().split()))
result_list = []
for i in range(n):
  if i == 0:
    a_ = a_list[-1] - k
    result =  k - (a_list[0] -a_)
    result_list.append(result)
  else: 
    result =  k - (a_list[i] -a_list[i-1])
    result_list.append(result)
print(min(result_list))

Submission status: Accepted

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

if DEBUG:
    print("K = %d  N = %d"%(K,N))
    print("A = \n"+str(A))
    print("Distance = \n"+str(distance))
print(sum(distance)-distance[distance.index(max(distance))])

Submission status: Accepted

Solution code:
import sys, re
import math
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

# -*- coding: utf-8 -*-

k, n = MAP()
a = LIST()

b = []

for x in range(0, n - 1):
    b.append(a[x + 1] - a[x])

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

print(k - max(b))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
*A, = map(int, input().split())
A = A + [K+A[0]]
chk = [0] * N
for i in range(N):
    chk[i] = A[i+1] - A[i]
chk.sort(reverse=True)
print(sum(chk[1:]))

Submission status: Accepted

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

K, N = map(int, readline().split())
A = list(map(int, readline().split()))
A_ = [a + K for a in A]

ans = 10**9 + 7
min1, max1 = min(A), max(A)

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

    if i != N - 1:
        if A[i] - A[i + 1] > 0:
            CCW = A[i] - A[i + 1]
        else:
            CCW = A[i] + K - A[i + 1]
    else:
        CCW = A[i] - A[0]

    ans = min(ans, min(CW, CCW))

print(ans)

Submission status: Accepted

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


def main(k, n, a):
    distance = []
    for i in range(n):
        dist = abs(a[i] - a[i - 1])
        if dist >= k / 2:
            dist = k - dist
        distance.append(dist)

    distance.pop(distance.index(max(distance)))

    res = sum(distance)

    return res


print(main(K, N, A))

Submission status: Accepted

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

ans = 0
max = 0 
for i in range(N-1):
	diff = Aj[i+1] - Aj[i]
	ans += diff
	if max < diff:
		max = diff
diff = K-Aj[N-1]+Aj[0] 
ans += diff
if max < diff:
	max = diff
ans -= max
print(ans)

Submission status: Accepted

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

print(k-c)

Submission status: Accepted

Solution code:
import numpy

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

a.sort()

diff = []
for i in range(len(a) - 1):
    diff.append(a[i + 1] - a[i])
diff.append(k - a[-1] + a[0])

max_diff = max(diff)
print(k - max_diff)

Submission status: Accepted

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

#print(len(A))
#print(A[0]-A[1])
#Maxの差
Ma=0
for i in range(N-1):
    if Ma < A[i+1]-A[i]:
        Ma = A[i+1]-A[i]

if Ma < A[0]+ K-A[N-1]:
    Ma = A[0]+ K-A[N-1]

#k-n
ans = K-Ma
#print(Ma)
print(ans)

Submission status: Accepted

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


dist_list = []

for i in range(len(a_list)-1):
    dist_list.append(a_list[i+1]-a_list[i])

dist_list.append(K+a_list[0]-a_list[-1])

max_dist = max(dist_list)

print(K-max_dist)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import deque

k, n = map(int, input().split())
#d = deque()
#d = (d.append(int(_)) for _ in input().split())
d = [int(_) for _ in input().split()]

ans = k

for a in range(n):
    b = a - 1
    if b < 0:
        b += n
    #tmp = d.index(0) - d.index(-1)
    tmp = d[b] - d[a]
    if tmp < 0:
        tmp += k
    if tmp < ans:
        ans = tmp
    #d.rotate()

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
inputNumber = input()
K = int(inputNumber.split(" ")[0])
N = int(inputNumber.split(" ")[1])

inputNumber = input()
A=inputNumber.split(" ")

A_Length=len(A)

for i in range(A_Length):
    A[i] = int(A[i])

result=0

for i in range(A_Length):
    if i == A_Length - 1:
        if result < (K - A[i]) + A[0]:
            result = (K - A[i]) + A[0]
    else:
        if result < A[i+1] - A[i]:
            result = A[i+1] - A[i]

#if result < (K - A[i]) + A[0]:
#    result = (K - A[i]) + A[0]


result = K - result
print(result)

Submission status: Accepted

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

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

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


print(sum(dist))

Submission status: Accepted

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

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

    if distance > max_distance:
        max_distance = distance

print(K - max_distance)

Submission status: Accepted

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

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

m = an[0] + (k - an[-1])
for i in range(1, n):
    d = abs(an[i] - an[i-1])
    m = max(m, d)

print(k-m)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = k
b = a[0] + (k-a[-1])
for i, v in enumerate(a):
    x = 0
    if i == 0:
        x = max(abs(a[i+1]-v), b)
    elif i == len(a)-1:
        x = max(b, abs(v-a[i-1]))
    else:
        x = max(abs(a[i+1]-v), abs(v-a[i-1]))
    ans = min(ans, k-x)
print(ans)

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)

s = np.sort(a)
temae = 0
oku = 0
m = k
for i in range(n):
  if s[(i-1)%n] - s[i] < 0:
    c1 = k+s[(i-1)%n] - s[i]
  else:
    c1 = s[(i-1)%n] - s[i]
  if s[i] - s[(i+1)%n] > 0:
    c2= s[i] - s[(i+1)%n]
  else:
    c2 = k + s[i] - s[(i+1)%n]
  if m > c1:
    m = c1
  if m > c2:
    m = c2
print(m)

Submission status: Accepted

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

def main(K, N, lst_A):

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

    sum_diff = sum(diff_dis)
    answer = sum(diff_dis)

    for diff in diff_dis:
        _answer = sum_diff - diff
        answer  = min(answer, _answer)

    return answer

if __name__ == "__main__":

    answer = main(K, N, lst_A)
    print(answer)

Submission status: Accepted

Solution code:
from functools import reduce
import numpy

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

D = numpy.diff(A) 
D = numpy.append(D, K - sum(D))

print(K - max(D))

Submission status: Accepted

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

A=list(map(int,input().split()))
d=[]

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

Submission status: Accepted

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

A = list(map(int, input().split()))
D = []

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

kankaku = max(D)
saitan = K - kankaku

print(saitan)

Submission status: Accepted

Solution code:
import numpy as np

K, N = map(int, input().split())
A = np.array(list(map(int, input().split())))
tmp = K - A[-1]

for i in range(N-1, 0, -1):
    A[i] = A[i] - A[i-1]

A[0] += tmp

A = np.sort(A)
print(np.sum(A[:-1]))

Submission status: Accepted

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

Submission status: Accepted

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

X = A[1] - A[0]
i = 1
while i < N-1:
    Y = A[i+1] - A[i]
    if X < Y:
        X = Y
    i += 1
Z = A[0] + K - A[N-1]
if X < Z:
    X = Z

print(K - X)

Submission status: Accepted

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

for i in range(len(A)):
    if A[i] != A[0]:
        r = A[i] - A[i-1]
    if A[i] != A[N-1]:
        l = A[i+1] - A[i]
    if ans > K - max(r,l):
        ans = K - max(r,l)
print(ans)

Submission status: Accepted

Solution code:
kk,nn = map(int, input().split(' '))
ll = list(map(int, input().split(' ')))
ll.append(kk)

aida = []
for j in range(len(ll)-1):
    #print(j)
    if j==0: aida.append(ll[-1]-ll[-2]+ll[0])
    else: aida.append(ll[j]-ll[j-1])
print(sum(aida)-max(aida))

Submission status: Accepted

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

Submission status: Accepted

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

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

distance.sort()
longest = distance.pop()

print(K - longest)

Submission status: Accepted

Solution code:
import numpy as np

K,N=list(map(int, input().split()))
a = np.array(list(map(int, input().split())))
diff_a=np.diff(a,n=1)
max_index=np.argmax(diff_a)
temp_max=diff_a[max_index]
last_first=K-(a[-1]-a[0])
if temp_max>last_first:
  print(int(K-temp_max))
else:
  print(int(K-last_first))

Submission status: Accepted

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

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

A = sorted(a)

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

if maxl <= 0:
    maxl = -1 * maxl


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

    if l <= 0:
        l = -1 * l
    
    if maxl < l:
        maxl = l

ans = K - maxl

print(ans)

Submission status: Accepted

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

maxD=0

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

print(K-maxD)

Submission status: Accepted

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

tmp = [0] * n
for i in range(n):
    tmp[i] = li[i] - k
li = li + tmp
li.sort()
ans = 10**9
for i in range(len(li) - n+1):
    ans = min(ans, abs(li[i] - li[i + n-1]))
print(ans)

Submission status: Accepted

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

l.append(k+l[0]);
maxdiff=0
for i in range(n):
	if(l[i+1]-l[i]>maxdiff):
		maxdiff=l[i+1]-l[i]
print(k-maxdiff)

Submission status: Accepted

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

dis =[]

dis.append(an[0] + K - an[len(an)-1])

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

work = sorted(dis)
total = 0

for i in range(N-1):
    total += work[i]

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
base = input().split()
k = int(base[0])
n = int(base[1])
house = input().split()
lenlist = []
a = int(house[0])
b = k-int(house[-1])
one = a+b
lenlist.append(one)
for i in range(n-1):
    lenlist.append(int(house[i+1])-int(house[i]))
print(k-max(lenlist))

Submission status: Accepted

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

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

ans = values[n-1] - values[0]

answer = [0]*n
answer[0] = ans

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

Submission status: Accepted

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

Submission status: Accepted

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

d = K - (j[-1] - j[0])

for n in range(1, N):
    if j[n] - j[n-1] > d:
        d = j[n] - j[n-1]

print(K - d)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


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


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    K, N = MI()
    A = LI()
    A.append(A[-1] + (K - A[-1]) + A[0])
    N = N + 1
    diff_sum = 0
    diff = -1
    target = -1

    for i in range(N-1):
        v = abs(A[i] - A[i+1])
        diff_sum += v

        if diff < v:
            diff = v
            target = i

    # print(A, diff, diff_sum, target)
    print(diff_sum - diff)

    # a = 0
    # for i in range(target+1, N-1):
    #     a += abs(A[i] - A[i+1])
    #     print('a', a, i, i + 1)

    # for i in range(0, target):
    #     a += abs(A[i] - A[i+1])
    #     print('a', a, i, i + 1)

    # b = 0
    # for i in range(target, 0, -1):
    #     b += abs(A[i] - A[i-1])
    #     print('b', i, i - 1)

    # for i in range(N-1, target+1, -1):
    #     b += abs(A[i] - A[i-1])
    #     print('b', i, i - 1)

    # print(a, b)
    # print(min(a, b))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

maxdis = 0
for i in range(len(A)-1):
  if maxdis < A[i+1] - A[i]:
    maxdis = A[i+1] - A[i]
if maxdis < (K - A[-1]) + A[0]:
  maxdis = (K - A[-1]) + A[0]
print(K-maxdis)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int,input().split())
As = list(map(int, input().split()))
As = np.array(As)
Bs = np.copy(As)
As = np.append(As, 0)
Bs = np.append(0, Bs)
Cs = As-Bs
Cs = np.delete(Cs, 0, 0)
Cs = np.delete(Cs, -1, 0)
Cs = np.append(Cs, As[0]+K-As[-2])
idx = np.argmax(Cs)
print(K-Cs[idx])

Submission status: Accepted

Solution code:
import numpy as np
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
A_diff = np.diff(A)
A_diff_sum = np.sum(A_diff)
max_diff = np.max([np.max(A_diff), (N - A[-1] + A[0])])
ans = A_diff_sum - max_diff + (N - A[-1] + A[0])
print(ans)

Submission status: Accepted

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

L = min(A[N-1] - A[0],K - A[1] + A[0])
L1 = min(A[N-1] - A[0],K - A[N-1] + A[N-2])
L = min(L,L1)
for i in range(1,N-1):
    n = min(K-A[i]+A[i-1],K-A[i+1]+A[i])
    L = min(L,n)
print(L)

Submission status: Accepted

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

List = [int(i) for i in input().split()]
dis =[0] *N
for j in range(N):
  if j == N-1:
    dis[j]=K-List[j]+List[0]
  else:
    dis[j] = List[j+1]-List[j]
ans = sum(dis)-max(dis)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

A.append(K + A[0])

diff = np.diff(A).tolist()
diff = sorted(diff)

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

Submission status: Accepted

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

a.append(a[0] + k)
x = k - a[-2]
a.insert(0, x)

li = []
for i in range(1, n+1):
    d = min(k - abs(a[i]-a[i-1]), k - abs(a[i]-a[i+1]))
    li.append(d)

ans = min(li)
print(ans)

Submission status: Accepted

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

mini = K
right = left = K
for i in range(N):
	l_begin = i-1
	r_begin = i+1
	if i == 0:
		l_begin = N-1
		right = A[l_begin] - A[i]
		left = K - (A[r_begin]-A[i])
	elif i == N-1:
		r_begin = 0
		right = K - (A[i]-A[l_begin])
		left = A[i] - A[r_begin]
	else:
		right = K - (A[i]-A[l_begin])
		left = K - (A[r_begin]-A[i])
	local = min(right, left)
	if local < mini:
		mini = local
		

print(mini)

Submission status: Accepted

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

Submission status: Accepted

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

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

if Asa <= ((K - A[N -1]) + A[0]):
        Asa = (K - A[N -1]) + A[0]

print(K - Asa)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

d_min = K
for i in range(N):
    start = A[i]
    if i + 1 < N:
        end = A[i + 1]
    else:
        end = A[0]
    rest = min(abs(start - end), K - abs(start - end))
    d = K - rest
    d_min = min(d_min, d)

print(d_min)

Submission status: Accepted

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

diff = []

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

diff.remove(max(diff))

print(sum(diff))

Submission status: Accepted

Solution code:
import numpy as np

def main():
    k, n = map(int,input().split())
    a = list(map(int, input().split()))
    a.append(k+a[0])
    #print(k,n)
    #print(a)
    a2 = []
    for i in range(len(a)-1):
        a2.append(a[i+1]-a[i])
    a2 = np.array(a2)
    print(k-np.max(a2))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

a.sort()
b = list()
for i in range(N-1):
    x = a[i+1] - a[i]
    b.append(x)
b.append(K-a[-1]+a[0])
print(K-max(b))

Submission status: Accepted

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

Submission status: Accepted

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

A_dif = [0]*n
A_dif[0] = A[0] + k - A[n-1]

for i in range(1,n):
  A_dif[i] = A[i]-A[i-1]

ans = k - max(A_dif)

 
print(ans)

Submission status: Accepted

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

md = l[0] + k-l[-1]

mx = md
before = l[0]
for i in l[1:]:
    if i-before > mx:
        mx = i-before
    before = i

print(k-mx)

Submission status: Accepted

Solution code:
K , N = map ( int , input().strip().split(" " ) ) ; 
arr = list ( map ( int , input().strip().split(" ") ) ) ; 


y = K ;
for p1 in range ( N ) :
	p2 = ( p1 -1 + N )  %  N ;
	if arr[p1] > arr[p2] :
		l1 = K - arr[p1] + arr[p2] ;	
	else :
		l1 = arr[p2] - arr[p1] ;
	
	p2 = ( p1 + 1 + N ) % N ; 
	if arr[p1] > arr[p2] : 
		l2 = arr[p1] - arr[p2] ;
	else:
		l2 = K - arr[p2] + arr[p1] ;
	l = min ( l1 , l2 ) ;
	y = min ( y , l1 ) ;
print ( y ) ;

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
max1=0
list1=list(map(int,input().split()))
list2=[0]*(K+1)
for i in range(K):
    list2[i]=list1[i]
list2[K]=list1[0]+N
list3=[0]*K
for i in range(K):
    list3[i]=list2[i+1]-list2[i]

max1=max(list3)

answer=N-max1

print(answer)

Submission status: Accepted

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

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

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



l_max = max(l)

ans = sum(l)-l_max

print(ans)

Submission status: Accepted

Solution code:
def inputs(): return [int(x) for x in input().split()]
from collections import defaultdict
k,n = inputs()
a = inputs()
ans = min(a[-1]-a[0],k-(a[1]-a[0]))
for i in range(n-1):
    if i!=0:
        ans = min(ans,min(k-abs(a[i+1]-a[i]),k-abs(a[i-1]-a[i])))
    #print(ans,i)
print(ans)

Submission status: Accepted

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


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

e = k + a[0] - a[-1]
if e >= d:
    d = e




print(k-d)