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: Wrong Answer

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

def turn_scor(i):
    j = i + 1
    if j > len(A) -1:
        j = len(A) - j
        if A[0] == 0:
            A[0] = K
    return K - abs(A[i] - A[j])
ans = float('INF')

l = [turn_scor(x) for x in range(N)]
print(min(l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
perimeter,n = map(int, input().split())
v = list(map(int, input().split()))
f , l= (v[n-1]-v[0]),  (perimeter - (v[n-1]-v[n-2]))
print(min(f, l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

B = [K]*N
for i in range(N):
    B[i] += A[i]
    
C = A+B
print(C)

for i in range(2*N):
    ans = 2*K
    l = 0
    if i<N:
        for j in range(N-1):
            l += C[i+j+1]-C[i+j]
            if l<ans:
                ans = l
    else:
        for j in range(N-1):
            l += C[i-j]-C[i-j-1]
            if l<ans:
                ans = l
            
print(ans)

Submission status: Wrong Answer

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

B = [K]*N
for i in range(N):
    B[i] += A[i]
    
C = A+B

for i in range(2*N):
    ans = 2*K
    l = 0
    if i<N:
        for j in range(N-1):
            l += C[i+j+1]-C[i+j]
        if l<ans:
            ans = l
    else:
        for j in range(N-1):
            l += C[i-j]-C[i-j-1]
        if l<ans:
            ans = l
            
print(ans)

Submission status: Wrong Answer

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

B = [K]*N
for i in range(N):
    B[i] += A[i]
    
C = A+B



for i in range(2*N):
    ans = 2*K
    l = 0
    if i<N:
        for j in range(N-1):
            l += C[i+j+1]-C[i+j]
        if l<ans:
            ans = l
    else:
        for j in range(N-1):
            l += C[i-j]-C[i-j-1]
        if l<ans:
            ans = l
            
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

A_n.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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-abs(a[i+1]-a[i]))
print(ans)

Submission status: Wrong Answer

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: Wrong Answer

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

Submission status: Wrong Answer

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

A.insert(0, 0)
A.append(K)

# print(A)

start = A[1] - A[0]
goal = A[-1] - A[-2]

mixi = 0
sum_len = 0

SorG = max(start, goal)

for i in range(len(A) - 1):
    if A[i+1] - A[i] > mixi:
        mixi = A[i+1] - A[i]
    sum_len += A[i+1] - A[i]

if max(SorG, mixi) == SorG or SorG == mixi:
    result = sum_len - (start + goal)
elif max(SorG, mixi) == mixi:
    result = sum_len - mixi

print(result)

Submission status: Wrong Answer

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

A.insert(0, 0)
A.append(K)

print(A)

start = A[1] - A[0]
goal = A[-1] - A[-2]

mixi = 0
sum_len = 0

SorG = max(start, goal)

for i in range(len(A) - 1):
    if A[i+1] - A[i] > mixi:
        mixi = A[i+1] - A[i]
    sum_len += A[i+1] - A[i]

if max(SorG, mixi) == SorG or SorG == mixi:
    result = sum_len - (start + goal)
elif max(SorG, mixi) == mixi:
    result = sum_len - mixi

print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

for home in Al:
  A.append(int(home))

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

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

print (10-m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
res = 10 ** 12
for i in range(n):
  if i == 0:
    res = min(max(A[i+1] - A[i], k - (A[i+1] - A[i])), max(A[n-1] - A[0], k - (A[n-1] - A[0])))
  if 0 < i < n-1:
    res = min(max(A[i+1] - A[i], k - (A[i+1] - A[i])), max(A[i] - A[i-1], k - (A[i] - A[i-1])))
  else:
    res = min(max(A[i] - A[0], k - (A[i] - A[0])), max(A[i] - A[i-1], k - (A[i] - A[i-1])))
print(res)

Submission status: Wrong Answer

Solution code:
k, n = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
res = 10 ** 12
for i in range(n):
  if i < n - 1:
  	res = min(max(A[i+1] - A[i], k - (A[i+1] - A[i])), max(A[i] - A[i-1], k - (A[i] - A[i-1])))
  else:
    res = min(max(A[i] - A[0], k - (A[i] - A[0])), max(A[i] - A[i-1], k - (A[i] - A[i-1])))
print(res)

Submission status: Wrong Answer

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

diffs = list()
max_diff = 0
for i in range(n):
    diff = 0
    if i == n-1:
        diff = k + a[0] - a[i]
    else:
        diff = a[i+1] - a[i]

    if max_diff < diff:
        max_diff = diff
    diffs.append(diff)

sum(diffs) - max_diff

Submission status: Wrong Answer

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

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


B.append(abs(A[0] - K - A[-1]))

B = sorted(B)

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

Submission status: Wrong Answer

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

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


B.append(abs(K - A[-1] - A[0]))

B = sorted(B)

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

Submission status: Wrong Answer

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

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


B.append(abs(K - A[-1] - A[0]))

B = sorted(B)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
import numpy as np

S = input()
N = len(S)
S2 = S[:N//2]
S3 = S[(N+3)//2-1:]

if S == S[::-1] and S2 == S2[::-1] and S3 == S3[::-1]:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

max_gap = max([abs(a_list[i-1] - a_list[i]) for i in range(n)])
print(k - max_gap)

Submission status: Wrong Answer

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

A = sorted(A)

diff = []
for i, a in enumerate(A[1:]):
    diff.append(a-A[i])

diff.append(K-A[N-1]+A[0])
print(max(diff))

Submission status: Wrong Answer

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

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

print(k-max(b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
q=deque()
K,N= list(map(int, (stdin.readline().strip().split())))
A=list(map(int, (stdin.readline().strip().split())))
B=A
max=A[1]-A[0]
home=0
home_B=0
place=0
kyori=0
for i in range(N-1):
    if max< A[i+1]-A[i]:
        max=A[i+1]-A[i]
        place=i
    home+=A[i+1]-A[i]
for i in range(place+1,N):
    B.insert(0,abs(B[i]-K))
    del B[i+1]
print(B)
for i in range(N-1):
    home_B+=abs(B[i+1]-B[i])
if home>home_B:
    print(home_B)
else:print(home)

Submission status: Wrong Answer

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

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

print(k-max(b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
mid = k // 2
sa = [k] * n
for i in range(1, n):
    sa[i - 1] = min(k - a[i], a[i] - a[i - 1])
print(sa)
sa.sort()
sum = sum(sa) - sa[-1]
print(sum)

Submission status: Wrong Answer

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

Smax=0
for i in range(len(A)):
    S=0
    if i!=len(A)-1:
        S=abs(A[i]-A[i+1])
    else:
        S=abs(A[-1]-A[0])
    if S>Smax:
        Smax=S

print(K-Smax)

Submission status: Wrong Answer

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

Smax=0
for i in range(len(A)-1):
    S=0
    if i!=K-1:
        S=abs(A[i]-A[i+1])
    else:
        S=abs(A[-1]-A[0])
    if S>Smax:
        Smax=S

print(K-Smax)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(n):
    A[i] = A[i] - A[0]
    if abs(A[i]) >= k/2:
        A[i] = abs(A[i]) - k
A.sort()
#print(A)
ans = A[n-1] - A[0]
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
B = [0]*n

for i in range(n):
    B[i] = A[i] - A[0]
    if abs(B[i]) >= k/2:
        B[i] = abs(B[i]) - k
    B.sort()
    #print(B)
    ans = B[n-1] - B[0]
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
B = [0]*n

for i in range(n):
    B[i] = A[i] - A[0]
    if B[i] > k/2:
        B[i] = B[i] - k
B.sort()
print(B[n-1] - B[0])

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
B = [0]*n
if n == 2:
    ans = A[1] - A[0]
elif n >= 3:
    for i in range(n):
        B[i] = A[i] - A[0]
        if B[i] > k/2:
            B[i] = B[i] - k
B.sort()
print(B[n-1] - B[0])

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
l = [int(i) for i in input().split()]
l.sort(reverse = True)

if l[N-1] == 0:
    print(K-l[0]+l[N-2])
else:
    print(K-l[0]+l[N-1])

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
l = [int(i) for i in input().split()]
l.sort(reverse = True)

if l[N-1] == 0:
    print(l[0]-l[N-2])
else:
    print(l[0]-l[N-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import statistics

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

for num, i in enumerate(a[:]):
  if num == (n - 1):
    dist.append(abs(k - i))
  else:
    dist.append(abs(a[num + 1] - i))
ind = dist.index(max(dist))
dist.pop(ind)
print(sum(dist))

Submission status: Wrong Answer

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

#posの隣同士の差を格納
#最も差が大きいポジションに対して、CCWとCWを考えて、小さいほうを採用

#差を格納
subtract = []
for i in range(n-1):
    subtract.append(pos[i+1]-pos[i])

#差を格納(posの終点～posの始点)
subtract.append(k-pos[n-1]+pos[0])

#差の配列から、最大差のインデックス番号を取得
idx = subtract.index(max(subtract))

if idx == 0:
    rsl = k-pos[idx-1]+pos[0]
else:
    rsl = pos[n-1]-pos[0]

print(rsl)

Submission status: Wrong Answer

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

#posの隣同士の差を格納
#最も差が大きいポジションに対して、CCWとCWを考えて、小さいほうを採用

#差を格納
subtract = []
for i in range(n-1):
    subtract.append(pos[i+1]-pos[i])

#差を格納(posの終点～posの始点)
subtract.append(k-pos[n-1]+pos[0])

#差の配列から、最大差のインデックス番号を取得
idx = subtract.index(max(subtract))

if idx == 0 or idx == n-1:
    cw_rsl = pos[n-1]-pos[0]
    ccw_rsl = k-pos[n-1]+pos[0]
else:
    cw_rsl = subtract[idx]
    ccw_rsl = k-pos[n-1]+pos[0]

if cw_rsl >= ccw_rsl:
    print(cw_rsl)
else:
    print(ccw_rsl)

Submission status: Wrong Answer

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:'''
"""
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 main():
    K, N = wi()
    A = wi()
    for i in range(len(A)):
        if i == 0:
            ans = min(K-abs(A[i]+K-A[-1]), K - abs(A[i]-A[i+1]))
        elif i == N - 1:
            ans = min(K-abs(K-A[i]+A[0]), K - abs(A[i]-A[i-1]))
        else:
            ans = min(K-abs(A[i]-A[i+1]), abs(A[i]-A[i-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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:'''
"""
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 main():
    K, N = wi()
    A = wi()
    for i in range(len(A)):
        if i == 0:
            ans = min(K-abs(A[i]+K-A[-1]), K - abs(A[i]-A[i+1]))
        elif i == N - 1:
            ans = min(K-abs(K-A[i]+A[0]), K - abs(A[i]-A[i-1]))
        else:
            ans = min(K-abs(A[i]-A[i+1]), K - abs(A[i]-A[i-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ll = l[:]

ll.append(k)
ll.pop(0)
s = []

for i,j in zip(l,ll):
    s.append(j - i)

print(k - max(s))

Submission status: Wrong Answer

Solution code:
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(min_LIST[i] <= (LIST[i + 1] - LIST[i])):
      x = (LIST[i + 1] - LIST[i])

  if(N != 2):
    min_LIST.append(LIST[0] + (k - LIST[-1]))
    if(x <= (LIST[0] + (k - LIST[-1]))):
      x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def main():

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

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



if __name__ =='__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = k - A[n-1]
z = A[0]
x = max(x,z)
for i in range(1,n):
    y = A[i]-A[i-1]
    if y > x:
        x = y

print(k-x)

Submission status: Wrong Answer

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

x = k - A[-1]
for i in range(1,n):
    y = A[i]-A[i-1]
    if y > x:
        x = y

print(k-x)

Submission status: Wrong Answer

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

x = max(A)
print(sum(A) - x)

Submission status: Wrong Answer

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[-1])
index = li.index(max(li))
li.pop(index)
print(sum(li))

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
li = list(map(int,input().split()))
li.sort()
if li[n-1] - li[0] <= li[n-2] + k - li[n-1]:
    print(li[n-1] - li[0])
else:
    print(li[n-2] + k - li[n-1])

Submission status: Wrong Answer

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

x_list = A_list[:-1]

y_list = A_list[1:]

diff_list = list(map(lambda x,y: y - x, x_list, y_list))
diff_list.append(int(K-A_list[-1]))

print(K - max(diff_list))

Submission status: Wrong Answer

Solution code:
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(min_LIST[i] <= (LIST[i + 1] - LIST[i])):
      x = (LIST[i + 1] - LIST[i])

  min_LIST.append(LIST[0] + (k - LIST[-1]))
  if(x <= (LIST[0] + (k - LIST[-1]))):
    x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(min_LIST[i] <= (LIST[i + 1] - LIST[i])):
      x = (LIST[i + 1] - LIST[i])
      print(x)

  print(min_LIST)

  min_LIST.append(LIST[0] + (k - LIST[-1]))
  if(x <= (LIST[0] + (k - LIST[-1]))):
    x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

distence_list = [x for x in [ A[i+1] - A[i] for i in range(len(A)-1)]]

m = max(distence_list)

print(K-m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
max = 0

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

Submission status: Wrong Answer

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

middle = K // 2

i=0
while A[i] <= middle:
  i += 1

d0 = A[i] - A[i-1]
d1 = K - A[-1] + A[0]

if d0 >= d1: print(K - d0)
else: print(K-d1)

Submission status: Wrong Answer

Solution code:
f=list(map(int,input().split()))
k=f[0];n=f[1]
dists=list(map(int,input().split()))
prev=0
maxdist=0
for d in range(n):
  maxdist=max(maxdist,abs(prev-dists[d]))
  prev=dists[d]
maxdist=max(maxdist,maxdist-dists[len(dists)-1]+dists[0])  
ans=k-maxdist
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import itertools

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

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

Submission status: Wrong Answer

Solution code:
f=list(map(int,input().split()))
k=f[0];n=f[1]
dists=list(map(int,input().split()))
prev=0
maxdist=0
for d in range(len(dists)):
  maxdist=max(maxdist,abs(prev-dists[d]))
  prev=dists[d]
a=max(maxdist,maxdist-dists[len(dists)-1]+dists[0])  
ans=k-a
print(ans)

Submission status: Wrong Answer

Solution code:
f=list(map(int,input().split()))
k=f[0];n=f[1]
dists=list(map(int,input().split()))
prev=0
maxdist=0
for d in range(1,len(dists)):
  maxdist=max(maxdist,abs(prev-dists[d]))
  prev=dists[d]
a=max(maxdist,maxdist-dists[len(dists)-1]+dists[0])  
ans=k-a
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import itertools

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

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

Submission status: Wrong Answer

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

middle = K / 2

i=0
while A[i] <= middle:
  i += 1

d0 = A[i] - A[i-1]
d1 = K - A[-1] + A[0]

if d0 >= d1: print(K - d0)
else: print(K-d1)

Submission status: Wrong Answer

Solution code:
f=list(map(int,input().split()))
k=f[0];n=f[1]
dists=list(map(int,input().split()))
ans=0
prev=dists[0]
for d in range(1,len(dists)):
  ans+=abs(prev-dists[d])
  prev=dists[d]
print(ans)

Submission status: Wrong Answer

Solution code:
f=list(map(int,input().split()))
k=f[0];n=f[1]
dists=list(map(int,input().split()))
ans=0
prev=dists[0]
for d in range(1,len(dists)):
  prev=max(prev,abs(prev-dists[d]))
print(prev)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
p,q= input().split()
a,b =(int(p), int(q))
f = map(int,input().split())
f_list = list(f)
out_dis =f_list[0] + a -f_list[b-1]
dis_list=[out_dis]
for i in range(b-1):
    dis = f_list[i+1]-f_list[i]
    dis_list.append(dis)

print(int((sum(dis_list)-max(dis_list))%20))

Submission status: Wrong Answer

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

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

x.pop(x.index(max(x)))

print(sum(x))

Submission status: Wrong Answer

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

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

x.append(K - max(A))
x.pop(x.index(max(x)))

print(sum(x))

Submission status: Wrong Answer

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

ri = r[-1] - r[-2]
le = r[0] + a - r[-1]

if ri < le:
    print(r[-1] - r[0])
else:
    num = 0
    for i in range(1, len(r) - 1):
        num += (r[i] - r[i-1])
    print(a - r[-1] + num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m =map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
print(abs(l[m-1]-l[0]))