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 = [int(e) for e in input().split(" ")]

A = [int(e) for e in input().split(" ")]

A.sort(reverse = True)

sum1 = 0
tmp = 0

if A[len(A) - 1] == 0:
    del A[len(A) - 1]

for i in range(len(A) - 1):
    tmp =  A[i] - A[i + 1]
    sum1 = sum1 + tmp
    
print(sum1)

Submission status: Wrong Answer

Solution code:
a = input()
b = input()
a = a.split()
K = int(a[0])
N = int(a[1])
b = b.split()
if b[0] == 0:
  c = int(b[len(b) - 1]) - int(b[1])
  d = (K - int(int(b[len(b) - 1]))) + (int(b[len(b) - 2]) - int(b[1]))
  if c >= d:
    print(d)
  else:
    print(c)

else:
  c = int(b[len(b) - 1]) - int(b[0])
  d = (K - int(int(b[len(b) - 1]))) + (int(b[len(b) - 2]) - int(b[0]))
  if c >= d:
    print(d)
  else:
    print(c)

Submission status: Wrong Answer

Solution code:
E = [input() for i in range(2)]
E0 = E[0].split()
K = int(E0[0])
N = int(E0[1])
E1 = E[1].split()
E1.append(E0[0])
d = 0
M = 0
gap = []
for k in range(len(E1)-1):
    gap.append(int(E1[k+1]) - int(E1[k]))
    d += gap[k]
    M = max(M, gap[k])

d -= M

print(d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(len(list_point)):
  list_point[i] = int(list_point[i])

#点と点との距離を記録
distance = []
for i in range(len(list_point)):
  
  if i == 0:
    distance.append(list_point[i])
  
  if i == len(list_point)-1:
    distance.append(N - list_point[i])
    break
  distance.append(list_point[i+1]-list_point[i])
  
distance.sort()
print(distance)
answer = []
for i in range(N_point):
  answer.append(distance[i])

print(sum(answer))

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
A = input().split()
for i in range(N):
    A[i] = int(A[i])
max = 0
for i in range(N-1):
    diff = abs(A[i] - A[i+1])
    if diff >= max:
        max = diff

if abs(K - A[N-1]) >= max:
    max = abs(K - A[N-1])

print(K - max)

Submission status: Wrong Answer

Solution code:
line = input().split(' ')
K = int(line[0])
N = int(line[1])
A = input().split(' ')
sum = 0
endDistance = K - int(A[N-1])
end2Distance = int(A[N-1]) - int(A[N-2])

if endDistance < end2Distance:
    sum += endDistance
    sum += int(A[0])
    for i in range(len(A)-2):
        sum += (int(A[i+1])-int(A[i]))
else:
    for i in range(len(A)-1):
        sum += (int(A[i+1])-int(A[i]))
#print(K, N, A)
#print(endDistance, end2Distance)
print(sum)

Submission status: Wrong Answer

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

distance = []
diff = 0

for i in range(0, N):
    
    if i == N-1:
        diff = K + A_list[i]
        distance.append(diff)
        
    else:
        diff = A_list[i+1] - A_list[i]
        distance.append(diff)
        

print(sum(distance) - max(distance))

Submission status: Wrong Answer

Solution code:


def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right


if __name__ == "__main__":
    k,n = list((map(int, input().split(' '))))
    a = list(map(int, input().split(' ')))
    result = 0
    for i in range(1,n):
        length = a[i]-a[i-1]
        result = max(length, result)
        
    length = a[0] + (k-a[n-1])
    result = max(result, length)
    print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
aaa = [int(i) for i in input().split()]

aaa.append(aaa[0] + a)

result = [c1 - c0 for c0,c1 in zip(aaa,aaa[0:])]

print(a - max(result))

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
tab = [int(x) for x in input().split()]
tab.sort()
dist=-tab[0]+tab[n-1]
print(dist)

Submission status: Wrong Answer

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

A.append(K - A[-1])

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

#print(answer)
for j in range(len(A)-N+1):
  temp = sum(A[j:j+N-1])
  #print(temp)
  if temp < answer:
    answer = temp

print(answer)

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])
ans=max(ans,k-a[-1])
print(k-ans)

Submission status: Wrong Answer

Solution code:
k,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
a.append(k)
l=[]
for i in range(len(a)-1):
    s = a[i+1]-a[i]
    if s<0:
        s=-s
    l.append(s)
max_value=max(l)
print(k-max_value)

Submission status: Wrong Answer

Solution code:
K=int(input().split()[0])
lst=list(map(int,input().split()))
lst.append(K-sum(lst))
print(K-min(lst))

Submission status: Wrong Answer

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

if A[-1] != K:
    A.append(K)

maxdist,maxindx = 0,0
for i,a in enumerate(A):
    dist = -a
    if i+1 == len(A):
        dist += K
    else:
        dist += A[i+1]
    if dist > maxdist:
        maxdist = dist
        maxindx = i
ans = 0
for x,a in enumerate(A):
    if x == maxindx:
        continue
    if x+1 == len(A):
        ans += K-a
    else:
        ans += A[x+1]-a
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
new = []
for i in range(N-1):
    new.append(A[i+1]-A[i])
new.append(K + A[0] - A[-1])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

max = 0
for i in range(1, N):
    if max < (A[i] - A[i-1]):
        max = A[i] - A[i-1]
if (max < A[N-1] - A[0]) and ( A[N-1] - A[0]) < K/2:
    max = A[N-1] - A[0]
print(K - max)

Submission status: Wrong Answer

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

max = 0
for i in range(1, N):
    if max < (A[i] - A[i-1]):
        max = A[i] - A[i-1]
if (max < A[N-1] - A[0]) and ( A[N-1] - A[0]) <= K/2:
    max = A[N-1] - A[0]
print(K - max)

Submission status: Wrong Answer

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

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

num = K - check
print(num)

Submission status: Wrong Answer

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

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

num = K - check
print(num)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A1 = sorted([int(a) for a in input().split()])
A2 = sorted([a if a <= N//2 else a-K for a in A1])
dist1, dist2 = 0, 0
for i in range(N-1):
    dist1 += abs(A1[i]-A2[i+1])
    dist2 += abs(A2[i]-A2[i+1])

print(min(dist1, dist2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

relative = []
relative.append(distance[0])

for i in range(int(N)-1):
    relative.append(distance[i+1] - distance[i])

relative.append(int(K) -  distance[-1])

print(sum(relative) - max(relative)- relative[0])

Submission status: Wrong Answer

Solution code:
inputs = [int(i) for i in input().split()]
K,N = inputs[0],inputs[1]

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

max_range = input_lines[0]+(K-input_lines[-1])
for i in range(N):
    if i!=N-1:
        torange = input_lines[i+1]-input_lines[i]
        if torange>max_range:
            max_range = torange
print(max_range)

Submission status: Wrong Answer

Solution code:
k, n = input().split()
a_list_org = list(map(int,input().split()))
a_list_org.sort()
a_list = [a - int(k) if (a > int(k) // 2) else a for a in a_list_org]
a_list.sort()
print(a_list)
ans1 = 0
ans2 = 0
for a in a_list:
    if a == a_list[0]:
        pre = a
        continue
    else:
        ans1 += (a - pre)
        pre = a
for a in a_list_org:
    if a == a_list_org[0]:
        pre = a
        continue
    else:
        ans2 += (a - pre)
        pre = a
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(" ")))
max_dist = 0
for i in range(len(A)-1):
  if A[i+1]-A[i] > max_dist:
    max_dist = A[i+1]-A[i] 
print(k-max_dist)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cal_A = A
cal_A.append(K)
dis_A = []
ans_raw = []
ans_sum = 0

for i in range(0, N):
    a = cal_A[i+1] - cal_A[i]
    dis_A.append(a)

dis_A.sort()

for i in range(0, N-1):
    ans_sum += dis_A[i]

print(str(ans_sum))

Submission status: Wrong Answer

Solution code:
K, N = input().split()
A = list(map(int, input().split()))
print(max(A) - A[-2] if A[0] == 0 else max(A) - min(A))

Submission status: Wrong Answer

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

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 0 <= i < N-1:
    b.append(a[i+1] - a[i])
  if i == N-1:  
    b.append(K - a[i-1] + a[0])
c = []
d = []
for i in range(N+1):
  c = b[:i] + b[i+1:]
  d.append(sum(c))
print(min(d))

Submission status: Wrong Answer

Solution code:
import sys
 
k, n = map(int, sys.stdin.readline().split())
a = [int(i) for i in sys.stdin.readline().split()]
a.sort()
max_dist = 0
for i in range(1, n):
	max_dist = max(a[i] - a[i - 1], max_dist)
    
if len(a) != 0:
	max_dist = max(a[0] - a[-1], max_dist)
sys.stdout.write(str(k - max_dist))

Submission status: Wrong Answer

Solution code:
import sys
 
k, n = map(int, sys.stdin.readline().split())
a = [int(i) for i in sys.stdin.readline().split()]
a.sort()
max_dist = 0
for i in range(1, n):
  if i == n-1:
    max_dist = max(k - a[i - 1] + a[i], max_dist)
  else:
  	max_dist = max(a[i] - a[i - 1], max_dist)
  
sys.stdout.write(str(k - max_dist))

Submission status: Wrong Answer

Solution code:
import sys

k, n = map(int, sys.stdin.readline().split())
a = [int(i) for i in sys.stdin.readline().split()]
a.sort()
max_dist = 0
for i in range(1, n):
  max_dist = max(a[i] - a[i - 1], max_dist)
  
sys.stdout.write(str(k - max_dist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
an = list(map(int, input().split()))
bn = list(map(lambda x: abs(x-k) if x>k/2 else x, an))
print(min((an[-1]-an[0]), (max(bn)+bn[-1])))

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

if (k-a[n-1]) + a[0]:
    max = (k-a[n-1]) + a[0]

print(k-max)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
B=sorted(A)
s=B[N-1]-B[N-2]
t=K-B[N-1]+B[0]
if s<=t:
  m=B[N-1]-B[0]
else:
  m=B[N-2]+K-B[N-1]
print(m)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
B=sorted(A)
s=K-B[N-1]
t=B[N-1]-B[N-2]
if s>t:
  m=B[N-1]-B[0]
else:
  m=B[N-2]+K-B[N-1]
print(m)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
B=sorted(A)
s=K-B[N-1]
t=B[N-1]-B[N-2]
if s>=t:
  m=B[N-1]-B[0]
else:
  m=B[N-2]+K-B[N-1]
print(m)

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
A = list(map(int, input().split()))
shortestDistance = A[-1] - A[0]

if shortestDistance >= K/2:
    for i in range(1, N):
        if shortestDistance > A[i] + K - A[-1]:
            shortestDistance = A[i] + K - A[-1]
        elif shortestDistance > K - A[i] + A[i-1]:
            shortestDistance = K - A[i] + A[i-1]

print(shortestDistance)

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
A = list(map(int, input().split()))
shortestDistance = A[-1] - A[0]

if shortestDistance >= K/2:
    for i in range(1, N):
        if shortestDistance > A[i] + K - A[-1]:
            shortestDistance = A[i] + K - A[-1]

print(shortestDistance)

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
A = list(map(int, input().split()))
shortestDistance = A[-1] - A[0]

if shortestDistance < K/2:
    for i in range(1, N):
        if shortestDistance > A[i] + K - A[-1]:
            shortestDistance = A[i] + K - A[-1]

print(shortestDistance)

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
A = list(map(int, input().split()))
shortestDistance = A[-1] - A[0]

if shortestDistance > K/2:
    for i in range(1, N):
        if shortestDistance > A[i] + K - A[-1]:
            shortestDistance = A[i] + K - A[-1]

print(shortestDistance)

Submission status: Wrong Answer

Solution code:
import numpy as np

input1=list(map(int,input().split()))
input2=list(map(int, input().split()))

distance=[]
for order,value in enumerate(input2):
    if order==0:
        pass
    else:
        distance.append(value-input2[order-1])


distance.append(input1[0]-np.array(distance).sum())

Submission status: Wrong Answer

Solution code:
s=input().split()
k,n=int(s[0]),int(s[1])
s=[]
a=input().split()

for i in range(0,n):
  
  if(i == n-1):
    s.append(abs(int(a[i])+k-int(a[0])))
  else:
  	s.append(abs(int(a[i])-int(a[i+1])))
print(k-max(s))

Submission status: Wrong Answer

Solution code:
s=input().split()
k,n=int(s[0]),int(s[1])
s=[]
a=input().split()

for i in range(0,n):
  
  if(i == n-1):
    s.append(abs(int(a[i])-int(a[0])))
  else:
  	s.append(abs(int(a[i])-int(a[i+1])))
print(k-max(s))

Submission status: Wrong Answer

Solution code:

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


a = [i for i in A if i != 0]

last = K + a[0]
a.append(last)

max_distance = 0
pre = 0

for i in a:

    if abs(i - pre) > max_distance:
        max_distance = abs(i - pre)
    pre = i



print (max_distance)

Submission status: Wrong Answer

Solution code:

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


a = [i for i in A if i != 0]

last = K + a[0]
a.append(last)

max_distance = 0
pre = 0

for i in a:

    if abs(i - pre) > max_distance:
        max_distance = abs(i - pre)
    pre = i



print (K - max_distance)

Submission status: Wrong Answer

Solution code:

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

try:
    A.remove(0)
except ValueError:
    pass

last = A[-1] + (K - A[-1]) + A[0]
A.append(last)

max_distance = 0
pre = 0

for i in A:

    if abs(i - pre) > max_distance:
        max_distance = abs(i - pre)
    pre = i



print (K - max_distance)

Submission status: Wrong Answer

Solution code:

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

try:
    A.remove(0)
except ValueError:
    pass

A.append(A[0])

max_distance = 0
pre = 0

for i in A:

    if abs(i - pre) > max_distance:
        max_distance = abs(i - pre)
    pre = i



print (K - max_distance)

Submission status: Wrong Answer

Solution code:
def C():
    kn=list(map(int,input().split()))
    A=list(map(int,input().split()))
    sum=0
    d=[]
    sum_ans=[]
  
    
    for i in range(kn[1]):
        
        if i==kn[1]-1:
            d.append(kn[0]-A[i])
        else:
            d.append(A[i+1]-A[i])
        
    
    for i in range(1,kn[1]):
        sum_ans.append(min(kn[0]-d[i],kn[0]-d[i-1]))
    
    print(min(sum_ans))    
        
    
if __name__=='__main__':
    C()

Submission status: Wrong Answer

Solution code:
def C():
    kn=list(map(int,input().split()))
    A=list(map(int,input().split()))
    sum=0
    d=[]
    sum_ans=[]
  
    
    for i in range(kn[1]):
        
        if i==kn[1]-1:
            sum+=kn[0]-A[i]
            d.append(kn[0]-A[i])
        else:
            sum+=A[i+1]-A[i]
            d.append(A[i+1]-A[i])
        
    
    for i in range(1,kn[1]):
        sum_ans.append(min(sum-d[i],sum-d[i-1]))
    
    print(min(sum_ans))    
        
    
if __name__=='__main__':
    C()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(max)

Submission status: Wrong Answer

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

print(max)

Submission status: Wrong Answer

Solution code:
K, N, *A = map(int, open(0).read().split())

A.append(K)

B = [b - a for a, b in zip(A, A[1:])]
print(sum(B) - max(B))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18
for i in range(n):
  dist1=(k-arr[i])%k+arr[i-1]
  dist2=arr[i]+(k-arr[(i+1)%n])%k
  ans=min(ans,dist1,dist2)
print(ans)

Submission status: Wrong Answer

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

len = 0

for x in range(N-1):
  if house[x+1] - house[x] > len:
    len = house[x+1] - house[x]
if K - house[N-1] > len:
  len = K - house[N-1]  

print(K-len)

Submission status: Wrong Answer

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

len = 0

for x in range(N-1):
  if house[x+1] - house[x] > len:
    len = house[x+1] - house[x]
print(K-len)

Submission status: Wrong Answer

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

dist = 0

for i in range(len(arr)):
    if i is 0:
        if k - arr[i-1] > dist:
            dist = k - arr[i-1]
    
    if arr[i] - arr[i-1] > dist:
        dist = arr[i] - arr[i-1]

print(k - dist)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(k)
A.sort()
dist = np.diff(A)
ans = k-max(dist)
print(ans)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(k)
A.sort()
m = max(y-x for x,y in zip(A,A[1:]))
ans = k-m
print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(k)
A.sort()
dist = np.diff(A)
dist.sort()
print(sum(dist[:n-1]))

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(k)
dist = np.diff(A)
dist.sort()
print(sum(dist[:n-1]))

Submission status: Wrong Answer

Solution code:
import math
miji = input().split(" ")
K = int(miji[0])
N = int(miji[1])
posilist = input().split(" ")
for i in range(0,N):
    posilist[i] = int(posilist[i])
posilist.sort()

if posilist[N-1] - posilist[0] < K - posilist[0]:
    R = posilist[N-1] - posilist[0]
else:
    R = K - posilist[0]

if R > posilist[0] + K - posilist[N - 1]:
    R = posilist[0] + K - posilist[N - 1]
print(R)

Submission status: Wrong Answer

Solution code:
import math
miji = input().split(" ")
K = int(miji[0])
N = int(miji[1])
posilist = input().split(" ")
for i in range(0,N):
    posilist[i] = int(posilist[i])
posilist.sort()

if posilist[N-1] - posilist[0] < K - posilist[0]:
    R = posilist[N-1] - posilist[0]
else:
    R = K - posilist[0]
if R > posilist[0] + 20 - posilist[N - 1]:
    R = posilist[0] + 20 - posilist[N - 1]
print(R)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

locations = input().split()
dists = []
prev_location = None

for location in locations:
  if prev_location is None:
    prev_location = int(location)
  else:
	  dists.append(int(location) - prev_location)  
	  prev_location = int(location)
dists.append(N - int(locations[-1]))

# print(dists)
print(N - np.array(dists).max())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
miji = input().split(" ")
K = int(miji[0])
N = int(miji[1])
posilist = input().split(" ")
for i in range(0,N):
    posilist[i] = int(posilist[i])
posilist.sort()
if posilist[N-1] - posilist[0] < K - posilist[0]:
    print(posilist[N-1] - posilist[0])
else:
    print(K - posilist[0])

Submission status: Wrong Answer

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

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

for i in range(1, n - 1):
    if a[i] - a[i -1] > r:
        r = a[i] - a[i -1]

print(k - r)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, N = map(int,input().split())
A = list(map(int, input().split()))
distance = []
for i in range(N):
    if i < N-1:
        distance.append(A[N-1]+A[0])
        distance.append(X-A[i+1]+A[i])
    else:
        distance.append(X-A[N-1]+A[0])
        distance.append(A[N-1]-A[0])
print(min(distance))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
arr=list(map(int,input().split()))
arr.append(arr[0])
ans=10**18
for i in range(n):
  dist1=(k-arr[i])%k+arr[i-1]
  dist2=arr[i]+(k-arr[i+1])%k
  ans=min(ans,dist1,dist2)
print(ans)

Submission status: Wrong Answer

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

dist_right = max(A)-min(A)
dist_left = X-dist_right

print(min(dist_left, dist_right))

Submission status: Wrong Answer

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

max_dist = -1
sum_dist = 0
for i in range(len(a_list)-1):
    dist = abs(a_list[i+1] - a_list[i])
    if max_dist < dist:
        max_dist = dist
    sum_dist += dist
sum_dist -= max_dist
print(sum_dist)

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.append(k)
diff = list(np.diff(a_list))
# print(diff)
if len(diff) <= 2:
    print(max(diff))
else:
    diff.remove(max(diff))
    print(sum(diff))

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.append(k)
diff = list(np.diff(a_list))
diff.remove(max(diff))
print(sum(diff))

Submission status: Wrong Answer

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


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

print(min(B))

Submission status: Wrong Answer

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


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

print(min(B))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


A=np.array(A)

lin=np.array(A[:-1])
le=np.array(A[1:])

length=abs(lin-le)

rem=abs(K-A[-1]+A[0])
print(length)
A=np.append(length,rem)

M=np.max(A)

print(K-M)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

# A=list(map(int, input().split()))
K,N=[20, 4]

A=[5, 10, 15]

A=np.array(A)

lin=np.array(A[:-1])
le=np.array(A[1:])

length=lin-le

rem=abs(K-A[-1]+A[0])

A=np.append(length,rem)

M=np.max(A)

print(K-M)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

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


main()

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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