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

print(k-max(dist))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

tmp = [0 for _ in range(N)]
for i in range(N-1):
    tmp[i] = A[i+1] - A[i]

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

print(K - max(tmp))

Submission status: Accepted

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

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

## Logic

A.append(A[0]+K)

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

Submission status: Accepted

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

print(k-l)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
A = list(map(int, input().split()))
d = []
for i in range(len(A)):
  if i == len(A)-1:
    r = a-A[i]+A[0]
    d.append(r)
  else:
    d.append(A[i+1]-A[i])
print(a-max(d))

Submission status: Accepted

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

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

b.sort()
print(k-b[n-1])

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
A = list(map(int, input().split()))
d = []
for i in range(len(A)):
  if i == len(A)-1:
    r = a-A[i]+A[0]
    d.append(r)
  else:
    d.append(A[i+1]-A[i])
print(a-max(d))

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

sum_memo = sum(memo)
sum_memo -= max(memo)
print(sum_memo)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
tot_sum = 0
curr_max = 0
i = 0
while i < N - 1:
    temp = A[i + 1] - A[i]
    tot_sum += temp
    curr_max = max(curr_max, temp)
    i += 1

curr_max = max(curr_max, (K - A[i]) + A[0])
tot_sum += (K - A[i]) + A[0]

print(tot_sum - curr_max)

Submission status: Accepted

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

maximum=0

for i in range(len(a)-1):
    if maximum<a[i+1]-a[i]:
        maximum=a[i+1]-a[i]
if maximum<=min(a[N-1]-a[0],K-a[N-1]+a[0]):
    maximum=min(a[N-1]-a[0],K-a[N-1]+a[0])

print(K-maximum)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')

#
#import numpy as np

from itertools import combinations

def main():
	k,n=MI()
	A=LI()
	ans=A[-1]-A[0]

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

	print(ans)





if __name__ == "__main__":
	main()

Submission status: Accepted

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

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

print(k - max(space))

Submission status: Accepted

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

Submission status: Accepted

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

print(k - max(dis))

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):

    left = sorted(_left(K, A))
    right = sorted(_right(K, A))

    ans = max(sum(left[: len(left) - 1]), sum(right[: len(right) - 1]))

    print(ans)
    return


def _left(K, A):
    rev_a = list(A[::-1])
    end = A[0] + K - A[len(A) - 1]
    left = np.ediff1d(np.array(rev_a), to_end=[end]).tolist()
    left = [abs(l) for l in left]
    return left


def _right(K, A):
    end = A[0] + K - A[len(A) - 1]
    right = np.ediff1d(np.array(A), to_end=[end]).tolist()
    return right


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

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

d=k-abs((a[0]-a[n-1]))
max_da=max(d,max_da)

print(k-max_da)

Submission status: Accepted

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

m = A[0] + K - A[-1]
res = A[0] + K - A[-1]

for i in range(len(A) - 1):
    res += A[i + 1] - A[i]
    m = max(m, A[i + 1] - A[i])
print(res - m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# s_sum = max(points) - min(points)
# e_sum = abs(points[-1] - max(map(lambda x: x+K, points[:-1])))

# print(min([s_sum, e_sum]))

# 解けなかった

K, N = map(int, input().split())
points = list(map(int, input().split()))
l = 0

points.append(points[0]+K) # ループの辺間距離を計算するため
for i in range(N+1):
    l = max([l, points[i] - points[i-1]])

ans = K - l
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(root)

Submission status: Accepted

Solution code:
lake_length, num_of_house = map(int, input().split())
positions = list(map(int, input().split()))

max_interval = lake_length - positions[num_of_house - 1] + positions[0]

for i in range(1, num_of_house):
    max_interval = max(max_interval, positions[i] - positions[i - 1])

print(str(lake_length - max_interval))

Submission status: Accepted

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

Submission status: Accepted

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

k, n, *a = map(int, read().split())
a = sorted(a)
d = [a[0]+k-a[-1]]
for i in range(n-1):
    d.append(a[i+1]-a[i])
print(sum(d)-max(d))

Submission status: Accepted

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


dist = k-a[n-1]+a[0]
for i in range(n-1):
	if a[i+1]-a[i] >= dist:
		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()))
tmp = 0
for i in range(len(a)):
  if i == len(a) - 1:
    tmp = max(a[0]+k-a[i], tmp)
    break
  tmp = max(a[i+1]-a[i], tmp)
print(k-tmp)

Submission status: Accepted

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

ans = 0
max_dif = 0
for i in list(range(1, n)):
	x = abs(a[i] - a[i-1])
	max_dif = max(max_dif, x)
	ans += x

ans += k-a[-1]+a[0]
max_dif = max(max_dif, k-a[-1]+a[0])
ans -= max_dif
print(ans)

Submission status: Accepted

Solution code:
def solve(K, N, As):
    longest = 0
    for i in range(len(As)):
        x = 0
        if i == len(As) - 1:
            x = As[0] + (K - As[i])
        else:
            x = As[i + 1] - As[i]
        longest = max(longest, x)
    print(K - longest)


if __name__ == '__main__':
    K, N = map(int, input().split())
    As = [int(a) for a in input().split()]
    solve(K, N, As)

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 - sum(b))
b.sort()
print(sum(b[:-1]))

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 - sum(b))
b.sort()
print(sum(b[:-1]))

Submission status: Accepted

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

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

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

print(sum(B))

Submission status: Accepted

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

A.sort()
D = []
D.append(K - max(A) + min(A))

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

print(K - max(D))

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])
d = max(d, k - a[-1] + a[0])

print(k - d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(A2))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(diffA))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

# In[12]:


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


# In[15]:


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


# In[ ]:

Submission status: Accepted

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

res = []
for i in range(n-1):
  j = a[i+1] - a[i]
  res.append(j)
l = k + a[0] - a[n-1]
res.append(l)
m = max(res)
print(k - m)

Submission status: Accepted

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

j = 0
i = 0
while i<n:
    if i == n-1:
        x = k + a[0] - a[i]
    else:
        x = a[i+1] - a[i]
    if x>j:
        j=x
    i+=1

print(k-j)

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
A = [0]*N
tmp = input().split()
for a in range(N):
    A[a] = int(tmp[a])

diff = [0]*N
for a in range(N):
    
    if a == N-1:
        diff[a] = K-A[N-1]+A[0]
    else:
        diff[a] = A[a+1]-A[a]

print(K-max(diff))

Submission status: Accepted

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

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

Submission status: Accepted

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

distance = [a[0] + (k - a[-1])]

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

distance = sorted(distance)

print(k - distance[-1])

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])    
print(sum(d)-max(d))

Submission status: Accepted

Solution code:
import numpy as np
def resolve():
    K,N = map(int,input().split())
    A = np.array(list(map(int,input().split())))
    Adiff = np.diff(A)
    max_gap = max(max(Adiff), A[0] + K - A[N-1])
    print(K-max_gap)
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

print(s)

Submission status: Accepted

Solution code:
import numpy as np

k,n = map(int,input().split())
a = list(map(int,input().split()))
temp =a[0]
a.append(temp)
base = np.empty(n)

for i in range(n):
  if i == n-1:
    base[i] = (k-a[i]) + a[0]
    break
    
  base[i] = a[i+1] - a[i]
  
print(k - int(np.max(base)))

Submission status: Accepted

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

ans = []

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

print(k - max(ans))

Submission status: Accepted

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

tmp = []
for i in range(1, n):
    tmp.append(clist[i] - clist[i-1])
    
tmp.append(k- clist[n-1] + clist[0])

print(k -max(tmp))

Submission status: Accepted

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

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

print(K-num)

Submission status: Accepted

Solution code:
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
diff_A = []
for i in range(1, N):
    diff_A += [(i - 1, A[i] - A[i - 1])]
### A_N → A1 の距離
diff_A += [(N, K - A[i] + A[0])]
sorted_diff_A = sorted(diff_A, key=lambda x: x[1], reverse=True)
print(K - sorted_diff_A[0][1])

Submission status: Accepted

Solution code:
k,n = list(map(int, input().split()))
a = list(map(int, input().split()))
# 1～nまでの家間の距離
distance=[a[i]-a[i-1] for i in range(1,n)]
# 末尾～先頭の距離を追加
distance.append(k-sum(distance))

# 円周から最大距離を除いたものが最短距離
print(k-max(distance))

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

Submission status: Accepted

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

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

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

Submission status: Accepted

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

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

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

print(K-max)

Submission status: Accepted

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

KUKAN = [0] * N

for i in range(N):
    if i != N - 1:
        KUKAN[i] = A[i + 1] - A[i]
    else:
        KUKAN[N - 1] = K - A[N - 1] + A[0]

print(str(sum(KUKAN) - max(KUKAN)))

Submission status: Accepted

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

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

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

    if max < dis:
        max = dis


print(K - max)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
houses=list(map(int,input().split()))
houses.sort()
maxx=-float('INF')
for i in range(n-1):
    if houses[i+1]-houses[i]>maxx:
        maxx=houses[i+1]-houses[i]

maxx=max([maxx,k-(houses[-1]-houses[0])])
print(k-maxx)

Submission status: Accepted

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

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

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

print(K - max(S))

Submission status: Accepted

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

Submission status: Accepted

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

#k,n = 20,5
#a = [1,2,3,4,19]

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

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

print(k - max(s))

Submission status: Accepted

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

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

print(K - max(S))

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
#K: 湖1周の長さ, N:湖の周りにある家の数
 
A = list(map(int, input().split()))
l = 0
A.append(A[0]+K)
for i,aa in enumerate(A):
    if(i<len(A)-1):
        l = max(l, A[i+1]-aa)
print(K-l)

Submission status: Accepted

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

Submission status: Accepted

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


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


print(min(far_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
An = list(map(int, input().split()))
##大きい順に並べ替える
##sort関数はnoneが返ってくるのでこっち
Bn = sorted(An)

##ここが一番遠いと仮定する
C = K-(Bn[N-1]-Bn[0])

##１つ１つの差を計算してリストへ入れて最大を探す
for i in range(N-1):
    C = max(C, Bn[i+1]-Bn[i])

print(K-C)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()
# sys.setrecursionlimit(100000)
# INF = 1001001001001
# MOD = 1000000007


def main():

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

    if N == 2:
        d = abs(A[0]-A[1])
        print(min(d, K-d))
    else:
        max_d = K - A[N-1] + A[0]
        for i in range(N - 1):
            max_d = max(max_d, abs(A[i] - A[i+1]))
        print(K - max_d)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
lists = []
for i in range(n-1):
    lists.append(abs(a[i] - a[i+1]))
lists.append(k - a[-1] + a[0])
jyogai = lists.index(max(lists))
cnt = 0
for i in range(len(lists)):
    if i != jyogai:
        cnt += lists[i]
print(cnt)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
list1=list(map(int,input().split()))
list1.sort()
list2=[]
for i in range(b-1):
    list2.append(list1[i+1]-list1[i])
list2.append(list1[0]+a-list1[-1])
list2.sort()
ans=[list1[-1]-list1[0],a-max(list2)]
print(min(ans))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
As = list(map(int, input().split()))
ds = []
for i in range(N):
  if i == 0:
    d = K+As[i]-As[i-1]
  else:
    d = As[i]-As[i-1]
  ds.append(d)
#print(ds)
r = K-max(ds)
print(r)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

d_max = A[0] + K - A[-1]
for a1, a2 in zip(A, A[1:]):
    d_max = max(d_max, a2 - a1)

print(K - d_max)

Submission status: Accepted

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

k,n = imap()
a = iarr()
dist = []
for i in range(n):
    tmp = a[i] - a[i-1]
    if tmp<0: tmp += k
    dist.append(tmp)
ans = k - max(dist)
print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
dist=0
max_dist=0
max_i=0
for i in range(N):
	a=A[i%N]
	b=A[(i+1)%N]
	tmp=b-a if b>a else K+b-a
	if max_dist<tmp:
		max_dist=max(max_dist,tmp)
		max_i = i
for i in range(N-1):
	i2 = i+max_i+1
	a=A[i2%N]
	b=A[(i2+1)%N]
	tmp=b-a if b>a else K+b-a
	dist+=tmp
print(dist)

Submission status: Accepted

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

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

for i in range(N-1):
    dis = A[i+1] - A[i]
    if maxdis < dis:
        maxdis = dis
        
print (K-maxdis)

Submission status: Accepted

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

ans = a[n-1]-a[0]

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

    if ans > x:
        ans = x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

dis=A[0]+K-A[N-1]

for i in range(N-1):
    if A[i+1]-A[i]>=dis:
        dis=A[i+1]-A[i]
    else:
        pass

print(K-dis)

Submission status: Accepted

Solution code:
K, N = list(map(int, input().split()))
a = list( map( int, input().split() ) )
for i in range(N):
  a.append( a[i] + K )

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

max_index = d.index(max(d))

print(a[max_index + N] - a[max_index + 1])

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
#K: 湖1周の長さ, N:湖の周りにある家の数

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