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:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    #距離が最大を通らないようにする
    K,N=MI()
    A=LI()
    dist = []
    for i,a in enumerate(A):
        if(i!=N-1):
            dist.append(A[i+1]-a)
        else:
            dist.append(A[0]+K-a)
    dist.sort()
    print(sum(dist[:-1]))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

L = [A[0] + K - A[-1]]

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

L.sort()

print(sum(L) - L[-1])

Submission status: Accepted

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

Submission status: Accepted

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

last = a[0] + k - a[-1]

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

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    *a, = map(int, input().split())
    a.append(K + a[0])

    b = [a1 - a0 for a1, a0 in zip(a[1:], a)]
    tot = sum(b)
    ret = tot - max(b)
    print(ret)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

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:
import sys
input = sys.stdin.readline

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline=sys.stdin.readline

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

maxdiff=K-(A[-1]-A[0])
for i in range(1,len(A)):
  if abs(A[i]-A[i-1])>maxdiff:
    maxdiff=abs(A[i]-A[i-1])
print(K-maxdiff)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    k,n=MI()
    aa=LI()
    mx=k-aa[-1]+aa[0]
    for a0,a1 in zip(aa,aa[1:]):
        mx=max(mx,a1-a0)
    print(k-mx)

main()

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    k,n = inpm()
    a = inpl()
    a.append(a[0]+k)
    MAX = 0
    for i in range(n):
        d = a[i+1]-a[i]
        MAX = max(MAX,d)
    print(k-MAX)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

ans = k - d
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k, n, *a = map(int, open(0).read().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:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

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

Submission status: Accepted

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

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

print(k - m)

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

Submission status: Accepted

Solution code:
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

K, N, *A = map(int, read().split())
A += [A[0] + K]
max_gap = max(y - x for x, y in zip(A, A[1:]))
print(K - max_gap)

Submission status: Accepted

Solution code:

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

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

ans = K - A[-1] + A[0]

for i in range(N-1):

    ans = max(ans,A[i+1]-A[i])

print (K - ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    #→1,.→0として読み込む
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '#' else 0 for s in read()[:-1]])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    return ret


MOD = 10**9 + 7
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# 幅が一番大きいのを調べて引けば良い
K, N = read_ints()
A = read_ints()
import numpy as np
A_diff = list(np.diff(A))
A_diff.append(A[0] + K - A[-1])
print(K - max(A_diff))
# print(A_diff)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

K, n = na()
a = na()

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

Submission status: Accepted

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

arr = list(map(int,input().split()))
d = [0 for i in range(n)]

for i in range(n-1):
    d[i] = arr[i+1]-arr[i]

d[-1] = k-arr[-1]+arr[0]
# print(d)

print(k-max(d))

Submission status: Accepted

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

Submission status: Accepted

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

m = 0
for i in range(n):
    if i != n-1:
        b = a[i+1]-a[i]
    else:
        b = a[0]+k - a[i]
    m = max(m, b)

ans = k-m
print(ans)

Submission status: Accepted

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

print(K-MAX)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

res = a[0] + k - a[-1]

for i, j in zip(a, a[1:]):
    res = max(res, j - i)

print(k - res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(K: int, N: int, A: "List[int]"):
    print(K - max((A[i] - A[i - 1]) % K for i in range(N)))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)


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)
print(k-max(a[i+1]-a[i]for i in range(n)))

Submission status: Accepted

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

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

Submission status: Accepted

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

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    k,n = LI()
    a = LI()
    a += [a[0]+k]
    ans = k
    for i in range(n):
        m = a[i+1]-a[i]
        s = k-m
        if s < ans:
            ans = s
    print(ans)
    return

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

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


K, N = MI()
A = LMI()

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

Submission status: Accepted

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

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

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


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

Submission status: Accepted

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

diff = [a1 - a0 for a0, a1 in zip(aaa, aaa[1:])]
print(k - max(diff))

Submission status: Runtime Error

Solution code:
import numpy as np
K, N = map(int, input().split())
A = input().split()
diff = np.append(np.array(A[1:]) - np.array(A[:-1]), K - A[-1])
diff = np.delete(diff, np.argmax(diff))
print(diff.sum())

Submission status: Runtime Error

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(abs(A[0] + (K - A[-1])))
print(sum(li) - max(li))

Submission status: Runtime Error

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



A = sorted(A)
L = []
ANS = 0

A_max = max(A)

for i in range(N-1):
    L.append(abs(A[i+1]-A[i]))
        
L.append(K + A[0] - A_max)

L = sorted(L)


L.pop(-1)

for i in range(len(L)):
    ANS = ANS + L[i]
    
print(ANS)

Submission status: Runtime Error

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

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

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

print(k - d)

Submission status: Runtime Error

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

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

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

print(k-c)

Submission status: Runtime Error

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

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

c = [a[i]-a[i-1] for i in range(1,n)]

d = max(b,c)

print(d)

Submission status: Runtime Error

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

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

c = [a[i]-a[i-1] for i in range(1,n)]

print(max(b,c))

Submission status: Runtime Error

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

c = a[0] + (K - a[n-1])

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

print(K - d)

Submission status: Runtime Error

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

c = A[0]+(K-A[N-1])

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

print(K-d)

Submission status: Runtime Error

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

c = A[0]+(K-A[N-1])

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

print(K-d)

Submission status: Runtime Error

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

b = A[0]+(K-A[N-1])

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

c = max(a,b)

print(K-c)

Submission status: Runtime Error

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

b = A[0]+(K-A[N-1])

a = [A[i]-A[i-1] for i in range(1,N)]

c = max(a,b)

print(K-c)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a = [A[i]-A[i-1] for i in range(1,N)]

b = A[0]+(K-A[N-1])

c = max(a,b)

print(K-c)

Submission status: Runtime Error

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

a = [A[i]-A[i-1] for i in range(1,N-1)]

b = A[0]+(K-A[N-1])

print(K-max(a,b))

Submission status: Runtime Error

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

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

dif.extend([K-A[N]])

print(K-max(dif))

Submission status: Runtime Error

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

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

dif.extend([K-A[N]])

print(K-max(dif))

Submission status: Runtime Error

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

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

dif.extend([K-A[N]])

print(K-max(dif))

Submission status: Runtime Error

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

for i in range[N]:
    dif = A[i+1]-A[i]

dif.extend([K-A[N]])

print(K-max(dif))

Submission status: Runtime Error

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

Ai = list(map(int,input().split()))
kyori = []

for x in range(n-1):
  kyori.append(Ai[x+1]-Ai[x])
  
kyori.append(Ai[0]+k-Ai[n-1])

kyoto.remove(max(kyori))

print(sum(kyori))

Submission status: Runtime Error

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

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

print(K-l)

Submission status: Runtime Error

Solution code:
k,n =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(a[0]+k-a[n])
print(k-max(li))

Submission status: Runtime Error

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(abs(A[0] + (K - A[-1]))) 
print(sum(li) - max(li))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L = [list(map(int,input().split())) for i in range(M)]
dict = {}
 
if M == 0 :
    if N == 1 :
        print(0)
    else :
        print(10**(N-1))
    exit()
 
for x in L :
    dict.setdefault(x[0],[])
    dict[x[0]].append(x[1])
dict.setdefault(1,[1])
 
for k,v in dict.items():
    dict[k] = set(v)
 
ansList = ["0"]*N
for i in range(N) :
    if i+1 in dict :
        ansList[i] = str(list(dict[i+1])[0])
 
isZeroHead = 2 <= N and ansList[0] == "0"
isOverlapping = any([ 2 <= len(v) for v in dict.values()])  
ans = -1 if isZeroHead or isOverlapping else int("".join(ansList))
 
# print(dict)
# print("isZeroHead :",isZeroHead)
# print("isOverlapping :",isOverlapping)
print(ans)

Submission status: Runtime Error

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

diff=[]
l.append(l[0]+k)

for i in range(n-1):
  diff.append(l[n+1]-l[n])
  
print(sum(diff)-max(diff))

Submission status: Runtime Error

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

result = []

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

result.sort()

print(sum(result[:-1]))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

maxDifPair = None
maxDif = 0
for i in range(N-1):
    if A[i+1] - A[i] > maxDif:
        maxDif = A[i+1] - A[i]
        maxDifPair = (i, i+1)

if abs(A[N-1] - A[0]) > maxDif:
    maxDifPair = (N-1, 0)

#右回り
A_ = A.copy()
for i in range(N):
    A_[i] -= A[maxDifPair[1]]

    if A_[i] < 0:
        A_[i] = K + A_[i]

dist1 = max(A_)
# print(A_)

#左回り
A__ = A.copy()

for i in range(N):
    A__[i] -= A[maxDifPair[0]]
    if A__[i] < 0:
        A__[i] = K + A__[i]

# print(A__)
dist2 = max(A__)

print(min(dist1, dist2, candidate1))

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=[]
p = A[-1]-K
for a in A:
  l.append(a-l)
  p=a
print(sum(l)-max(l))

Submission status: Runtime Error

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

ans = 10**6 + 1
for i in range(n):
    ans = min(a[-1] - a[i])
    a.append(a[i]+k)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

for i in (1,n):
  b.append(a[i] - a[i-1])
b.append(k - a[n-1] + a[0])
b = sorted(b)

print(sum(b[:-1]))

Submission status: Runtime Error

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

b.sorted(b)

print(sum(b[:-1]))

Submission status: Runtime Error

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

b.sorted(b)

print(sum(b[:-1]))

Submission status: Runtime Error

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

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

print(sum(b[:n-1])

Submission status: Runtime Error

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

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

print(sum(b[:n])

Submission status: Runtime Error

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

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

print(sum(b:[:n])

Submission status: Runtime Error

Solution code:
N = int(input())
x = 0
a = []
for i in range(2,N,2):
    while i % 2 == 0:
        i //= 2
        x += 1
    a.append(x)
    x = 0
a = sorted(a)
a = a[::-1]
print(a[0])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(max(ans,c))

Submission status: Runtime Error

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

print(max(ans,c))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

print(K-l)

Submission status: Runtime Error

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

for i in range(n):
  l=max(A[i]-A[i-1])

print(K-l)

Submission status: Runtime Error

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

st = sorted(st)
st.append(st[0] + k)

diffs = [st[i+1] - st[i] for i in range(n)]

print(k - max(diffs))

Submission status: Runtime Error

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

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

print(k-g)

Submission status: Runtime Error

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n):
  if a[i] >= k/2
    a[i] = a[i] -k
print(max(a)-min(a))

Submission status: Runtime Error

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

dtlst = [al[0]]
for i in range(1,n):
  dtlst.append(al[i]-al[i-1])
dtlst.append(k - al[-1]+al[0])

print(k - max(dtlst))

Submission status: Runtime Error

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

dtlst = [al[0]]
for i in range(1,n):
  dtlst.append(al[i]-al[i-1])
dtlst.append(k - al[-1]+al[0])

print(k - max(dtlst))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import copy
import math

a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

L = math.inf
for i in b:
    b_1 = copy.deepcopy(b)
    j = copy.deepcopy(i)
    H = 0
    
    
    while True:
        if i in b_1:
            b_1.remove(i)
        if b_1 == []:
            break
        if H > L:
            break
        i += 1
        if i == a[0]:
            i = 0
        H += 1
    
    if L > H:
        L = H
        
    b_1 = copy.deepcopy(b)
    H = 0
    
    while True:
        if j in b_1:
            b_1.remove(j)
        if b_1 == []:
            break
        if H > L:
            break
        j -= 1
        if j == 0:
            j = a[0]
        H += 1
    
    if L > H:
        L = H
print(L)

Submission status: Runtime Error

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

D=[]
for i in range(len(A)-1):
    d=A[i+1]-A[i]
    D.append(D)

print(K-max(D))