Problem:


Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.



Sample input:
20 3
5 10 15


Sample output:
10

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
mi = float("inf")
ma = 0
tokei = 1
ans = 0

for index, tmp in enumerate(A):
    if index==0:
        left = tmp
    elif index == 1:
        right = tmp
        b1 = right - left
        b2 = left+K-right
#         print(b1)
#         print(b2)
        if b1>b2:
            ans = b2
            tokei = 0
            break
        else:
            ans = b1
    else:
        b1 = tmp-right
        b2 = left+K-tmp
#         print(b1)
#         print(b2)
        print(left)
        print(right)
        if b1<b2:
            ans += b1
            right = tmp
        else:
            ans += b2
            break
print(ans)

Submission status: Wrong Answer

Solution code:
# 入力の取得
k, n = map(int, input().split())
a = list(map(int, input().split()))

min_distance = k + 1
for index, x in enumerate(a):
    start = a[index]
    goal = a[(index + k) % n]

    if start < goal:
        work_distance = goal - start
    else:
        work_distance = k - start + goal

    if work_distance < min_distance:
        min_distance = work_distance

print(min_distance)

Submission status: Wrong Answer

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

K,N = map(int, input().split())
A = list(map(int, input().split()))
a = (A[0]+A[-1])//2
mi = -1
m = 10**7
for i in range(N):
    if abs(a-A[i]) < m:
        m = abs(a-A[i])
        mi = i
ans = 0
d = [0]*N
for i in range(N):
    d[i] = min(A[i], abs(A[mi]-A[i]))
for i in range(N):
    ans += abs(d[i]-d[mi])
print(ans)

Submission status: Wrong Answer

Solution code:

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

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

if A[0] != 0:
  b = abs(A[0] - A[N-1])
  L.append(b)

else:
  b = abs(K - A[N-1])
  L.append(b)

ans = K - max(L)

print(ans)

Submission status: Wrong Answer

Solution code:
K, N = [int(n) for n in input().split()]
A = [0]
A += [int(n) for n in input().split()]
A += [K]
dx = []
for i in range(N+1):
    dx.append(A[i+1] - A[i])
m = max(dx)
print(K-m if dx.index(m) != 0 else K-dx[-1]-dx[0])

Submission status: Wrong Answer

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

print(sum(arr)-n)

Submission status: Wrong Answer

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

bet = []
#bet = [210000] * (n - 1) + [0] * 1
for i in range(n):
    bet.append(a[i])
    for j in range(n):
        if i != j:
            if (0 <= a[i] < 10) and (10 < a[j] < 20):
                bet[i] = min(bet[i], abs(a[i]) - (a[j] - k))
            elif (0 <= a[j] < 10) and (10 < a[i] < 20):
                bet[i] = min(bet[i], abs(a[j]) - (a[i] - k))
            else:
                bet[i] = min(bet[i], abs(a[i] - a[j]))
#print(bet)
print(sum(bet))

Submission status: Wrong Answer

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

print(K-max(B))

Submission status: Wrong Answer

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

for i in range(N-1):
    result=+c[i+1]-c[i]
print(result)

Submission status: Wrong Answer

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


s=0
check=0
f=0
save=0
for i in range(1,k):
	s=s+l[i]-l[i-1]
	if(f==0 and l[i]>= n//2):
		check=i
		f=1
	else:
		if(f==0):
			save=s

s1=0
#print(save,check)
for i in range(check+1,k+1):
	s1=s1+n-abs(l[i%k]-l[(i-1)%k])

s1=s1+save
#print(s,s1)
print(min(s,s1))

Submission status: Wrong Answer

Solution code:
k, n = map(int,input(). split())
lstr_a = input().split(" ")
lint_a = [int(n) for n in lstr_a]
alist = sorted(lint_a)
t = alist.count(0)

for i in range(n-1):
  if alist[i]-alist[i-1]>=k/2:
    answer = k-alist[i]+alist[i-1]
    print(answer)
    break



if alist[n-1]-alist[0]>=k/2 and t >= 1:
  answer = k-alist[n-1]+alist[1]
  print(answer)
elif alist[n-1]-alist[0]>=k/2:
  answer = k-alist[n-1]+alist[0]
  print(answer)
else:
  answer = alist[n-1]-alist[0]
  print(answer)

Submission status: Wrong Answer

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

if 0 in A or K in A:
    print(K - max(A) + A[1])

else:
    print(max(A) - min(A))

Submission status: Wrong Answer

Solution code:
"""def power(x, y): 
    res = 1
    x=x
    while (y > 0): 
        if ((y & 1) == 1) : 
            res = (res * x) 
        y = y >> 1
        x = (x * x)
    return res""" 
"""def fact(n):
    if(n==0):
        return 1
    if(n==1):
        return 1
    return fact(n-1)+fact(n-2)"""

import math
#import combinations

#t=int(input())
#for _ in range(t):
#n=int(input())
#s=input()
#arr=list(map(int,input().split()))
k,n=map(int,input().split())
#count=0
arr=list(map(int,input().split()))
arr.sort()
length=arr[n-1]-arr[0]
for i in range(1,n):
    if(arr[i]>k//2):
        arr[i]=arr[i]-k
arr.sort()
if(length>arr[n-1]-arr[0]):
    length=arr[n-1]-arr[0]
print(length)

Submission status: Wrong Answer

Solution code:
#ANS
temp=input()
temp2=temp.split(" ")
K=int(temp2[0])
N=int(temp2[1])

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

A.append(K)
D=list()
#D.append(A[0])
for i in range(N):
  D.append(A[i+1]-A[i])

maxind=D.index(max(D))


sumd=0


for i in range(N):
  if(i!=maxind):
    sumd+=D[i]


print(sumd)

Submission status: Wrong Answer

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

if a[n-1] + a[0] >= k:
    print(a[n-1] - a[0])
else:
    max_n = 0
    for i in range(n-1):
        diff = abs(a[i]-a[i+1])
        max_n = max(diff, max_n)
    print(k-max_n)

Submission status: Wrong Answer

Solution code:
#--------------------------------------------
#ライブラリ呼び出し
import sys
import math
#from math import sqrt
#--------------------------------------------
K, N = map(int, input().split())
arr = list(map(int, input().split()))
for i in range(N-2):
    sa =int(arr[i+1]-arr[i])
    ssa =int(arr[i+2]-arr[i+1])
    max_num = max(sa,ssa)

hasi = K-arr[N-1]+arr[0]
ans =K-(max(max_num,hasi))
print(ans)

Submission status: Wrong Answer

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

for i in range(n):
  if A[i] > k-A[i]:
    ans += k-A[i]
    print(k-A[i], A[i])
    idx = i
    break
ans += A[idx-1]
print(ans)

Submission status: Wrong Answer

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

num = len(listA) -2
dis = max(listA) - min(listA)
## 一周　##
if dis > K/2:
  out1 = K - max(listA) + listA[num]
  out2 = dis
  if out1 >= out2:
    out = out2
  else:
    out = out1
## 周回無し##
if dis <= K/2:
  out = dis

print(out)

Submission status: Wrong Answer

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

for i in range(1, n):
  s = a[i]-a[i-1]
  s_k = abs(a[i]-k)
  ans.append(min(s,s_k))
    
print(sum(ans))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
A = list(map(int,input().split()))
maxNum, minNum = 0, K
for n in range(N):
  if maxNum < A[n]:
    maxNum = A[n]
  if minNum > A[n]:
    minNum = A[n]
res = maxNum - minNum
maxNum, minNum = 0, K
for n in range(N):
  if A[n] > K/2:
    A[n] = (K - A[n]) * -1
  if maxNum < A[n]:
    maxNum = A[n]
  if minNum > A[n]:
    minNum = A[n]
print(min(res, maxNum - minNum))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
#print(k)
#print(n)
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

for i in reversed(range(0, n)):
  if l_si_i[i] == 0:
    l_si_i.pop(i)

#print(l_si_i)

minIndex = l_si_i.index(min(l_si_i))

#print(minIndex)

if minIndex != 0:
  temp = l_si_i.pop(minIndex)
  l_si_i.insert(0, temp)

#print(l_si_i)

sub = 0
l = len(l_si_i)
for i in range(l):
  if i != 0:
    sub += abs(l_si_i[i] - l_si_i[i-1])

print(sub)

Submission status: Wrong Answer

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

if a[-1] <= k / 2:
    print(a[-1] - a[0])

else:
    max_distance = k - a[-1] - a[0]
    str_index = 0
    gl_index = -1
    for i in range(n):
       	if i == 0:
            continue
        elif max_distance < abs(a[i-1] - a[i]):
            max_distance = abs(a[i-1] - a[i])
            str_index = i-1
            gl_index = i
    print(a[str_index] + (k - a[gl_index]))

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
A = [int(N) for N in input().split()]
#print(A[0])
sum = 0.0
for i in range(0,N):
    sum = sum+A[i]
average = sum/N
print(average)

Submission status: Wrong Answer

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


l_s = []
check = 0
for i in range(0,N):
    if(A[i] == 0):
        check =1
if(check == 1):
    A.append(K)
    for i in range(1,N+1):
          l_s.append(A[i]-A[i-1])
    num = 0
    ans = 99999999999999999
    for i in range(0,N):
        num = K - l_s[i]
        ans = min(ans,num)
    print(ans)
    
else: 
    A.append(K)
    for i in range(1,N+1):
          l_s.append(A[i]-A[i-1])
    m1 = max(l_s)
    ans = K-1 -m1
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

B=[0]*N
for i in range(N):
    if A[i]<=K//2:
        B[i]=A[i]
    if A[i]>K//2:
        B[i]=-(K-A[i])
        
print(min(abs(max(A)-min(A)),abs(min(B))+max(B),abs(abs(K-max(A))+(K+min(B)))))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
list_str = input().split()
Kyori_list = [int(s) for s in list_str]

Saidai_Kyori = max(Kyori_list)
Saitan_Kyori = K
for i in range(len(Kyori_list)):
    if (int(Kyori_list[i]) > 0) and (Saitan_Kyori > int(Kyori_list[i])):
        Saitan_Kyori = int(Kyori_list[i])

Idou_Kyori = Saidai_Kyori - Saitan_Kyori
print(Idou_Kyori)

Submission status: Wrong Answer

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

l2 = [i for i in l if i <= (a/2)]
l3 = [a - i for i in l if i > (a/2)]
print(min(l2[-1] + l3[0],max(l) - min(l)))

Submission status: Wrong Answer

Solution code:
l, num = input().split()
point = input().split()

l = int(l)
num = int(num)

point0 = int(point[0])

for p in point:
  p = int(p)
  if l / 2 > p:
  	rpoint = p
    
lpoint = int(point[-1])

if rpoint == lpoint:
  print(rpoint - point0)
elif rpoint < lpoint:
  if rpoint == point0:
    if(lpoint - rpoint < rpoint + l - lpoint):
      print(lpoint - rpoint)
    else:
      print(rpoint + l - lpoint)
  else:
    ltop = rpoint - point0 + point0 + l - lpoint
    lunder = rpoint - point0 + lpoint - rpoint
    if ltop > lunder:
      print(lunder)
    else:
      print(ltop)

Submission status: Wrong Answer

Solution code:
A=input()
LA=A.split()
K=int(LA[0])
N=int(LA[1])

B=input()
LB=B.split()

C=0
samax=0
start=0

for n in LB:
  sa=int(int(n)-int(C))
  C=n
  if samax < sa:
    samax=sa
    start=int(n)-int(samax)

ans1=int(LB[N-1])-int(LB[0])
ans2=int((K-int(LB[N-1]))+int(start))

if ans2 > ans1:
  ans=ans1
else :
  ans=ans2
print(ans)

Submission status: Wrong Answer

Solution code:
_in = list(map(int, input().split()))
k = _in[0]
n = _in[1]
p = 0
_i = list(map(int, input().split()))
dis = 0
mdis = k - _i[n-1] + _i[0]
print(mdis)
for i in range(1,n-1):
    dis = _i[i] - _i[i-1]
    print(dis)
    if dis>mdis:
        mdis = dis
print(k-mdis)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
if n == 2 :
  a.sort(reverse=True)
  print(min(k - max(a), a[0] - a[1]))  
elif 0 in a :
  a.sort(reverse=True)
  print((k - max(a)) + a[1])
else :
  print(max(a) - min(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int , input().split())
lis = list(map(int,input().split()))
mi = [min(lis)] * n
li = [x - y for (x, y) in zip(lis, mi)]

ans1 = 0
for i in range(n - 1):
    ans1 += li[(i + 1)] - li[i]

chuten = k / 2
for i,j in enumerate(li):
  if j > chuten:
    li[i] = j - k
li.sort()
ans2 = 0
for i in range(n - 1):
    ans2 += li[(i + 1)] - li[i]

print(min(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import statistics
K,N = map(int,input().split())
A = [int(n) for n in input().split()]

a1 = max(A)-min(A)

for i in range(len(A)-1):
    max_v = 0
    index = None
    if (A[i+1]-A[i])>max_v:
        max_v = A[i+1]-A[i]
        index = i+1

a2 = A[index-1]+abs(K-A[index])
print(a2) if a1>a2 else print(a1)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
mi = float("inf")
ma = 0
tokei = 1
ans = 0

for index, tmp in enumerate(A):
    if index==0:
        left = tmp
    elif index == 1:
        right = tmp
        b1 = right - left
        b2 = left+K-right
#         print(b1)
#         print(b2)
        if b1>b2:
            ans = b2
            tokei = 0
            break
        else:
            ans = b1
    else:
        b1 = tmp-right
        b2 = left+K-tmp
#         print(b1)
#         print(b2)
#         print(left)
#         print(right)
        if b1<b2:
            ans += b1
        else:
            ans += b2
            break
print(ans)

Submission status: Wrong Answer

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

A.insert(0, 0)
A.append(K)

mixi = 0
sum_len = 0


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

if space == 0 or space == len(A) - 1:
    result = sum_len - (mixi + (K - A[-2]))
else:
    result = sum_len - mixi

print(result)

Submission status: Wrong Answer

Solution code:
ddd = [input() for i in range(2)]
eee = ddd[0].split()
fff = ddd[1].split()
longest_distance = int(eee[0]) - int(fff[-1])
for i in range(len(fff) - 1):
    distance = int(fff[i+1]) - int(fff[i])
    if longest_distance < distance:
        longest_distance = distance
shortest_road = int(eee[0]) - longest_distance
print(shortest_road)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
li = list(map(int, input().split()))
li2 = []
x = k/2
for i in li:
    temp = 0
    if i < x:
        temp = i+k
    elif i == int(x):
        temp = i*2
    else:
        temp = i
    li2.append(temp)
li2.sort()
print(li2[-1] - li2[0])

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
li = list(map(int, input().split()))
half = K/2
if li[0] == 0 and li[1] >= half:
    print(li[1])
else:
    re_li = [v-K if v > half else v for v in li]
    dist1 = max(li)-min(li)
    dist2 = max(re_li)-min(re_li)
    print(dist1 if dist1 < dist2 else dist2)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
l = list(map(int, input().split()))
m = []
for i in range(len(l)):
    if(l[i]!=0):
        m.append(n-l[i])
    else:
        m.append(0)
if(m[0]==0):
    a=m[0]
    m.pop(0)
    m.append(0)
m1=[]
d=0
for i in range(len(m)-1):
    a=abs(m[i]-m[i+1])
    d+=a
    m1.append(a)
mi=0
for i in range(len(l)-1):
    b=d
    d+=(abs(l[i]-l[i+1])-m1[i])
    mi=min(b,d)
print(mi)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [None] * len(a)
c = [None] * len(a)
ans = 0
for i in range(len(a)):
    if i != len(a)-1:
        b[i] = a[i+1]-a[i]
    else:
        b[i] = k - a[i] + a[0]        
min_index = b.index(min(b))
if min_index == 0:
    if b[min_index+1] > b[len(a)-1]:
        ans = sum(b)-b[min_index+1]
    else:
        ans = sum(b)-b[len(a)-1]
else:
    if b[min_index+1]>b[min_index-1]:
        ans = sum(b)-b[min_index+1]
    else:
        ans = sum(b)-b[min_index+1]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
array = list(map(int, input().strip().split()))
a = array[1]-array[0]
ans = a*(N-1)
print(int(ans))

Submission status: Wrong Answer

Solution code:
kn = list(map(int, input().split()))

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

l=[]

l.append(kn[0]-a[-1])

for i in range(kn[1]-1):
    l.append(a[i+1]-a[i])

print(kn[0]-max(l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

a = list(map(int, input().split()))
a = sorted(a)
max_ = max(a)
min_ = min(a)
a_ = deepcopy(a)
ans = 100000000000
for index in range(len(a)):
    # temp = max(a) - min(a)
    # print(max_,min_)
    temp = max_ - min_
    max_ = min_ + k
    if index != len(a)-1:
        min_ = a[index+1]
    a[index] = a[index] + k
    # print(temp)
    if ans > temp:
        ans = temp

max_ = max(a_)
min_ = min(a_)

for index in reversed(range(len(a))):
    # print(a_, temp, max(a_), min(a_))
    # temp = (max(a_) - min(a_))
    print(max_,min_)
    temp = max_-min_
    min_ = max_ -k
    if index != 0:
        max_ = a_[index-1]
    print(temp)
    # a_[index] -= k
    if ans > temp:
        ans = temp

print(ans)

Submission status: Wrong Answer

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

if A[0]==0:
  print(k-(A[2]-A[1]))
else:
  print(A[-1]-A[0])

Submission status: Wrong Answer

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

  Max = A_list[N-1]
  Min = A_list[0]

  for i in range(N-1):
    space =  A_list[i+1] - A_list[i]
    if space >= K/2:
      print(K-space)
      exit()
    else:
      pass
  print(Max -Min)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
k,n=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
m=100000
s=0
for i in range(len(A)-1):
    s+=abs(A[i]-A[i+1])
x=A[len(A)-1]
x=k-x
for i in range(len(A)-1):
    x+=A[i]
print(min(x,s))

Submission status: Wrong Answer

Solution code:
k, n = map(int,input().split())
a = list(map(int, input().split()))
ans1 = max(a) - min(a)
for i in range (0,n):
    if a[i] > (k/2):
        a[i] = a[i] - k
ans2 = abs(max(a) - min(a))
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
k,n=map(int, input().split())
A=list(map(int, input().split()))
B=[0]*(n-1)
t=0
for i in range(n-1):
    B[i]=A[i+1]-A[i]
if B[n-2]>k-A[n-1]:
    B[n-2]=k-A[n-1]
B.sort()
for i in range(len(B)):
    t+=B[i]
print(t)

Submission status: Wrong Answer

Solution code:

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

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

else:
    lst.append(K - (A[N - 1] - A[0]))

print(lst)
print(K - max(lst))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
    a.remove(0)
a.sort()
ans = 0
n = 1
for i in a:
    try:
        ans += a[n] - a[n-1]
        n += 1
    except:
        pass
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
p,n=map(int,input().split())
arr=list(map(int,input().split()))
mid=n//2
f=0
for i in range(mid):
    f+=abs(arr[i]-arr[i+1])
for j in range(mid+1,n-1):
    f+abs(arr[j]-arr[j+1])
f+=abs(p-arr[-1])
b=0
for i in range(mid,n-1):
    b+=abs(arr[i]-arr[i+1])
b+=abs(p-arr[-1])
for j in range(0,mid):
    b+=abs(arr[j]-arr[j+1])
print(min(f,b))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
s=list(map(int,input().split()))
ans=sum(s)
for i in s:
    sum=0
    for j in s:
        sum+=abs(i-j)
    if sum<ans:
        ans=sum
print(ans)

Submission status: Wrong Answer

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

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

a.append(k)

big = 0
big_count = 0

for i in range(n):
    b = a[i + 1] - a[i]
    if b > big_count:
        big_count = b
        big = i

if big == 0:
    ans = a[n - 1] - a[big]
else:
    ans = k - big_count

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
harf=k/2
harf1=[]
harf2=[]

if max(a)-min(a)==0:
  print(0)
  sys.exit()
  
for i in a:
  if i<=harf:
    harf1.append(i)
  else:
    harf2.append(i)

ans1=max(a)-min(a)
ans2=max(harf1)+(k-min(harf2))
  
if ans1<=ans2:
  print(ans1)
else:
  print(ans2)

Submission status: Wrong Answer

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

right = max(filter(lambda x: x<=k//2, a))
left = k-min(filter(lambda x: x>k//2, a))
if max(a)-min(a) < right+left:
    print(max(a)-min(a))
else:
    print(right+left)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

tmp =list(map(int, input().split()))
num=np.array(list(map(int, input().split())))

r = np.diff(num).sum()

l=np.array(num[num>int(tmp[0]//2)].tolist()+num[num<=int(tmp[0]//2)].tolist())
l[l>int(tmp[0]//2)]=tmp[0]-l[l>int(tmp[0]//2)]
l=np.abs(np.diff(l)).sum()

if l>r:
    print(r)
else:
    print(l)

Submission status: Wrong Answer

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

a=[]
for i in range(N-1):
  a.append(abs(A[i]-A[i+1]))
  #print(a)
a.append(abs(K-A[N-1]))
print(a)

a.remove(max(a)) #一致するものを一個だけ消す
ans=sum(a)
print(ans)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
l= list(map(int,input().split()))
li = []

for i in range(n-1):
  li.append(l[n-i-1]-l[n-i-2])

li.sort(reverse=True)
print(k-li[0])

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
K, N = map(int, input().split())
text = input().split()
distance = []
for i in range(N):
    dis = abs(int(text[i]) - int(text[i-1]))
    distance.append(dis)

min_dis = sum(distance) - max(distance)

print(min_dis)

Submission status: Wrong Answer

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

interval = 0
max_interval = 0
index = None
distance = 0
for i in range(N):
    if i == 0:
        interval = 20 - As[-1] + As[0]
    else:
        interval = abs(As[i] - As[i-1])
    if interval > max_interval:
        max_interval = interval
        index = i
if index == 0:
    distance = K - As[index-1] + As[index]
else:
    distance = K - As[index] + As[index -1]
print(distance)

Submission status: Wrong Answer

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

a_list_sorted = sorted(a_list)

distance_1 = a_list_sorted[-1] - a_list_sorted[0]

a_list_sorted_minus = list(map(lambda x: x - k, a_list_sorted))
min_tmp = 100000000
for i in range(len(a_list_sorted_minus)):
    if a_list_sorted_minus[i] > -(k / 2):
        if a_list_sorted_minus[i] < min_tmp:
            min_tmp = a_list_sorted_minus[i]
            index = i

distance_2 = a_list_sorted[index-1] - min_tmp

print(min(distance_1, distance_2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

result = c[-1] - c[0]

print(result)

Submission status: Wrong Answer

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

dist = [
    abs(
        min(
            A[i] - A[(i + 1) % len(A)],
            K - (A[i] - A[(i + 1) % len(A)])
        )
    )
    for i in range(len(A))
]

start = (dist.index(max(dist)) + 1) % len(A)

dist_l = abs(
    min(
        A[start] - A[(start - 1) % len(A)],
        K - (A[start] - A[(start - 1) % len(A)])
    )
)
dist_r = abs(
    min(
        A[start] - A[(start + 1) % len(A)],
        K - (A[start] - A[(start + 1) % len(A)])
    )
)

if dist_l <= dist_r:
    A = A[:start + 1][::-1] + A[start + 1:][::-1]
else:
    A = A[start:] + A[:start]

total_distance = sum(
    [
        abs(
            min(
                A[i] - A[i + 1],
                K - (A[i] - A[i + 1])
            )
        )
        for i in range(len(A) - 1)
    ]
)

print(total_distance)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
#print(k)
#print(n)
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

for i in reversed(range(0, n)):
  if l_si_i[i] == 0:
    l_si_i.pop(i)

#print(l_si_i)

minIndex = l_si_i.index(min(l_si_i))

#print(minIndex)

if minIndex != 0:
  temp = l_si_i.pop(minIndex)
  l_si_i.insert(0, temp)

#print(l_si_i)

sub = 0
l = len(l_si_i)
for i in range(l):
  if i != 0:
    sub += l_si_i[i] - l_si_i[i-1]

print(sub)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
l = list(map(int,input().split()))
if l[0] != 0:
  print(min(l[-1] - l[0],a-l[-1] + l[-2]))
else:
  print(min(a - l[-1] + l[-2],l[-1]))

Submission status: Wrong Answer

Solution code:
ddd = [input() for i in range(2)]
eee = ddd[0].split()
fff = ddd[1].split()
longest_distance = 0
for i in range(len(fff) - 1):
    distance = int(fff[i+1]) - int(fff[i])
    if longest_distance < distance:
        longest_distance = distance
shortest_road = int(eee[0]) - longest_distance
print(shortest_road)

Submission status: Wrong Answer

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

if A[0]==0:
  if n>3:
    print(k-A[1])
  else:
    print(A[-1]-A[1])
else:
  print(A[-1]-A[0])

Submission status: Wrong Answer

Solution code:
_in = list(map(int, input().split()))
k = _in[0]
n = _in[1]
p = 0
_i = list(map(int, input().split()))
dis = 0
mdis = k - _i[n-1] + _i[0]
print(mdis)
for i in range(1,n):
    dis = _i[i] - _i[i-1]
    print(dis)
    if dis>mdis:
        mdis = dis
print(k-mdis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
for i in A:
  if i < N-1:
    B[i]=A[i+1]-A[i]
  else:
    B[N-1] = A[0] + K - A[N-1]

print(K-max(B))