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())
dis=list(map(int,input().split()))
ans=[]
for i in range(n):
    if i>=1:
        ans.append(dis[i]-dis[i-1])
print(k-max(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(max(l,n-l))

Submission status: Wrong Answer

Solution code:
def main():
    k,n = map(int, input().split())
    s = list(map(int, input().split()))
    s_d = [i/k for i in s] # 角度として扱う。1が1周
    print(s_d)
    ans = float("inf")
    for i in range(n):
        if s_d[i] == 0:
            t = s_d[-1] * k
            ans = min(ans,t)
        else:
            t = (1 - abs(s_d[i] - s_d[i-1])) * k
            ans = min(t,ans)
    print(int(ans))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    k, n = map(int, readline().rstrip().split())
    A = sorted(list(map(int, readline().rstrip().split())))
    maxA = k + A[0] - A[n-1]
    for i in range(n-1):
    	a = abs(A[i+1] - A[i])
    	maxA = max(maxA,a)
    print(maxA)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    k, n = map(int, readline().rstrip().split())
    A = sorted(list(map(int, readline().rstrip().split())))
    maxA = k + A[0] - A[n-1]
    for i in range(n-1):
    	a = abs(A[i+1] - A[i])
    	maxA = max(maxA,a)
    print(min(maxA,k - maxA))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

n_array = sorted(array)
for i in range(len(n_array)):
    if n_array[i] != 0:
        break
print(n_array[-1] - n_array[i])

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if A[2] - A[0] <= 10:
    print(A[2] - A[0])
else:
    print(min(A[1] -A[0], A[2] - A[1]) + 20 - A[2] + A[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(max(ans,c))

Submission status: Wrong Answer

Solution code:
import numpy as np
a,b=list(map(int,input().split()))
C=list(map(int,input().split()))

def getnear(list,num):
  near=np.abs(np.asarray(list)-num).argmin()
  return list[near]

S=sorted(C)
H=getnear(C,a/2)
print(abs(H-S[0]-S[-1]))

Submission status: Wrong Answer

Solution code:
import numpy as np
a,b=list(map(int,input().split()))
C=list(map(int,input().split()))

def getnear(list,num):
  near=np.abs(np.asarray(list)-num).argmin()
  return list[near]

S=sorted(C)
H=getnear(C,a/2)
r=abs(H+abs(a-S[0]))
l=abs(H+abs(a-S[-1]))
if r>l:
  print(l)
else:
  print(r)

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = map(int,input().split())
a = np.array([int(i) for i in input().split()])
'''
隣同士の家の距離が最大のものを求めて、Kメートルから引けばよい
時計回りと、反時計回りの経路がありうる
'''
d1 = np.diff(a)
d1 = np.append(d1, a[-1] - a[0])

ar = a[::-1]
d2 = np.diff(ar)
d2 = np.append(d2, ar[-1] - ar[0])
ans = k - np.concatenate([d1,d2], 0).max()
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min = 10**7
dist = []
for i in range(N-1):
    dist.append(A[i+1]-A[i])
    
dist.append(K-A[-1])

dist.sort()

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

Submission status: Wrong Answer

Solution code:
import numpy as np
ans = 9999999
K , N = map(int,input().split())
A = np.array(list(map(int,input().split())))
for i in range(N):
    start = np.array([A[i]] * N)
    kyori = np.sum(np.abs(start - A))
    if kyori < ans:
        ans = kyori
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

ma = 0
s = 0
for i in range(n):
    tmp = abs(a[i] - a[i + 1])
    s += tmp
    ma = max(ma, tmp)

print(s - ma)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

a = sorted(a)
if a[0] == 0:
    if n > 3:
        print(min(a[1] + (k - a[2]), a[n - 1]))
    else:
        print(abs(a[n - 1] - a[1]))
else:
    print(abs(a[n - 1] - a[0]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

a = sorted(a)
if a[0] == 0:
    if n > 3:
        print(min(a[1] + (a[n - 1] - a[2]), a[n - 1]))
    else:
        print(abs(a[n - 1] - a[1]))
else:
    print(abs(a[n - 1] - a[0]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

a = sorted(a)
if a[0] == 0:
    print(k - a[1] - abs(k - a[n - 1]))
else:
    print(abs(a[n - 1] - a[0]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

a = sorted(a)
if a[0] == 0:
    print(abs(a[n - 1] - a[n - 2]))
else:
    print(abs(a[n - 1] - a[0]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
max_dis = abs(A[0]-A[-1])%K
pre = A[0]
for a in A[1:]:
    max_dis = max(max_dis, abs(a-pre))
    pre = a
ans = K - max_dis
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
max_dis = 0
pre = A[0]
for a in A[1:]:
    max_dis = max(max_dis, abs(a-pre))
    pre = a
ans = K - max_dis
print(ans)

Submission status: Wrong Answer

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

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

dist_list = []
dist_list.append(20 - A[N-1] + A[0])
for i in range(1, N):
  dist_list.append(A[i]-A[i-1])
dist_list.remove(max(dist_list))
print(sum(dist_list))

Submission status: Wrong Answer

Solution code:
x = input()
num = int(x.split()[1])
perimeter = int(x.split()[0])
houses = [int(i) for i in input().split()]

maxDis = perimeter - houses[-1] + houses[0]
total = maxDis
for i in range(1, len(houses)):
    maxDis = max(maxDis, houses[i]-houses[i-1])
    total += houses[i]-houses[i-1]
print(total)

Submission status: Wrong Answer

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

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

print(k-length)

Submission status: Wrong Answer

Solution code:
def main():
    k,n  = map(int,input().split())
    A = [int(i) for i in input().split()]
    rev = []
    if A[0]==0:
        rev.append(0)
        for i in range(n-2,-1,-1):
            rev.append(k-A[i])
    else:
        
        for i in range(n-1,-1,-1):
            rev.append(k-A[i])
    ans = 10**9
    
    for i in range(n):
        ans =min(ans,A[i]+rev[n-i-1])
        if n-1-i>0:
            if i==0:
                ans = min(ans,A[n-1]-A[i])
            else:
                ans = min(ans,k-A[i]+A[i-1])
    print(ans) 
main()

Submission status: Wrong Answer

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

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

print(k-g)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
li = list(map(int, input().split()))
li_2 = []
for i in range(1,len(li)):
  li_2.append(li[i]-li[i-1])
print(k-max(li_2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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] = a[i] -k
print(max(a)-min(a))

Submission status: Wrong Answer

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

max_d=0

s_point=0


for i in range(n):
	if i==0:
		x=arr[i]-0

	elif i==n-1:
		if arr[0]==0:
			x=k-arr[i]
		else:
			x=arr[i]-arr[0]
	else:
		x=arr[i+1]-arr[i]

	if x>max_d:
		max_d=x
		s_point=(i+1)%n


total=0

j=0

while j<n-1:
	nxt_ind=(s_point+1)%n

	if arr[nxt_ind]==0:
		total+=abs(arr[s_point]-k)
	else:
		total+=abs(arr[s_point]-arr[nxt_ind])
	j+=1

print(total)

Submission status: Wrong Answer

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

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

count=0
for i in range(n-1):
    if l[0]!=0:
        count+=l[n-1]-l[n-2]
        n-=1
print(count)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a += [k]
ans = 1001001001
d = []

for i in range(1, len(a)):
    d.append(abs(a[i]-a[i-1]))
#print(d*2)
d *= 2
for i in range(len(d)-2):
    ans = min(ans, sum(d[i:i+2]))
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a += [k-a[-1]]
a = a*2
ans = 1001001001
d = [0 for _ in range(len(a))]

for i in range(1, len(a)):
    d[i] = abs(a[i]-a[i-1])
for i in range(len(d)-2):
    ans = min(ans, sum(d[i:i+3]))
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if i == 0:
        base = A[i]
    else:
        ans += min(A[i]-base,K-A[i])

print(ans)

Submission status: Wrong Answer

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

A.append(K)

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

result = sum(dist[:-1])

print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
for i in range(1,n):
    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を1直線状のリストと考える
a.append(k+a[0])
#k+a[0]はkを超えた後のa[0]
print("k+a[0]:",k+a[0])
d = 0

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

Submission status: Wrong Answer

Solution code:
def resolve():
    k, n = map(int, input().split())
    a_list = list(map(int, input().split()))
    total = 0
    test = a_list[:-1]
    for i in range(n-2):
        total += (test[i+1] - test[i])
    if a_list[1]-a_list[0] < k-a_list[-1]+a_list[0]:
        total += a_list[-1]-a_list[-2]
    else:
        total += (k-a_list[-1]) + a_list[0]
    print(total)
resolve()

Submission status: Wrong Answer

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

A.append(A[0]+K)
print(max(abs(b-a) for a, b in zip(A, A[1:])))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
Nlist = list(map(int, input().split()))
ans = min(Nlist[N-1]-Nlist[0], Nlist[0]+K-Nlist[N-1])
sa = Nlist[0]
for i in range(1, N):
    if Nlist[i]-sa > ans:
        ans = Nlist[i]-sa
    sa = Nlist[i]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
res = min(K - A[-1] + A[-2] - A[0], A[-1] - A[0])
print(res)

Submission status: Wrong Answer

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

max_dis = 0
for i in range(len(A)-1):
  max_dis = max(max_dis, A[i+1]-A[i])

print(K-max_dis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(sum(sorted(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(len(A) - 1):
    B.append(A[i + 1] - A[i])
B.append(K - A[-1])

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

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

Submission status: Wrong Answer

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

An_list.append(An_list[0]+K)
print(An_list)

distance=[0]*N

for i in range(0,N):
    distance[int(i)]=An_list[int(i+1)]-An_list[int(i)]

distance.sort()
del distance[-1]
print(distance)
print(sum(distance))

Submission status: Wrong Answer

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

distance=[0]*N

for i in range(0,N):
    distance[int(i)]=An_list[int(i+1)]-An_list[int(i)]

distance.sort()
del distance[-1]
#print(distance)
print(sum(distance))

Submission status: Wrong Answer

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

M = 0
for i in range(-1, N-1):
  dis = abs(A[i] - A[i + 1])
  if 2*dis > K:
    dis = K - dis
  print(dis)
  if dis > M:
    M = dis
    
print(K-M)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i<n-1:
            dist.append(a[i+1]-a[i])
        else:
            dist.append(k-a[i]+a[0])
    print(dist)
    x = max(dist)
    print(k-x)
    return

if __name__=='__main__':
    Main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i<n-1:
            dist.append(a[i+1]-a[i])
        else:
            dist.append((a[i]-a[0])%(k//2+1))
    midx = dist.index(max(dist))
    print(abs(a[midx]-a[(midx+1)%n]))
    return

if __name__=='__main__':
    Main()

Submission status: Wrong Answer

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

path = 0

for i in range(n):
    if i == 0:
        path += min(abs(A[i]-A[n-1]),abs(A[i]-A[i+1]))
    elif i == n-1:
        path += min(abs(A[i]-A[i-1]),abs(A[i]-A[0]))
    else:
        path += min(abs(A[i]-A[i+1]),abs(A[i]-A[i-1]))

print(path)

Submission status: Wrong Answer

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

path = 0

for i in range(n-1):
    if i == 0:
        path += min(abs(A[i]-A[n-1]),abs(A[i]-A[i+1]))
    else:
        path += min(abs(A[i]-A[i+1]),abs(A[i]-A[i-1]))

print(path)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A=list(map(int, input().split()))
A.sort()
sub = []
mi=0
m=0
for i in range(N):
    if i==N-1:
        if m <= K+A[0]-A[i]:
            m=K+A[0]-A[i]
            mi=i
        #sub.append(N+A[0]-A[i])
    else:
        if m <= A[i+1]-A[i]:
            m=A[i+1]-A[i]
            mi=i
        #sub.append(A[i+1]-A[i])
if mi==N-1:
    print(abs(K-A[mi]+A[0]))
else:
    print(abs(A[mi]-A[mi+1]))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A=list(map(int, input().split()))
A.sort()
sub = []
mi=0
m=0
for i in range(N):
    if i==N-1:
        if m <= K+A[0]-A[i]:
            m=K+A[0]-A[i]
            mi=i
        #sub.append(N+A[0]-A[i])
    else:
        if m <= A[i+1]-A[i]:
            m=A[i+1]-A[i]
            mi=i
        #sub.append(A[i+1]-A[i])
if mi==N-1:
    print(abs(A[mi]-A[0]))
else:
    print(abs(A[mi]-A[mi+1]))

Submission status: Wrong Answer

Solution code:
K,N=map(int, input().split()) 
c=list(map(int, input().split())) 
a = 0
b = 0
for i in range(N):
  print(a)
  if i == 0:
    a = min((c[N-1]-c[0]),K-(c[i+1]-c[0]))
  elif i == N-1:
    a = min(a,K-(c[i-1]-c[i]))
    a = min(a,K-(c[0]-c[i]))
    
  else:
    a = min(a,K-(c[i-1]-c[i]))
    a = min(a,K-(c[i+1]-c[i]))
    
print(a)

Submission status: Wrong Answer

Solution code:
def main():
    k, _ = map(int, input().split())
    a = list(map(int, input().split()))
    a.append(k)
    before = a[0]
    new = []
    for i in a:
        new.append(i - before)
        before = i
    print(new)
    print(sum(new) - max(new))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    k, _ = map(int, input().split())
    a = list(map(int, input().split()))
    a.append(k)
    before = a[0]
    new = []
    for i in a[1:]:
        new.append(i - before)
        before = i
    print(new)
    print(sum(new) - max(new))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    a.append(k)
    before = a[0]
    new = []
    for i in a[1:]:
        new.append(i - before)
        before = i
    print(sum(new) - max(new))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    before = a[0]
    new = []
    for i in a[1:]:
        new.append(i - before)
        before = i
    new.append(k - before)
    print(sum(new) - max(new))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

count = []

for i in range(n):
  if a[i]-a[i-1]<0:
    count.append(20+(a[i]-a[i-1]))
  else:
    count.append(20-(a[i]-a[i-1]))
print(min(count))

Submission status: Wrong Answer

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

count = []

for i in range(n):
  if a[i]-a[i-1]<0:
    count.append(20-(a[i]-a[i-1]+20))
  else:
    count.append(20-(a[i]-a[i-1]))
print(min(count))

Submission status: Wrong Answer

Solution code:
def resolve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = [max(A)-min(A)]
    for i in range(N-1):
        B.append(A[i+1]-A[i])
    for j in B:
        if j > K/2:
            B.remove(j)
            B.append(20-j)
    print(K-max(B))
resolve()

Submission status: Wrong Answer

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

count = []

for i in range(n):
  if (a[i-1]-a[i])<0:
    count.append(a[i-1]-a[i]+20)
  else:
    count.append(a[i-1]-a[i])
print(min(count))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
k, n, *a = map(int, open(0).read().split())
a.append(k)
e = []
for i in range(n):
    e.append(a[i+1]-a[i])
print(sum(e)-max(e))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

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

print(dist)

Submission status: Wrong Answer

Solution code:


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

a=list(map(int,input().split()))
a.sort()
l=0
r=0
t=a[n-1]
ans=0

for i in a[:n-1]:
    right=t-i
    left=k-right
    tl=0
    tr=0
    if left>l:
        tl=left-l
    if right>r:
        tr=right-r

    if ans+tl<ans+tr:
        l=left
        ans=ans+tl
    else:
        r=right
        ans=ans+tr

print (ans)

Submission status: Wrong Answer

Solution code:


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

a=list(map(int,input().split()))
l=0
r=0
t=a[n-1]
ans=0

for i in a[:n-1]:
    right=t-i
    left=k-right
    tl=0
    tr=0
    if left>l:
        tl=left-l
    if right>r:
        tr=right-r

    if ans+tl<ans+tr:
        l=left
        ans=ans+tl
    else:
        r=right
        ans=ans+tr

print (ans)

Submission status: Wrong Answer

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

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

route.append(route[0]+K-route[-1])
route.remove(max(route))
print(sum(route)-route[0]*N)

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]!=0:
        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()))

a += [a[0]+k]
max_len = 0

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

Submission status: Wrong Answer

Solution code:
N = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

distanceList = []

distance = 0
for i in range(N[1]):
    M.append((M[i] + N[0]))

for i in range(len(M) - 1):
    distanceList.append(M[i + 1] - M[i])

print(distanceList)
print(N[0] - max(distanceList))

Submission status: Wrong Answer

Solution code:


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

a=list(map(int,input().split()))
l=0
r=0
t=a[n-1]

for i in a[:n-1]:
    x=t-i
    y=k-x
    if x<=y :
        if x>r:
            r=x
    else:
        if y>l:
            l=y
    #print(l+r)

print(l+r)

Submission status: Wrong Answer

Solution code:


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

a=list(map(int,input().split()))
ans=max(a)-min(a)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def resolve():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()
    ans = 0
    if 0 in A:
        ans = A[-1] - A[N // 2]
    else:
        ans = A[-1] - A[0]

    print(ans)
    return

resolve()

Submission status: Wrong Answer

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

print(a)
print(k-max(dist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans1 = A[-1] - A[0]
ans2 = (K - A[-1]) + A[-2]
ans3 = A[0] + (K - A[1]) 
ans = min(ans1, ans2, ans3)
print(ans)

Submission status: Wrong Answer

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

ans1 = A[-1] - A[0]
ans2 = (K - A[-1]) + A[0] + A[-2]
ans = min(ans1, ans2)
print(ans)

Submission status: Wrong Answer

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

ans1 = A[-1] - A[0]
ans2 = (K - A[-1]) + A[-2]
ans = min(ans1, ans2)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
A = list(map(int, input().split()))
d = []
for i in range(len(A)):
  if i == len(A)-1:
    r = a-A[i]+A[0]
    d.append(r)
  else:
    d.append(A[i+1]-A[i])
print(20-max(d))

Submission status: Wrong Answer

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