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

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 = map(int, input().split())
A = list(map(int, input().split()))
num = (N-2)-N
l = []
for i in range(1,len(A)):
    if A[num] == 0:
        l.append(0)
    else:
        ans = A[-i] - A[num]
        num -= 1
        l.append(ans)
res = K - sum(l)
print(res)

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

ans=max(A)
for i in range(n):
  A[i]=k-A[i]
ans-=min(A)
print(ans)

Submission status: Wrong Answer

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

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

int(K)
int(N)

#Bは各距離とする
B = []
for i in range(0, len(A)-1):
    B.insert(i,A[i+1]-A[i])
B.insert(int(N)-1,int(K)-A[int(N)-1]+A[0])

ans = 0

print(B)

for j in range(int(N)):
    ans += B[j]

ans -= max(B)

print(ans)

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[-1] + A[i])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
a_s = a[1:]-a[:-1]

#for i in range(n):
#  neg = a_s[:i]
#  pog = a_s[i:]
#  print(neg, pog)

print(sum(a_s))

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])
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()))
result = 0
mx = -float("inf")
for i in range(N-1):
    mx = max(mx, A[i+1] - A[i])
mx = max(mx, K - A[-1])

result = K - mx
print(result)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
s = A[0]
for a in A[1:]:
    if abs(s - a) > abs(K - a):
        ans += abs(K - a)
    else:
        ans += abs(s - a)
print(ans)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
a = [int(i) for i in input().split()]
dist1 = max(a)-min(a)
a1 = []
for i in a:
    if i <= K//2:
        a1.append(-i)
    else:
        a1.append(K-i)
dist2 = max(a1)-min(a1)
a2 = []
for i in a:
    if i < K//2:
        a2.append(-i)
    else:
        a2.append(K-i)
dist3 = max(a2)-min(a2)
print(min(dist1,dist2,dist3))

Submission status: Wrong Answer

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

for x in a:
    if x > k // 2:
        dis.append(x - k)
    else:
        dis.append(x)

dis.sort()
d = dis[-1]
for x in dis[-2::-1]:
    d -= x
print(d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[]
for i in(A):
  if(i!=0):
    B.append(i)
B.sort()

if((B[-1]-B[0])<(B[0]+K-B[-1])):
   print(B[-1]-B[0])
else:
   print(B[0]+K-B[-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A.sort()
ans = A[-1] - A[0]
for i in range(N):
    if A[i] > K // 2:
        A[i] = A[i] - K
A.sort()
ans = min(ans, abs(A[-1] - A[0]))
    
print(ans)

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):
	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()))
A.sort()
length = A[len(A)-1] - A[0]
if length > K / 2:
    print(K - A[len(A) - 1]  + A[1])
else:
    print(A[len(A)-1] - A[0])

Submission status: Wrong Answer

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

MAX = 0
for i in range(len(A)-1):
    A[i]=A[i+1]-A[i]
    MAX = max(MAX,A[i+1]-A[i])
A[len(A)-1]=A[len(A)-1]-A[0]
print(A)
MAX = max(MAX,A[len(A)-1])
print(K-MAX)

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[1],l[-1]))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort()
    
    rou=a[-1]-a[0]
    half=0
    end=0
    for i in range(n):
        if a[i]-k//2>0:
            a[i]=abs(a[i]-k)
            end=i
            break
        else:
            half=i
    print(min(rou,a[end]+a[half]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
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 = list(map(int,input().split()))
A = list(map(int,input().split()))
d = [A[i+1]-A[i] for i in range(N-1)]
de = K - A[N-1] + A[0]
dis = 0
for i in range(N-2):
  dis += d[i]
if d[N-2] <= de:
  dis += d[N-2]
else:
  dis += de
print(dis)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.append(K)
ans=0
num=A[0]
B=[]
counter=0
for i in A:
    if counter==0:
        counter+=1
        continue
    B.append(i-num)
    num=i


    counter+=1
B.sort()
B.pop()
print(sum(B))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

k,n = map(int,input().split())
num_list = list(map(int, input().split()))
min_num = num_list[0]+num_list[-1]

for i in range(0,int(len(num_list)/2)):
    res = num_list[-1]-k
    #print(num_list)
    num_list.pop(-1)
    #print(num_list)
    num_list.insert(0, res)
    #print(num_list)
    sum_num = abs(num_list[0])+abs(num_list[-1])
    #print("sum_num")
    #print(sum_num)
    
    if sum_num < min_num:
        min_num = sum_num

print(min_num)

Submission status: Wrong Answer

Solution code:
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    f_num = A[0]
    l_num = A[-1]
    ans = 10 ** 5
    a = 0
    b = 0
    for i in range(N-1):
        a += A[i + 1] - A[i]
        b += A[-(i + 1)] - A[-(i + 2)]
    if ans > a:
        ans = a
    if ans > b:
        ans = b
    c = a - (A[1] - f_num) + ((K + f_num) - l_num)
    d = b - (l_num - A[-2]) + ((K - l_num) + f_num)
    if ans > c:
        ans = c
    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
        f = d - (A[i + 2] - A[i + 1]) + (A[i + 1] - A[i])
        if ans > f:
            ans = f
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in range(n):
  li.append(l[n-1]-l[n-2])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
re = 0
for i in A[::-1]:
    re = i - re
print(re)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
a = [int(x) for x in input().split()]
a.append(K)
dist = [0]*(N+1)

dist[0] = a[N-1] - K - a[0]
for i in range(len(a)-1):
    dist[i+1] = a[i+1] - a[i]

dist.pop()
dist.remove(max(dist))
ans = abs(min(dist)) + abs(max(dist))
print(ans)

Submission status: Wrong Answer

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

a = A[1] - A[0]

for i in range(1,N):
    b = A[i] - A[i-1]
    c = max(a,b)

d = K - A[N-1] + A[0]

e = max(c,d)

print(min(e,K-e))

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
Pi = lambda x: stdout.write(str(x) + '\n')
S = lambda x: x*(x+1) // 2
I = lambda x: 1+(2*x)
R = lambda:stdin.readline()
Ri = lambda x:map(int,x.split())
Rs = lambda x:map(str,x.split())
Rf = lambda x:map(float,x.split())
 
def main():	
	# t = int(R())
	for x in stdin:
		k,n = Ri(x)
		A = list(Ri(R()))
		ans = 10000000
		for i in range(n-1):
			ans = min(ans, A[i] + k - A[i+1]-A[0])
			# Pi(ans)
		ans = min(ans,A[-1]-A[0])
		Pi(ans)

if __name__=='__main__':
	main()

Submission status: Wrong Answer

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

kyori = 0
for i in range(a[1] - 1):
  if b[i] > 0:
    kyori += b[i+1] - b[i]

print(kyori)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.append(n)
d=[]
for i in range(len(a)-1):
    d.append([i,a[i+1]-a[i]])
d[-1][1]+=a[0]
l=sorted(d,key=lambda x:x[1])

o=l[-1][0]
if o==len(a)-1:
    g=a[-2]-a[0]
else:
    g=a[o]+n-a[o+1]
print(g)

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
    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())	
le = list(map(int,input().split()))	
for i in range(N):
    if i==N-1:
        le[N-1]=K-le[N-1]
    else: 
        le[i]=abs(le[i]-le[i+1])
print(sum(le)-max(le))

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] = (k/2)-a[i]
ans2 = int(abs(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()))

x_plus = [K-A[-1]+A[0]] + [A[i+1] - A[i] for i in range(N-1)]
print(max(x_plus))

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


print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    import numpy as np
    a = np.array(a)

    kouho = a[0] + (k - a[-1])

    sa = a[1:] - a[0: -1]

    kouho1 = k - max(sa)

    print(max(kouho,kouho1))

Submission status: Wrong Answer

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

sa =  []
for i in range(1,n):
    sa_ = a[i]-a[i-1]
    sa.append(sa_)
#print(sa)
sa.append(k-max(a))
ans = sum(sa)-max(sa)

print(ans)

Submission status: Wrong Answer

Solution code:
p,n=map(int,input().split())
arr=list(map(int,input().split()))
mid=n//2
f=0
for i in range(mid):
    f+=abs(arr[i]-arr[i+1])
for j in range(mid+1,n-1):
    f+abs(arr[j]-arr[j+1])
f+=abs(p-arr[-1])
b=0
for i in range(mid,n-1):
    b+=abs(arr[i]-arr[i+1])
b+=abs(p-arr[-1])
for j in range(0,mid-1):
    b+=(arr[j]-arr[j+1])
print(min(f,b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

def solve():
    K, N = map(int, input().split())
    As = list(map(int, input().split()))
    last = N-1

    if As[last]-As[0] <= K-As[last]+As[last-1]:
        print(As[last]-As[0])
    else:
        print(K-As[last]+As[last-1])


# Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

a = A[1] - A[0]

for i in range(1,N):
    b = A[i] - A[i-1]
    c = max(a,b)

d = K - A[N-1] + A[0]

e = max(c,d)

print(K-e)

Submission status: Wrong Answer

Solution code:
_in = list(map(int, input().split()))
k = _in[0]
n = _in[1]
p = 0
_i = list(map(int, input().split()))
dis = 0
mdis = k - _i[n-1] + _i[0]
for i in range(1,n-2):
    dis = _i[i+1]- _i[i]
    if dis>mdis:
        mdis = dis
print(k-mdis)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

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

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

Submission status: Wrong Answer

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

ans = 0
if a[0] == 0 and len(a) == 3:
    ans += a[1]
    ans += k - a[-1]

elif a[0] == 0 and len(a) > 3:
    ans += a[1]
    ans += k - a[-1]
    for i in range(2,len(a)):
        ans += a[i] - a[i-1]
else:
    ans += max(a) - min(a)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
r = n//(3.14*2)
arr = list(map(int,input().split()))
print(int(sum(arr)//r))

Submission status: Wrong Answer

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

d = a - c_list[2]
e = c_list[2] - c_list[1]
f = c_list[1] - c_list[0]

answer = []
answer.append(d)
answer.append(e)
answer.append(f)
answer.sort()
print(answer[0] + answer[1])

Submission status: Wrong Answer

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

if a[n-1]-a[0] <= k-a[n-1]+a[0]:
    print(a[n-1]-a[0])
else:
    for i in range(n):
        ans=max(ans,a[i]-a[i-1])
    print(k-ans)

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
another = abs(a[0] - a[n-1])
if max_diff < another:
    max_diff = another
ans = k - max_diff
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

a_list.append(k)

ans_list = np.diff(a_list)

if max(ans_list) == ans_list[-1]:
    print(sum(ans_list) - max(ans_list) - a_list[0])
elif max(ans_list) == ans_list[0]:
    print(sum(ans_list) - max(ans_list) - ans_list[-1])
else:
    print(sum(ans_list) - max(ans_list))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

if k - a_list[-1] > a_list[0]:
    a_list.insert(0, a_list[-1] - k)
    a_list.pop(-1)
    a_list = np.array(a_list)
    print(sum(np.diff(a_list)))
else:
    a_list = np.array(a_list)
    print(sum(np.diff(a_list)))

Submission status: Wrong Answer

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

def distance(a1, a2):
  return abs(a1 - a2)

sum, max = 0, 0
for i in range(N):
  if (i + 1) != N:
    d = distance(An[i], An[i+1])
    sum += d
    if(d > max):
      max = d
  else:
    d = distance(An[i], K)
    sum += d
    if(d > max):
      max = d

print(sum - max)

Submission status: Wrong Answer

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

ans = sum(dif)-max(dif)
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
s0 = min(a[0], k - a[0])
s1 = min(a[1], k - a[1])
s2 = min(a[2], k - a[2])
ans = s0+s1+s2
print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
*A, = map(int, input().split())
A = A + [K]
chk = [0] * N
for i in range(N):
    chk[i] = A[i+1] - A[i]
chk.sort(reverse=True)
print(sum(chk[1:]))

Submission status: Wrong Answer

Solution code:
#スペース区切りの整数の入力
K, N = map(int, input().split())
#文字列の入力
A = input()
#コンマで区切る
A_list = A.split(' ')
for i in range(len(A_list)):
    A_list[i] = int(A_list[i])

amari = K-int(A_list[N-1])

aida_list = [0]*len(A_list)
for i in range(len(A_list)-1):
  aida_list[i] = A_list[i+1] - A_list[i]
aida_list[len(A_list)-1] = amari

max_ = max(aida_list)

#print(aida_list)
aida_list.remove(max_)

ans = sum(aida_list)

#print(A_list)
#print(aida_list)
print(ans)

Submission status: Wrong Answer

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

distance = 0
for i in range(1,N-1):
    distance += An[i]

print(An[N-1] - distance + An[0])

Submission status: Wrong Answer

Solution code:
import numpy as np

K, N = [int(i) for i in input().split()]
A = np.array(list(map(int, input().split())))
B = np.append(A, K)
diff = np.diff(B)
print(diff.sum() - diff.max())

Submission status: Wrong Answer

Solution code:
import numpy as np

KN=np.array(input().split(" "),dtype=np.int64)
A=np.array(input().split(" "),dtype=np.int64)

K=KN[0]
N=KN[1]
dst=0

A[-1]=K-A[-1]
A=np.sort(A)

for i in range(1,N):
    calc=A[i]-A[i-1]
    if(calc==0):
        dst+=A[i]
    else:
        dst+=A[i]-A[i-1]

print(dst)

Submission status: Wrong Answer

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

ans=0
if max(a)<=(k/2.0) or min(a)>=(k/2.0):
  for i in range(n-1):
    ans+=abs(a[i+1]-a[i])
else:
  for i in range(n):
    if a[i]>k/2.0:
      a[i]-=k
    else:
      j=i
  for i in range(j):
     ans+=abs(a[i+1]-a[i])
  ans+=abs(a[n-1]-a[0])
  for i in range(j+1,n-1):
    ans+=abs(a[i+1]-a[i])
print(ans)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
l = []
l.append(abs(max(k, 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())
A = list(map(int,input().split()))
A.sort()
length = A[len(A)-1] - A[0]
if length > K / 2:
    print(A[len(A) - 1] - A[1])
else:
    print(A[len(A)-1] - A[0])

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
arr=list(map(int,input().split()))
arr2=[i for i in arr if i!=0]
print(k-(max(arr2)-min(arr2)))

Submission status: Wrong Answer

Solution code:

import numpy as np

N,M=(int(x) for x in input().split())
A=list(map(int, input().split()))
A=np.array(A)
B=A[1:]-A[:-1]
b=max(B)
c=A[-1]-A[0]
if c > N/2:
    c=int(c-N/2)
else:
    c=c
d=max(b,c)
e=N-d
if e > N/2:
    print(N-e)
else:
    print(e)

Submission status: Wrong Answer

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

ans1 = 0
for i in range(n - 1):
    ans1 += li[(i + 1)] - li[i]
#print(ans1)

chuten = k / 2
for i,j in enumerate(li):
  if j > chuten:
    li[i] = j - k
li.sort()
#print(li)
ans2 = 0
for i in range(n - 1):
    ans2 += li[(i + 1)] - li[i]
#print(ans2)

print(min(ans1, ans2))

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//2 - l3[0]),l[-1] - l[0]))

Submission status: Wrong Answer

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

l.append(20+l[0]);
maxdiff=0
for i in range(n):
	if(l[i+1]-l[i]>maxdiff):
		maxdiff=l[i+1]-l[i]
print(k-maxdiff)

Submission status: Wrong Answer

Solution code:
import sys
import math
def i():return int(sys.stdin.readline().replace("\n",""))
def i2():return map(int,sys.stdin.readline().replace("\n","").split())
def s():return str(sys.stdin.readline().replace("\n",""))
def l():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))
def t():return tuple(map(int,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":pass
k,n = i2()
a = intl()
b = [0]*n
s = 0
for i in range(1,n):
    s += a[i]-a[i-1]
for i in range(n):
    b[i] = min(k-a[i],a[i])
print(min(sum(b),s))

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split(" "))
A = [int(i) for i in input().split(" ")]
a1 = [i for i in A if i<=K/2]
a2 = [j for j in A if j>K/2]

Max = 0
if len(a1)==0:
    print(a2[-1]-a2[0])
elif len(a2)==0:
    print(a1[-1]-a1[0])
else:
    for i in range(len(a1)-1):
        if a1[i+1] - a1[i] > Max:
            Max = a1[i+1] - a1[i]
            
    for j in range(len(a2)-1):
        if a2[j+1] - a2[j]>Max:
            Max = a2[j+1] - a2[j]
            
    if a1[0]+K-a2[-1]>Max:
        Max = a1[0]+K-a2[-1]
        
    if a2[0]+a1[-1]>Max:
        Max = a2[0]-a1[-1]
        
    print(K-Max)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

maxdis = 0
for i in range(len(A)-1):
  if maxdis < A[i+1] - A[i]:
    maxdis = A[i+1] - A[i]
if maxdis < A[-1] - A[0]:
  maxdis = A[-1] - A[0]
print(K-maxdis)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	K,N = inputMap()
	A = inputList()

	toto = 0
	tmp = 0
	for j,vol in enumerate(A):
		if j == 0:
			tmp = vol
			continue
		toto += vol - tmp
		tmp = vol

	ans = toto
	toto = toto + (K - A[-1])

	A.append(K - A[-1])

	for j,vol in enumerate(A):
		if j == 0:
			continue

		tmpans = toto - (vol - A[j-1])

		if ans > tmpans:
			ans = tmpans

	print(ans)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import math

(k, n) = [int(s) for s in input().split()]

d = [int(s) for s in input().split()]
d.append(k)
e = [d[i+1] - d[i] for i in range(0, len(d) - 1)]

idx = e.index(max(e))

e = e[0:idx] + e[idx+1:]

ans = sum(e)

print(ans)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
dis_lst=list(map(int,input().split()))
sa_lst=[]
for i in range(N):
    if i==N-1 and 0 in dis_lst:
      sa_lst.append(abs(K-dis_lst[i]))
      break
    elif i==N-1:
      sa_lst.append(abs(dis_lst[i]-dis_lst[0]))
      #print(sa_lst)
      break
    sa_lst.append(abs(dis_lst[i]-dis_lst[i+1]))
    #print(sa_lst)
    #print('pass')
sa_lst.remove(max(sa_lst))
print(sum(sa_lst))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

ans = 0
is_zeroPoint = False
for ele in list:
    ans += ele
    if ele == 0:
        is_zeroPoint = True


max = max(list)
if is_zeroPoint == True:
    ans -= max
else:
    secondMax = sorted(list)[-2]
    ans -= max+secondMax

print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
sum_min = K * N
for i in A:
    sumn = 0
    for j in A:
        abso = abs(i-j)
        mawaru = 100-i+j
        n = min(abso, mawaru)
        sumn += n
        
        if sumn > sum_min:
            break
    if sumn < sum_min:
        sum_min = sumn
        
print(sum_min)

Submission status: Wrong Answer

Solution code:
K, N = [int(x) for x in input().split(" ")]
a = [int(x) for x in input().split(" ")]
a.sort()
a.insert(0,a[-1])
max_d = 0
for i in range(0,N):
  if a[i] >= int(K/2):
    a[i] = a[i]-int(K/2)
  if abs(a[i+1] - a[i]) >= max_d:
    max_d = abs(a[i+1] - a[i])

print(K-max_d)