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

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

Submission status: Wrong Answer

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

B = []
for a in range(len(A)):
  if a != 0:
    B.append(A[a-1] - A[a] + 20)
  else:
    B.append(A[len(A)-1] -A[a])

print(min(B))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if min(A)+(K-max(A)) >= max(A)-min(A):
    distance=max(A)-min(A)
else:
    distance=A[1]+(K-max(A))
print(distance)

Submission status: Wrong Answer

Solution code:
import sys

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

if n==2:
  print(a[0])
  sys.exit()

b=[]
b.append(k-a[n-1])

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

b.remove(max(b))

print(sum(b))

Submission status: Wrong Answer

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

a = list(map(int,input().split()))
ans = 0
gr = 0
for i in range(1,n):
    if(a[i] - a[i-1] > gr):
        gr = a[i ] - a[i-1]

if(a[n-1] - a[0] > gr):
    gr = a[n-1] - a[0]
# print(gr)

print(k - gr)

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    num_list = list(map(int, input().split()))

    max_num = num_list[n-1]
    min_num = num_list[0]
    # if min_num == 0:
    #     min_num = num_list[1]

    ans1 = max_num - min_num

    ans2 = k - max_num + num_list[n-2]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 21:11:28 2020

@author: Kanaru Sato
"""

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

for i in range(n-1):
    d = a[i+1]-a[i]
    if d > k/2:
        d = k-d
    dl.append(d)

d = a[n-1]-a[0]
if d > k/2:
    d = k-d
dl.append(d)

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

print(ans)

Submission status: Wrong Answer

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

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

print(sum(delta)-maxd)

Submission status: Wrong Answer

Solution code:
import bisect
K, N = map(int, input().split())
a = list(map(int, input().split()))

a.sort()
tmp1 = a[-1]-a[0]
tmp2 = (K - a[-1]) + (a[-2] - a[0])
ans = min(tmp1, tmp2)
print(ans)

Submission status: Wrong Answer

Solution code:
def calc(K, N, A):
    A.sort()
    B = [A[i + 1] - A[i] for i in range(N - 1)]
    return K - max(B)


(K, N) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
print(calc(K, N, A))

Submission status: Wrong Answer

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

r = []

for i in range(1,n):
    t = a_L[i] - a_L[i-1]
    r.append(t)

if a_L[0] != 0:
    print(sum(r))
else:
    print(sum(r[1:]))

Submission status: Wrong Answer

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

if A[0] == 0:
  for i in range(2, N):
    sum += abs(A[i-i] - A[i])
  print(sum)
else:
  for i in range(1, N):
    sum += abs(A[i-1] - A[i])
    print(i, sum)
  print(sum)

Submission status: Wrong Answer

Solution code:
#import collections
import sys
input = sys.stdin.readline

#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''
#import copy
#import numpy as np

#import bisect
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
i = list(map(str, input().split()))
K = i[0]
N = i[1]
j = list(map(int, input().split()))

A = j[-1] - j[0]
B = (int(K) - j[-1]) +j[-2] 

if A < B:
    print(A)
else:
    print(B)

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
A_list = list(map(int,input().split()))
sabun = []
for i in range(1,N):
    sabun.append(A_list[i] - A_list[i-1])
sabun.append(K-A_list[N-1])
#print(sabun)
print(sum(sabun)-max(sabun))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
min_=0
max_=0
for i in range(n):
    l=a[i]-k
    c=0
    if abs(l)<a[i]: #左採用
        c=1
    if c==0:
        if max_<a[i]:
            max_=a[i]
    else:
        if l<min_:
            min_=l
print(min(max_-min_,a[-1]-a[0]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.append(n)
d=[]
for i in range(len(a)-1):
    d.append([i,a[i+1]-a[i]])
l=sorted(d,key=lambda x:x[1])

o=l[-1][0]
g=a[o]+n-a[o+1]
print(g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
maxnum1 = 0
maxnum2 = 0
for i in A:
    if i <= K/2:
        maxnum1 = max(i,maxnum1)
    else:
        maxnum2 = max(K-i,maxnum2)


print(min(A[N-1] - A[0],maxnum1+maxnum2))

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

b.append(a[0]+k-a[n-1])
c=max(b)
print(c)

Submission status: Wrong Answer

Solution code:
# C
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))

A = np.array(sorted(A))

A_div = np.append(A[1:], np.array([K]))- A

print(np.sum(A_div)-np.max(A_div))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
a=A[-1]-A[0]
for i in range(N):
    A[i]=K//2-A[i]
A.sort()
b=A[-1]-A[0]
print(min(a, b))

Submission status: Wrong Answer

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

res = 0
if (a_lst[-1] - a_lst[-2]) <= (k - a_lst[-1]):
    for i, a in enumerate(a_lst[:-1]):
        res += a_lst[i+1] - a
else:
    for i, a in enumerate(a_lst[1:]):
        res += a - a_lst[i-1]
    res += (a_lst[0] + k) - a_lst[-1] 
print(res)

Submission status: Wrong Answer

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

k, n, *lst = map(int, read().split())
lst.sort()
dis = 0
for i in range(n):
    if i == 0:
        dis = max(dis, k+lst[0]-lst[-1])
    else:
        dis = max(dis, lst[i] - lst[i - 1])

dis = min(dis, k - dis)
print(dis)

Submission status: Wrong Answer

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

a.sort()
x = a[-1] - a[0]
y = k - a[-1] + a[-2]

print(min(x,y))

Submission status: Wrong Answer

Solution code:

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

res= a[n-1]

max=0
n_prev=0

for nn in a:
    if (nn-n_prev) > (k-a[n-1]):
        res=(nn-n_prev)
    n_prev=nn

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    
    a = K - (A[-1] - A[0])
    b = A[-1] - A[0]
    if a < b:
        return K - (A[-1] - A[1])
    else:
        return b

print(solve())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K+A[0] - A[N-1]
	A[1:] = A[1:]-A[:N-1]
	np.sort(A)
	print(K-max(A[N-1],A_LAST))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys

K, N = [int(i) for i in sys.stdin.readline().strip().split(" ")]
A = [int(i) for i in sys.stdin.readline().strip().split(" ")]

s = 0
m = -1
a = []
for i in range(0, N-1):
    n = A[i+1] - A[i]
    m = max(m, n)
    s += n

n = K-A[-1]
m = max(m, n)
s += n

print(s - m)

Submission status: Wrong Answer

Solution code:

K, N = map(int, input().split())
A = [int(x) for x in input().split()]
B = [A[i] - A[i-1] for i in range(1, N)]
B.append(K - A[-1])
ans = sum(B) - max(B)
print(ans)

Submission status: Wrong Answer

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

max_a = max(A)
min_a = min(A)

if max_a - min_a <= K/2:
    print(max_a-min_a)
else:
    if max_a - min_a < A[-2] + (K-max_a):
        print(max_a - min_a)
    else:
        print(A[-2] + (K-max_a))

Submission status: Wrong Answer

Solution code:
m, n = map(int, input().split())
nums = list(map(int, input().split()))

a = 0
for i in range(n):
  if i == n-1:
    dif = m - nums[i]
  else:
    dif = nums[i+1] - nums[i]
  a = max(a, dif)
  
print(m-a)

Submission status: Wrong Answer

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

A_new = sorted(A)
zero = A_new.count(0)
l = len(A)

total = 0
for i in range(zero,l-1):
  total += (A[i+1] - A[i])

print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
A = np.array(A)
ma = 0
for i in range(N-1):
  tmp = abs(A[i] -A[i+1])
  if min(tmp,K - tmp) > ma:
    ma = min(tmp,K-tmp)
print(K - ma)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

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

old = None
for a in A:
    if not old:
        old = a
        continue
    distance = a - old
    if distance > max_distance:
        max_distance = distance

answer = K - max_distance
print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import tee


def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

if A[0] == 0:
    end = K
else:
    end = A[0]

lengths = [abs(p - q) for p, q in pairwise(A + [end])]
sum_length = sum(lengths)
max_length = max(lengths)

print(sum_length - max_length)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K+A[0] - A[N-1]
	A[1:] = A[1:]-A[:N-1]
	np.sort(A)
	print(K-max(A[N-1],A_LAST)-A[0])

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

for i in range(N):
    if A[i] >= K//2:
        B.append(A[i] - K)
    else:
        B.append(A[i])

MA = max(A)
mA = min(A)

MB = max(B)
mB = min(B)

print(min(MA-mA, MB-mB))

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):
    m=a[i+1]-a[i]
    ans = max(ans,m)
h = K - a[N-1]+a[0]
ans = max(ans,h)
ans = K-ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

diff_list = []
for i in range(n-1):
    diff_list.append(a_list[i+1] - a_list[i])

diff_list.append(k - a_list[n-1] + a_list[0])

print(max(diff_list))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
distances = list(map(int,input().split()))
distances.append(K-distances[-1])

for i in range(N-1, 0,-1):
    distances[i] -= distances[i-1]
print(sum(sorted(distances)[:-1]))

Submission status: Wrong Answer

Solution code:

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

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

max_diff = 0
for i in range(1,n):
    if i != 0:
        max_diff = max(abs(An[i] - An[i-1]), max_diff)

max_diff = max(max_diff, k-An[n-1] + An[0])
print(max_diff)

Submission status: Wrong Answer

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

nums = [K - As[-1]]
for idx in range(len(As)-1):
    nums.append(As[idx+1] - As[idx])

print(sum(nums) - max(nums[0], nums[-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    K, N = il()
    A = il()
    A.sort()
    l = len(A)//2
    t = A[l]
    tmp = t + (K-A[l+1])
    ret = max(A) - min(A)
    print(min(ret, tmp))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

print(max)

Submission status: Wrong Answer

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

hanbun = k /2
a.sort

if a[n-1] > hanbun:
    start = k - a[n-1] 
else:
    start = a[n-1]

result = a[n-1] - start
print(result)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
s = A[0]
for i in range(n):
  A[i] -= s 
ans = 0
idx = n
for i in range(n):
  if A[i] > k-A[i]:
    ans += k-A[i]
    idx = i
    break

ans += A[idx-1]
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    s=[0]*n
    for i in range(n):
        if i==0:
            s[i] = abs(a[i]-a[i-1])
        else:
            s[i] = 20-abs(a[i]-a[i-1])
    print(min(s))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
half = K / 2
for i in A:
    if i >= half:
        ans += K - i
    else:
        ans += i

print(ans)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [0]*n
a.sort()
amin = a[n-1]-a[0]
for i in range(n):
  if a[i] < k/2:
    b[i] = a[i] + k
  else:
    b[i] = a[i]
b.sort()
bmin = b[n-1]-b[0]

print(min(amin,bmin))

Submission status: Wrong Answer

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

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


ans = K//2


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    ans = max(K - max(A) + min(A), max(A) - min(A))
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

d = 0
pre = a[0]
for i in a[1:]:
    if i - pre > d:
        d = i - pre
    pre = i
if k - a[-1] + a[0] > d:
    d = a[-1] - a[0]
print(k - d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K - A[N-1]
	A[1:] = A[1:]-A[:N-1]
	np.sort(A)
	print(K-max(A[N-1],A_LAST)-A[0])

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if a[0] == 0:
  print(str(a[n-1]-a[1]))
else:
  print(str(a[n-1]-a[0]))

Submission status: Wrong Answer

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

d=max(d,k-a[-1]+a[0])
s+=(k-a[-1])+a[0]

print s-d

Submission status: Wrong Answer

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

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

print(K-max(L))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
A_list = list(map(int,input().split()))
sa = []
for i in range(1,N):
    sa.append(abs(A_list[i]-A_list[i-1]))
sa.append(K-A_list[-1])
print(sum(sa)-max(sa))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
points = list(map(int, input().split()))
start_end = (K - points[-1]) + points[0]
if start_end <= points[-1] - points[-2]:
  print(start_end + points[-2] - points[0])
else:
  print(points[-1] - points[0])

Submission status: Wrong Answer

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

kankaku = []
for i in range(len(A_list) - 1):
  kankaku.append(A_list[i+1] - A_list[i])
kankaku.append(A_list[0] - A_list[-1] + K)

print(kankaku)
_kankaku_max = max(kankaku)
print(K - _kankaku_max)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
x=list(map(int,input().split()))
xx=list(set(x))
y=[]
for i in x:
    if i*2>k:
        y.append(i-k)
    else:
        y.append(i)
yy=list(set(y))
print(min(max(xx)-min(xx),max(yy)-min(yy)))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
A_list = list(map(int,input().split()))
sa = []
for i in range(1,N):
    sa.append(abs(A_list[i]-A_list[i-1]))
sa.append(K-A_list[-1])
print(sa)
print(sum(sa)-max(sa))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = list(map(int,input().split()))
ans = 0
gr = 0
for i in range(1,n):
    if(a[i] - a[i-1] > gr):
        gr = a[i ] - a[i-1]

# print(gr)

print(k - gr)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

B += K - A[-1]
print(B - D)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=[int(i) for i in input().split()]
s=0
for i in a:
    if i!=0:
        s+=1
print(sum(a)//s)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
a_list=list(map(int,input().split()))
b_list=[]
for i in range(N):
    X=a_list[i]-a_list[i-1]
    b_list.append(abs(X))
print(K-max(b_list))

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    num_list = list(map(int, input().split()))

    max_num = num_list[n-1]
    min_num = num_list[0]
    if min_num == 0:
        min_num = num_list[1]

    ans1 = max_num - min_num

    ans2 = k - max_num + num_list[n-2]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
a , b = map(int,input().split())
res = list(map(int,input().split()))
k = 0
if res[0] == 0:
    k = k + 1
aa = abs(res[k] - res[-1])
bb = res[-1] - res[k]
minn = min(aa,bb)
print(minn)

Submission status: Wrong Answer

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

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

print(K - MIN_LIST)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = 0

for i in range(0,K-1):
    ans1 += (A[i+1]-A[i])

ans2 = 0
ans2 += (N - A[-1] + A[0])

for i in range(0,K-2):
    ans2 += (A[i+1]-A[i])
    
print(min(ans1,ans2))

Submission status: Wrong Answer

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

inv_ans = 0

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

print(K - inv_ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(k - (k-max(b)))

Submission status: Wrong Answer

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

o=d[-1][0]
g=a[o]+n-a[o+1]

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
K,N = LI()
A = LI()
ans = float('INF')
_A = []
for x in A:
    _A.append(x-K)
allA = _A+A
for i in range(N*2-2):
    ans = min(ans,allA[i+2]-allA[i])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

dp = [0] * n
dp[0] = 0

l = r = A[0]
for i in range(1, n):
  to = A[i]
  if A[i]<l:
  	dis_l = A[i] - l
  else:
    dis_l = l + k - A[i]
    
  if A[i]>r:
    dis_r = A[i] - r
  else:
    dis_r = A[i] + k - r
  
  if dis_r<dis_l:
    r = A[i]
  else:
    l = A[i]
    
ans = 0
if r>l:
  ans = r-l
else:
  ans = r + k - l
  
print(ans)

Submission status: Wrong Answer

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

k2 = k // 2

for i in range(n):
    if a[i] >= k2:
        a[i] = k2 - a[i]
re_a = sorted(a)
#print(re_a)
print(re_a[n-1] - re_a[0])

Submission status: Wrong Answer

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

k, n, *lst = map(int, read().split())
lst.sort()
dis = 0
for i in range(n):
    if i == 0:
        dis = max(dis, k-lst[-1])
    else:
        dis = max(dis, lst[i] - lst[i - 1])

dis = min(dis, k - dis)
print(dis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K - A[N-1]
	A[1:] = A[1:]-A[:N-1]
	np.sort(A)
	print(K-max(A[N-1],A_LAST))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

    l = 0
    dist = []
    for a_i in a[1:]:
        dist.append(a_i-l)
        l = a_i
    dist.append(k-a[-1]+a[0])

    print(k-max(dist))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

diff = [a[i + 1] - a[i] for i in range(n)]

ans = sum(diff) - max(diff)
print(ans)

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
List = [int(i) for i in input().split()]
s = [0]*b
s[b-1] = abs(a- List[b-1])
for i in range(b-1):
    s[i] = abs(List[i]-List[i+1])
print(a-max(s))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
if n == 2:
    a1, a2 = A
    print(min(a2 - a1, k + a1 - a2))
else:
    A[-1] -= k
    maximum = 0
    for i in range(n):
        x = A[i] - A[i-1]
        if x > maximum:
            maximum = x
    print(k - maximum)