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])
 
most_min_path = 10000
 
for i in range(n):
    if i == 0:
        normal_ck = k - d[i] - d[-1]
        reverse_ck = k - d[i+1]
    elif i == n - 1:
        normal_ck = k - d[i]
        reverse_ck = k - d[i+1] - d[0]
    elif 0 < i < n - 1:
        normal_ck = k - d[i]
        reverse_ck = k - 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:
import sys
from copy import deepcopy
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def cal(a,b):
    return min(abs(a-b), 20-abs(a-b))

def cal2(l):
    n = len(l)
    ans = 0
    for i in range(n-1):
        ans += l[i+1] - l[i]
    return ans
def main():
    k,n = map(int, input().split())
    As = list(map(int, input().split()))
    le = list()
    for i in range(n):
        if i == n-1:
            le.append(cal(As[i], As[0]))
        else:
            le.append(cal(As[i], As[i+1]))
    print(k-max(le))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
ans=0
t=[]
s=K/2
for i in range(N):
    t.append(A[i]-s)
ans=K-max(t)*2
print(int(ans))

Submission status: Wrong Answer

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

dis = (K-A[-1]) + A[0]
for i in range(len(A)-1):
    if A[i+1] - A[i] > dis:
        ans = K - (A[i+1] - A[i])
    else:
        ans = K - dis
print(ans)

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(K - np.max(distance))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
readline = sys.stdin.buffer.readline
K, N = map(int, readline().split())
A = list(map(int,input().split()))
N1= -K+A[N-1]
L1=abs(N1)+A[N-2]
L2=A[N-1]-A[0]
c = min(L1,L2)
print(c)

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)

A= A[::-1]
C = []
for i in range(N):
    C.append(A[i+1] - A[i])
C = sorted(C,reverse = False)


print(min(abs(sum(B[:N-1])),abs(sum(C[:N-1]))))

Submission status: Wrong Answer

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

    elif i<len(A)-1:
        dist.append(K-A[i+1]-A[i])
        dist.append(A[i+1]-A[i])

target=max(dist)
print(K-target)

Submission status: Wrong Answer

Solution code:
def main():
    K, N = map(int, input().split(' '))
    half_k = K // 2
    A = list(map(int, input().split()))
    A.sort()
    a_sub = A[-1] - A[0]
    if A[-1] > half_k:
        a_sub = (A[-1] - half_k) + A[0]
    if a_sub < 0:
        a_sub *= -1
    for n in range(N - 1):
        if A[n + 1] - A[n] > a_sub:
            a_sub = A[n + 1] - A[n]
    print(K - a_sub)


if __name__ == '__main__':
    main()

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] = 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:
I = input().split()
K = int(I[0])
N = int(I[1])

I = input().split()

A = [ int(I[0]) ]
L = [ A[0] ]
for i in range(1,N):
  A.append( int(I[i]) )
  L.append( A[i] - A[i-1] )

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

L.sort()
print(L[N-1])

Submission status: Wrong Answer

Solution code:

KN = input("").split()
A = input("").split()

K = int(KN[0])
N = int(KN[1])
A_0 = int(A[0])
A_N = int(A[N-1])
L = A_0

for j in range(N-1):
    A_i = int(A[j+1])
    A_m = int(A[j])
    if A_i - A_m > L:
        L == A_i - A_m

if L > K - A_N + A_0:
    print(K-L)
else:
    print(K - A_N + A_0)

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
a = True
for n in range(N):
  b = True
  if A[n] == K/2:
    a = False
  if A[n] > K/2:
    A[n] = (K - A[n]) * -1
    b = False
  if maxNum < A[n]:
    maxNum = A[n]
  if minNum > A[n]:
    minNum = A[n]
  if b == False:
    A[n] = (K + A[n])
res = min(maxNum - minNum, res)
if a == False:
  minNum,maxNum = K, 0
  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))
else:
  print(res)

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
if A[0]==0:
    lastlength = K-A[N-1]
    if lastlength >max2:
        max2=lastlength
else:
    lastlength = A[N-1]-A[0]
    if lastlength>max2:
        max2=lastlength
print(K-max2)

Submission status: Wrong Answer

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

I = input().split()

A = [ int(I[0]) ]
L = [ A[0] ]
for i in range(1,N):
  A.append( int(I[i]) )
  L.append( A[i] - A[i-1] )

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

A.sort()
print(L[N-1])

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
if A[0]==0:
    lastlength = 20-A[N-1]
    if lastlength >max2:
        max2=lastlength
else:
    lastlength = A[N-1]-A[0]
    if lastlength>max2:
        max2=lastlength
print(20-max2)

Submission status: Wrong Answer

Solution code:
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
ans=0
t=[]
s=K/2
for i in range(N):
    t=[[A[i]-s]]
tt=max(t)
ans=K-tt[0]*2
print(int(ans))

Submission status: Wrong Answer

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

A.sort()
d = []
for i in range(N):
    if i != N-1:
        d.append(A[i+1]-A[i])
    else:
        d.append(K-A[N-1])

ans = 10**9
t = sum(d[:N-1])
for j in range(N):
    if ans > t:
        ans = t
    t = t-d[0]+d[N-1]
    d.append(d.pop(0))

print(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
a = True
for n in range(N):
  b = True
  if A[n] == K/2:
    a = False
  if A[n] > K/2:
    A[n] = (K - A[n]) * -1
    b = False
  if maxNum < A[n]:
    maxNum = A[n]
  if minNum > A[n]:
    minNum = A[n]
  if b == False:
    A[n] = (K + A[n])
res = min(maxNum - minNum, res)
if a == False:
  minNum,maxNum = K, 0
  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=list(map(int,input().split()))
l=0
for i in range(N):
    if (A[i]>K):
        A[i]=A[i]%K
A.sort()
for i in range(N-2):
    if (A[i+2]-A[i+1])>=(A[i+1]-A[i]):
        l=A[i+2]-A[i+1]
if (K-A[N-1]+A[0])>=l:
    l=K-A[N-1]+A[0]
print(K-l)

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)
        value_temp = A00
        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())
A=list(map(int,input().split()))
x=[]
y=[]
for i in A:
  if i>10:
    a=20-i
    y.append(a)
  else:
    x.append(i)

if len(x)==0:
  ans=max(y)-min(y)
elif len(y)==0:
  ans=max(x)-min(x)
else:
  v=[(max(x)+max(y)),(10-min(x)+max(y)),(max(x)+10-min(x)),(10-min(x)+10-min(y))]
  ans=min(v)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = [int(i) for i in input().split()]
a = np.array([int(i) for i in input().split()])
print(a)
print((sum(a)+sum(k - a))//(2*n))

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
diff = np.diff(a_list)

max_dist = diff[0]
max_i = 0

for i in range(len(diff)):
    if max_dist < diff[i]:
        max_dist = diff[i]
        max_i = i+1
# print(max_dist, max_i)


a_list = [a_list[i] if i < max_i else a_list[i] -
          k for i in range(len(a_list))]
# print(a_list)
a_list.sort()
a_list = np.array(a_list)
print(sum(np.diff(a_list)))

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):
    print(start_i)
    if (start_i == 0) or ((start_i + 1) == N):
        total = A[start_i] - A[1]
    else:
        total = A[start_i]
        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()))
dist=[]
for i in range(len(A)):
    if i==0:
        dist.append(K-A[len(A)-1]+A[i])
        dist.append(A[i+1]-A[i])

    elif i<len(A)-1:
        dist.append(K-A[i+1]+A[i])
        dist.append(A[i+1]-A[i])

target=max(dist)
print(K-target)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
sa=[]
be=k

for i in a:
  sa.append(be-i)
  be=i
  
print(sum(sa)-max(sa))

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)
# print("右回り")
# print(a[n-1]-a[0])
# print("左回り")
# print(list[n]+list[0])
if list[n]+list[0] >= a[n-1]-a[0]:
    if a[n-1] == k/2:
        ans -= max
    else:
        secondMax = sorted(list)[-2]
        ans -= max+secondMax
else:
    ans -= max

print(ans)

Submission status: Wrong Answer

Solution code:
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.insert(0, 0)
ans = 0
for i in range(1, n):
    b, c = a[i] - a[i - 1], a[i + 1] - a[i]
    if b != 0 and c != 0:
        ans += min(b, c)
    else:
        ans += max(b, c)
print(ans)

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)
print("右回り")
print(a[n-1]-a[0])
print("左回り")
print(list[n]+list[0])
if list[n]+list[0] >= a[n-1]-a[0]:
    if a[n-1] == k/2:
        ans -= max
    else:
        secondMax = sorted(list)[-2]
        ans -= max+secondMax
else:
    ans -= max

print(ans)

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

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

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

print(som)

Submission status: Wrong Answer

Solution code:
l, num = input().split()
point = input().split()
 
l = int(l)
num = int(num)
 
point0 = int(point[0])
 
rpoint = -1
for p in point:
  p = int(p)
  if l / 2 > p:
  	rpoint = p
    
lpoint = int(point[-1])
 
lpoint1 = -1
for p in point[::-1]:
  a = l - int(p)
  if(a > l / 2):
    break
  if  a > lpoint1:
    lpoint1 = a
    
#全て左
if(rpoint == -1):
  print(lpoint - rpoint)
#全て右
elif rpoint == lpoint:
  print(lpoint - point0)
else:
  if rpoint == point0:
    if(lpoint - rpoint < rpoint + l - lpoint):
      print(lpoint - rpoint)
    else:
      print(rpoint + l - lpoint)
  else:
    ltop = rpoint - point0 + point0 + l - lpoint1
    lunder = rpoint - point0 + lpoint - rpoint
    if ltop > lunder:
      print(lunder)
    else:
      print(ltop)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def cal(a,b):
    return min(abs(a-b), 20-abs(a-b))

def cal2(l):
    n = len(l)
    ans = 0
    for i in range(n-1):
        ans += l[i+1] - l[i]
    return ans
def main():
    k,n = map(int, input().split())
    As = list(map(int, input().split()))
    As.append(k)
    lf = list()


    graph = [None] *n
    for i in range(n):
        if i == n-1:
            graph[i] = [0, i-1]
        elif i == 0:
            graph[i] = [i+1, n-1]
        else:
            graph[i] = [i+1, i-1]
    print(graph)




    nax = 10**9
    # dp[i] := iから(i+k-1)%k の頂点に行くときの距離(右回り)
    # dp[i] = dp[i-1] - (As[i]- As[i-1]) + (As[(i+k-1)%k] - As[(i+k)%k])
    dp = [None]*n
    dp[0] = As[n-1] - As[0]
    for i in range(1, n):
        dp[i] = dp[i-1] -(As[i]-As[i-1]) + abs(As[(i+n)%(n)] - As[(i+n)%(n)-1])
        print(As[(i+k+2)%(n+1)] , As[(i+k+1)%(n+1)])

    tf = False
    As = As[:n]
    if As[0] != 0:
        As.append(0)
    else:
        tf = True

    As = As[::-1]
    print(As)
    pp = [None]*n
    pp[0] = As[n-1] - As[0]
    for i in range(1, n):
        pp[i] = pp[i-1] -(As[i]-As[i-1]) + abs(As[(i+n)%(n)] - As[(i+n)%(n)-1])
    
    print(pp)
    print(dp)
if __name__ == '__main__':
    main()

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/2:
    d.append((k / 2 - 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())
a = list(map(int, input().split()))

# 時計回り
if a[0] + a[n-1] <= k - a[0] - a[n-1]:
    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:
k, n = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
    a.remove(0)
a.sort()
ans = 0
l = [k-i for i in a]
n = 1
for i in l:
    try:
        ans += l[n] - l[n-1]
        n += 1
    except:
        pass
print(-ans)

Submission status: Wrong Answer

Solution code:

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

A_sorted=sorted(A)

if A_sorted[N-1]-A_sorted[0] <K/2:
    print(A_sorted[N-1]-A_sorted[0])

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

    a=A_sorted[N-1]-A_sorted[0]
    b=K-a

    print(min(a, b))

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split( ))
a=sorted(list(map(int,input().split( ))),reverse=True)
for i in range(n):
 a.append(int(a[i])+k)
b=sorted(a)
w=10000000
for i in range(2*n-2):
 if a[i+2]-a[i]<w:
  w=a[i+2]-a[i]
print(-w)

Submission status: Wrong Answer

Solution code:
K, N = input().split()
A = list(map(int, input().split()))
if 0 in A:
    A.remove(0)
print(max(A)-min(A))

Submission status: Wrong Answer

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

    elif i<len(A)-1:
        dist.append(K-A[i+1]+A[i])
        dist.append(A[i+1]-A[i])

target=max(dist)
print(K-target)

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[N-1]-A[0]
  
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:
k,n = map(int,input().split())
a_list = list(map(int,input().split()))
k2=k//2
a2=a_list[n-1]-k2
if n==2:
  print(min(a_list[n-1]-a_list[0],k-a_list[n-1]+a_list[0]))
else:
  if a_list[0]==0:
    a_list[0]=a_list[1]
  ans='small'
  for i in range(n-1):
    if a_list[i] <= a2:
      for j in range(n-1):
        if a_list[j] > a2:
          ans='big'
          break
    if a_list[i] > a2:
      ans='big'
      break
  if ans=='big':
    print(a_list[n-1]-a_list[0])
  else:
    print(k-a_list[n-1]+a_list[0])

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(abs(a[n-1]-a[n//2]))

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 / 2 - 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 = (int(x) for x in input().split())
A = list(map(int, input().strip().split()))

if (0 not in A) and (K not in A):
    print(max(A) - min(A))
else:
    a = max(A) - min(A)
    k = []
    for n in range(N):
        if n + 1 == N:
            pass
        else:
            k.append(A[n + 1] - A[n])
    b = K - max(k)
    if a > b:
        print(b)
    else:
        print(a)

Submission status: Wrong Answer

Solution code:
def resolve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    max_distance = 0
    distance = []

    for i in range(1, len(A)):
        tmp = A[i] - A[i - 1]
        distance.append(tmp)
        if max_distance < tmp:
            max_distance = tmp
            start_index = i
            goal_index = i - 1

    ans = sum(distance[start_index:goal_index])

    if (start_index > goal_index):
        ans = sum(distance[start_index:]) + \
            sum(distance[:goal_index]) + K - A[-1]
    print(ans)


resolve()

Submission status: Wrong Answer

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

# 時計回り
if a[0] + a[n-1] >= k - a[0] - a[n-1]:
    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:
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:
B = list(map(int,input().split()))
A = list(map(int,input().split()))

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

Submission status: Wrong Answer

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

hanbun = k /2
a.sort

i = 0
while True:
    if a[i] != 0:
        end = a[i]
        break
    else:
        i += 1
start = a[n-1]


result = start - end
if result < 0:
    result = result * -1
print(result)

Submission status: Wrong Answer

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

As.append(K)
if As[0] != 0:
  As.append(K + As[0])
else:
  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:
import numpy
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(a[0])
c = numpy.diff(b)
d = numpy.sort(c)
print(numpy.sum(d)-d[len(d)-1])

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())
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]
if k-a[-1]>maxx: maxx=k-a[-1]
print(k-maxx)

Submission status: Wrong Answer

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

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:
        if a == K:
            tmp1 = A[0]
        else:
            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:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n, m = input_list()
  a = input_list()
  sa = sorted(a)
  mi = min(a)
  ma = max(a)
  pa = sa[-2] + (n-ma)
  pb = ma - mi
  print(min(pa, pb))
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

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[-1] - A_list[0] <= K/2:
    print(A_list[-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 = 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))
if 0 not in distance_list:
    distance_list.remove(max(distance_list))
print(sum(distance_list))

Submission status: Wrong Answer

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

for _ in range(len(a)-1):
    if diff < abs(a[_]-a[_+1]):
        diff = abs(a[_]-(a[_+1]))

print(k-diff)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=0
s=[0]*n
#間隔を求める
for i in range(n-1):
    s[i]=a[i+1]-a[i]
#間隔を足す
for i in range(n):
  if s[i]<k/2:
    b=b+s[i]
  else:
    con=k-s[i]
    print(con)
    exit()
  
print(b)

Submission status: Wrong Answer

Solution code:
syu,n=map(int,input().split())
a=[int(i) for i in input().split()]
clock=a[n-1]-a[0]
han_clock=(min(a[n-2]-a[0],a[0]+syu-a[n-2]))+(a[0]+syu-a[n-1])
print(min(clock,han_clock))

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):
    if start_i == N:
        total = A[start_i] - A[1]
    else:
        total = A[start_i]
        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()))
ans=0
if a[-1]-a[0]<=(k-a[-1])+a[0]:
    ans=a[-1]-a[0]
else:
    for i in range(n-1):
        if a[-1]-a[i]>=(k-a[-1])+a[i]:
            ans=((k-a[-1])+a[i])+(a[-1]-a[i+1])
print(ans)

Submission status: Wrong Answer

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

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

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)
# print("右回り")
# print(a[n-1]-a[0])
# print("左回り")
# print(list[n]-list[0])
if list[n]+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:
K, N = input().split()
A = list(map(int, input().split()))
if 0 in A:
    A.remove(0)
x = max(A) - min(A)
print(x)

Submission status: Wrong Answer

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

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

point0 = int(point[0])

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

lpoint1 = -1
for p in point[::-1]:
  a = l - int(p)
  if(a > l / 2):
    break
  if  a > lpoint1:
    lpoint1 = a
    
#全て左
if(rpoint == -1):
  print(lpoint - rpoint)
#全て右
elif rpoint == lpoint:
  print(lpoint - point0)
else:
  if rpoint == point0:
    if(lpoint - rpoint < rpoint + l - lpoint):
      print(lpoint - rpoint)
    else:
      print(rpoint + l - lpoint)
  else:
    ltop = rpoint - point0 + point0 + l - lpoint1
    lunder = rpoint - point0 + lpoint - rpoint
    if ltop > lunder:
      print(lunder)
    else:
      print(ltop)