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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

A = [0]*n

A = [int(s) for s in input().split()]

A_dif = [0]*n

for i in range(n):
    a_bak = i-1
    a_f = i+1
    if(i == 0):
        a_bak = n-1
    if(i == n-1):
        a_f = 0
    
    #print()   
    A_dif[i] 
    tmp = max(abs(A[a_f]-A[i]),abs(A[a_bak]-A[i]))
    
    if(tmp > k//2):
        tmp = k - tmp
    
    A_dif[i] = tmp
    

#print(A_dif)
print(k - max(A_dif))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(dis) - max(dis))

Submission status: Accepted

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

# 時計回りで動くことを考えて。。。

# K以上はないので、Kにしておく
max_distance = K
# 最後の家だけは別途評価することとする
for start_house_index in range(N) :
    if (start_house_index == 0) :
        # 最初の家
        d = A_list[N-1] - A_list[0]
    elif (start_house_index == N-1) :
        # 最後の家
        d = (K - A_list[N-1]) + A_list[N-2]
    else :
        # 最初でも最後でもない家
        d = (K - A_list[start_house_index]) + A_list[start_house_index-1]
        
    
    # print(d)
    if (d < max_distance) :
        max_distance = d

print(max_distance)

Submission status: Accepted

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

dp = [0]*N
dp[0] = K-As[N-1]+As[0]

for i in range(1,N):
    dp[i] = max(dp[i-1],As[i]-As[i-1])

ans = K - max(dp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N=map(int,input().split())                            # 1行2列
inList=list(map(int,input().split()))                     # 1行N列・・・Nは任意
distList=[]

for i in range(N):
	right = 0
	left  = 0
	if i == 0:
		left  = inList[0]+(K - inList[1])
		right = inList[N-1] - inList[0]
	elif i == N-1:
		left  = (K - inList[i]) + inList[i - 1]
		right = inList[i] + (K - inList[0])
	else:
		left  = (K - inList[i]) + inList[i - 1]
		right = inList[i] + (K - inList[i + 1])

	if(left < right):
		distList.append(left)
	else:
		distList.append(right)

distList.sort()
print(distList[0])

Submission status: Accepted

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

kyori=[0]*n

for i in range(n-1):
    kyori[i]=a[i+1]-a[i]

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

print(k-max(kyori))

Submission status: Accepted

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

dis = A + int(Tree[0]) - int(Tree[-1])

for i in range(N - 1):
    dis = max(dis, int(Tree[i + 1]) - int(Tree[i]))

print(A - dis)

Submission status: Accepted

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

B = []
for a in range(len(A)):
  if a != 0:
    B.append(A[a-1] - A[a] + K)
  else:
    B.append(A[len(A)-1] -A[a])

print(min(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


k, n = map(int, sys.stdin.readline().split())
homes = list(map(int, sys.stdin.readline().split()))
M = -1
for i in range(n-1):
    if homes[i+1] - homes[i] > M:
        M = homes[i+1] - homes[i]
if homes[0] + k - homes[-1] > M:
    M = homes[0] + k - homes[-1]

print(k - M)

Submission status: Accepted

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

ans = k
max_dis = 0

for i in range(n-1):
    s = li[i+1] - li[i]
    if max_dis < s:
        max_dis = s

tmp = li[0] + (k - li[-1])

max_dis = max(tmp, max_dis)

print(k - max_dis)

Submission status: Accepted

Solution code:
#C
line = [int(x) for x in input().split()]

l = [int(x) for x in input().split()]

total  = 0
maxx = -99999999999

for i in range(0, line[1]-1):
    total = total + (l[i+1] - l[i])
    if maxx < (l[i+1] - l[i]):
        maxx = (l[i+1] - l[i])

yeet = (l[0] + (line[0]-l[line[1]-1]))
total = total + yeet

if maxx < yeet:
        maxx = yeet


print(total - maxx)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
for i in range(N):
    A.append(A[i] + K)
ans = float("inf")
for i in range(1, N+1):
    ans = min(ans, A[i+N-1] - A[i])
print(ans)

Submission status: Accepted

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

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

point = A[0]
maxlen = A[0] + (K-A[N-1])
for i in range(1,N):
    maxlen = max(A[i]-A[i-1],maxlen)
    #print(str(A[i]-A[i-1]) + "max="+str(maxlen))


print(K-maxlen)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
K,N = LI()
A = LI()
ans = float('INF')
_A = []
for x in A:
    _A.append(x-K)
allA = _A+A
for i in range(N*2-(N-1)):
    ans = min(ans,allA[i+N-1]-allA[i])
print(ans)

Submission status: Accepted

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

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

print(k - max(diff))

Submission status: Accepted

Solution code:
def solve():
    K, N = map(int,input().split())
    An = list(map(int,input().split()))
    dsum = 0
    dmax = 0

    for i in range(N):
        if i == 0:
            d = An[i] + K - An[N-1]
        else:
            d = An[i] - An[i-1]
        if d > dmax:
            dmax = d
        dsum += d
    print(dsum-dmax)

solve()

Submission status: Accepted

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
dist = []
dist.append(N - A[-1] + A[0])
ans = 10**18
for i in range(0,K-1):
    dist.append(A[i+1]-A[i])

s = sum(dist)
for i in dist:
    ans = min(ans,s-i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

distance_list.sort()

interval_list = []

a = 0
for i in  range(n):
    interval_list.append(distance_list[i] - a)
    a = distance_list[i]

b = k - distance_list[n-1]

interval_list[0] = interval_list[0] + b

interval_list.sort()

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

e = [0 for _ in range(n)]
for i in range(1, n):
    e[i] = a[i] - a[i-1]
e[0] = k + a[0] - a[-1]
print(k - sorted(e, reverse=True)[0])

Submission status: Accepted

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

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

    max_blank = 0

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

    print(K - max_blank)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    K,N = map(int,input().split())
    a = list(map(int,input().split()))
    
    a = [a[-1]-K] + a + [a[0]+K]
    
    ans = K
    
    for i in range(N):
        use = max(a[i+1]-a[i],a[i+2]-a[i+1])
        ans = min(ans,K-use)
    
    print(ans)
    
if __name__ == "__main__":
    main()

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):
    tmp = A[i+1] - A[i]
    if tmp > max_:
        max_ = tmp
if max_ > A[0]+(K-A[-1]):
    print(K-(max_))
else:
    print(K-(A[0]+(K-A[-1])))

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

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

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

K, N = MII_split()
A_list = LII_split()

max = 0
for i in range(len(A_list)):
    if i + 1 < len(A_list):
        dist = abs(A_list[i] - A_list[i + 1])
    else:
        dist = abs(K - A_list[i] + A_list[0])
    if dist > max:
        max = dist

print(K - max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
l = sorted(map(int,input().split()))
x=n*2
for i in range(k):
  x=min(x,n-(l[(i+1)%k]-l[i])%n)
print(x)

Submission status: Accepted

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

@author: Kanaru Sato
"""

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

for i in range(n-1):
    d = a[i+1]-a[i]
    if d > k/2:
        d = k-d
    dl.append(d)

d = a[n-1]-a[0]
if d > k/2:
    d = k-d
dl.append(d)

ans = k-max(dl)

print(ans)

Submission status: Accepted

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

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])
c=max(b)
print(k-c)

Submission status: Accepted

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

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

maxdist = distlist[0] + K - distlist[N-1]
for i in range(0, N-1):
  dist2houses = distlist[i+1] - distlist[i]
  if (dist2houses > maxdist):
    maxdist = dist2houses

print(K - maxdist)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
D = [abs(A[(i+1)%N] - A[i]) for i in range(N-1)]
D.append(K - A[-1] + A[0])
print(sum(D) - max(D))
# Ar = [i for i in A]
# for i,a in enumerate(A):
#     if K - a < a:
#         Ar[i] = a - K
# print(min(max(A) - min(A), max(Ar) - min(Ar)))

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))
m = list(map(int,input().split()))
l = [0,0]
for i,j in enumerate(m):
    if i == 0:
        l[0] = i
        l[1] = j-0+n[0]-m[n[1]-1]
    else:
        if l[1] < m[i]-m[i-1]:
            l[0] = i
            l[1] = m[i]-m[i-1]
print(n[0]-l[1])

Submission status: Accepted

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

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

for i in range(len(a)):
  a[n - 1 - i] -= k
  new = -(a[n - 1 - i]) + a[n - 2 - i]
  # print(new)
  if new < ans:
    ans = new
print(ans)

Submission status: Accepted

Solution code:
inps = input().split()
k = int(inps[0])
n = int(inps[1])
ap = input().split()
ap = [int(s) for s in ap]
a = []
for i in range(n):
    if i == 0:
        a.append(ap[0] + k - ap[n - 1])
    else:
        a.append(ap[i] - ap[i-1])
print(k - max(a))

Submission status: Accepted

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

sorted(A)
A.append(A[0]+K)
ls = []

for i in range(len(A)-1):
    dis = A[i+1] - A[i]
    ls.append(dis)
print(K - max(ls))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

k, n = LI()
A = LI()
ret = k - A[n - 1] + A[0]
for i in range(n - 1):
    ret = max(ret, A[i + 1] - A[i])

print(k - ret)

Submission status: Accepted

Solution code:

import math
import itertools
import statistics
#import collections
#x = int(input())
k, n = (map(int, input().split()))
#A= (map(int, input().split()))
A =  list(map(int, input().split()))

dif = 0
dif_index = -1
for i in range(n-1):
    if A[i+1]-A[i] >= dif:
        dif = A[i+1]-A[i]
        dif_index = i+1

if A[0] > 0:
    if A[0]+(k-A[-1]) >= dif:
       dif =  A[0]+(k-A[-1])

else :
    if k-A[-1] >= dif:
        dif = k-A[-1] 
        dif_index = 0

print(k-dif)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 160: C (2020/03/28)
K, N = map(int, input().split())
S = input().split()
A = [0 for i in range(N)]
for i in range(N):
    A[i] = int(S[i])
max = 0
for i in range(N):
    if i != N-1:
        D = A[i+1]-A[i]
    else:
        D = K-A[i]+A[0]
    if max < D:
        max = D
print(K-max)

Submission status: Accepted

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

kyori = 0
if A[0] == 0:
    kyori = k-A[-1]
else:
    kyori = k-(A[-1]-A[0])

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

Submission status: Accepted

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

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

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

tmp = max(tmp, gyaku)

print(K - tmp)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A_list = sorted(list(map(int, input().split())))
M = 0

for n, A in enumerate(A_list):
    if (n != (N - 1)) and (n != 0):
        D  = max(abs(A - A_list[n - 1]), abs(A - A_list[n + 1]))
    elif n == (N - 1):
        D = abs(A - A_list[n - 1])
    else:
        D = max(abs(A +(K - A_list[n - 1])), abs(A - A_list[n + 1]))
    if M < D:
        M = D


print(K - M)

Submission status: Accepted

Solution code:
import numpy as np


def main():
    k, n = [int(v) for v in input().split(' ')]
    a = np.array([int(v) for v in input().split(' ')])
    d = np.zeros(a.shape)
    d[1:] = a[1:] - a[:-1]
    d[0] = k - a[-1] + a[0]
    ans = k - np.max(d)
    print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
# coding: utf-8

# 標準入力 <int><int>
K, N = map(int, input().split())

# 標準入力 <int> list[]
A = list(map(int, input().split()))

lmax = 0

for a in range(N):
  if a != N - 1:
    if A[a + 1] - A[a] > lmax:
      lmax = A[a + 1] - A[a]
  else:
    if A[0] + K - A[a] > lmax:
      lmax = A[0] + K - A[a]

print(K - lmax)

Submission status: Accepted

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

m=max(m,k-a[-1]+a[0])
s+=(k-a[-1])+a[0]

print s-m

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
point=list(map(int,input().split()))
long=0
dist=0
for i in range(n-1):
  dist=point[i+1]-point[i]
  if dist>=long:
    long=dist
dist=k-point[n-1]+point[0]
if dist>=long:
  long=dist
print(k-long)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

A.sort()
max_distance = A[0] + K-A[-1]

old = None
for a in A:
    if not old:
        old = a
        continue
    distance = a - old
    if distance > max_distance:
        max_distance = distance
    old = a

answer = K - max_distance
print(answer)

Submission status: Accepted

Solution code:

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

arr = list(map(int, input().split()))
# arr.append(arr[0])

maxv = -float('inf')

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

maxv = max(maxv, k - arr[-1] + arr[0])

print (k - maxv)

Submission status: Accepted

Solution code:
from math import pi
K, N = map(int, input().split())
A = list(map(int, input().split()))
D = [0 for i in range(N)]
D[0] = abs(A[0] + (K - A[-1]))
for i in range(1, N):
  D[i] = abs(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()))

c=[0]*N
for i in range(N-1):
    c[i] = A[i+1]-A[i]
c.append(K-A[N-1]+A[0])
    
c.sort(reverse=True)

print(sum(c[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)]
for i in range(n-1):
  b[i] = a[i+1] - a[i]

b[-1] = a[0] + (k - a[-1])

print(k - max(b))

Submission status: Accepted

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

distance = [k] * n

for i in range(n - 1):
  distance[i] -= houses[i + 1] - houses[i]

distance[n - 1] -= k - houses[n - 1] + houses[0]

print(min(distance))

Submission status: Accepted

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

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

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

ans = max(ans, input_list[0] + K - input_list[-1])
print(K - ans)

Submission status: Accepted

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

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(D, A[i+1] - A[i])

D = max(D, K + A[0] - A[-1])
print(K - D)

Submission status: Accepted

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

m = K-A[-1] + A[0]
for i in range(len(A)-1):
    a = A[i+1] - A[i]
    m = max(m, a)
        
ans = K - m
print ans

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
diff_A = []
for n in range(N-1):
    diff_A.append(A[n+1] - A[n])
diff_A.append(K - A[-1] + A[0])

print(sum(diff_A)-max(diff_A))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

tot = A[0] + k - A[-1]
length = tot
for i in range(1, n):
    now = A[i] - A[i-1]
    tot += now
    length = max(length, now)

ans = tot - length
print(ans)

Submission status: Accepted

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

A.sort()

max_dis = A[0] + K - A[-1]
max_disidx = 0

for i in range(1, N):
    distance = A[i] - A[i-1]
    if max_dis < distance:
        max_dis = distance
        max_disidx = i

print(K-max_dis)

Submission status: Accepted

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

def calc_add(N):
    return N + k

A_2 = list(map(calc_add, A_1))

A_total = A_1 + A_2

min_d = k

i = 0
while i < n:
    if A_total[i + n -1] - A_total[i] <= min_d:
        min_d = A_total[i + n -1] - A_total[i]
    
    i += 1

print(min_d)

Submission status: Accepted

Solution code:
K,N=map(int,input().split(' '))
A=list(map(int,input().split(' ')))
#print(A)
D=[]
for i in range(len(A)):
    if i!=len(A)-1:
        d=A[i+1]-A[i]
    else:
        d=K-A[i]+A[0]
    D.append(d)

D.sort()
D.pop()
#print(D)

print(sum(D))

Submission status: Accepted

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

arr=[]

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

arr.append(l[0]+k-l[n-1])

m=max(arr)
print(k-m)

Submission status: Accepted

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

c=a+b
ans=10**9
for i in range(n+1):
  ans=min(ans,c[i+n-1]-c[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

total = k

minimum = 10000000000000
for i in range(n):
    if i == 0:
        dist = total - ((k - as_[-1]) + as_[0])
    else:
        dist = total - (as_[i] - as_[i - 1])

    minimum = min(minimum, dist)

print(minimum)

Submission status: Accepted

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

Submission status: Accepted

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

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

if (K-A[N-1]+A[0]) > max_a:
    print(A[N-1]-A[0])
else:
    print(K-max_a)

Submission status: Accepted

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

	else:
		delta.append(a[i]-a[i-1])
print(sum(delta)-max(delta))

Submission status: Accepted

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

print(k - ans)

Submission status: Accepted

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

print(K-max(C))

Submission status: Accepted

Solution code:
import sys
import math
import numpy

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

sorted(A)

total = K

for i in range(N):
    tmp =0
    if i < N -1:
        tmp = A[i+1] - A[i]
        total = min(K-tmp , total)
    else:
        tmp = K - A[i] + A[0]
        total = min(K-tmp , total)


print(total)

Submission status: Accepted

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

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

print(K-long)

Submission status: Accepted

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

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

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

print(K-maxlen)

Submission status: Accepted

Solution code:
import sys,math
sys.setrecursionlimit(10**9)
from collections import defaultdict
 
IA = lambda: map(int,input().split())
IAS= lambda: map(str,input().split())


L, n=IA()
a=list(IA())
a.sort()
cha=[0 for i in range(n)]

cha[0]=a[0]+L-a[n-1]

summ=cha[0]
maxx=cha[0]

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

print(summ-maxx)

Submission status: Accepted

Solution code:
import sys

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

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

minimum_distance = K - max(distances)
print(minimum_distance)

Submission status: Accepted

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

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

dis = []
for i in range(len(list)):
  if i ==0:
    dis.append(list[i] + (k - list[i-1]))
  else:
    dis.append(list[i] - list[i-1])

print(sum(dis)- max(dis))

Submission status: Accepted

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