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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# maintain overall min 
# iterate over two loops 
# find the absolute distance and before exiting the first maintain overall min
# also check the distance of last and first house
def findDistBetween(i,j):
    return abs(i - j)

def minDistfindDistBetweenLastAndFirstHouse(i,j):
    dist = findDistBetween(i, j)
    return K - dist 

K, N = input().split()
As = input().split()

K = int(K)
N = int(N)

# print ("As is")
# print (As)
# print (As.split())
A = [0] * len(As)
for i in range(len(As)):
    A[i] = int(As[i])
    
# A = list(map(lambda x: int(x), As))
# print (A)
# overAllMin = float('inf')
# minDist = 0 
# for i in range(N):
#     minDist = 0
#     for j in range(N):
#         # print (i, j)
#         minDist += findDistBetween(A[i], A[j])
#     # find the dist b/w last and first house
#     # minDist2 = minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
#     # minDist = min(minDist, minDist2)
#     print ("Min dist", minDist)
#     # print ("min dist2", minDist2)
#     overAllMin = min(overAllMin, minDist)

# print (overAllMin)
overAllMin = float('inf')
Arev = list(reversed(A))
dist1 = 0 
dist2 = 0
prevHouse = -1
for i in range(N):
    # print ("PRev house", prevHouse)
    # print ("Next house", A[i])

    dist1 = max(findDistBetween(A[i], prevHouse), dist1)
    prevHouse = A[i]
    

ma = max(dist1, minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1]))
print (K - ma)
# for i in range(N):
#     count = 0
#     curHouse = A[i]
#     j = i
#     flag0 = 0 
#     flag1 = 0
#     dist1 = 0 
#     dist2 = 0
#     while count < N:
#         if flag0 is 0:
#             dist1 += findDistBetween(curHouse, A[j])
#         curHouse = A[j]
#         j += 1
#         flag0 = 0
#         count += 1

#         if j == N and count < N: 
#             print ("in condition")
#             j = 0
#             dist1 += minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
#             flag0 = 1
        
        # count += 1

        # print ("dist1", dist1)

    # count = 0
    # curHouse = A[i]
    # j = i
    # while count < N:
    #     if flag1 is 0:
    #         dist2 += findDistBetween(curHouse, A[j])
    #     curHouse = A[j]
    #     j -= 1
    #     count += 1
    #     flag1 = 0
    #     if j == -1 and count < N: 
    #         j = N - 1 
    #         dist2 += minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
    #         flag1 = 1
    #     # count += 1 
# overAllMin = min(dist1, K - findDistBetween(A[0], A[-1]), overAllMin)
# print (dist1,  K - findDistBetween(A[0], A[-1]), overAllMin)
        
# print (overAllMin)

Submission status: Accepted

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

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

print(K-max(dist))

Submission status: Accepted

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

a.append(k+a[0])

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

print(k-d)

Submission status: Accepted

Solution code:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]

print(n-max(distance))

Submission status: Accepted

Solution code:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]
distance[2*k-1] = n-array[k-1] + array[0]
for i in range(k - 1):
    distance[k+i] = array[i+1] - array[i]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k * 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Submission status: Accepted

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

for i in range(1, N):
  List.append(A[i] - A[i-1])
  
d = K - A[-1] + A[0]
List.append(d)
List.sort()
print(sum(List[:-1]))

Submission status: Accepted

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

n_list=input().split(" ")

for i in range(len(n_list)):
  n_list[i]=int(n_list[i])
ans=n_list[len(n_list)-1]-n_list[0]
for i in range(1,len(n_list)):
  ans=min(ans, k-n_list[i]+n_list[i-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

local = False
if local:
    file = open("C.txt", "r")

def inp():
    if local:
        return file.readline().rstrip()
    else:
        return input().rstrip()

K,N = [int(_) for _ in inp().split()]
arr = [int(_) for _ in inp().split()]

temp = 0
max_dist = -1

for a in arr:
    cur_dist = a-temp

    max_dist = max(max_dist, cur_dist)

    temp = a

max_dist = max(max_dist, arr[0] + K-temp)

print(str(K-max_dist))

Submission status: Accepted

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

# 位置がsortされてなければsortしてあげる
an = list(map(int, input().split()))
max_interval = 0
for i in range(N):
    if i != N-1:
        max_interval = max(an[i+1] - an[i], max_interval)
    else:
        max_interval = max(an[0] + length - an[i], max_interval)
print(length - max_interval)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - max(dist))

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

print(ans)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
dist = list(map(int, input().split()))
maximum = 0
total = 0
for i in range(1, n):
    diff = dist[i] - dist[i - 1]
    total += diff
    maximum = max(diff, maximum)

diff = k - dist[n - 1] + dist[0]
maximum = max(diff, maximum)

print(total + diff - maximum)

Submission status: Accepted

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

ans = 0
for i in range(N):
    d = a[i + 1] - a[i]
    ans = max(ans, d)

print(K - ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split()) 
# K:円周, N :家の数
A = list(map(int, input().split()))
# A:北から時計回りの家距離
k = []
for c in range(N):
    if c == N - 1:
        kk = K - sum(k)
    else:
        kk = A[c + 1] - A[c]
    k.append(kk)
print(sum(k) - max(k))

Submission status: Accepted

Solution code:
KN=list(map(int,input().strip().split()))
K=KN[0]
N=KN[1]
A_list=list(map(int,input().strip().split()))

Dis_list=[A_list[n]-A_list[n-1] for n in range(1,N)]
Dis_list.append(K-(A_list[N-1]-A_list[0]))

min_distance=K-max(Dis_list)

print(min_distance)

Submission status: Accepted

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


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


ans = max(ans, An[0] - (An[-1]-K))

print(K-ans)

Submission status: Accepted

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

diffs = [-1] * (n)
for i in range(n-1):
    diffs[i] = positions[i+1] - positions[i]
diffs[n-1] = positions[0] - positions[-1] + k

print(k - max(diffs))

Submission status: Accepted

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

dist = [0] * N

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

dist[-1] = K - (A[-1] - A[0])

ans = K - max(dist)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C
KN = input().split(' ')
K = int(KN[0]) ; N = int(KN[1])
Ai = input().split(' ')
#Ai = Ai.sort()

AnoSa = []

for k in range(N-1):
    AnoSa.append(int(Ai[k + 1]) - int(Ai[k]))
if K - max(AnoSa) > int(Ai[N-1]) - int(Ai[0]):
    print(int(Ai[N-1]) - int(Ai[0]))
else:
    print(K - max(AnoSa))

Submission status: Accepted

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

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

mindis=0

for i in range(len(a)):
  cw=0 #時計回り
  ccw=0#反時計

  if i==0:
    cw=a[len(a)-1]-a[0]
    ccw=k-(a[i+1]-a[i])

  elif i==len(a)-1:
    cw=k-(a[i]-a[i-1])
    ccw=a[i]-a[0]


  else:
    cw=k-(a[i+1]-a[i])
    ccw=k-(a[i]-a[i-1])

  #print(cw,ccw)

  if mindis==0:
    if ccw>cw:
      mindis=cw
    else:
      mindis=ccw

  if mindis>cw:
    mindis=cw
  if mindis>ccw:
    mindis=ccw

print(mindis)

Submission status: Accepted

Solution code:
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
revA = [a - K for a in A[1:]]
S = revA + A

dis = []
for i in range(N):
  st = S[i]
  ed = S[i + N - 1]
  dis.append(max(st, ed) - min(st, ed))
  
print(min(dis))

Submission status: Accepted

Solution code:
k, n = input("").split()
k = int(k)
n = int(n)
Loc = list(map(int, input("").split()))
temp = Loc[-1]-Loc[0]
for i in range(n-1):
    x1 = Loc[i-1] + k - Loc[i]
    x2 = Loc[i] + k - Loc[i+1]
    temp = min(temp, x1, x2)
temp = min(temp,Loc[-1]-Loc[0], Loc[-2]+k-Loc[-1])

print(temp)

Submission status: Accepted

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

d= k - a[-1] + a[0] # dist btw last and 1st
dist.append(d)
dist.sort()
print(sum(dist[:-1]))

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(20, [5, 10, 15]), 10)

    def test_2(self):
        self.assertEqual(think(20, [0, 5, 15]), 10)


def solve():
    k, a = read()
    result = think(k, a)
    write(result)


def read():
    k, n = read_int(2)
    return k, read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(k, a):
    distance = []
    for i in range(len(a) - 1):
        distance.append(a[i + 1] - a[i])
    distance.append(k - a[-1] + a[0])
    return sum(distance) - max(distance)


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

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

a.append(a[0]+k)

for i in range(n):
    ans.append(a[i+1]-a[i])
print(k-max(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

C = []

for i,v in enumerate(A):
  if i <= len(A) - 2:
    C.append(A[i+1] - A[i])
  else: #最後は0番目との距離
    C.append(k - A[i] + A[0])

maxwidth = max(C)
print(k-maxwidth) #円周 - 最も物件同士の距離が開いている孤の長さ

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

As = map(int, raw_input().split())

a = N
for i in range(len(As)):
  start = As[i]
  stop = As[i-1]
  a_t = stop-start
  if a_t <= 0: a_t += N
  if a_t < a: a = a_t

print a

Submission status: Accepted

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

Submission status: Accepted

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

b = a
c = []
for i,v in enumerate(b):
    if i <= len(b)-2:
        c.append(b[i+1] - b[i])
    else: #最後は0番目との距離
        c.append(k-b[i]+b[0])

maxwidth = max(c)
print(k - maxwidth)

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):
    if i != N-1:
        B.append(max(K -abs(A[i]-A[i+1]), abs(A[i]-A[i+1])))
    else:
        B.append(max(K - abs(A[0]-A[N-1]),abs(A[0]-A[N-1])))
print(min(B))

Submission status: Accepted

Solution code:
L, k = map( int, input().split() )
a = list( map( int, input().split() ))
 
ret = L
for i in range( len(a) ):
  if i == 0:
    ret = min( ret, L - abs(a[0] - ( a[-1] - L )))
  else:
    ret = min( ret, L - abs(a[i] - a[i-1]))
print( ret )

Submission status: Accepted

Solution code:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys


def input() -> str:
    return sys.stdin.readline().rstrip("\r\n")


def main():
    k, n = map(int, input().split())
    houses = [int(s) for s in input().split()]
    houses = houses + [houses[0] + k]
    distance = 0
    for i in range(1, len(houses)):
        distance = max(distance, houses[i] - houses[i-1])
    print(k - distance)


if __name__ == "__main__":

    main()

Submission status: Accepted

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

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

dist = 0

dist = l - lis[n-1] + lis[0]
for kk in range(n-1):
    	if dist < lis[kk + 1] - lis[kk]:
    			dist = lis[kk + 1] - lis[kk]

print(l-dist)

Submission status: Accepted

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


def main():
    import sys
    input = sys.stdin.readline

    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    ans = a[-1] - a[0]

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

len = -float("INF")

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

print(k-len)

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
l=list(map(int,input().split()))
m=0
for j in range(1,len(l)):
	m=max(m,l[j]-l[j-1])
m=max(m,K-(l[len(l)-1]-l[0]))
print(K-m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

dis_list = [0 for i in range(n)]
for i in range(1, n):
    dis_list[i] = List[i] - List[i - 1]

dis_list[0] = k - List[-1] + List[0]

print(int(k -max(dis_list)))

Submission status: Accepted

Solution code:
n = input().split(" ")
input = input().split(" ")
dist = []

for i in range(0, int(n[1]) - 1):
    dist.append(int(input[i + 1]) - int(input[i]))

dist.sort()
dist.reverse()

if dist[0] >= int(input[0]) + (int(n[0]) - int(input[int(n[1]) - 1])):
    print(int(n[0]) - dist[0])
else:
    print(int(input[int(n[1]) - 1]) - int(input[0]))

Submission status: Accepted

Solution code:
#coding:utf-8

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

#逆周りの方が距離が短く済むので、距離を2倍にして考える
#池の一周の長さ+a[0]を座標として追加
a.append(a[0] + k)

#距離が最大になる経路は通らないので除外する
for i in range(n):
    dist.append(a[i+1] - a[i])

#print(a, dist)
#池の一周の長さから、距離が最大になるところを除外したものが最短
print(k - max(dist))

Submission status: Accepted

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

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

print(k-max(dif_list))

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
A = list(map(int,input().split()))
list = []
x = K-A[len(A)-1] + A[0]
list.append(x)
for i in range(1,len(A)):
    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()))
b = [0]*(n-1)
p = a[-1]
for i in range(n-1):
    b[i] = a[i+1] - a[i]
b.append(k-p+a[0])
ans = k- max(b)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]

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

tmp = A[0]
tmp += (K - A[-1])

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

ans.sort(reverse=True)

print(sum(ans[1:]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def start(k, n, a):
    max_distance=0
    for i in range(n-1):
        if(a[i+1]-a[i]>max_distance):
            max_distance = a[i+1]-a[i]
    if(k-a[-1]+a[0]>max_distance):
        max_distance = k-a[-1]+a[0]
    print(k-max_distance)

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

Submission status: Accepted

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

for i in range(N):
    A.append(A[i] + K)

result = 10000000
for i in range(N):
    temp = A[i+N-1] - A[i]
    if temp < result:
        result = temp
print(result)

Submission status: Accepted

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

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

    print(k - max_length)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

Submission status: Accepted

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

Submission status: Accepted

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

min = a[-1] - a[0]

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

print(min)

Submission status: Accepted

Solution code:
import copy
import math
import string
import heapq
import bisect
import sys
sys.setrecursionlimit(10**7)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

prev = a[0]

for v in a[1:]:
    ans = min(ans, k-(v-prev))
    prev = v
ans = min(ans, k-b)
    
print(ans)

Submission status: Accepted

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

prev = a[0]

for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k-b)
    ans = min(ans, k-(v-prev))
    prev = v
    
print(ans)

Submission status: Accepted

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

prev = a[0]

for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k-b)
    ans = min(ans, k-abs(v-prev))
    prev = v
    
print(ans)

Submission status: Accepted

Solution code:
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[b[0]+(a[0]-b[a[1]-1])]

for i in range(a[1]-1):
  c.append(b[i+1]-b[i])
print(sum(c)-max(c))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = k
b = a[0] + (k-a[-1])
prev = a[0]
for i, v in enumerate(a):
    x = 0
    if i == 0:
        x = max(abs(a[i+1]-v), b)
    elif i == len(a)-1:
        x = max(b, abs(v-prev))
    else:
        x = max(abs(a[i+1]-v), abs(v-prev))
    ans = min(ans, k-x)
    prev = v
print(ans)

Submission status: Accepted

Solution code:
k, n = map(int,input().split())
xs = list(map(int,input().split()))
xs = xs + [x+k for x in xs]

ans = float('inf')
for i in range(n):
    delta = xs[i+n-1] - xs[i]
    ans = min(delta, ans)

print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

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

max_distance = 0
max_distance_idx = 0
for i in range(0, N-1):
  dist = A_list[i+1]-A_list[i]
  if max_distance < dist:
    max_distance = dist
    max_distance_idx = i

if max_distance <= (K-A_list[N-1]) + (A_list[0]):
  print(A_list[N-1] - A_list[0])
else:
  print((A_list[max_distance_idx]) + (K-A_list[max_distance_idx+1]))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

kyori = []
for i in range(n - 1):
    kyori.append(abs(A[i] - A[i + 1]))

kyori.append(A[0] + (k-A[-1]))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K,N = map(int,input().split())
len = []
a = [int(x) for x in input().split()]
for i in range(N):
  if i == N-1:
    if a[0] == 0:
      len.append(K - a[i])
    else:
      len.append((K - a[i]) + a[0])
  else:
    len.append(a[i+1] - a[i])
len.sort(reverse=True)
print(K-len[0])

Submission status: Accepted

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

Submission status: Accepted

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

a += [a[0]+k]#aに要素を追加する

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
h_list = list(map(int, input().split()))
distance = []
tmp = h_list[0]
for i in range(len(h_list)):
    a = h_list[i] - tmp
    distance.append(a)
    tmp = h_list[i]
x = (k - h_list[len(h_list)-1] )+ h_list[0]
distance.append(x)
sorted_d_list = sorted(distance)
sorted_d_list.pop()
print(sum(sorted_d_list))

Submission status: Accepted

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

B = [ A[i+1]-A[i] for i in range(n-1) ] + [A[0]+k-A[-1]]
print(sum(B)-max(B))