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:
#!/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:
        b=A[0] if A[0]<A[1] else A[1]
        print(abs(A[0]-A[1]))
        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(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())
li = list(map(int, input().split()))
res = 0
if k - li[-1] + li[0] > li[1] - li[0]:
    res += li[1] - li[0]
    end = n - 1
    start = 1
else:
    res += k - li[-1] + li[0]
    end = n - 2
    start = 0

for i in range(start, end):
    res += li[i+1] - li[i]
print(res)

Submission status: Wrong Answer

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

    c = a - (A[1] - A[0]) + ((K + A[0]) - A[-1])
    if ans > c:
        ans = c
    d = a - (A[-1] - A[-2]) + ((K + A[0]) - A[-1])
    if ans > d:
        ans = d
    for i in range(N-3):
        e = c - (A[i + 2] - A[i + 1]) + (A[i + 1] - A[i])
        if ans > e:
            ans = e
    print(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)'''

first=l[1]-l[0]
last=n-l[k-1]
s=first
for i in range(1,k):
	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()))


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: 
    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())
A=sorted(list(map(int,input().split())))

max_a = max(A)
min_a = min(A)
diff_min = max_a - min_a

if diff_min <= K/2:
    print(diff_min)
else:
    for i in range(1,N-1):
        tmp = K - (A[i+1] - A[i])
        if tmp < diff_min:
            diff_min = tmp
        tmp = K - (A[i] - A[i-1])
        if tmp < diff_min:
            diff_min = tmp

print(diff_min)

Submission status: Wrong Answer

Solution code:
s_field = list(map(int, input().split()))
s_house = list(map(int, input().split()))
sum = 0

s_house.append(s_field[0])

s_list = []
for n in range(0, s_field[1]):
  s_list.append(s_house[n+1] - s_house[n])
s_list.append(s_house[s_field[1]]-s_house[0])

s_list = sorted(s_list)

for n in range(0, s_field[1]-1):
  sum+=s_list[n]

print(sum)

Submission status: Wrong Answer

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

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

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

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
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:
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
dist=[0]*(n)

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):
    right+=dist[(i+1)%n]-dist[i]
    left+=dist[i-1]-dist[i]
    ret=min(left,right)

print(ret)

Submission status: Wrong Answer

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

if a[0] == 0:
    start = 20 - a[1]
    goal = 20 - a[n-1]
    
else:
    start = 20 - a[0]
    goal = 20 - a[n-1]

res = start - goal
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A.append(K + A[0])
print(A)
A_sub = []

for i in range(len(A)-1):
    A_sub.append(abs(A[i] - A[i + 1]))
# print(A_sub)
print(sum(A_sub) - max(A_sub))

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

print(ansdiv)

Submission status: Wrong Answer

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

  space_max = 0

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

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


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

for i in range(n):
    if i==1:
        x=a[i]-a[i-1]
    elif a[i]-a[i-1]>x:
        x=a[i]-a[i-1]

if a[n-1]-a[0]>x and a[0]!=0:
    x=a[n-1]-a[0]

print(k-x)

Submission status: Wrong Answer

Solution code:
i = list(map(int,input().split()))
house = list(map(int,input().split()))
house.append(i[0])
ans = []
for h in range(len(house)-1):
  num = house[h+1]-house[h]
  ans.append(num)
ans.append(ans[-1]+house[0])
ans.remove(max(ans))
ans.remove(max(ans))
print(sum(ans))

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)
else:
  print (y)

Submission status: Wrong Answer

Solution code:
K, N = input().split()
K = int(K)
N = int(N)
houses = list(map(int, input().split()))
maxDistance = 0
distance = 0
for i in range(N):
  firstHouse = houses[i]
  lastHouse = houses[i - 1]
  if lastHouse > firstHouse:
    distance = lastHouse - firstHouse
  else:
    distance = (K - firstHouse) + lastHouse
  if distance > maxDistance:
    maxDistance = distance
print(distance)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
ans=0
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]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
*a, = map(int, input().split())
a += [i+k for i in a]
print(min(j-i for i, j in zip(a, a[2:])))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
A_str = input().split()
A = [int(h) for h in A_str]
print(A)
D = []
for i in range(1,N):
  D.append(A[i]-A[i-1])
D.append(A[0]+(K-A[N-1]))
print(D)
maximum = max(D)
print(K-maximum)

Submission status: Wrong Answer

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

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

mixi = 0
sum_len = 0
space = 100000000000000000000000000000000000000000000

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

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

print(result)

Submission status: Wrong Answer

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

def res_kyori(pos1,pos2):
    if pos1 <= pos2:
        return pos2-pos1
    else:
        return pos1-pos2


high=0
mpos=[]
for i in range(len(A)-1):
    if high < res_kyori(A[i],A[i+1]):
        high=res_kyori(A[i],A[i+1])
        mpos=[i,i+1]

r1,r2=0,0
for i in range(len(A)-1):
    r1 += res_kyori(A[(mpos[1]+i)%N],A[(mpos[1]+(i+1))%N])
    r2 += res_kyori(A[abs(mpos[0]-i)%N],A[abs(mpos[0]-(i+1))%N])

print(r1 if r1<=r2 else r2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

'''for i in range(k-1):
  if arr[i]!=0 and arr[i+1]!=0:
    ans+= arr[i+1]-arr[i]
print(ans)
'''
a = arr.count(0)
print(sum(arr)//(k-a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

k,n =list(map(int,input().split()))
a = list(map(int,input().split()))
ans = [0]*n 
for i in range(n):
    if a[i]>k/2:
        a[i] = abs(k-a[i])

a.sort(reverse=True)
kai = a[0]+a[1]
print(kai)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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:
            w = b + ensyu - 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):

    return 

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

mid = k//2;
for i in range(n) :
    if (l[i] > mid) :
        l[i] = l[i] - k
    else :
        continue 

maxi = max(l)
mini = min(l)

if(mini < 0) :
    for i in range(n) :
        if l[i] == mid :
            l[i] = 0; 

maxi = max(l)
mini = min(l)


#print(l)
#print(maxi,mini)
print(maxi - mini)

Submission status: Wrong Answer

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

if A[0] != 0:
  for i in range(1,N):
    sum = sum + (A[i] - A[i-1])
else:
  for i in range(2,N):
      sum = sum + (A[i] - A[i-1])

print(sum)

Submission status: Wrong Answer

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

A.sort()

reverse = None

for i in range(len(A)):
    j = i+1
    if j > N-1:
        j -= N
    if (K-A[i])- A[0] < A[j] - A[i]:
        reverse = i
if reverse is not None:
    A = [A[i] if i < reverse else A[i] - K  for i in range(len(A))]
A.sort()
print(max(A) - min(A))

Submission status: Wrong Answer

Solution code:
k,m = map(int,input().split())
a = input().split()
a =[int(s) for s in a]
a.sort(reverse=True)
a.insert(0,k)

dis = []

for i in range(m):
  part = int(a[i]) - int(a[i+1])
  dis.append(part)
  del part

dis.append(int(a[m-1]))
dis.sort()
print(dis)

print(sum(dis[:m-1]))

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)-1):
	ml = max(ml,abs(al[i]-(al[i+1])))


print(k-ml)

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

if np.max(distance) > start_end_distance:
    print(abs(K - np.max(distance)))
else:
    print(abs(K - start_end_distance))

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=(a[1]-a[0])+(a[0]+syu-a[n-1])
print(min(clock,han_clock))

Submission status: Wrong Answer

Solution code:
import numpy as np
 
tmp =list(map(int, input().split()))
num=list(map(int, input().split()))

num.append(tmp[0])

print(np.diff(num).sum()-np.diff(num).max())

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):
    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
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
      else:
        ans='small'
  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:
import sys
input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# 上側通る場合
upper_list = [i if i <= K/2 else i - K for i in l]
# 下側を通る場合
lower_list = [0 if i == K/2 else i - K/2 for i in l]

upper_length = abs(max(upper_list) - min(upper_list))
lower_length = abs(max(lower_list) - min(lower_list))

print(int(min(upper_length,lower_length)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
(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=abs(a[-1]-a[0]-d)
l.append(c)
l=sorted(l)
l1=sum(l)
print(l1-l[-1])

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):
    kouho = home - A[i - 1]
    if kouho < 0:
        answer.append(20 + kouho)
    else:
        answer.append(kouho)

print(20 - max(answer))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
root1 = A[N-1] - A[0]
for i in range(N):
    if A[i] > K/2:
        A[i] -= K
root2 = max(A) - min(A)
print(root1 if root1 < root2 else root2)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
listA = list(map(int,input().split()))
listA.sort()
num = len(listA) -2
dis = max(listA) - min(listA)
## 一周　##
if dis > K/2:
  out1 = K - max(listA) + listA[num]
  out2 = dis
  if out1 >= out2:
    out = out2
  else:
    out = out1
## 周回無し##
if dis <= K/2:
  out = dis

print(out)

Submission status: Wrong Answer

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

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

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=(a[1]-a[0])+(syu-a[n-1])
print(min(clock,han_clock))

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

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]
  print(r)
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())
#print(k)
#print(n)
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

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

#print(l_si_i)

minIndex = l_si_i.index(min(l_si_i))

#print(minIndex)

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

#print(l_si_i)

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

print(sub)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

print(np.diff(num).sum()-np.diff(num).max())

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 / 2
    d = max(d, p)

print(k - d)

Submission status: Wrong Answer

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

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):
  if arr[i]!=0 and arr[i+1]!=0:
    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()))
lst = []
for i in range(1, N-1):
    diff_1 = abs(A[i + 1] - A[i])
    lst.append(diff_1)
    diff_2 = abs(A[i] - A[i - 1])
    lst.append(diff_2)

lst.append(K + A[0] - A[N-1])
print(lst)
print(K - max(lst))

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
    if ele == 0:
        flag = True

max = max(list)
ans -= max
# else:
#     secondMax = sorted(list)[-2]
#     ans -= max+secondMax


print(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-1):
  if alist[i]-alist[i-1]>=k/2:
    answer = k-alist[i]+alist[i-1]
    print(answer)
    break

answer = alist[n-1]-alist[0]
print(answer)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

K2 = K / 2

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

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

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 + dist)
#print("dist_np",dist_np)

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()))
a.sort()
l = []

if a[0] == 0:
    l.append(abs(k - a[n - 1]))
else:
    l.append(abs(a[0] - a[n - 1]))
for i in range(1, n):
    dis = abs(a[i] - a[i - 1])
    l.append(dis)
 
print(sum(l) - max(l))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

#print(l_si_i)

minIndex = l_si_i.index(min(l_si_i))

#print(minIndex)

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

#print(l_si_i)

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

print(sub)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

a.append(k)

big = 0
big_count = 0

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

if big == 0:
    ans = a[n - 1] - a[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
    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))
    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:
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:
            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):

    return 

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
around = a[0]
number = a[1]
dis = []
i = 0
for i in range(number):
  if i == number-1:
    dis.append(around-b[i])
  else:
    dis.append(b[i+1]-b[i])
  i=i+1
dis_m = max(dis)
dis_sum=sum(dis)-dis_m
print(dis_sum)

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):
    kouho = home - A[i - 1]
    if kouho < 0:
        answer.append(20 + kouho)
    answer.append(kouho)

print(20 - max(answer))

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]

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

dis.append(last_dis)

print(K - max(dis))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
n=2*10^6
for i in range(N):
  m = abs(A[i-1] - A[i])
  if n > m:
    n = m
print(m)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
s = A[0]
#for i in range(n):
  #A[i] -= s
ans = 0
idx = n
for i in range(n):
  if A[i] > k-A[i]:
    ans += k-A[i]
    print(k-A[i], A[i])
    idx = i
    break
ans += (A[idx-1]-s)
print(ans)

Submission status: Wrong Answer

Solution code:
K, N, *A = map(int, open(0).read().split())
B = []
for i in range(N - 1):
    B.append(A[i + 1] - A[i])
B.append(K - A[-1] + A[0])
print(B)
print(K - max(B))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

length = K//2
for i in range(N-1):
    length = max(A[i+1] - A[i] , length)
    if length == A[i+1] - A[i]:
        ans = A[i] + K - A[i+1]
      
if length <= K//2:
    ans = A[-1] - A[0]
print(ans)

Submission status: Wrong Answer

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

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

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