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())
l=list(map(int,input().split()))

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

Submission status: Accepted

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

maxLen = 0
A.sort()
for i in range(len(A)-1):
    l = A[i+1] - A[i]
    if l > maxLen:
        maxLen = l 
l = (A[0]+K) - A[i+1]
if l > maxLen:
    maxLen = l 

print(K-maxLen)

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
#import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
input = sys.stdin.readline
#文字列のときはうまく行かないのでコメントアウトすること
sys.setrecursionlimit(2147483647)

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

print(k - maximum)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

K,N = LI()
A = LI()

A.sort()

x = 0
for i in range(1,N):
    if A[i]-A[i-1] > x:
        x = A[i]-A[i-1]

if K-(A[N-1]-A[0]) > x:
    x = K-(A[N-1]-A[0])

print(K-x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from pprint import pprint

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

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

print(K - max(d))

Submission status: Accepted

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

max_l = k-a[-1]+a[0]


for i in range(1,n):
    if a[i] - a[i-1] > max_l:
        max_l = a[i] - a[i-1]



print(k-max_l)

Submission status: Accepted

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

A.append(A[0] + K)
lis = sorted(list(np.diff(A)))
print(sum(lis[:-1]))

Submission status: Accepted

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

x = np.array(a)
a1 = x[1:] - x[:-1]

ans = list(a1)
ans.append((k-a[-1])+a[0])

print(int(k-max(ans)))

Submission status: Accepted

Solution code:
import numpy as np

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

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

a_l = np.array(a_l)

b_l = np.empty(a_l.shape[0])

b_l[1:] = a_l[:-1]

b_l[0] = a_l[-1]

c_l = a_l - b_l

c_l[0] = K - a_l[-1] + a_l[0]

print(int(K - np.max(c_l)))

Submission status: Accepted

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

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

ans = K - max_d
print(ans)

Submission status: Accepted

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

arr.sort()

distances = [(arr[(i + 1) % n] - arr[i]) % k for i in range(len(arr))]

print(k - max(distances))

Submission status: Accepted

Solution code:
# 2020/03/28
# AtCoder Beginner Contest 160 - C

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

# Init
interhouse = [0] * n

# Calc
for i in range(n-1):
    interhouse[i] = a[i+1] - a[i]

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

ans = k - max(interhouse)

# Output
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def input_multiple_number():
    return map(int, input().split())

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


K ,N = input_multiple_number()
listA = input_multiple_number_as_list()

# listA[0] += listA[-1]
# listA[-1] = 0

diffy=[0]*(len(listA))

bif = 0
for d in range(len(diffy)):
	diffy[d] = listA[d]-bif
	bif = listA[d]
diffy[0] += (K-listA[-1])

maxx = 0
for a in diffy:
	maxx = max(a,maxx)

print(K-maxx)

Submission status: Accepted

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


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



# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ld.sort()

print(k - ld[-1])

Submission status: Accepted

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

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

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

ans = sum(d) - max(d)

print(ans)

Submission status: Accepted

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

#間が一番大きいところを避けたい
between_house = list()
between_house = [A_list[i+1]-A_list[i] for i in range(N-1)]
between_house.append((K - A_list[-1])+A_list[0])

print(K-max(between_house))

Submission status: Accepted

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

maxdis = 0
for i in range(n):
  if i == 0:
    continue
  if As[i] - As[i-1] > maxdis:
    maxdis = As[i] - As[i-1]
if As[0] + k - As[n-1] > maxdis:
  maxdis = As[0] + k - As[n-1]
print(k - maxdis)

Submission status: Accepted

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

ans=k-max(b)
print(ans)

Submission status: Accepted

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


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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

k = inputs1[0]
n = inputs1[1]

b = [0 for i in range(n)]

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

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

m = max(b)

print(k-m)

Submission status: Accepted

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

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-1):
    B.append(A[i+1]-A[i])
B.append((K-A[N-1])+A[0])

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

Submission status: Accepted

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

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

print(k - ans)

Submission status: Accepted

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

Submission status: Accepted

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

dA = [A[i+1]-A[i] for i in range(n-1)]
dA += [k- A[-1]+A[0]]

print(sum(dA) - max(dA))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
k,n=map(int, raw_input().split())
houses=map(int, raw_input().split())
maxd=0
l=-1
r=-1
for i in xrange(1, n):
    maxd=max(maxd, houses[i]-houses[i-1])

maxd=max(maxd, k-houses[-1]+houses[0])
# print maxd
print k-maxd

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):
    d = 0
    for i in range(0, N):
        if i + 1 == N:
            next = K - A[i] + A[0]
        else:
            next = A[i + 1] - A[i]
        d = max(d, next)
    print(K - d)
        
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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 a[i + 1] - a[i] > m:
        m = a[i + 1] - a[i]
if k + a[0] - a[n - 1] > m:
    m = k + a[0] - a[n - 1]
ans = k - m
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

k,n = map(int,input().split())
ai = [int(i) for i in input().split()]

sukima = ai[0] + k - ai[n-1]

for i in range(n-1):
    sukima = max(sukima,ai[i+1]-ai[i])
    
print(k-sukima)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

print(K-dis)

Submission status: Accepted

Solution code:
#def main(S, b):
if __name__ == '__main__':
    k, n = map(int, input().split())

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

    for i in range(n):
        A.append(A[i] + k)

    #print(A)
    ans = 10000000000
    for i in range(n):
        #print(ans, A[i + n - 1], A[i])
        if (ans > A[i + n - 1] - A[i]):
            ans = A[i + n - 1] - A[i]

    print(ans)

Submission status: Accepted

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

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

print(K-a)

Submission status: Accepted

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

Submission status: Accepted

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

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

s2 = []
for i in range(N-1):
  s2.append(s[i+1] - s[i])
s2.append(s[0]+L - s[N-1])

s2 = sorted(s2)

print(sum(s2[:-1]))

Submission status: Accepted

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

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

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

print(k - max(diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

D = 0

for idx, a in enumerate(A):
    d = K + a - A[idx-1] if idx == 0 else a - A[idx-1]
    if D < d:
        D = d
print(K-D)

Submission status: Accepted

Solution code:
d = input().split(" ")
k = int(d[0])
n = int(d[1])
a = input().split(" ")
for i in range(n):
	a[i] = int(a[i])
a.sort()
p = 0
for i in range(n-1):
	c = a[i+1] - a[i]
	if c > p:
		p = c
e = a[0] + k - a[n-1]
if e > p:
	p = e
print(k - p)

Submission status: Accepted

Solution code:
import sys

max_dist = -1

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

for i in range(n):
	if i == n - 1:
		max_dist = max(max_dist, p + a[0] - a[i])
	else:
		max_dist = max(max_dist, a[i + 1] - a[i])

print(p - max_dist)

Submission status: Accepted

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


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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

    print(k - longest)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
inp = list(map(lambda x: int(x), input().split(" ")))
K = inp[0]
N = inp[1]
inp = list(map(lambda x: int(x), input().split(" ")))

diffs = []

for i in range(N-1):
    diffs.append(inp[i+1]-inp[i])
diffs.append(inp[0]+K-inp[-1])

print(sum(diffs)-max(diffs))

Submission status: Accepted

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

Submission status: Accepted

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

max_distance = 0

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

#最初と最後の距離を計算
distance = A[0]+(K-A[N-1])
max_distance = max(distance,max_distance)

print(K-max_distance)

Submission status: Accepted

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

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

Submission status: Accepted

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

b=sorted(b)
print(sum(b[:n-1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 21:05:47 2020

@author: H_Hoshigi
"""

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

A_list.append(A_list[0]+K)
#A_listは入力の時点で昇順にソートされている。

dA_list = [A_list[i+1]-A_list[i] for i in range(N)]

print(K-max(dA_list))

Submission status: Accepted

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

Submission status: Accepted

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

a = [0 for i in range(N)]
amax = 0
for i in range(N):
    a[i] = A[i] - A[i-1]
    if a[i] < 0:
        a[i] += K
    if a[i] > amax:
        amax = a[i]

print(K-amax)

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):
  cnt = a[i-1] - a[i] + k
  ans = min(cnt, ans)
print(ans)

Submission status: Accepted

Solution code:
l,n = map(int,input().split())
input_list = list(map(int, input().split()))
maxlens = l-input_list[-1]+input_list[0]

for i in range(1,n):
  	maxlens = max(maxlens,input_list[i]-input_list[i-1])

print(l-maxlens)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
k,n=map(int,sys.stdin.readline().split())
arr=list(map(int,sys.stdin.readline().split()))
dis=-1
for i in range(n-1):
    d=arr[i+1]-arr[i]
    dis=max(dis,d)
d=(k-arr[-1]+arr[0])
dis=max(dis,d)
print(k-dis)

Submission status: Accepted

Solution code:
import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))

    ans = INF

    temp1 = a[-1] - a[0]
    temp2 = k - (a[1] - a[0])
    ans = min(ans, temp1, temp2)

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

    temp1 = k - (a[-1] - a[-2])
    temp2 = a[-1] - a[0]
    ans = min(ans, temp1, temp2)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
d = list(map(lambda x: x[1]-x[0], zip(a, a[1:])))
first_and_last = a[0] + (k - a[-1])
ignore = max(d+[first_and_last])
ans = k - ignore
print(ans)

Submission status: Accepted

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

# d[i] = i番目とi+1番目の間の距離（d[n]はnと0番目の家の距離）
d = [0 for i in range(n)]
for i in range(n-1) :
  d[i] = a[i+1] - a[i]
d[n-1] = k - a[n-1] + a[0]

print(sum(d)-max(d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    length, num = map(int, input().split())
    a = list(map(int, input().split()))
    max_dis = 0 
    first_h = a[0]
    prev_h = a[0]
    for h in a[1:]:
        dis = h - prev_h
        if dis > max_dis:
            max_dis = dis
        prev_h = h

    dis = (length - prev_h) + first_h
    max_dis = max(max_dis, dis)

    print(length - max_dis)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print K-max_dis

Submission status: Accepted

Solution code:
K,N = list(map(int,input().split()))
place_list = list(map(int,input().split()))
place = set()
for i in range(N-1):
    place.add(abs(place_list[i] - place_list[i+1]))
place.add(abs(K-place_list[N-1]+place_list[0]))
print(K-max(place))

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
P=list(map(int,input().split()))
SA=[0]*(n)

for i in range(n-1):
    SA[i]=P[i+1]-P[i]
SA[-1]=k-P[-1]+P[0]
print(k-max(SA))

Submission status: Accepted

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

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

print(k-max_diff)

Submission status: Accepted

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

import numpy as np
print(k - max(max(np.diff(a)), (a[0]-0)+(k-a[-1])))

Submission status: Accepted

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

print(min(dis))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
k, n = map(int, input().split())
a_list = list(map(int, input().split()))
d_list = [a_list[0] + k - a_list[n - 1]]
max_d = a_list[0] + k - a_list[n - 1]
for i in range(0, n - 1):
    d_list.append(a_list[i + 1] - a_list[i])
    if max_d < a_list[i + 1] - a_list[i]:
        max_d = a_list[i + 1] - a_list[i]
print(sum(d_list) - max_d)

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import bisect

def solve(m, A):
    #print('m: %d, A: %s' % (m, str(A)))
    n = len(A)
    A.sort()
    min_val = 10 ** 18
    for i in range(n):
        j = (i + 1) % n
        d = abs(A[i] - A[j])
        val = m - min(d, m - d)
        #print('A[%d]: %d, A[%d]: %d, val: %d' % (i, A[i], j, A[j], val))
        min_val = min(min_val, val)
    return min_val

def main():
    m, n = map(int, input().split())
    A = list(map(int, input().split()))
    print(solve(m, A))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 20:51:34 2020

@author: naoki
"""

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

print(K - max(diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
    k,n = map(int,input().split())
    A = sorted(list(map(int,input().split())))
    mx = 0

    for i,a in enumerate(A):
        if i == 0:
            continue
        if abs(A[i] - A[i-1]) > mx:
            mx = abs(A[i] - A[i-1])

    delta = max(A) - min(A)



    print(min(delta,k-mx))




if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

A = list(map(int,input().split()))
# 家どうしの距離
dis = [A[i+1] - A[i] for i in range(N-1)]
dis.append(A[0]+K-A[N-1])
# 家どうしの距離が最長になるものを選んで，それをKから引く[
print(K-max(dis))

Submission status: Accepted

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

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

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

    d = (K - A[-1]) + A[0]
    ans += d
    md = max(md, d)

    ans -= md

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(K-mx)