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: Time Limit Exceeded

Solution code:
# coding: utf-8
from itertools import permutations


def solve(*args: str) -> str:
    k, n = map(int, args[0].split())
    A = list(map(int, args[1].split()))

    ret = k+1
    for P in permutations(A):
        total = 0
        cur = P[0]
        for a in P[1:]:
            total += min(abs(k-a)+cur, abs(a-cur))
            cur = a
        ret = min(ret, total)

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Time Limit Exceeded

Solution code:
I=lambda:list(map(int,input().split()))
p,n=I()
l=I()
mi=10**9+7
for i in range(n):
    te=0
    j=i
    while (j+1)%n!=i%n:
        if j==n-1:
            te+=abs(l[-1]-sum(l))
        else:
            te+=abs(l[j%n]-l[(j+1)%n])
        j+=1
    mi=min(te,mi)
l=l[::-1]
for i in range(n):
    te=0
    j=i
    while (j+1)%n!=i%n:
        if j==n-1:
            te+=abs(l[-1]-sum(l))
        else:
            te+=abs(l[j%n]-l[(j+1)%n])
        j+=1
    mi=min(te,mi)


print(mi)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = max(A) - min(A)

for i in range(N):
  A[-(i+1)] -= K
  if max(A) - min(A) < ans:
    ans = max(A) - min(A)

print(ans)

Submission status: Time Limit Exceeded

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])
ans = sum(dist)
for i in range(N):
    ans = min(ans,sum(dist)-dist[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
K,N=map(int, input().split())
A=list(map(int, input().split()))
max_diff = 0
for i in range(0,N):
    for j in range(i,N):
        ck = abs(A[j] - A[i])
        if max_diff < ck:
            if ck <= K/2:
                max_diff = ck
print(max_diff)

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


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

    K, N = il()
    A = il()
    A.sort()
    ret = max(A) - min(A)

    for n in range(N - 1):
        mi = min(A)
        tmp = abs(A[n] - mi) + (K - A[n + 1])
        ret = min(ret, tmp)

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(k-max(b))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
assert len(A) == N


dists = []
for i,start in enumerate(A):
  # 右回り
  posR = A[(i - 1) % N] + (K if A[(i - 1) % N] < start else 0)
  #max([a + K for a in A[:i]] + A[i+1:])
  # 左回り
  posL = A[(i + 1) % N] - (K if A[(i + 1) % N] > start else 0)
  #posL = min(A[:i] + [a - K for a in A[i+1:]])
#  print(start, posL, posR)
  dists.append(min(posR - start, - posL + start))
#print(dists)
print(min(dists))

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    K,N=MI()
    A=LI()
    S=sum(A)
    L=[]
    for i in range(N-1):
        temp=A[i+1]-A[i]
        L.append(temp)
        
    L.append(A[0]+K-A[-1])
    M=max(L)
    
    print(K-M)

main()

Submission status: Accepted

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

A3 = A + [K+a for a in A]
dist = [A3[i+N-1] - A[i] for i in range(N)]

print(min(dist))

Submission status: Accepted

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

A2 = [K+a for a in A]
A3 = A + A2

dist = [0] * N
for i in range(N):
    j = i + N - 1
    dist[i] = A3[j] - A[i]

print(min(dist))

Submission status: Accepted

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

max_d = 0

for a in range(1, N):
    d = A[a] - A[a-1]
    if d > max_d:
        max_d = d
last_d = K - A[-1] + A[0]
if last_d > max_d:
    max_d = last_d
print(K - max_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):
    tmp=A[i+1]-A[i]
    ans=max(ans,tmp)
ans=K-ans
print(ans)

Submission status: Accepted

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

k, n = LI()
a = LI()

# 一周kメートルの円形湖があり、n軒の家がある。
# i番目のいえは北端から時計回りにAメートルのところにある。
# いずれかの家から出発してn件すべての家に尋ねるための最短移動距離を求めよ

# 円形で考えるからややこしい
# 一番距離が長い家と家の区間を除外した直線状と考えればよいのでは。

far = []

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

    far.append(d)

longest = 0

for i in range(n):
    if far[i] > longest:
        longest = far[i]
        longest_i = i
del far[longest_i]
print(sum(far))

Submission status: Accepted

Solution code:
#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc160/tasks/abc160_c

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[-1] + A[0])

print(K - max(dist))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

da =[-1] * n
da[n-1] = k - a[n-1] + a[0]
for i in range(n-1):
    da[i] = a[i+1] - a[i]
    
print(sum(da)-max(da))

Submission status: Accepted

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

D = [0] * (N)

A2 = [A1[i]+K for i in range(len(A1)-1)]
A1 = A1+A2

for i in range(N):
    D[i] = A1[i+N-1] - A1[i]
    #print(A1[i],A1[i+N-1])
print(min(D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(len(A) - 1):
    dis = A[i + 1] - A[i]
    if dis > mx:
        mx = dis

print(K - mx)

Submission status: Accepted

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


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

    return


# Generated by 1.1.7.1 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 sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

c,n=sep()
ar=lis()
ar.sort()
m=-10
for i in range(n):
    t=min(abs(ar[i]-ar[((i-1) + n)%n]),c-abs(ar[i]-ar[((i-1) + n)%n]))
    #print(t)
    if t>m:
        m=t
print(c-m)

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
array = list(map(int,input().split()))
ans = 10**8
for i in range(N):
  if i == 0:
    ans1 = array[-1] - array[i]
  else:
    ans1 = array[i-1] + ( K - array[i] )
  if i == N-1:
    ans2 = array[-1] - array[0]
  else:
    ans2 = array[i] + ( K - array[i+1] )
  ans = min(ans,ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:

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

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

print(k - max(b))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-m)

Submission status: Accepted

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

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

B = []

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

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

b_max       = max(B)
b_max_index = B.index(max(B))


B.remove(b_max)
print(sum(B))

Submission status: Accepted

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

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

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

print(sum(b) - max(b))

Submission status: Accepted

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

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

y = a[0] + k - a[n-1]
root.append(y)

for i in root:
    ans += i

ans -= max(root)
print(ans)

Submission status: Accepted

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

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

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

print(sum(b)-max(b))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# import math
# import statistics
# import itertools
# a=int(input())
# b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(int(i))
A,B= map(int,input().split())
f = list(map(int,input().split()))
# g = [input().split for _ in range(a)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen
kyori=[]
for i in range(len(f)-1):
    an=f[i+1]-f[i]
    kyori.append(an)
    
an2=A-f[-1]+f[0]
ans=max(max(kyori),an2)
print(A-ans)

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
A = np.array(list(map(int, input().split())))
diff = np.append(A[1:] - A[:-1], K + A[0] - A[-1])
print(np.delete(diff, np.argmax(diff)).sum())

Submission status: Accepted

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

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

print(k - max(max_diff, k - abs(a[n-1] - a[0])))

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

d.sort()
d[n-1] = 0

print(sum(d))

Submission status: Accepted

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

d = []
for i in range(n - 1):
    d.append(a[i + 1] - a[i])
d.append(k - a[n - 1] + a[0])
d.sort()
print(sum(d[:-1]))

Submission status: Accepted

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

ans = 10 ** 6 + 1
for i in range(n):
  r_idx = ((i+1)%n)
  r_ans = a[i]+(k-a[r_idx]) if a[i] < a[r_idx] else abs(a[r_idx]-a[i])
  l_idx = (i-1+n)%n
  l_ans = (k-a[i])+a[l_idx] if a[i] > a[l_idx] else a[l_idx]-a[i]

  ans = min([ans, r_ans, l_ans])

print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
Alst = list(map(int, input().split()))
zero = Alst[0] + K
M = 0
now = Alst[0]
for i in Alst:
    dis = i - now
    if dis > M:
        M = dis
    now = i

last = zero - now
if last > M:
    M = last

print(K - M)

Submission status: Accepted

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

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

d1 = (k - A[n-1]) + A[0]
d = max(d, d1)
print(k-d)

Submission status: Accepted

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

for i in range(n-1):
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)

print(k-max(longest, k-a_list[n-1]+a_list[0]))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a_list = list(map(int, input().split()))
longest = 0
for i in range(n):
    if i==n-1:
        longest = max(longest, k-a_list[i]+a_list[0])
    else:
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)
print(k-longest)

Submission status: Accepted

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

longest = 0
for i in range(n):
    # if i==0:
    #     continue
    if i==n-1:
        longest = max(longest, k-a_list[i]+a_list[0])
    else:
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)

print(k-longest)

Submission status: Accepted

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

Submission status: Accepted

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

n=len(a)-1
cnt=[a[n]-a[0]]
for i in range(n) :
    cnt.append(k-(a[i+1]-a[i]))
ans=min(cnt)

print(ans)

Submission status: Accepted

Solution code:
k, n = map(int,input().split())
a = list(map(int,input().split()))
new_a = sorted(a)
num = len(new_a) - 1
s_a = []
b = (k - new_a[num]) + new_a[0]
s_a.append(b)
cun = 0
for i in new_a[1::]:
    s = i - new_a[cun]
    s_a.append(s)
    cun += 1
max_num = max(s_a)
print(sum(s_a) - max_num)

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(sum(b)-max(b))

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

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
ans=A[0]+K-A[-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()))
sa = []

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

print(K-max(sa))

Submission status: Accepted

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

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

    li.append(abs(A[0] + (K - A[-1])))
    print(sum(li) - max(li))
except:
    pass

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

a_array.append(k + a_array[0])

a_diff = np.diff(a_array)
ans = np.sum(a_diff) - np.max(a_diff)
print(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,k-a[n-1]+a[0])
print(k-temp)

Submission status: Accepted

Solution code:
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
rst = -1
for i in range(N):
    l = A_ls[(i + 1) % N] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % N]
    if rst == -1:
        rst = l
    else:
        rst = max(rst, l)
print(K - rst)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

max_d = max(d)

print(K - max_d)

Submission status: Accepted

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

Submission status: Accepted

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

a.sort()
a.append(a[0]+k)

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

print(k - ans)

Submission status: Accepted

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



A = sorted(A)
L = []
ANS = 0

A_max = max(A)

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

L = sorted(L)


L.pop(-1)

for i in range(len(L)):
    ANS = ANS + L[i]
    
print(ANS)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

    print(K - max(tmp1, tmp2))


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(len(a)-1):
    if i == 0:
        maxDis = a[i]+k-a[i-1]
    elif abs(a[i]-a[i-1]) > maxDis:
        maxDis = abs(a[i]-a[i-1])
    if abs(a[i]-a[i+1]) > maxDis:
        maxDis = abs(a[i]-a[i+1])

print(k-maxDis)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - max_distance)

Submission status: Accepted

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

dlist=[]

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

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

print(K-max(dlist))

Submission status: Accepted

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

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

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

print(sum(li) - max(li))

Submission status: Accepted

Solution code:
import copy
a = [int(c) for c in input().split()]
K = a[0]
N = a[1]
A = [int(c) for c in input().split()]
B = list(map(lambda x: x+K,copy.deepcopy(A)))
A = A+B
l = 0
tmp = 0
cnt = 0
for i in range(N):
    tmp = A[i+1]-A[i]
    if tmp > l :
        cnt = i
        l = tmp

print(K-l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    K, N = tuple([int(_x) for _x in input().split()])
    A = [int(_x) for _x in input().split()]
    maxdiff = 0
    for i in range(1, len(A)):
        maxdiff = max(maxdiff, A[i] - A[i-1])
    maxdiff = max(maxdiff, K + A[0] - A[-1])
    print(K-maxdiff)


main()

Submission status: Accepted

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

check = 0
for i in range(n):
  x = lst[i + 1] - lst[i]
  if (x > check):
    check = x

print(k - check)

Submission status: Accepted

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

Submission status: Accepted

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

last = a[0] + k - a[n - 1]
sum = 0
l =[]

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

l.append(last)

print(k - 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):
    kyori = a[i+1] - a[i]
    b.append(kyori)
b.append(abs(a[0]+(k-a[-1])))

print(sum(b)-max(b))

Submission status: Accepted

Solution code:
k,n = map(int,input().split()) 
A=list(map(int,input().split()) )
ans=10**10
for i in range(len(A)):
    
    #時計回り　iからi-1に行く
    #0を超えない
    if A[i-1] - A[i] > 0:    
        dist1  = A[i-1] - A[i]    
    
    #0を超える
    else:
        dist1 = A[i-1] + (k-A[i])
    
        
    #反時計回り　i-1からiに行く
    
    if A[i] - A[i-1] < 0:    
        dist2  = A[i-1] - A[i]    
    
    #0を超える
    else:
        
        dist2 = A[i] + (k-A[i-1])
        
    dist = min(dist1,dist2)
    
    ans = min(ans,dist)

print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
max_l = 0
for i in range(N):
    l = A_ls[(i + 1) % N] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % N]
    max_l = max(max_l, l)
print(K - max_l)

Submission status: Accepted

Solution code:
k, n = map(int,input().split())
a_l = list(map(int, input().split()))
d = [ abs(a_l[i+1] - a_l[i]) for i in range(n-1) ]
d += [ k-a_l[-1]+a_l[0] ]
print(sum(sorted(d, reverse=True)[1:]))

Submission status: Accepted

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

t = K - A[N-1] + A[0]
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()))
m=-10000
for i in range(1,n):
  x=a[i]-a[i-1]
  m=max(m,x)

print(min((k-m),abs(a[n-1]-a[0])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
a = list(map(int,input().split()))
b = K - a[N - 1] + a[0]
d = 10**20
for i in range(N):
  if i == 0:
    if b < a[i + 1] - a[i]:
      c = K - (a[i + 1] - a[i])
    else:
      c = K - b
  if i == N - 1:
    if a[i] - a [i - 1] < b:
      c = K - b
    else:
      c = K - (a[i] - a[i - 1])
  else:
    if a[i] - a[i - 1] < a[i + 1] - a[i]:
      c = K - (a[i + 1] - a[i])
    if a[i] - a[i - 1] < a[i + 1] - a[i]:
      c = K - (a[i + 1] - a[i])
  if d > c:
    d = c
print(d)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
ans=[l[i+1]-l[i] for i in range(0,m-1)]
ans.append(n-l[m-1]+l[0])
ans.sort()
print(sum(ans[:m-1]))

Submission status: Accepted

Solution code:
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
max_l = -1
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)]
    if max_l == -1:
        max_l = l
    else:
        max_l = max(max_l, l)
print(K - max_l)

Submission status: Accepted

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

dist = 0
ng = 0
for i in range(n-1):
    sub = A[i+1] - A[i]
    if sub > ng:
        ng = sub
    dist += sub

sub = A[0] + (k - A[-1])
if sub > ng:
    ng = sub
dist += sub

print(dist - ng)

Submission status: Accepted

Solution code:
import sys

K, N = map(int,input().split())
A = list(map(int,input().split()))
L = [0]*(2*N)
MIN = 1e18

for i in range(0,N):
	L[i] = A[i]
for i in range(N,2*N):
	L[i] = A[i-N]+K
for i in range(N-1,2*N):
	MIN = min(MIN,L[i]-L[i-(N-1)])

print(MIN)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    houses=[]
    k,n=map(int,input().split())
    houses=list(map(int,input().split()))
    distance=[]
    
    for i in range(1,n):
        distance.append(houses[i]-houses[i-1])
    distance.append(k-(houses[-1]-houses[0]))

    max_length=max(distance)    
    print(sum(distance)-max_length)

if __name__=="__main__":
    main()

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


length.append(K-sum(length))

max_l = length[0]
for j in range(N):
  if max_l <= length[j]:
    max_l = length[j]
  else:
    pass


length.remove(max_l)
result = sum(length)
print(result)

Submission status: Accepted

Solution code:
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
rst = 0
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = A_ls[(i + 1) % len(A_ls)] + K - A_ls[i]
    rst = max(rst, l)
print(K - rst)

Submission status: Accepted

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

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

d = max([a[i] - a[i - 1] for i in range(1, n)])

c = max(c,d)

print(k - c)

Submission status: Accepted

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

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

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

print(k-c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
l, max_l = 0, 0
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)]
    max_l = max(max_l, l)
print(K - max_l)

Submission status: Accepted

Solution code:

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

if __name__ == "__main__":
    main()