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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
def input():
    return sys.stdin.readline()[:-1]

def main():
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))
    tmp = 0
    for i in range(N-1):
        tmp = max(tmp, A[i+1] - A[i])

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

S = [0]*(N+1)

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

Smax = S.index(max(S))

ans = sum(S)-S[Smax]
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k = 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: Runtime Error

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

print(k-max(glist))

Submission status: Runtime Error

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(int(k-max(glist)))

Submission status: Runtime Error

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

print(k - max(b))

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
x1 = list(map(int,input().split()))
x2 = np.array(x1, dtype=np.int64)

x2 = np.sort(x2)
sum = 0

for i in range(N-1):
    sum += x2[1] - x2[0]
    
print(sum)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
k = input()
k = int(k)

n = input()
n = int(n)

array = list(map(int, input().strip().split()))

count = []
for i in range(n):
    count.append(int(array[i]))

ans = count.copy()
for i in range(1, n):
    ans.insert(0, count[i] - k)
ans.sort()
# print(ans)
dif = []
for i in range(0, len(ans)-1):
    dif.append(ans[i+1]-ans[i])
dif.sort()
a = dif[:n-1]
print(sum(a))

Submission status: Runtime Error

Solution code:
k = input()
k = int(k)

n = input()
n = int(n)

a = [input() for i in range(n)]

count = []
for i in range(n):
    count.append(int(a[i]))

ans = count.copy()
for i in range(1, n):
    ans.insert(0, count[i] - k)
ans.sort()
# print(ans)
dif = []
for i in range(0, len(ans)-1):
    dif.append(ans[i+1]-ans[i])
dif.sort()
a = dif[:n-1]
print(sum(a))

Submission status: Runtime Error

Solution code:
import numpy as np
k = input()
k = int(k)

n = input()
n = int(n)

a = [input() for i in range(n)]

count = []
for i in range(n):
    count.append(int(a[i]))

ans = count.copy()
for i in range(1, n):
    ans.insert(0, count[i] - k)
ans = np.sort(ans)
# print(ans)
dif = []
for i in range(0, len(ans)-1):
    dif.append(ans[i+1]-ans[i])
a = np.sort(dif)[:n-1]
print(np.sum(a))

Submission status: Runtime Error

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 = []
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r.append(check(K - As[x-1]))
      continue
    elif As[x-1] == 0:
      r.append(check(As[x]))
      continue
    solve = As[x] - As[x-1]
    r.append(check(solve))
  ans = sorted(r)
  print(K-ans)

if __name__ == "__main__":
  main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
print(max(abs(a1 - a2)))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
l,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
sol.sort()
#unwanted segment of max length removed while printing
print(sum(sol[0:n-1]))

Submission status: Runtime Error

Solution code:
n,k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
 
sol.sort()
#unwanted segment of max length removed while printing
print(sum(sol[0:n-1]))

Submission status: Runtime Error

Solution code:
n,k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
 
sorted(sol)
#unwanted segment of max length removed while printing
print(sum(sol[0:n-1]))

Submission status: Runtime Error

Solution code:
n,k=[int(x) for x in input.split()]
a=[int(x) for x in input.split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
 
sorted(sol)
#unwanted segment of max length removed while printing
print(sum(sol[0:n-1]))

Submission status: Runtime Error

Solution code:
=[int(x) for x in input.split()]
a=[int(x) for x in input.split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
 
sol.sort()
#unwanted segment of max length removed while printing
print(sum(sol)[0:n-1])

Submission status: Runtime Error

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

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

sol.sorted()
#unwanted segment of max length removed while printing
print(sum(sol)[0:n-1])

Submission status: Runtime Error

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

max_dist = A_dist[0] = A[0] + (K - A[-1])
for i in range(1, N):
    if A[i] - A[i-1] > min_dist:
        max_dist = A[i] - A[i-1]

print(K - max_dist)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

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

m = max(dist)

print(K-)

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

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

m = min(dist)

print(K-dist)

Submission status: Runtime Error

Solution code:
count = 0
maxlength = 0
B = A.copy()
s=B.pop(0)
B.append(s)
sum = 0
for a in range(0,N):
    diff = B[a]-A[a]
    if a == N -1:
        diff = diff +K
    sum = sum + diff
    if diff>maxlength:
        maxlength = diff

print(sum -maxlength)

Submission status: Runtime Error

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

l = list(map(int, input().split()))
m = [0]*n
count = 0

for i in range(n-1):
    m[count] = l[i+1] - l[i]
    count+=1
m[n-1] = k - l(n-1) + l[0]

print(k - max(m))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

max_d = 0
for i in range(0, N-1):
    if A[i] == 0:
        pass
    else:
        d = abs(A[i] - A[len(A)-1])
        max_d = max(d, max_d)

print(k - max_d)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K,N=int(map(int,input().split()))
A=list(map(int,input().split()))
m=K
for j in range(len(A)):
  m=min(m,min(K-abs(A[j]-A[j-1]),K-abs(A[j]-A[j+1])))
print(m)

Submission status: Runtime Error

Solution code:
K,N=int(map(int,input().split()))
A=list(map(int,input().split()))
m=K
for j in range(len(N)):
  m=min(m,min(K-abs(N[j]-N[j-1]),K-abs(N[j]-N[j+1])))
print(m)

Submission status: Runtime Error

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)
    ans = 10**20
    for i in range(n):
        if A[0] != 0:
            if i == 0:
                ans = min(ans, abs(C[i] - C[i+n-1]), k - abs(C[i] - C[i+n-1]))
            else:
                ans = min(ans, k - abs(C[i] - C[i+1]),
                          k - abs(C[i] - C[i+n-1]))

        else:
            if i == 0:
                ans = min(ans, A[n-1] - A[0], k - (A[1] - A[0]))
            else:
                if i == n-1:
                    ans = min(ans, abs(C[i] - C[i+1]),k - abs(C[i] - C[i+n-1]))
                else:
                    ans = min(ans, k-abs(C[i] - C[i+1]),k - abs(C[i] - C[i+n-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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
    ans = 10**20
    for i in range(n):
        if A[0] != 0:
            if i == 0:
                ans = min(ans, abs(C[i] - C[i+n-1]), k - abs(C[i] - C[i+n-1]))
            else:
                ans = min(ans, k - abs(C[i] - C[i+1]),
                          k - abs(C[i] - C[i+n-1]))

        else:
            if i == 0:
                ans = min(A[n-1] - A[0], k - (A[1] - A[0]))
            else:
                if i == n-1:
                    ans = min(ans, abs(C[i] - C[i+1]),
                              k - abs(C[i] - C[i+n-1]))
                else:
                    ans = min(ans, k-abs(C[i] - C[i+1]),
                              k - abs(C[i] - C[i+n-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
k, n = map(int, input().split())
aa = 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: Runtime Error

Solution code:
k, n = map(int, input().split())
aa = 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: Runtime Error

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

A = []

for i in range(N):
  A.append(int(input()))
  
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: Runtime Error

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

for i in range(1,N):
    A_m.append(int(A[i] - A[i-1]))
  

if abs(A[N-1]-A[0]) > 10:
  A_m.append(A[0]-A[N-1] +20)
else:
  A_m.append(A[N-1]-A[0])
  
X = sum(A_m) - max(A_m)

print(X)

Submission status: Runtime Error

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: Runtime Error

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_lise)
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: Runtime Error

Solution code:
data = input().split()
k = data[0]
n = data[1]

data = input().split()
house = [int(s) for s in data]

ans = k - max(house[0], k - house[len(house)-1])
print(ans)

Submission status: Runtime Error

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: Runtime Error

Solution code:
k,n=map(int,input())
l=list(map(int,input()))
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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(l[0])
n = int(l[1])
x = 0
for i in range(n):
  d = int(j[i+1]) - int(j[i])
  x = max(x,d)
dt = k - int(x)
print(dt)

Submission status: Runtime Error

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(l[0])
n = int(l[1])
for i in range(n-1):
  d1 = int(j[i+1]) - int(j[i])
  d2 = int(j[i+2]) - int(j[i+1])
  max = max(d1,d2)
dt = k - int(max)
print(dt)

Submission status: Runtime Error

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(l[0])
n = int(l[1])
for i in range(n-1):
  d1 = int(j[i+1]) - int(j[i])
  d2 = int(j[i+2]) - int(j[i+1])
  max = max(d1,d2)
  dt = k - int(max)
print(dt)

Submission status: Runtime Error

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(l[0])
n = int(l[1])
for i in range(j):
  d1 = j[i+1] - j[i]
  d2 = j[i+2] - j[i+1]
  max = max(d1,d2)
  dt = k - int(max)
print(dt)

Submission status: Runtime Error

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(m[0])
n = int(m[1])
for i in range(j):
  d1 = j[i+1] - j[i]
  d2 = j[i+2] - j[i+1]
  max = max(d1,d2)
  dt = k - int(max)
print(dt)

Submission status: Runtime Error

Solution code:
l = input().split(" ")
j = input().split(" ")
k = int(m[0])
n = int(m[1])
A = []
for i in range(j):
  d1 = j[i+1] - j[i]
  d2 = j[i+2] - j[i+1]
  min = min(d1,d2)
  A.append(int(min))
print(A)

Submission status: Runtime Error

Solution code:
KN = input().split(' ')
K = int(KN[0])
N = int(KN[1])
M = K/2
Al = input().split(' ')
A = []
for home in Al:
  A.append(int(home))
m = 0

m = distance(A[0],A[-1],M,K)

for i in range(N-1):
  m = max(m,distance(A[i],A[i+1],M,L))
print (10-m)

def distance(d1,d2,M,K):
  if d1<=M and d2<=M:
    return abs(d2-d1)
  if d1<=M and d2>=M:
    return d1+(K-d2)
  if d1>=M and d2>=M:
    return abs(d2-d1)

Submission status: Runtime Error

Solution code:
class Graph:
    edges = {}

    def append_edge(self, sv, ev, weight = 1):
        if sv in self.edges:
            self.edges[sv].append((ev, weight))
        else:
            self.edges[sv] = [(ev, weight)]

    def bfs(self, sv):
        dist = [0]*len(self.edges.keys())
        q = []
        q.append(sv)
        dist[sv] = 0
        connect_vtxs = []
        while len(q) > 0:
            p = q.pop(0)
            connect_vtxs.append(p)
            for (ev, w) in self.edges[p]:
                if dist[ev] != -1:
                    continue
                q.append(ev)
                dist[ev] = dist[p] + 1
        return dist, connect_vtxs


N, X, Y = list(map(int, input().split()))

g = Graph()
for i in range(N - 1):
    g.append_edge(i, i + 1)
    g.append_edge(i + 1, i)
g.append_edge(X - 1, Y - 1)
g.append_edge(Y - 1, X - 1)

dist_cnt = [0]*N
for sv in range(N):
    dist, connect_vtxs = g.bfs(sv)
    for j in range(dist.shape[0]):
        if dist[j] == -1: continue
        dist_cnt[dist[j]] += 1

for i in range(N-1):
    print(int(dist_cnt[i+1]/2))

Submission status: Runtime Error

Solution code:
class Graph:
    edges = {}

    def append_edge(self, sv, ev, weight = 1):
        if sv in self.edges:
            self.edges[sv].append((ev, weight))
        else:
            self.edges[sv] = [(ev, weight)]

    def bfs(self, sv):
        dist = [0]*len(self.edges.keys())
        q = []   # キュー
        q.append(sv)
        dist[sv] = 0
        connect_vtxs = []
        while len(q) > 0:
            p = q.pop(0)     # キュー取りだし（先頭）
            connect_vtxs.append(p)
            for (ev, w) in self.edges[p]:
                if dist[ev] != -1:
                    continue
                q.append(ev)    # キュー追加
                dist[ev] = dist[p] + 1
        return dist, connect_vtxs


N, X, Y = list(map(int, input().split()))

g = Graph()
for i in range(N - 1):
    g.append_edge(i, i + 1)
    g.append_edge(i + 1, i)
g.append_edge(X - 1, Y - 1)
g.append_edge(Y - 1, X - 1)

dist_cnt = [0]*N
for sv in range(N):
    dist, connect_vtxs = g.bfs(sv)
    for j in range(dist.shape[0]):
        if dist[j] == -1: continue
        dist_cnt[dist[j]] += 1

for i in range(N-1):
    print(int(dist_cnt[i+1]/2))

Submission status: Runtime Error

Solution code:
from collections import deque
class Graph:
    edges = {}

    def append_edge(self, sv, ev, weight = 1):
        if sv in self.edges:
            self.edges[sv].append((ev, weight))
        else:
            self.edges[sv] = [(ev, weight)]

    def bfs(self, sv):
        dist = [0]*len(self.edges.keys())
        q = deque([])   # キュー
        q.append(sv)
        dist[sv] = 0
        connect_vtxs = []
        while len(q) > 0:
            p = q.popleft()     # キュー取りだし（先頭）
            connect_vtxs.append(p)
            for (ev, w) in self.edges[p]:
                if dist[ev] != -1:
                    continue
                q.append(ev)    # キュー追加
                dist[ev] = dist[p] + 1
        return dist, connect_vtxs


N, X, Y = list(map(int, input().split()))

g = Graph()
for i in range(N - 1):
    g.append_edge(i, i + 1)
    g.append_edge(i + 1, i)
g.append_edge(X - 1, Y - 1)
g.append_edge(Y - 1, X - 1)

dist_cnt = [0]*N
for sv in range(N):
    dist, connect_vtxs = g.bfs(sv)
    for j in range(dist.shape[0]):
        if dist[j] == -1: continue
        dist_cnt[dist[j]] += 1

for i in range(N-1):
    print(int(dist_cnt[i+1]/2))

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import deque
class Graph:
    edges = {}

    def append_edge(self, sv, ev, weight = 1):
        if sv in self.edges:
            self.edges[sv].append((ev, weight))
        else:
            self.edges[sv] = [(ev, weight)]

    def bfs(self, sv):
        dist = np.full(len(self.edges.keys()), -1)
        q = deque([])   # キュー
        q.append(sv)
        dist[sv] = 0
        connect_vtxs = []
        while len(q) > 0:
            p = q.popleft()     # キュー取りだし（先頭）
            connect_vtxs.append(p)
            for (ev, w) in self.edges[p]:
                if dist[ev] != -1:
                    continue
                q.append(ev)    # キュー追加
                dist[ev] = dist[p] + 1
        return dist, connect_vtxs


N, X, Y = list(map(int, input().split()))

g = Graph()
for i in range(N - 1):
    g.append_edge(i, i + 1)
    g.append_edge(i + 1, i)
g.append_edge(X - 1, Y - 1)
g.append_edge(Y - 1, X - 1)

dist_cnt = np.zeros(N)
for sv in range(N):
    dist, connect_vtxs = g.bfs(sv)
    for j in range(dist.shape[0]):
        if dist[j] == -1: continue
        dist_cnt[dist[j]] += 1

for i in range(N-1):
    print(int(dist_cnt[i+1]/2))