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

diffA = []
_a = K + A[0]
for a in A[::-1]:
    diffA.append(_a - a)
    _a = a
maxid = diffA.index(max(diffA))
diffA.pop(maxid)
print(sum(diffA))

Submission status: Accepted

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

Submission status: Accepted

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

ans=[]
for i in range(n):
  ans.append(A[-1]-A[i])
  A.append(k+A[i])
print(min(ans))

Submission status: Accepted

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

p = 0
Z = []

for a in A :
    d = a - p
    Z.append(d)
    p = a

B = K - A[N-1]
Z[0] = B + A[0]
print( K - max(Z) )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = list(map(int,input().split()))
a = list(map(int,input().split()))
max_d = 0
a.append(a[0] + k)
for idx, i in enumerate(a[:-1]):
    if max_d < a[idx + 1] - i:
        max_d = a[idx + 1] - i
print(k - max_d)

Submission status: Accepted

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

minDistance = K

for i in range(1, N-1):
    next_left = i - 1
    next_right = i + 1
    #前隣に遠回り
    d1 = A[i] - A[next_left]
    if d1 > K/2:
        d1 = K - d1
    d1 = K - d1
    #次隣に遠回り
    d2 = A[next_right] - A[i]
    if d2 > K/2:
        d2 = K - d2
    d2 = K - d2
    if d1 < minDistance:
        minDistance = d1
    if d2 < minDistance:
        minDistance = d2

#A0とAnの距離
d = abs(A[N-1] - A[0])
if d > K/2:
    d = K - d

if K-d < minDistance:
    minDistance = K-d

###########　出力
print(minDistance)

Submission status: Accepted

Solution code:
k,n=list(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]=k-a[n-1]+a[0]
ret=10**7
for i in range(n):
    ret=min(ret,k-dist[i])

print(ret)

Submission status: Accepted

Solution code:
def main():
    tmp = input().split(" ")
    k = int(tmp[0])

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

    ans = []
    for i in range(len(a)):
        if i == 0:
            ans.append(k - a[-1] + a[i])
        else:
            ans.append(a[i] - a[i - 1])
        
    print(sum(ans) - max(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 入力の取得
k, n = map(int, input().split())
a = list(map(int, input().split()))

min_distance = k + 1
for index, x in enumerate(a):
    start = a[index]
    goal = a[(n - 1 + index) % n]
    if goal == 0:
        goal = k

    if start < goal:
        work_distance = goal - start
    else:
        work_distance = k + goal - start

    if work_distance < min_distance:
        min_distance = work_distance

print(min_distance)

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(1, n):
    dis = abs(a[i] - a[i - 1])
    l.append(dis)
 
print(sum(l) - max(l))

Submission status: Accepted

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

for i in range(n):
    if i==1:
        x=a[i]-a[i-1]
        y=i
    elif a[i]-a[i-1]>x:
        x=a[i]-a[i-1]
        y=i

if k-a[n-1]+a[0]>x:
    x=k-a[n-1]+a[0]
    y=0

print(k-x)

Submission status: Accepted

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

Submission status: Accepted

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

l_ans = []

for i in range(N):
    if i + N - 1 < N:
        l_ans.append(l[i + N - 1] - l[i])
    else:
        l_ans.append(K - l[i] + l[i - 1])

print(min(l_ans))

Submission status: Accepted

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


l_s = []
check = 0
for i in range(0,N):
    if(A[i] == 0):
        check =1
if(check == 1):
    for i in range(1,N):
          l_s.append(A[i]-A[i-1])
    l_s.append(K-A[N-1])
    num = 0
    ans = 99999999999999999
    for i in range(0,N):
        num = K - l_s[i]
        ans = min(ans,num)
    print(ans)
    
else: 
    for i in range(1,N):
        l_s.append(A[i]-A[i-1])
    l_s.append(K-A[N-1]+A[0])
    m1 = max(l_s)
    ans = K-m1
    print(ans)

Submission status: Accepted

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

flag=1
now=0
mae=[]
back=[]
for i in range(N-1):
  mae.append(A[i+1]-A[i])
mae.append(A[len(A)-1]-A[0])

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

back.sort()
mae.sort()
ans=min(sum(mae[:len(mae)-1]),sum(back[:len(back)-1]))
print(ans)

Submission status: Accepted

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

dis_list = [num_list[i+1] - num_list[i] for i in range(N-1)]


last_dis = (K-num_list[-1])+num_list[0] if num_list[-1] > (K//2) else num_list[-1] - num_list[0]
dis_list.append(last_dis)

max_dis = max(dis_list)

ans = K-max_dis
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
K, N = map(int, input().split())
S=deque(list(map(int, input().split())))
ans = [S[-1] - S[0]]
for i in range(N-1):
    ans.append(S[i]- S[i+1] + K)
print(min(ans))

Submission status: Accepted

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

maxDis = (k - lst[-1]) + lst[0]
for i in range(n-1):
    if lst[i + 1] - lst[i] > maxDis:
        maxDis = lst[i + 1] - lst[i]
print(k - maxDis)

Submission status: Accepted

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

k = kn[0]
n = kn[1]


dis = []

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

del dis[dis.index(max(dis))]

print(sum(dis))

'''
dis = [[0]*n]

for i in range(n):
    for j in range(n):
        if i+j+1 >= n-1:
            dis[i][j] = k - a[i+j+1-(n-1)] + a[i+j+1-(n-1)-1]
        else:
            dis[i][j] = a[i+j+1] - a[i+j]

    dis.append([0]*n)
    print(dis)

print(dis)
'''

Submission status: Accepted

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

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

li.sort(reverse=True)
print(k-li[0])

Submission status: Accepted

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

Submission status: Accepted

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

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

ans1 = 0
ans2 = 0
max = 0

a.sort()
"""
if k - a[n-1] < a[0] - 0:
a.append(k)
"""

for i in range(n-1):
    ans1 += a[i+1] - a[i]
    if a[i+1] - a[i] > max:
        max = a[i+1] - a[i]

ans1 += k - a[n-1] + a[0]
if k - a[n-1] + a[0] > max:
    max = k - a[n-1] + a[0]

ans1 -= max
print(ans1)

Submission status: Accepted

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

Submission status: Accepted

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

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


print(k -maxa)

Submission status: Accepted

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

a1 = max(A)-min(A)

max_v = 0
A_i = None
for i in range(len(A)-1):
    if (A[i+1]-A[i])>max_v:
        max_v = A[i+1]-A[i]
        A_i = i+1

a2 = A[A_i-1]+abs(K-A[A_i])
print(a2) if a1>a2 else print(a1)

Submission status: Accepted

Solution code:
s = input().split(" ")
k = int(s[0])
n = int(s[1])

d = input().split(" ")
dist = []

for i in d:
    dist.append(int(i))

res = dist[n-1] - dist[0]

for i in range(1, n):
    diff = dist[i] - dist[i-1]
    aux = k - diff
    if aux < res:
        res = aux

print(str(res))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    sorted(A)
    d = [A[i]-A[i-1] for i in range(1,N)]
    d.append(K - max(A) + min(A))
    ans = K-max(d)
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ansdiv = K

# 一番近いやつ求める
for i, j in zip(A[:-1], A[1:]):
    div = K - j + i
    if div < ansdiv:
        ansdiv = div

# 最初のと最後のを忘れない
lastdiv = K - A[N-1] + A[0]
lastans = K - lastdiv
if lastans < ansdiv:
    ansdiv = lastans

print(ansdiv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
 
k, n = map(int, input().split())
arr = list(map(int, input().split()))
diff = [0] * n
 
for i in range(1, n):
  diff[i] = arr[i] - arr[i-1]
diff[0] = k - arr[-1] + arr[0]
 
print(int(np.sum(diff) - max(diff)))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = min(A[-1]-A[0],K-(A[-1]-A[0]))
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()))
result = 0
mx = -float("inf")
for i in range(N-1):
    mx = max(mx, A[i+1] - A[i])
mx = max(mx, K - A[-1] + A[0])

result = K - mx
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

dist = 0
dist_list = []
max = 0
sum = 0
 
for i in range(len(a_list)):
  if i == len(a_list) - 1:
    dist = int(K) - abs(int(a_list[i]) - int(a_list[0]))
  else:
    dist = abs(int(a_list[i]) - int(a_list[i + 1]))
  if i != 0: 
    if max < dist:
        sum += max
        max = dist
    else:
        sum += dist
  else:
      max = dist
  
    
print(sum)

Submission status: Accepted

Solution code:
import numpy as np
kn = list(map(int,input().split()))
K = kn[0]
N = kn[1]
As = list(map(int,input().split()))

subtract_list_r = np.array([])
subtract_list_l = np.array([])
subtract_list_r = list(subtract_list_r)
subtract_list_l = list(subtract_list_l)

subtract_value_r = 0
subtract_value_l = 0
for i in range(1,N-1):
    subtract_value_r = K-(As[i]-As[i-1])
    subtract_value_l = K-(As[i+1]-As[i])
    subtract_list_r.append(subtract_value_r)
    subtract_list_l.append(subtract_value_l)


subtract_list_r.append(As[N-1]-As[0])
subtract_list_l.append(K-(As[1]-As[0]))
subtract_list_r.append(K-(As[N-1]-As[N-2]))
subtract_list_l.append(abs(As[0]-As[N-1]))
r_min = min(subtract_list_r)
l_min = min(subtract_list_l)

print("{}".format(int(min([r_min,l_min]))))

Submission status: Accepted

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(len(a)):
  if i == len(a) - 1:
    distance.append(k+a[0] - a[i])
  else:
  	distance.append(a[i+1] - a[i])
distance.sort()
print(sum(distance[:-1]))

Submission status: Accepted

Solution code:
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    minn = min(a[n - 1] - a[0], int(10e10))
    for i in range(n):
        ans = (a[n - 1] - a[i]) + (k + a[i - 1] - a[n - 1])
        minn = min(ans, minn)
    print(minn)


if __name__ == "__main__":
    # IOE()
    main()

Submission status: Accepted

Solution code:
import sys
a,b=map(int,input().split())
c=list(map(int,input().split()))
c.sort()
d=[0]*1000000
for i in range(b-1):
  d[i]+=abs(c[i]-c[i+1])
  

d.append(a-(c[-1]-c[0]))

print(a-max(d))

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

Submission status: Accepted

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

lists = [int(x) for x in input().split()]
lists.sort()
sor = []
for i in range(N-1):
    sor.append(lists[i+1]-lists[i])
sor.append(K-lists[N-1]+lists[0])
print(K-max(sor))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
house = list(map(int,input().split()))
house = house + house
min = house[N - 1] - house[0]

for i in range(N - 1):
  if K - (house[N + 1 + i] - house[i]) < min and house[N + 1 + i] - house[i] <= K/2 :
    min = K - (house[N + 1 + i] - house[i])

print(int(min))

Submission status: Accepted

Solution code:
t = (int(x) for x in input().split(" "))
T = list(t)
K = T[0]
N = T[1]
x = (int(x) for x in input().split(" "))
X = list(x)
y = []
for i in range(0, N-1):
    y.append(X[i+1]-X[i])
y.append(X[0]+K-X[N-1])
z = max(y)
s =K - z
print(s)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
import math
# stdin = open("sample.txt")

K, N = [int(x) for x in stdin.readline().rstrip().split()]
A = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
span_list = []

for i in range(N):
    # print(A[i])
    if i < N-1:
        span = A[i+1] - A[i]
        span_list.append(span)
    else:
        span = K - A[i] + A[0]
        span_list.append(span)

span_list.sort()
# print(span_list)
del span_list[-1]
# print(span_list)
print(sum(span_list))

Submission status: Accepted

Solution code:
num_list = [int(i) for i in input().split()]
dist_list = [int(i) for i in input().split()]

temp = [(dist_list[-1]-num_list[0])] + dist_list[:-1]
sabun_list = [dist_list[i] - temp[i] for i in range(len(dist_list))]

print(sum(sabun_list) - max(sabun_list))

Submission status: Accepted

Solution code:
#X = int(input().strip())

K, N = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
m = 0
ind = 0
ans = 0

for i in range(N-1):
  _m = A[i+1] - A[i]
  if _m > m:
    m = _m
    ind = i+1
    last = i

_m = K + A[0] - A[N-1]
if _m > m:
  m = _m
  ind = 0
  last = N-1

#print(m)
#print(ind)
#print(last)
i = ind
while i != last:
  #print(ans)
  if i < N-1:
    ans += A[i+1] - A[i]
    i += 1
  else:
    ans += K + A[0] - A[i]
    i = 0

print(ans)

Submission status: Accepted

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

ans=0 
b.sort()
 
for i in range(len(b)-1):
    ans = b[i] + ans

print(ans)

Submission status: Accepted

Solution code:
k, n = map(int,input(). split())
lstr_a = input().split(" ")
lint_a = [int(n) for n in lstr_a]
alist = sorted(lint_a)
t = alist.count(0)
x = 0
for i in range(n-1):
  x = max(alist[i+1]-alist[i], x)
b = k-alist[n-1]+alist[0]
c = max(x,b)
answer = k-c
print(answer)

Submission status: Accepted

Solution code:
import numpy as np
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=np.array(a)
if max(abs(a[1:]-a[:-1])) < k-abs(a[0]-a[-1]):b=k-abs(a[0]-a[-1])
else:b= max(abs(a[1:]-a[:-1]))
print(k-b)

Submission status: Accepted

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

A.append(K+A[0])

D = []

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

print(K-max(D))

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
dis_lst=list(map(int,input().split()))
sa_lst=[]
for i in range(N):
    if i==N-1 and 0 in dis_lst:
      sa_lst.append(K-dis_lst[i])
      break
    elif i==N-1:
      sa_lst.append(K-dis_lst[i]+dis_lst[0])
      #print(sa_lst)
      break
    sa_lst.append(abs(dis_lst[i]-dis_lst[i+1]))
    #print(sa_lst)
    #print('pass')
sa_lst.remove(max(sa_lst))
print(sum(sa_lst))

Submission status: Accepted

Solution code:
#--------------------------------------------
#ライブラリ呼び出し
import sys
import math
#--------------------------------------------
K, N = map(int, input().split())
arr = list(map(int, input().split()))
List=[]
for i in range(N-1):
    sa =int(arr[i+1]-arr[i])
    List.append(sa)

max_num = max(List)
hasi = K-arr[N-1]+arr[0]
PPP = (max(max_num,hasi))
ans = K-PPP
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    
    mini=k
    for i in range(n):
        tmp=a[i-1]-a[i]
        if tmp<0:
            tmp+=k
        mini=min(mini,tmp)
    print(mini)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

k,n = map(int,input().split())
A = list(map(int, input().split()))
A = np.array(A)
B = np.diff(A, n=1)

B_ = (A[0])+(k-A[n-1])
B_ans = np.append(B,B_)
ans = np.argmax(B_ans)

print(k - B_ans[ans])

Submission status: Accepted

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

deff_list = []

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

deff_list.append(min(a)+(k-max(a)))

deff_list[deff_list.index(max(deff_list))] = 0

print(sum(deff_list))

Submission status: Accepted

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

Submission status: Accepted

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


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

print(int(K-sum))

Submission status: Accepted

Solution code:
from sys import stdin, stdout  

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

m=max(dist)
for idx,val in enumerate(dist):
    if(val==m):
        dist[idx]=0
        break
print(sum(dist))

Submission status: Accepted

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

sorted_A = sorted(A)
sorted_A = [i - sorted_A[0] for i in sorted_A]

dis = []
for i in range(len(sorted_A)-1):
    dis.append(abs(sorted_A[i] - sorted_A[i+1]))

last_dis = abs(sorted_A[-1] - sorted_A[0])

if(last_dis >= K/2):
    last_dis = K - last_dis

dis.append(last_dis)

print(K - max(dis))

Submission status: Accepted

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

dist=[]
dist.append(abs(Ai[N-1]-Ai[0]))
for i in range(N-1):
  dist.append(abs((K+Ai[i])-Ai[i+1]))

print(min(dist))
#print(dist)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
data = input().split()

round = int(data[0])
cnt_house = int(data[1])

distance = input().split()

min_dist = None

for start in range(cnt_house):
  if start == 0:
    move_dist = int(distance[cnt_house - 1]) - int(distance[start])
  else:
    move_dist = (round - int(distance[start])) + int(distance[start - 1])
  
  if min_dist is None or min_dist > move_dist:
    min_dist = move_dist

print(min_dist)

Submission status: Accepted

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

root.append(k-list[-1]+list[0])

sum = sum(root)
ans = []

for j in range(len(root)):
    ans.append(sum-root[j])


ans.sort()
print(ans[0])

Submission status: Accepted

Solution code:
import math
a , b = map(int,input().split())
res = list(map(int,input().split()))
res2 = []
if len(res) == 1:
    print(0)
else:
    for i in range(len(res)-1):
        aa = res[i+1] - res[i]
        res2.append(aa)
    if res[0] == 0:
        res2.append(a-res[-1])
    else:
        res2.append(res[0] + (a - res[-1]))
print(sum(res2) - max(res2))

Submission status: Accepted

Solution code:
k, n = map(float, input().split())
m = list(map(float, input().split()))
half_k = k / 2
min = float(1000000)
for i in range(int(n)):
    first = m[i]
    last = m[i-1]
    len = last - first
    if len < 0:
        len = len + k
    if min > len:
        min = len
print(int(min))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    input1 = input()
    input2 = input()
    k , n = map(int , input1.split())
    al = list(map(int , input2.split()))
    arg = [[k,n],al]
    ans = process(arg)
    print(ans)
    
def process(arg):
    ans = 0
    ensyu = arg[0][0]
    kensu = arg[0][1]
    aida = list()
    for x in range(arg[0][1]):
        mae = arg[1][x-1]
        b = arg[1][x]
        if x == 0:
            # 1件目は演習
            w = b + ensyu - arg[1][kensu-1]
        else:
            w = b - mae
        aida.append(w)

    m = max(aida)
    flg = False
    for y in aida:
        if flg == True or y != m :
            ans = ans + y
        else:
            flg = True
    return ans

def getInput(input):

    return 

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)

distance = []

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

mindistance = 0
for j in distance:
    mindistance += j

print(mindistance - max(distance))

Submission status: Accepted

Solution code:
# C - Traveling Salesman around Lake
K,N = list(map(int, input().split()))
A = list(map(int, input().split()))

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

b = K-(A[N-1]-A[0])
c = max(a,b)
ans = K -c
print(ans)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
*a, = map(int, input().split())
a += [i+k for i in a]
print(min(j-i for i, j in zip(a, a[n-1:])))

Submission status: Accepted

Solution code:
def solution():
    k, n = map(int, input().split())
    house = list(map(int, input().split()))
    next_dist = []

    for i in range(1, len(house)):
        next_dist.append(house[i]-house[i-1])
    next_dist.append(k-house[len(house)-1]+house[0])
    max_dis_index = next_dist.index(max(next_dist))
    del next_dist[max_dis_index]
    print(sum(next_dist))

solution()

Submission status: Accepted

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

l = [0] * N
hoge = 0

for n in range(N):
  l[n] = A[n]-hoge
  hoge = hoge + l[n]
  
l = l + [K-A[N-1]]

if A[0]!=0 :
  l[N] = l[N]+l[0]
  l[0] = 0

print(sum(l)-max(l))

Submission status: Accepted

Solution code:
k, n, *a = map(int, open(0).read().split())
a.append(k + a[0]) # 円環は2周する
ans = 0
for i in range(n):
    ans = max(ans, a[i + 1] - a[i]) # 最も間隔のある家を両端にすると最小
print(k - ans)

Submission status: Accepted

Solution code:
i = list(map(int, input().split()))
K = i[0]
N = i[1]
max = 0
count = 0


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

for j in range(len(A)-1):
    count += 1
    if abs(A[j+1] - A[j]) > max:
        max = abs(A[j+1] - A[j])
    if count == (len(A)-1):
        if max < abs(A[0] + (K - A[j+1])):
            max = abs(A[0] + (K - A[j+1]))

print(K - max)

Submission status: Accepted

Solution code:
k, n = map(int , input().split())
lis = list(map(int,input().split()))
mi = [min(lis)] * n
li = [x - y for (x, y) in zip(lis, mi)]

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

Submission status: Accepted

Solution code:
k, n = map(float, input().split())
m = list(map(float, input().split()))
half_k = k / 2
min = float(1000000)
for i in range(int(n)):
    first = m[i]
    last = m[i-1]
    len = last - first
    if len < 0:
        len = len + k
    if min > len:
        min = len
print(int(min))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a_array = [int(x) for x in input().split()]
diff = []
tmp = 0
for i in range(n - 1):
    diff.append(abs(a_array[i + 1] - a_array[i]))
diff.append(k - a_array[-1] + a_array[0])
print(sum(diff) - max(diff))

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
A = list(map(int,input().split()))
ans = float("inf")
for i in range(N):
    if i == 0:
        ans = min(K+A[i]-A[1], -A[i]+A[N-1], ans)
    elif i == N-1:
        ans = min(A[i]-A[0], K-A[i]+A[i-1], ans)
    else:
        ans = min(K+A[i]-A[i+1], K-A[i]+A[i-1], ans)
print(ans)

Submission status: Accepted

Solution code:


def solve():
    K, N = map(int, input().split())
    As = list(map(int, input().split()))

    dis = 0
    point = 0
    for i in range(1, N):
        tmp = As[i]-As[i-1]
        if tmp > dis:
            dis = tmp
            point = i

    if As[N-1]-As[0] < K-As[point]+As[point-1]:
        print(As[N-1]-As[0])
    else:
        print(K-As[point]+As[point-1])


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

max_distance = abs(k - a[-1] + a[0])
str_index = 0
gl_index = -1
for i in range(n):
    if i == 0:
        continue
    elif max_distance < abs(a[i-1] - a[i]):
        max_distance = abs(a[i-1] - a[i])
        str_index = i-1
        gl_index = i
if gl_index == -1:
    print(a[gl_index] - a[str_index])
else:
    print(a[str_index] + (k - a[gl_index]))

Submission status: Accepted

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

dis = 0
d_li = []
for i in range(1,len(a)):
    tmp = abs(a[i] - a[i-1])
    dis += tmp
    d_li.append(tmp)
    
print(dis - max(d_li))

Submission status: Accepted

Solution code:
import sys
from math import sqrt
from collections import Counter, defaultdict

input = sys.stdin.readline


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    k, n = MI()
    a_list = LI()

    a_list.sort()
    max = 0
    for i, a in enumerate(a_list):
        if i != n - 1:
            d = a_list[i + 1] - a
        else:
            d = a_list[0] + k - a

        if d > max:
            max = d
            max_i = (i + 1) % n

    l = []
    for a in a_list:
        tmp = a - a_list[max_i]
        if tmp >= 0:
            l.append(tmp)
        else:
            l.append(k + tmp)

    l.sort()
    print(l[n - 1])

    pass


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
A=input()
LA=A.split()
K=int(LA[0])
N=int(LA[1])

B=input()
LB=B.split()

C=0
samax=0
start=0

for n in LB:
  sa=int(int(n)-int(C))
  C=n
  if samax < sa:
    samax=sa
    start=int(n)-int(samax)
    goal=int(n)
ans1=int(LB[N-1])-int(LB[0])
ans2=int((K-goal)+int(start))

if ans2 > ans1:
  ans=ans1
else :
  ans=ans2
print(ans)

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N, K = iim()
    A = list(iim())

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
i = list(map(int,input().split()))
house = list(map(int,input().split()))

ans = []
for h in range(len(house)-1):
  num = house[h+1]-house[h]
  ans.append(num)
l = i[0] - house[-1] + house[0]
ans.append(l)

ans.remove(max(ans))

print(sum(ans))

Submission status: Accepted

Solution code:
# ABC160 C

def main():
    k, n = map(int, input().split())
    A = list(map(int, input().split()))
    
    maxIndex = 0
    maxDist = 0
    
    for i in range(n):
        if i == n - 1:
            gap = A[0]+k - A[i]
        else:
            gap = A[i+1] - A[i]
        
        if gap >= maxDist:
            maxDist = gap
            maxIndex = i
    
    if maxIndex == n - 1:
        print(A[-1] - A[0])
    else:
        print((A[maxIndex]+k) - A[maxIndex+1])            

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
res = A[-1] - A[0]
for a1, a2 in zip(A, A[1:]):
    res = min(res, K - a2 + a1)
print(res)