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())
Alist = list(map(int,input().split()))
p = 10**8
p = min(max((K-abs(Alist[0]-Alist[N-1])),abs(Alist[0]-Alist[N-1])),max((K-abs(Alist[0]-Alist[1])),abs(Alist[0]-Alist[1])),p)
for i in range(1,N-1):
    p = min(p,max((K-abs(Alist[i]-Alist[i-1])),abs(Alist[i]-Alist[i-1])),max((K-abs(Alist[i]-Alist[i+1])),abs(Alist[i]-Alist[i+1])))
p = min(p,max((K-abs(Alist[N-1]-Alist[N-2])),abs(Alist[N-1]-Alist[N-2])),max((K-abs(Alist[N-1]-Alist[0])),abs(Alist[N-1]-Alist[0])))
print(p)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


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

    
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

ans = 0

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

ans = max(ans, a[0] + k - a[n - 1])

print(k - ans)

Submission status: Accepted

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

Submission status: Accepted

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

distance_list = []
for i, j in zip(A_list[0:-1], A_list[1:]):
    distance_list.append(j - i)
    
max_distance = max(distance_list)
distance_last_to_first = K - A_list[-1] + A_list[0]
max_distance = max(max_distance, distance_last_to_first)

ans = K - max_distance
print(ans)

Submission status: Accepted

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

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

print(sum(dis)-max(dis))

Submission status: Accepted

Solution code:
il1 = input().split()
il2 = input().split()

K = int(il1[0])
N = int(il1[1])
A = [int(i) for i in il2]

import copy

fixed_A = copy.copy(A)

for i in A:
    fixed_A.append(i+K)

ans_list =[]

for i in range(len(A)):
    # print("i = "+str(i))
    ans_list.append(fixed_A[i+N-1] - A[i])

ans_list.sort()

print(ans_list[0])

Submission status: Accepted

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

import numpy as np

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

A_diff = np.diff(A, n= 1).tolist()
A_diff.append(K+A[0]-A[-1])
print(K-max(A_diff))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

print(K-max)

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
A = np.array([int(i) for i in input().split()])
B = A[1:]
B = np.insert(B, N - 1, A[0] + K)
X = B - A
print(K - max(X))

Submission status: Accepted

Solution code:
import numpy as np
K, N = map(int, input().split())
A = np.array([int(i) for i in input().split()])
B = A[1:]
B = np.insert(B, N - 1, A[0] + K)
X = B - A
print(sum(X) - max(X))

Submission status: Accepted

Solution code:
K, N = map(int,input().split())
A = list(map(int, input().split()))
a = A[0]
sub = []
for _ in range(N):
    p = A.pop()
    sub.append(K-p)
    K = p
sub[0] += a
sub.remove(max(sub))
print(sum(sub))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(k-maxN)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b=map(int,input().split())

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

d=[]
for i in range(b-1):
  x=c[i+1]-c[i]
  d.append(x)

x=c[0]+a-c[b-1]
d.append(x)
d.sort()
print(sum(d[:b-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import deque, defaultdict, Counter
import math
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    k, n = wi()
    A = wi()
    B = A.copy()
    C = A + B
    # print(C)
    m = 0
    for i in range(n):
        m = max(m, C[i+1] - C[i])
        if i == n-1:
            m = max(m, k - C[i]+C[0])
    print(k-m)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a = np.array(a)
print(k - np.maximum((a[1:] - a[:-1]).max(), k - a[-1] + a[0]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
*A, = map(int, input().split())
c = []
for i in range(1, b):
    c.append(A[i]-A[i-1])
c.append(a-sum(c))
print(a-max(c))

Submission status: Accepted

Solution code:
#! /usr/bin/env python3

import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

k,n = na()
a = na()
b = [0]*n

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


print(k-max(b))

Submission status: Accepted

Solution code:
# 各ポイントから右回り(right)、左回り(left)の２通りで距離を計算する

K,N = map(int,input().split())
aList = [int(x) for x in input().split()]

minDist=pow(10,6)

for i in range(N-1):
    right=0
    left=0
    if i == 0:
        right = aList[N-1] - aList[0]
        left  = K - aList[1] + aList[0]
    elif i == N-1:
        right = K + aList[N-2] - aList[N-1]
        left  = aList[N-1] - a[0]
    else:
        right = K + aList[i-1] - aList[i]
        left  = K - aList[i+1] + aList[i]
    minDist =min(minDist,min(right,left))

print(minDist)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
for test in range(int(input())):
'''
inf = 100000000000000000  # 1e17

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

Submission status: Accepted

Solution code:
# solution

nim, mike = map(int, input().split())

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

A.append(A[0]+nim)

high=0

for i in range(mike):
  if A[i+1]-A[i]>high:
    high=A[i+1]-A[i]
print(nim-high)

Submission status: Accepted

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

diff = []
for i in range(len(A)):
    if i < N-1:
        diff.append(A[i+1] - A[i])

diff = diff + [K-A[-1]+A[0]]
longest_interval = diff.index(max(diff))
diff.pop(longest_interval)
ans = sum(diff)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

housekyori = []
for i,a in enumerate(A):
    if i == N-1:
        housekyori.append(K-a+A[0])
    else:
        housekyori.append(A[i+1]-a)
print(sum(housekyori)-max(housekyori))

Submission status: Accepted

Solution code:
import numpy as np

k, n = map(int, input().split())
l = list(map(int, input().split()))
a = np.array(l)
b = np.delete(np.append(a,k+a[0]), 0)

sa = b-a
sa_dash = np.delete(sa, np.argmax(sa))
print(np.sum(sa_dash))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np


def solve():
    dists = np.zeros(n, dtype=int)
    for i in range(n):
        dists[i] = abs(a[(i+1)%n]-a[i%n])
        dists[i] = min(dists[i], k-dists[i])
    ans = k - np.max(dists)
    print(ans)
    return




k, n = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
solve()

Submission status: Accepted

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

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

check_l.append(k+a[0]-a[n-1])
print(k-max(check_l))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
m = a[0]+k-a[n-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=list(map(int,input().split()))
a=list(map(int,input().split()))

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

Submission status: Accepted

Solution code:

if __name__ == "__main__":
    k, n = map(int, input().split())
    k = int(k)
    n = int(n)
    a = list(map(int, input().split()))
    ans = float('inf')

    for start in range(n):
        tmp = 0
        if start == 0:
            tmp = a[-1] - a[start]
        else:
            tmp = k - (a[start] - a[start - 1])
        if ans > tmp:
            ans = tmp

    print(ans)

Submission status: Accepted

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

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


print(sum(L) - max(L))

Submission status: Accepted

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

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

Submission status: Accepted

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

tmp = 0

for i in range(1, len(a)):
    if a[i]-a[i-1] >=tmp:
        tmp = a[i] - a[i-1]

if a[0]+k-a[len(a)-1] > tmp:
    tmp = a[0]+k-a[len(a)-1]

print(k-tmp)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
k,n = map(int,input().split())
a = np.array(input().split(),dtype=int)
max = 0
for i in range(n):
    if max < (a[i]-a[i-1])%k:
        max = (a[i]-a[i-1])%k

print(k-max)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

A.append(K + A[0])

distance = [ abs(A[i] - A[i + 1]) for i in range(N)]

print(K - max(distance))

Submission status: Accepted

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

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

Submission status: Accepted

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


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = k + homes[0] - homes[-1]
    if max_distance < distance:
        max_distance = distance
    return max_distance


print(k - max_distance_homes(home_positions))

Submission status: Accepted

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

b = []

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

b.append(k - sum(b))
print(k - max(b))

Submission status: Accepted

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

Submission status: Accepted

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

A = sorted(A)

for i in range (N-1):
    n = A[i + 1]
    m = A[i]
    if n - m > load:
        load = n - m

if A[0] + (K-A[N-1]) > load:
    load = A[0] + (K-A[N-1])

print(K-load)

Submission status: Accepted

Solution code:
j = list(map(int,input().split(" ")))
k = j[0]
n = j[1]
a = list(map(int,input().split(" ")))
# print(k)
# print(n)
# print(a)

difn = []
difn.append(k-a[n-1]+a[0])

for i in range(1,n):
    difa = a[i]-a[i-1]
    difn.append(difa)
    # print(difn)
difn.sort(reverse = True)
# print(difn)
print(k-difn[0])

Submission status: Accepted

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

B = []

for i in range(1, n):
    B.append(aa[i] - aa[i - 1])

B.append(k - sum(B))
print(k - max(B))

Submission status: Accepted

Solution code:
input_=[]
for i in range(2):
    input_.append(input())
    
K,N = list(map(int,input_[0].split()))
L = list(map(int,input_[1].split()))

dist=[0]*N

d = -1    
dist[d]=K + L[d+1]-L[d]
d_max = dist[d]

for d in range(N-1):
    dist[d] = L[d+1]-L[d]
    if d_max < dist[d]:
        d_max = dist[d]

ans=sum(dist)-d_max
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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):
    m=a[i+1]-a[i]
    ans = max(ans,m)
h = K - a[N-1]+a[0]
ans = max(ans,h)
ans = K-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):
    if i == 0:
        d.append(a[0]+K-a[N-1])
    else:
        d.append(a[i]-a[i-1])
print(sum(d)-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k-max(d))

Submission status: Accepted

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

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

Submission status: Accepted

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

# 隣り合う二つの配列の差を計算する
max_len = 0
A.append(A[0] + K)

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

# 円周Kから最長の距離を引くと答え
print(K - max_len)

Submission status: Accepted

Solution code:
# (160-c)
a = input()
b = input()
a_list = list(map(int, a.split()))
b_list = list(map(int, b.split()))
edges = []
for num in range(len(b_list)):
  if num == 0:
    edges.append(b_list[num])
  else:
    edges.append(b_list[num] - b_list[num - 1])

edges[0] = a_list[0] - b_list[-1] + b_list[0] 
edges[edges.index(max(edges))] = 0
print(sum(edges))

Submission status: Accepted

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
M = 0
i = 0
while i<N-1:
    if M<(A[i+1]-A[i]):
        M=(A[i+1]-A[i])
    i+=1

print(min(K-M,A[N-1]-A[0]))

Submission status: Accepted

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

D = [0] * K

for i in range(K-1):
    D[i] = S[i+1] - S[i]

D[-1] = (N - S[-1]) +S[0]
print(sum(D) - max(D))

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
house_list = list(map(int, input().split()))
for i in range(n):
  house_list.append(house_list[i] - k)
tmp_list = sorted(house_list)
ans_list = []
for j in range(n + 1):
  ans_list.append(tmp_list[j + n - 1] - tmp_list[j])
print(min(ans_list))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(K-m)

Submission status: Accepted

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

def check(K,N,A):
    a_dsn =[]
    sum = 0

    for a in range(len(A)-1):
        a_dsn.append(A[a+1] - A[a])
    
    a_dsn.append(A[0]+K-A[N-1])
    a_dsn.sort()
    #print(a_dsn)
    
    for a in range(len(a_dsn)-1):
        sum += a_dsn[a]

    print(sum)
    



K,N,A = nyu()
check(K,N,A)

Submission status: Accepted

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

diff = 0
total = 0
for i in range(n-1):
    diff = max(abs(a[i] - a[i+1]), diff)
    total += abs(a[i] - a[i+1])
    
diff_tmp = abs(a[n-1] - k) + a[0]
diff = max(diff_tmp, diff)
total += abs(a[n-1] - k) + a[0] - diff

print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

d = [0 for _ in range(n)]
for i in range(n):
    if i != n-1:
        d[i] = a[i+1]-a[i]
    else:
        d[i] = k+a[0]-a[i]
print(k-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K - max(space))

Submission status: Accepted

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

sum_num = 0
max_num = 0
tmp = K - A[-1] + A[0]
max_num = tmp
sum_num += tmp
diff.append(tmp)
for i in range(N - 1):
    tmp = A[i + 1] - A[i]
    diff.append(tmp)
    if tmp >= max_num:
        max_num =tmp
    sum_num += tmp

ans = sum_num - max_num
print(ans)

Submission status: Accepted

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

sum_num = 0
max_num = 0
max_num = K - A[-1] + A[0]
sum_num += K - A[-1] + A[0]
diff.append(K - A[-1] + A[0])
for i in range(N - 1):
    tmp = A[i + 1] - A[i]
    diff.append(tmp)
    if tmp >= max_num:
        max_num =tmp
    sum_num += tmp

ans = sum_num - max_num
print(ans)

Submission status: Accepted

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

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

B=[]

s=0

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

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

Submission status: Accepted

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

l.append(K+l[0])
l.insert(0, 0)

lsub = list()
for i in range (1, N+2):
    lsub.append(l[i] - l[i-1])

print(K - max(lsub))

Submission status: Accepted

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

long=0

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

if k+a[0]-a[n-1]>long:
  long=k+a[0]-a[n-1]

print(k-long)

Submission status: Accepted

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

l = []
for i in range(1, N+1):
    l.append(abs(A[i-1] - A[i]))
l.pop(l.index(max(l)))
print(sum(l))

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K, N = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    A_arr_diff = []
    for i in range(len(A_arr[1:])):
        A_arr_diff.append(A_arr[i+1]-A_arr[i])
    A_arr_diff.append(K-A_arr[-1]+A_arr[0])
    cnt = K - max(A_arr_diff)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

diff=[]
for i in range(len(a_list)-1):
    diff.append(a_list[i+1]-a_list[i])
diff.append(k-(a_list[n-1]-a_list[0]))
diff.sort(reverse=True)
print(k-diff[0])

Submission status: Accepted

Solution code:
import sys

k, n, *a = map(int, sys.stdin.read().split())
a += [a[0] + k]

def main():
    d = [a[i+1] - a[i] for i in range(n)]
    print(k - max(d))

if __name__ ==  '__main__':
    main()

Submission status: Accepted

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


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = min(homes[-1] - homes[0], k + homes[0] - homes[-1])
    if max_distance < distance:
        max_distance = distance
    return max_distance


print(k - max_distance_homes(home_positions))