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: Runtime Error

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

all_length = k*2
point = []
for i in range(n):
    a_left  = k-a[i]
    a_right = k+a[i]
    point.append(a_left)
    point.append(a_right)
point = list(set(point))
point.sort()

num = len(point)
num = round(num)
distance = []
for i in range(num):
    dis = 0
    for j in range(n-1):
        dis += point[i+j+1] - point[i+j]
    distance.append(dis)
mindis = min(distance)
print(mindis)

Submission status: Runtime Error

Solution code:
a, b, c, d, e = input().split()
lists = [c, d, e]
intList = [int(x) for x in lists]
print(max(intList) - min(intList))

Submission status: Runtime Error

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


def loopf(i, k):

    if i == len(A) - 1:

        if K - A[i] + A[0] >= k:
            k = K - A[i] + A[0]

        return K - k

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

    return loopf(i + 1, k)


print(loopf(0, 0))

Submission status: Runtime Error

Solution code:
k, n = map(float, input().split())
m = list(map(float, input().split()))
min = float(0)
half_k = k / 2
for i in range(n):
    first = m[i]
    last = m[i-1]
    len = last - first
    if len < 0:
        len = len + k
    if min < len:
        min = len
print(int(min))

Submission status: Runtime Error

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


def loopf(i, k):

    if i == len(A) - 1:
        print(A[i])

        if K - A[i] + A[0] > k:
            k = K - A[i] + A[0]

        return K - k

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

    return loopf(i + 1, k)


print(loopf(0, 0))

Submission status: Runtime Error

Solution code:
import numpy as np
k,n=map(int,input().split())
a=list(int,input().split())
a=np.array(a)
if max(abs(a[1:]-a[:-1])) < k-abs(a[0]-a[-1]):b=k-abs(a[0]-a[-1])
else:b= max(abs(a[1:]-a[:-1]))
print(k-b)

Submission status: Runtime Error

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

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

A_sorted=sorted(A)

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

Submission status: Runtime Error

Solution code:
K = int(input())
N = int(input())

A = list(range(N))

for i in range(N):
    A[i]=int(input())
    
max = 0

for i in range(N-1):
    if max < A[i+1]-A[i]:
        max = A[i+1]-A[i]

if max < K-A[N-1]+A[0]:
    max = K-A[N-1]+A[0]
    
print(K-max)

Submission status: Runtime Error

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

for i in range(N-1):
    temp=c[i+1]-c[i]
    if temp>length_max:
        length_max=temp
print(length_max)

Submission status: Runtime Error

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

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

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

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

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

print(str(res))

Submission status: Runtime Error

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

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

lans=0
ans = 999999999999

for i in range(len(b)):
    lans = 0
    for j in range(len(b)):
        if i != j:
            lans = b[j] + lans
    ans = min(ans,lans)
    
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K = int(input())
N = int(input())

A = list(range(N))

for i in range(N):
    A[i]=int(input())
    
max = A[0]

for i in range(N-1):
    if max < A[i+1]-A[i]:
        max = A[i+1]-A[i]

if max < K-A[N-1]+A[0]:
    max = K-A[N-1]+A[0]
    
print(K-max)

Submission status: Runtime Error

Solution code:

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

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

A_sorted=sorted(A)

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

Submission status: Runtime Error

Solution code:
a=input().split(' ')
b=input().split(' ')
c=0
for i in int(a[1]):
    if i != int(a[1])-1:
        if int(b[i+1]) - int(b[i]) > c:
            c = int(b[i+1]) - int(b[i])
    else:
        if int(a[0]) - int(b[-1])+ int(b[0]) > c:
            c = int(a[0]) - int(b[-1])+ int(b[0])
print(int(a[0])-c)

Submission status: Runtime Error

Solution code:
def ai_time(K, A, ai):
  r = ai_time_r(K, A, ai)
  l = ai_time_r(K, A, ai)
  return min([r, l])

def ai_time_r(K, A, ai):
  long_lst = [i for i in a if i < ai]
  if not long_lst:
    return max(A) - ai
  else:
    return max(long_lst) - ai + K

def ai_time_l(K, A, ai):
  long_lst = [i for i in a if i > ai]
  if not long_lst:
    return ai - min(A)
  else:
    return K - ai + min(long_lst)
  
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
res = min([ai_time(K, A, ai) for ai in A])
print(res)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
count = 0
for i in A:
  count += 1
  if count != N:
    B[i]=A[i+1]-A[i]
  else:
    B[N-1] = A[0] + K - A[N-1]

print(K-max(B))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a=input().split(' ')
b=input().split(' ')
c=0
for i in int(a[1]):
    if i != a[1]-1:
        if int(b[i+1]) - int(b[i]) > c:
            c = int(b[i+1]) - int(b[i])
    else:
        if int(a[0]) - int(b[-1])+ int(b[0]) > c:
            c = int(a[0]) - int(b[-1])+ int(b[0])
print(int(a[0])-c)

Submission status: Runtime Error

Solution code:
# C - Traveling Salesman around Lake
K,N = list(map(int, input().split()))
A = list(map(int, input().split()))

import numpy as np
D = np.zeros((N, N))

for i in range(N):
    j = (i+1)%N
    if j == 0:
        D[i,j]=K-abs(A[j]-A[i])
    else:
        D[i,j] = abs(A[j]-A[i])

idx = np.unravel_index(np.argmax(D), D.shape)
dmax = D[idx]
ans = int(K - dmax)
print(ans)

Submission status: Runtime Error

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:
        aanswer.append(kouho)

print(20 - max(answer))

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
x=[]
y=[]
for i in A:
  if i>10:
    a=20-i
    y.append(a)
  else:
    x.append(i)
if len(A)==1:
  if A[0]>=10:
    print(20-A[0])
  else:
    print(A[0])
else:
  ans=max(x)+max(y)
  print(ans)

Submission status: Runtime Error

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

a_list = list(A)

dist = 0
max = 0
sum = 0

for i in range(len(a_list)):
  if i == len(int(a_list)) - 1:
    dist = abs(int(a_list[i]) - int(a_list[0]))
  else:
    dist = abs(int(a_list[i]) - int(a_list[i + 1]))
  
  if max < dist:
    sum += max
    max = dist
  else:
    sum += dist
    
print(sum)

Submission status: Runtime Error

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
ans=10**9
for i in range(n):
  s=a[i]
  t=0
  for j in range(1,n):
    m=min(abs(s-a[i-j]),abs(s+20-a[i-j]),abs(s-a[i+j]),abs(20-s+a[i+j]))
    t=t+m
    s=a[i-j]
  ans=min(ans,t)
print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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: Runtime Error

Solution code:
k, n = map(int, input().split())
 
An = list(map(int, input().split()))
 
mn = 0
Amax = max(An)
Amin = min(An)
for i in range(len(An)):
    cal = 0
    A = An[i:]  + An[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
        
        if mn > 0 and cal > mn:
            break
    
    if mn ==0 or mn > cal:
        mn = cal
        
rAn = An[:]
rAn.reverse()
for i in range(len(rAn)):
    cal = 0
    A = rAn[i:]  + rAn[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
            
        if mn > 0 and cal > mn:
            break
 
    if mn ==0 or mn > cal:
        mn = cal
print(mn)
k, n = map(int, input().split())

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

mn = 0
Amax = max(An)
Amin = min(An)
for i in range(len(An)):
    cal = 0
    A = An[i:]  + An[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
        
        if mn > 0 and cal > mn:
            break
    
    if mn ==0 or mn > cal:
        mn = cal
        
rAn = An[:]
rAn.reverse()
for i in range(len(rAn)):
    cal = 0
    A = rAn[i:]  + rAn[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
            
        if mn > 0 and cal > mn:
            break

    if mn ==0 or mn > cal:
        mn = cal
print(mn)

Submission status: Runtime Error

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

print(K-max(B))

Submission status: Runtime Error

Solution code:
K = int(input())
N = int(input())

A = [0]

for i in range(N):
    A[i]=int(input())

for i in range(N-1):
    if max < A[i+1]-A[i]:
        max = A[i+1]-A[i]

if max < K-A[N-1]+A[0]:
    max = K-A[N-1]+A[0]
    
print(K-max)

Submission status: Runtime Error

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


def loopf(i, k):

    if i == N - 1:
        if K - A[i] + A[0] > k:
            k = K - A[-1] + A[0]

        return K - k

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

    return loopf(i + 1, k)


print(loopf(0, 0))

Submission status: Runtime Error

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):
    k , n = map(int , input().split())
    al = list(map(int , input().split()))

    return [[k,n],al]

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

print(K-max(B))

Submission status: Runtime Error

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 lenghth == 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: Runtime Error

Solution code:
K = int(input())
N = int(input())

A = []

for i in range(N):
    A.append(int(input()))
    
max = A[0]
memo = 0

for i in range(N-1):
    if max < A[i+1]-A[i]:
        max = A[i+1]-A[i]

if max < K-A[N-1]+A[0]:
    max = K-A[N-1]+A[0]
    
print(K-max)

Submission status: Runtime Error

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):
            # 取得したい形式に変換
            ret = list(map(int, input[x].split()))

    return ret

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
K,N=input().split()
K=int(K)
N=int(N)
A=list(map(int,input().split()))
i=0
C=[]
for i in range(N-1):
    if (2<=K<=10**5)and(2<=N<=2*10**5)and(0<=A[i]<A[i+1]<K)and(i<N-1):
        B=A[i+1]-A[i]
        C.append(B)
        i+=1
    if (i==N-1)and(A[N-1]<K):
        B=K-A[N-1]+A[0]
        C.append(B)
d=max(C)
e=K-d
print(e)

Submission status: Runtime Error

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 lenghth == 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: Runtime Error

Solution code:
a=a.split(' ')
for b in range(n):
    a[b]=int(a[b])

m=0
for b in range(n-1):
    if m<=a[b+1]-a[b]:
        m=a[b+1]-a[b]

if m<=k-a[n-1]+a[0]:
    m=k-a[n-1]+a[0]

print(k-m)

Submission status: Runtime Error

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: Runtime Error

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

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: Runtime Error

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

r = int(n/(3.14*2))
ans = 0
for i in range(r-1):
  ans+=arr[i+1]-arr[i]
print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

print(K-max(B))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

# 最短経路のリスト
min = []
ans = []

# 距離の取得
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]

# 出力
for j in range(0, N -1):
    print(ans[j])

Submission status: Runtime Error

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

A_list = list(itertools.permutations(l, N))
ans = 10**8
for i in A_list:
    tmp = 0
    
    if i[0] == min(A) and i[2] == max(A):
        pass
    else:
        for j in range(N-1):
            if abs((i[j+1]) - (i[j])) <= abs(K-i[j+1]):
                tmp = tmp +  abs((i[j+1]) - (i[j]))
            else:
                tmp = tmp + abs(K-i[j+1])
        
        if tmp <= ans:
            ans = tmp
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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


l_s = []
check = 0
for i in range(0,N):
    if(A[i] == 0):
        check =1
if(check == 1):
    for i in range(1,N+1):
          l_s.append(A[i]-A[i-1])
  
    print(l_s)
    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: Runtime Error

Solution code:
import numpy as np

K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
x=np.array(A[1:])
y=np.array(A[:-1])
B=x-y
B.append(K+A[0]-A[N-1])
print(K-int(max(B))

Submission status: Runtime Error

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

print(K-max(B))

Submission status: Runtime Error

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
D = np.zeros((N,N))
for i in range(N-1):
  for j in range(i+1,N):
    a = abs(A[i]-A[j])
    D[i][j] = min(a, K-a)
    D[j][i] = min(a, K-a)
ans = 10 ** 12
print (int(min(np.sum(D, axis=0))))

Submission status: Runtime Error

Solution code:
import numpy as np

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

next_ls = [0] * n
for i in range(n-1):
    next_ls[i] = a_ls[i+1] - a_ls[i]
next_ls[n-1] = k - a_ls[-1] + a_ls[0]
argmax = np.argmax(next_ls)
next_ls.pop(argmax)
print(sum(next_ls))

Submission status: Runtime Error

Solution code:
import copy

K,N = map(int, input().split())
An = list(map(int, input().split()))
ans = [max(An) - min(An)]
for i in range(len(An)/2,len(An)):
    A_list = copy.copy(An)
    A_list[i] = A_list[i]-K
    ans.append(max(A_list) - min(A_list))
print(min(ans))

Submission status: Runtime Error

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

print(K-max(B))

Submission status: Runtime Error

Solution code:
ddd = [input() for i in range(2)]
eee = ddd[0].split()
fff = ddd[1].split()
longest_distance = 0

for i in range(len(fff - 1)):
    distance = fff[i+1] - fff[i]
    if longest_distance < distance:
        longest_distance = distance

shortest_road = eee[0] - longest_distance

print(shortest_road)

Submission status: Runtime Error

Solution code:
N, K = 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: Runtime Error

Solution code:
import numpy as np

K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
x=np.array(A[1:])
y=np.array(A[:-1])
B=list(map(x-y))
B.append(K+A[0]-A[N-1])
print(K-max(B))

Submission status: Runtime Error

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

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

ans = 0
p = A[0]
low_index = 1
high_index = N-1
Done = set([p])

while len(A) > len(Done):
  #print('p:' + str(p))
  #print(Done)
  #print(ans)
  if abs(A[low_index] - p) < abs(p - A[high_index]):
    #print('low')
    Done.add(A[low_index])
    ans += A[low_index] - p
    p = A[low_index]
    for i in range(1, N):
      if low_index + i not in Done:
        low_index += i
        break
  else:
    #print('high')
    Done.add(A[high_index])    
    ans += K - A[high_index]
    p = A[high_index]
    for i in range(1, N):
      if high_index - i not in Done:
        high_index -= i
        break

print(ans)

Submission status: Runtime Error

Solution code:
ENkei, Nken = map(int, input().split())
Kyori_list = input().split()

Saidai_Kyori =  max(Kyori_list)
Saitan_Kyori = ENkei
for i in range(len(Kyori_list)):
    if (int(Kyori_list[i]) > 0) and (Saitan_Kyori > int(Kyori_list[i])):
        Saitan_Kyori = int(Kyori_list[i])

Idou_Kyori = Saidai_Kyorri - Saitan_Kyori
print(Idou_Kyori)

Submission status: Runtime Error

Solution code:
import numpy as np

K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
x=np.array(A[1:])
y=np.array(A[:-1])
B=x-y
B.append(K+A[0]-A[N-1])
print(K-max(B))

Submission status: Runtime Error

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

X = A[1] - A[0]
i = 1
while i < N-1:
    Y = A[i+1] - A[i]
    if X < Y:
        X = Y
    i += 1
Z = A[0] + K - A[N-1]
if X < Z:
    X = Z

print(K - X)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int,input().split()))
A.append(A[0]+K)
A.append(A[1]+K)
A.append(A[2]+K)
d = 0; ans = float("INF")
for i in range(N):
  d = 0
  for j in range(N-1):
    d += A[i+j+1] - A[i+j]
  ans = min(ans, d)
print(ans)

Submission status: Runtime Error

Solution code:
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)
print(N-d)

Submission status: Runtime Error

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]-a[0])
  for i in range(j+1,n-1):
    ans+=abs(a[i+1]-a[i])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

min_list = []
for i in range(N):
    if i == 0:
        left = an[i] + K - an[-1]
        right = an[i+1] - an[i]
    elif i == N-1:
        left = an[i] - an[i-1]
        right = an[0] + K - an[N-1]
    else:
        left = an[i] - an[i-1]
        right = an[i+1] - an[i]
    min_list.append(min(K-left, K-right))
print(min(n_list))

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
C = A.sort()
B=C[1:]-C[:-1]
B.append(K+C[0]-C[N-1])
print(K-max(B))

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = []
while all(a< N-1 for a in A):
  B.append(A[a+1]-A[a])

B.append(A[1]+K-A[a])

print(K-max(B))

Submission status: Runtime Error

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):
    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]-a[0])
  for i in range(j+1,n):
    ans+=abs(a[i+1]-a[i])
print(ans)

Submission status: Runtime Error

Solution code:
k=input().split(" ")[0]
a = list(map(lambda x: int(x), input().split("\n")[1].split(" ")))
df = max(a) - min(a)
print(min(df, k - df))

Submission status: Runtime Error

Solution code:
length, num = map(int, input().split())
dist=[int(i) for i in input().split()]

Max=0
for i in range(num):
    Max = max([abs(dist[(i+1)%num]-dist[i]),Max])

print(length-Max)
input()

Submission status: Runtime Error

Solution code:
B1=K-A[N-1]+A[0]

B=[]
i=0
while i<=N-2:
    B.append(A[i+1]-A[i])
    i=i+1
B=sorted(B)
print(K-max(B1,B[-1]))

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=A[1:]-A[:-1]
B.append(K+A[0]-A[N-1])
print(K-max(B))

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
a,b,c=map(int,input().split())
print(min(c-a,K-b+a,b+K-c))

Submission status: Runtime Error

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

ans = k - max_diff
print(ans)

Submission status: Runtime Error

Solution code:
k,n=map(int, input().split())
l=list(map(int, input().split()))
m=float("inf")
for i in range(len(l)):
  j = abs(l[-1-iL] - l[-i])
  if m == j:
    l[-1-i] -= k
  m=min(m, j)
print(m)

Submission status: Runtime Error

Solution code:
# 2入力
K, N = map(int, input().split())
A = input().split()
A = [int(i) for i in A]

lag = []
for i in range(len(A)):
    if i != N-1:
        lag.append(A[(i+1)%N] - A[i%N])
    else:
        lag.append(K-A[i] + A[0])

import numpy as np
max_index = np.argmax(lag)
del lag[max_index]
print(sum(lag))

Submission status: Runtime Error

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

o=l[-1][0]
g=a[o]+n-a[o+1]
print(g)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=A[1:]-x[:-1]
B.append(K+A[0]-A[N-1])
print(K-max(B))

Submission status: Runtime Error

Solution code:
import numpy as np
 
tmp =list(map(int, input().split()))
num=list(map(int, input().split()))
 
a[a>int(tmp[0]//2)]=a[a>int(tmp[0]//2)]-int(tmp[0]//2)
print(sum(a))

Submission status: Runtime Error

Solution code:
if a[0] == 0:
    print(a[len(a) - 1] - a[1])
else:
    print(a[len(a) - 1] - a[0])

Submission status: Runtime Error

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

x = I()
ans = (x // 500) * 1000
x %= 500
ans += x // 5 * 5
print(ans)

Submission status: Runtime Error

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

A_list = list(itertools.permutations(l, N))

print(A_list)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = []
for a in A:
  if a < N:
    B.append(A[a+1]-A[a])
  else:
    B.append(A[1]+K-A[a])

print(K-max(B))

Submission status: Runtime Error

Solution code:
import numpy as np

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

Submission status: Runtime Error

Solution code:
import numpy as np

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

a[a>int(tmp[0]//2)]=np.abs(a[a>int(tmp[0]//2)]-int(tmp[0]//2))

print(sum(a))

Submission status: Runtime Error

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

print(N-max(K))