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()))
dist = sorted([a[i+1] - a[i] for i in range(n-1)] + [k-a[-1]+a[0]])
print(sum(dist[:-1]))

Submission status: Accepted

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

a = list(map(int,input().split()))
ans = 0
gr = 0
for i in range(1,n):
    if(a[i] - a[i-1] > gr):
        gr = a[i] - a[i-1]

if((k - a[n-1]) + a[0] > gr):
    gr = (k - a[n-1]) + a[0]
# print(gr)

print(k - gr)

Submission status: Accepted

Solution code:

k, n = (int(x) for x in input().split())
arr = [int(x) for x in input().split()]

def dist(x, y):
    v = abs(x -y)
    return min(v, k-v)

arr.sort()

if n == 2:
    sol = dist(*arr)
else:
    sol = k - max(dist(arr[i-1], arr[i]) for i in range(n))

print(sol)

Submission status: Accepted

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

A.append(A[0] + K)

ma = 0

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

print(int(K-ma))

Submission status: Accepted

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

L2 = []

for i in range(n-1):
    L2.append(List[i+1] - List[i])
L2.append(k - List[-1] + List[0])
print(k - max(L2))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

diff = np.diff(Al)
diff = np.append(diff, K-Al[-1]+Al[0])

ans = K - diff.max()
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np
a,b=map(int,input().split())
#print(a)
n_l=list(map(int,input().split()))
n_l.append(n_l[0]+a)
n_l=list(sorted(n_l))
# 一次元配列を作成
arr = np.array(n_l)

# 隣り合う要素同士の差分を計算
diff = np.diff(arr)
#print(diff)
#print(max(diff))
print(a-max(diff))

Submission status: Accepted

Solution code:
k,n=(int(x) for x in input().split())
a_list=[int(x) for x in input().split()]
b_list=[]

first_element=k-a_list[n-1]+a_list[0]
b_list.append(first_element)
for b in range(n-1):
    b_list.append(a_list[b+1] - a_list[b])

max_value = max(b_list)

print(k-max_value)

Submission status: Accepted

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

Submission status: Accepted

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

maxV = 0

for i in range(1, len(a)):
    d.append(abs(a[i] - a[i-1]))

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

# print(d)

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

Submission status: Accepted

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

Submission status: Accepted

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

nums = [K - As[-1] + As[0]]
for idx in range(len(As)-1):
    nums.append(As[idx+1] - As[idx])

print(sum(nums) - max(nums))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
As = list(map(int, input().split()))
Ds = [ai2-ai1 for ai1,ai2 in zip(As, As[1:]+[As[0]+K])]
print(sum(Ds)-max(Ds))

Submission status: Accepted

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

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

a = sum(d) - max(d)
print(a)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff = []
A.sort()
for idx in range(len(A)):
    if idx == len(A)-1:
        tmp = abs(A[idx] - A[0])
        if tmp > K/2:
            tmp = K - tmp
        diff.append(tmp)
    else:
        tmp = abs(A[idx+1] - A[idx])
        if tmp > K/2:
            tmp = K - tmp
        diff.append(tmp)

print(K - max(diff))

Submission status: Accepted

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

prev = A[0]
dist = K - A[N-1] + A[0]
for i in range(1,N):
#  print('dist=', dist)
#  print('A[i]=', A[i])
#  print('prev=', prev)
#  print('A[i]-prev=', A[i] - prev)
  if A[i] - prev > dist:
    dist = A[i] - prev
  prev = A[i]

ans = K - dist
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
A.sort()
max_dis = 0
dis = 0
for i in range(len(A)):
    if i == len(A)-1:
        temp = (K-A[i])+A[0]
        if temp > max_dis:
            max_dis = temp
        dis += temp
    else:
        if A[i+1]-A[i] > max_dis:
            max_dis = A[i+1]-A[i]
        dis += A[i+1]-A[i]

print(dis-max_dis)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
points = list(map(int, input().split()))
max_index = -1
max_value = -1

for index in range(1, N):
  d = points[index] - points[index-1] 
  if d > max_value:
    max_index = index
    max_value = d

start = K - points[max_index]
end   = points[max_index-1]

l = start + end
plane_l = points[-1] - points[0]
if l <= plane_l:
  print(l)
else:
  print(plane_l)

Submission status: Accepted

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

for i in range(n):
    if i == 0 or i==n-1:
        continue
    else:
        ans = max(ans, abs(a[i]-a[i-1]), abs(a[i]-a[i+1]))
print(k-ans)

Submission status: Accepted

Solution code:
import math

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

path = 0
lastPosition = 0

for i in range(KN[1]):
    if (i == 0):
        path = KN[0] + A[0] - A[KN[1] - 1]
        lastPosition = A[0]
    else:
        currentPosition = A[i]
        if (path < currentPosition - lastPosition):
            path = currentPosition - lastPosition
        lastPosition = currentPosition

print(KN[0] - path)

Submission status: Accepted

Solution code:
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(a[0])
m = 0
for i in range(0, n):
  d = abs(a[i+1] - a[i])
  x = min(d, k-d)
  if(x>m):
    m = x
    i1 = i
f = 0
for i in range(0, n):
  d = abs(a[i+1] - a[i])
  x = min(d, k-d)
  if(i!=i1):
    f+=x
print(f)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=a*2
s=0
t=10**9
def dis(a,b,k):
    if b-a<0:
        return b-a+k
    else:
        return b-a
for i in range(n):
    s=min(k-dis(a[i],a[i+1],k),k-dis(a[i-1+n],a[i],k))
    t=min(s,t)
print(t)

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

max_length = max(length)
ans = k- max_length
print(ans)

Submission status: Accepted

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

def one_int():
    return int(input())

def one_str():
    return input()

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

K, N = input_int()

A = many_int()

b_list=[]
n_list=[]
for i,a in enumerate(A):
    nexts = 0 if i+1==N else i+1
    back  = N-1 if i==0 else i-1
    
    if A[back] > a:
        back = (a+K)-A[back]
    else:
        back = a-A[back]
    
    if A[nexts] < a:
        nexts = (A[nexts]+K)-a
    else:
        nexts = A[nexts]-a
    
    
    b_list.append(back)
    n_list.append(nexts)

print(sum(b_list)-max(b_list))

Submission status: Accepted

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

An_1 = [A for A in An if A <= K/2]
An_2 = [A-K for A in An if A > K/2]
An_new = An_1 + An_2

ans = 0
for i in range(len(An)-1):
    ans = max(ans, An[i+1]-An[i])
    
ans = max(ans, An_new[0] - An_new[-1])

print(K-ans)

Submission status: Accepted

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

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

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

sa.sort()

print(sum(sa[:N - 1]))

Submission status: Accepted

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

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

Adist.pop(Adist.index(max(Adist)))

print(sum(Adist))

Submission status: Accepted

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

Submission status: Accepted

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

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

print (k-max(b))

Submission status: Accepted

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

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

c = b.remove(max(b))

print(sum(b))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
fst = None
lst = None
pre = None
mx = 0
for a in sorted(map(int, input().split())):
    lst = a
    if fst is None:
        fst = pre = a
        continue
    d = a - pre
    if d > mx:
        mx = d
    pre = a
d = (K - lst) + fst
if d > mx:
    mx = d
print(K - mx)

Submission status: Accepted

Solution code:
from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 300010
MOD = 10**9+7
spf = [i for i in range(MAXN)]
spf[0]=spf[1] = -1
def sieve():
    for i in range(2,MAXN,2):
        spf[i] = 2
    for i in range(3,int(MAXN**0.5)+1):
        if spf[i]==i:
            for j in range(i*i,MAXN,i):
                if spf[j]==j:
                    spf[j]=i
def fib(n,m):
    if n == 0:
        return [0, 1]
    else:
        a, b = fib(n // 2)
        c = ((a%m) * ((b%m) * 2 - (a%m)))%m
        d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m
        if n % 2 == 0:
            return [c, d]
        else:
            return [d, c + d]

def charIN(x= ' '):
    return(sys.stdin.readline().strip().split(x))

def arrIN(x = ' '):
    return list(map(int,sys.stdin.readline().strip().split(x)))

def ncr(n,r):
    num=den=1
    for i in range(r):
        num = (num*(n-i))%MOD
        den = (den*(i+1))%MOD

    return (num*(pow(den,MOD-2,MOD)))%MOD

def flush():
    return sys.stdout.flush()

'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
k,n = arrIN()
a = arrIN()
cd = 0
mx = -1
ad = 0
for i in range(1,n):
    cd+=a[i]-a[i-1]
    mx = max(mx,a[i]-a[i-1])
cd+=k-a[-1]+a[0]
mx = max(mx,k-a[-1]+a[0])
cd-=mx
print(cd)

Submission status: Accepted

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

p = k
for i in range(n):
    x = a[i-1] - a[i]
    if x < 0:
        x+=k
    p = min(p,x)

print(p)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# K, N = 20, 3
# A = [0,5,15]

sa=[A[i+1]-A[i] if (i!=N-1) else (K-A[-1]+A[0]) for i in range(N) ]
answer=sum(sa) - max(sa)
print(answer)

Submission status: Accepted

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

#最大の区間を求める

ma = k - (a[-1] - a[0])

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

print(k-ma)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc160/tasks/abc160_c
# -*- coding: utf-8 -*-
K,N = map(int,input().split())
A = input().split()
A = [int(i) for i in A]
saityou = K-A[-1]+A[0]
for i in range(1,len(A)):
  if saityou < A[i]-A[i-1]:
    saityou  = A[i]-A[i-1]
print(K-saityou)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

pre = A[len(A)-1] 
m = x-pre+A[0]
s = m
#print ("最初",pre,m,s)

pre = A[0]

for a in A[1:]:
    m = max([a - pre , m])
    s = s +  a - pre
    
    #print (a,pre,m,s)
    pre = a
    
    
print(s-m)

Submission status: Accepted

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

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

distSum = distSum - maxA
print(distSum)

Submission status: Accepted

Solution code:
def main():
    k,n = tuple([int(t)for t in input().split()])
    a = [int(t)for t in input().split()]

    l = a[0]
    dist = []
    for a_i in a[1:]:
        dist.append(a_i-l)
        l = a_i
    dist.append(k-a[-1]+a[0])

    print(k-max(dist))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    K = int(s[0])
    N = int(s[1])
    s = input().rstrip().split(' ')
    A = [int(elem) for elem in s]
    B = [[0,0] for elem in s]
    for i in range(len(A)-1):
        B[i][0] = abs(A[i]-A[i+1])
        B[i+1][1] = abs(A[i]-A[i+1])

    B[len(A)-1][0] = K-abs(A[len(A)-1]-A[0])
    B[0][1] = K-abs(A[len(A)-1]-A[0])

    ans = float('inf')

    for elem in B:
        if(ans > K-elem[0]):
            ans = K-elem[0]
        if(ans > K-elem[1]):
            ans = K-elem[1]

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

K, N = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in input().split()]
ans1 = 100000000000000000
for i in range(N-1):
    if i == 0:
        ans1 = min(a[N-1]-a[0], K-(a[1]-a[0]))
        continue
    # print(a[i]+K-a[i+1])
    # print(K-(a[i]-a[i-1]))
    # print()
    ans = min(a[i]+K-a[i+1], K-(a[i]-a[i-1]))
    if ans1 > ans:
        ans1 = ans
print(ans1)

Submission status: Accepted

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

Submission status: Accepted

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

ans = float("inf")
for i in range(N):
    if i == 0:
        ans = min(ans, K - abs(K + AList[i] - AList[N-1]),
                  K - abs(AList[i] - AList[i + 1]))
    elif i == N - 1:
        ans = min(ans, K - abs(AList[i] - AList[i - 1]),
                  AList[0] + AList[i])
    else:
        ans = min(ans, K - abs(AList[i] - AList[i - 1]),
                  K - abs(AList[i] - AList[i + 1]))

print(ans)

Submission status: Accepted

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

Bn = [0] * N
for i in range(N-1):
    Bn[i] = An[i+1] - An[i]

Bn[-1] = K - An[-1] + An[0]
print(sum(Bn) - max(Bn))

Submission status: Accepted

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

ans = 0
for i in range(n):
    if i != index:
        ans += ax[i+1]-ax[i]


print(ans)

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):
    ans=min(ans,K-A[i]+A[i-1])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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



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

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

Submission status: Accepted

Solution code:
k,n = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
from collections import deque
d = deque()
d.append(k-l[n-1]+l[0])
for i in range(n-1):
  d.append(l[i+1]-l[i])
print(k-max(d))

Submission status: Accepted

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

a.append(k + a[0])

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

print(sum(b) - max(b))

Submission status: Accepted

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

Submission status: Accepted

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

sorted_distance = sorted(A)
dists = []
for i in range(N - 1):
    dists.append(sorted_distance[i + 1] - sorted_distance[i])
dists.append(K + sorted_distance[0] - sorted_distance[-1])
max_dist = max(dists)
max_dist_id = dists.index(max_dist)

if max_dist_id == len(dists) - 1:
    total_dist = sum(dists[:-1])
else:
    total_dist = sum(dists[max_dist_id+ 1:]) + sum(dists[:max_dist_id])

print(total_dist)

Submission status: Accepted

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

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

B=[]
i=0
while i<=N-2:
    B.append(A[i+1]-A[i])
    i=i+1
B=sorted(B)
print(K-max(B1,B[-1]))

Submission status: Accepted

Solution code:
def a():
    S = input()

    if S[3] == S[2] and S[4] == S[5]:
        print("Yes")
    else:
        print("No")


def b():
    X = int(input())
    y_500 = (X // 500) * 1000
    y_5 = ((X - (y_500 / 2)) // 5) * 5
    print(int(y_500 + y_5))


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

    max_A_d = -1
    for i in range(len(A)):
        A_d = 0
        if i == 0:
            A_d = K - (A[-1] - A[0])
        else:
            A_d = (A[i] - A[i - 1])
        if max_A_d < A_d:
            max_A_d = A_d
    d = K - max_A_d
    print(d)


def d():
    print()


def e():
    print()


c()

Submission status: Accepted

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

Submission status: Accepted

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

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()))
a.insert(0,0)
a.append(k)
tosa = []
for i in range(len(a)-1):
    tosa.append(a[i+1] - a[i])
tmp = tosa[0] + tosa[-1]
tosa[0] = tmp
del tosa[-1]
tosa.sort()
result = sum(tosa[:n-1])
print(result)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

A = np.array(A)
B = A[1:] - A[:-1]
B = np.insert(B, N-1, K+A[0]-A[N-1])

B = B.tolist()
max_A = max(B)
max_idx = B.index(max_A)

#print(B)
#print(max_A, max_idx)

print(K-max_A)

Submission status: Accepted

Solution code:


km, houseNum = map(int, input().split())

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

maxDistance = 0

# まずリスト最初と最後の間の距離を求める
maxDistance = lst[0] + (km - lst[-1])

prevPos = lst[0]
for house in lst[1:]:
    maxDistance = max(maxDistance, house - prevPos)
    prevPos = house

print(km - maxDistance)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    K, N = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    last_dist = K-A_list[-1] + A_list[0]
    res = A_list[-1] - A_list[0]

    temp_dist = res - (A_list[1] - A_list[0]) + last_dist
    res = min(res, temp_dist)

    for i in range(2,N):
        temp_dist = temp_dist - (A_list[i] - A_list[i-1]) + (A_list[i-1] - A_list[i-2])
        res = min(res, temp_dist)

    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

x = 0
index = 0

for i in range(n-1):
    if x < a[i+1] - a[i]:
        x = a[i+1] - a[i]
        index = i

if x < k-a[-1]+a[0]:
    x = k-a[-1]+a[0]

print(k - x)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
import fractions
import copy
import bisect
#import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
from operator import itemgetter
from sys import stdin
#input = sys.stdin.readline#こっちの方が入力が早いが使える時に使っていこう
from operator import mul
from functools import reduce
from collections import Counter


N=3
M=3
i=0
j=0
k=0
n=3
r=1
a=[0]*5
diff=[]
newlist=[]
K,N=map(int, input().split())  #int style 複数数値入力　「A B」みたいなスペース空いた入力の
number_list=list(map(int, input().split(" ")))
newlist=sorted(number_list)
for i in range(N-1):
    dis=newlist[i+1]-newlist[i]
    diff.append(dis)
diff.append(newlist[0]+K-newlist[N-1])
print(K-max(diff))
#n=int(input())
#r=int(input())
#print(M)
#A=int(input())
#B=int(input())
#print(N)
"1行1つの整数を入力を取得し、整数と取得する"


#number_list=list(map(int, input().split(" ")))#数字の時
#print(number_list)
"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#メモ
for i in number_list:#こっちの方がrage使うより早いらしい
    print(number_list[i-1])#

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''
#array=[input().split()for i in range(N)]
#print(type(array[0][0]))
#print(array)
"正方行列にstr型の値を入力"
'''

#brray=[list(map(int, input().split(" ")))for i in range(N)]
#print(type(brray[0][0]))
#print(brray)
'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"
#以下に別解を記載
#N, M = [int(i) for i in input().split()]
'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''




#s=input()
#a=[int(i) for i in s]
#print(a[0])
#print([a])
#単数値．桁ごとに分割したい．入力と出力は以下の通り
#イメージとして1桁ごとにリストに値を入れているかんじ

'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()#str style 何個でもいけることが多い
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''


#a= stdin.readline().rstrip()
#print(a.upper())
"aという変数に入っているものを大文字にして出力"


#a,b=map(int, input().split())  #int style 複数数値入力　「A B」みたいなスペース空いた入力のとき

#なんかうまく入力されるけど
#a=[[int(i) for i in 1.strip()]for 1 in sys.stdin]
#a = [[int(c) for c in l.strip()] for l in sys.stdin]]
#print(a)
#複数行の数値を入力して正方行列を作成


##############################################################################################
##############################################################################################
#under this line explains example calculation


#nCr combination
'''
def cmb(n,r):
    #When n < r , this function isn't valid
    r= min(n-r,r)
    #print(n,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    #flochart mul(n,n-1)=x
    #next     mul(x,n-2)........(n-r+1,n-r)
    #mul read a,b and returns a*b

    under = reduce(mul, range(1, r+1))
    #print(over, under)
    #reduce is applied mul(1,2)=2
    #next   mul(2,3)=6
    #next   mul(6,4)=4.........last(r!,r+1)=r+1!

    return over // under
    #// is integer divide
    #calc example 5C2
    #over=5*4*3
    #under=3*2*1
a = cmb(n, r)
#print(a)
'''

'''
#集計
#example
#a=[2,2,2,3,4,3,1,2,1,3,1,2,1,2,2,1,2,1]
#a=Counter(a)
for i in a.most_common(n):print(i)
#most_common()メソッドは、出現回数が多い要素順にCounterオブジェクトを並び替えます。
#引数にint型の数字nを設定した場合は、出現回数が高い上位ｎ個の要素を返します。
#何も設定しなければ、コンテナ型にあるすべての要素を出現回数の順番に並び替えたタプル型オブジェクトを返します。
#out put
#(2, 8)
 #(1, 6)
#(3, 3)
#(4, 1)
'''


#二部探索(binary search)
#A = [1, 2, 3, 3, 3, 4, 4, 6, 6, 6, 6]
#print(A)
#index = bisect.bisect_left(A, 5) # 7 最も左(前)の挿入箇所が返ってきている
#A.insert(index, 5)
#print(index)
#print(A)
'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''





########################################################################
########################################################################
#b2=a[:]  #1次元のときはコピーはこれで良い
#print(b2)

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b3=copy.deepcopy(a)  #2次元配列はこうコピーする
#print(b3)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    a = A[-1] - A[0]
    
    ma = 0
    for i in range(N-1):
        ma = max(ma, A[i+1]-A[i])
    
    return min(a, K-ma)

print(solve())

Submission status: Accepted

Solution code:
#At coder 2020/3/27 C

# スペース区切りの整数の入力
K, N = map(int, input().split())
A = list(map(int, input().split())) 

mlen=K-A[N-1]+A[0]
for ii in range(N-1):
    len2=A[ii+1]-A[ii]
    mlen=max(mlen,len2)
ans=K-mlen


print(ans)

Submission status: Accepted

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

    max_interval = 0
    for i in range(N-1):
        tmp_interval = A_list[i + 1] - A_list[i]
        if max_interval < tmp_interval:
            max_interval = tmp_interval

    first_interval = A_list[0] + (K - A_list[N-1])

    if max_interval < first_interval:
        max_interval = first_interval

    ans = K - max_interval

    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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