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_s = []
check = 0
for i in range(0,N):
    if(A[i] == 0):
        check =1
if(check == 1):
    A.append(K)
    for i in range(1,N+1):
          l_s.append(A[i]-A[i-1])
    num = 0
    ans = 99999999999999999
    for i in range(0,N):
        num = K - l_s[i]
        ans = min(ans,num)
    print(ans)
    
else: 
    #A.append(K-A[len(A)-1])
    m1 = 0
    m2 = 0
    m1 = max(A)
    m2 = min(A)
    ans = m1 -m2
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    arg = getInput(1)
    # arg = getInput(input())
    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:
            w = b + arg[1][kensu-1]
        else:
            w = b - mae
        aida.append(w)

    m = max(aida)
    for y in aida:
        if y != m:
            ans = ans + y
    return ans

def getInput(input):
    # 行数
    n = 2
    # 戻り値
    ret = None
    ret2 = None
    if n == 1:
        pass
        # ret = input
        # ret = list(input)
        # ret = int(input)
        # ret = input.split()
        # ret = list(map(int, input.split()))
        # ret = input.split('T')
    else:
        for x in range(n):
            # 取得したい形式に変換
            if x == 1:
                # ret = list(map(int, input[x].split()))
                ret = list(map(int, '20 3'.split()))
            else:
                # ret2 = list(map(int, input[x].split()))
                ret2 = list(map(int, '5 10 15'.split()))

    return [ret, ret2]

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
s = list(input().split(' '))
sum = 0

for n in range(0, len(s)-1):
  sum += int(s[n+1]) - int(s[n])  
print(sum)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = [int(x) for x in input().split()]
if a[0] == 0:
    a.append(k)
else:
    a.append(a[0])

dis = 0
d_li = []
for i in range(1,len(a)):
    tmp = abs(a[i] - a[i-1])
    dis += tmp
    d_li.append(tmp)
    
print(dis - max(d_li))

Submission status: Wrong Answer

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

max = K - A[-1] + A[0]
B.append(max)
ind = 0
for i in range(1, N, 1):
	_max = A[i] - A[i-1]
	B.append(_max)
	if _max < 0:
		_max = K + _max
	if _max > max:
		max = _max
		ind = i
dis1 = 0
for i in range(1, N, 1):
	if i + ind >= N:
		dis1 += B[(i + ind) - N]
	else:
		dis1 += B[(i + ind)]
	print(dis1)

Submission status: Wrong Answer

Solution code:
K = 20
N = 3
A = [5, 10, 15]

ans = 0
for i in range(N - 1):
    S = [A[i] - A[i + 1]]
    S.append((K - A[N - 1]) + A[0])
    S.sort
    for n in range(N - 1):
        ans += S[i]
print(ans)

Submission status: Wrong Answer

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

min_dis = sum(distance) - max(distance)

print(min_dis)

Submission status: Wrong Answer

Solution code:
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 :
            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))
    value_temp = H - A0
    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:
def main():
    arg = getInput(1)
    # arg = getInput(input())
    ans = process(arg)
    print(ans)
    
def process(arg):
    ans = 0
    print(arg)
    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:
            w = b + arg[1][kensu-1]
        else:
            w = b - mae
        aida.append(w)

    print(aida)
    m = max(aida)
    for y in aida:
        if y != m:
            ans = ans + y
    return ans

def getInput(input):
    # 行数
    n = 2
    # 戻り値
    ret = None
    ret2 = None
    if n == 1:
        pass
        # ret = input
        # ret = list(input)
        # ret = int(input)
        # ret = input.split()
        # ret = list(map(int, input.split()))
        # ret = input.split('T')
    else:
        for x in range(n):
            # 取得したい形式に変換
            if x == 1:
                # ret = list(map(int, input[x].split()))
                ret = list(map(int, '20 3'.split()))
            else:
                # ret2 = list(map(int, input[x].split()))
                ret2 = list(map(int, '5 10 15'.split()))

    return [ret, ret2]

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 1行目の入力を空白で分割された数値として扱う
K, N = map(int, input().split())
# 2行目の入力を数値のリストとして扱う
A = list(map(int, input().split()))

# 答えの準備
ans = 0
max = 0

# 距離の取得
for i in range(0, N -1):
    d = A[i+1] - A[i]
    ans += d
    if d > max:
        max = d

# 一周した最後
dl = K - A[N - 1]
if dl < max:
    ans = ans - max + dl + A[0]

# 出力
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
mini = -1
for i in a:
    tmp = a - i
    tmp = np.abs(tmp)
    if mini == -1:
        mini = max(tmp) - min(tmp)
    else:
        mini = min(mini, max(tmp) - min(tmp))
print(mini)

Submission status: Wrong Answer

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

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:
import sys
sys.setrecursionlimit(4100000)

k,n = map(int,input().split())
a = list(map(int,input().split()))
b = []
for v in range(n-1):
    b.append(a[v+1]-a[v])

b.append(k-a[n-1]+a[0])
print(b)
anss = max(b)
ans = k - anss
print(str(ans))

Submission status: Wrong Answer

Solution code:
import heapq

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

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

dis = [-abs(A[i + 1] - A[i]) for i in range(N - 1)] + [-abs(K - A[N - 1])]

heapq.heapify(dis)

heapq.heappop(dis)

print(-sum(dis))

Submission status: Wrong Answer

Solution code:
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    for i in range(n):
        if a[i] >= (k // 2):
            a[i] -= (k // 2)
    print(sum(a))


if __name__ == "__main__":
    #IOE()
    main()

Submission status: Wrong Answer

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

s = list(map(int,input().split()))
longest = []
for i in range(n-1):
    long = s[i+1] - s[i]
    longest.append(long)
if max(longest) >= k - s[n-1] + s[0]:
    print(k - max(longest))
else:
    print(k - s[n-1] + s[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if __name__ == "__main__":
    main()

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()
#print("A",A)
A_list = []
for i in range(N):
    A_list.append(int(A[i]))
A_list.append(K)
#print("A_list",A_list)

dist = []
for i in range(len(A_list)-1):
    diff = A_list[i+1] - A_list[i]
    dist.append(diff)
dist_np = np.array(dist)
#print("dist_np",dist_np)

total_dist = []
for i in range(N):
    if i < N-1:
        temp_total_dist = dist_np[i:(i+N-1)].sum()
        total_dist.append(temp_total_dist)
    if i==N-1:
        temp_total_dist = dist_np[i:(i+N-2)].sum() + dist_np[0]
        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()))

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

Submission status: Wrong Answer

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

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

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):
    total = 0
    prev_v = 0

    for i in range(start_i, 0, -1):
        total += A[i] - A[i - 1]

    for i in range(len(A) - 1, start_i + 1, -1):
        total += A[i] - A[i - 1]

    print(total)


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

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

start_i = samax_index(A) - 1


distance(A, start_i)

Submission status: Wrong Answer

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

K = 20
N = 3
A = [5, 10, 15]


def loopf(i, k, n):
    if i == N - 1:
        if K - A[N - 1] + A[0] > k:
            k = K - A[-1] + A[0]
            n = i

        return k

    if A[i + 1] - A[i] > k:
        k = A[i + 1] - A[i]
        n = i

    return loopf(i + 1, k, n)


n = loopf(0, 0, 0)

print(K - n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ascend = A[-1] - A[0]
re_A = [i if i <= K/2 else i - K  for i in A]
re_A.sort()
descend = re_A[-1] - re_A[0]
print(min([ascend, descend]))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
c = min(abs(k-max(a)), min(a))
if c == 0:
   c = k-max(a)
calc = c + abs(a[n-2]-min(a))
print(calc)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

min_dis = sum(distance) - max(distance)

print(min_dis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
a = list(map(int,input().split()))
maru = [0]*n
ans = 999999999999
maru[0] = abs(a[-1] - k)
for i in range(1,n):
    maru[i] = abs(a[i]-a[i-1])
for i in range(n-(n-1)+1):
    tmp = sum(maru[i:i+(n-1)])
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    for i in range(n):
        if a[i] > (k // 2):
            a[i] -= (k // 2)
    print(sum(a))


if __name__ == "__main__":
    # IOE()
    main()

Submission status: Wrong Answer

Solution code:
kn = input().split()
a = input().split()

al = [int(i) for i in a]
k = int(kn[0])
n = kn[1]

ml = 20 - abs(al[len(al)-1] - al[0])


for i in range(len(al)):
	if i == 0:
		ml = max(ml, abs(al[0]-al[1]))
		# print(abs(al[0])-al[len(al)-1])
	elif i == len(al)-1:
		1 == 1
		# ml = max (ml, abs(al[i]-al[i-1])) 
	else:

		ml = max(ml,(al[i-1]-al[i]),abs(al[i]-(al[i+1])))


print(k-ml)

Submission status: Wrong Answer

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

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

else:
    for i in range(N):
        if A[i] > K/2:
            A[i] -= K
    print(max(A) - min(A))

Submission status: Wrong Answer

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

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

print(out)

Submission status: Wrong Answer

Solution code:
a=input().split()
b=input().split()
l=[]
for i in b:
	l.append(int(i))
i=0
while i<len(l)-1:
	l[i+1]-=l[i]
	i+=1
print(max(l),end="")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

hanbun = k /2
a.sort()

gentenkyori = []

for i in range(n):
    if a[i] > hanbun:
        gentenkyori.append(str(-1* (k - a[i])))
    else:
        gentenkyori.append(str(a[i]))
gentenkyori.sort()
start = int(gentenkyori[0])
end = int(gentenkyori[-1])
print(end - start)

"""
result = end - start
print(result)
"""

Submission status: Wrong Answer

Solution code:
input_first = input()
input_second = input()

split_input_first = input_first.split()
split_input_second = input_second.split()

k = int(split_input_first[0])
n = int(split_input_first[1])

distance_list = []
for i in range(n):
    if i == n - 1:
        distance_list.append(k + int(split_input_second[0]) - int(split_input_second[n-1]))
    else:
        distance_list.append(int(split_input_second[i+1]) - int(split_input_second[i]))

sorted(distance_list)
result = 0
for i in range(n):
    if i == n - 1:
        break
    else:
        result += distance_list[i]
print(result)

Submission status: Wrong Answer

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

for i in range(n-1):
    dist[i]=a[i+1]-a[i]
dist[n-1]=k-a[n-1]+a[0]

right=a[n-1]-a[0]
left=k-dist[0]

ret=min(left,right)

for i in range(1,n):
    left=k-dist[i]
    right=k-dist[i-1]
    ret=min(left,right)

print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import itertools

from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

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

a_np = np.array(a)
a_np = a_np.flatten()
a_np = np.hstack((a_np, k))
a_sa = abs(a_np[:-1] - a_np[1:])
print(a_sa.sum() - a_sa.max())

Submission status: Wrong Answer

Solution code:
import numpy as np

K, N = map(int, input().split())
A = [int(x) for x in input().split()]
A.append(K)

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

Submission status: Wrong Answer

Solution code:
kn = input().split()
a = input().split()

al = [int(i) for i in a]
k = int(kn[0])
n = kn[1]

ml = 20 - abs(al[len(al)-1] - al[0])
print(ml)

for i in range(len(al)):
	if i == 0:
		ml = max(ml, abs(al[0]-al[1]))
		# print(abs(al[0])-al[len(al)-1])
	elif i == len(al)-1:
		1 == 1
		# ml = max (ml, abs(al[i]-al[i-1])) 
	else:

		ml = max(ml,(al[i-1]-al[i]),abs(al[i]-(al[i+1])))

		print(abs(al[len(al)-1]-(al[i+1])))


print(k-ml)

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:
a,b = map(int,input().split())
l = list(map(int,input().split()))
if l[0] != 0:
  print(l[-1] - l[0])
else:
  print(min(a - l[-1] + l[-2],l[-1]))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
max_diff = 0
for idx in range(len(a)-1):
  diff = a[idx+1] - a[idx]
  if max_diff < diff:
    max_diff = diff
max_diff = max_diff + a[0]
ans = k - max_diff
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

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

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

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

Submission status: Wrong Answer

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


def solve(K: int, N: int, A: "List[int]"):
    max=A[-1]-A[-2]
    o=[]
    count=0
    if N==2:
        print(A[1]-A[0])
        exit()
    for i in reversed(range(N)):
        if count==0:
            count=1
            continue
        if A[i]-A[i-1]>max:
            max=A[i]-A[i-1]
        o.append(abs(A[i]-A[i-1]))
    print(abs(sum(o)-max))
    #print(max)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

#res = 0
if 0 in A:
    A.remove(0)

print(abs(max(A) - min(A)))

Submission status: Wrong Answer

Solution code:
def travel(ar,K):
    m = K//2
    s = 0
    for a in ar:
        s+= a%m
    return s

def io():
    K,N = map(int,input().split())
    ar = list(map(int,input().split()))
    print(travel(ar,K))

if __name__=='__main__':
    io()

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A_list = list(map(int, input().split()))
#DP = [10**6 for i in range(K)]
max1 = -1
ans = 0
for i in range(N-1):
    dis = abs(A_list[i]-A_list[i+1])
    if dis > max1:
        max1 = dis
        max_i = i
if A_list[0]!=0:
    dis = abs(A_list[0]-A_list[-1])
else:
    dis = abs(K-A_list[-1])
if dis > max1:
      max_i = -1
      max1 = dis
print(K-max1)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
lst = list(map(int, input().split()))
lst.sort()
dlst = list()

ans = 0
for i in range(n):
    if i == 0:
        continue
    dlst.append(lst[i] - lst[i - 1])

if lst[0] == 0:
    dlst.append(k - lst[n - 1])
else:
    dlst.append(lst[n - 1] - lst[0])

dlst.sort(reverse=True)
dlst.pop(0)

print(sum(dlst))

Submission status: Wrong Answer

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

if a[0]==0 and a[n-1]<kk:
    print(a[n-1]-a[1])
elif a[0]==0 and a[1]>kk:
    print(k-a[1])
else:
    print(a[n-1]-a[0])

Submission status: Wrong Answer

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

max = max(A)
min = min(A)

print(max - min)

Submission status: Wrong Answer

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

i=0
while i < n-1:
  if (a[i+1]-a[i]) >= k/2:
    print(str(k-a[i+1]+a[i]))
    break
  i += 1
else:
  print(str(a[n-1]-a[0]))

Submission status: Wrong Answer

Solution code:
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)

#入力
k,n = map(int,input().split())
a = list(map(int,input().split()))
d = k//2
aright = []
aleft = []
for i in range(len(a)):
    if a[i]>d:
        a[i] -= d
diff = []
for i in range(len(a)-1):
    d2 = a[i+1]-a[i]
    diff.append(abs(d2))
print(sum(a))
#解答
#出力

Submission status: Wrong Answer

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

if 0 in a and n >= 4 :
  a.sort()
  print((k - max(a)) + a[1])
else :
  print(max(a) - min(a))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
k, n = map(float, input().split())
m = list(map(float, input().split()))
first = m[0] if not m[0] == 0 else m[1]
last = m[-1]
half_k = float(k/2)

if last < half_k or first > half_k:
    print(int(last - first))
else:
    left_to_right = int(k + first - last)
    right_to_left = int(last - first)
    if left_to_right > right_to_left:
        print(right_to_left)
    else:
        print(left_to_right)

Submission status: Wrong Answer

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

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

c = sum(b)
print(min(c-max(b), c))

Submission status: Wrong Answer

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

a= list(map(int, input().split()))
l=[]

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

maxi = 0

for i in range(1, n):
    x = a[i] - a[i-1]

    maxi = max(maxi, x)

x = a[-1] - a[0]

if k - x < x:
    x = k - x

maxi = max(maxi, x)

print(maxi)

Submission status: Wrong Answer

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

diff = 0
largest_diff = 0
sumNum = 0 

for x in range(n-1):
	diff = a[x+1]-a[x]
	sumNum += diff
	if largest_diff < diff:
		 largest_diff = diff

last_diff = k-a[n-1]+a[0]
sumNum += last_diff
if largest_diff < last_diff:
	largest_diff = last_diff

print(sumNum)
print(largest_diff)

print(sumNum-largest_diff)

Submission status: Wrong Answer

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

ans1 = max(A)-min(A)

def adj(x):
  if x >= K/2:
    return x-K
  else:
    return x
  
A = list(map(adj,A))

ans2 = max(A)-min(A)

print(min(ans1,ans2))

Submission status: Wrong Answer

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

C = sorted(C)
for i in range(N-1):
    ans += C[i]
print(ans)

Submission status: Wrong Answer

Solution code:


############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
k,n = invr()
nums = inlt()
ans = min(nums[-1] - nums[0],k - (nums[1]))
for i in range(1,n - 1):
    
    result = min(k - (nums[i] - nums[i - 1]), \
             k - abs(nums[i + 1] - nums[i]))
    #print(i,result)
    if result < ans:
        ans = result

ans = min(ans,k - (nums[-1] - nums[-2]))
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = [int(x) for x in input().split()]
a_ = [x - a[0] for x in a] + [k]
d = [y - x for x, y in zip(a, a[1:])]
print(k - max(d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# 端っこ同士の距離、短い方が答え
ans = K
tmp = max(num_list)-min(num_list)
ans = tmp if tmp < ans else ans

print(ans)

Submission status: Wrong Answer

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

s = list(map(int,input().split()))
longest = []
for i in range(n-1):
    long = s[i+1] - s[i]
    longest.append(long)
if max(longest) > k - s[n-1] + s[0]:
    print(k - max(longest))
else:
    print(k - s[n-1] + s[0])

Submission status: Wrong Answer

Solution code:
t,n=map(int,input().split())
x=list(map(int,input().split()))
x.insert(0,x[-1]-t)
dis=[x[i+1]-x[i] for i in range(n)]
dis1=dis[:n-1]
dis2=dis[1:]
print(min(sum(dis1),sum(dis2)))

Submission status: Wrong Answer

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

for i in range(k-1):
  ans += min(arr[i],arr[i+1])
print(ans)

Submission status: Wrong Answer

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

A.append(K)
A.reverse()
A.append(0)

mini = 0
sum_len = 0


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

    sum_len += A[i] - A[i+1]

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

print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int,input().split()))
dis = 0
if min(a) == 0:
    a.remove(0)

shortest = min(a)
longest = max (a)

if longest - shortest >= k/2:
    dis = k-(longest-shortest)
else:
    dis = longest-shortest

print(dis)

Submission status: Wrong Answer

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

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

d.append(k-a[-1]+a[0])

print(min(sum(d[:-1]),sum(d[1:])))

Submission status: Wrong Answer

Solution code:
def rMap():
	return map(int,input().split())
def rList():
	return list(map(int,input().split()))
k,n = rMap()
a = sorted(rList())
z = 1000000
for i in range(n):
	if a[i]>k//2 and i!=0:
		z = a[i-1]+k-a[i]
print(min(z, a[-1]-a[0]))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
s=list(map(int,input().split()))
for i in s:
    if i==0:
        b-=1
sum_s=sum(s)
print(int(sum_s/b))

Submission status: Wrong Answer

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


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

B[N-1] = K - (A[N-1] - A[0])
B_max = max(B)
print(B)
print(K - B_max)