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()))

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

print(k - dmax)

Submission status: Accepted

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

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

print(K-distance_max)

Submission status: Accepted

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

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

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


print(sum(diff_list)-max(diff_list))

Submission status: Accepted

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

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)]

# ----------------------------------------------------------------


def main():
    k, n = LI()
    a = LI()

    dists = []

    for i in range(len(a) - 1):
        dists.append(a[i + 1] - a[i])

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

    ans = sum(dists) - max(dists)

    print(ans)


main()

Submission status: Accepted

Solution code:
import numpy as np

K, N = [int(i) for i in input().rstrip().split(' ')]
A = [int(i) for i in input().rstrip().split(' ')]

x = A[0]
y = K - A[-1]
n = [int(A[i+1] - A[i]) for i in range(N-1)]
n.append(x+y)

np_list = np.array(n)
n.pop(np_list.argmax())
print(sum(n))

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
A = sorted(A)
new = []
for i in range(N-1):
    new.append(A[i+1]-A[i])
new.append(K + A[0] - A[-1])
new = sorted(new)
print(sum(new[:-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

A = sorted(A)

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

print(ans)

Submission status: Accepted

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

diff = []


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


DIFF = sorted(diff)

del DIFF[-1]

print(sum(DIFF))

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
#print(K,N)
A=list(map(int,input().split()))
#print(A)
#print(A[1:N])
#print(A[0:(N-1)])

D = max([i-j for i,j in zip(A[1:N], A[0:(N-1)])])
#print(D)
z = K - A[N-1] + A[0]
if z > D:
    D = z
print(K - D)

Submission status: Accepted

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

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

print(K - ans)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
e=a[1:]+[k+a[0]]
l=[abs(x-y) for x,y in zip(a,e)]
print(sum(l)-max(l))

Submission status: Accepted

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

dists = []

last = -1
for a in ai:
    if last < 0:
        last = a
        continue
    
    dists.append(a - last)
    last = a

dists.append(k - last + ai[0])

print(k - max(dists))

Submission status: Accepted

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

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

maxDistance = max(d)
startIndex = d.index(maxDistance)
ans = 0
for i in range(N-1):
  ans += d[startIndex - i-1]

print(ans)

Submission status: Accepted

Solution code:
length, num = map(int, input().split())
dist=[int(i) for i in input().split()]

Max=0
for i in range(num-1):
    Max = max([dist[i+1]-dist[i],Max])

Max=max(Max,length-dist[-1]+dist[0])
print(length-Max)

Submission status: Accepted

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

distA = []

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

Submission status: Accepted

Solution code:
# C
K, N = map(int,input().split())
A =list(map(int,input().split()))
l = len(A)
ans = 10**9
for i in range(l):
    if (i == 0):
        ans = min(A[-1] - A[0], A[0] + K - A[1], ans)
    elif (i == l-1):
        ans = min(A[-1] - A[0], A[-2] + K - A[-1], ans)
    else:
        ans = min(K - A[i] + A[i-1], A[i] + K - A[i+1], ans)
print(ans)

Submission status: Accepted

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

points.sort()

# 2週目追加
points += [p + K for p in points]

# 2 ＊ 10^5 だけど間に合うはず
print(min(
    points[start + N - 1] - points[start]
    for start in range(N)
))

Submission status: Accepted

Solution code:


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

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




max_range = (K+A[0]-A[N-1])

for i in range(N-1):
    if max_range < A[(i+1)%K]-A[i%K]:
        max_range = A[i+1]-A[i]
        


if max_range>=K/2:
    print(K-max_range)
else:
    print(K-max_range)

Submission status: Accepted

Solution code:
from sys import stdin

k, n = [int(x) for x in stdin.readline().rstrip().split()]

l = [int(x) for x in stdin.readline().rstrip().split()]
#print(k)
#print(n)
#print(l[0])

mylist = []

for i in range(n):
    if i == n-1:
        mylist.append( k - l[i] + l[0])
    else:
        mylist.append(l[i+1]-l[i])

#print(mylist)

sortlist = sorted(mylist)
length = 0
for i in range(n-1):
    length += sortlist[i]

print(length)

Submission status: Accepted

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):
        ret = min(ret, A[n] + (K - A[n + 1]))

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

arr.append(x[0]+abs(x[len(x)-1]-k))

for i in range(len(x)-1):
    arr.append(x[i+1]-x[i])


print(sum(arr) - max(arr))

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[N-1] = A[0] - A[-1] + K

print(K - max(dist))

Submission status: Accepted

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

    shiftA = A[1:] + [K+A[0]]

    subA = [shiftA[i] - A[i] for i in range(N)]
    maxA = max(subA)
    subA.remove(maxA)

    print(sum(subA))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = list(map(int, input().split()))
sa = 0
for i in range(b):
    if i == 0:
        sa = c[0] - (c[b-1] - a)
    elif c[i] - c[i-1] >= sa + 1:
        sa = c[i] - c[i-1]
print(a-sa)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
houses = list(map(int, input().split()))
# syaku_houses = []
# for i, num in enumerate(houses):
#     if i == 0:
#         syaku_houses.append(num)
#     else:
#         syaku_houses.append(num - houses[i - 1])
# print(syaku_houses)

# min_costs = []
# for house in syaku_houses:

# 右回りか左回りか？
for i in range(N - 1):
    houses.append(K + houses[i])

min_costs = []
for i in range(N):
    min_costs.append(houses[N - 1 + i] - houses[i])
print(min(min_costs))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [K - i for i in A]
dpB = [0]*N
dpA = [0]*N
# dpA = [A[i+1] - A[i] for i in range(N-1)]
dpA[0] = A[0] + B[N-1]
dpB[0] = B[0] + A[N-1]

for i in range(N):
    if i>0:
        dpA[i] = A[i] - A[i-1]
for i in range(N):
    if i>0:
        dpB[i] = A[i] - A[i-1]
MAXA = max(dpA)
MAXB = max(dpB)
SUMA = sum(dpA) - MAXA
SUMB = sum(dpB) - MAXB
# print(dpA)
# print(dpB)
# print(SUMA)
# print(SUMB)
print(min(SUMA, SUMB))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

lines = stdin.readlines()
k, n = [int(x) for x in lines[0].rstrip().split()]
a = [int(x) for x in lines[1].rstrip().split()]

diffs = [int(a[x+1]-a[x]) for x in range(len(a)-1)]
diffs.append(k - a[-1] + a[0])

print(k - max(diffs))

Submission status: Accepted

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

for i in range(n-1):
    if a[i+1]-a[i] > MAX:
        MAX=a[i+1]-a[i]
if a[0]+k-a[n-1] > MAX:
        MAX=a[0]+k-a[n-1]
print(str(k-MAX))

Submission status: Accepted

Solution code:
import numpy as np
# 整数複数個
K, N = map(int, input().split())

# 整数 N 個 (改行区切り)
A = list(map(int, input().split()))


lengths = np.concatenate([[A[0] + (K - A[-1])], np.diff(A)],0)
print(K - lengths[np.argmax(lengths)])

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=len(a)
diff=max(a)-min(a)
temp1=a[0]
for i in range(1,b):
    temp2=a[i]
    a[i]=a[i]-temp1
    temp1=temp2


la=k-diff
a[0]=la

print(sum(a)-max(a))

Submission status: Accepted

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

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

mn = k - arr[-1] + arr[0]
m.append(mn)
m.sort()
print(sum(m[:n-1]))

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    readline=stdin.readline
    K,N=map(int,readline().split())
    A=list(map(int,readline().split()))
    B=list()
    for i in range(N):
        if A[i]==0 or A[i]==K//2:
            B.append(A[i])
        else:
            B.append(A[i])
            B.append(A[i]-K)
    B.sort()
    mi=0
    ma=0
    res=K
    for i in range(len(B)):
        if i+N-1<=len(B)-1:
            mi=B[i]
            ma=B[i+N-1]
            res=min(res,ma-mi)
        else:
            break
    print(res)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys

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

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

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

b.remove(max(b))

print(sum(b))

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    K, N = list(map(int, input().split()))
    a = list(map(int, input().split()))
    d = [0 for i in range(N)]
    for i in range(N):
        if i == N - 1:
            d[i] = K - a[i] + a[0]
        else:
            d[i] = a[i+1] - a[i]
    print(sum(d) - max(d))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
A = list(map(int, input().split()))
max_L = 0
max_R = 0
left = 0
right = 0
for i in range(n):
  d = {}
  if i-1 < 0:
    d['left'] = k - A[n-1] + A[i]
    d['right'] = A[i + 1] - A[i]
  elif i + 1 > n-1:
    d['left'] = A[i] - A[i-1]
    d['right'] = k - A[i] + A[0]
  else:
    d['left'] = A[i] - A[i-1]
    d['right'] = A[i + 1] - A[i]
    
  if max_L < d['left']:
    max_L = d['left']
  if max_R < d['right']:
    max_R = d['right']

  right += d['right']
  left += d['left']
  
if right - max_R < left - max_L:
  print(right - max_R)
else:
  print(left - max_L)

Submission status: Accepted

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

min_list = []
for i in range(N):
    if i == 0:
        left = an[i] + K - an[-1]
        right = an[i+1] - an[i]
    elif i == N-1:
        left = an[i] - an[i-1]
        right = an[0] + K - an[N-1]
    else:
        left = an[i] - an[i-1]
        right = an[i+1] - an[i]
    min_list.append(min(K-left, K-right))
print(min(min_list))

Submission status: Accepted

Solution code:
import numpy

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

distance = numpy.empty(n,dtype=numpy.int)
distance[0] = a[0]

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

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

print(k - max(distance))

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# strlist = "abcdefghijklmnopqrstuvwxyz"
k,n = map(int,input().split())
# 配列入力の受け取り
arrA = list(map(int,input().split()))
# arrA = np.array(input().split(),dtype=np.int64)

ans = k+10
for i in range(n):
    if i == 0:
        migi = arrA[n-1]-arrA[0]
        hidari = k - (arrA[1]-arrA[0])
        ans = min(migi,hidari,ans)
    elif i == n-1:
        migi = k -(arrA[n-1] - arrA[n-2])
        hidari = arrA[n-1]-arrA[0]
        ans = min(migi,hidari,ans)
    else:
        migi = arrA[i-1] + k-arrA[i]
        hidari = k-(arrA[i+1]-arrA[i])
        ans = min(migi,hidari,ans)

print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

Solution code:
I=lambda:list(map(int,input().split()))
p,n=I()
l=I()
ne=[]
ma=10**9+7
for i in range(n-1):
    ne.append(abs(l[i]-l[i+1]))
if l[-1]==p:
    ne.append(l[0]-l[-1])
else:    
    ne.append(l[0]+abs(p-l[-1]))
g=sum(ne)
for i  in ne:
    ma=min(ma,g-i)
print(ma)

Submission status: Accepted

Solution code:
import collections
import math
import numpy as np
K, N = map(int, input().split(' '))

A = sorted(list(map(int, input().split(' '))))
for i in range(N):
    A.append(A[i]+K)


m = float('inf')

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

Submission status: Accepted

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

li = []

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

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

print(K - max(li))

Submission status: Accepted

Solution code:
lake_len, house_num = map(int, input().split())
house = list(map(int, input().split()))
diff = []

for i in range(house_num):
    if i != house_num - 1:
        diff.append(house[i+1] - house[i])
    else:
        diff.append(lake_len - house[i] + house[0])

diff_sort = sorted(diff, reverse=True)

print(sum(diff_sort) - diff_sort[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [abs(x - y) for x, y in zip(A[1:], A)]
B += [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()))
res = [0 for _ in range (N)]
for i in range(N-1):
  res[i] = K - max(A[i+1]-A[i], A[i]-A[i-1])
res[N-1] = K - max(A[N-1]-A[N-2], A[0]+K-A[N-1])
print(min(res))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
Ai = list(map(int, input().split()))
Di = [0]*len(Ai)

for i in range(len(Ai)):
    if i == len(Ai) - 1:
        Di[i] = (K - Ai[i]) + Ai[0]
    else:   
        Di[i] = Ai[i + 1] - Ai[i]

ans = K - max(Di)

print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
d = [0] * N
for i in range(N-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:
k,n=map(int,input().split())
l=list(map(int,input().split()))
l.append(k+l[0])
ans_lst=[]
ans=0
for i in range(n):
    ans_lst.append(l[i+1]-l[i])
#print(l)
#print(ans_lst)
ans=sum(ans_lst)-max(ans_lst)
print(ans)

Submission status: Accepted

Solution code:
k,n=map(int, input().split())
a=list(map(int,input().split()))
maxi=0
for i in range(n):
    if i==0:
        if max(a[1]-a[0],k-a[n-1]+a[0])>maxi:
            maxi=max(a[1]-a[0],k-a[n-1]+a[0])
    elif i!=n-1:
        if max(a[i+1]-a[i],a[i]-a[i-1])>maxi:
            maxi=max(a[i+1]-a[i],a[i]-a[i-1])
    else:
        if a[n-1]-a[n-2]>maxi:
            maxi=a[n-1]-a[n-2]
print(k-maxi)

Submission status: Accepted

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

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

print(K - max(D))

Submission status: Accepted

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

def main():
    n, m = map(int, input().split())
    lis = list(map(int, input().split()))
    su = []
    for i in range(m - 1):
        su.append(lis[i + 1] - lis[i])
    ou = lis[0] - lis[-1] + n
    print(sum(su) + ou - max(ou, max(su)))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

var = distances[0]
for i in range(N):
    distances[i] -= var
distances.append(K-distances[-1])

for i in range(N-1, 0,-1):
    distances[i] -= distances[i-1]

print(sum(sorted(distances)[:-1]))

Submission status: Accepted

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

result1 = A_list[-1]-A_list[0]
dist = []
for i in range(N-1):
    dist.append(A_list[i+1]-A_list[i])
dist = max(dist)
result2 = K-dist
if result1>=result2:
    print(result2)
else:
    print(result1)

Submission status: Accepted

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


start_diff = A[0] + k - A[-1]
diff = [start_diff]
answer = 0
start = A[0]

for i in range(1, n):
    diff.append(A[i] - start)
    start = A[i]

# print(diff)
# print(start_diff)

answer = sum(diff) - max(diff)
print(answer)

Submission status: Accepted

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

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

maxDist = max(dist_betweenHomes)
result = k - maxDist
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
k,n = [int(a) for a in input().split()]
N = [int(a) for a in input().split()]
L = []
for s in range(len(N)-1):
  L.append(N[s+1] - N[s])
L.append(N[0] + k-N[-1])
print(k - max(L))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = [int(e) for e in input().split()]
k =s[0]
n = s[1]
a=[int(e) for e in input().split()]
a_length=[]
for i in range(n-1):
    a_length.append(a[i+1]-a[i])
a_length.append(k-a[-1]+a[0])
amax = max(a_length)
print(k-amax)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
X=input().split()
LONG=int(X[0])
HOUSE=int(X[1])
X=input().split()
PLACE=[0]*LONG
for _ in range(HOUSE):
    place=int(X[_])
    PLACE[place]=1

i=0
while(PLACE[i]==0):
    i+=1
START=i
i=LONG-1
while(PLACE[i]==0):
    i-=1
END=i
"""
一番前にいるやつ: START
一番後ろにいるやつ: END
"""
s_g_long=LONG+START-END
MOST_LONG=s_g_long
"""
s_g_long: STARTとENDの間の距離
MOST_LONG: 最長の間隔
"""
where=START
for _ in range(HOUSE-1):
    """
    全ての間隔の長さをはかり、最長のものを通らないような道のりが最短
    """
    i_idash_long=0
    while(PLACE[where+1]==0):
        where+=1
        i_idash_long+=1
    where+=1
    i_idash_long+=1
    if MOST_LONG<i_idash_long:
        MOST_LONG=i_idash_long

print(LONG-MOST_LONG)

Submission status: Accepted

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

s = sa.index(max(sa))
del sa[s]

print(sum(sa))

Submission status: Accepted

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

Submission status: Accepted

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

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

minV = (K - A[N-1]) + A[0]
minI = 0

for i in range(1,N+1):
    if i == N:
        tmp = K - A[i-1]
    else:
        tmp = A[i] - A[i-1]
    # print(i, tmp)
         
    if minV < tmp:
        minV = tmp
        minI = i

# print(minI)
ans = 0

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

Submission status: Accepted

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

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

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

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

print(K - dist)

Submission status: Accepted

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

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

# 最も感覚が広い家を探す
max_dis = 0
index = 0
for i in range(len(a)):
    if i+1 == n:
        if a[0] + k - a[i] > max_dis:
            max_dis = a[0] + k - a[i]
            index = i
    else:
        if a[i+1] - a[i] > max_dis:
            max_dis = a[i+1] - a[i]
            index = i

if index+1 == n:
    print(a[n-1]-a[0])
else:
    print(k - a[index+1] + a[index])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - max(B))

Submission status: Accepted

Solution code:
import numpy as np

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

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

A_1 = np.array(A[1:])
A_11 = np.append(A_1, A[0] + K)

A_2 = np.array(A)
print(K - max(A_11 - A_2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
i_str = input()
K,N = map(int,i_str.split())
i_str = input()
As = list(map(int,i_str.split()))
A_range = []
As_b = 0
for i,A in enumerate(As):
    A_range.append(A - As_b)
    As_b = A
A_range[0] = (K - As[len(As)-1] + A_range[0])
A_range_max = max(A_range)
print(K - A_range_max)

Submission status: Accepted

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

a = [i for i in list(map(int, input().split()))]

gap_max=0
for i in range(n-1):
  gap = a[i+1]-a[i]
  if gap>gap_max:
    gap_max=gap

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

Submission status: Accepted

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

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

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

A_kyori.append(K - A_list[-1] + A_list[0])

print(sum(A_kyori) - max(A_kyori))

Submission status: Accepted

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

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

longest = sorted(c, reverse=True)[0]

print(k-longest)

Submission status: Accepted

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

A = list(map(int,input().split()))
ans = 10**20
for i in range(N):
    if A[i]>=A[i-1]:
        ans = min(K - (A[i]-A[i-1]),ans)
    else:
        ans = min(K- (A[i]+ K -A[i-1]), ans)

print(ans)

Submission status: Accepted

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

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

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

print(k-max)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    K, N = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]
    As1 = sorted(As)
    result = As1[-1] - As1[0]
    for i in range(1, len(As1)):
        x = (As1[-1] - As1[i]) + (K - As1[-1]) + As1[i - 1]
        result = min(result, x)

    As2 = [K - a for a in As]
    As2 = sorted(As2)
    result = min(result, As2[-1] - As2[0])
    for i in range(1, len(As2)):
        x = (As2[-1] - As2[i]) + (K - As2[-1]) + As2[i - 1]
        result = min(result, x)

    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    ma = A[0] + k - A[n - 1]
    mai = n - 1
    for i in range(n - 1):
        if A[i + 1] - A[i] > ma:
            ma = A[i + 1] - A[i]
            mai = i

    if mai == n - 1:
        ans = A[mai] - A[0]
    else:
        ans = A[mai] + k - A[mai + 1]

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:


k, n = [0] * 2
a = []

def format_input(filename = None):
	global k, n
	global a
	if filename == None:
		k, n = list(map(int, input().split()))
		a = list(map(int, input().split()))

	elif filename == '__random__':
		from random import randint as rng
		from random import sample
		k = rng(2, 10**6)
		n = rng(2, 2 * 10**5)
		a = sample(range(k), n)
		a.sort()
		print(k, n)
		print(' '.join(list(map(str, a))))

def get_answer():
	deff_a = [a[i] - a[i-1] for i in range(1, n)]
	answer = k - max(max(deff_a), k - a[-1] + a[0])
	return answer

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)