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) = tuple(map(int,input().split()))
a = list(map(int,input().split()))

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

print(k-max(b))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Smax=0
for i in range(K):
    S=0
    if i==0:
        S=max(abs(A[0]-A[-1]),abs(A[0]-A[1]))
    elif i==K-1:
        S=max(abs(A[-1]-A[0]),abs(A[-1]-A[-2]))
    else:
        S=max(abs(A[i]-A[i+1]),abs(A[i]-A[i-1]))
    if Smax<S:
        Smax=S

print(K-Smax)

Submission status: Runtime Error

Solution code:
def main():
    k,n = map(int,input().split())
    pos = list(map(int, input().split()))
    dist[]
    for i in range(n-1):
        dist.append(pos[i+1] - pos[i])
    dist.append(k-pos[n]+pos[0])

    print(k - max(dist))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
 
K, N, *A = map(int, read().split())
A.append([A[0] + K])
gap = max(y - x for x, y in zip(A, A[1:]))
print(K - gap)

Submission status: Runtime Error

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

K, N, *A = map(int, read.split())

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

Submission status: Runtime Error

Solution code:
def main():
  k, N = (int(x) for in input().split())
  LIST = []
  min_LIST = []
  for i in range(N):
    l.append(int(input()))

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(min_LIST <= (LIST[i + 1] - LIST[i])):
      x = (LIST[i + 1] - LIST[i])

  if(min_LIST <= (LIST[0] + (k - LIST[-1])):
    x = (LIST[0] + (k - LIST-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
f=list(map(int,input().split()))
k=f[0];n=f[1]
dists=list(map(int,input().split()))
prev=0
maxdist=0
for d in range(1,len(dists)):
  maxdist=max(maxdist,abs(prev-dists[d]))
  prev=dists[d]
a=max(maxdist,maxdist-dists[len(dists)]+dists[0])  
ans=k-a
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
k, n = map(int, input().split())
list_a = list(map(int, input().split()))
 
x = 0
longest = 0
for a in list_a:
  if a - x > longest:
    longest = a - x
    x = a - x
if longest < a[0] + (k - a[n-1]):
  longest = a[0] + (k - a[n-1])
print(k-longest)

Submission status: Runtime Error

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

x = 0
longest = 0
for a in list_a:
  if a - x > longest:
    longest = a - x
    x = a - x
if longest < a[0] + (k - a[n-1]):
  longest = a[0] + (k - a[n-1]):
print(k-longest)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split())
distances = {}
for i in range(1, N):
	distances += (A[i] - A[i - 1]
most = max(distances.keys())
distances += (A[0] + K) - A[-1]                   
output = K - most
print(output)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split())
distances = {}
for i in range(1, len(A)):
	distances += (A[i] - A[i - 1]
most = max(distances.keys())
distance += (A[0] + K) - A[-1]                   
output = K - most
print(output)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split())
distances = {}
for i in range(1, len(A) + 1):
	distances += (A[i] - A[i - 1]
most = max(distances.keys())
output = K - most
print(output)

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split())
distances = {}
for i in range(1, len(A) + 1):
	distances += (A[i] - A[i - 1]
most = max(distances.keys())
output = K - most
                  print(output)

Submission status: Runtime Error

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

m=0
for i in range(n-1):
    d=a[i+1]-a[i]
    m=max(d,m)
d_last=a[0]+k-a[n-1]
m=max(d_last,m)

ans=k-m
print(ans)

Submission status: Runtime Error

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

m=0
for i in range(n-1):
    d=a[i+1]-a[i]
    m=max(d,m)
d_last=a[0]+k-a[n-1]
m=max(d_last,m)

ans=k-m
print(ans)

Submission status: Runtime Error

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

m=0
for i in range(n-1):
    d=a[i+1]-a[i]
    m=max(d,m)
d_last=a[0]+k-a[n-1]
m=max(d_last,m)

ans=k-m
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def main():
    x, y = map(int, input().split())
    a = list(map(int, input().split()))
    cost = 10*10
    for i in range(n-1):
        cost = min(cost, k - abs(a[i]-a[i+1]))
    print(min(cost, k-abs(a[0]+k-a[n-1])))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

num[0] = [min([A[K-1]-A[0], A[0]+K-A[1]])]
num[i+1] = [min([K-A[i+1]+A[i], A[i+1]+K-A[i+2]]) 
          for i in range(N-2)]
num[N-1] = [min([A[K-1]-A[0], A[N-2]+K-A[N-1]])]
          
print(min(num))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K,N = map(int, input().split())
An_list = np.array(input().split(), dtype=int)

An_delta_list = np.diff(An_list)
An_delta_list = np.append(An_delta_list, K - An_list[N-1])
An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max
print(result)

Submission status: Runtime Error

Solution code:
#入出力
K,N = map(int, input("K,N:").split())
An_list = np.array(input("An:").split(), dtype=int)

# ここの距離を求める
An_delta_list = np.array([], dtype=int)

# 差のリストをつくる
for i in range(len(An_list)-1):
    An_delta_list = np.append(An_delta_list, An_list[i+1] -An_list[i])
An_delta_list = np.append(An_delta_list, K - An_list[-1])

# 差が最大の部分を見つける
An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max


print(result)

Submission status: Runtime Error

Solution code:
#入出力
K,N = map(int, input("K,N:").split())
An_list = np.array(input("An:").split(), dtype=int)


"""
# 入出力
K = 20
N = 3
An_list = np.array([0, 5, 15], dtype=int)
"""


# ここの距離を求める
An_delta_list = np.array([], dtype=int)

# 差のリストをつくる
for i in range(len(An_list)-1):
    An_delta_list = np.append(An_delta_list, An_list[i+1] -An_list[i])
An_delta_list = np.append(An_delta_list, K - An_list[-1])

# 差が最大の部分を見つける
An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max
print(result)

"""
print("全リスト：", An_delta_list)
print("差の最大の値：", An_delta_max)
print("結果：", result)
"""

Submission status: Runtime Error

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

num[i] = [max([K-A[i]+A[i-1], A[i]+K-A[i]]) 
          for i in range(cnt)]
print(max(num))

Submission status: Runtime Error

Solution code:
arrayNM = list(map(int, input().split()))
array = list(map(int, input().split()))
route = [array[0] + arrayNM[0] - array[arrayNM[1] - 1]]
for i in range(1,arrayNM[1]):
  route.append(array[i] - array[i-1])
  
Max = max(route)
maxIndex = route.index(Max)
distance = 0

for i in range(0,maxIndex):
  distance += route[i]

for i in range(maxIndex + 1, array[1]):
  distance += route[i]

print(distance)

Submission status: Runtime Error

Solution code:
arrayNM = list(map(int, input().split()))
array = list(map(int, input().split()))
route = [array[0] + arrayNM[0] - array[arrayNM[1] - 1]]
for i in range(1,arrayNM[1]):
  route.append(array[i] - array[i-1])
  
Max = max(route)
maxIndex = route.index(Max)
distance = 0

for i in range(0,maxIndex):
  distance += route[i]

for i in range(maxIndex + 1, array[1]):
  distance += route[i]

print(distance)
print(route)

Submission status: Runtime Error

Solution code:
arrayNM = list(map(int, input().split()))
array = list(map(int, input().split()))
route = [array[0] + arrayNM[0] - array[arrayNM[1] - 1]]
for i in range(1,arrayNM[1]):
  route.append(array[i] - array[i-1])
  
Max = max(route)
maxIndex = route.index(Max)
distance = 0

for i in range(0,maxIndex):
  distance += route[i]

for i in range(maxIndex, array[0]):
  distance += route[i]

print(distance)
print(route)

Submission status: Runtime Error

Solution code:
# coding: UTF-8
import os

def remove_empty(target_list: list):
    empty_removed_list = [x for x in target_list if x]
    return empty_removed_list


def get_stdin_str_list(input_str=None):
    if input_str is None:
        input_str = input()
    input_str = input_str.strip()
    input_str_list = input_str.split()
    return input_str_list


def get_stdin_int_list(input_str=None):
    input_str_list = get_stdin_str_list(input_str)
    input_int_list = list(map(int, input_str_list))
    return input_int_list


def get_stdin_str_multi_list(input_str=None):
    if input_str is None:
        input_str = input()
    input_str = input_str.strip()
    input_str_line_list = input_str.split("\n")
    input_str_multi_list = list()
    for str_line in input_str_line_list:
        input_str_list = get_stdin_str_list(str_line)
        input_str_multi_list.append(input_str_list)
    return input_str_multi_list


def get_stdin_int_multi_list(input_str=None):
    input_str_multi_list = get_stdin_str_multi_list(input_str)
    input_int_multi_list = list()
    for str_list in input_str_multi_list:
        input_int_list = list(map(int, str_list))
        input_int_multi_list.append(input_int_list)
    return input_int_multi_list

def get_most_far_pos_index_from_both_neighbors(pos_list):
    N = len(pos_list)
    max_distance = 0
    most_far_pos_index = 0
    for i, p in enumerate(pos_list):
        if i == 0:
            distance = pos_list[-1] + pos_list[i+1]
        elif i == N-1:
            distance = pos_list[i-1] + pos_list[0]
        else:
            distance = pos_list[i-1] + pos_list[i+1]
        
        if distance > max_distance:
            max_distance = distance
            most_far_pos_index = i
            
    return most_far_pos_index

input_list = get_stdin_int_multi_list()
K, N = input_list[0]
A = input_list[1]
start_i = get_most_far_pos_index_from_both_neighbors(A)

if start_i == 0:
    far_neighbor_pos_distance = abs(A[start_i+1]-A[start_i]) if abs(A[start_i+1]-A[start_i]) > abs(A[start_i]-A[N-1]) else abs(A[start_i]-A[N-1])
elif start_i == N-1:
    far_neighbor_pos_distance = abs(A[0]-A[start_i]) if abs(A[0]-A[start_i]) > abs(A[start_i]-A[start_i-1]) else abs(A[start_i]-A[start_i-1])
else:
    far_neighbor_pos_distance = abs(A[start_i+1]-A[start_i]) if abs(A[start_i+1]-A[start_i]) > abs(A[start_i]-A[start_i-1]) else abs(A[start_i]-A[start_i-1])

shortest_distance = K - far_neighbor_pos_distance
print(shortest_distance)

Submission status: Runtime Error

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

max_distance = K + (A[0] - A[-1])

for i in range(1, N):
  max_distance = max(max_distance, A[i] - A[i - 1])
  
print(K - max_distance)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
houselist = list(map(int, input().split()))
max=0
for i in range(1,N-1):
  if houselist(i)-houselist(i-1)>max:
    max=houselist(i)-houselist(i-1)
if K+houselist(0)-houselist(N)>max:
  max=K+houselist(0)-houselist(N)
print(K-max)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

b.append(K - a[N-1] + a[0])

ans = K - max(b)

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split(" "))
arr = map(int, input().split(" ")
          
print(min(arr) - max(arr))

Submission status: Runtime Error

Solution code:
k,n=map(int,input().split())
a=map(int,input().split())
max = -1
for i in range(0,n-1):
  if max>abs(a[i]-a[i+1]):
    max=abs(a[i]-a[i+1])
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()))
dist = [k-dist[n-1]+dist[0]]
for i in range(n):
  dist.append(a[i+1]-a[i])

print(k-max(dist))

Submission status: Runtime Error

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

A_split.pop(A_split.index(max(A_split)))
print(sum(A_split))

Submission status: Runtime Error

Solution code:
#include <stdio.h>
int main(void){
    long k,n,i,max=-1,a[200000];
    scanf("%ld%ld",&k,&n);
    for(i=0;i<n;i++) scanf("%ld",&a[i]);
    for(i=0;i<n-1;i++){
        int tmp=a[i+1]-a[i];
        if(tmp>max) max=tmp;
    }
    printf("%ld",k-max);
    return 0;
}

Submission status: Runtime Error

Solution code:


def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right


if __name__ == "__main__":
    k,n = list((map(int, input().split(' '))))
    a = list(map(int, input().split(' ')))
    a = quick_sort
    result = 0
    for i in range(1,n):
        length = a[i]-a[i-1]
        result = max(length, result)
        
    length = a[0] + (k-a[n-1])
    result = max(result, length)
    print(k-result)
    





      
# for i in range(10):
#     print(i)
#     if i%5==4:
#         break

Submission status: Runtime Error

Solution code:

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right


if __name__ == "__main__":
    k,n = list((map(int, input().split(' '))))
    a = list(map(int, input().split(' ')))
    a = quick_sort
    result = 0
    for i in range(1,n):
        length = a[i]-a[i-1]
        result = max(length, result)
        
    length = a[0] + (k-a[n-1])
    result = max(result, length)
    print(k-result)
    





      
# for i in range(10):
#     print(i)
#     if i%5==4:
#         break

Submission status: Runtime Error

Solution code:
K,N=map(int,input().split())
A=[list(map(int,input().split()))]
del A[index(min(A))]
for a in A:
  a-=min(A)
print(max(A)-min(A))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    c = np.zeros(a[1])
    c[:-1] = b[1:]
    c[-1] = a[0] + b[0]
    print(int(a[0] - max(c - np.array(b))))

Submission status: Runtime Error

Solution code:
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    c = np.zeros(a[1])
    c[:-1] = b[1:]
    c[-1] = b[0]
    print(int(a[0] - max(abs(c - np.array(b)))))

Submission status: Runtime Error

Solution code:
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    c = np.zeros(a[1])
    c[:-1] = b[1:]
    c[-1] = a[0]
    print(int(a[0] - max(c - np.array(b))))

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = np.zeros(a[1])
c[:-1] = b[1:]
c[-1] = a[0]
print(a[0] - max(c - np.array(a)))

Submission status: Runtime Error

Solution code:
k,n = map(int input().split())
tab = [int(x) for x in input().split()]
tab.sort()
dist=-tab[0]+tab[n-1]
print(dist)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
new = []
for i in range(N-1):
    new.append(A[i+1]-A[i])
new.append(K + A[0] - A[-1])
new.sort()
print(sum(new[:y-1]))

Submission status: Runtime Error

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
new = []
for i in range(N-1):
    new.append(A[i+1]-A[i])
new.append(K + A[0] - A[-1])
new.sort()
print(sum(new[:y-1]

Submission status: Runtime Error

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

Submission status: Runtime Error

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

relative = []
relative[0] = distance[0]
for i in range(1, len(distance)):
    relative[i] = distance[i+1] - distance[i]
relative.append(K -  distance[-1])
print(sum(relative) - max(relative))

Submission status: Runtime Error

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

x = k-m
print(x)

Submission status: Runtime Error

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

for i in range(n-1)
	m = max(m,a[i+1]-m[n])

x = k-m
print(x)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
kp = list(map(int, input().rsplit()))
A = list(map(int, input().rsplit()))
max_range = kp[0]-A[-1]+A[0]
for idx in range(len(A)):
  dif = A[idx+1]-A[idx] 
  if dif >max_range:
    max_range = dif

print(kp[0] - max_range)

Submission status: Runtime Error

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

max = 0

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

if (k - a[n]) + a[0] >= max:
  max = (k - a[n]) + a[0]

print(k - max)

Submission status: Runtime Error

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

max = 0

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

if (k - a[n]) + a[0] >= max:
  max = (k - a[n]) + a[0]

print(k - max)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
k=int(input())
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
b=[]
for j in range(-1,n-1):
  if j==-1:
    b.append((k-a[j])+a[j+1])
  else:
    b.append(a[j+1]-a[j])
print(k-max(b))

Submission status: Runtime Error

Solution code:
k=int(input())
n=int(input())
for i in range(n):
  a[i]=int(input())
for j in range(-1,n):
  if j==-1:
    b[j+1]=(k-a[j])+a[j+1]
  else:
    b[j+1]=a[j+1]-a[j]
print(k-b.max())

Submission status: Runtime Error

Solution code:
k,n=int(input())
for i in range(n):
  a[i]=int(input())
for j in range(-1:n):
  if j==-1:
    b[j+1]=(k-a[j])+a[j+1]
  else:
    b[j+1]=a[j+1]-a[j]
print(k-b.max)

Submission status: Runtime Error

Solution code:
k,n=int(input())
for i in range(n):
  a[i]=int(input())
for j in range(-1:n):
  if j==-1:
    b[j+1]=(k-a[j])+a[j+1]
  else:
    b[j+1]=a[j+1]-a[j]
print(k-b.max())

Submission status: Runtime Error

Solution code:

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

A.remove(0)
A.append(A[0])

max_distance = 0
pre = 0

for i in A:

    if abs(i - pre) > max_distance:
        max_distance = abs(i - pre)
    pre = i



print (K - max_distance)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K = int(input())
N = int(input())
A = {}
for i in range(N):
    A[i] = int(input())
max = 0
for i in range(N):
    len = 0
    if i == 0:
        len = int(A[i] + (K - A[N - 1]))
    else:
        len = int(A[i] - A[i - 1])

    if len > max:
        max = len

print(K - max)

Submission status: Runtime Error

Solution code:
k, n = map(int, input().split())
houses = list(map(int, input().split()))
  
for i in range(n):
   houses[i] = abs(k-houses[i])

a = min(houses)
house.pop(a)
b = min(houses)

fw = abs(k-a) + abs(k-b)
sw = abs(a-b)
     
print(min(fw,sw))

Submission status: Runtime Error

Solution code:
k, n = map(int, input().split())
houses = list(map(int, input().split()))
smallestAnswer = max(houses)
  
for i in range(n):
   houses[i] = abs(k-houses[i])

a = min(houses)
house.pop(a)
b = min(houses)

fw = abs(k-a) + abs(k-b)
sw = abs(a-b)
     
print(min(fw,sw))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()    
# 1行ずつ取り出し
KN = input().strip(" ")
A = input().strip(" ")

K=int(KN[0])

X = K - int(A[-1])
temp = 0
for Ai in A:
  Y = int(Ai) - temp
  if Y > X:
    X = Y
  temp = int(Ai)

print(K-X)

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()    
# 1行ずつ取り出し
KN = lines[0].strip(" ")
A = lines[1].strip(" ")

K=int(KN[0])

X = K - int(A[-1])
temp = 0
for Ai in A:
  Y = int(Ai) - temp
  if Y > X:
    X = Y
  temp = int(Ai)

print(K-X)

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()    
# 1行ずつ取り出し
KN = lines[0].strip(" ")
A = lines[1].strip(" ")

K=KN[0]

X = K - A[-1]
temp = 0
for Ai in A:
  Y = Ai - temp
  if Y > X:
    X = Y
  temp = Ai

print(K-X)

Submission status: Runtime Error

Solution code:
import math
K = int(input())
N = int(input())
posilist = [0]
for inppp in range(0,N):
    posilist.append(int(input()))
posilist.sort()
if posilist[N] - posilist[1] < K - posilist[1]:
    print(posilist[N] - posilist[1])
else:
    print(K - posilist[1])

Submission status: Runtime Error

Solution code:
K, N = list(map(int, input().srtip().split()))
As = list(map(int, input().strip().split()))
 
diffs =[]
for base, switch in zip(As[:-1], As[1:]):
  diffs.append(switch-base)
 
diffs.append(As[0]+K-As[-1])
 
print(K-max(diffs))

Submission status: Runtime Error

Solution code:
K, N = list(map(int, input().srtip().split()))
As = list(map(int, input().strip().split()))
 
diffs =[]
for base, switch in zip(arr[:-1], arr[1:]):
  diffs.append(switch-base)

diffs.append(arr[0]+K-arr[-1])
 
print(K-max(diffs))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stsin.readlines()
K, N = list(map(int, lines[0].srtip().split()))
As = list(map(int, lines[1].strip().split()))
 
diffs =[]
for base, switch in zip(arr[:-1], arr[1:]):
  diffs.append(switch-base)

diffs.append(arr[0]+K-arr[-1])
 
print(K-max(diffs))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stsin.readlines()
K, N = list(map(int, lines[0].srtip().split()))
As = list(map(int, lines[1].strip().split()))

diffs =[]
for base, switch in zip(arr[:-1], arr[1:]):
  diffs.append(switch-base)

diffs.append(arr[0]+K-arr[-1])

print(K-max(diffs))

Submission status: Runtime Error

Solution code:
K, N = map(int, input().split())
As = map(int, input().split())
dists = []
for base, switch in zip(As[:-1], As[1:]):
    dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stsin.readlines()
K, N = map(int, lines[0].srtip().split())
As = map(int, lines[1].strip().split())

diffs =[]
for base, switch in zip(arr[:-1], arr[1:]):
  diffs append(switch-base)

diffs append(arr[0]+K-arr[-1])

print(K-max(diffs))

Submission status: Runtime Error

Solution code:
lines = input().split()
dists = []
for base, switch in zip(As[:-1], As[1:]):
    dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
lines = input().split()
K, N = map(int, lines[0].split())
As = map(int, lines[1].split())
dists = []
for base, switch in zip(As[:-1], As[1:]):
    dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
lines = input().split()
K, N = map(int, lines[0].strip().split())
As = map(int, lines[1].strip().split())
dists = []
for base, switch in zip(As[:-1], As[1:]):
    dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stsin.readlines()
K, N = map(int, lines[0].srtip().split())
As = map(int, lines[1].strip().split())

diffs =[]
for base, switch in zip(arr[:-1], arr[1:]+[0]):
  diffs append(switch-base)

diffs append(arr[0]+K-arr[-1])

print(K-max(diffs))

Submission status: Runtime Error

Solution code:
lines = map(int, input().split())
K, N = map(int, lines[0].strip().split())
As = map(int, lines[1].strip().split())
dists = []
for base, switch in zip(As[:-1], As[1:]):
    dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()
K, N = map(int, lines[0].strip().split())
As = map(int, lines[1].strip().split())
dists = []
for base, switch in zip(As[:-1], As[1:]):
  dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()
K, N = map(int, lines[0].strip().split())
As = map(int, lines[1].strip().split())
dists = []
for idx, A in enumerate(As[:-1]):
  dists.append(A - As[idx+1])
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()
K, N = map(int, lines[0].strip().split())
As = map(int, lines[1].strip().split())
for idx, A in enumerate(As[:-1]):
  dists.append(A - As[idx+1])
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))