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:
#! env/bin/local python3
# -*- coding: utf-8 -*-

k, n = list(map(int, input().split()))
distances = list(map(int, input().split()))
related_distances = [
    d - distances[0]
    if d - distances[0] <= k/2 else k//2 - (d - distances[0])
    for d in distances]


print(min(max(related_distances) - min(related_distances), max(distances) - min(distances)))

Submission status: Wrong Answer

Solution code:
#! env/bin/local python3
# -*- coding: utf-8 -*-

k, n = list(map(int, input().split()))
distances = list(map(int, input().split()))
related_distances = [
    d - distances[0]
    if d - distances[0] <= k/2 else k//2 - (d - distances[0])
    for d in distances]


print(max(related_distances) - min(related_distances))

Submission status: Wrong Answer

Solution code:
#! env/bin/local python3
# -*- coding: utf-8 -*-

k, n = map(int, input().split())
distances = sorted(map(int, input().split()), reverse=True)

candidate = max(distances) - min(distances)
if candidate > k//2:
    another_candidate = distances[1] - (k//2 - distances[0])
    print(another_candidate if another_candidate < candidate else candidate)
else:
    print(candidate)

Submission status: Wrong Answer

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

res = []
for i in range(N-1):
    res.append(A[i+1] - A[i])
    if i+1 == N-1:
        res.append(K - A[i+1])
print(sum(res) - max(res))

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
list1 = list(map(int,input().split()))
pre_num = None
res = 0
for num in list1:
  if pre_num is None:
    res = max(res, num-(K-list1[-1]))
    pre_mum = num
    continue
  res = max(res, num-pre_num)
  pre_num = num
print(K-res)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
a = [int(x) for x in input().split()]
k=0
j=0
dis=[]
DIS=[]
for k in range(N):
    p=a[k]
    del a[k]
    for j in range(N-1):
        dis.append(abs(p-a[j]))
    a.insert(k, p)
    DIS.append(max(dis))
    dis=[]
print(min(DIS))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A.append(A[0]+K)
maxD = 0

for n in range(0, N):
  maxD = max(maxD, A[n+1] - A[n])
  
print(maxD)

Submission status: Wrong Answer

Solution code:
inp  = list(map(int, input().strip().split()))
array = list(map(int, input().strip().split()))

ans = array.copy()
for i in range(1, inp[1]):
    ans.insert(0, array[i] - inp[0])
ans.sort()

dif = []
for i in range(0, len(ans)-1):
    dif.append(ans[i+1]-ans[i])
dif.sort()
a = dif[:inp[1]-1]
print(sum(a))

Submission status: Wrong Answer

Solution code:
def check(a):
  if a < 0:
    return a * -1
  else:
    return a

def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = []
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r.append(check(K - As[x-1]))
      continue
    elif As[x-1] == 0:
      r.append(check(As[x]))
      continue
    solve = As[x] - As[x-1]
    r.append(check(solve))
  ans = sorted(r)
  print(K-ans[-1])

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def check(a):
  if a < 0:
    return a * -1
  else:
    return a

def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = []
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r.append(check(K - As[x-1]))
      continue
    elif As[x-1] == 0:
      r.append(check(As[x]))
      continue
    solve = As[x] - As[x-1]
    r.append(check(solve))
  ans = sorted(r)
  ans.remove(ans[-1])
  sum_ans = sum(ans)
  print(sum_ans)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def check(a):
  if a < 0:
    return int(a * -1)
  else:
    return a

def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = []
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r.append(check(K - As[x-1]))
      continue
    elif As[x-1] == 0:
      r.append(check(As[x]))
      continue
    solve = int(As[x] - As[x-1])
    r.append(check(solve))
  ans = sorted(r)
  ans.remove(ans[-1])
  sum_ans = sum(ans)
  print(sum_ans)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = 0
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r = max(As) - sorted(As)[-2]
      break
    solve = int(As[x] - As[x-1])
    if 0 <= solve:
      r += solve
  print(r)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = 0
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r = max(As) - sorted(As)[-2]
      break
    solve = int(As[x] - As[x-1])
    if 0 <= solve:
      r += solve
    print(As[x], As[x-1], solve)
  print(r)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = 0
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      As[x] = K
    elif As[x-1] == 0:
      As[x-1] = K
    solve = int(As[x] - As[x-1])
    if 0 <= solve:
      r += solve
  print(r)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

x=0
for i in range(n-1):
 p=a[i+1]-a[i]
 if p >= x:
  x=p

X=max(x,k-a[n-1]-a[0])
print(k-X)

Submission status: Wrong Answer

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

d = [None]*n

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

d[n-1] = k - a[n-1] + d[0]

print(k - max(d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = 0
b = A[-1] - A[0]
c = K - A[-1]
for i in range(N - 1):
    if A[0] == 0:
        a = A[i + 1] - A[i]
        c = max(a, c)
        ans = K - c
    else:
        a = A[i + 1] - A[i]
        b = max(a, b)
        ans = K - b
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x=0
for i in range(n-1):
 p=a[i+1]-a[i]
 if p >= x:
  x=p

X=max(x,a[n-1]-a[0])
print(k-X)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
K,N=map(int, input().split())
A=list(map(int, input().split()))
d=[0]*(N+1)
for n in range(N+1):
    if n==0:
        d[n]=A[0]
    elif n==N:
        d[n]=K-A[n-1]
    else:
        d[n]=A[n]-A[n-1]
if d[0]!=0:
    d=d[1:]
print(sum(d)-max(d))

Submission status: Wrong Answer

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

for i in range(N):
  if i == 0:
    distance = int(A[N-1]) - int(A[0]) 
  else:  
    distance = int(A[i]) - int(A[i-1]) 
  print(K-distance)

Submission status: Wrong Answer

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

for i in range(N):
  if i == 0:
    distance = int(A[N-1]) - int(A[0]) 
  else:  
    distance = int(A[i]) - int(A[i-1]) 
  print(distance)

Submission status: Wrong Answer

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

A.sort()

x = 0
for i in range(N-1): #Nにi+1を入れるとはみ出るからN-1
    if x < A[i+1] - A[i]:
        x = A[i+1] - A[i]

if x < A[0] + (K - A[N-1]):
    x = A[0] + (K - A[N-1])

print(x)

Submission status: Wrong Answer

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

k2 = k/2
si = 0
d = 0
for i in range(len(a)-1):
    l = abs(a[i-1]-a[i])
    r = abs(a[i]-a[i+1])
    if l>k2:
        l -= k2
    if r>k2:
        r -= k2
    if l>r and l>d:
        si = i
        d = l
    elif l<=r and r>d:
        si = i+1
        d = r
a1 = a[si:]
a2 = a[0:si]
a = a1+a2
s = 0
for i in range(len(a)-1):
    d = abs(a[i]-a[i+1])
    if d>k2:
        d -= k2
    s += d
print(s)

Submission status: Wrong Answer

Solution code:
import math

k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(k)
ans = 0
for i in range(0, n):
    ans = max(ans, abs(A[i] - A[i+1]))

print(k-ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = [i for i in a if i > k/2]
c = [i for i in a if i <= k/2]

if max(a) - min(a) <= k/2:
    print(max(a)-min(a))
else:
    print(k-min(b)+max(c))

Submission status: Wrong Answer

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a1 = np.zeros(1, dtype=int)
a1 = np.append(a1, a)
a2 = a
a2 = np.append(a2, a[0]+k)
print(max(a2[1:] - a1[1:]))

Submission status: Wrong Answer

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a1 = np.zeros(1, dtype=int)
a1 = np.append(a1, a)
a2 = a
a2 = np.append(a2, a[0]+k)
print(max(a2 - a1))

Submission status: Wrong Answer

Solution code:
k,n = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
    if a[i] <= k/2:
        b.append(a[i])
    else:
        b.append(a[i]-k)
c = max(a)-min(a)
d = abs(min(b))+max(b)
if c >= d:
    print(d)
else:
    print(c)

Submission status: Wrong Answer

Solution code:
import re
K,W = map(int,input().split())
#N = str(input())
#K = int(input())
A = list(map(int,input().split()))
a=[20-A[-1]+A[0]]
for i in range(W-1):
    l = abs(A[i]-A[i+1])
    a.append(l)

a.remove(max(a))
print(sum(a))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

a = [int(i) for i in a]

diff = list()
diff.insert(0,K-a[N-1]+a[0]) # seq 1
for i in range(2,N+1):       # seq 2 to N-1
  diff.insert(i-1,abs(a[i-1]-a[i-2]))

print (diff)

print (sum(diff)-max(diff))

Submission status: Wrong Answer

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

a = [int(i) for i in a]

diff = list()
diff.insert(0,K-a[N-1]) # seq 1
for i in range(2,N+1):  # seq 2 to N-1
  diff.insert(i-1,abs(a[i-1]-a[i-2]))

print (sum(diff)-max(diff))

Submission status: Wrong Answer

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

ans = [20 + A[0] - A[-1]]

for i in range(1, N):
    ans.append(A[i] - A[i - 1])

print(sum(ans) - max(ans))

Submission status: Wrong Answer

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

R = A[N-1] - A[0]

for i in range(1, N-1):
  S = A[i]
  E = A[i-1]
  
  R1 = A[N-1] - S
  R2 = K - A[N-1] + E
  
  R3 = R1 + R2
  
  if R3 < R:
    R = R3

print(R)

Submission status: Wrong Answer

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

K1 = A[N-1] - A[0]
K2 = (K - A[N-1]) + A[N-2]

if K1 < K2:
  print(K1)
else:
  print(K2)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a = list(map(int,input().split()))
a_l = 0
a_s = 0
a_g = 0
for i in range(n):
    if i < n -1:
        a_short = min(abs(k-abs(a[i]-a[i+1])),abs(a[i]-a[i+1]))
        if a_l < a_short:
            a_l = a_short
            a_s = i
            a_g = i+1
    else:
        a_short = min(abs(k-abs(a[0]-a[n-1])),abs(a[0]-a[n-1]))
        if a_l < a_short:
            a_l = a_short
            a_s = 0
            a_g = n-1
print(min(abs(k-abs(a[a_s]-a[a_g])),abs(a[a_s]-a[a_g])))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    *N, = map(int, open(0).read().split())
    A = N[2:]
    K = N[0]
    N = N[1]

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

main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    *N, = map(int, open(0).read().split())
    A = N[2:]
    K = N[0]
    N = N[1]

    S = []
    for i in range(0, N):
        if i == 0:
            S.append(A[0] + 20 - A[N - 1])
        else:
            S.append(A[i] - A[i - 1])
    
    if S[0] > max(S[1:]):
        print(sum(S[1:]))
    else:
        print(sum(S[:N - 1]))

main()

Submission status: Wrong Answer

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

B = sorted([(A[i+1]- A[i]) for i in range(N)])
ans = K - B[-1]
print(ans)

Submission status: Wrong Answer

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

B = sorted([(A[i+1]- A[i]) for i in range(N+1)])
ans = K - B[-1]
print(ans)

Submission status: Wrong Answer

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

B = sorted([(A[i+1]- A[i]) for i in range(N)])
ans = K - B[-1]
print(ans)

Submission status: Wrong Answer

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

A.sort()

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

print(K - tmp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

k, n = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
d = max(max([abs(a[x] - a[x - 1]) for x in range(len(a)) if x > 0]), abs(a[0] + k - a[len(a)-1]))

print(d)

Submission status: Wrong Answer

Solution code:
from sys import stdin

k, n = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
d = max(max([a[x] - a[x - 1] for x in range(len(a)) if x > 0]), a[0] + k - a[len(a)-1])

print(d)

Submission status: Wrong Answer

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

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

print(sum(B)-max(B))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
for i in range(a[1]):
    if i==a[1]-1:
        if b[0]==0:
            b[0]=a[0]
            c.append(abs(b[i]-b[0]))
        else:
            c.append(abs(b[i]-b[0]))
    elif i!=a[1]-1:
        c.append(abs(b[i]-b[i+1]))
print(sum(c)-max(c))

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
for i in range(a[1]):
    if i==a[1]-1:
        if b[0]==0:
            b[0]=a[0]
            c.append(abs(b[i]-b[0]))
        else:
            c.append(abs(b[i]-a[0]))
    elif i!=a[1]-1:
        c.append(abs(b[i]-b[i+1]))
print(sum(c)-max(c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

D = [0] * n
for i in range(len(A)-1):
    D[i] += A[i+1]-A[i]

a = max(D)
b = A[0] + k - A[n-1]
print(max(a,b))

Submission status: Wrong Answer

Solution code:
k,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
 
sorted(sol)
#unwanted segment of max length removed while printing
print(sum(sol[0:n-1]))

Submission status: Wrong Answer

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

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

max = max(length)
sum1 = sum(length) #内側の合計

if A[0] == 0:
    length.append(K-A[N-1])
else:
    length.append(A[N-1]-A[0])

sum2 = sum(length)-max #外側を通るときの合計

if sum1 >= sum2:
    print(sum2)
else:
    print(sum1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
b=sorted(map(int,input().split()))
c=[0]*n
for i in range(n-1):
    c[i]=b[i+1]-b[i]
c[n-1]=b[0]+k-b[n-1]
d=max(c)
print(d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    temp = A[i+1]-A[i]
    ans  = max(ans, temp)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=map(int, input().split())
As=[int(i) for i in input().split()]
ans=0
for num in range(1,len(As)-1):
    ans+=As[num+1]-As[num]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))
A_dist = [0]*N

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

print(A_dist)
A_dist_max = max(A_dist)
print(K-A_dist_max)

Submission status: Wrong Answer

Solution code:
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
ans = 0
if(nums[-1] > N[0]//2):
    Max = (N[0]-nums[-1]) + nums[0]
    t = 0
else:
    Max = nums[-1] - nums[0]
    t = 0
for i in range(N[1]-1):
    if(nums[i+1] - nums[i])>Max:
        Max = nums[i+1] - nums[i]
        t = i+1
for k  in range(0,t-1):
    ans += nums[k+1] - nums[k]
for j in range(t,N[1]-1):
    ans += nums[j+1] - nums[j]
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-1):
    b.append(a[i+1]-a[i])
b.append(K+a[0]-a[N-1])
print(max(b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
b = sorted(a)
ans = 0
ans2 = 0
for i in range(len(a)-1):
    ans += abs(a[i] - a[i+1])
for i in range(len(b)):
    if b[i] > 10:
        b[i] -= 20
b = sorted(b)
for i in range(len(b)-1):
    ans2 += abs(b[i] - b[i+1])
if ans < ans2:
    print(ans)
else:
    print(ans2)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
b = sorted(a)
for i in range(len(b)):
    if b[i] > 10:
        b[i] -= 20
b = sorted(b)
if a[-1] - a[0] < b[-1] - b[0]:
    print(a[-1] - a[0])
else:
    print(b[-1] - b[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ad = []
for i in range(n):
    if i != (n-1):
        ad.append(a[i+1] - a[i])
    if i == (n-1):
        ad.append(k-a[n-1] + a[0])

ad_max = max(ad)
ad_max_ind = ad.index(ad_max)

print(ad_max_ind)

if ad_max_ind == (n-1):
    ans = a[n-1] - a[0]
if ad_max_ind != (n-1):
    ans = k - a[ad_max_ind+1] + a[ad_max_ind]
    
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

dist = []
for i in range(N):
  dist.append(l[i+1]-l[i])
print(dist)
m = max(dist)

print(K-m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

dist = []
for i in range(N):
  dist.append(l[i-1]-l[i])

m = max(dist)

print(K-m)