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()))
degree_max = 0

for i in range(len(A)):

    if i+1 == len(A):
        degree = (k - A[i]) + A[0]

        if degree_max < degree:
            degree_max = degree
    else:
        degree = A[i+1] - A[i]
        if degree_max < degree:
            degree_max = degree
ans = k - degree_max
print(ans)

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] = K-A[-1]+A[0]
B.sort()
print(sum(B[:-1]))

Submission status: Accepted

Solution code:
kn=input().rstrip().split(" ")
k=int(kn[0])
n=int(kn[1])
numbers=input().rstrip().split(" ")
ans=0
for i in range(n-1):
    delta=int(numbers[i+1])-int(numbers[i])
    if delta>=ans:
        ans=delta
lasdelta=int(numbers[0])+k-int(numbers[n-1])
if lasdelta>=ans:
    ans=lasdelta
#print(ans)
print(k-ans)

Submission status: Accepted

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

for i in range(n - 1):
    if i == 0:
        j = n - 1
        r = min(r, A[j] - A[i])
    else:
        j = i + 1
        r = min(r, A[i] + k - A[j])
print(r)

Submission status: Accepted

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

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

print(K-max(dis))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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


max_value = max(lag)
max_index = lag.index(max_value)
lag.pop(max_index)
print(sum(lag))

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(sum(d) - max(d))

Submission status: Accepted

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

MaxD = 0
B = sorted(A)
for i in range(N-1):
    D = B[i+1] - B[i]
    if D > MaxD:
        MaxD = D

if K - B[N-1] + B[0] > MaxD:
    MaxD = K-B[N-1]+B[0]

print(int(K-MaxD))

Submission status: Accepted

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

d=[]
for i in range(n-1):
    d.append(c[i+1]-c[i])

d.append(k-c[n-1]+c[0])
max = max(d)
sum = sum(d)
print(sum-max)

Submission status: Accepted

Solution code:
# coding:utf-8

K, N = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
dist = []
ans = []

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

s = sum(dist)

for i in range(N):
    ans.append(s - dist[i])

print(min(ans))

Submission status: Accepted

Solution code:
# 湖の円周、家の数
K, N = map(int, input().split())

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

A_list = sorted(A_list)

max_d = 0

for i in range(N-1):
    d = A_list[i + 1] - A_list[i]

    if d > max_d:
        max_d = d

l = K - A_list[N -1] + A_list[0]
if l > max_d:
    max_d = l


print(K - max_d)

Submission status: Accepted

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

saidai_kyori = 0
sou_kyori = 0
for i, position in enumerate(house_list):
    kyori = 0
    if i == 0:
        kyori = K + house_list[0] - house_list[-1]
    else:
        kyori = house_list[i] - house_list[i-1]

    if kyori > saidai_kyori:
        saidai_kyori = kyori
    
    sou_kyori += kyori

print(sou_kyori - saidai_kyori)

Submission status: Accepted

Solution code:
kn=input().split()
k=int(kn[0])
n=int(kn[1])
an=input().split()
num_list=[]
for a in an :
  num_list.append(int(a))
count_list=[]

for i in range(n) :
  value=num_list[i-1]-num_list[i]
  if value<0 :
    value+=k
  
  count_list.append(value)
  
print(min(count_list))

Submission status: Accepted

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

A.sort()
#print(A)

B = A[1:]
B.append(A[0])
#print(B)

SUB = [b-a for (a, b) in zip(A,B)]

SUB[-1] += K
#print(SUB)

print(sum(SUB)-max(SUB))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
list_A =list(map(int,input().split()))
mx = 0
for i in range(n-1):
  a = list_A[i+1]-list_A[i]
  mx = max(mx,a)
 #最後のパターン
b = (list_A[0]-0)+(k-list_A[-1])
mx = max(mx,b)
print(k-mx)

Submission status: Accepted

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

Submission status: Accepted

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

l = []
# [0, 1] -> [0]
for i in range(N - 1):
    x = A[i + 1] - A[i]
    l.append(x)

x_last = (K - A[-1]) + A[0]
l.append(x_last)
#print(x_last)

max_value = max(l)
#max_index = l.index(max_value)

ans = K - max_value

print(ans)

Submission status: Accepted

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

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

dlst.remove(max(dlst))
ans = sum(dlst)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

l.sort()
for i in range(n-1):
  if a > l[i+1]-l[i]:
    continue
  else:
    a = l[i+1]-l[i]
    
if l[0] + k - l[n-1] > a:
  a = l[0] + k - l[n-1]

print(k-a)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

D=[0]*(N+1)
for i in range(N-1):
    D[i+1]=A[i+1]-A[i]
D[-1]=K-A[-1]+A[0]
D.extend(D[1:])
D=list(accumulate(D))

ans=float('inf')
for l in range(1,N+1):
    r=l+N-1
    ans=min(ans,abs(D[r]-D[l]))
print(ans)

Submission status: Accepted

Solution code:
# x = int(input())
k,n = map(int, input().split())
a = list(map(int, input().split()))
b=[0]*(len(a)-1)
c=[0]*(len(a)-1)
mx =0 
index =0
flag = True
for i in range(len(a)-1):
    if i==0 :
        mae = (k-a[-1])+a[0]
        ushiro = a[1]-a[0]
        if mx < mae:
            mx = mae
            index =i
            flag =True
        elif mx < ushiro:
            mx = ushiro
            index = i
            frag =False
    else:
        mae = a[i]-a[i-1]
        ushiro = a[i+1] - a[i]
        if mx < mae:
            mx = mae
            index =i
            flag =True
        elif mx < ushiro:
            mx = ushiro
            index = i
            frag =False

print(k-mx)

Submission status: Accepted

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

print(K-tem)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
k,n = map(int,input().split())
al = input().split()
al2 = [int(i) for i in al]
distance = []
for i in range(len(al)-1):
    distance.append(al2[i+1]-al2[i])
distance.append(((al2[0])+(k-al2[-1])))
distance.remove(max(distance))
print(sum(distance))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
a=input().split()
A=[int(b) for b in a]
answers=[]
for z in range(N-1):
    sa=A[z+1]-A[z]
    answers.append(sa)
answers.append(K-A[-1]+A[0])
kotae=max(answers)
print(K-kotae)

Submission status: Accepted

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

k, n = map(int, input().split())
a = np.array(input().split(), dtype='int')

b = np.zeros((n), dtype='int')

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

ans = k - np.max(b)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    k,n = map(int,input().split())
    arr = list(map(int,input().split()))
    ans = float('inf')
    prev = 0
    for i in range(n):
        if i == n-1:
            if prev == 0:
                total = arr[-1]-arr[i]
            else:
                total = k-arr[i]+prev
        else:
            if i == 0:
                total = arr[-1]-arr[i]
            else:
                total = min(k-arr[i]+prev,arr[i]+k-arr[i+1])

        prev += arr[i]
            
        ans = min(ans,total)

    print(ans)
                            
main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

dist = 0
num = 0
for i in range(n):
    temp = s[i+1] - s[i]
    if temp > dist:
        dist = temp

print(k - dist)

Submission status: Accepted

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

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

print(k - max(d))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(i for i in sorted(x)[:-1]))

Submission status: Accepted

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

distance = [0] * N

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

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

print(K - max(distance))

Submission status: Accepted

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

print(K - max_diff)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

A.append(k + A[0])

dists = []
for i in range(n):
    dist = A[i+1] - A[i]
    dists.append(dist)
print(k - max(dists))

Submission status: Accepted

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

dist = 0

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

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

print(K - dist)

Submission status: Accepted

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

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

Submission status: Accepted

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

min = int(A[N - 1]) - int(A[0])

for i in range(1, len(A)):
    d = int(A[i - 1]) - int(A[i]) + K
    if d < min:
        min = d

print(min)

Submission status: Accepted

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

diff = []
for i, a in enumerate(a_lst[:-1]):
    diff.append(a_lst[i+1] - a)
diff.append((a_lst[0] + k) - a_lst[-1])

res = sum(diff) - max(diff)
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [a+K for a in A]
A.extend(B)

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

cum = [0]
cum.extend(list(accumulate(B)))

ans = float('inf')

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

print(ans)

Submission status: Accepted

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

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

start = 0
max_sa = 0
for i in range(n):
	if i+1 == n:
		sa = a[(i+1)%n] - (a[i%n]-k)
	else:
		sa = a[(i+1)%n] - a[i%n]

	if max_sa < sa:
		max_sa = sa
		start = (i+1)%n

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

print(ans)

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-1):
  distance[i] = a[i+1] - a[i]
distance[n-1] = k-a[n-1] + a[0]

distance.sort(reverse=True)
print(sum(distance[1:]))

Submission status: Accepted

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

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


D.remove(max(D))

print(sum(D))

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import numpy as np

def solve(_k, _A):
  ans = np.inf
  tmp_list = _A + [i+_k for i in _A]
  for i in range(len(_A)):
    dist = tmp_list[len(_A)+i-1] - tmp_list[i]
    if ans > dist:
      ans = dist
  return ans

if __name__ == "__main__":
  _k,_n = map(int, input().split())
  _A = [int(i) for i in input().split()]
  print(int(solve(_k, _A)))

Submission status: Accepted

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

a = A[0]
A = [x - a for x in A]

cur = 0
res = float('inf')
l = len(A)
for i in range(len(A)):
    x = A[i]

    if i == l - 1:
        d = abs(K - x)
    else:
        y = A[i + 1]
        d = y - x

    if d > cur:
        res = min(K - d, res)
        cur = d

print(res)

Submission status: Accepted

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

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

ans = 0
for i in range(N):
    if not ans:
        ans = min(abs(A[i - 1] - A[i]), abs(A[i] + (K - A[i + 1])))
    else:
        d = min(abs(K + A[i - 1] - A[i]), abs(A[i] + (K - A[i + 1]))) if i != N - \
            1 else min(abs(A[0] - A[i]), abs(A[i] + (K - A[0])))
        ans = min(ans, d)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

MAX=A.pop(N-1)
pre=MAX
counter=[]

for a in reversed(A):
    counter.append(pre - a)
    pre=a

counter.append(pre + K - MAX)
counter.sort()
print(sum(counter[:N-1]))

Submission status: Accepted

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

Submission status: Accepted

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

print(m-a)

Submission status: Accepted

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

AList.sort()

maxIso = -1

for indx in range(N-1):
    if (AList[indx+1] - AList[indx]) > maxIso:
        maxIso =  AList[indx+1] - AList[indx]

if (AList[0] + (K - AList[N-1])) > maxIso:
    maxIso = AList[0] + (K - AList[N-1])

print(K - maxIso)

Submission status: Accepted

Solution code:
k,n = (int(x) for x in input().split())
la = list(map(int, input().split()))

l1 = la[:-1]
l2 = la[1:]

ls= [x - y for (x, y) in zip(l2, l1)]
ls.append(la[0]+k-la[n-1])

print(k-max(ls))

Submission status: Accepted

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

ans = min(a[-1] - a[0], K - a[-1] + a[-2])
for i in range(1, N - 1):
    ans = min(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()))

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

print(K - m)

Submission status: Accepted

Solution code:
import numpy
k,n=map(int,input().split())
h=numpy.array(input().split(),dtype=int)
i=numpy.append(h[1:],k+h[0])
print(k-max(i-h))

Submission status: Accepted

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

a = input().split()

point = k - int(a[-1]) + int(a[0])

for i in range(n-1):
    b = int(a[i+1]) - int(a[i])
    if point < b:
        point = b
sum_a = k - point
print(sum_a)

Submission status: Accepted

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

dis = list(map(int, input().split(' ')))
dis = [__ - dis[0] for __ in dis]
dis.insert(0, 0)
dis = np.array(dis[1:]) - np.array(dis[:-1])
dis = list(dis)
dis.append(K - sum(dis))
#print(dis)
dis = sorted(dis)

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

Submission status: Accepted

Solution code:
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    K, N = input_number_list()
    A = input_number_list()
    A.sort()
    ans = A[-1] - A[0]
    for i in range(1, N - 1):
        l = A[i] + (K - A[i + 1])
        r = K - A[i] + A[i - 1]
        ans = min(ans, l, r)
    ans = min(ans, K - A[-1] + A[-2])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 10**9
for i in range(N):
	right = A[i-1] - A[i]
	right += K if right < 0 else 0
	left = A[i] - A[0] if i == N - 1 else A[i] + K - A[i+1]
	ans = min(ans, min(right, left))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(sum(sorted(sabun)[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k - max(d))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    k, n = tuple([int(i) for i in input().split()])
    distances = [int(i) for i in input().split()]

    between = [0] * len(distances)
    between[0] = distances[0] + k - distances[-1]

    mx = 0
    for i in range(1, len(distances)):
        between[i] = distances[i] - distances[i - 1]
        mx = max(mx, between[i])

    total = sum(between)
    res = total
    for i in range(len(between)):
        res = min(res, total - between[i])

    print(res)

Submission status: Accepted

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

print(K-maxnum)

Submission status: Accepted

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

max1 = 0

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

max1 = max(max1, k - (a[-1] - a[0]))

print(k - max1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 21:18:11 2020

@author: liang
"""

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

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

temp = A[0] + K - A[N-1]
if temp > max_distance:
    max_distance = temp

result = K - max_distance

print(result)

Submission status: Accepted

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

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

max_diff = max(A_diff)
print(int(K - max_diff))

Submission status: Accepted

Solution code:
import copy

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

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

# print(k, n)
# print(a)

dis = copy.deepcopy(a)
dis[-1] = k - a[-1] + a[0]
for i in range (len(a)-1):
  dis[i] = a[i+1] - a[i]

# print(dis)

max_d = max(dis)
ans = sum(dis) - max_d

print(ans)

Submission status: Accepted

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

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

B = []
max_len = 0

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

sum_v = sum(B)
print(sum_v-max_len)

Submission status: Accepted

Solution code:
def main():
  k, n = map(int, input().split())
  max_range = 0
  first = True
  first_range = 0
  pre_range = 0
  for i in [int(i) for i in input().split()]:
    if first:
      first = False
      first_range = i
      pre_range = i
      continue
    
    if max_range < i - pre_range:
      max_range = i - pre_range
    
    pre_range = i
  
  if max_range < first_range + (k - pre_range):
    max_range = first_range + (k - pre_range)
  
  return k - max_range

print(main())

Submission status: Accepted

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

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

print(K-d)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))

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


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = list(map(int, input().split()))
houses = list(map(int, input().split()))
d = []

for i in range(0, n-1):
    d.append(houses[i+1] - houses[i])
d.append(k - houses[-1] + houses[0])
ans = sorted(d)
del(ans[-1])
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

D = [None] * N
last = A[-1] - K
for i, a in enumerate(A):
  diff = (a - last, i)
  D[i] = diff
  last = a

i = max(D)[1]
if i == 0:
  print(A[i - 1] - A[i])
else:
  print(K - A[i] + A[i - 1])

Submission status: Accepted

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

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

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