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:
n,m =map(int, input().split())
l = list(map(int, input().split()))
print(abs(l[0]-l[m-1]))

Submission status: Wrong Answer

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

res = 0
for i in range(n):
    if arr[i] != 0:
        res += abs(arr[i]-(k/2))
print(int(res))

Submission status: Wrong Answer

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

A = []
for i in input().split():
  A.append(int(i))

mergin = []
for i in range(len(A)-1):
  mergin.append(A[i+1]-A[i])
mergin.append(A[0]+20-A[-1])
mergin.sort()

print(K-mergin[-1])

Submission status: Wrong Answer

Solution code:
date0=input().rstrip().split(" ")
date1=input().rstrip().split(" ")
list0=[]
list1=[]
for i in range(int(date0[1])):
  list0.append(int(date1[i]))
#print(list0)
list0=sorted(list0)
#print(list0)
for j in range(int(date0[1])-1):
  list1.append(list0[j+1]-list0[j])
list1.append(int(date0[0])-list0[int(date0[1])-1])
list1=sorted(list1,reverse=True)
print(int(date0[0])-list1[0])

Submission status: Wrong Answer

Solution code:
round,house_num = map(int,input().split())

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

min = distance[-1] - distance[0]

for i in range(house_num - 1):
  if round-(distance[i+1]-distance[i]) < min:
    min = round-(distance[i+1]-distance[i])
  print(min)

Submission status: Wrong Answer

Solution code:
round,house_num = map(int,input().split())

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

tmp = [i-round if abs(i-round)<i else i for i in distance]
    
ans1 = max(distance) - min(distance)
ans2 = max(tmp) - min(tmp)

print(min(ans1,ans2))

Submission status: Wrong Answer

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

import numpy as np
A=np.array(A)

ans=max(np.diff(A).max(), K-A[-1]+A[0])
if ans<=K/2:
    print(ans)
else:
    print(K-ans)

Submission status: Wrong Answer

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
if longest < list_a[0] + (k - list_a[n-1]):
  longest = list_a[0] + (k - list_a[n-1])
print(k-longest)

Submission status: Wrong Answer

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 < list_a[0] + (k - list_a[n-1]):
  longest = list_a[0] + (k - list_a[n-1])
print(k-longest)

Submission status: Wrong Answer

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
print(k-longest)

Submission status: Wrong Answer

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

a.append(kn[0])
dist = []
for i in range(1, kn[1]+1):
    dist.append(a[i]-a[i-1])

dist[dist.index(max(dist))] = 0

print(sum(dist))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k)
longest = 0
prev = a[0]
for h in a[1:]:
    dif = h - prev
    prev = h
    if dif > longest:
        longest = dif
print(k - longest - a[0])

Submission status: Wrong Answer

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

for i in range(N):
    if i < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    print(sa)
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if i + 1 < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if i + 1 < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    print(sa)
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)

Submission status: Wrong Answer

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

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

gap = []

for i in range(N-1):
    if i + 1 < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)

Submission status: Wrong Answer

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

for i in range(N-1):
  d.append(A[i+1] - A[i])
d.append((K - A[N-1]) + A[0])

print(d)
d.remove(max(d))
print(sum(d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = [ int (x) for x in input().split() ]

kyori_sa = []

print(len(a)-1)


for i in range(n):
  if i == len(a)-1:
    kyori_sa.append(a[0] - ( k - a[i] ))
  else:
    kyori_sa.append(a[i+1] - a[i])

print(k-max(kyori_sa))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n, k = map(int, input().split())
A = list(map(int, input().split()))
AA = sorted(A)

print(AA[-1]-A[0])

Submission status: Wrong Answer

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

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

res = k - m
print(res)

Submission status: Wrong Answer

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

print(K-a)

Submission status: Wrong Answer

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

print(min(K-a,A[0]))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=[int(_) for _ in 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: Wrong Answer

Solution code:
def main():
    k, n = 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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
lake, houses = list(map(int, input().split()))
distances = list(map(int, input().split()))
if distances[0] == 0:
    answer = min(distances[-1] - distances[0], abs(distances[-1] - distances[1]))
else:
    answer = distances[-1] - distances[0]
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
i = []
for line in sys.stdin.readlines():
  i.append(line.rstrip().split(" "))

list_i1 = [int(s) for s in i[1]]

a = 0
distance = []
for list_i1[a] in list_i1:
  if a > len(list_i1)-2:
    break
  distance.append(list_i1[a+1] - list_i1[a]) 
  a += 1
  
for b in distance:
  b += b
distance.append(int(i[0][0]) - b)

print(int(i[0][0]) - max(distance))

Submission status: Wrong Answer

Solution code:
import sys
i = []
for line in sys.stdin.readlines():
  i.append(line.rstrip().split(" "))

#1軒目と2軒目
a = int(i[1][1]) - int(i[1][0])
#2軒目と3軒目
b = int(i[1][2]) - int(i[1][1])
#1軒目と3軒目(K-1軒目から3軒目の距離)
c = int(i[0][0]) - (a + b)

if a < c:
  print(a + b)
else:
  print(a + c)

Submission status: Wrong Answer

Solution code:
# スペース区切りの文字列の入力
KN = input().split()
A = input().split()

i  = ans = 0
max = 0

for i in range(int(KN[1])):
  if(i == int(KN[1]) - 1):
    A2 = int(KN[0])
  else:
    A2 = int(A[i+1])
    
  # 距離が最大のもの以外をansに加算
  if(max < abs(A2 - int(A[i]))):
      ans += max
      max = abs(A2 - int(A[i]))
  else:
    ans += abs(A2 - int(A[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
# スペース区切りの文字列の入力
KN = input().split()
A = input().split()

i  = ans = 0
max = 0

for i in range(int(KN[1])):
  if(i == int(KN[1]) - 1):
    A2 = int(KN[0])
  else:
    A2 = int(A[i+1])
    
  # 距離が最大のもの以外をansに加算
  if(max < abs(A2 - int(A[i]))):
    if(max == 0):
      max = abs(A2 - int(A[i]))
      ans += max
    else:
      ans += max
      max = abs(A2 - int(A[i]))
  else:
    ans += abs(A2 - int(A[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
# スペース区切りの文字列の入力
KN = input().split()
A = input().split()

i  = 0
max = ans = 0

for i in range(int(KN[1])):
  if(i == int(KN[1]) - 1):
    A2 = int(KN[0])
  else:
    A2 = int(A[i+1])
    
  # 距離が最大のもの以外をansに加算
  if(max < abs(A2 - int(A[i]))):
    ans += max
    max = abs(A2 - int(A[i]))
  else:
    ans += abs(A2 - int(A[i]))

print(ans)

Submission status: Wrong Answer

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

an.append(an[0]+k)
a = an[0]
for i in range(n+1):
    temp = an[i]
    an[i] -= a
    a = temp
print(20-max(an))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

K = a[0]
N = a[1]

c = np.zeros(N)

for i in range(N-1):
  c[i] = abs(b[i+1] - b[i])
  
c[N-1] = abs(K - b[N-1] + b[1])

r = max(c)

print(int(K-r))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

K = a[0]
N = a[1]

c = np.zeros(N)

for i in range(N-1):
  c[i] = abs(b[i+1] - b[i])
  
c[N-1] = abs(K - b[N-1])

r = max(c)

print(int(K-r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
li = list(map(int,input().split()))
if li[n - 1] - li[0] <= li[n - 2] + (k-li[n - 1]):
    print(li[n - 1] - li[0])
else:
    print(li[n - 2] + (k-li[n - 1]))

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
li = list(map(int,input().split()))
li.sort()
lenght = len(li)
if li[lenght - 1] - li[0] <= li[lenght - 2] + (k-li[lenght - 1]):
    print(li[lenght - 1] - li[0])
else:
    print(li[lenght - 2] + (k-li[lenght - 1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(" "))
houses = list(map(int, input().split(" ")))
print(houses)
dif_houses = [houses[x+1]-houses[x] for x in range(k-1)]
dif_houses.append(houses[0]-houses[k-1]+n)

max_dif = max(dif_houses)
print(n-max_dif)

Submission status: Wrong Answer

Solution code:
a,b= map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
ar.append(a)
count = 0
mx = 0
for i in range(len(ar)-1):
    count += ar[i+1] - ar[i]
    if mx < ar[i+1] - ar[i]:
        mx = ar[i+1] - ar[i]
print(count - mx)

Submission status: Wrong Answer

Solution code:
N = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

distanceList = []

distance = 0
for i in range(N[1]):
    M.append((M[i] + N[0]))

for i in range(len(M) - 1):
    distanceList.append(M[i + 1] - M[i])

print(distanceList)
print(N[0] - max(distanceList))

Submission status: Wrong Answer

Solution code:
import numpy as np

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[-1])
An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max
print(result)

Submission status: Wrong Answer

Solution code:
k,n  = map(int,input().split())
diff = lambda x:x[1]-x[0]
sum_pair = lambda x:x[0]+x[1]
a = list(map(int,input().split()))
distance = a+[a[0]+k]
diffs = [diff(distance[i:i+2]) for i in range(len(distance)-1)]
diffs += [diffs[0]]
sums = [sum_pair(diffs[i:i+2]) for i in range(len(diffs)-1)]
print(min(sums))

Submission status: Wrong Answer

Solution code:
import numpy as np

K,N = map(int, input().split())
An_list = np.array(input().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: Wrong Answer

Solution code:
import numpy as np

Submission status: Wrong Answer

Solution code:
import numpy as np


##get int
first_input = list(map(lambda x:int(x), input().split(" ")))
second_input = list(map(lambda x:int(x), input().split(" ")))

##get float
#first_input = list(map(lambda x:float(x), input().split(" ")))
#second_input = list(map(lambda x:float(x), input().split(" ")))

##get string
#first_input = input().split(" ")

K = first_input[0]
N= first_input[1]

A_list = second_input

A_list.append(K+A_list[0])

dist_list = np.array(A_list)[1:]-np.array(A_list)[:-1]
print(A_list)
print(dist_list)

print(sum(dist_list)-max(dist_list))

Submission status: Wrong Answer

Solution code:
import numpy as np


##get int
first_input = list(map(lambda x:int(x), input().split(" ")))
second_input = list(map(lambda x:int(x), input().split(" ")))

##get string
#first_input = input().split(" ")

K = first_input[0]
N= first_input[1]

A_list = second_input

A_list.append(K)

dist_list = np.array(A_list)[1:]-np.array(A_list)[:-1]

print(sum(dist_list)-max(dist_list))

Submission status: Wrong Answer

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

print(
  n - \
    max(
      [a_2 - a_1 \
          for a_1, a_2 in \
          zip(
            a_list[:-1], 
            a_list[1:]
          )
      ] + \
      [n - a_list[-1] + a_list[0]]
    )
)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
for i in range(n):
	if i == n-1:
		b[i]=k-a[i]+a[0]
		print("I")
	else:
		b[i]=a[i+1]-a[i]
	print(i,b[i])
		
max_a=0
for i in range(n):
	if b[i]>max_a:
		max_a=b[i]
		print(i,max_a)

print(k-max_a)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

K, N = na()
A = na()
A.append(K)
t = [A[i+1] - A[i] for i in range(N)]
print(sum(t) - max(t))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

Bn=An[1:]+[K+An[0]]
print(An)
print(Bn)
dis = [x-y for x,y in zip(An,Bn)]
print(K-abs(min(dis)))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
*a, = map(int, input().split())
diffs = [a[i + 1] - a[i] if i < n - 1 else k - a[i] for i in range(n)]
diffs.sort()
print(sum(diffs[:-1]))

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
*a, = map(int, input().split())
diffs = []
for i in range(n):
    if i == n - 1:
        diff = k - a[i]
    else:
        diff = a[i + 1] - a[i]
    diffs.append(diff)
diffs.sort()
print(sum(diffs[:n - 1]))

Submission status: Wrong Answer

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

max = -1

for i in range(0,n-1):
  d=abs(a[i+1]-a[i])
  if max<d:
    max=d

if max<k-a[n-1]+a[0]:
  max=k-a[n-1]+a[0]
  
print(max)

Submission status: Wrong Answer

Solution code:
def int_spaced_input():
    return map(int, input().split())

def get_distances(A):
    distances = []
    for i in range(1, len(A)):
        prev = A[i-1]
        curr = A[i]
        distances.append(abs(prev-curr))
    return distances
  
K, N = int_spaced_input()
A = list(int_spaced_input())

distances = get_distances(A)
# add last house near the north
distances.append(K - A[-1])

maximum_segment = max(distances)
minimum_travel = sum(distances) - max(distances)

print(minimum_travel)

Submission status: Wrong Answer

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, end_pos_distance=None):
    if end_pos_distance is None:
        end_pos_distance = pos_list[-1]
    N = len(pos_list)
    max_distance = 0
    most_far_pos_index = 0
    for i, p in enumerate(pos_list):
        if i == 0:
            distance = end_pos_distance - pos_list[-1] + pos_list[i+1]
        elif i == N-1:
            distance = end_pos_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

K, N = get_stdin_int_list()
A = get_stdin_int_list()
start_i = get_most_far_pos_index_from_both_neighbors(A, K)

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(K-A[N-1]+A[start_i]) else abs(K-A[N-1]+A[start_i])
elif start_i == N-1:
    far_neighbor_pos_distance = abs(K-A[start_i]+A[0]) if abs(K-A[start_i]+A[0]) > 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: Wrong Answer

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, end_pos_distance=None):
    if end_pos_distance is None:
        end_pos_distance = pos_list[-1]
    N = len(pos_list)
    max_distance = 0
    most_far_pos_index = 0
    for i, p in enumerate(pos_list):
        if i == 0:
            distance = end_pos_distance - pos_list[-1] + pos_list[i+1]
        elif i == N-1:
            distance = end_pos_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

i = "20 3"
j = "5 10 15"
K, N = get_stdin_int_list(i)
A = get_stdin_int_list(j)
start_i = get_most_far_pos_index_from_both_neighbors(A, K)

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(K-A[N-1]+A[start_i]) else abs(K-A[N-1]+A[start_i])
elif start_i == N-1:
    far_neighbor_pos_distance = abs(K-A[start_i]+A[0]) if abs(K-A[start_i]+A[0]) > 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: Wrong Answer

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

K, N = get_stdin_int_list()
A = get_stdin_int_list()
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: Wrong Answer

Solution code:
import sys

k,n=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))

max=0
for i in range(n):
    if i==0:
        pre=a[i]
    else:
        now=a[i]
        if now-pre>max:
            max=now-pre
        pre=now
if k-pre+a[0]>max:
    max=k-pre+a[0]
print(max)

Submission status: Wrong Answer

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-1]>max:
  max=K+houselist[0]-houselist[N-1]
if max==0:
  print(0)
else:
  print(K-max)

Submission status: Wrong Answer

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-1]>max:
  max=K+houselist[0]-houselist[N-1]
print(K-max)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
L = [i for i in map(int,input().split())]
L.append(L[0])
dist = []
for i in range(N):
  if L[i+1] == 0 or L[i+1] == K:
    dist.append(min(abs(0-L[i]),abs(K-L[i])))
  else:
    dist.append(abs(L[i+1]-L[i]))
print(sum(dist)-max(dist))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a2 = []
for i, d in enumerate(a):
    if i != 0:
        a2.append(d - a[i - 1])
    else:
        a2.append(d + k - a[n - 1])
print(a, a2)
a2.sort(reverse=True)
print(a, a2)
print(k - a2[0])

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
temp = a[0] + k - a[n - 1]
for i, d in enumerate(a):
    if i != 0:
        a[i] = d - a[i - 1]
a[0] = temp
a.sort(reverse=True)
print(k - a[0])

Submission status: Wrong Answer

Solution code:
KN = list(map(int, input().split()))
dis = list(map(int, input().split()))
lists = []
for i in range(KN[1]-1):
    lists.append(dis[i+1]-dis[i])

lists.append(KN[0]+dis[0]-dis[KN[1]-1])
print(lists)
print(KN[0]-max(lists))

Submission status: Wrong Answer

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

for i, d in enumerate(a):
    if i != 0:
        a[i] = d - a[i - 1]
    else:
        a[i] = d + 20 - a[n - 1]

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a = a + [k]
for i, d in enumerate(a):
    if i != 0:
        a[i] = d - a[i - 1]

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a = a + [k - a[n - 1]]
for i, d in enumerate(a):
    if i != 0:
        a[i] = d - a[i - 1]

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# coding: utf-8
# import sys
# import pprint
# import numpy as np
# import code
import functools

if __debug__:
    debug = lambda *x: None
else:
    import logging
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    debug = logger.debug

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

debug(k)
debug(n)
debug(a)

aa = a.copy()
aa.pop(0)
aa.append(k + a[0])
debug(aa)
# r = functools.reduce(lambda )

d = [y - x for x, y in zip(a,aa)]
debug(d)
d.sort()
debug(d)
d.pop()
debug(d)
debug(sum(d))

Submission status: Wrong Answer

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

dist = []
for i in range(N):
	dist.append(abs(A[i] - A[i-1]))
dist.append(K + A[0] - A[-1])

print(K - max(dist))

Submission status: Wrong Answer

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

if B > C:
    print(C)
else:
    print(B)

Submission status: Wrong Answer

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

for i, d in enumerate(a):
    if i != 0:
        a[i] = d - a[i - 1]

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split()))
kankaku = [(a[i] - a[i-1]) % 20 for i in range(len(a))]
print(k-max(kankaku))