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:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

K,N = imn()
A = iln()

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

if max_index == 0:
    print(A[-1] - A[0])
else:
    print(A[-1]-A[0]-max+diff)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# C - Traveling Salesman around Lake
# 標準入力の取得
K,N = list(map(int, input().split()))
A_LIST = list(map(int, input().split()))

# 各家間の距離を算出し、その最大値だけ避ける
## 要素0は、A_N～A_1の距離を入れる
diff_list = [A_LIST[i] - A_LIST[i-1] if i != 0 else A_LIST[0] + (K-A_LIST[N-1]) for i in range(N)]
print(K - max(diff_list))

Submission status: Accepted

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

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

Submission status: Accepted

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

min_dist = K
for i, Ai in enumerate(A):
  clockwise = (A[(i - 1) % N] - Ai) % K
  counter_clockwise = (K - (A[(i + 1) % N] - Ai)) % K
  if clockwise < min_dist:
    min_dist = clockwise
  if counter_clockwise < min_dist:
    min_dist = counter_clockwise

print(min_dist)

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    ans = float('inf')
    for i in range(N):
        if A[i] > A[i-1]:
            tmp = (K + A[i-1]) - A[i]
        else:
            tmp = A[i-1] - A[i]
        ans = min(ans, tmp)

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

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

a.sort()
ans = a[n-1]-a[0]
for i in range(n):
    tmp = a[i-1]-a[i]
    tmp%=k
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
k, n = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
a.append(k + a[0])
tmp = 0
maxi = 0
end = 0
for i, aa in enumerate(a):
    l = aa - tmp
    if l > maxi:
        maxi = l
        end = i
    tmp = aa
print(k - (a[end] - a[end - 1]))

Submission status: Accepted

Solution code:
str1 = input()
k = int(str1.split()[0])
n = int(str1.split()[1])
stra = input().split()
a = [int(x) for x in stra]
maxlen = k - a[n - 1] + a[0]
for i in range(1, n):
    if a[i] - a[i - 1] > maxlen:
        maxlen = a[i] - a[i - 1]
print(k - maxlen)

Submission status: Accepted

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

la = 0

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K, N = i_l()
A = i_l()
A = A + [x + K for x in A]

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

print(min(ans))

Submission status: Accepted

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

distance = []

A.sort()

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

diff = []

for i in range(N_ken - 1):
    d = A[i + 1] - A[i]
    diff.append(d)

diff.append(K_meter - A[N_ken - 1] + A[0])

summ = sum(diff)

ans = summ - max(diff)

print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = sorted(A)
C = [] * N
L = 0
for i in range(N):
    if i == 0:
        x = K - B[N - 1] + B[0]
    else:
        x = B[i - 1] - B[i]
    y = abs(x)
    if L < y:
        L = y
ans = K - L
print(ans)

Submission status: Accepted

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

b.append(k+a[0]-a[-1])
print(k-max(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import numpy as np
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    b = np.array(a + [a[-1]+k-a[-1]+a[0]])
    c = np.diff(b)
    ans = sum(c) - max(c)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k,n = map(int,input().strip().split())
ls = list(map(int,input().strip().split()))
mini1 = abs(ls[-1]-ls[0])
if n>1:
    mini_cons = -1
    sind = -1
    for i in range(1,n):
        ans = ls[i]-ls[i-1]
        if ans>mini_cons:
            sind = i-1
            mini_cons = ans
    if sind!=-1:
        mini1 = min(ls[sind]+k-ls[sind+1],mini1)
print(mini1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, n, *a = map(int, open(0).read().split())
s = 0
for i in range(n):
    s = max(s, a[i] - a[i-1])
print(K - max(s, a[0]+K-a[-1]))

Submission status: Accepted

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().strip().split())))
d = np.zeros(n,dtype=np.int64)
d[1:] = a[1:] - a[:-1]
d[0] = a[0] + (k-a[-1])

#print(d)
m = 10**9
q = d[0:-1].sum()
for i in range(n):
    if i == 0:
        q = d[1:].sum()
    else:
        q = q + d[i-1] - d[i]

    if q < m:
        m = q

print(m)

Submission status: Accepted

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

distances = []
for i in range(N):
    if i == N-1:
        distances.append(array[0] + (K - array[i]))
    else :
        distances.append(abs(array[i] - array[i+1]))
        
print(K - max(distances))

Submission status: Accepted

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

max_diff = -1
for i in range(n-1):
    max_diff = max(max_diff, a_list[i+1] - a_list[i])
max_diff = max(max_diff, abs(k-a_list[-1] + a_list[0]))
print(k-max_diff)

Submission status: Accepted

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

mi = 10000000
for i in range(N):
  if i == 0:
    d = A[-1] - A[0]
  else:
    d = K - (A[i] - A[i - 1])
  mi = min(d, mi)
print(mi)

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

Submission status: Accepted

Solution code:
import sys

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

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

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

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Submission status: Accepted

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

li_diff = []
for i in range(len(li) - 1):
    li_diff.append(li[i + 1] - li[i])
li_diff.append(k + li[0] - li[len(li) - 1])

ans = sum(li_diff) - max(li_diff)
print(ans)

Submission status: Accepted

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

def solve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = max((A[i]-A[i-1])%K for i in range(N))
    res = K-B
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = (int(each) for each in input().split(' '))

a = [int(each) for each in input().split(' ')]
distance = []
for i in range(len(a)-1):
    distance.append(a[i+1]-a[i])

distance.append(a[0] + (k-a[-1]))

print(k - max(distance))

Submission status: Accepted

Solution code:
def resolve():
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))
    t = [0] * len(A)
    max_v = K - A[-1] + A[0]
    curr_sum = max_v
    for i in range(1, len(A)):
        max_v = max(max_v, A[i] - A[i-1])
        curr_sum += A[i] - A[i-1]
    print(curr_sum - max_v)

resolve()

Submission status: Accepted

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

Bs = [k - As[-1] + As[0]]

for i in range(1, n):
    Bs.append(As[i] - As[i - 1])

print(k - max(Bs))

Submission status: Accepted

Solution code:
def input_int_list():
    return [int(thing) for thing in input().split(" ")]

K,N=input_int_list()
A_list=sorted(input_int_list())
A_list=A_list+[A_list[0]+K]
max_dis=0
for i in range(N):
    max_dis=max(max_dis,A_list[i+1]-A_list[i])
print(K-max_dis)

Submission status: Accepted

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

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

print(k - ma)

Submission status: Accepted

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

Submission status: Accepted

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

maxx = 0
for i,v in enumerate(A):
    if i == 0:
        right = A[i+1]
        a ,b = K-A[-1]+A[0], abs(v-right)
    elif i == len(A)-1:
        left = A[i-1]
        a, b = abs(v-left) , K-v+A[0]
    else:
        left,right = A[i-1],A[i+1]
        a, b = abs(v-left) , abs(v-right)


    if maxx < a:
        T = i
        turn = 'right'
        maxx = a
    
    if maxx < b:
        T = i
        turn = 'left'
        maxx = b

print(K - maxx)

Submission status: Accepted

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

doublelist = alist.copy()

for a in alist:
    doublelist.append(k + a)

# print(doublelist)

res = float('inf')
prev = float('inf')

for i in range(n + 1):
    prev = doublelist[i + n - 1] - doublelist[i]
    # print(prev)
    res = min(res, prev)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n,*a_l = map(int, open(0).read().split())
a_before = 0
del_a = k-a_l[-1] + a_l[0]
for a in a_l:
    del_a = max(del_a, a - a_before)
    a_before = a
print(k - del_a)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
T = [0] * n

A.sort()

for i in range(n):
    if abs(A[i] - A[i - 1]) <= k / 2:
        T[i] = abs(A[i] - A[i - 1])
    else:
        T[i] = k - abs(A[i] - A[i - 1])

print(k - max(T))

Submission status: Accepted

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

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

print(K-M)

Submission status: Accepted

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

max = k -A[n-1] + A[0]
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:
k,n = list(map(int,input().split()));
A = list(map(int,input().split()));

A.sort();

Am = [0]*n;
for i in range(n-1) :
    Am[i] = A[i+1] - A[i];
Am[n-1] = k-A[n-1] + A[0];

Am.sort();

print(sum(Am)-Am[n-1]);

Submission status: Accepted

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

###
# main
if(__name__ == '__main__'):
    # input
    k, n = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))

    a.sort()

    d_max = 0
    for elem in range(0,len(a)):
        d = 0
        if elem == len(a) - 1:
            d = k - a[elem] + a[0]
        else:
            d = a[elem+1] - a[elem]

        if d > d_max:
            d_max = d

    ans = k - d_max
    
    print(ans)
# else:
    # do nothing

Submission status: Accepted

Solution code:
def Main(K, N, A):
    dist = [0]*N
    for i in range(1, N):
        dist[i-1] = A[i] - A[i-1]
    dist[N-1] = K - A[N-1] + A[0]
    return sum(dist) - max(dist)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

max_dist = 0
for i in range(len(x)):
  now_dist = 0
  if i == 0:
    now_dist = k - (x[len(x)-1] - x[0])
  else:
    now_dist = x[i] - x[i-1]
  if now_dist > max_dist:
    max_dist = now_dist
print(k - max_dist)

Submission status: Accepted

Solution code:
def main():
    K,N,*A=map(int, open(0).read().split())
    A+=[A[0]+K]
    print(K-max(abs(A[i+1]-A[i]) for i in range(N)))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

def main():
    K, _ = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    # 最後の家と最初の家の距離
    A.append(K+A[0])
    X = np.diff(np.array(A))
    print(X.sum() - X.max())

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:

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

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

max_diff = 0
for i in range(1,n):
    if i != 0:
        max_diff = max(abs(An[i] - An[i-1]), max_diff)

max_diff = max(max_diff, k-An[n-1] + An[0])
print(k-max_diff)

Submission status: Accepted

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

end = a[0] + k - a[n-1]
b = []
b.append(end)

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

b.sort(reverse=True)
max = b[0]
print(k-max)

Submission status: Accepted

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

la = len(a)
for i in range(la):
    d = a[(i+1)%la] - a[i]
    if amax < a[(i+1)%la] - a[i]:
        amax = a[(i+1)%la] - a[i]
if amax < k - a[-1] + a[0] :
    amax = k - a[-1] + a[0]

print(k - amax)

Submission status: Accepted

Solution code:
k, n, *a = map(int, open(0).read().split())

dis_max = 0

for i in range(1, len(a)):
  dis_i = a[i] - a[i-1]
  if dis_i > dis_max:
    dis_max = dis_i

if (k-a[-1]+a[0]) > dis_max:
  dis_max = (k-a[-1]+a[0])

ans = k - dis_max

print(ans)

Submission status: Accepted

Solution code:
k,n = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7
A =        list( map(int, input().split()) )	# 1 2 3 4 5 ...

dMax=(k-A[-1]+A[0])

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

ans = k - dMax
print(ans)

Submission status: Accepted

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

dis_max=0

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

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

print(k-dis_max)

Submission status: Accepted

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    s=[0]*n
    for i in range(n):
        if i==0:
            s[i] = abs(a[i]-a[i-1])
        else:
            s[i] = k-abs(a[i]-a[i-1])
    print(min(s))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(1,n):
    k_diff.append(a[i] - a[i-1])

k_diff.append((k-a[-1])+a[0])

print(k - max(k_diff))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
def main():
  nagasa, num_house = map(int, input().split())
  house_list = [int(t) for t in input().split()]
  
  max_length = 0
  for i in range(num_house):
    if i != num_house-1:
      dif = house_list[i+1] - house_list[i]
    if i == num_house-1:
      dif = nagasa - house_list[i] + house_list[0]
    if max_length < dif:
      max_length = dif
  print(nagasa - max_length)

main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
K, N = map(int,input().split()) 
A = list(map(int,input().split()))
A.append(A[0]+K)
B=[]
for i in range(1,N):
    B.append(A[i+1] - A[i])


print((K - max(B)))

Submission status: Accepted

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

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

print(K-max(D))

Submission status: Accepted

Solution code:
r, n = map(int, input().split())
num = list(map(int, input().split()))
ans = 0
for i in range(n-1):
  max_bet = abs(num[i] - num[i+1])
  ans = max(ans, max_bet)
bet = r-num[n-1]+num[0]
ans = max(ans, bet)
print(r-ans)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = float("inf")
for right in range(N):
    left = right - (N - 1)
    if left >= 0:
        ans = min(ans, A[right] - A[left])
    else:
        ans = min(ans, A[right] + (K - A[left]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    A.append(A[i]+K)
    
ans = 10**9+7

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin, stdout


def read():
    x = stdin.readline().rstrip()
    return x


def __main__():

    k, n = map(int, read().split())
    A = [int(i) for i in read().split()]

    A.sort()
    d = []
    a0 = A[0]
    for a in A[1:]:
        d.append(abs(a-a0))
        a0 = a
    d.append(abs(k - A[-1]) + A[0])

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

__main__()

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
X = list(map(int, input().split()))
b = 0
cs = []
for i in range(N):
    cs.append(X[i]-b)
    b = X[i]

cs[0] += K-X[-1]
print(K-max(cs))

Submission status: Accepted

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

Submission status: Accepted

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

kankaku = []
for i in range(len(A_list) - 1):
  kankaku.append(A_list[i+1] - A_list[i])
kankaku.append(A_list[0] - A_list[-1] + K)

_kankaku_max = max(kankaku)
print(K - _kankaku_max)

Submission status: Accepted

Solution code:
list_in = list(map(int, input().split()))
list_house = list(map(int, input().split()))
K = list_in[0]
N = list_in[1]
list_diff = [0] * N

for i in range(1, N):
    list_diff[i] = list_house[i] - list_house[i-1]

list_diff[0] = K + list_house[0] - list_house[N-1]

max_diff = max(list_diff)
start_idx = list_diff.index(max_diff)

sum = 0
for i in range(N-1):
    tmp_idx = ( start_idx + i + 1 ) % N
    sum += list_diff[tmp_idx]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[K,N] = [int(s) for s in input().rstrip().split(' ')]
L_A = [int(s) for s in input().rstrip().split(' ')]
s = L_A[0] + K - L_A[-1]
for i in range(N-1):
  if s < L_A[i+1] - L_A[i]:
    s = L_A[i+1] - L_A[i]
print(K-s)

Submission status: Accepted

Solution code:
import sys
import math
from collections import Counter

k, n = [int(s) for s in sys.stdin.readline().strip().split()]
a = [int(s) for s in sys.stdin.readline().strip().split()]

longest = -1
prev = a[0]

for i in range(1, n):
    ai = a[i]
    dist = ai - prev
    longest = max(longest, dist)
    prev = ai

last_cand = k - a[-1] + a[0]
longest = max(longest, last_cand)

print(k - longest)

Submission status: Accepted

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

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

print(k - lgt_dist)

Submission status: Accepted

Solution code:
import numpy as np
from sys import stdin

def get_result(data):
    K, N = data[0]
    A = data[1]
    tmp = A[0]
    inter_dist = []
    for i in range(1, len(A)):
        inter_dist.append(A[i]-tmp)
        tmp = A[i]
    inter_dist += [K-A[-1]+A[0]]
    ans = sum(inter_dist) - max(inter_dist)
    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Accepted

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

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

sorted_dist = sorted(dist)
print(sum(sorted_dist[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

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

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

    res = K - most
    print(res)



main()

Submission status: Accepted

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

Submission status: Accepted

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

# S = [0] * (N + 1)
# for i in range(N):
#     S[i + 1] = S[i] + A[i]


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

# 0
right = A[N - 1] - A[0]
left = K - A[0] - (K - A[N - 1])
ans = min(ans, min(right, left))

# N - 1
left = A[N - 1] - A[0]
right = K - A[0] - (K - A[N - 1])
ans = min(ans, min(right, left))


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-m)