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()))
l=len(a)
if a[-1]-a[0]<=(k-a[-1])+a[0]:
    print(a[-1]-a[0])
else:
    for i in range(n-1):
        if a[-1]-a[i]>(k-a[-1])+a[i]:
            start=i
        else:
            print(((k-a[-1])+a[i])+(a[-1]-a[i+1]))
            break

Submission status: Wrong Answer

Solution code:
K, N = [int(x) for x in input().split()]
A = list(filter(lambda x: x != 0, (int(x) for x in input().split())))
print(int(sum(A) / len(A)))

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))
ans_2 = max(a) - min(a)
ans = min(ans_1, ans_2)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(n):
  if i == n-1:
    distance.append(abs(k - a[i]))
  else:
  	distance.append(abs(a[i+1] - a[i]))
distance.sort()
print(sum(distance[:-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
k,n = map(int,input().split())
a = deque(list(map(int,input().split())))
b = []
for i in range(n):
	b.append(a[i]-k)
b = deque(reversed(sorted(b)))
for i in range(n):
	a.appendleft(abs(b[i]))
a = list(a)
x = []
#print(a)
for i in range(len(a)-n):
    c = sum(a[i:i+n])
    x.append(c)
print(min(x))

Submission status: Wrong Answer

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

ans1=max(a)-min(a)
ans2=abs((a[-1]-k)-a[-2])
#print((a[-1]-k),a[-2],ans2)

print(min(ans1,ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    d = [A[i]-A[i-1] for i in range(1,N)]
    ans = min(K-max(d), max(d))
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
K, N = [int(x) for x in input().split()]
A = list(filter(lambda x: x != 0, (int(x) for x in input().split())))
print(sum(A) / len(A))

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
c=list(map(int,input().split()))
c.sort()
d=[0]*a
for i in range(b-1):
  d[i]+=abs(c[i]-c[i+1])

d.append(a-c[-1])

print(a-max(d))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
t = 0
id = 0
l = 0
for i in range(n):
    if i < n - 1:
        d = abs(a[i] - a[i + 1])
    else:
        d = k - a[i]
    if t < d:
        t = d
        id = i + 1 if i < n - 1 else 0
for i in range(n - 1):
    l += abs(a[(i + id) % n] - a [(i + id + 1) % n]) if i + id + 1 < n - 1 else k - a[n - 1]
print(l)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import sys
input=lambda :sys.stdin.readline().rstrip()

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

S = [0] + list(accumulate(A))
d = [S[i] - S[i - k] for i in range(k, n + 1)]
print(d)

Submission status: Wrong Answer

Solution code:
def main():
    input1 = input()
    input2 = input()
    k , n = map(int , input1.split())
    al = list(map(int , input2.split()))
    arg = [[k,n],al]
    ans = process(arg)
    print(ans)
    
def process(arg):
    ans = 0
    ensyu = arg[0][0]
    kensu = arg[0][1]
    aida = list()
    for x in range(arg[0][1]):
        mae = arg[1][x-1]
        b = arg[1][x]
        if x == 0:
            # 1件目は演習
            w = b + ensyu - arg[1][kensu-1]
        else:
            w = b - mae
        aida.append(w)

    m = max(aida)
    flg = False
    for y in aida:
        if flg == True or y != m :
            flg = True
            ans = ans + y
    return ans

def getInput(input):

    return 

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# print(num_list)

# 1
num_list.sort()


# print(num_list)


# 2 
dis_list = [num_list[i+1] - num_list[i] for i in range(N-1)]


last_dis = (K-num_list[-1])+num_list[0] if num_list[-1] > (K//2) else num_list[-1] - num_list[0]
dis_list.append(last_dis)

max_dis = max(dis_list)

half = K // 2
ans = K-max_dis if max_dis > half else max_dis

print(ans)

Submission status: Wrong Answer

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

i=0
d=0
P=0
r=0

while(i<N-1):
  if(d<list[i+1]-list[i]):
    d=list[i+1]-list[i]
    p=i+1
  i+=1

j=0
k=p
l=0
m=0
s=0

if(K-list[N-1]<=d):
  while(j<p-1):
    r+=list[j+1]-list[j]
    j+=1
  while(k<N-1):
    r+=list[k+1]-list[k]
    k+=1
  r+=K-list[N-1]
  while(m<N-1):
    s+=list[m+1]-list[m]
    m+=1
  if(r<s):
    print(r)
  else:
    print(s)

else:
  while(l<N-1):
    r+=list[l+1]-list[l]
    l+=1
  print(r)

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-2):
    if (A[i+2]-A[i+1])>=(A[i+1]-A[i]):
        l=A[i+2]-A[i+1]
if (20-A[N-1]+A[0])>=l:
    l=20-A[N-1]+A[0]
print(K-l)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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
flag = False
for ele in list:
    ans += ele

max = max(list)
if a[n-1]-a[0] >= k/2:
    secondMax = sorted(list)[-2]
    ans -= max+secondMax
else:
    ans -= max
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
max_num = 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]

    if tmp1 > tmp2:
        max_num = max(max_num, tmp2)
        ans += tmp2
    else:
        max_num = max(max_num, tmp1)
        ans += tmp1

print(ans-max_num)

Submission status: Wrong Answer

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

low = 0
high = 0

low = min(a)
high = max(a)

length = -1 * ((a[N-1] - K) - a[1])
if a[N-1] - a[0] >= length:
    print(length)
else:
    print(high - low)

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
    if i == len(A)-1 and (A[0]+abs(K-A[-1]))>max_v:
        print(a1)
        exit()

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


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

Submission status: Wrong Answer

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

mdiff = 0
for i in range(n):
    mdiff = max(mdiff, abs(k + aa[i] - aa[i - 1]) % k)
print(min(mdiff, k - mdiff))

Submission status: Wrong Answer

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(n):
  if i == (n-1):
    distance.append(k - a[i])
  else:
    distance.append(a[i+1] - a[i])
distance.sort()
print(int(sum(distance[:-1])))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import copy
k,n = map(int,input().split())
a = [int(i) for i in input().split()]
b=[]
bf=0
c=a[0]
if a[0]!=0:
    lis_=[]
    lis_.append(a[0]+k-a[-1])
    for i in a[1:]:
        lis_.append(i-c)
        c=i
    e=k-lis_[-1]
    f=k-lis_[0]
    g = lis_[-1]+lis_[0]
    if e==f==g:
        print(e)
    elif e<f and e<g:
        print(e)
    elif f<e  and f<g :
        print(f)
    else:
        print(g)

else:
    lis_=[]
    lis_.append(k-a[-1])
    for i in a[1:]:
        lis_.append(i-c)
        c=i

    e = k-lis_[-1]
    f= k-lis_[0]

    g=lis_[-1]+lis_[0]

    if e==f==g:
        print(e)
    elif e<f and e<g:
        print(e)
    elif f<e  and f<g :
        print(f)
    else:
        print(g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sorted_A = sorted(A)
sorted_A = [i - sorted_A[0] for i in sorted_A]

print(sorted_A)

dis = []
for i in range(len(sorted_A)-1):
    dis.append(abs(sorted_A[i] - sorted_A[i+1]))

last_dis = abs(sorted_A[-1] - sorted_A[0])

if(last_dis >= K/2):
    last_dis = K - last_dis

dis.append(last_dis)

print(K - max(dis))

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)

#print(listB)
out1 = max(listA) - min(listA)
out2 = max(listB) - min(listB)
#print(out1)
#print(out2)
if out1 >= out2:
  print(out2)
else:
  print(out1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(n):
  if i == n-1:
    distance.append(k - a[i])
  else:
  	distance.append(a[i+1] - a[i])
distance.sort()
print(sum(distance[:-1]))

Submission status: Wrong Answer

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

for i in range(n):
  if i != n-1:
    li.append(l[n-i-1]-l[n-i-2])
  else:
    li.append(l[n-1]-l[0])

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
def cal(N, A):
    ans = 0
    for i in range(N - 1):
        ans += abs(A[i] - A[i + 1])
    return ans

K, N = map(int, input().split())
A = list(map(int, input().split()))
m = K // 2
lst_a = [K]

if 0 not in A:
    lst_b = [0] + [i for i in A if i <= m]
    lst_a = A[len(lst_b) - 1:] + lst_a
else:
    lst_b = [i for i in A if i <= m]
    lst_a = A[len(lst_b):] + lst_a

ans1 = cal(len(lst_b), lst_b) + cal(len(lst_a), lst_a)
ans2 = cal(N, A)

print(min(ans1, ans2))

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 sum(dx[1:-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    input1 = input()
    input2 = input()
    k , n = map(int , input1.split())
    al = list(map(int , input2.split()))
    arg = [[k,n],al]
    ans = process(arg)
    print(ans)
    
def process(arg):
    ans = 0
    ensyu = arg[0][0]
    kensu = arg[0][1]
    aida = list()
    for x in range(arg[0][1]):
        mae = arg[1][x-1]
        b = arg[1][x]
        if x == 0:
            # 1件目は演習
            w = b + ensyu - arg[1][kensu-1]
        else:
            w = b - mae
        aida.append(w)

    m = max(aida)
    flg = False
    for y in aida:
        if flg == True or y != m :
            ans = ans + y
    return ans

def getInput(input):

    return 

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

# x = distance between the first and last house
x = (a[-1] - a[0])
# y = k - x (the "other" distance) 
y = (k - x)

if x < y:
  print (x)
elif y < x:
  print (y)
else:
  print (x)

Submission status: Wrong Answer

Solution code:
K, N = [int(x) for x in input().split()]
A = list(filter(lambda x: x != 0, (int(x) for x in input().split())))
print(max(A) - min(A))

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:
    print(abs(min(a[1:])-max(a[1:])))
else:
    print(max(a) - min(a))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
KN = input().split()
K = int(KN[0])
N = int(KN[1])
#print(K,N)

A = input().split()
A_np = np.append(np.array(A, dtype=np.int64), K)
A_np_lag = np.append(A_np[1:], A_np[0])
A_diff = A_np_lag[:N] - A_np[:N]
dist_np = np.append(A_diff, A_diff)
total_dist = []
for i in range(N):
    #print(dist_np[i:(i+N-1)])
    temp_total_dist = dist_np[i:(i+N-1)].sum()
    total_dist.append(temp_total_dist)

print(min(total_dist))

Submission status: Wrong Answer

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

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

A_sorted=sorted(A)

print(A_sorted[N-1]-A_sorted[0])

Submission status: Wrong Answer

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

m = 0
p = 0
for n in range(0, N):
    if (n == N - 1):
        if (K - a[n] >= m):
            m = K - a[n]
            p = n
    elif ((a[n + 1] - a[n]) >= m):
        m = a[n + 1] - a[n]
        p = n

if (p == N - 1):
    print(a[p] - a[0])
else:
    print(K - a[p + 1] + a[p])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=input().split()
A=input().split()
n=0
m=1
S=list()
for i in range(int(N)-1):
  S.append(int(A[n+1])-int(A[n]))
  n=n+1
S.append(int(A[0])+(int(K)-int(A[-1])))

ans=int(K)-int(max(S))
print(ans)
print(S)

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]) ]
x.append(K-A[-1])

#　求めるのは　K　－　max(隣と離れた家の距離)
kyori_min = K - max(max(x) , x[0] + x[-1])
print(kyori_min)

Submission status: Wrong Answer

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

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]):
    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())
p = list(map(int,input().split()))
d = []
for i in range(n - 1):
  d.append(p[i + 1] - p[i])
d.append(k - p[n - 1])
d.sort()
print(d)
del d[-1]
print(sum(d))

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

dist = 0
for i in range(N-1):
  d = abs(B[i] - B[i+1])
  dist += d

print(dist)

Submission status: Wrong Answer

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

d = 0

for i in range(1,n):
    d += (arr[i] - arr[i-1])
    
print(d)

Submission status: Wrong Answer

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

list_house = list(map(int, input().split()))
index = 0
distance = 0
ans = []
while index != len(list_house) - 1:
    temp = list_house[0:index]
    del list_house[0:index]
    list_house = list_house + temp
    for i in range(len(list_house) - 1):
        if list_house[i + 1] > list_house[i]:
            distance += list_house[i + 1] - list_house[i]
        else:
            distance += list_house[i] - list_house[i + 1]

    print(list_house, distance)
    index += 1
    ans.append(distance)
    distance = 0
print(min(ans))

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-2):
  x = max(alist[i+1]-alist[i], alist[i+2]-alist[i+1])
b = k-alist[n-1]+alist[0]
c = max(x,b)
answer = k-c
print(answer)

Submission status: Wrong Answer

Solution code:
k,n = map(int,raw_input().split())
l = map(int,raw_input().split())
sm = 0
ku = k/2
for i in range(1,n):
    # print l[i]
    if l[i]>ku:
        # print (ku-l[i]), (l[i]-l[i-1])
        sm+= min(abs(ku-l[i]), (l[i]-l[i-1]))
    else:
        sm+= (l[i]-l[i-1])


print sm

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#X = int(input().strip())

K, N = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
m = 0
ind = 0
ans = 0

for i in range(N-1):
  _m = A[i+1] - A[i]
  if _m > m:
    m = _m
    ind = i+1
    last = i

_m = K - A[N-1]
if _m > m:
  m = _m
  ind = 0
  last = N-1

#print(m)
#print(ind)
#print(last)
i = ind
while i != last:
  #print(ans)
  if i < N-1:
    ans += A[i+1] - A[i]
    i += 1
  else:
    ans += K - A[i]
    i = 0

print(ans)

Submission status: Wrong Answer

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

d = input().split(" ")
dist = []

for i in range(0,n):
    dist.append(int(d[i]))

ex = dist[0] + k - dist[n-1]
res = dist[n-1] - dist[0]

for i in range(i, n):
    diff = dist[i] - dist[i-1]
    aux = res + ex - diff
    if aux < res:
        res = aux
        ex = diff

print(str(res))

Submission status: Wrong Answer

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

k = int(k/2)

#print(list(map(lambda x:k -x,A)))

print(sum(list(map(lambda x:k -x,A))))

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 = a[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[0]
else:
    ans = a[i - 1] + (k - a[i])

print(ans)

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
    AA_list = []
    for A in A_list:
        if A < H :
            AA_list.append(A)
        else :
            A = A - H
            AA_list.append(A)
    AA_list.sort()
    #print(str(AA_list))
    for A in AA_list :
        if i == 0 :
            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 H != A0 :
        value_temp = H - A0
        value_list.append(value_temp)
    else :
        value_list.append(value_temp)
    #print(str(value_list))
    max_value = max(value_list)
    ans = sum(value_list) - max_value
    print (str(ans))


if __name__ == '__main__':
    main()

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:
import sys

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

if max(a)<=harf:
  print(max(a)-min(a))
  sys.exit()
else:
  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 = map(int, input().split())
a = list(map(int, input().split()))
c = 0
left = 0
right = len(a) - 1
mid = (left + right) // 2
if abs(a[mid] - a[mid+1]) == abs(a[mid] - a[mid-1]):
    print(a[len(a)-1] - a[0])
else:
    for i in reversed(a):
        if i > (k//2):
            c += k-i
        else:
            c += i
    print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ansdiv = K // 2

for i, j in zip(A[:-1], A[1:]):
    div = j - i
    if div > ansdiv:
        ansdiv = div

last = A[N-1] - A[0]
if last > ansdiv:
    ansdiv = last

print(ansdiv)

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:
  A.append(K)
  d = []
  for i in range(N):
    d.append(abs(A[i]-A[i+1]))
  ans = K-max(d)
print(ans)

Submission status: Wrong Answer

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

ans=[]
for i in range(n-1):
  ans.append(A[-1]-A[i]+A[max(0,i-1)])
print(min(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 = a[0]

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

if big == 0:
    ans = a[n - 1] - a[0]
else:
    ans = a[i - 1] + (k - a[i])

print(ans)

Submission status: Wrong Answer

Solution code:
(n,m)=map(int,input().split())
a=input().split()
a=list(map(int,a))
l=[]
k=0
a=sorted(a)
for i in range(m-1):
    d=abs(a[i]-a[i+1])
    k=k+d
    l.append(d)
# k=k-d
c=n-k
print(c)
l=sorted(l)
l1=sum(l)
print(l1-l[-1])

Submission status: Wrong Answer

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

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(a1) if a1<a2 else print(a2)

Submission status: Wrong Answer

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

l = [0] * N
hoge = 0

for n in range(N):
  l[n] = A[n]-hoge
  hoge = hoge + l[n]
  
l = l + [K-A[N-1]]

print(sum(l)-max(l))

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

first=l[1]-l[0]
last=n-l[k-1]
s=first
for i in range(1,k-1):
	z=abs(l[i]-l[i-1])
	s=s+z
	if(z>first):
		first=z
	if(first>last):
		s=s-first
		s=s+last
print(s)

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:
  A.append(K)
  d = []
  for i in range(N):
    d.append(A[i]-A[i+1])
  ans = K-max(d)
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
D = [[0 for _ in range(N)] for _ in range(N)]
D[1][1] = 20
for i in range(N-1):
  for j in range(i+1,N):
    a = abs(A[i]-A[j])
    D[i][j] = min(a, K-a)
    D[j][i] = min(a, K-a)
ans = K*N
for k in range(N-1):
  ans = min(ans,sum(D[k]))
print (ans)

Submission status: Wrong Answer

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

d = input().split(" ")
dist = []

for i in range(0,n):
    dist.append(int(d[i]))

ex = dist[0] + k - dist[n-1]
res = k - ex

for i in range(1, n):
    diff = dist[i] - dist[i-1]
    aux = res + ex - diff
    ex = diff
    
    if aux < res:
        res = aux

print(str(res))

Submission status: Wrong Answer

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

K2 = K / 2

l_replace = [K2 if i == 0 else i for i in A]

hoge1 = max(l_replace) - min(l_replace)
hoge2 = K -max(l_replace) + min(l_replace)

if hoge1 > hoge2:
    print(hoge2)
else:
    print(hoge1)

Submission status: Wrong Answer

Solution code:

k,n =list(map(int,input().split()))
a = list(map(int,input().split()))
ans = [0]*n 
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(min(kai,ans_a))

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(a1) if a1>a2 else print(a2)

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
bb = min(lists)
for i in range(len(lists)):
    if abs(lists[i] - bb) == 0:
        continue
    elif abs(lists[i] - bb) < b:
        b = abs(lists[i] - bb)

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

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
    prev_v = 0
    if A[1] == 0:
        loop = 0
    else:
        loop = 1

    for i in range(start_i, 0, -1):
        loop += 1
        total += A[i] - A[i - 1]
        if loop == N:
            print(total)
            return

    for i in range(len(A) - 1, start_i + 1, -1):
        loop += 1
        total += A[i] - A[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:
KN = list(map(int, input().split()))
A = list(map(int, input().split()))

answer = []

for i,home in enumerate(A):
    if home < A[i - 1]:
        answer.append(home - A[i - 1] + 20)
    else:
        answer.append(home - A[i - 1])

print(20 - max(answer))

Submission status: Wrong Answer

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

for i in range(n):
    p = abs(a[i] - a[i-1])
    if abs(a[i] - a[i-1]) > k / 2:
        p = k - p
    d = max(d, p)

print(min (d, k - d))