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:
# ABC160_C
k,n=map(int,input().split())
a=list(map(int,input().split()))
# mid=(a[0]+a[-1])/2
d=[]
for i in range(n-1):
    d.append(a[i+1]-a[i])
# d.append(k-a[-1])
# d=[k-a[-1]]+d

# print(d)
ans=10**10
d=[k-a[-1]+a[0]]+d
d+=[k-a[-1]+a[0]]
# print(d)
for i in range(1,n):
    t=min(k-d[i-1],k-d[i+1])
    ans=min(t,ans)
print(ans)

Submission status: Accepted

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

answer = []

for i,home in enumerate(A):
    if home < A[i - 1]:
        answer.append(home - A[i - 1] + KN[0])
    else:
        answer.append(home - A[i - 1])

print(KN[0] - max(answer))

Submission status: Accepted

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

Submission status: Accepted

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

# print(k)
# print(n)
# print(a)

minmin = k
for i in range(n):
    start = i
    end = -1 + i
    if i == 0:
        end = n - 1
    # print("{} -> {}".format(start, end))
    
    result = k
    if i == 0:
        result = a[end] - a[start]
        # print(result)
    else:
        result = (a[end] + k) - a[start]
        # print(result)
        
    if result < minmin:
        minmin = result
        
    # print(minmin)
        
print(minmin)

Submission status: Accepted

Solution code:
K,N=input().split()
A=input().split()
n=0
m=1
S=list()
for i in range(int(N)-1):
  S.append(int(A[n+1])-int(A[n]))
  n=n+1
S.append(int(A[0])+(int(K)-int(A[-1])))

ans=int(K)-int(max(S))
print(ans)

Submission status: Accepted

Solution code:
K, N = (int(x) for x in input().split())
A = [int(i) for i in input().split()]
A.append(K + A[0])
D = [0] * N
for i in range(N):
    D[i] = A[i+1] - A[i]

print(K - max(D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=input().split(' ')
b=input().split(' ')
c=0
for i in range(int(a[1])):
    if i != int(a[1])-1:
        if int(b[i+1]) - int(b[i]) > c:
            c = int(b[i+1]) - int(b[i])
    else:
        if int(a[0]) - int(b[-1])+ int(b[0]) > c:
            c = int(a[0]) - int(b[-1])+ int(b[0])
print(int(a[0])-c)

Submission status: Accepted

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

count = A[-1]-A[0]
def double(n):
     return n+K

B = map(double, A)
B = list(B)

i=0
while i<N-1:
    if B[i]-A[i+1] < count:
        count = B[i]-A[i+1]
    i+=1

print(count)

Submission status: Accepted

Solution code:
import numpy as np
 
tmp =list(map(int, input().split()))
num=list(map(int, input().split()))

num.append(num[0]+tmp[0])

print(np.diff(num).sum()-np.diff(num).max())

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
A = list(map(int,input().split()))
result = 1000001
S = [0]
s = 0

for n,i in enumerate(A):
    if(n <= len(A)-2):
        s = A[n+1] - A[n]
        S.append(s)
S.append(k-A[len(A)-1]+A[0])
#print(S)
tmp = 0
for s in range(len(S)):
    tmp += S[s]
    S[s] = tmp
#print(S)


for i in range(len(S)):
    if(i == 0):
        ans = S[len(S)-2] - S[0]
        if(result>ans):
            #print(result,'1')
            result = ans
    else:
        ans = S[len(S)-1]- S[i] + S[i-1] - S[0]
        if(result>ans):
            #print(result,'2')
            result = ans
A.reverse()
S = [0]
for n,i in enumerate(A):
    if(n <= len(A)-2):
        s = k - A[n]
        S.append(s)
S.append(k)
#print(S)

for i in A:
    s += i
    S.append(s)
for i in range(len(S)):
    if(i == 0):
        S[len(S)-2] - S[0]
        if(result>ans):
            #print(result,'3')
            result = ans
    else:
        ans = S[len(S)-1]- S[i] + S[i-1] - S[0]
        if(result>ans):
            #print(result,'4')
            result = ans
print(result)

Submission status: Accepted

Solution code:
k,n = list(map(int,input().split()))
arr = list(map(int,input().split()))
x = []
val = arr[-1] - arr[0]
if val<= k/2:
	x.append(val)
else:
	x.append(k-val)
for i in range(1,n):
	val = arr[i] - arr[i-1]
	if val <= (k/2):
		x.append(val)
	else:
		x.append(k-val)
print(k-max(x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

K, N = map(int, input().split())
A = list(map(int, input().split()))
lst = []
for i in range(1, N-1):
    diff_1 = abs(A[i + 1] - A[i])
    lst.append(diff_1)

lst.append(K + A[0] - A[N-1])
print(K - max(lst))

Submission status: Accepted

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

max_a = max(A)
min_a = min(A)
diff_min = max_a - min_a

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

print(diff_min)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K, N = input().split()
K = int(K)
N = int(N)
houses = list(map(int, input().split()))
first = True
distance = 0
for i in range(N):
  firstHouse = houses[i]
  lastHouse = houses[i - 1]
  if lastHouse > firstHouse:
    distance = lastHouse - firstHouse
  else:
    distance = (K - firstHouse) + lastHouse
  if first or distance < minDistance:
    minDistance = distance
    first = False
print(minDistance)

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

Submission status: Accepted

Solution code:

K, N = map(int, input().split())
A = list(map(int, input().split()))
lst = []
for i in range(1, N-1):
    diff_1 = abs(A[i + 1] - A[i])
    lst.append(diff_1)
    diff_2 = abs(A[i] - A[i - 1])
    lst.append(diff_2)

lst.append
lst.append(K + A[0] - A[N-1])
print(K - max(lst))

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*(n-1)
b= [x - y for (x, y) in zip(a[1:],a[:-1])]
c=0
c=k+a[0]-a[n-1]
m=max(b)
M=max(m,c)
print(k-M)

Submission status: Accepted

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

dist = k
for i in range(len(a)-1):
    d = k - (a[i+1]-a[i])
    if d < dist:
        dist = d
if max(a)-min(a) < dist:
    print(max(a)-min(a))
else:
    print(dist)

Submission status: Accepted

Solution code:
K,N  =list(map(int,input().split()))
A = list(map(int,input().split()))
C = list()
ans = int()
B = list(sorted(A))
for i in range(len(B)-1):
    C.append(B[i+1]-B[i])
C.append((K-B[-1])+(B[0]))
C = sorted(C)
for i in range(N-1):
    ans += C[i]
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(len(A) - 1):
    # print(i)
    tmp = A[i] - (A[i + 1] - K)
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
kn = input().split()
a = input().split()

al = [int(i) for i in a]
k = int(kn[0])
n = kn[1]

ml = k - abs(al[len(al)-1] - al[0])


for i in range(len(al)-1):
	ml = max(ml,abs(al[i]-(al[i+1])))


print(k-ml)

Submission status: Accepted

Solution code:
#ANS
temp=input()
temp2=temp.split(" ")
K=int(temp2[0])
N=int(temp2[1])

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

A.append(K+A[0])
#if K=A[N-1]:
#  A.append(K)
#else:
#  A.append()


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

maxind=D.index(max(D))


sumd=0


for i in range(N):
  if(i!=maxind):
    sumd+=D[i]


print(sumd)

Submission status: Accepted

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

max_interval = 0
index = None
distance = 0
for i in range(N):
    if i == N-1:
        interval = K - As[i] + As[0]
    else:
        interval = abs(As[i+1] - As[i])
    if interval > max_interval:
        max_interval = interval
        index = i
        
if index == N-1:
    distance = As[index] - As[0]
else:
    distance = As[index] + K - As[index+1]
print(distance)

Submission status: Accepted

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

for i in range(len(a)-1):
    l = abs(a[i] - a[i+1])
    kyori.append(l)
kyori.append(k - abs(a[0]-a[n-1]))

saitann = k - max(kyori)
print(saitann)

Submission status: Accepted

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

Submission status: Accepted

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

A.append(K + A[0])
# print(A)
A_sub = []

for i in range(len(A)-1):
    A_sub.append(abs(A[i] - A[i + 1]))
# print(A_sub)
print(sum(A_sub) - max(A_sub))

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
k = 0
for i in A:
  k += 1
  if k != N:
    B[k-1]=A[k]-A[k-1]
  else:
    B[N-1] = A[0] + K - A[N-1]

print(K-max(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

if __name__ == "__main__":
    K, N = MI()
    A = LI()
    a = []
    for i in range(N):
        if i != N-1:
            a.append(abs(A[i]-A[i+1]))
        else:
            a.append(abs(A[i]-K-A[0]))
    a.remove(max(a))
    print(sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

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

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

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

for i in range(n-1):
    b = a[i+1]-a[i]
    if(maxim<b):
        maxim = b
        
print(k-maxim)

Submission status: Accepted

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

Submission status: Accepted

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

m = n

max_dist = 0
while n >= 1:
    if max_dist < li[n -1] - li[n -2]:
        max_dist = li[n -1] - li[n -2]
    n -= 1

x = 0
while m >= 2:
    x += li[m -1] - li[m -2]
    m -= 1

print(min([k - max_dist, x]))

Submission status: Accepted

Solution code:
def find_d(a,k,n):
    d = a[0] + (k - a[n-1])
    point = 0
    
    for i in range(n-1):
        tmp = a[i+1] - a[i]
        if d<tmp:
            d = tmp
            point = i+1
    return d

if __name__ == "__main__":
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    d = find_d(a,k,n)
    
    print(k-d)

Submission status: Accepted

Solution code:
input_line = input()
tmpa,tmpb = input_line.split()
K = int(tmpa)
N = int(tmpb)
listin = input().split()
int_s = [int(s) for s in listin]
Distance = []

for i in range(N):
  if i != N-1:
    tmp = int_s[i+1] - int_s[i]
    Distance.append(tmp)
  else:
    tmp = (K - int_s[i])+int_s[0]
    Distance.append(tmp)

print(sum(Distance)-max(Distance))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = [int(x) for x in input().split()]
a_ = [x - a[0] for x in a] + [k]
d = [y - x for x, y in zip(a_, a_[1:])]
print(k - max(d))

Submission status: Accepted

Solution code:

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

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

b = K -abs(A[0]-A[N-1])
L.append(b)

ans = K - max(L)

print(ans)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
home = input()
home = home.split()
for i in range(len(home)):
    home[i] = int(home[i])
d=[]
for i in range(len(home)):
    if i == len(home)-1:
        d.append(home[0]+k-home[i])
        break
    d.append(home[i+1]-home[i])
Max = max(d)
leng=len(home)
count=0
for i in range(len(d)):
    if d[i] == Max:
        break
d=0
while True:
    
    if count==leng-1:
        break
    j=i-1
    if i==0:
        d+=abs(home[leng-1]-(home[0]+k))
        count+=1
    else:
        d += abs(home[i]-home[i-1])
        count+=1
    i-=1
    if i == -1:
        i=leng-1
print(d)

Submission status: Accepted

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

min1 =10**10
#min1 = k-L[n-1]+L[0]
for i in range(n):
   min1 = min(min1,(k-li[i]))
   #print(min1)
print(min1)

Submission status: Accepted

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

Submission status: Accepted

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

#output
answer = K

for i in range(1, N):
    temp =  K + A[i-1] - A[i]
    answer = min(temp, answer)

answer = min(answer, A[N-1]-A[0])

print(answer)

Submission status: Accepted

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    k, n = map(int, args[0].split())
    A = list(map(int, args[1].split()))

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

    return str(k-max(D))


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

Solution code:
a = input().split(' ')
a = [int(s) for s in a]

b = input().split(' ')
b = [int(s) for s in b]

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

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

print(K - max(B))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

val = input().split(' ')
k = int(val[0])
n = int(val[1])

alist = sorted([int(a) for a in input().split(' ')])

a2list = list(map(lambda a: a + k, alist))
alist.extend(a2list[:-1])
adiff = np.diff(alist)
before = sum(adiff[0: n-1])
result = before
for i in range(1, n):
    tmp = before - adiff[i - 1] + adiff[i - 2 + n]
    before = tmp
    if tmp < result:
        result = tmp
print(result)

Submission status: Accepted

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

for i in range(n):
    if(i == 0):
        f = l[i-1] - l[0]
        s = (k - l[i+1]) + l[i]
    elif(i == n-1):
        f = l[i] - l[0]
        s = (k - l[i]) + l[i-1]
    else:
        f = l[i] + (k - l[i+1])
        s = (k - l[i]) + l[i-1]
    
    if(m > min(f,s)):
        m = min(f,s)

print(m)

Submission status: Accepted

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

a_list_sorted = sorted(a_list)

max_distance = 0
for i in range(1, len(a_list_sorted)):
    tmp_max_distance = a_list_sorted[i] - a_list_sorted[i-1]
    if max_distance < tmp_max_distance:
        max_distance = tmp_max_distance

tmp_max_distance_last = a_list_sorted[0] + (k - a_list_sorted[-1])
if max_distance < tmp_max_distance_last:
    max_distance = tmp_max_distance_last

print(k-max_distance)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

S =  list(map(int,input().split()))
K = S[0]
N = S[1]
P =  list(map(int,input().split()))
R = []
for i in range(len(P)):
    if i >= 1:
        X = P[i]-P[i-1]
        R.append(X)
        if i == N-1:
            X = K + P[0] - P[i]
            R.append(X)
B = max(R)
answer = K-B        
print(answer)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict, Counter

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

Submission status: Accepted

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

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

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

print('{}'.format(k-max_sub))

Submission status: Accepted

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

# 距離間リスト
d_li = []
for i in range(N-1):
	d_li.append(A_li[i+1]-A_li[i])

d_li.append(K+A_li[0] - A_li[N-1])


allsum = sum(d_li)
ans = allsum
for d in d_li:
	ans = min(ans,allsum-d)

print(ans)

Submission status: Accepted

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

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

sa.append(k-max(a)+min(a))
ans = sum(sa)-max(sa)
# print(sa)
# print(k)
# print(sum(sa))
print(ans)

Submission status: Accepted

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

max = 0
result = 0
j = 0
k = 0
#print(A)
for x in range(1, len(A)):
  #print(x)
  #print(A[x]-A[x-1])
  #print(A[x-1])
  #print(A_next)
  A_next.append(abs(A[x] - A[x-1]))
  if max < A_next[j]:
    max = A_next[j]
    #print(max)
    k = j
  j += 1
#print(k)
#print(A_next)
for x in range(len(A_next)):
  if x == k:
    pass
  else:
    result += A_next[x]
print(result)

Submission status: Accepted

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

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

if a[0] == 0:
    li.append(k - a[n - 1])
    li.sort()
    li.pop(-1)
else:
    li.append(k - a[n - 1] + a[0])
    li.sort()
    li.pop(-1)
print(sum(li))

Submission status: Accepted

Solution code:
import sys

K,N = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))
# K = 20
# N = 3
# A =[0, 5, 15]
# A =[5, 10, 15]

distance = []
# 家間距離
for i,a in enumerate(A):
    if i == len(A) - 1:
        distance.append(K - A[i] + A[0])
    else:
        distance.append(A[i+1] - A[i])

# print(distance)
print(sum(distance) - max(distance))

Submission status: Accepted

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

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

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

print(k-max(b))

Submission status: Accepted

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

sub_a = []

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

max_sub = max(sub_a)

print(K - max_sub)

Submission status: Accepted

Solution code:
ddd = [input() for i in range(2)]
eee = ddd[0].split()
fff = ddd[1].split()
longest_distance = int(eee[0]) + int(fff[0]) - int(fff[-1])
for i in range(len(fff) - 1):
    distance = int(fff[i+1]) - int(fff[i])
    if longest_distance < distance:
        longest_distance = distance
shortest_road = int(eee[0]) - longest_distance
print(shortest_road)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 19 22:37:09 2019

@author: lucifer
"""
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
max=k+a[0]-a[n-1]
sum=max
for i in range(n-1):
    if a[i+1]-a[i]>max:
        max=a[i+1]-a[i]
    sum=sum+a[i+1]-a[i]
sum=sum-max
print(sum)

Submission status: Accepted

Solution code:


if __name__ == "__main__":
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    # print(A)
    A_dif = []
    for i in range(len(A)-1):
        # print(A[i + 1])
        # print(A[i])
        A_dif.append(A[i + 1] - A[i])
    A_dif.append((abs(K - A[-1]) + A[0]))
    # print(A_dif)
    A_dif.sort(reverse=True)
    ret = sum(A_dif[1:])
    print(ret)

Submission status: Accepted

Solution code:
KN = input().split()
A = input().split()
kn = [int(s) for s in KN]
a = [int(i) for i in A]
b = kn[0] + a[0] - a[-1]
num = 0
while num < len(a) -1:
    a[num] = a[num+1] - a[num]
    num += 1
a[len(a)-1] = b
print(int(KN[0])-max(a))

Submission status: Accepted

Solution code:
import numpy as np
ina = input().split()
k=int(ina[0])
n=int(ina[1])
inaa = list(map(int, input().split()))
f=int(inaa[0])
inaa.pop(0)
inaa.append(k+f)
inaaa=np.array(inaa)
inaaaa=np.diff(inaaa,n=1)
print(k-np.amax(inaaaa))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

l=[]

l.append(kn[0]-a[-1]+a[0])

for i in range(kn[1]-1):
    l.append(a[i+1]-a[i])

print(kn[0]-max(l))

Submission status: Accepted

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

#find distance
d_ls = []
for i in range(1, n):
  d = abs(a[i] - a[i-1])
  d_ls.append(d)

# final distance
d = k - sum(d_ls)
d_ls.append(d)

#delete maximum distance
d_ls.remove(max(d_ls))

print(sum(d_ls))

Submission status: Accepted

Solution code:
import sys

x = sys.stdin.readline()
x = x.split(' ')
(k,n)=[int(str) for str in x]

x = sys.stdin.readline()
x = x.split(' ')
a=[int(str) for str in x]

prev = 0
amax = 0
sa = 0
for i in range(0,n):
  if(i == 0):
    prev = a[i]
  else:
    among = a[i] - prev
    if( amax < among):
      amax = among
    prev = a[i]
    sa += among

last = a[0] + k - a[n-1]
if( amax < last):
  amax = last
sa += last

print(sa-amax)

sys.stdout.flush()

Submission status: Accepted

Solution code:
s=0;f=0;p=0;n=0
K,N=map(int,input().split())
n=N
A=list(map(int,input().split()))
if K!=A[-1]:
  if A[0]==0:
    A.append(K)
  else:
    A.append(K+A[0])
#print(A)
for i in range(N):
  if abs(A[i+1]-A[i])>s:
    s=abs(A[i+1]-A[i])
    f=i+1
if f==(N):
  f=0
#print("f",f)
if f==0:
  n=n-1
for i in range(f,n):
  p=p+abs(A[i+1]-A[i])
  #print(i,p)
for i in range(f-1):
  p=p+abs(A[i+1]-A[i])
print(p)

Submission status: Accepted

Solution code:
from sys import stdin
K, N = [int(x) for x in stdin.readline().rstrip().split()]
l = [int(x) for x in stdin.readline().rstrip().split()]

l.sort()

l2 = []

for i in range(N-1):
    l2.append(l[i+1] - l[i])

l2.append(l[0] + K - l[N-1])

ans = K - max(l2)

print(ans)

Submission status: Accepted

Solution code:
S = input().split(" ")
K = int(S[0])
N = int(S[1])
ARR = [int(s) for s in input().split(" ")]


def calculate(k,n,arr):
    result = []
    for i in range(n-1):
        s = arr[i+1] - arr[i]
        m = k - s
        result.append(m)

    result.append(arr[n-1] - arr[0])

    print(min(result))


calculate(K,N,ARR)

Submission status: Accepted

Solution code:
length,house = map(int,input().split())	
plot= list(map(int,input().split()))
plot.append(plot[0]+length)
ans=[]
for i in range(1,house):
  ans.append(min(length-abs((plot[i-1]-plot[i])),length-abs((plot[i+1]-plot[i]))))
ans.append(min(length-abs(plot[-1]-plot[-2]),length-abs(plot[1]-plot[0])))  
print(min(ans))

Submission status: Accepted

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

#find distance
d_ls = []
for i in range(1, n):
  d = abs(a[i] - a[i-1])
  d_ls.append(d)

# final distance
d = k - sum(d_ls)
d_ls.append(d)

#delete maximum distance
d_ls.remove(max(d_ls))

print(sum(d_ls))

Submission status: Accepted

Solution code:
K,N=input().split()
K=int(K)
N=int(N)
A=list(map(int,input().split()))
i=0
C=[]
for i in range(N-1):
    if (2<=K<=10**6)and(2<=N<=2*10**5)and(0<=A[i]<A[i+1]<K)and(i<N-1):
        B=A[i+1]-A[i]
        C.append(B)
        i+=1
    if (i==N-1)and(A[N-1]<K):
        B=K-A[N-1]+A[0]
        C.append(B)
d=max(C)
e=K-d
print(e)

Submission status: Accepted

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

s = 0
save = 0
start = 0
for i, a in enumerate(an):
  term = a - start
  if term > save:
    save = term
    s = i
  start = a

term = (k - an[-1]) + an[0]
if term > save:
  save = term
  s = 0

print(k-save)

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int,input().split())
a = np.array(list(map(int, input().split())))
diff = a[1:] - a[:-1]
diff = np.append(diff, k - a[-1] + a[0])
if np.argmax(diff) == len(a) - 1:
    print(a[-1] - a[0])
else:
    print(k - a[np.argmax(diff) + 1] + a[np.argmax(diff)])

Submission status: Accepted

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

Submission status: Accepted

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

# それぞれの家の間隔リストを作る
kankaku = [0] * N

for i, v in enumerate(A):
	if i == 0:
		# 最後の家との距離
		kankaku[i] = A[i] + (K - A[N - 1])
	else:
		kankaku[i] = v - A[i - 1]

kankaku.remove(max(kankaku))
print(sum(kankaku))

Submission status: Accepted

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

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[-1]+a[0])
d=sorted(d,reverse=True)
print(sum(d[1:]))

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a=[int(x) for x in input().split()]
half=k//2
min_a,max_a=min(a),max(a)
ans=0
diff=[0]*n
for i in range(1,n):
	diff[i-1]=a[i]-a[i-1]
diff[n-1]=k-a[n-1]+a[0]
#start_id=a.index(max(diff))
#end_id=start_id+1
ans=k-max(diff)
print(ans)

Submission status: Accepted

Solution code:
k,n=map(int,input('').split())
a=input('')
a=a.split(' ')
for b in range(n):
    a[b]=int(a[b])
a.sort()

m=0
for b in range(n-1):
    if m<=a[b+1]-a[b]:
        m=a[b+1]-a[b]

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

print(k-m)