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())
mp = [int(x) for x in input().split()]
a = []
for i in range(n):
    if(i==(n-1)):
        last = k - mp[i] + mp[0]
        a.append(last)
        break
    a.append(mp[i+1] - mp[i])

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

Submission status: Accepted

Solution code:
line1 = input().split()
k = int(line1[0])
n = int(line1[1])
line2 = input().split()
a = [int(i) for i in line2]
dist_list = [a[i] - a[i - 1] for i in range(1, len(a))]
dist_list.append(k-a[-1] + a[0])
max_dist = max(dist_list)
index = len(dist_list) - dist_list[::-1].index(max_dist) - 1
start_index = (index + 1) % len(dist_list)
ans = 0
for i in range(start_index, start_index + n - 1):
  ans += dist_list[i % len(dist_list)]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

lmx=max(L)
print(K-lmx)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = map(int, input().split())
dist = [0] * K
for a in A:
    dist[a] = 1
d = dist.index(1)
ans = 0
count = 0
for i in range(K):
    if dist[(d + i) % K] == 1:
        ans = max(ans, count)
        count = 0
    else:
        count += 1
ans = max(ans, count)
print(K - ans - 1)

Submission status: Accepted

Solution code:
import sys
K, N = map(int, sys.stdin.readline().split())
A = sorted(list(map(int, sys.stdin.readline().split())))
ans = K
A.append(K + A[0])

for i in range(N):
    d = A[i + 1] - A[i]
    dist = K - d
    ans = min(ans, dist)
print(ans)

Submission status: Accepted

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

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

print(N-l)

Submission status: Accepted

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

A14=K-A[N-1]+A[0]
sa=[A14]

for i in range(N-1):
  sa.append(A[i+1]-A[i])
sa.sort()
ans=0
for i in range(N-1):
  ans+=sa[i]
print(ans)

Submission status: Accepted

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

ans = 0
maxi = 0

for i in range(1, len(A)):
    ans += abs(A[i]-A[i-1])
    if maxi < abs(A[i]-A[i-1]): maxi = abs(A[i]-A[i-1])
    
if A[0]==0:
    if maxi < N-A[-1]: maxi = N-A[-1]
    ans += N-A[-1]
else :
    if maxi < N-A[-1]+A[0]: maxi = N-A[-1]+A[0]
    ans += N-A[-1]+A[0]
    
print(ans-maxi)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))
B = np.roll(A, -1)
delta = B - A
delta = list(delta % K)
saidai = max(delta)

delta.remove(saidai)

print(sum(delta))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, n):
    y = a[i]
    x = a[i-1]
    d.append(y-x)
d.append(k - (a[n-1] - a[0]))
d.sort(reverse = True)
print(k-d[0])

Submission status: Accepted

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

diff = [None] * N

for i in range(N-1):
    diff[i] = A[i+1] - A[i]

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

max_diff = max(diff)
answer = sum(diff) - max(diff)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

can = k - max(c)
print(min((a[-1] - a[0]), can))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    k,n = map(int,input().split())
    a = list(map(int,input().split()))
    
    for i in range(n-1):
      a[i] = a[i+1]-a[i]
    a[n-1] = k-sum(a)+a[n-1]
    ans = k-max(a)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

for i in range(n):
  if i==0:
    ans=a[-1]-a[0]
  else:
    ans=min(ans,k-a[i]+a[i-1])
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])

ans = sum(d) - max(d)

print(ans)

Submission status: Accepted

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

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

print(k-max)

Submission status: Accepted

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

dis = 0
for i in range(len(a)-1):
    if dis < a[i+1]-a[i]:
        dis = a[i+1]-a[i]

if dis < a[0]+k-a[len(a)-1]:
    dis = a[0] + k-a[len(a)-1]

print(k-dis)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-max(list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

C = sum(B) - max(B)

print(C)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
*A,=map(int,input().split())
A.append(A[0]+K)
ans=float('inf')
for i in range(N):
  a,b=A[i],A[i+1]
  ans=min(ans, K-(b-a))
print(ans)

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
*A,=map(int,input().split())
ans=float('inf')
for i in range(N-1):
  a,b=A[i],A[i+1]
  b=abs(K-b)
  ans=min(ans, a+b)
print(min(ans,A[N-1]-A[0]))

Submission status: Accepted

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


max_d = 0
max_index = 0
for i in range(n-1):
    if A[i+1] - A[i] > max_d:
        max_d = A[i+1]-A[i]
        max_index = i

if k-A[n-1]+A[0] > max_d:
    max_d = k-A[n-1]+A[0]
    max_index = n-1

min_distance = 0

if(max_index != n-1):
    min_distance = A[max_index]+k-A[max_index+1]
else:
    min_distance = A[n-1]-A[0]

print(min_distance)

Submission status: Accepted

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

print(k-max(glist))

Submission status: Accepted

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

ans =[]

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(min(A[-1]-A[0], ans))

Submission status: Accepted

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

Submission status: Accepted

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

#湖の周長からaの間隔が一番大きいところを引く
a.append(k+a[0])
max_len = 0

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

print(k-max_len)

Submission status: Accepted

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

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

A.append(K+A[0])

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

print(K - maxl)

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

print(ans)

Submission status: Accepted

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

D = [0]*N

for i in range(N-1):
  D[i] = A[i+1] - A[i]
D[N-1] = A[0] - A[N-1] + K
      
D.sort()
print(K - D[N-1])

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)
###################################################

def main():
    K,N=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    mmax=0
    for a in range(0,N):
        mmax=max(mmax,abs(A[(a+1)%N]+K*((a+1)//N)-A[a]))
    print(K-mmax)

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(k - max(b))

Submission status: Accepted

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

print(k-c)

Submission status: Accepted

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

Submission status: Accepted

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

max_v = 0
for i in range(1, len(a_list)):
    max_v = max(max_v, a_list[i] - a_list[i - 1])

# 最初と最後の家の距離
max_v = max(max_v, k - a_list[-1] + a_list[0])

print(k - max_v)

Submission status: Accepted

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

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

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[n-1]))
for i in range(1,n):
    b[i] = a[i]-a[i-1]
print(sum(b)-max(b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def resolve():
    K,N=pin()
    A=lispin()
    temp=K
    for i in range(0,N):
        temp=min(temp,max(abs(A[i-1]-A[i]),K-abs(A[i]-A[i-1])))
    print(temp)
        
#%%submit!
resolve()

Submission status: Accepted

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

sub_x_list = x_list.copy()[1:]
sub_x_list.append(K + x_list[0])

for i in range(N):
    sub_x_list[i] -= x_list[i]
    
max_i = max(sub_x_list)
print(K - max_i)

Submission status: Accepted

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

sub_A_list = A_list.copy()[1:]
sub_A_list.append(K + A_list[0])

for i in range(N):
    sub_A_list[i] -= A_list[i]

maxi = max(sub_A_list)
print(K - maxi)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    k, n = map(int, stdin.readline().split())
    a = list(map(int, stdin.readline().split()))
    b = [0] * n
    for i in range(n):
        if 0 <= i < n - 1:
            b[i] = a[i + 1] - a[i]
        else:
            b[i] = k - a[i] + a[0]
    print(k - max(b))


if __name__ == "__main__":
    main()

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 - 1):
    max_dist = max(max_dist, abs(a[i] - a[i + 1]))
max_dist = max(max_dist,abs(k - a[n - 1] + a[0]))
print(k - max_dist)

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
R=[]
for i in range(N):
    if i == N-1:
        R.append(abs(A[i]-(K+A[0])))
    else:
        R.append(abs(A[i]-A[i+1]))
R.sort(key=int)
print(K-R.pop(-1))

Submission status: Accepted

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

Submission status: Accepted

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

right = a[n-1]-a[0]
left = 0

min = right

for x in range(n-1):
    left = (k-a[x+1])+a[x]
    if left <= right and left < min:
        min = left

print(str(min))

Submission status: Accepted

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

Submission status: Accepted

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

    a_diff = [a[i] - a[i-1] for i in range(1, len(a))]
    a_diff.append((k - a[-1]) + a[0])
    a_diff = sorted(a_diff, reverse=True)

    print(sum(a_diff[1:]))


resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  6 16:35:32 2020

@author: shinba
"""

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

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

print(K-max(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Traveling Salesman around Lake

K, N = map(int, input().split())
A = [a for a in map(int, input().split())]

total = 0
mx = 0

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

print(total - mx)

Submission status: Accepted

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

Submission status: Accepted

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

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

dist.append(K - A[N-1] + A[0])

print(K - max(dist))

Submission status: Accepted

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

l = [0]*n
l[-1] = k+a[0]-a[-1]
for i in range(n-1):
  l[i] = a[i+1] - a[i]
print(sum(sorted(l)[:-1]))

Submission status: Accepted

Solution code:
def bubbleSort(array):
    sortCompleted = False
    while sortCompleted != True:
        sortCompleted = True
        for i in range(len(array)-1):
            if array[i] > array[i+1]:
                array[i], array[i+1] = array[i+1], array[i]
                sortCompleted = False

def getMaxDistance(array):
    maxDistance = 0
    for i in range(len(array)-1):
        distance = abs(array[i] - array[i+1])
        if maxDistance < distance:
            maxDistance = distance
    return maxDistance

def function(K, An): 
    bubbleSort(An)
    An.append(K+An[0])
    maxDistance = getMaxDistance(An)
    minDistance = K - maxDistance
    return str(minDistance)

if __name__ == '__main__':
    K, N = map(int, input().split())
    An = list(map(int,input().split()))
    print(function(K, An))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

k, n = [int(i) for i in input().split()]
distances = [int(d) for d in input().split()]
sections = [distances[i+1] - distances[i] for i in range(n-1)]
sections.append(distances[0] - (distances[-1] - k))

print(k - max(sections))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = [a[i]-a[i-1] for i in range(1, n)]
b.append(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()))
t=[]
for i,j in zip(a,a[1:]):
  t.append(j-i)
t.append(k+a[0]-a[-1])
print(k-max(t))

Submission status: Accepted

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

Submission status: Accepted

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

interval = []
ans = 1e+9
for i in range(len(a_list)):
    if i == 0:
        distance = a_list[-1] - a_list[0]
    else:
        interval = a_list[i] - a_list[i-1]
        distance = k - interval
    ans = min(ans, distance)
print(ans)

Submission status: Accepted

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

distance = [0] * N
for i in range(N - 1):
    distance[i] = A[i + 1] - A[i]
distance[N - 1] = K - sum(distance)
# print(distance)

sorted_distance = sorted(distance, reverse=True)
print(sum(distance) - sorted_distance[0])

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
A = np.array(list(map(int, input().split())))
B = A[1:]
dist = B - A[:-1]
dist = np.append(dist, k-A[-1]+A[0])
print(np.sum(dist) - np.max(dist))

Submission status: Accepted

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

B = [A[-1] - k] + A[:n]

Diff = sorted(list(map(lambda x, y: abs(x-y), A, B)))
print(sum(Diff[:-1]))

Submission status: Accepted

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

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

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


resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - m)

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(N-1):
    if A[i+1]-A[i] > l:
        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(A[0]+K)
maxD = 0
 
for n in range(0, N):
  maxD = max(maxD, A[n+1] - A[n])
  
print(K - maxD)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    k,n = LI()
    a = LI()
    r = a[0] - a[-1] + k
    for i in range(n-1):
        r = max(r, a[i+1] - a[i])

    return k - r


print(main())

Submission status: Accepted

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

sum = 0
long_distance = 0
distance = 0
for i in range(n):
    if i == n-1:
        distance = k - As[i] + As[0]
    else:
        distance = As[i+1] - As[i]
    sum += distance
    if long_distance < distance:
        long_distance = distance

print(sum-long_distance)

Submission status: Accepted

Solution code:
def check(a):
  if a < 0:
    return a * -1
  else:
    return a

def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = []
  r.append(K-As[-1]+As[0])
  for x in range(N-1, -1, -1):
    solve = As[x] - As[x-1]
    r.append(check(solve))
  ans = sorted(r)
  ans.remove(ans[-1])
  print(K-ans[-1])

if __name__ == "__main__":
  main()