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=[int(X ) for X in input().split()]
x=[0]*(n+1)
for i in range(1,n+1):
    x[i]=a[i-1]-a[i-2]
x[0]=min(a[-1]-a[0],k-a[-1]+a[0] )
print(max(x))

Submission status: Wrong Answer

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


dists = []
for i,start in enumerate(A):
  # 右回り
  posR = A[(i - 1) % N] + K  
  #max([a + K for a in A[:i]] + A[i+1:])
  # 左回り
  posL = A[(i + 1) % N] - K
  #posL = min(A[:i] + [a - K for a in A[i+1:]])
  dists.append(min(posR - start, - posL + start))
print(min(dists))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
assert len(A) == N
k = max(A) - min(A)
print(max(K - k, k))

Submission status: Wrong Answer

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

A2 = [K+a for a in A]

A = A + A2
dist1 = A[N-1] - A[0]
dist2 = A[2*N-2] - A[N-1]

print(min(dist1, dist2))

Submission status: Wrong Answer

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

A2 = [K+a for a in A]

A = A + A2
print(A)
dist1 = A[N-1] - A[0]
dist2 = A[2*N-2] - A[N-1]

print(min(dist1, dist2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8
a,b = map(int,input().split())
c = list(map(int,input().split()))
d = 0

for i in range(b-1):
    if c[i]== 0:
        d += 0
    else:
     d += abs(c[i]-c[i+1])
print(d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
ab=list(map(int,input().split()))
i=max(ab)
while True:
  if i%ab[0]==0 and i%ab[1]==0:
    print(i)
    break
  i+=max(ab)

Submission status: Wrong Answer

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

i = max(A, B)

while True:
  
  if i % A == 0 and i % B == 0:
    print(i)
    exit()
    
  i += max(A, B)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
array = list(map(int,input().split()))
array = sorted(array)
i = N // 2
half = K / 2
if array[i] > K / 2:
  ans1 =  ( K - array[i] ) + array[i -1]
elif array[i] == K/2:
  ans1 = array[i-1] + ( K - array[i+1] )
else:
  ans1 = ( K - array[i + 1] ) + array[i]
ans2 = array[N-1] - array[0]
ans = min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
array = list(map(int,input().split()))
array = sorted(array)
i = N // 2
if array[i] > K / 2:
  ans =  ( K - array[i] ) + array[i -1]
elif array[i] == K/2:
  ans = array[i-1] + ( K - array[i+1] )
else:
  ans = ( K - array[i + 1] ) + array[i]
print(ans)

Submission status: Wrong Answer

Solution code:
# import math
# import statistics
# import itertools
# a=int(input())
# b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(int(i))
A,B= map(int,input().split())
f = list(map(int,input().split()))
# g = [input().split for _ in range(a)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen

miti1=f[-1]-f[0]
miti2=A-f[-1]+f[-2]
print(min(miti1,miti2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
Alst = list(map(int, input().split()))
zero = Alst[0] + K
M = 0
now = Alst[0]
for i in Alst:
    dis = i - now
    if dis > M:
        M = dis
    now = i

last = zero - now
if last > M:
    M = last

print(M)

Submission status: Wrong Answer

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

for i in range(n-1) :
    d = max(d, A[i+1] - A[i])
d = max(d, abs(A[n-1]-k))
print(k-d)

Submission status: Wrong Answer

Solution code:

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
longest = 0

for i in range(n-1):
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)

print(max(longest, k-a_list[n-1]+a_list[0]))

Submission status: Wrong Answer

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

for i in range(len(distance_list)):
    if i==0:
        continue
    distance_list[i] = distance_list[i] - distance_list[i-1]

print(k - max(distance_list))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
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(ans)

Submission status: Wrong Answer

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

a_sort = sorted(a_array)
ans_1 = a_sort[-1] - a_sort[0]

a_array = list(map(lambda x: x if x < abs(k - x) else x - k, a_array))
a_sort = sorted(a_array)
ans_2 = a_sort[-1] - a_sort[0]

print(min(ans_1, ans_2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
dis = list(map(int,input().split()))
print(sum(dis)-max(dis))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
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_max)

L = sorted(L)


L.pop(-1)

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
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_max)

L = sorted(L)
print(L)

L.pop(-1)

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

Submission status: Wrong Answer

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



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

A_max = max(A)

for i in range(N-1):

    if i == N:
        L.append(abs(A[i]-A[i-1]))
        break
    else:
        L.append(abs(A[i+1]-A[i]))
        
L.append(K - A_max)

L = sorted(L)

L.pop(-1)

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

Submission status: Wrong Answer

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

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

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):
    kyori = a[i+1] - a[i]
    b.append(kyori)
b.append(abs(a[1]+(k-a[-1])))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for n in A:
  if n != 0 and n > K/2:
    Ap.append(n-K)
  else:
    Ap.append(n)


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


Ap.sort()
lp = 0
for j in range(N-1):
  lp += abs(Ap[i]-Ap[i+1])


if l >= lp:
  result = lp
elif l < lp:
  result = l 

print(result)

Submission status: Wrong Answer

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

for n in A:
  if n != 0 and n > K/2:
    Ap.append(n-K)
  else:
    Ap.append(n)


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


Ap.sort()
lp = 0
for j in range(N-1):
  lp += abs(Ap[i]-Ap[i+1])


if l >= lp:
  result = lp
elif l < lp:
  result = l 

print(Ap)
print(result)

Submission status: Wrong Answer

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

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

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

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

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

print(K-b)

Submission status: Wrong Answer

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

a.extend([A[0]+(K-A[N-1])])

print(K-max(a))

Submission status: Wrong Answer

Solution code:
# ABC160_c

K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for a in A:
    ans += min(a, K-a)
print(ans)

Submission status: Wrong Answer

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

before = 0
max_n = 0
for num in nums:
  dist = num - before
  max_n = max(max_n, dist)
max_n = max(max_n, K-nums[-1])
print(K-max_n)

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
a = [int(x.strip()) for x in input().split()]
for i in range(n):
  if i != 0 and i != n-1:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] + (k - a[i+1])
    min_dist = min(dist_r,dist_l)
  elif i == 0:
    dist_r = a[n-1] - a[i]
    dist_l = a[i] + (k - a[i+1])
    min_dist0 = min(dist_r,dist_l)
  else:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] - a[0]
    min_distn = min(dist_r,dist_l)
ans = min(min_dist,min_dist0,min_distn)
print(ans)

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
a = [int(x.strip()) for x in input().split()]
for i in range(n):
  if i != 0 and i != n-1:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] + (k - a[i+1])
    min_dist = min(dist_r,dist_l)
  elif i == 0:
    dist_r = a[n-1] + (k - a[i])
    dist_l = a[i] + (k - a[i+1])
    min_dist0 = min(dist_r,dist_l)
  else:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] + (k - a[0])
    min_distn = min(dist_r,dist_l)
ans = min(min_dist,min_dist0,min_distn)
print(ans)

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
a = [int(x.strip()) for x in input().split()]
for i in range(n):
  if i != n-1 and i != 0:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] + (k - a[i+1])
    min_dist = min(dist_r,dist_l)
  elif i == 0:
    dist_r = a[n-1]
    dist_l = k - a[i+1]
    min_dist0 = min(dist_r,dist_l)
  else:
    dist_r = (k-a[i])+a[i-1]
    dist_l = a[i] - a[0]
    min_distn = min(dist_r,dist_l)
ans = min(min_dist,min_dist0,min_distn)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = []

diss = []

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


diss.append(k-a[-1])
mig = sum(diss[:-1])
left = sum(diss[1:])
diss.pop(diss.index(max(diss)))
print(sum(diss))

Submission status: Wrong Answer

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

sum = 0
max = 0

for i in range(n) :
    d = abs(a[i]-a[i+1])
    sum += d
    if max<d :
        max = d
#    print(sum,d,max)

print(sum-max)

Submission status: Wrong Answer

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] -= K

print(max(A) - min(A))

Submission status: Wrong Answer

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] -= K
    
print(max(A) - min(A))

Submission status: Wrong Answer

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

#print("sum",sum(theta))

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(l_min)

Submission status: Wrong Answer

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

#print("sum",sum(theta))

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(int(l_min)+1)

Submission status: Wrong Answer

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

#print("sum",sum(theta))

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(int(l_min))

Submission status: Wrong Answer

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(int(l_min))

Submission status: Wrong Answer

Solution code:
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(l_min)

Submission status: Wrong Answer

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

max_dist = 0
for i in range(N):
	dist = houses[N-1] - houses[i]
	if dist > K / 2:
		dist = K - dist
	if dist > max_dist:
		max_dist = dist
		
print(K - max_dist)

Submission status: Wrong Answer

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

dist = [0] * N

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

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

max_dist = max(dist)

print(max_dist)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    A = [int(x) for x in input().split()]
    minv = 2 * 10**5
    for i in range(n): 
        if i == n-1:
            right_d = (k - A[i]) + A[0]
        else:
            right_d = A[i+1] - A[i]
        if i == 0:
            left_d = A[i] + (k - A[n-1]) 
        else:
            left_d = A[i] - A[i-1]
        if right_d < left_d:
            ans = k - left_d
        else:
            ans = k - right_d
        # print(right_d, left_d)
        # print(i, ans)  
        minv = min(minv, ans)
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    A = [int(x) for x in input().split()]
    minv = 2 * 10**5
    for i in range(n): 
        if i == n-1:
            right_d = (k - A[i]) + A[0]
        else:
            right_d = A[i+1] - A[i]
        if i == 0:
            left_d = A[0] + (k - A[-1]) 
        else:
            left_d = A[i] - A[i-1]
        if right_d < left_d:
            ans = k - left_d
        else:
            ans = k - right_d
        minv = min(minv, ans)
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    A = [int(x) for x in input().split()]
    minv = 2 * 10**5
    for i in range(n): 
        if i == n-1:
            right_d = (k - A[-1]) + A[0]
        else:
            right_d = A[i+1] - A[i]
        if i == 0:
            left_d = A[0] + (k - A[-1]) 
        else:
            left_d = A[i] - A[i-1]
        if right_d < left_d:
            ans = k - left_d
        else:
            ans = k - right_d
        minv = min(minv, ans)
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
S=[0 for _ in range(k)]

for i in range(1,k+1):
    if i!=k:
        S[i-1]=A[i]-A[i-1]
    else:
        S[i-1]=S[0]+n-A[k-1]

print(n-max(S))

Submission status: Wrong Answer

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

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

c = abs(A[0] - A[N-1])

for i in range(N-1):
  d = abs(A[i] - A[i+1])
  if c < d:
    c = d
  else:
    c = c
    
print(c)

Submission status: Wrong Answer

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

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

c = abs(A[0] - A[N-1])

for i in range(N-1):
  d = abs(A[i] - A[i+1])
  if c < d:
    c = d
  else:
    c = c
    
print(K-c)

Submission status: Wrong Answer

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

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

c = abs(A[1] - A[N-1])

for i in range(N-1):
  d = abs(A[i] - A[i+1])
  if c < d:
    c = d
  else:
    c = c
    
print(K-c)

Submission status: Wrong Answer

Solution code:

def main():
  K, N = map(int, input().split())
  A = [int(a) for a in input().split()]
  i=0
  while A[i]<=K//2:
    if i==len(A)-1:
      break
    i+=1
  print(min(A[i-1] + (K - A[i]), A[-1]-A[0]))

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:

def main():
  K, N = map(int, input().split())
  A = [int(a) for a in input().split()]
  i=0
  while A[i]<K//2:
    if i==len(A)-1:
      break
    i+=1
  print(min(A[i-1] + (K - A[i]), A[-1]-A[0]))
    



if __name__=='__main__':
  main()

Submission status: Wrong Answer

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

print(K-max(a))

Submission status: Wrong Answer

Solution code:
k,N=map(int,input().split())
A=list(map(int,input().split()))
a=[]
for i in A:
    a.append(abs(k-i))
    
print(max(a)-min(a))

Submission status: Wrong Answer

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

dis.sort
print(sum(dis) - dis[len(dis) - 1])

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
k,n = readInts()
A = sorted(readInts())
A.append(A[0])
long = 0
for i in range(n):
    if i != n-1:
        long = max(long,abs(A[i+1] - A[i]))
    else:
        long = max(long, abs((20-A[i]) + A[i+1]))
print(k - long)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for j in range(n,2*n):
    aa[j] = aa[j] + k

dis = k

for i in range((2*n)-1):
    tonari = aa[i+1]-aa[i]
    dis = min(dis,k-tonari)

print(dis)
print(aa)

Submission status: Wrong Answer

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

for j in range(n):
    aa[j] = aa[j] + k

aaa = a + aa
dis = k

for i in range((2*n)-1):
    short = aaa[i+1]-aaa[i]
    dis = min(dis,k-short)

print(dis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = sorted(map(int, input().split()))
print(min(a[-1]-a[0], k-a[-1]+a[-2]))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
l=list(map(int,input().split()))
m=[]
for i in range(b):
  if i<=b-2:
    m.append(abs(l[i+1]-l[i]))
  else:
    m.append(abs(l[b-1]-l[0]))
m.sort(reverse=True)
print(a-m[0])

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
l=list(map(int,input().split()))
m=[]
for i in range(b):
  if i<=b-2:
    m.append(abs(l[i+1]-l[i]))
  else:
    m.append(abs(l[b-1]-l[0]))
m.sort(reverse=True)
print(m)
print(a-m[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

minHouse = min(A)
maxHouse = max(A)

if minHouse == 0:
    for i in range(N):
        if A[i] != 0:
            minHouse = A[i]
            break
candidate1 = maxHouse - minHouse

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)

#右回り
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: Wrong Answer

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)

#右回り
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))

Submission status: Wrong Answer

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

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

A.sort()

minHouse = min(A)
maxHouse = max(A)

if minHouse == 0:
    for i in range(N):
        if A[i] != 0:
            minHouse = A[i]
            break

print(maxHouse - minHouse)

Submission status: Wrong Answer

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

max_ = [0, 20+A[0]-A[-1]]

for i in range(1, N):
    tmp = A[i] - A[i-1]
    if tmp > max_[1]:
        max_[0] = i
        max_[1] = tmp
print(K-max_[1])

Submission status: Wrong Answer

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

l=[]
a.append(k)
for i in range(0,n):
  abs_= abs(a[i]-a[i+1])
  l.append(abs_)
abc=l.pop(l.index(max(l)))
print(k-abc)

Submission status: Wrong Answer

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

l=[]
a.append(k)
for i in range(0,n):
  abs_= abs(a[i]-a[i+1])
  l.append(abs_)
l.pop(l.index(max(l)))
print(sum(l))

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

Submission status: Wrong Answer

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

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-1):
    B[i]=A[i+1]-A[i]

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

ans = sum(B[0:-1])
print(ans)

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-1):
    B[i]=A[i+1]-A[i]

B[-1]=K-A[-1]


ans = K-max(B)
print(ans)

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-1):
    B[i]=A[i+1]-A[i]

B[-1]=K-A[-1]

B=sorted(B)
ans = sum(B[:-1])

print(ans)

Submission status: Wrong Answer

Solution code:
#C - Traveling Salesman around Lake WA(ヒント)
K,N = map(int,input().split())
A = list(map(int, input().split()))

A[N+1:] = [A[0] + K]#Aを二回繰り返して円を表現

distance_list = []
for i in range(N):
    distance = abs(A[i] - A[i+1])
    distance_list.append(distance)
print(distance_list)
distance_list.remove(max(distance_list))
print(sum(distance_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(lambda x:x+a,A))
defo=100000
H=A+B
for i in range(b):
  D=H[i+b-1]-H[i]
  if D<defo:
    defo=D
print(defo)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(lambda x:x+a,A))
defo=100000
H=A+B
for i in range(b):
  D=H[i+2]-H[i]
  if D<defo:
    defo=D
print(defo)

Submission status: Wrong Answer

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

B=[]
t=0
for a in A:
    B.append(a-t)
    t=a
B[0] +=k-t

print(B, k-max(B) )

Submission status: Wrong Answer

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

if 0 not in A:
    print(A[-1]-A[0])
else:
    print(A[-1]-A[1])

Submission status: Wrong Answer

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

S=[A[-1]-K, *A[0:-1]]
print(min(A[-1]-A[0], S[-1]-S[0]))

Submission status: Wrong Answer

Solution code:


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

Submission status: Wrong Answer

Solution code:


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

Submission status: Wrong Answer

Solution code:
line1 = input()
line2 = input()

aryLine1 = line1.split()
arystrLine2 = line2.split()

K = int(aryLine1[0]);
N = int(aryLine1[1]);

aryLine2 = [int(s) for s in arystrLine2]
#print(aryLine2)

aryLine2.sort()
#print(aryLine2)

ans = 0

for i in range(0, N):
    temp1 = int(aryLine2[i])
    if i == N - 1:
        temp2 = int(aryLine2[0]) + K
    else:
        temp2 = int(aryLine2[i + 1])
    #print(str(temp1) + " " + str(temp2))

    if temp2 - temp1 > temp1 - temp2 + K:
        temp = temp1 - temp2 + K
    else:
        temp = temp2 - temp1
    #print(temp)

    if temp > ans:
        ans = temp

print(ans)

Submission status: Wrong Answer

Solution code:
line1 = input()
line2 = input()

aryLine1 = line1.split()
aryLine2 = line2.split()

K = int(aryLine1[0]);
N = int(aryLine1[1]);

minA = K
maxA = 0

for i in range(0, N):
    temp = int(aryLine2[i])
    if temp < minA:
        minA = temp
    if temp > maxA:
        maxA = temp

ans1 = maxA - minA
ans2 = minA - maxA + K

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)