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:
#!/usr/bin/env python3
import sys


def solve(K: int, N: int, A: "List[int]"):
    B = A+[A[i] + K for i in range(N)]
    answer = 10**9
    for i in range(N):
        answer = min(answer,B[i+N-1]-B[i])
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

A = [a % K for a in A]
A.sort()

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

print(K - max(b))

Submission status: Accepted

Solution code:


k,n = map(int, input().split())
A = list(map(int, input().split()))
x = []
for a in A:
    x.append(a%k)
x.sort()
t = 0
for i in range(len(x)-1):
    t = max(t, x[i+1]-x[i])
t = max(t, k-x[-1]+x[0])
print(k-t)

Submission status: Accepted

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

tmp = K-abs(A[0]-A[-1])

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

k,n = l_in()
A = l_in()
A.append(A[0]+k)
print(k-max(A[i+1]-A[i] for i in range(n)))

Submission status: Accepted

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

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

print(K-maxDis)

Submission status: Accepted

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

dif_list = []

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

print(k-max(dif_list))

Submission status: Accepted

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

Submission status: Accepted

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

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

d_list = []
prev = A_list[-1] - K
for a in A_list:
	d_list.append(a - prev)
	prev = a

print(sum(d_list) - max(d_list))

Submission status: Accepted

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

a = sorted(a)

mins = 0
for i in range(1, len(a)):
    if a[i] -  a[i-1] > mins:
        mins = a[i] - a[i-1]
if k - a[n-1] + a[0] > mins:
    mins = k - a[n-1] + a[0] 

print(k-mins)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#gcdは3.4.3だとfractionsにある
from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    k,n = mi()
    a = li()

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


main()

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

Submission status: Accepted

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

Submission status: Accepted

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

ls = ls + [a+l for a in ls]

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

Submission status: Accepted

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

L=[]

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

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

print(K-max(L))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    K, N = lr()
    A = lr()
    ans = 0
    for i in range(N-1):
        ans = max(A[i+1]-A[i], ans)
    ans = max(K-A[N-1]+A[0], ans)
    print(K-ans)
resolve()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    m = 0
    for i in range(len(A)):
        m = max(m, (A[i] - A[i - 1]) % K)
    return K - m

print(main())

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):
    B = []
    for i in range(N-1):
        B.append(A[i+1]-A[i])
    B.append(A[0] + K - A[N-1])
    C = []
    for b in B:
        C.append(K-b)
    print(min(C))
    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:
def main():
    K,N = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    res = A[-1] - A[0]
    for i in range(N-1):
        res = min(res, K - A[i+1] + A[i])
    print(res)
main()

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(abs(a[i+1] - a[i]))
dist.append(abs(k - a[-1] + a[0]))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    K, N = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    m = 0

    for a, b in zip(A, A[1:]):
        m = max(m, abs(a - b))

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

    print(K - m)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    K, N = map( int, input().split())
    A = list( map( int, input().split()))
    ans = K - A[0] - (K-A[N-1])
    for i in range(N-1):
        if K - (A[i+1] - A[i]) < ans:
            ans = K - (A[i+1] - A[i])
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(sum_d - max_d)

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

if __name__=='__main__':
    main()

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
k,n=map(int,input().split())
A=[int(i) for i in input().split()]
ans=k-(k-A[-1]+A[0])
for i in range(n-1):
  ans=min(ans,k-A[i+1]+A[i])
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
K, N = map(int, input().split())
a = list(map(int, input().split()))
d = [0] * N
for i in range(N): d[i] = abs(a[i - 1] - a[i])
d[0] = min(d[0], K - d[0])
print(sum(d) - max(d))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(k-max_diff)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
k,n=map(int,input().split())
a_list=list(map(int,input().split()))
ans=10**9
for i in range(n):
    if i==0:
        t=a_list[n-1]-a_list[0]
        ans=t
    else:
        t=k-a_list[i]+a_list[i-1]
        ans=min(ans,t)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int,input().split())
a = list(map(int,input().split()))
res = np.max(np.diff(a))
res = max(res, abs(K+a[0]-a[-1]))
print(K-int(res))

Submission status: Accepted

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

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

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

print(k - ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
list_a = list(map(int, input().split()))
max_distance = 0
for i in range(n):
    now_pos = list_a[i]
    prev_pos = list_a[i-1]
    distance = now_pos - prev_pos
    if(now_pos < prev_pos):
        distance += k
    if(max_distance < distance):
        max_distance = distance
print(k - max_distance)

Submission status: Accepted

Solution code:

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

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

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

print(k-mx)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

k,n,*a = [int(i) for i in read().split()]

d = max(j-i for i,j in zip(a,a[1:]))
d = max(d,k-a[-1]+a[0])

print(k-d)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ma = 0
for i in range(1, N):
    d = As[i] - As[i-1]
    ma = ma if ma > d else d

d = K - As[-1] + As[0]
ma = ma if ma > d else d
print(K - ma)

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def check(s):
    for i in range(len(s)):
        if s[i] != s[len(s)-1-i]:
            return False
    return True

def main():
    k, n = rli()
    la = rli()
    la.sort()
    m = 0
    for i in range(n):
        l = la[i]
        r = la[(i+1)%n]
        if l > r:
            r += k
        m = max(m, r-l)
    print(k-m)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

diff_max = 0

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

diff_max = max(diff_max, a_list[0] + (k - a_list[-1]))

print(k - diff_max)

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

Submission status: Accepted

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

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

A.append(K+A[0])

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

print(sum(B)-max(B))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Adelta = [A[i+1]-A[i] if i < (N-1) else A[0] + (K - A[N-1]) for i in range(N)]

AdeltaMax = max(Adelta)

print(K - AdeltaMax)

Submission status: Accepted

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

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

print(K - M)

Submission status: Accepted

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


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[-1]

B.sort()
ans = sum(B[:-1])
print(ans)

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

Submission status: Accepted

Solution code:
# C
K, N = map(int, input().split())
*A, = map(int, input().split())

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

print(K-maxd)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k - max(b))

Submission status: Accepted

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

a.append(a[0] + k)

ans = 0
for i in range(n):
    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()))

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

print(k - max(diff))

Submission status: Accepted

Solution code:
# n = int(input())
# s = input()
n, k = map(int,input().split())
da = list(map(int,input().split()))
# a = [int(input()) for i in range(n)]

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

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    k,n=INTM()
    A=LIST()
    As=[]
    for i in range(n-1):
        As.append(A[i+1]-A[i])
    As.append(k+A[0]-A[n-1])
    print(k-max(As))
    
if __name__ == '__main__':
    do()

Submission status: Accepted

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


main()

Submission status: Accepted

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

diff_max = 0
for a, b in zip(A[:-1], A[1:]):
    diff_max = max(diff_max, b - a)

print(k - diff_max)

Submission status: Accepted

Solution code:
#create date: 2020-03-28 21:04

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    k, n = na()
    a = na()
    lmax = a[0] + (k-a[-1])
    for ai, aj in zip(a[0:-1], a[1:]):
        lmax = max(lmax, (aj-ai))
    print(k-lmax)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

B = [b-a for b,a in zip(A[1:],A[:-1])]

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

a.append(a[0] + k)
max_diff = 0
for a1, a2 in zip(a, a[1:]):
    max_diff = max(max_diff, a2 - a1)
print(k - max_diff)

Submission status: Accepted

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


b=[a[i]-a[i-1] for i in range(1,n)] + [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()))
a.append(k+a[0])
m = 0
for i in range(n):
    m = max(m, abs(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()))
B = [b-a for a,b in zip(A,A[1:])]
B.append(A[0] + K-A[-1])
print(K - max(B))

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    K, N = map(int, input().split())
    A = [int(a) for a in input().split()]
    longestL = A[0] + (K - A[N - 1])
    for i in range(N - 1):
        longestL = max(longestL, A[i+1] - A[i])
    print(K - longestL)


    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

max_gap = 0

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

print(K - max_gap)

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

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

ans = K - (A[0] + K - A[-1])
for a, b in zip(A, A[1:]):
    ans = min(K - (b - a), ans)
print(ans)

Submission status: Accepted

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

saidai=alist[0]-alist[-1]+k
for i in range(n-1):
    if alist[i+1]-alist[i]>saidai:
        saidai=alist[i+1]-alist[i]

print(k-saidai)

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
B = A + K

A = np.concatenate((A, B)).tolist()
ans = 10 ** 18
for i in range(N):
    d = A[i + N - 1] - A[i]
    ans = min(d, ans)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

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

max_len = 0

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

print(K - max_len)

Submission status: Accepted

Solution code:
import sys
import itertools
import bisect
from operator import itemgetter
 
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

K,N = IL()
a = IL()

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def examA():
    S = SI()
    if S[2]==S[3] and S[4]==S[5]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    X = I()
    ans = 0
    ans += (X//500) * 1000
    X %= 500
    ans += (X//5) * 5
    print(ans)
    return

def examC():
    K, N = LI()
    A = LI()
    L = [0]*N
    L[0] = K-A[-1] + A[0]
    for i in range(1,N):
        L[i] = A[i]-A[i-1]
    ans = sum(L) - max(L)
    #print(L)
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examC()

"""

"""

Submission status: Accepted

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

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

print(K-max_num)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-mx)

Submission status: Accepted

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

Submission status: Accepted

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