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:
from itertools import tee


def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

lengths = [abs(p - q) for p, q in pairwise(A + [A[0] + K])]
sum_length = sum(lengths)
max_length = max(lengths)

print(sum_length - max_length)

Submission status: Accepted

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

Submission status: Accepted

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


minimum = 10**8
for i in range(N):
    if minimum > (A[N - 1 + i] - A[i]): minimum = A[N - 1 + i] - A[i]
print(minimum)

Submission status: Accepted

Solution code:
# exclude the largest distance between consecutive positions.
# (also considering going around the circle).

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

Submission status: Accepted

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

dif = []

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

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

print(k - max(dif))

Submission status: Accepted

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

mx = K - A[N-1] +A[0]
for i in range(N-1):
  if A[i+1] - A[i] > mx:
    mx = A[i+1] - A[i]
    
ans = K - mx
print(ans)

Submission status: Accepted

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

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):
    m=max(m,A[i+1]-A[i])
m=max(m,A[0]+k-A[-1])
print(k-m)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
import numpy as np
a = np.array(list(map(int, input().split())))
dist = np.abs(a -np.roll(a, 1))
dist[0] = min(k-a[n-1] + a[0], a[n-1]-a[0])
ans_a = np.sum(dist)
ans = ans_a
for i in range(n):
  ans_sub = ans_a-dist[i]
  if ans_sub < ans:
    ans = ans_sub
print(ans)

Submission status: Accepted

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

for i in range(n-1):
    d = A[i+1]-A[i]
    if d > dmax:
        dmax = d
dmax = max(k-A[-1]+A[0], dmax)

print(k-dmax)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

dummy = list(map(int, input().split(" ")))
K = dummy[0]
N = dummy[1]
A = np.array(list(map(int, input().split(" "))))
Distance = A - np.roll(A, 1)
Distance[0] = K - sum(Distance[1:])
#out = sum(A) - max(A)
#print(A, Distance)
out = K - max(Distance)
print(out)

Submission status: Accepted

Solution code:
# C
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))

A = np.array(sorted(A))

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

print(np.sum(A_div)-np.max(A_div))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = [int(i) for i in input().split()]
e = K - A[N - 1] + A[0] # N番目から1番目の間の距離
R = []
for i in range(N - 1):
    R.append(A[i + 1] - A[i])
R.append(e)
R.sort()
del R[-1]
print(sum(R))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

d = h_list[0] + k - h_list[n - 1]
for i in range(n - 1):
    d = max(d, h_list[i + 1] - h_list[i])
out = k - d
print(out)

Submission status: Accepted

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

Submission status: Accepted

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

two_loops = As + [A+K for A in As]

max_dist = 0
max_dist_index = 0
for i in range(len(two_loops)-1):
    new_dist = two_loops[i+1]-two_loops[i]
    if new_dist > max_dist:
        max_dist = new_dist
        max_dist_index = i+1
print(two_loops[max_dist_index+N-1]-two_loops[max_dist_index])

Submission status: Accepted

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

li.append(pond+li[0])
maxl=0

for i in range(len(li)-1):
    maxl=max(maxl, li[i+1]-li[i])
print(pond-maxl)

Submission status: Accepted

Solution code:
input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line]

K,N = Line[0],Line[1]

# print(K,N)

input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line]

# print(Line)

sinLine = [y - x for (x, y) in zip(Line[0:], Line[1:])]

sinLine.append(Line[0]+K-Line[-1])

print(sum(sinLine)-max(sinLine))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
a = np.array(list(map(int, input().split())) + [k])

a[-1] += a[0]
diff = np.diff(a)
print(np.sum(diff) - np.max(diff))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys 

K, N = [int(i) for i in sys.stdin.readline().strip().split(" ")]
A = [int(i) for i in sys.stdin.readline().strip().split(" ")]

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

n = K-A[-1] + A[0]
m = max(m, n)
s += n

print(s - m)

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
li = list(map(int,input().split()))
li.append(li[0]+k)
d_li = [0] * n
for i in range(len(li)-1):
    d_li[i] = li[i+1] - li[i]
d_li.sort()
print(sum(d_li[:n-1]))

Submission status: Accepted

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

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

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


print(K-max)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 10 ** 9
mx = max(A)
mi = min(A)
tmp = mx - mi
ans = tmp
for i in range(N):
    A.append(A[i]+K)

# ここから連続するNの部分の最小幅を出す

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

print(ans)

Submission status: Accepted

Solution code:
#n=int(input())
k,n=map(int,input().split())
al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

mn=min(al)
mx=max(al)
dis=[]
for i in range(n-1):
    dis.append(al[i+1]-al[i])
dis.append(mn+(k-mx))

dis.sort(reverse=False)
ans=0
for i in range(n-1):
    ans+=dis[i]
print(ans)

Submission status: Accepted

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

B = [None] * N


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

print(sum(B) - max(B))

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

    maxi = 0
    ind = 0
    for i in range(n):
        if maxi < a[i+1] - a[i]:
            maxi = a[i+1] - a[i]
            ind = i

    for i in range(ind+1, n):
        yield a[i+1] - a[i]

    for i in range(0, ind):
        yield a[i+1] - a[i]


print(sum(main()))

Submission status: Accepted

Solution code:
import sys
import os
from io import BytesIO, IOBase

#########################
# imgur.com/Pkt7iIf.png #
#########################

# returns the list of prime numbers less than or equal to n:
'''def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r'''

# returns all the divisors of a number n(takes an additional parameter start):
'''def divs(n, start=1):
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors'''

# returns the number of factors of a given number if a primes list is given:
'''def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
    return divs_number'''

# returns the leftmost and rightmost positions of x in a given list d(if x isnot present then returns (-1,-1)):
'''def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)'''

# returns (b**p)%m 
'''def modpow(b,p,m):
    res=1
    b=b%m
    while(p):
        if p&1:
            res=(res*b)%m
        b=(b*b)%m
        p>>=1
    return res'''

# if m is a prime this can be used to determine (1//a)%m or modular multiplicative inverse of a number a
'''def mod_inv(a,m):
    return modpow(a,m-2,m)'''

# returns the ncr%m for (if m is a prime number) for very large n and r
'''def ncr(n,r,m):
    res=1
    if r==0:
        return 1
    if n-r<r:
        r=n-r
    p,k=1,1
    while(r):
        res=((res%m)*(((n%m)*mod_inv(r,m))%m))%m
        n-=1
        r-=1
    return res'''

# returns ncr%m (if m is a prime number and there should be a list fact which stores the factorial values upto n):
'''def ncrlis(n,r,m,fact):
    return (fact[n]*(mod_inv(fact[r],m)*mod_inv(fact[n-r],m))%m)%m'''

#count xor of numbers from 1 to n:
def xor1_n(n):
    d={0:n,1:1,2:n+1,3:0}
    return d[n&3]

def ceil(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def ddd(): return collections.defaultdict(collections.defaultdict(int))
 

# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
			while True:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					if not b:
							break
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines = 0
			return self.buffer.read()
 
	def readline(self):
			while self.newlines == 0:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					self.newlines = b.count(b"\n") + (not b)
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines -= 1
			return self.buffer.readline()
 
	def flush(self):
			if self.writable:
					os.write(self._fd, self.buffer.getvalue())
					self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
			self.buffer = FastIO(file)
			self.flush = self.buffer.flush
			self.writable = self.buffer.writable
			self.write = lambda s: self.buffer.write(s.encode("ascii"))
			self.read = lambda: self.buffer.read().decode("ascii")
			self.readline = lambda: self.buffer.readline().decode("ascii")
 

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#import collections
#from collections import deque
#import math
#import time
#import itertools
#import timeit
#import random
#from bisect import bisect_left as bl
#from bisect import bisect_right as br
#from bisect import insort_left as il
#from collections import OrderedDict

for _ in range(1):
    k,n=mi()
    a=li()
    mx=k-a[-1]+a[0]
    res=k-a[-1]+a[0]
    for i in range(n-1):
        res+=a[i+1]-a[i]
        mx=max(mx,a[i+1]-a[i])
    print(res-mx)

Submission status: Accepted

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

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

if x<K-A[N-1]+A[0]:
  x=K-A[N-1]+A[0]
print(K-x)

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
lists = list(map(int,input().split()))
maxdist = 0
for i in range(N-1):
    dist = lists[i+1] - lists[i]
    if dist > maxdist:
        maxdist = dist
adis = K-lists[N-1]+lists[0]
if adis > maxdist:
    maxdist = adis

print(K-maxdist)

Submission status: Accepted

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

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

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

if buf_2 > buf_1 :
    print(A[N-1] - A[0])
else :
    print(K - buf_1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
def main():
    k, n = map(int, input().split(" "))
    As = list(map(int, input().split(" ")))

    As = np.array(As)

    As_dists = As[1:] - As[:-1]
    #print(As_dists)
    As_dists = np.append(As_dists, As[0] + (k - As[-1]))
    max_dists = np.max(As_dists)
    #print(As_dists)
    #print(max_dists)

    ans = k - max_dists
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

    a = np.array(A)
    p = a[1:] - a[:-1]
    q = K - a[-1] + a[0]
    print(K -max(np.max(p),q))

    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 = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
a.append(a[0] + k)

longest = 0

for i in range(n):
    tmp = a[i+1] - a[i]
    longest = max(longest, tmp)
    
print(k - longest)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(B))

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
k,n = map(int,stdin.readline().rstrip().split())
a = list(map(int,stdin.readline().rstrip().split()))
b = []
for i in range(n-1):
    b.append(a[i+1]-a[i])
b = [0]+b
c = b.index(max(b))
print(min(a[-1]-a[0],a[c-1]+k-a[c]))

Submission status: Accepted

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

inter = 0


for i in range(n-1):
    inter = max(inter, a[i+1]-a[i])
    if inter == int(k/2):
        break

inter = max(inter, a[0]+k-a[n-1])

print(k-inter)

Submission status: Accepted

Solution code:
def LI():
	return [ int(s) for s in input().split() ]

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

A_diff = [0]*N
A_diff[0] = A[0] + K-A[N-1]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

  tmp = max(tmp,a[i+1]-a[i])

print(k - tmp)

Submission status: Accepted

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

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

print(K-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(L))

Submission status: Accepted

Solution code:
[k,n] = [int(x) for x in input().split()]
data = [int(x) for x in input().split()]
data.append(data[0]+k) # 最後の家から最初の家までの距離を追加する

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

target_distance = k - max(kankaku_list)

print(target_distance)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
import numpy as np

k,n = mi()
a=np.array(li())

b = a[1:] - a[:n-1]
b_max = np.max(b)
c = a[0] + k-a[n-1]

print(k-max(b_max,c))

Submission status: Accepted

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

print(K - max(sa))

Submission status: Accepted

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

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

print(tmp)

Submission status: Accepted

Solution code:
KNlist = list(map(int, input().split()))
Alist = list(map(int, input().split()))
K = KNlist[0]
N = KNlist[1]
northMiddleS = K - (Alist[-1]-Alist[0])
distances = []
for i in range(N-1):
    difference = Alist[i+1]-Alist[i]
    distances.append(difference)
distances.append(northMiddleS)
maxDistanceS = max(distances)
shortest = K-maxDistanceS
print(shortest)

Submission status: Accepted

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


i_max=0
d_max=0

for i in range(N):
    im1=(i-1)%N
    d=(As[i]-As[im1])%K
    if d > d_max:
        d_max=d
        i_max=i


ans=(As[(i_max-1)%N]-As[i_max])%K
print(ans)

Submission status: Accepted

Solution code:
n= [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
lake = n[0]
num_ie = n[1]
kyori = []
for i in range(num_ie-1):
    kyori.append(l[i+1]-l[i])
kyori.append(lake - l[num_ie-1] + l[0])
output = 0
output += sum(kyori) 
output -= max(kyori)

print(output)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import defaultdict
    K,N=list(map(int,sys.stdin.readline().split()))
    AL=list(map(int,sys.stdin.readline().split()))
    diff=[AL[i]-AL[i-1] for i in range(1,N)]
    diff.append(K-AL[-1]+AL[0])
    print(K-max(diff))


main()

Submission status: Accepted

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

max_distance = 0

for i in range(N):
	if i == N-1:
		if max_distance < (house[0] + circle_meter - house[i]):
			max_distance = house[0] + circle_meter - house[i]
		else:
			break
	elif max_distance < (house[i+1] -house[i]):
		max_distance = (house[i+1]-house[i])
	
result = circle_meter - max_distance
print(result)

Submission status: Accepted

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

between_distances = []
for i, a in enumerate(A):
    if i == 0: continue
    old_a = A[i-1]
    between_distances.append(a - old_a)
between_distances.append(K - A[-1] + A[0])

print(sum(between_distances) - max(between_distances))

Submission status: Accepted

Solution code:
kn = input('').split(' ')
k = int(kn[0])
n = int(kn[1])
a = input('').split(' ')

d = []
d.append(k - int(a[n-1])+int(a[0]))
xd = d[0]
for i in range(1,n):
  d.append(int(a[i]) - int(a[i-1]))
  if xd < d[i]:
    xd = d[i]
print(k - xd)

Submission status: Accepted

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

distance = [A[i+1]-A[i] for i in range(len(A)-1)]

max_distance = max(distance)

#print(distance)
#print(max_distance)

candidate = A[0] + (K-A[N-1])
#print(candidate)

max_distance = max(max_distance,candidate)

#print(max_distance)

ans = K - max_distance
print(ans)

Submission status: Accepted

Solution code:
k,n = list(map(int, input().split())) 
i= list(map(int, input().split())) 
ans=0
for j in range(n):
    if j==0:
        x=i[n-1]-i[0]
        y=(k-i[1])+i[0]
        ans=min(x,y)
    elif j==n-1:
        x=(k-i[j])+i[j-1]
    
        if ans>x:
            ans=x
    else:
        x=(k-i[j])+i[j-1]
        y=i[j]+(k-i[j+1])
        if ans>min(x,y):
            ans=min(x,y)
print(ans)

Submission status: Accepted

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

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):
    l = A[i+1] - A[i]
    if m <= l:
        m = l
        
if m <= K-A[N-1]+A[0]:
    m = K-A[N-1]+A[0]
    
print(K-m)

Submission status: Accepted

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

max_dist = 0
for i in range(len(A)):
    if i != len(A) - 1:
        dist = A[i + 1] - A[i]
    else:
        dist = K - A[i] + A[0]
    if dist > max_dist:
        max_dist = dist
print(K - max_dist)

Submission status: Accepted

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

max_dis = 0
for i in range(n):
    if i < n - 1:
        dis = abs(a[i] - a[(i+1)%n])
    else:
        dis = a[0] + k - a[i]
    if max_dis < dis:
        max_dis = dis
print(k - max_dis)

Submission status: Accepted

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

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

max = 0

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

print(K-max)

Submission status: Accepted

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

for i in range(n):
    if i != (n - 1):
        num = a[i+1] - a[i]
    else:
        num = (k - a[i]) + a[0]
    
    res += num
    if max_num < num:
        max_num = num

print(res - max_num)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

diff_max=A[0]+K-A[N-1]
imax=N
for i in range(N-1):
    d=A[i+1]-A[i]
    if d>diff_max:
        diff_max=d
        imax=i

print(K-diff_max)

Submission status: Accepted

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

x = c[n-1]-c[0]
if x < answer:
  answer = x

y = c[0]+k-c[1]
if y < answer:
  answer = y

z = k-c[n-1]+c[n-2]
if z < answer:
  answer = z

w = c[n-1] - c[0]
if w < answer:
  answer = w

for i in range(1,n-1):
  a = c[i-1]+k-c[i]
  b = c[i]+k-c[i+1]
  if a < answer:
    answer = a
  elif b < answer:
    answer = b

print(answer)

Submission status: Accepted

Solution code:
K, N, *A = map(int, open(0).read().split())

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

Submission status: Accepted

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

from sys import stdin


def main():
    K, N = list(map( int, stdin.readline().split() ))
    A = list(map(int, stdin.readline().split()))
    B = []
    B.append( A[0] + (K - A[-1]) )
    for i in range(N - 1):
        B.append(A[i + 1] - A[i])

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
K,N=map(int, input().split())  
A=np.array(input().split(),np.int64)
A_diff = np.diff(A, n=1)
A_diff = np.insert(A_diff, 0, A[0]+(K-A[-1]))
#print(A_diff)
ans = np.sum(A_diff)-np.max(A_diff)
print(ans)

Submission status: Accepted

Solution code:
num = input().split(' ')
num_data = [int(num[i]) for i in range(2)]
data = input().split(' ')
data_m = [int(data[i]) for i in range(num_data[1])]
data_m.append(num_data[0] + data_m[0])
key = data_m[0]
locate = 0
for i in range(num_data[1]):
    if(key < data_m[i + 1] - data_m[i]):
        key = data_m[i + 1] - data_m[i]
        locate = i + 1
print(num_data[0] - key)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
k,n=map(int,input().split())
arr=list(map(int,input().split()))
right_dis=k-arr[-1]
ans=1000000000000000
ans=min(ans,k-right_dis-arr[0])
# print(ans)
for i in range(1,len(arr)):
	dis=k-abs(arr[i]-arr[i-1])
	# print(dis,i)
	ans=min(ans,dis)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

A_K = [A[x]-K for x in range(len(A))] 
newA = A_K + A

kyorist = [newA[x+len(A)-1] - newA[x] for x in range(len(A))]

print(min(kyorist))

Submission status: Accepted

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

if N == 2:
    ans = AList[1] - AList[0]
else:
    lengths = [AList[-1] - AList[0]]
    for i in range(1, N):
        lengths.append(K - AList[i] + AList[i-1])
    ans = min(lengths)
print(ans)

Submission status: Accepted

Solution code:
## coding: UTF-8
from itertools import permutations, combinations,combinations_with_replacement,product
s = input().split()
q = [int(p) for p in s]
#print(q)
K = q[0]
N = q[1]

t = input().split()
r = [int(p) for p in t]
#print(K)
#print(N)
#print(r)

#r = [2, 9 ,17]
dist = []

def calc_dist(a, b):
    if(a - b < 0):
        return a - b + K
    else:
        return a - b

for i in range(N):
    dist.append(calc_dist(r[i], r[i-1]))
#print(dist)

answer = K - max(dist)
print(answer)

Submission status: Accepted

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

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

amax=k-a[n-1]+a[0]
for i in range(n-1):
    ai=a[i+1]-a[i]
    amax=max(ai,amax)
#    print(ai,amax)

print(k-amax)