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()))
s = []
for i in range(N-1):
    s.append(A[i+1] - A[i])
s.append(A[0] + K - A[-1])
print(K - max(s))

Submission status: Accepted

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

for i in range(n-1):
  dst.append(lst[i+1]-lst[i])
dst.append(lst[0] + k-lst[n-1])

dst_rev = sorted(dst)

ans = 0

for i in range(n-1):
  ans += dst_rev[i]

print (ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

min_num = data[-1] - data[0]

for _ in range(N-1):
  temp = data[_] + K-data[_+1]
  if min_num > temp:
    min_num = temp
print(min_num)

Submission status: Accepted

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

segments = [a[i] - a[i-1] for i in range(1, n)]
segments.append(k - a[n-1] + a[0])

print(k - max(segments))

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

max = A[0] + (K - A[-1])
for i in range(len(A)-1):
  d = A[i+1] - A[i]
  if d > max:
    max = d
print(K-max)

Submission status: Accepted

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

    else:
        x = l[i+1]-l[i]

    if x > m:
        m = x

print(k-m)

Submission status: Accepted

Solution code:
def main():
    K, N = [int(_) for _ in input().split()]
    A = list(map(int, input().split()))
    mx = 0

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

Submission status: Accepted

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

mx = 0
for i in range(n):
  nxt = (i+1) % n
  dist = a[nxt] - a[i]
  dist = (dist + k) % k
  mx = max(mx, dist)

print(k-mx)

Submission status: Accepted

Solution code:
p, n = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
val = l[-1]-l[0]
for i in range(1,n):
    val = min(val, l[-1] - l[i] + p-l[-1]+l[i-1])
print(val)

Submission status: Accepted

Solution code:
import sys
K,N=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

L=[]
prev=None
for a in A:
    if prev is None:
        prev=a
    else:
        L.append(a-prev)
        prev=a
else:
    L.append(K-A[-1]+A[0])

print K-max(L)

Submission status: Accepted

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

Submission status: Accepted

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

per_dist = list(A[i+1]-A[i] for i in range(0,N-1))
last_dist = A[0] + K - A[N - 1]
type(last_dist)
per_dist.append(last_dist)
max_dist = max(per_dist)
print(K - max_dist)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

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:
        ans.append(a[n-1] - a[i])
        continue
    ans.append((a[i-1]+k)-a[i])
#print(ans)
print(min(ans))

Submission status: Accepted

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

min_num = data[-1] - data[0]

for _ in range(N-1):
    min_num = min([min_num,data[_] + K-data[_+1]])
print(min_num)

Submission status: Accepted

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

Submission status: Accepted

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

D = [j - i for i, j in zip(A, A[1:])]

ans = k - max(D)

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

tmp.remove(max(tmp))

print(sum(tmp))

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

Submission status: Accepted

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

m = A[N-1] - A[0]

for i in range(N - 1):
    n = (K - A[i+1]) + A[i]
    # print("i:{}, n:{}".format(i, n))

    if n < m:
        m = n

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8
k, n = map(int,input().split())
a = input().split()
a.append(str(int(a[0]) + k))
distance = []
for i in range(n):
    preh, nexh = int(a[i]),int(a[i+1])
    d = abs(nexh - preh)
    distance.append(d)
b = abs(k - max(distance))
print("{}".format(b))

Submission status: Accepted

Solution code:
#traveling salesman
k, n = map(int, input().split())
homes = list(map(int, input().split()))
d_list = []
for i in range(n-1):
    dist = homes[i+1] - homes[i]
    d_list.append(dist)
dist_0 = homes[0] + (k - homes[n-1])
d_list.append(dist_0)
print(k - max(d_list))

Submission status: Accepted

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n, m = input_list()
  a = input_list()
  b = []
  for i in range(m):
    if i == 0:
      b.append((n - a[-1]) + a[0])
      continue
    b.append(a[i] - a[i-1])
  bm = max(b)
  print(sum(b) - bm)
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

input1 = [int(x) for x in input().split()]
lake = input1[0]
homes = input1[1]
input2 = [int(x) for x in input().split()]

result = 1000000
last = homes - 1
for x in range(homes):
    forward = 0
    backward = 0
    start = input2[x]

    # forward
    if x == 0:
        forward = input2[last] - start
    else:
        forward = (lake - start) + input2[x - 1]
    
    # backward
    if x == last:
        backward = (start) - input2[0]
    else:
        backward = (start) + lake - (input2[x + 1])

    #print("forward: {}, backward: {}".format(forward, backward))
    result = min(result, forward, backward)

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

###################################
len_array=[]
for i in range(n):
    if i==n-1:
        length=a[0]-a[i]+k
    else:
        length=a[i+1]-a[i]
    len_array.append(length)  
# len_array.index(max(len_array))
print(sum(len_array)-max(len_array))
###################################

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

A.append(K+A[0])
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()))
d = [0]*N
d[0] = K-A[N-1]+A[0]
for n in range(1,N):
  d[n] = A[n] - A[n-1]
print(K-max(d))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
A = [int(i) for i in 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())
house = list(map(int,input().split()))
house = house + house
min = house[N - 1] - house[0]
 
for i in range(N - 1):
  if K - (house[N + 1 + i] - house[i]) < min:
    min = K - (house[N + 1 + i] - house[i])
 
print(min)

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
a = [int(i) for i in input().split()]
max_dist = 0
for i in range(N):
    nxt = (i+1)%N
    dist = a[nxt] - a[i]
    if dist < 0:
        dist += K
        
    max_dist = max(max_dist,dist)
print(K-max_dist)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k+a[0])
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()))
a.append(k+a[0])
#print(a)
l = 0
for i in range (n):
    l = max(l,a[i+1]-a[i])
ans = k-l
print(ans)



'''
ans_a = 0
for i in range(len(a)-1):
    ans_a +=(a[i+1]-a[i])
for i in range(n):
    if a[i]>k/2:
        a[i] = k-a[i]

a.sort(reverse=True)
kai = a[0]+a[1]
#print(kai,ans_a)
print(min(kai,ans_a))
'''

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n = list(map(int, input().split()))
a = list(map(int, input().split()))
"""
ans = max(a) - min(a)
for i, num in enumerate(a):
    if num>(k/2):
        a[i] = num - k

print(min(ans,max(a) - min(a)))

min
abs(ai-bi)
abs(ai-(bi-k))
"""
def saitan(num1,num2,k):
    return min(abs(num1 - num2), abs(num1-(num2-k)))

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

print(k-bb)

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
 
A.append(K+A[0])
l=0
for i in range(len(A)-1):
    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()))

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

print(K-l)

Submission status: Accepted

Solution code:

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

k, n = inpl()
a = inpl()
a.append(k+a[0])
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()))
dif=[]
for i in range(len(A)-1):
  dif.append(A[i+1]-A[i])
dif.append(K+A[0]-A[N-1])
print(K-max(dif))

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [None] * len(a)
ans = 0
for i in range(len(a)):
    if i != len(a)-1:
        b[i] = a[i+1]-a[i]
    else:
        b[i] = k - a[i] + a[0]
ans = sum(b)-max(b)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

diff=-pow(10,9)
for i in range(1,k):
	diff=max(diff,l[i]-l[i-1])

diff=max(diff,n-l[k-1]+l[0])
print(n-diff)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def c_traveling_salesman_around_lake():
    K, N = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]  # ソート済
    A.append(A[0] + K)  # N 番の家と 1 番の家の間の距離も調べたい

    return K - max(A[i + 1] - A[i] for i in range(N))

print(c_traveling_salesman_around_lake())

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a,b = [int(i) for i in input().split()]
l  = [int(i) for i in input().split()]
l.append(l[0] + a)

l2 = [l[i+1] - l[i] for i in range(b)]

print(a - max(l2))

Submission status: Accepted

Solution code:
import sys
import random

def test(K, N, A):
    ans = []
    maxp = N-1
    minp = 0
    for i in range(len(A)):
        ans.append(A[maxp] - A[minp])
        A[i] += K
        maxp = (maxp + 1) % (N)
        minp = (minp + 1) % (N)
    print(min(ans))

if __name__ == "__main__":
    if "random" in sys.argv:
        K = int(random.random() * 20000)
        N = int(random.random() * 65536)
        A = sorted([int(random.random() * K) for _ in range(N)])
    else:
        K, N = [int(x) for x in input().split()]
        A = [int(x) for x in input().split()]
    test(K,N,A)
    pass

Submission status: Accepted

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

dct = sorted(A)
ans = [] * (N + 1)
new = K + dct[0] - dct[N-1]

ans.append(new)

for i in range(N-1):
    ans.append(dct[i+1] - dct[i])

print(sum(ans) - max(ans))

Submission status: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K, N = lr()
A = np.array(lr())
A = np.append(A, A[0] + K)
diff = np.diff(A)
answer = K - diff.max()
print(answer)

Submission status: Accepted

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

for i in range(n):
    up = (a[i]-a[i-1]) % k
    down = (a[i+1] - a[i]) % k
    ans = min(ans, k-up, k-down)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k,n,*a=map(int,open(0).read().split())
a+=a[0]+k,
print(min(k-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_length_max = K-A[N-1]+A[0]
for i in range (N-1): #0, 1, 2, ...N-2
    A_length_max=max(A_length_max, A[i+1]-A[i])
print(K-A_length_max)

Submission status: Accepted

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

# a.append(k)

diff = []

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

# print(diff)

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

num_list = []
for i in range(n):
  if i == 0:
    sub = l_si_i[0] + (k - l_si_i[n-1])
  else:
    sub = l_si_i[i] - l_si_i[i-1]
  
  num_list.append(sub)
  
#print(num_list)
#print(sum(num_list))

idx = num_list.index(max(num_list))
#print(idx)
num_list.pop(idx)
print(sum(num_list))

Submission status: Accepted

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

Submission status: Accepted

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

dists = []

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

Submission status: Accepted

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

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

print(K - dif)

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

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

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

    print(k - max(d))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def calc(k, kiten, a):
    koho1 = abs(kiten -a)
    koho2 = kiten + k - a
    koho3 = a + k - kiten

    return min(koho1, koho2, koho3)


def main():
    k, n = map(int, input().split())
    a_list = list(map(int, input().split()))

    max_kkn = a_list[0] + k - a_list[-1]
    for a, b in zip(a_list, a_list[1:]):
        max_kkn = max(max_kkn, b - a)

    print(k - max_kkn)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

k,n=map(int,input().split())
a=[int(x) for x in input().split()]
a.sort()
mini=a[n-1]-a[0]
for i in range(n):
    if 0<i<n-1:
        mini=min(mini,min(abs(a[i]-a[i+1]+k)%k,(a[(i-1)%n]-a[i]+k)%k))
    else:
        if i==0:
            mini=min(mini,a[i]-a[i+1]+k)
        else:
            mini=min(mini,a[i-1]-a[i]+k)
print(mini)

Submission status: Accepted

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

dist = [0] * (N + 1)

for i in range(N-1):
    dist[i] = a[i+1] - a[i]

dist[N] = K - a[N - 1] + a[0]

print(K - max(dist))

Submission status: Accepted

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

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

    As.sort()
    if len(As) == 1:
        print(0)
        return

    Ls = []  # 隣家間の距離をぶちこんでいく
    for i in range(1, len(As)):
        Ls.append(As[i]-As[i-1])
    Ls.append(As[0] + K-As[-1])

    Ls.sort()
    print(K-Ls[-1])


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
(n,m)=map(int,input().split())
a=input().split()
a=list(map(int,a))
l=[]
k=0
a=sorted(a)
for i in range(m-1):
    d=abs(a[i]-a[i+1])
    k=k+d
    l.append(d)
# k=k-d
c=n-k
l.append(c)
l=sorted(l)

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
x=list(map(int,input().split()))
xx=[]
for i in x:
    xx.append(i)
    xx.append(i-k)
y=sorted(xx)
z=[]
for i in range(n+1):
    z.append(y[i+n-1]-y[i])
print(min(z))

Submission status: Accepted

Solution code:
sel='C'

if sel=='A':
    S=input()
    if S[2]==S[3] and S[4]==S[5]:
        print('Yes')
    else:
        print('No')

if sel=='B':
    X=int(input())
    ans=0
    ans+=(X//500)*1000
    ans+=((X%500)//5)*5
    print(ans)

if sel=='C':
    K,N=map(int,input().split())
    A=[int(i) for i in input().split()]
    d=[A[0]+K-A[-1]]
    for i in range(N-1):
        d.append(A[i+1]-A[i])
    d.sort()
    print(K-d[-1])

# if sel=='D':

Submission status: Accepted

Solution code:
k,_,*a=map(int,open(0).read().split());a.append(k+a[0]);b=[y-x for x,y in zip(a,a[1:])];print(sum(b)-max(b))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
n = K + min(A)
A.append(n)
A = sorted(A)
l = len(A)
B = []
for i in range(l-1):
  b = abs(A[i+1] - A[i])
  B.append(b)
x = sum(B) - max(B)
print(x)

Submission status: Accepted

Solution code:
#C
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * n
max=0
for i in range(n):
    if i == n - 1:
        b[i] = k - a[i] + a[0]
    else:
        b[i] = a[i + 1] - a[i]
    if max < b[i]:
        max = b[i]
        ii=i
# print(b,max)
b.pop(ii)
# print(b)
print(sum(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
A = list(map(int,input().split()))
for n in range(N):
  A.append(-(K-A[n]))
A.sort()
res = K
for n in range(N):
  res = min(A[n+N-1] - A[n], res)
print(res)

Submission status: Accepted

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

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

ans = min(K-Big, max(A)-min(A))
print(ans)

Submission status: Accepted

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

ans = K-max_d
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def II():return int(input())
def MI():return map(int,input().split())
def LI():return list(MI())
def ok():print("Yes")
def ng():print("No");exit()

import numpy
k,n=MI()
a=LI()
a+=(a[0]+k),
a=numpy.diff(a)
print(k-max(a))

Submission status: Accepted

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

Submission status: Accepted

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


def calc(k, kiten, a):
    koho1 = abs(kiten -a)
    koho2 = kiten + k - a
    koho3 = a + k - kiten

    return min(koho1, koho2, koho3)


def main():
    k, n = map(int, input().split())
    a_list = list(map(int, input().split()))

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

    print(k - max_kkn)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

max = K-A[-1]+A[0]
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:
#通らない区間は高々1区間のみなので、区間の合計から最長距離を除いた距離が最短距離となる
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
#各座標間の距離の合計
total = 0
#各座標間の距離
dist = []

#各座標間の距離を求めつつ、合計を算出
for i in range(n):
    if i == n - 1:
        #座標0をまたいだ場合を考慮する
        temp = (k - a[i]) + a[0]
    else:
        temp = a[i + 1] - a[i]
    total += temp
    dist.append(temp)

ans = total - max(dist)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

d.append(K - A[N-1] + A[0])
mx = max(mx, K - A[N-1] + A[0])


# print(mx)
# print(d)
ans = K - mx
print(ans)