Problem:


Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.



Sample input:
20 3
5 10 15


Sample output:
10

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a= list(map(int, input().split()))
b=[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_lis = list(map(int, input().split()))
a_lis = sorted(a_lis)
longest = 0
for i in range(n):
    if i != n-1:
        longest = max(longest, abs(a_lis[i+1]-a_lis[i]))
    else:
        longest = max(longest, abs(a_lis[n-1]-(a_lis[0]+k)))
print(k - longest)

Submission status: Accepted

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

print(k - max(dis))

Submission status: Accepted

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

dif_max = 0

for i in range(N):
    if i == 0:
        dif = A[0] + (K - A[-1])
    else:
        dif = abs(A[i] - A[i-1])
    if dif > dif_max:
        dif_max = dif

print(K - dif_max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import string
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

K,N = nums()
A = nums()

ans = K*2

for i in range(N):
    start = A[i]
    goal = A[i-1]
    if goal < start:
        goal = K + goal
    dis = goal - start
    if dis < ans:
        ans = dis


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = k - da
print(ans)

Submission status: Accepted

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

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

a.sort()

b = []

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

b.append(k - sum(b))

ans = sum(b) - max(b)

print(ans)

Submission status: Accepted

Solution code:
import math

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

max_dist = None

for i in range(N-1):
    dist = A[i+1] - A[i]
    if max_dist is None or max_dist < dist:
        max_dist = dist

dist = A[0] + K - A[N-1]
if max_dist < dist:
    max_dist = dist

print(K - max_dist)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def S(): return input().rstrip()


def LS(): return S().split()


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


mod = 1000000007
inf = 1e10

#solve


def solve():
    k, n = LI()
    
    a = LI()
    a = a + list(map(lambda x: x + k, a))
    ans = inf
    for i in range(n):
        ans = min(ans, a[i + n] - a[i] , k - (a[i + 1] - a[i]))
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

A.sort()
l_max = A[0] + (K - A[N-1])
for i in range(N-1):
    l = A[i+1] - A[i]
    if l > l_max:
        l_max = l

print(K - l_max)

Submission status: Accepted

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

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):
    if i == n - 1:
        dis = abs(k - a[i] + a[0])
    else:
        dis = abs(a[i] - a[i + 1])
    if dis > dis_max:
        dis_max = dis

print(int(k - dis_max))

Submission status: Accepted

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

sa = []

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

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

ma = max(sa)
sa.remove(ma)

print(sum(sa))

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):
    m = 0
    for i in range(N):
        d = A[i] if i == 0 else A[i] - A[i - 1]
        m = max(m, d)
        if i == N - 1:
            m = max(m, K + A[0] - A[i])
    print(K - m)

    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
k,n=map(int,input().split())
a=np.array(list(map(int,input().split())))

a_diff=np.diff(a)
a_diff2=(k - a[n-1])+a[0]
diff_max=np.max(np.append(a_diff,a_diff2))
print(k-diff_max)

Submission status: Accepted

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

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

print(K - maxLen)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int, input().split())
a = list(map(int, input().split()))
dist = []#家間の距離
for i in range(len(a)-1):
    dist.append(a[i+1] - a[i] )
dist.append(k - a[-1] + a[0])
print(sum(dist) - max(dist))

Submission status: Accepted

Solution code:
import numpy as np
k,n = [int(i) for i in input().split()]

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

A.sort()

ans_p = A[0] + k - A[-1]

for i in range(n-1):
    cand = A[i+1] - A[i]
    if cand > ans_p:
        ans_p = cand
        
ans = k - ans_p

print(ans)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
arr=[int(x) for x in input().split()]
dist=[arr[x]-arr[x-1] for x in range(1,n)]
dist.append(k-arr[-1] + arr[0])
print(min([k-item for item in dist]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
d,n=map(int,input().split())
l=list(map(int,input().split()))
al=[]
for i in range(1,len(l)):
    al.append(l[i]-l[i-1])
x=l[0]+d-l[n-1]
al.append(x)
#print(al)
mx=max(al)
ss=sum(al)
print(ss-mx)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[int(j) for j in input().split()]
ma=0
sum=0
for i in range(1,m):
    diff=a[i]-a[i-1]
    ma=max(ma,diff)
    sum+=diff

diff=n-sum
ma=max(ma,diff)
print(n-ma)

Submission status: Accepted

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

def main():
  k, n = map(int, input().split())
  la = list(map(lambda x:int(x)%k, input().split()))
  la.sort()
  diff = [(la[i] - la[i-1])%k for i in range(len(la))]
  print(k - max(diff))
  

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
K, N = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
Adif = []
for i in range(1, len(A)):
    dif = A[i] - A[i-1]
    Adif.append(dif)
Adif.append(K - A[-1] + A[0])
Adif.remove(max(Adif))
ans = sum(Adif)

print(ans)

Submission status: Accepted

Solution code:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

k, n = load_list()
A = load_list()


def dist(a, b):
    if b > a:
        return b - a
    else:
        return (k-a) + b

ds = []
for i in range(n):
    if i == n-1:
        ds.append(dist(A[i], A[0]))
    else: 
        ds.append(dist(A[i], A[i+1]))

print(sum(ds) - max(ds))

Submission status: Accepted

Solution code:
# coding: utf-8

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

if l_max < K - A[-1] + A[0]:
    l_max = K - A[-1] + A[0]

ans = K - l_max
print(ans)

Submission status: Accepted

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

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

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

max_d = max(max_d, 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()))
dis = [A[i+1]-A[i] for i in  range(N-1)]
dis.append(K-A[-1]+A[0])
# print(dis)
dis.remove(max(dis))
print(sum(dis))

Submission status: Accepted

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

Submission status: Accepted

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

A = sorted(A)
B = []

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

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()))
a.sort()
A=a[-1]
iti=k-A
hantei=10**9
for i in range(1,n-1):
  #右回り
  sr=k+a[i-1]-a[i]
  #左回り
  sl=k-(a[i+1]-a[i])
  if hantei>sr:
    hantei=sr
  if hantei>sl:
    hantei=sl
if hantei>(a[-1]-a[0]):
  hantei=(a[-1]-a[0])
print(hantei)

Submission status: Accepted

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

A.insert(0, A[N-1] - K)
diffs = []
for i in range(1, len(A)):
    diff = A[i] - A[i-1]
    diffs.append(diff)

print(K - max(diffs))

Submission status: Accepted

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

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

print(k-b)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Ad = [A[i+1] - A[i] if i != N-1 else K-A[-1]+A[0] for i in range(len(A))]
print(sum(Ad)-max(Ad))

Submission status: Accepted

Solution code:
import numpy as np

K,N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = []
maxdist = 0
for i in range(N-1):
    B.append(A[i+1]-A[i])
    if B[-1]>maxdist:
        maxdist = B[-1]

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

print(K - maxdist)

Submission status: Accepted

Solution code:
K, N= map(int, input().split())
A = list(map(int, input().split()))
A.append(K+A[0])
M=0
for i in range(N):
    if M<=(A[i+1]-A[i]):
        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()))
D = list()
D.append(A[0] + K - A[-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 = list(map(int, input().split()))
arry = list(map(int, input().split()))
t_arry = []
for i in range(0,len(arry)-1):
    t_arry.append(arry[i+1]-arry[i])
# 始点と終点の距離
dist = arry[0] + k - arry[len(arry)-1]
t_arry.append(dist)

print(sum(t_arry) - max(t_arry))

Submission status: Accepted

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

A.append(K + A[0])

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

print(K - distance)

Submission status: Accepted

Solution code:
first = str(input()).split(' ')
second = str(input()).split(' ')

k = int(first[0]) #metri
n = int(first[1]) #case
A = [int(i) for i in second]

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

Submission status: Accepted

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

a = sorted(a)
miner = a[-1] - a[0]
for i in range(1, n):
    miner = min(miner, abs(a[- 1  + i] + k - a[i]))

print(miner)

Submission status: Accepted

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

Submission status: Accepted

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

print(K - max(D))

Submission status: Accepted

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

Submission status: Accepted

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

now = k - l[n-1] + l[0]

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

Submission status: Accepted

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

A = sorted(A)
max_l = 0

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

Submission status: Accepted

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

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


print(K-max_diff)

Submission status: Accepted

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

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

Submission status: Accepted

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

As = [A - As[0] for A in As]
As_diffs = [As[i+1] - As[i] for i in range(len(As)-1)]
As_diffs += [K - As[-1]]
As_fiff_max = max(As_diffs)

# print('-----output-----')
print(K - As_fiff_max)

Submission status: Accepted

Solution code:
import sys
k, n = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
L=[]
for i in range(0, n-1):
    L.append(abs(A[i+1]-A[i]))
L.append(k-A[-1]+ A[0])
#print(L)
print(sum(L)-max(L))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
k, n = map(int, input().split())
a = list(map(int, input().split()))

sum_dist = 0
dist = [0] * n

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

dist[n-1] = a[0] + k - a[n-1]
sum_dist += dist[n-1]

result = sum_dist - max(dist)

print(result)

Submission status: Accepted

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

dis = [0] * n

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

dis[n-1] = min(abs(a[n-1] - a[0]), abs((a[0]+k)-a[n-1]))

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

Submission status: Accepted

Solution code:
k, n, *A = map(int, open(0).read().split())
B = [a1-a0 for a0, a1 in zip(A, A[1:])] + [k-A[-1]+A[0]]
print(sum(B) - max(B))

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = [int(i) for i in input().split()]
a_list += [a_list[0]+k]

a_list = np.array(a_list)

dif = a_list[1:] - a_list[:-1]
dif_max = np.max(dif)

print(k-dif_max)

Submission status: Accepted

Solution code:
KN = list(map(int,input().split()))
A = list(map(int,input().split()))
maxi = A[0] - A[KN[1]-1] + KN[0]
result = 0

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

print(KN[0]-maxi)

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(len(A) - 1)]
print(k - max(B))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


K, N = inp_list()
A = inp_list()

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

print(K - max_dist)

Submission status: Accepted

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


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

print(k-max(dist))

Submission status: Accepted

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

print(K-A_max)

Submission status: Accepted

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

diff_A = [K - A[-1] + A[0]] + [A[i+1] - A[i] for i in range(len(A) - 1)]
max_diff = max(diff_A)
print(K - max_diff)

Submission status: Accepted

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

ans = float("inf")
for i in range(N-1):
    # 右回りを実現
    if i == 0:
        ans = min(ans, A[N-1]-A[i])
    else:
        ans = min(ans, (K-A[i])+A[i-1])

    # 左回りを実現
    if i == 0:
        ans = min(ans, A[i]+(K-A[i+1]))
    else:
        ans = min(ans, A[i]+(K-A[i+1]))

# # 最後の家を考える
# 左回り
ans = min(ans, A[N-1]-A[0])
# 右回り
ans = min(ans, K-A[N-1]+A[N-2])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

B = deque(A)
b = B.pop()
B.appendleft(b - K)

print(K - max(a - b for a, b in zip(A, B)))

Submission status: Accepted

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

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

Submission status: Accepted

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

import numpy as np
diff = np.array(A[1:] + [K + A[0]]) - np.array(A)
print(K - diff.max())

Submission status: Accepted

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

# 一番遠いところを避ければ良い
max = 0
for i in range(N-1):
    if As[i+1] - As[i] > max:
        max = As[i+1] - As[i]

if (As[0]+K) - As[N-1] > max:
    max = (As[0]+K) - As[N-1]

print(K-max)

Submission status: Accepted

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

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

for i in range (n-1):
    d = a[i+1]-a[i]
    if d > c:
        c = d
    else:
        c = c

s = k - c

print (s)

Submission status: Accepted

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

max_diff = 0

for i in range(len(A)):
    if i == 0:
        max_diff = abs(K - A[-1]) + A[i]
    elif i == len(A) -1:
        break
    else:
        if max_diff < A[i+1] - A[i]:
            max_diff = A[i+1] - A[i]

print(K - max_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
Created on 2020/03/28

@author: fanta
'''
N,K = map(int,input().split())
A = [int(x) for x in input().split()]
B = []

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

print(N - max(B))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum(total)-max(total))

Submission status: Accepted

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

mi = 0

for n in range(N):
    j = n
    k = (n + 1) % N
    i = (A[k] - A[j] + K) % K
    if i > mi:
        mi = i

print(K-mi)

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):
    a = k-a_[i]+a_[i-1]
    ans = min(ans,a)
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
total, N = map(int, input().split())
nums = list(map(int, input().split()))
if N == 1:
    print(0)
else:
    ans = total - (total - nums[-1] + nums[0])
    prev = nums[0]
    for num in nums[1:]:
        ans = min(total-(num - prev), ans)
        prev = num
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, n):
    list_ans.append(list_A[i]-list_A[i-1])

print(k-max(list_ans))

Submission status: Accepted

Solution code:
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(abs(a[0] + k - a[-1]))
b.sort()

print(sum(b[:-1]))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
li1 = list(map(int, input().split()))
i = 0
li2 = []
while i <= N-2:
  li2.append(li1[i+1] - li1[i])
  i += 1
li2.append(li1[0]+K - li1[N-1])
print(K - max(li2))

Submission status: Accepted

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

k,n = miinps()
a = lmiinps()

l = -1

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

if a[0] == 0:
    l = max(l,k-a[n-1])
else:
    l = max(l,(k-a[n-1])+a[0])

print(k-l)

Submission status: Accepted

Solution code:
import math
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)


K,N = nm()
A  = nl()

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

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

print(K - maxL )

Submission status: Accepted

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

list_distance=[]
for i in range(n-1):
    list_distance.append(list[i+1]-list[i])
list_distance.append(k-(list[n-1]-list[0]))
print(k-max(list_distance))

Submission status: Accepted

Solution code:
k, n = list(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:
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

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

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
for i in range(n):
  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()))
a.append(k+a[0])
d=0
for i in range(len(a)-1):
    if i == len(a):
        break
    else:
        if d < a[i+1] - a[i]:
            d = a[i+1] - a[i]
print (k-d)

Submission status: Accepted

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

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

Submission status: Accepted

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

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