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

Submission status: Accepted

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

listB = []
## last - first ##
sum = max(listA) - min(listA)
listB.append(sum)

## n - (n-1) ##
for i in range(1,N):
  out = listA[i] - listA[i-1]
  out = K - out
  listB.append(out)


print(min(listB))

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])
print(K-max(d))

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 - 1):
    ref = k - (a[i + 1] - a[i])
    if ref < ans:
        ans = ref

print(ans)

Submission status: Accepted

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

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

for i in range(N):
  a = A[i+1]-A[i]
  L.append(a)
ans = K-max(L)

print(ans)

Submission status: Accepted

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

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

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

print(sum(distance) - max(distance))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

dist = []
for i in range(0,n-1):
  dist.append(a[i+1]-a[i])#距離のリスト

longest = 0
for j in range(0,n-1):
  if dist[j] >= longest:
    longest = dist[j]

short_cont_1 = k-longest
short_cont_2 = a[n-1]-a[0]
if short_cont_1>=short_cont_2:
  print(short_cont_2)
else:
  print(short_cont_1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
A = list(map(lambda x: x-K, A)) + A
minimum = -1
i = 0
for i in range(len(A)-N+1):
    cost = abs(A[i] - A[i+N-1])
    if minimum == -1 or cost < minimum:
        minimum = cost
print(minimum)

Submission status: Accepted

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial,hypot
#import sys
#from fractions import gcd
setrecursionlimit(10**6+1)
input=stdin.readline

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

Submission status: Accepted

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

if N == 2:
    print(min(A[1] - A[0], K + A[0] - A[1]))

max_distance = 0

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

    print(min(K - max_distance, A[N - 1] - A[0]))

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

if N == 2:
    print(min(A[1] - A[0] , K + A[0] - A[1]))

max_distance = 0

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

    print(min(K-max_distance,A[N-1]-A[0]))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

a.append(k + a[0])

section = []

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

b_max = max(section)
ans = k - b_max

print(ans)

Submission status: Accepted

Solution code:
def solve():
  raw = input().split()
  K = int(raw[0])
  N = int(raw[1])
  vals = [None] * N
  ans = 99999999
  raw = input().split()
  for i in range(N):
    vals[i] = int(raw[i])
  for i in range(N):
    cc = -1
    if i == 0:
      cc = vals[N-1] - vals[0]
    else:
      cc = vals[i-1] + K - vals[i]
    ans = min(ans, cc)
  print(ans)
solve()

Submission status: Accepted

Solution code:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list()
i = 0
while i + 1 < len(b):
    c.append(b[i + 1] - b[i])
    i += 1
c.append(a[0] - b[len(b) - 1] + b[0])
print(sum(c) - max(c))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

maxDist = A[0] + (K - A[N-1])

for i in range(N-1) :
  dist = A[i+1] - A[i]
  if dist > maxDist :
    maxDist = dist
    
print(K-maxDist)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort()
r_list = list()
for index, a in enumerate(a_list):
	if index == n - 1:
		r_list.append((k - a) + a_list[0])
	else:
		r_list.append(a_list[index+1] - a)
r = k - max(r_list)
print(r)

Submission status: Accepted

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

if A[-1]>A[0]:
    longdis = abs(K-A[-1]+A[0])
else:
    longdis = abs(A[-1]-A[0])

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

Submission status: Accepted

Solution code:
#C traveling salesman around lake

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

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

last = a[0] + k - a[n-1]
if last > ans:
    ans = last

print(k-ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

K, N, *A = map(int, read().split())
A.append(K + A[0])
d = [j - i for i, j in zip(A, A[1:])]

print(K - max(d))

Submission status: Accepted

Solution code:
import sys


input = sys.stdin.readline


def main():
  k, n = map(int, input().rstrip().split())
  a = [int(_) for _ in input().rstrip().split()]
  
  dist = [(a[i] - a[i - 1]) % k for i in range(n)]
  
  print(k - max(dist))


if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

K, N, *A = map(int, read().split())
A.append(K + A[0])
D = 0
for i, j in zip(A, A[1:]):
    d = j - i
    if d > D:
        D = d

print(K - D)

Submission status: Accepted

Solution code:
# 隣り合う長さの最大値をKから引く
circ_length, n_house = map(int, input().split())
places = sorted(list(map(int, input().split())))

delta_places = []
for i in range(n_house):
  if i == 0:
    d = circ_length - places[-1] + places[0]
  else:
    d = places[i] - places[i-1]
  delta_places.append(d)

result = circ_length - max(delta_places)

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    K,N=MI()
    A=LI()
    diff=[0]*N
    for i in range(N-1):
        diff[i]=A[i+1]-A[i]
        
    diff[-1]=A[0]+(K-A[-1])
    print(K-max(diff))

main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

k,n = map(int,readline().split())
lst1 = list(map(int,readline().split()))

dist = 0

for i in range(n-1):
    dist = max(dist,abs(lst1[i]-lst1[i+1]))

dist = max(dist,k-lst1[-1]+lst1[0])

print(k-dist)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


max = 0
for i in range(1,n):
    if lista[i]-lista[i-1]>=max:
        max = lista[i]-lista[i-1]

if k-lista[n-1]+lista[0]>=max:
    max = k-lista[n-1]+lista[0]

print(k-max)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

max = 0
for i in range(len(a_list)):
    if i == len(a_list)-1:
        length = (k-a_list[i])+a_list[0]
    else:
        length = a_list[i+1] - a_list[i]
    if length > max:
        max = length
print(k-max)

Submission status: Accepted

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

print(k - max(dis))

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
A = list(map(int,input().split()))
A.append(A[0]+K) ##円の最後に追加する．

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

Submission status: Accepted

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

r = []

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

ind = r.index(max(r))

if ind == n-1:
    print(a[n-1] - a[0])
else:
    print(k - a[ind+1] + a[ind])

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
lst = list(map(int, input().split()))
min_num = k
for i in range(len(lst)):
    if i == 0:
        tmp = min(lst[0] + (k - lst[1]), lst[-1] - lst[0])
    elif i == n - 1:
        tmp = min(lst[-1] - lst[0], (k - lst[-1]) + lst[n - 2])
    else:
        tmp = min(lst[i] + (k - lst[i + 1]), (k - lst[i]) + lst[i - 1])
    if tmp < min_num:
        min_num = tmp

print(min_num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
A += [a+K for a in A]
minD = 1e6
for n in range(N):
  minD = min(minD, A[n+N-1] - A[n])
print(minD)

Submission status: Accepted

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

dis = [0] * N

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

print(K-max(dis))

Submission status: Accepted

Solution code:
import numpy as np
import sys
readline = sys.stdin.buffer.readline
K, N = map(int, readline().split())
A = list(map(int,input().split()))
N1= A[0]+K-A[N-1]

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

print(K-N1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
C,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]+C-a[N-1])

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

Submission status: Accepted

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

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

print(K-result)

Submission status: Accepted

Solution code:
# Problem C - Traveling Salesman around Lake

# input
K, N = map(int, input().split())
A_list = list(map(int, input().split()))
list_len = len(A_list)

# initialization
min_ans = 10**18

# search
for i in range(list_len):
    mae_i = 0
    if i==list_len-1:
        mae_i = 0
    else:
        mae_i = i + 1

    kyori = 0
    if A_list[mae_i]-A_list[i]<0:
        kyori = K - (A_list[mae_i] - A_list[i] + K)
    else:
        kyori = K - (A_list[mae_i] - A_list[i])
    min_ans = min(min_ans, kyori)

# output
print(min_ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(k-max_dis)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

prev = arr[0]
maxi = 0
sums = 0
for a in arr[1:]:
    sums += a - prev
    maxi = max(maxi, a - prev)
    prev = a

sums += k - (arr[-1] - arr[0])
maxi = max(maxi,  k - (arr[-1] - arr[0]))
print(sums - maxi)

Submission status: Accepted

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

# min_=10**6
dA=np.diff(A)
dA=np.append(dA,A[0]+K-A[-1])
skip_dist=max(dA)
ans=K-skip_dist

print(ans)

Submission status: Accepted

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

ans = float('inf')
for i in range(N):
    if i == 0:
        ans = min(ans, A[-1] - A[0])
    else:
        ans = min(ans, (A[-1]-A[i]) + (K-A[-1]) + (A[i-1]))

print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
KN = input().split()
K = int(KN[0])
N = int(KN[1])
#print(K,N)

A = input().split()
A_np = np.append(np.array(A, dtype=np.int64), int(A[0])+K)
A_np_lag = np.append(A_np[1:], A_np[0])
A_diff = A_np_lag[:N] - A_np[:N]
#print(A_diff)
print(A_diff.sum() - A_diff.max())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = input().split()
A = [int(val) for val in A]
A_org = min(A)
A = [val - A_org for val in A]
A.sort()
dist = []
for i in range(len(A)):
  if i != len(A)-1:
    dist.append(A[i+1]-A[i])
  else:
    dist.append(K-A[i])
print(K - max(dist))

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0] * (2 * N)

    for i in range(2*N):
        if i < N:
            B[i] = A[i]
        else:
            B[i] = K + A[i - N]

    l = 0
    for i in range(N):
        tmp = B[i+1] - B[i]
        l = max(l, tmp)

    ans = K - l
    print(ans)



if __name__ == "__main__":
    main()

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

anti_clockwise_dist = A[0] - (A[-1] - K) 

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

dist = max(dist, anti_clockwise_dist)

print(K - dist)

Submission status: Accepted

Solution code:
# 初期入力
K,N = (int(x) for x in input().split(" "))
A = list(map(int, input().split()))
A.append(0) # 基準地点を最後に追加、スタートが０でもうまくいく

# 1件となりまでの距離
kyori = [ A[i+1] -A[i] for i,v in enumerate(A[:-1]) ]
kyori[-1] = kyori[-1] + K # n番目から基準までは全体から「引く」
kyori.append(A[0] -A[-1] ) # 基準から1番目までの距離
x_saisyo_saigo = kyori[-2] +kyori[-1]
kyori.append(x_saisyo_saigo) # n番目から1番目の距離

# 最短距離は、全体からmax(隣の家との距離)を引く
kyori_min = K - max(kyori)
print(kyori_min)

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(A[0]+k)
B = [0 for i in range(n)]
for j in range(n):
    B[j] = A[j+1] - A[j]
#print(A)
#print(B)
B.sort()
B.pop()
#print(B)
ans = sum(B)
print(ans)

Submission status: Accepted

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

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

print(d)

Submission status: Accepted

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

i=0
dist=-1
while i < n:
  dist=max(dist,a[i+1]-a[i])
  i += 1

print(str(k-dist))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

distances = [K-sum(distances)] + distances
distances.remove(max(distances))

print(sum(distances))

Submission status: Accepted

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

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

memo = []

for i in range(1,n):
    memo.append(abs(a[i]-a[i-1]))
memo.append(k-a[-1]+a[0])
memo.sort()

ans = 0

for i in range(len(memo)-1):
    ans += memo[i]

print(ans)

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()))
A.append(A[0]+k)

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

Submission status: Accepted

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

max_dist = 0
for i in range(n):
    next = (i+1) % n
    dist = a[next] - a[i]
    if dist < 0: dist+= k
    max_dist = max(max_dist, dist)
print(k - max_dist)

Submission status: Accepted

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

a = K + A[0]
A = A + [a]

#print(A)

B = []
for i in range(N):
    B.append(A[i+1] - A[i])
B = sorted(B,reverse = False)

print(sum(B) - max(B))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

distance = (k - a[n-1]) + a[0]

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

print(k - distance)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''
20 3
5 10 15
'''

def solve(l,K):
	n = len(l)
	l += [l[0] + K]
	l = sorted(l)
	maxx = 0
	for i in range(n):
		maxx = max(maxx,l[i+1]-l[i])
	return K - maxx
	

K,N = list(map(int, stdin.readline().rstrip().split()))
l = list(map(int, stdin.readline().rstrip().split()))
ans = solve(l,K)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

rec = list()

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

for i, _ in enumerate(A[:-1]):
    rec.append(A[i+1] - A[i])

sum = 0
for r in rec:
    sum += r

max_r = max(rec)
print(sum - max_r)

Submission status: Accepted

Solution code:
P, N = list(map(int, input().split()))
arr = list(map(int, input().split()))
mn = 100000000
l, r = 0, len(arr) - 1
for i in range(N):
    if (arr[r] - arr[l]) < mn:
        mn = arr[r] - arr[l]
    arr[l] += P
    l = (l + 1) % N
    r = (r + 1) % N
print(mn)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Traveling Salesman around Lake
def main():
    K, N, *A = map(int, open(0).read().split())
    A.append(K + A[0])
    longest_dist = max(j - i for i, j in zip(A, A[1:]))
    ans = K - longest_dist
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
li = list(map(int, input().split()))
li.append(li[0]+K)
delta = [li[i+1]-li[i] for i in range(len(li)-1)]
ans = K - max(delta)
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(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()))
A.sort()
dist = []
for i in range(N-1):
    dist.append([A[i+1]-A[i],i])
dist.sort()
ind = dist[-1][1]
ans = min(A[-1]-A[0],A[ind]+abs(K-A[ind+1]))

print(ans)

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
A_s = list(map(int,input().split()))
MAX = max(list(map(lambda i:(A_s[(i+1)%N]-A_s[i])%K, range(N))))
print(K-MAX)

Submission status: Accepted

Solution code:
k,n,*a=map(int,open(0).read().split())
a+=[a[0]+k]
d=[abs(a[i]-a[i+1]) 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())))
d = [0]*N
d[0] = abs(a[0]+K-a[N-1])
for i in range(1,N):
    d[i] = abs(a[i]-a[i-1])
print(K-max(d))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]



def main():
    m=LI()
    k=m[0]
    n=m[1]
    a=LI()
    for i in range(n):
        if i!=0:
            a[i]-=a[0]
    a[0]=0;
    ans=k;
    for i in range(n-1):
        ans=min(ans, k-(a[i+1]-a[i]))
    ans=min(ans, a[n-1])
    print(ans)
main()

Submission status: Accepted

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

lists_slic = lists[1:] + lists[0:1]

# print(lists_slic)
a = 0
for i in range(len(lists)):
    if lists_slic[i] - lists[i] < 0:
        b = lists_slic[i] - lists[i] + K
        if b > a:
            a = b
    else:
        if (lists_slic[i] - lists[i])>a:
            a = lists_slic[i] - lists[i]

c = K - a

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,y = map(int,input().split())
li = list(map(int,input().split()))	
minmin = x - (li[0] + x - li[y-1])
for i in range(len(li)-1):
    if minmin > x - (li[i+1] - li[i]):
        minmin =  x - (li[i+1] - li[i])
    else:
      continue
print(minmin)

Submission status: Accepted

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