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 sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


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




if __name__ == "__main__":

    main()

Submission status: Accepted

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

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

print(k-mn)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(K - l)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - max(B))

Submission status: Accepted

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

b=[0 for i in range(N)]
for i in range(N):
    if i ==0:
        b[i] = a[0] +(K-a[N-1])
    else:
        b[i] = a[i]-a[i-1]


minb = max(b)
print(K-minb)

Submission status: Accepted

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

Submission status: Accepted

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

max_dis=0
tot_dis=0
posi=a[0]
for i in range(1,len(a)):
    tmp=a[i]-posi
    if max_dis < tmp:
        max_dis=tmp
    posi=a[i]
    tot_dis+=tmp
tmp=a[0]+(k-a[-1])
if max_dis < tmp:
    max_dis = tmp
tot_dis += tmp

print(tot_dis-max_dis)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

d = K - A[-1] + A[0]
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()))
X=[]
for i in range(n-1):
    X.append(A[i+1]-A[i])
X.append(A[0]+k-A[n-1])
print(k-max(X))

Submission status: Accepted

Solution code:
k,n,*a=map(int,open(0).read().split())
a.sort()
print(k-max([i-j for i,j in zip(a[1:],a)]+[k-a[-1]+a[0]]))

Submission status: Accepted

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

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

print(K-max(diff))

Submission status: Accepted

Solution code:
k,n = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
ds = [0] * n # iとi+1の距離
for i in range(n-1):
    ds[i] = a[i+1] - a[i]
ds[n-1] = (k - a[n-1]) + a[0]
m = max(ds)
print(k - m)

Submission status: Accepted

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

maxd = 0

b = 0
for a in As:
    maxd = max(maxd, a - b)
    b = a
maxd = max(maxd, K - As[-1] + As[0])

print(K - maxd)

Submission status: Accepted

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

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

max_d = -float("inf")
total_d = 0
for i, a in enumerate(a_list):
    if i == 0:
        d = (K - a_list[-1]) + a_list[i]
    else:
        d = a_list[i] - a_list[i - 1]
    total_d += d
    max_d = max(max_d, d)
ans = total_d - max_d

print(ans)

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


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

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

Submission status: Accepted

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

Submission status: Accepted

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

res = al[-1] - al[0]
for i in range(1,n):
    r = k - al[i] + al[i-1]
    res = min(res, r)

print(res)

Submission status: Accepted

Solution code:
K,N = map(int, input().split())
a = [int(c) for c in input().split()]
b = [a[(i+1)%N]-a[i] for i in range(N-1)]
b += [a[0]+K-a[N-1]]
ans = K-max(b)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


dist = 0
for x, y in zip(A, A[1:]):
    dist = max(dist, y-x)

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

print(k-dist)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,y=list(map(int, input().split()))
a=list(map(int, input().split()))
ans=0
for i in range(y-1):
	if ans<abs(a[i]-a[i+1]):
		ans=abs(a[i]-a[i+1])

if (x+a[0])-a[y-1]>ans:
	ans=(x+a[0])-a[y-1]

print(x-ans)

Submission status: Accepted

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

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

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

print(k - p)

Submission status: Accepted

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

print(k-mdist)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

#print(D)
ans = k - max(D)
print(ans)

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])
print(k-max(dist))

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

k,n = get_nums_l()
aaa = get_nums_l()
for i in range(n):
    aaa.append(aaa[i] + k)

ans = 99999999999999999999
for i in range(n):
    c = aaa[i+n-1] - aaa[i]
    ans = min(ans, c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

K, N = MAP()
A = LIST()

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

Submission status: Accepted

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

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

print(k - diff)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

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

dist = 0
start = -1
for i in range(N - 1):
    a = A[i + 1]
    pre = A[i]
    if dist < a - pre:
        dist = a - pre
        start = i

if dist < A[0] + (K - A[N - 1]):
    dist = A[0] + (K - A[N - 1])
    start = N - 1

print(K - dist)

Submission status: Accepted

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

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

print(K-distance_max)

Submission status: Accepted

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

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

tmp = (K - A[-1]) + A[0]
maximum = max(tmp, maximum)

print (K - maximum)

Submission status: Accepted

Solution code:
import numpy as np

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

D = np.diff(A)

print(int(K) - np.max(D))

Submission status: Accepted

Solution code:

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

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

dist = As[0] + (K - As[-1])
for i in range(len(As) - 1):
    dist = max(dist, As[i+1] - As[i])

print(K - dist)

Submission status: Accepted

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

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    K, N = LI()
    A = LI()
    ans = inf
    for i in range(len(A) - 1):
        dist = A[i+1] - A[i]
        ans = min(ans, K - dist)
    dist = A[0] + K - A[-1]
    ans = min(ans, K - dist)
    print(ans)

main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys 
MOD = 10 ** 9 + 7
INF = 10**11
from heapq import heapify,heappush,heappop

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

if __name__=='__main__':
    main()

Submission status: Accepted

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

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

print(K - max(diff))

Submission status: Accepted

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

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

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

print(k - maxD)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

a,b = m()

ppp = l()



dp = [0 for i in range(b)]

for i in range(b):
    if i != b-1:
        dp[i] = abs(ppp[i+1]-ppp[i])
    else:
        dp[i] = abs(a-ppp[-1]+ppp[0])

print(sum(dp)-max(dp))

Submission status: Accepted

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

dmax=0
a_old=-(k-a_list[-1])
for a in a_list:
    d=a-a_old
    if d > dmax:
        dmax=d

    a_old=a

print(int(k-dmax))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(k-max(d))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

delta_list = []

for a, b in zip(A[:-1], A[1:]):
    delta = b - a
    delta_list.append(delta)

delta_list.append(k - A[-1] + A[0])

candidates = [k-delta for delta in delta_list]
print(min(candidates))

Submission status: Accepted

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


ans = 10**12

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
K, N = [int(str) for str in input().strip().split()]
A = [int(str) for str in input().strip().split()]

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

print(K - max(diffs))

Submission status: Accepted

Solution code:
from sys import stdin, stdout 
from math import *
import itertools
import sys

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

d = max(d, k - a[-1] + a[0])
print(k - d)

Submission status: Accepted

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

import numpy as np

d = np.diff(a)
d = np.append(d, k - a[-1] + a[0])

print(np.sum(np.sort(d)[:-1]))

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=10**8
B=[]
for i in range(1,n):
    B.append(A[i]-A[i-1])
B.append(k-A[-1]+A[0])
print(k-max(B))

Submission status: Accepted

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

d = []

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

print(K-max(d))

Submission status: Accepted

Solution code:
import sys

n, k, *a = map(int, sys.stdin.read().split())
a.append(n + a[0])

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n) 


def main():
    K, N = read_values()
    A = read_list()
    A += [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:
k,n=map(int,input().split())
A=list(map(int,input().split()))
D=[A[i+1]-A[i] for i in range(n-1)]+[A[0]+k-A[-1]]
print(k-max(D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if maxi<=a[0]+(k-a[-1]):
    print(max(a)-min(a))
else:
    print(k-maxi)

Submission status: Accepted

Solution code:
import sys
k, n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()
m = a[0] + k - a[-1]
for i in range(1, n):
    m = max(m, a[i] - a[i - 1])
print(k - m)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K, N = [int(x) for x in input().strip().split()]
A = [int(x) for x in input().strip().split()]
A2 = [0] * len(A)
for i, a in enumerate(A[1:], start=1):
    A2[i] = A[i] - A[i-1]
A2.append(A[0] + K - A[-1])
print(K - max(A2))

Submission status: Accepted

Solution code:
def main():

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

    for v in dist:
        ans = min(ans, K - v)
    return ans


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    K,N = LMIIS()

    A = LMIIS()
    D = []
    pre = 0
    max_diff = A[0] + K - A[-1]
    for ai in A:
        max_diff = max(max_diff,ai-pre)
        pre = ai
    
    print(K-max_diff)

    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

al = sorted(al)

ans = 0

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

ans = max(al[0] + (k - al[-1]) , ans)

print(k-ans)

Submission status: Accepted

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

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

B = []

for i in range(b):
    begin = A[i]
    # end = A[-1+i]
    if i == 0:
        end = A[-1+i]
    else:
        end = A[-1+i] + a
    B.append(end - begin)
print(min(B))

Submission status: Accepted

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

ans = 10**16

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

tmp = K-(K+A[0]-A[N-1])
ans = min(tmp,ans)

print(ans)

Submission status: Accepted

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

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

print(K-ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-max_dist)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

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

ans=k-max(b)
print(ans)

Submission status: Accepted

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

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

print(k-maxi)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


def minimize(k, n, bs):
    deltas = [bs[i + 1] - bs[i] for i in range(n - 1)]
    deltas.append(bs[0] - bs[-1] + k)
    return k - max(deltas)


def main():
    k, n = (int(z) for z in input().split())
    bs = [int(b) for b in input().split()]
    res = minimize(k, n, bs)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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