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

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

Submission status: Accepted

Solution code:
import numpy as np

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

K = a[0]
N = a[1]

c = np.zeros(N)

for i in range(N-1):
  c[i] = abs(b[i+1] - b[i])
  
c[N-1] = abs(K - b[N-1] + b[0])

r = max(c)

print(int(K-r))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

for i in range(n):
    a.append(a[i]+k)
#print(a)

lis = []
for i in range(n):
    lis.append(a[i+n-1]-a[i])
#print(lis)

print(min(lis))

Submission status: Accepted

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

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

print(min(len))

Submission status: Accepted

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

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

print(K-max(x))

Submission status: Accepted

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

Submission status: Accepted

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

def get_dis(distance_list):
    longest = 0
    sum = 0
    for num in distance_list:
        sum += num
        if num > longest:
            longest = num
    return (sum-longest)

def cover(k, n, position_list):
    distance_list = []
    for i in range(len(position_list)-1):
        distance = position_list[i+1] - position_list[i]
        distance_list.append(distance)
    # 最初と最後の距離
    distance_list.append(position_list[0]+k-position_list[-1])
    ans = get_dis(distance_list)
    return ans

print(cover(k, n, position_list))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc160/tasks/abc160_c

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

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

print(K - max_distance)

Submission status: Accepted

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

Submission status: Accepted

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

    max_dist = 0
    A.append(K + A[0])

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

    print(K - max_dist)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

maxdist = 0
for i in range(N - 1):
  dist = dist_list[i + 1] - dist_list[i]
  maxdist = max(maxdist, dist)
else:
  dist = (dist_list[0] + K) - dist_list[-1]
  maxdist = max(maxdist, dist)

print(K - maxdist)

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(A[0] + k - A[n-1])
print(sum(B) - max(B))

Submission status: Accepted

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

maxdist = 0
for i in range(n - 1):
    dist = A[i+1]-A[i]
    maxdist = max(maxdist, dist)
    # print(f'maxdist=max({maxdist},{dist})={max(maxdist,dist)}')
else:
    dist = A[0] + k - A[n-1]
    maxdist = max(maxdist, dist)
    # print(f'maxdist=max({maxdist},{dist})={max(maxdist,dist)}')

ans = k - maxdist
# print(maxdist)
print(ans)

Submission status: Accepted

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

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

a = K-A[N-1]

sub.append(a+A[0])
ans = K - max(sub)

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split(" "))
houses = list(map(int, input().split(" ")))
dif_houses = [houses[x+1]-houses[x] for x in range(k-1)]
dif_houses.append(houses[0]-houses[k-1]+n)

max_dif = max(dif_houses)
print(n-max_dif)

Submission status: Accepted

Solution code:
a,b= map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
ar.append(a+ar[0])
count = 0
mx = 0
for i in range(len(ar)-1):
    count += ar[i+1] - ar[i]
    if mx < ar[i+1] - ar[i]:
        mx = ar[i+1] - ar[i]
print(count - mx)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
    a.append(a[i]+k)

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

print(k-maxi)

Submission status: Accepted

Solution code:
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
i = 1
max_diff = 0
while i < n:
    if a[i] - a[i - 1] > max_diff:
        max_diff = a[i] - a[i - 1]
    i += 1
if k - a[n - 1] + a[0]> max_diff:
    max_diff = k - a[n - 1] + a[0]
print(k - max_diff)

Submission status: Accepted

Solution code:
import numpy as np

K,N = map(int, input().split())
An_list = np.array(input().split(), dtype=int)

An_delta_list = np.diff(An_list)
An_delta_list = np.append(An_delta_list, (K - An_list[-1])+An_list[0])
An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#初期条件
K_N = list(map(int, input().split()))
A_line = list(map(int, input().split()))

#メイン実行部
length = []
for i in range(K_N[1] - 1):
  length.append(A_line[i + 1] - A_line[i])
else:
  length.append(K_N[0] - sum(length))

min_length = K_N[0] - max(length)
print(min_length)

Submission status: Accepted

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

e = []
max = 0

for i in range(n-1) :
  e.append(houses[i + 1] - houses[i])
e.append(perimeter - houses[n-1] + houses[0])

for i in range(n):
  if e[i] > max:
    max = e[i]

print(perimeter - max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq
def main():
    k,n = map(int,input().split())
    a_n = map(int,input().split())
    l_1 = []
    l_2 = []
    for a_i in a_n:
        heapq.heappush(l_1,a_i)
        heapq.heappush(l_2,a_i)
    a_1 = heapq.heappop(l_2)
    heapq.heappush(l_2,a_1 + k)
    dif_m = 0
    for _ in range(n):
        start = heapq.heappop(l_1)
        goal = heapq.heappop(l_2)
        dif = goal - start
        if dif_m < dif:
            dif_m = dif
    print(k - dif_m)
                


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
kn=input().split()
k=int(kn[0])
n=int(kn[1])
disfn=[int(i) for i in input().split()]
dismax=disfn[0]+k-disfn[-1]
for i in range(n-1):
	if dismax<disfn[i+1]-disfn[i]:
		dismax=disfn[i+1]-disfn[i]
print(k-dismax)

Submission status: Accepted

Solution code:
from sys import exit, stdin
K, N = [int(_) for _ in stdin.readline().rstrip().split()]
A = [int(_) for _ in stdin.readline().rstrip().split()]
distances = []
for i in range(N-1):
    distances.append(A[i+1]-A[i])
distances.append(A[0]+K-A[N-1])
print(K-max(distances))

Submission status: Accepted

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

max_diff = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np


##get int
first_input = list(map(lambda x:int(x), input().split(" ")))
second_input = list(map(lambda x:int(x), input().split(" ")))

##get float
#first_input = list(map(lambda x:float(x), input().split(" ")))
#second_input = list(map(lambda x:float(x), input().split(" ")))

##get string
#first_input = input().split(" ")

K = first_input[0]
N= first_input[1]

A_list = second_input

A_list.append(K+A_list[0])

dist_list = np.array(A_list)[1:]-np.array(A_list)[:-1]

print(sum(dist_list)-max(dist_list))

Submission status: Accepted

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

K = a[0]
N = a[1]

A_nplus1 = K + A[0]
A.append(A_nplus1)

max = -1
max_id = -1

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

Submission status: Accepted

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

print(k - max(b))

Submission status: Accepted

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

a = list(map(int, input().split()))
a.append(a[0] + k)
# (K - 最大辺の長さ)を求めたい

max = 0
for x, y in zip(a, a[1:]):
    if max < y-x:
        max = y-x

print(k-max)

Submission status: Accepted

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

print(
  n - \
    max(
      [a_2 - a_1 \
          for a_1, a_2 in \
          zip(
            a_list[:-1], 
            a_list[1:]
          )
      ] + \
      [n - a_list[-1] + a_list[0]]
    )
)

Submission status: Accepted

Solution code:
l, n = (int(i) for i in input().split());
a = [int(i) for i in input().split()];
mx = 0;
for i in range(1, n):
    mx = max(mx, a[i] - a[i - 1]);

mx = max(mx, a[0] + l - a[n - 1]);

print(l - mx);

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
arrayNM = list(map(int, input().split()))
array = list(map(int, input().split()))
route = [array[0] + arrayNM[0] - array[arrayNM[1] - 1]]
for i in range(1,arrayNM[1]):
  route.append(array[i] - array[i-1])
  
Max = max(route)
maxIndex = route.index(Max)
distance = 0

for i in range(0,maxIndex):
  distance += route[i]

for i in range(maxIndex + 1, arrayNM[1]):
  distance += route[i]

print(distance)

Submission status: Accepted

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

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

    print(sum(dist) - max(dist))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    A = list(map(int,input().split()))
    ans = K
    for i in range(0,N,1):
        s = A[i]
        if i == 0:
            g = A[N-1]
            ans = min(ans, g-s)
        else:
            g = A[i-1]
            ans = min(ans, K-(s-g))

    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(abs(a[i+1]-a[i]))
print(str(k-max(b)))

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.append(a[i+1]-a[i])
d.sort()
print(sum(d[0:n-1]))

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

Submission status: Accepted

Solution code:
import numpy as np
k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0] + k)
a = np.array(A)
 
max_dist = np.max(np.diff(A))
print(k - max_dist)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
k, n = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls += [i + k for i in a_ls]
best = 10**18
for i in range(n):
    best = min(best, a_ls[i+n-1] - a_ls[i])
print(best)

Submission status: Accepted

Solution code:
import numpy as np
k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0] + k)
a = np.array(A)

max_dist = np.max(np.diff(A))
print(k - max_dist)

Submission status: Accepted

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

b[n-1] = k - a[n-1] + a[0]
ans = k - max(b)
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np
k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0] + k)
a = np.array(A)

max_dist = np.max(np.diff(A))
print(k - max_dist)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

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

Submission status: Accepted

Solution code:
k,n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0] + k)
dist = 0
for i, j in zip(A, A[1:]):
    dist = max(dist, j - i)

print(k - dist)

Submission status: Accepted

Solution code:
# coding: utf-8

K, N = (int(x) for x in input().strip().split())
A = list(map(int, input().strip().split()))

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

list=[]

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

Submission status: Accepted

Solution code:
def main():
    length, num = map(int, input().split())
    a = list(map(int, input().split()))
    max_dis = 0
    first_h = a[0]
    prev_h = a[0]
    for h in a[1:]:
        dis = h - prev_h
        max_dis = max(max_dis, dis)
        prev_h = h

    dis = (length - prev_h) + first_h
    max_dis = max(max_dis, dis)
    print(length - max_dis)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Bn=An[1:]+[K+An[0]]
dis = [x-y for x,y in zip(An,Bn)]
print(K-abs(min(dis)))

Submission status: Accepted

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

d = 0
for i in range(len(lst)-1):
  if d < lst[i+1] - lst[i]:
    d = lst[i+1] - lst[i]
if k-lst[-1] + lst[0] > d:
  d = k-lst[-1] + lst[0]
print(k-d)

Submission status: Accepted

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

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

dist.sort()

min_dist = 0
for i in range(n-1):
    min_dist += dist[i]

print(min_dist)

Submission status: Accepted

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

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

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

print(K - max(A_aida))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(k,a):
    b = a[1:] + [k+a[0]]
    dif = [y-x for x,y in zip(a,b)]
    return k-max(dif)

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

Submission status: Accepted

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

a.append(a[0] + k)
b = [p - q for p, q in zip(a[1:], a)]
ans = sum(b) - max(b)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

temp = 0
for i in range(N):
  if temp < A[i+1] - A[i]:
    temp = A[i+1] - A[i]
A.pop(-1)

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

print(ans)

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.reverse()
span = []
for i in range(len(A)):
  if i + 1 > len(A) - 1:
    span.append(K - A[0] + A[-1])
    continue
  span.append(A[i] - A[i + 1])
print(sum(span) - max(span))

Submission status: Accepted

Solution code:
distance,num = map(int, input().split())
tmp = input()
a = tmp.split()
a = list(map(int,a))
b = []

for i in range(len(a)-1):
    b.append(a[i+1] - a[i])
b.append(distance - a[i+1] + a[0])

b = sorted(b)
min = 0
for i in range(len(b)-1):
    min += b[i]

print(min)

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(n-1):
    cnt=a[i+1]-a[i]
    tmp=max(cnt,tmp)

print(k-tmp)

Submission status: Accepted

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

a.sort()

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

print(k-max_l)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class TravelingSalesmanAroundLake:
    def calculdat_diff(self, lake_dis, house_num, coordinate_list):
        count = 0
        max_dis = (lake_dis - coordinate_list[len(coordinate_list) - 1]) + coordinate_list[0]
        for c in coordinate_list:
            dis = coordinate_list[count + 1] - c
            if dis > max_dis:
                max_dis = dis
            count +=1
            if count + 1 == len(coordinate_list):
                break
        return max_dis
    
    def calculate_minimum(self, lake_distance, max_dis):
        return lake_distance - max_dis

if __name__ == "__main__":
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    tsl = TravelingSalesmanAroundLake()
    max_dis = tsl.calculdat_diff(K, N, A)
    result = tsl.calculate_minimum(K, max_dis)
    print(str(result))

Submission status: Accepted

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

max = 0
sum = 0
for i in range(n):
	if i+1 == n:
		buf = k - (a[i] - a[0])
	else:
		buf = a[i+1] -a[i]
	if buf > max:
		max = buf
	sum += buf
sum -= max
print(sum)

Submission status: Accepted

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

G =[]
for idx,a in enumerate(A):
    if idx == 0:
        G.append(A[idx]+K-A[N-1])
    else:
        G.append(A[idx]-A[idx-1])

G.append(K-A[N-1])

print(K-max(G))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

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

max_dist = max(max_dist, A[0]-(A[-1]-K))

print(K-max_dist)

Submission status: Accepted

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

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

print(k-ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: UTF-8
import os

def remove_empty(target_list: list):
    empty_removed_list = [x for x in target_list if x]
    return empty_removed_list


def get_stdin_str_list(input_str=None):
    if input_str is None:
        input_str = input()
    input_str = input_str.strip()
    input_str_list = input_str.split()
    return input_str_list


def get_stdin_int_list(input_str=None):
    input_str_list = get_stdin_str_list(input_str)
    input_int_list = list(map(int, input_str_list))
    return input_int_list


def get_stdin_str_multi_list(input_str=None):
    if input_str is None:
        input_str = input()
    input_str = input_str.strip()
    input_str_line_list = input_str.split("\n")
    input_str_multi_list = list()
    for str_line in input_str_line_list:
        input_str_list = get_stdin_str_list(str_line)
        input_str_multi_list.append(input_str_list)
    return input_str_multi_list


def get_stdin_int_multi_list(input_str=None):
    input_str_multi_list = get_stdin_str_multi_list(input_str)
    input_int_multi_list = list()
    for str_list in input_str_multi_list:
        input_int_list = list(map(int, str_list))
        input_int_multi_list.append(input_int_list)
    return input_int_multi_list

def get_most_far_pos_index_from_both_neighbors(pos_list, end_pos_distance=None):
    if end_pos_distance is None:
        end_pos_distance = pos_list[-1]
    N = len(pos_list)
    max_distance = 0
    most_far_pos_index = 0
    for i, p in enumerate(pos_list):
        if i == 0:
            distance = end_pos_distance - pos_list[-1] + pos_list[i+1]
        elif i == N-1:
            distance = end_pos_distance - pos_list[i-1] + pos_list[0]
        else:
            distance = pos_list[i+1] - pos_list[i-1]
        
        if distance > max_distance:
            max_distance = distance
            most_far_pos_index = i

    return most_far_pos_index

def get_longest_interval(pos_list, end_pos_distance=None):
    if end_pos_distance is None:
        end_pos_distance = pos_list[-1]
    N = len(pos_list)
    longest_interval = 0

    for i, p in enumerate(pos_list):
        if i == N-1:
            pos_n_1 = end_pos_distance + pos_list[0]
        else:
            pos_n_1 = pos_list[i+1]

        interval = pos_n_1 - p

        if interval > longest_interval:
            longest_interval = interval

    return longest_interval

K, N = get_stdin_int_list()
A = get_stdin_int_list()
longest_interval = get_longest_interval(A, K)
shortest_distance = K - longest_interval
print(shortest_distance)

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split(" "))
A_list = [int(i) for i in input().split()]
max_diff = -1

for i in range(0, len(A_list)):
  if i == len(A_list)-1:
    if max_diff < (K-A_list[i]+A_list[0]):
      max_diff = K-A_list[i]+A_list[0]
  else:
    if max_diff < (A_list[i+1] - A_list[i]):
      max_diff = A_list[i+1] - A_list[i]

print(K - max_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3

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

maxd = 0

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


print(k - maxd)

Submission status: Accepted

Solution code:
import sys

k,n=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))

max=0
for i in range(n):
    if i==0:
        pre=a[i]
    else:
        now=a[i]
        if now-pre>max:
            max=now-pre
        pre=now
if k-pre+a[0]>max:
    max=k-pre+a[0]
print(k-max)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = list(map(int, input().split()))
b = []


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

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

b.sort()
res = 0
for i in range(len(b) - 1):
  res += b[i]
print(res)

Submission status: Accepted

Solution code:
#n=int(input())
k,n=map(int,input().split())
al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

difl=[]

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

print(k-max(difl))

Submission status: Accepted

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

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

print(k-max(diffarr))

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10**9
for i in range(n):
  if i == 0:
    a1 = a[-1]-a[0]
    a2 = a[0]+(k-a[1])
    ans = min(ans,a1,a2)
  else:
    a1 = (k-a[i])+a[i-1]
    ans = min(ans,a1)
print(ans)

Submission status: Accepted

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