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

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

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

print(sum(relative_distance) - max(relative_distance))

Submission status: Accepted

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

dList = [0] * n
dMax = 0
for i in range(n-1):
  dList[i] = aList[i+1] - aList[i]
  if dMax < dList[i]:
    dMax = dList[i]

dList[n-1] = aList[0] + k - aList[n-1]
if dMax < dList[n-1]:
  dMax = dList[n-1]

dSum = sum(dList) - dMax
print(dSum)

Submission status: Accepted

Solution code:
import numpy as np

K, N = map(int, input().split())
an = [int(x) for x in input().split()]
arr = np.array(an)

dist = arr[1:] - arr[:-1]
dist = list(dist)
dist.append(K - an[N-1] + an[0])

print(K - max(dist))

Submission status: Accepted

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

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

print(k-max)

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,abs(a[(i+1)%n]-a[i]))
print(k-d)

Submission status: Accepted

Solution code:
#!python3.4
# -*- coding: utf-8 -*-
# abc160/abc160_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    K, N = i2nn()
    A = i2nn()
    A.append(K + A[0])
    max_d = 0
    for i in range(N):
        d = A[i + 1] - A[i]
        max_d = max(max_d, d)
    print(K - max_d)
    return

main()

Submission status: Accepted

Solution code:
def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs

def main(inputs):
	k,n=getIntList(inputs[0])
	aList=getIntList(inputs[1])
	
	d=k-aList[n-1]+aList[0]
	di=n-1
# 	print(d,di)
	for ai in range(n-1):
		dd=aList[ai+1]-aList[ai]
# 		print(dd)
		if dd>d:
			di=ai
			d=dd
	
	print(k-d)


if __name__=="__main__":
	inputs=getInputs()
# 	inputs=simInputs()
	main(inputs)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a_array = map(int, input().split())
first_a = a_array.__next__()
forward_a = first_a
max_length = 0
for i in range(n - 1):
    a = a_array.__next__()
    max_length, forward_a = max(max_length, a - forward_a), a
print(k-max(max_length, k - a + first_a))

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  k,n=LI()
  l=LI()
  a=copy.deepcopy(l)

  for x in a:
    l.append(k+x)

  ans=inf
  for i in range(n):
    ans=min(ans,l[i+n-1]-l[i])

  return ans

# main()
print(main())

Submission status: Accepted

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

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

print(k-max(lst))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(str(K - max_dist))

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

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
d = [a[i]-a[i-1] for i in range(1,n)] + [(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()))
diff_list = []

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

print(K - max(diff_list))

Submission status: Accepted

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

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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):
  m = max(m, A[i+1] - A[i])

print(K - m)

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
d = 0
for i in range(N):
    tmp = abs(A[i] - A[i-1])
    if tmp > K / 2:
        tmp = K - tmp
    if tmp > d:
        d = tmp
print(K - d)

Submission status: Accepted

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7

# ここから書き始める
# x = int(input())
# ans = (x // 500) * 1000
# x %= 500
# ans += (x // 5) * 5
# print(ans)


k, n = map(int, input().split())
a = list(map(int, input().split()))
max_gap = 0
start = 0
for i in range(n):
    if i == 0:
        if k - a[-1] + a[0] > max_gap:
            start = i
            max_gap = k - a[-1] + a[0]
    elif a[i] - a[i - 1] > max_gap:
        start = i
        max_gap = a[i] - a[i - 1]
# print("max_gap =", max_gap)
# print("start =", start)
ans = k - max_gap
print(ans)

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[N-1]+A[0])
max = List[0]
for i in range(len(List)):
    if List[i] >= max:
        max = List[i]
print(K-max)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K, N = list(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:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
dis=[]
for i in range(N-1):
  dis+=[A[i+1]-A[i]]
dis+=[A[0]+K-A[N-1]]
ans=K-max(dis)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

m = 10 ** 12
for i in range(1,len(a)-1):
    r = k - abs(a[i] - a[i-1])
    l = k - abs(a[i] - a[i+1])
    if min(r,l) <= m:
        m = min(r,l)

r = abs(a[-1] - a[0])
l = k - abs(a[1] - a[0])
if min(r,l) <= m:
    m = min(r,l)

r = k - abs(a[-1] - a[-2])
l = abs(a[-1] - a[0])
if min(r,l) <= m:
    m = min(r,l)
    
print(m)

Submission status: Accepted

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

def main():
     k, n = map(int, input().split())
     l = list(map(int, input().split()))

     l = sorted(l)
     diff_max = 0
     for i in range(len(l)-1):
          diff = l[i+1] - l[i]
          if diff_max < diff:
               diff_max = diff
     
     ans = k - diff_max
     last = l[-1] - l[0]
     if last < ans:
          ans = last
     print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

print(k - max(dist))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
K,N=map(int,input().split())

A=list(map(int,input().split()))
B=list(map(lambda x:x+K,A))

A+=B
#print(A)
ans=10**9
for i in range(N):
    ans=min(ans,A[i+N-1]-A[i])
    #print(ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
import heapq

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

As = np.asarray(input().split(), dtype=np.int32)

H = []

for i in range(len(As)):
    if i == 0:
        heapq.heappush(H, As[-1]-k-As[0])
    else:
        heapq.heappush(H, As[i-1] - As[i])

print(k + H[0])

Submission status: Accepted

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

Submission status: Accepted

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

max_d = A[0] + (K-A[-1])
for n in range(1,N):
  _ = A[n-1]
  __ = A[n]
  max_d = max(__-_, max_d)

ans = K-max_d
print(ans)

Submission status: Accepted

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

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

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

print(k-temp)

Submission status: Accepted

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

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

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])
ans = min(k - diff, a[n-1] - a[0])
print(ans)

Submission status: Accepted

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


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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
k, n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
# Ignore the longest yard.


def gen(a):
    for i, d in enumerate(a):
        if i < n - 1:
            yield a[i + 1] - d
        elif i == n - 1:
            yield k + a[0] - d


max_d = max(gen(a))
print(k - max_d)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(l) - max(l))

Submission status: Accepted

Solution code:
k, n = map(int,input().split())
A = list(map(int,input().split()))
ans = 10**9
ans = min(ans, A[-1] - A[0])

for i in range(1, len(A)):
    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(i) for i in input().split()]

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    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,A[0]+k-A[-1])
    print(k-m)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

tmp = 0
for i in range(n-1):
    if abs(a_list[i] - a_list[i+1]) > tmp:
        tmp = abs(a_list[i] - a_list[i+1])
        max_index = i

if (a_list[0] + (k-a_list[-1])) > tmp:
    tmp = a_list[0] + (k-a_list[-1])
    max_index = n-1

if max_index != n-1:
    print(a_list[max_index] + (k-a_list[max_index+1]))
else:
    print(a_list[max_index]-a_list[0])

Submission status: Accepted

Solution code:
import sys

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

max_diff = A[1] - A[0]

for i in range(N - 1):
    diff = A[i + 1] - A[i]
    if diff > max_diff:
        max_diff = diff

diff = A[0] + (K - A[N - 1])
if diff > max_diff:
    max_diff = diff

ans = K - max_diff

print(ans)

Submission status: Accepted

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

# print(K, N)
# print(Alist)

gaps = []
for i in range(N):
    if i==0:
        gap = Alist[0] + (K - Alist[N-1])
        gaps.append(gap)
    else:
        gap = Alist[i] - Alist[i-1]
        gaps.append(gap)

gapmax = 0

for i, gap in enumerate(gaps):
    # if gapmax < gap:
    gapmax = max(gapmax, gap)
print(K - gapmax)

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(n-1):
  tmp.append(a[i + 1] - a[i])
print(k - max(tmp))

Submission status: Accepted

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

print(np.sum(B)-np.max(B))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

    print(k - max(b))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
m,n=tuple(map(int,input().split(" ")))
lis=list(map(int,input().split(" ")))
lis.append(lis[0]+m)
min=0
for i in range(n):
    if(lis[i+1]-lis[i]>min):
        min=lis[i+1]-lis[i]
print(m-min)

Submission status: Accepted

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

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

Submission status: Accepted

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

if a[0] + k - a[n-1] > ans:
  ans = a[0] + k - a[n-1]
  
print(k - ans)

Submission status: Accepted

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

print(sum(s))

Submission status: Accepted

Solution code:
def c160(k , n , al):

    ans = 0
    count = 0

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

    return k - count

def main():
    k , n = map(int , input().split())
    al = list(map(int , input().split()))
    print(c160(k , n , al))
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(N)]
C = 0
for i in range(N):
  if i == N-1:
    B[i] = A[0] + K - A[i]
  else:
    B[i] = A[i+1] - A[i]
  if B[i] > C:
    C = B[i]
print(K-C)

Submission status: Accepted

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

Submission status: Accepted

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

	m = 0

	for i in range(N-1):
		sa = A[i+1] - A[i]
		if sa > m:
			m = sa

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

	print(K-m)

Submission status: Accepted

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

print(k - max(box))

Submission status: Accepted

Solution code:
import numpy as np

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

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

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

max_dist = np.max(B)

print(K - max_dist)

Submission status: Accepted

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

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


print(K - max(B))

Submission status: Accepted

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

A.sort()
A += [K+A[0]]

delta = [A[i+1]-A[i] for i in range(N)]
print(K-max(delta))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(L))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
k,n=mii()
arr0=limii()
p=k+arr0[0]
arr=arr0+[p]

ans=0
arr.sort()
for i in range(n):
    ans=max(ans,arr[i+1]-arr[i])

print(k-ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

distance = [A[i+1]-A[i] for i in range(len(A)-1)]
distance = [K-A[-1]+A[0]] + distance

min_distance = max(distance)
print(K-min_distance)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-ma)

Submission status: Accepted

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

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

# 最も家と家の間の距離が大きい長さ（max_distance）を求め、K - max_distanceを求める

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

ans = K - max_distance
print(ans)

Submission status: Accepted

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

MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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

m = K-A[len(A)-1] + A[0]

index = 0

for i in range(len(A)-1):
    d = A[i+1]-A[i]
    if m < d:
        m = d
        index = i

ans = K - m
print(ans)

Submission status: Accepted

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

span = []

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

print(K-max(span))

Submission status: Accepted

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

df = [None] * n

for i in range(n - 1):
    df[i] = an[i + 1] - an[i]

df[n - 1] = k - an[n - 1] + an[0]

print(sum(df) - max(df))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
maximum = 0
for i in range(N):
  if i == 0:
    continue
  elif maximum < A[i]-A[i-1]:
    maximum = A[i]-A[i-1]
if maximum < A[0]+(K-A[N-1]):
  maximum = A[0]+(K-A[N-1])

print(K-maximum)

Submission status: Accepted

Solution code:

K, N = [int(s) for s in input().split()]
A_list = [int(s) for s in input().split()]

d_list = []
for i, A in enumerate(A_list):
    if i == N - 1:
        d = K - A_list[i] + A_list[0]
    else:
        d = A_list[i + 1] - A_list[i]
    d_list.append(d)

d_sum_min = K - max(d_list)
print(d_sum_min)

Submission status: Accepted

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

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

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

print(K-longest)

Submission status: Accepted

Solution code:
mod = 10**9 + 7

def iip(listed = False):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        #print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod

#####################################################ライブラリ集ここまで

def main():
    K, N = iip()
    A = iip()
    A.sort()

    dis = []
    cur = -(K - A[-1])
    for i in A:
        d = i - cur
        cur = i
        dis.append(d)

    md = max(dis)
    print(K-md)


    pass

if __name__ == "__main__":
    main()

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):
    D = []
    for i in range(1,len(A)):
        D.append(abs(A[i]-A[i-1]))
    D.append(A[0]+K-A[-1])
    print(sum(D)-max(D))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def c():
    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[N-1])
    # print(diff)
    print(K - max(diff))


##########
if __name__ == "__main__":
    c()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

list_Y = list(map(int, input().split()))
addY = list_Y[0] + M
list_Y.append(addY)

max_Y = 0 

for i in range(N):
  kyori = list_Y[i+1] - list_Y[i]
  if kyori >= max_Y:
    max_Y = kyori
    
print(M - max_Y)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools


a = [int(s) for s in stdin.readline().rstrip().split()]
K = a[0]
N = a[1]
A = [int(s) for s in stdin.readline().rstrip().split()]

diff = []
for el in range(len(A)):
    if el == len(A) - 1:
        diff.append(K-A[el]+A[0])
        break
    diff.append(A[el+1]-A[el])

print(K-max(diff))

Submission status: Accepted

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

lens = []
max_len = 0
for i in range(len(A)):
  if i == len(A) - 1:
    dist = K - A[i] + A[0]
  else:
    dist = A[i+1] - A[i]
  lens.append(dist)
print(sum(lens) - max(lens))

Submission status: Accepted

Solution code:
k,n = map(int, input().split())
a = list(map(int, input().split()))
max_diff = -float("inf")
ind = 0
for i in range(n):
    if i == n-1:
        x = (k-a[i]) + a[0]
        if x > max_diff:
            max_diff = x
            ind = i
    else:
        x = a[i+1] - a[i]
        if x > max_diff:
            max_diff = x
            ind = i
dist = 0
for i in range(n):
    if i == ind:
        continue
    if i == n-1:
        dist += (k-a[i])+a[0]
    else:
        dist += a[i+1] - a[i]
print(dist)

Submission status: Accepted

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

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 + [k + A[0]])
d = A[1:] - A[:-1]
print(np.sum(d) - np.max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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