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

d = [a[i] - a[i-1] for i in range(1, n)]
d.insert(0, a[0])
d.append(k - a[-1])
sum_d = sum(d)

most_min_path = 10000

for i in range(n):
    if i == 0:
        normal_ck = sum_d - d[i] - d[-1]
        reverse_ck = sum_d - d[i+1]
    elif i == n - 1:
        normal_ck = sum_d - d[i]
        reverse_ck = sum_d - d[i+1] - d[0]
    elif 0 < i < n - 1:
        normal_ck = sum_d - d[i]
        reverse_ck = sum_d - d[i+1]

    min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
    if most_min_path > min_ck:
        most_min_path = min_ck

print(most_min_path)

Submission status: Wrong Answer

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

for i in listA:
  if i > K/2:
    temp = i - K
    listB.append(temp)
  else:
    listB.append(i)
## 順転 ##
out1 = max(listA) - min(listA)
## 逆転 ##
out2 = max(listB) - min(listB)
if out1 > out2:
  print(out2)
else:
  print(out1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

K, N = map(int, input().split())
li = list(map(int, input().split()))
half = K/2
re_li = [v-K if v >= half else v for v in li]
li2 = copy.copy(li)
if 0 in li2:
    del li2[0]
    li2.append(K)
dist1 = max(li)-min(li)
dist2 = max(re_li)-min(re_li)
dist3 = max(li2)-min(li2)
if dist1 < dist2 and dist1 < dist3:
    print(dist1)
elif dist2 < dist1 and dist2 < dist3:
    print(dist2)
else:
    print(dist3)

Submission status: Wrong Answer

Solution code:
# 初期入力
K,N = (int(x) for x in input().split(" "))
A = [0]
for i in input().split():
    if i !="0":
        A.append( int(i) )

# 1軒ずつ距離のリスト
x =[A[i+1] - A[i] for i,v in enumerate(A[:-1]) ]
if N < len(x):
    x.append(K - A[-1] +A[1])
else:
    x.append(K -A[-1])
#　求めるのは　K　－　max(隣と離れた家の距離)
kyori_min = K - max(x)
print(kyori_min)

Submission status: Wrong Answer

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


first = 0
for i in house:
    if i > 0:
        first += i
        break
print(house[-1] - first)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
l=list(map(int,input().split()))
ans=1000000
for i in range(n-1):
  d = 0
  d+=min(abs(l[i]-l[i-1]),abs(k-l[i-1]))
  d+=min(abs(l[i]-l[i-2]),abs(k-l[i-2]))
  d+=min(abs(l[i]-l[i-3]),abs(k-l[i-2]))
  if ans > d:
    ans=d
print(ans)

Submission status: Wrong Answer

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

if A == [0]*N:
  ans = 0
else:
  d = []
  for i in range(N):
    d.append(abs(A[i]-A[i]))
  d.append(abs(0-A[0])+abs(A[N-1]-K))
  ans = K-max(d)
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
s = []
count = 0
for i in a:
  if i > k / 2:
    s.append((k - a[count]) * -1)
  else:
    s.append(a[count])
  count += 1
ans_1 = abs(min(s)) + abs(max(s))
d = []
counts = 0
for j in a:
  if i > k:
    d.append((k - a[counts]) * -1)
  else:
    d.append(a[counts])
  counts += 1
ans_2 = max(a) - min(a)
ans_3 = max(d) - min(d)
ans = min(ans_1, ans_2, ans_3)
print(ans)

Submission status: Wrong Answer

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


p = max(x) - min(x)
q = max(x) - k
r = x[n-2] - q
print(min(p,r))

Submission status: Wrong Answer

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

r=0
s=0

i=0
while(i<N-1):
  r+=list[i+1]-list[i]
  i+=1

j=0

while(j<N-2):
  s+=list[j+1]-list[j]
  j+=1
s+=K-list[N-1]


if(r<s):
  print(r)
else:
  print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
a, b = map(int, input().split())
l = list(map(int, input().split()))
f = b - 1
g = b - 2
c = int(l[-1])
d = int(l[0])
v = list([1]*b)
for i in range (f):
  	v[i] = l[i+1]-l[i]
v[f] = a + l[0] - l[-1]
print(v)
if	max(v) == v[f]:
	print(l[-1]-l[0])
else:
	print(a-max(v))

Submission status: Wrong Answer

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

  space_max = 0
  space_sum = 0

  if A_list[N-1] - A_list[0] <= K/2:
    print(A_list[N-1] - A_list[0])
    exit()
  else:
    pass

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

Submission status: Wrong Answer

Solution code:
k,n=input().split()
k=int(k)
n=int(n)
l=list(map(int, input().split()))
ans=[l[0]]
num=l[0]
for i in range(1,len(l)):
    ans.append(min(abs(l[i]-k),l[i]))
ans.sort()
if ans[0]==0:
    print(sum(ans))
else:
    print(sum(ans)-ans[-1])

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
l=list(map(int,input().split()))
ans=1000000
for i in range(n-1):
  d = 0
  d+=min(abs(l[i]-l[i-1]),abs(k-l[i-1]))
  d+=min(abs(l[i]-l[i-2]),abs(k-l[i-2]))
  if ans > d:
    ans=d
print(ans)

Submission status: Wrong Answer

Solution code:

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

ans_a = 0
for i in range(len(a)-1):
    ans_a +=(a[i+1]-a[i])
for i in range(n):
    if a[i]>k/2:
        a[i] = abs(k-a[i])

a.sort(reverse=True)
kai = a[0]+a[1]
#print(kai,ans_a)
print(min(kai,ans_a))

Submission status: Wrong Answer

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

way = list(map(int,input().split())) # 道のり

i = 0
dis = [] #距離の絶対値

for i in range(n):
  if way[i] < k // 2:
    dis.append((k // 2 - way[i]))
  elif way[i] == k//2:
    dis.append(way[i])
  else:
    dis.append(k - way[i])

print(dis)

route = []
route.append(way[n-1] - way[0])

x = 1

for x in range(n):
  route.append(dis[x] + way[x - 1])

print(min(route))

Submission status: Wrong Answer

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

# a = 1000000
# for i in range(len(lists)):
#     if abs(K - lists[i]) == 0:
#         continue
#     elif abs(K - lists[i]) < a:
#         a = abs(K - lists[i])
# b = 1000000
# for i in range(len(lists)):
#     for j in range(len(lists)):
#         if abs(lists[i] - lists[j]) == 0:
#             continue
#         elif abs(lists[i] - lists[j]) < b:
#             b = abs(lists[i] - lists[j])

list_min = min(lists)
list_max = max(lists)
delta = list_max - list_min
kk = K/2
if delta >= kk:
    c = kk
else:
    c = delta

# a = []
# for i in range(len(lists)):
#     if abs(K - lists[i]) == 0:
#         continue
#     else:
#         a.append(abs(K - lists[i]))
# b = []
# for i in range(len(lists)):
#     for j in range(len(lists)):
#         if abs(lists[i] - lists[j]) == 0:
#             continue
#         else:
#             b.append(abs(lists[i] - lists[j]))
# a = min(a)
# b = min(b)

# c = a+b
print(int(c))

Submission status: Wrong Answer

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

distance_list = []

i = 0
while i < n:
    if i == 0:
        distance_list.append(ls[i])
        i += 1
        continue
    distance_list.append(ls[i] - ls[i-1])
    i += 1

distance_list.append(k - ls[n-1])
distance_list.remove(max(distance_list))
distance_list.remove(max(distance_list))
print(sum(distance_list))

Submission status: Wrong Answer

Solution code:
k,n=[int(i) for i in input().split()]
a=[int(x) for x in input().split()]
if a[0]==0:
  a[0]=k/2
b=max(a)-min(a)
print(b)

Submission status: Wrong Answer

Solution code:
#C

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

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

d = []
#d[]は各家の距離
d.append(a[0])
for i in range(n-1):
    d.append(a[i+1] - a[i])

d.append(k - a[n-1])
# print(d)

# print(max(d))

# print(sum(d))

print(k - max(d))

Submission status: Wrong Answer

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

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

if a[0] != 0:
    print((a[n//2]-a[0])+(a[n-1]-a[n//2]))
else:
    print((a[n//2]-a[0])+(k-a[n-1]))

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
l = list(map(int,input().split()))
ma = 0
ind = 0
f = 1
for i in range(len(l)):
    if i == len(l)-1:
        x = l[0]
        y = l[len(l)-2]

    else:
        x = l[i+1]
        y = l[i-1]

    en = abs(x-l[i])
    ep = abs(k-l[i]-y)
    if en > ma:
        ma = en
        ind = i
        f = -1

    if ep > ma:
        ma = ep
        ind = i
        f = 1

if f == -1:
    ind = len(l)-1-ind
    l = l[::-1]

i = ind
som = 0
prev = l[i]
i = i+1
#print(ind)
#print(i)
while(i % len(l) > ind):
    som = som + abs(l[i]-prev)
    prev = l[i]
    i = i+1

print(som)

Submission status: Wrong Answer

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

if a[0] == 0 and len(a) == 3:
    print(max(a[1:])-min(a[1:]))
    exit()
elif a[0] == 0 and len(a) > 3:
    print(max(a))
    exit()
else:
    h = k
    for i in a:
        ans = 0
        for j in a:
            ans += (abs(i-j))
        if ans < h:
            h = ans
    print(h)

Submission status: Wrong Answer

Solution code:
import numpy as np

rawRounds = input()
rawDistances = input()

mizuumiRound = int(rawRounds.split()[0])
houseNumber = int(rawRounds.split()[1])
distances = [int(n) for n in rawDistances.split()]

sortedDistances = sorted(distances)

betweens = list(sortedDistances)
for i in range(len(betweens)-1) :
    betweens[i+1] -= sortedDistances[i]

betweens[0] = (sortedDistances[len(sortedDistances)-1] - sortedDistances[0])
if betweens[0] > (mizuumiRound/2):
    betweens[0] -= int(mizuumiRound/2)

startPoint = betweens.index(max(betweens))

routeLen = sum(betweens) - betweens[startPoint]

print(routeLen)

Submission status: Wrong Answer

Solution code:
def main():
    H , W = map(int, input().split())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    value_temp = 0
    value_list = []
    i = 0
    for A in A_list :
        if i == 0 :
            A00 = A
            A0 = A
            i = i + 1
            continue
        else :
            value_temp = A - A0
            A0 = A
            value_list.append(value_temp)
            #print(str(value_temp))
            #print(str(A))
    if W > 2 :
        value_temp = H - A0
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    elif W == 2 :
        value_temp = H - A0
        value_list.append(value_temp)
        value_temp = A00
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    else :
        ans = sum(value_list)

    #print(str(value_list))

    print (str(ans))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

td = 0
d = 0

# correct distances
for i in range(n):
    if arr[i] > k:
        arr[i] = (arr[i]-k)
        
# sort
arr = list(set(arr))
arr.sort()

# cal actual distances
for i in range(1,n):
    temp = (arr[i] - arr[i-1])
    td += temp
    
    
print(td)

Submission status: Wrong Answer

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

m = A[min(((i, abs(max(A) / 2 - n)) for i, n in enumerate(A)), key=lambda x: x[1])[0]]
print(m)
print(max((max(A) - m), (m - min(A))))

Submission status: Wrong Answer

Solution code:
a = list(map(int, input().rstrip().split()))
k = a[0]
n = a[1]
b = list(map(int, input().rstrip().split()))
distances = []
for i in range(0, n):
    sec = i - 1
    if sec < 0:
        sec = n - 1
    distances.append(k - abs(b[i] - b[sec]))
for i in range(0, n):
    sec = i + 1
    if sec >= n:
        sec = 0
    distances.append(k - abs(b[i] - b[sec]))
print(min(distances))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

B = sorted(B,reverse = False)

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

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split(" "))
A = list(map(int,input().split(" ")))
A.sort()
distance_list = []
for i in range(N-1):
    distance = A[i+1] - A[i]
    distance_list.append(distance)
distance_list.append(K-max(A))
distance_list.remove(max(distance_list))
ans = sum(distance_list)
print(ans)

Submission status: Wrong Answer

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

s=min(K/2,li[N-1]-li[0])
print(int(s))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
s = []
count = 0
for i in a:
  if i > k / 2:
    s.append((k - a[count]) * -1)
  else:
    s.append(a[count])
  count += 1
ans_1 = abs(min(s)) + abs(max(s))
counts = 0
for j in a:
  if i > k:
    s.append((k - a[counts]) * -1)
  else:
    s.append(a[counts])
  counts += 1
ans_2 = max(a) - min(a)
ans = min(ans_1, ans_2)
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

k, n = map(int, input().split())
a = list(map(int, input().split()))
# print(a)
far = -1
sep = -1
for i in range(1, n):
    temp = a[i] - a[i-1]
    if temp > far:
        far = temp
        sep = i
# print(far, sep)
f, s = a[:sep], a[sep:]
# print(f,s)
if min(s) - max(f) == far:
    combine = a

    # print(combine)

    print(reduce(lambda x,y: y-x, combine))
else:
    left = reduce(lambda x,y: y-x, f)
    total = 0
    for i in range(0, len(s)):
        if i == len(s)-1:
            total += k - s[i]
        else:
            total += s[i+1]-s[i]
    print(left+total)

Submission status: Wrong Answer

Solution code:
import sys

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

#print(an)

an.append(k)

diff = list()
for ii in range(0,n):
  diff.append(an[ii+1] - an[ii])
  
allsum = sum(diff)
cand = list()
for ii in range(0,len(diff)):
  cand.append(allsum - diff[ii])
  
if k <= 3:
  diff.pop()
  print(sum(diff))
  sys.exit()
  
print(min(cand))

Submission status: Wrong Answer

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

b = [a[0]]
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:
K, N = map(int, input().split())
A = list(map(int, input().split()))

if A[N-1]-A[N-2] >= 10:
  ans = (K-A[N-1])+A[N-2]
else:
  ans = A[0]+A[N-1]
  
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
max = 0
len = [0] * n
for i in range(n):
    if i == 0:
        len[i] = a[i]
    else:
        len[i] = a[i] - a[i - 1]
        if len[i] > len[max]:
            max = i
if len[max] > int(k / n) and len[max] > (k - a[n - 1]) and (a[n - 1] - a[0]) > a[0]:
    ans = (k - a[max]) + a[max - 1]
else:
    ans = a[n - 1] - a[0]
print(ans)

Submission status: Wrong Answer

Solution code:
#C

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

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

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

d.append(k - a[n-1])

print(sum(d) - max(d))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

min_index = np.where(dist == dist.min())[0]
s = np.sum(dist)
result = []
for start_index in min_index:
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

i = 0
dis = []

for i in range(n):
  if way[i] < k // 2:
    dis.append((k // 2 - way[i]))
  elif way[i] == k//2:
    dis.append(way[i])
  else:
    dis.append(k - way[i])

route = []
route.append(way[n-1] - way[0])

x = 1

for x in range(n):
  route.append(dis[x] + way[x - 1])

print(route)
print(min(route))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def ai_time(K, A, ai, max_a, min_a):
  r = ai_time_r(K, A, ai, max_a)
  l = ai_time_r(K, A, ai, min_a)
  return min([r, l])
 
def ai_time_r(K, A, ai, max_a):
  long_lst = [i for i in A if i < ai]
  if long_lst:
    return max(long_lst) - ai + K
  else:
    return max_a - ai

def ai_time_l(K, A, ai, min_a):
  long_lst = [i for i in A if i > ai]
  if long_lst:
    return K - ai + min(long_lst)
  else:
    return ai - min_a
  
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
max_a = max(A)
min_a = min(A)
res = min([ai_time(K, A, ai, max_a, min_a) for ai in A])
print(res)

Submission status: Wrong Answer

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

td = 0
d = 0

# correct distances
for i in range(n):
    if arr[i] > k:
        arr[i] = (arr[i]-k)
        
# sort 
arr.sort()

# cal actual distances
for i in range(1,n):
    temp = (arr[i] - arr[i-1])
    td += temp
    
    
print(td)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
if 2  <= K <= 10 ** 6 and 2 <= N <= 2 * 10 ** 5:
    if 0 in A:
        A.remove(0)
    x = max(A) - min(A)
    print(x)

Submission status: Wrong Answer

Solution code:
import numpy as np

K, N = [int(element) for element in input().split(" ")]
AList = np.array([int(element) for element in input().split(" ")])
distance = np.diff(AList)
start_end_distance = abs((20 - AList[-1]) + AList[0])
distance = np.append(distance, start_end_distance)

print(np.sum(distance) - np.max(distance))

Submission status: Wrong Answer

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

if n == 2:
    print(min(a[1] - a[0], a[0] - 0))
else:
    a.append(k)
    m = 0
    ans = 0
    for i in range(1, n + 1):
        x = a[i] - a[i - 1]
        ans += x
        m = max(m, x)
    print(ans - m)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
x = max(A)
B = list(map(lambda x: K - x, A))
y = min(B)
z = x - y
print(z)

Submission status: Wrong Answer

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

N=B[1]
K=B[0]
N1= -K+A[N-1]
L1=-N1+A[N-2]
L2=A[N-1]-A[0]
c = min(L1,L2)
print(c)

Submission status: Wrong Answer

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

k = kn[0]
n = kn[1]

list = []
for i in range(n):
    if i == 0:
        list.append(a[i])
    else:
        list.append(a[i] - a[i-1])

    if i == n-1:
        list.append(k - a[i])

ans = 0
for ele in list:
    ans += ele
max = max(list)
if list[n-1]+list[0] >= a[n-1]-a[0]:
    secondMax = sorted(list)[-2]
    ans -= max+secondMax
else:
    ans -= max

print(ans)

Submission status: Wrong Answer

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

kyori = 0
i = -1
f = 0
e = 0
while True:
  i += 1
  if i >= len(b):
    break
  else:
    if b[i] == 0:
      continue
    else:
      if f == 0:
        f = b[i]
      else:
        e = b[i]
        if f < e:
          kyori += e - f
        else:
          kyori += f - e
        f = e
  
print(kyori)

Submission status: Wrong Answer

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

As.append(K)
As.append(K + As[1])

min_dist = 0
for i in range(N - 1, N + 2):
  dist = As[i] - As[i - N + 1]
  if min_dist > dist or min_dist == 0:
    min_dist = dist
print(min_dist)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
k,n = map(int,input().split())
n_list = list(map(int,input().split()))
new_list = copy.copy(n_list)
for i in range(len(new_list)):
  if new_list[i] > k-new_list[i]:
    new_list[i]=-(k-new_list[i])
new_list = sorted(new_list)
if (n_list[-1]-n_list[0]) < (new_list[-1]-new_list[0]):
  print((n_list[-1]-n_list[0]))
else:
  print((new_list[-1]-new_list[0]))

Submission status: Wrong Answer

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

a = list(map(int,input().split()) )
j = 0
if a[j] != 0:
    print(a[n-1]-a[j])
elif a[j] == 0:
    while a[j] == 0:
        a[j] += a[j+1]
        b = [a[n-1]-a[j] ]
    print(min(b))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
print(k-(a[-1]-a[-2]))

Submission status: Wrong Answer

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

sum_1=0
sum_2=0
for i in range(1,int(N)):
  sum_1+=A[i]-A[i-1]
  sum_2+=A[i]-A[i-1]
  
sum_2+=int(K)-A[-1] - (A[-1]-A[-2])
  
  
if sum_1 < sum_2:
  print(sum_1)
else :
  print(sum_2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = []
ans.append(max(a) - min(a))
i = n - 2
a.sort()
while i >= 0:
    if a[i + 1] - a[i] >= (k - 1) - a[i + 1]:
        a[i + 1] = (k - a[i + 1]) * -1
    i -= 1
ans.append(max(a) - min(a))
print(min(ans))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans1 = 0
ans2 = 0
max_num1 = 0
max_num2 = 0
for index, a in enumerate(A):
    if index == 0:
        tmp1 = A[index + 1] - a
        tmp2 = K - A[-1] + a
    elif index == N - 1:
        tmp1 = K - a
        tmp2 = a - A[index - 1]
    else:
        tmp1 = A[index + 1] - a
        tmp2 = a - A[index - 1]

    max_num2 = max(max_num2, tmp2)
    ans2 += tmp2
    max_num1 = max(max_num1, tmp1)
    ans1 += tmp1

if (ans1 - max_num1) < (ans2 - max_num2):
    print(ans1 - max_num1)
else:
    print(ans2 - max_num2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if a[0] == 0 and len(a) == 3:
    print(max(a[1:])-min(a[1:]))
    exit()
elif len(a) == 2:
    print(max(a) - min(a))
    exit()
elif a[0] == 0 and len(a) > 3:
    print(max(a))
    exit()
else:
    print(max(a) - min(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if a[0] != 0:
    print((a[n//2]-a[0])+(a[n-1]-a[n//2]))
else:
    print((a[n-1]-a[n//2]))

Submission status: Wrong Answer

Solution code:
import numpy as np
KN = input().split()
A = input().split()
K = int(KN[0])
N = int(KN[1])
a = [0]*(N+1)
a[N] = a[0]+K
for i in range(N):
    a[i] = int(A[i])
a.sort()
AM = max(a)
Am = min(a)
s = AM-Am
a = np.array(a)
print(K-(max(a[1:]-a[:-1])))

Submission status: Wrong Answer

Solution code:
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
 
    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])
    e = sum(d)
 
    most_min_path = 10000
 
    for i in range(n):
        if i == 0:
            normal_ck = e - d[i] - d[-1]
            reverse_ck = e - d[i+1]
        elif i == n - 1:
            normal_ck = e - d[i]
            reverse_ck = e - d[i+1] - d[0]
        else:
            normal_ck = e - d[i]
            reverse_ck = e - d[i+1]
 
        min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
        if most_min_path > min_ck:
            most_min_path = min_ck
 
    print(most_min_path)
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a = list(map(int,input().split()) )
j = 0
if a[j] != 0:
    print(a[n-1]-a[j])
elif a[j] == 0:
        while a[j] == 0:
            a[j] += a[j+1]    
        print(a[n-1]-a[j])

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
if l[0]==0:
    l=l[1:]
t=l[0]
p=0
for val in l:
    p+=(val-t)
    t=val
print(p)
#l=l[::-1]
#t=l[0]
#r=0
#for val in l:
#    r+=(t-val)
#    t=val
#print(min(r,p))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
right = 0
left = 0
for i in range(n-1):
    right += abs(a[i]-a[i+1])
for j in range(n-1):
    if a[len(a)-1-j] > (k//2):
        left += k - a[len(a)-1-j]
    else:
        left += a[len(a)-1-j]
print(min(right,left))

Submission status: Wrong Answer

Solution code:




from functools import reduce

k, n = map(int, input().split())
a = list(map(int, input().split()))
print(a)
far = -1
sep = -1
for i in range(1, n):
    temp = a[i] - a[i-1]
    if temp > far:
        far = temp
        sep = i
print(far, sep)
f, s = a[:sep], a[sep:]
print(f,s)
if min(s) - max(f) == far:
    combine = a

    print(combine)

    print(reduce(lambda x,y: y-x, combine))
else:
    left = reduce(lambda x,y: y-x, f)
    total = 0
    for i in range(0, len(s)):
        if i == len(s)-1:
            total += k - s[i]
        else:
            total += s[i+1]-s[i]
    print(left, total)

Submission status: Wrong Answer

Solution code:
def samax_index(A):
    samax = 0
    max_i = 0
    prev_v = 0
    for i, v in enumerate(A):
        if samax <= (v - prev_v):
            max_i = i
            samax = v - prev_v
        prev_v = v
    return max_i


def distance(A, start_i, N):
    total = 0
    if A[1] == 0:
        total = A[start_i]
    else:
        total = A[start_i] - A[1]

    if start_i == N:
        print(total)
        return
    else:
        total += A[1]
        total += A[-1] - A[start_i + 1]

    print(total)


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

A = list(map(int, input().split()))
A.append(K)
A.insert(0, 0)
start_i = samax_index(A) - 1


distance(A, start_i, N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
k,n=map(int,input().split())
a=list(map(int,input().split()))
d=360/k
r=k/math.pi/2
ans=float("inf")
for i in range(n):
  if a[i]-a[i-1]<0:
    x=(abs(a[i]-a[i-1]))*360/k
  else:
    x=(k-abs(a[i]-a[i-1]))*360/k
  l=2*math.pi*r*x/360
  ans=min(ans,l)
print(int(ans))

Submission status: Wrong Answer

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

ans1 = a[n-1] - a[0]

ans2 = a[n-2] - (a[n-1] - k)

ans3 = a[0] - (a[n-2] - k)

print(min(ans1, ans2, ans3))

Submission status: Wrong Answer

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

print(min(K - A[-1] + A[-2] - min(A), max(A)-min(A), A[0] + K - A[1]))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
max = 0
len = [0] * n
for i in range(n):
    if i == 0:
        len[i] = a[i]
    else:
        len[i] = a[i] - a[i - 1]
        if len[i] > len[max]:
            max = i
if len[max] > int(k / n) and len[max] > (k - a[n - 1]) and (a[n - 1] - a[0]) > len[max]:
    ans = (k - a[max]) + a[max - 1]
else:
    ans = a[n - 1] - a[0]
print(ans)

Submission status: Wrong Answer

Solution code:
def around(K,A):
    N2 = A.pop(0)
    A.append(N2+K)
    return A

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

X = 0
X_min = 1000000000
for j in range(0,len(A)):
    X=0
    X = A[len(A)-1] - A[0]
    X_min = X
    A=around(K,A)
    
print(X_min)

Submission status: Wrong Answer

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

T = K // 2
B = []
for i in range(N):
  if T < A[i]:
    B.append(A[i] - K)
for i in range(N):
  B.append(A[i])

dist1 = 0
dist2 = 0
for i in range(N-1):
  d1 = abs(A[i] - A[i+1])
  dist1 += d1

for i in range(N-1):
  d2 = abs(B[i] - B[i+1])
  dist2 += d2

print(min(dist1, dist2))