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:
# maintain overall min 
# iterate over two loops 
# find the absolute distance and before exiting the first maintain overall min
# also check the distance of last and first house
def findDistBetween(i,j):
    return abs(i - j)

def minDistfindDistBetweenLastAndFirstHouse(i,j):
    dist = findDistBetween(i, j)
    return K - dist 

K, N = input().split()
As = input().split()

K = int(K)
N = int(N)

print ("As is")
print (As)
# print (As.split())
A = [0] * len(As)
for i in range(len(As)):
    A[i] = int(As[i])
    
# A = list(map(lambda x: int(x), As))
# print (A)
# overAllMin = float('inf')
# minDist = 0 
# for i in range(N):
#     minDist = 0
#     for j in range(N):
#         # print (i, j)
#         minDist += findDistBetween(A[i], A[j])
#     # find the dist b/w last and first house
#     # minDist2 = minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
#     # minDist = min(minDist, minDist2)
#     print ("Min dist", minDist)
#     # print ("min dist2", minDist2)
#     overAllMin = min(overAllMin, minDist)

# print (overAllMin)
overAllMin = float('inf')
Arev = list(reversed(A))
dist1 = 0 
dist2 = 0
prevHouse = -1
for i in range(N):
    print ("PRev house", prevHouse)
    print ("Next house", A[i])

    dist1 = max(findDistBetween(A[i], prevHouse), dist1)
    prevHouse = A[i]
    

ma = max(dist1, minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1]))
print (K - ma)
# for i in range(N):
#     count = 0
#     curHouse = A[i]
#     j = i
#     flag0 = 0 
#     flag1 = 0
#     dist1 = 0 
#     dist2 = 0
#     while count < N:
#         if flag0 is 0:
#             dist1 += findDistBetween(curHouse, A[j])
#         curHouse = A[j]
#         j += 1
#         flag0 = 0
#         count += 1

#         if j == N and count < N: 
#             print ("in condition")
#             j = 0
#             dist1 += minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
#             flag0 = 1
        
        # count += 1

        # print ("dist1", dist1)

    # count = 0
    # curHouse = A[i]
    # j = i
    # while count < N:
    #     if flag1 is 0:
    #         dist2 += findDistBetween(curHouse, A[j])
    #     curHouse = A[j]
    #     j -= 1
    #     count += 1
    #     flag1 = 0
    #     if j == -1 and count < N: 
    #         j = N - 1 
    #         dist2 += minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
    #         flag1 = 1
    #     # count += 1 
# overAllMin = min(dist1, K - findDistBetween(A[0], A[-1]), overAllMin)
# print (dist1,  K - findDistBetween(A[0], A[-1]), overAllMin)
        
# print (overAllMin)

Submission status: Wrong Answer

Solution code:
k,n=map(int,input().split())
A=sorted(list(map(int,input().split())))
n1=max(A)-min(A)
for i in range(n):
    if A[i]>=k//2:
        A[i]-=k
n2=max(A)-min(A)
print(min(n1,n2))

Submission status: Wrong Answer

Solution code:
# maintain overall min 
# iterate over two loops 
# find the absolute distance and before exiting the first maintain overall min
# also check the distance of last and first house
def findDistBetween(i,j):
    return abs(i - j)

def minDistfindDistBetweenLastAndFirstHouse(i,j):
    dist = findDistBetween(i, j)
    return K - dist 

K, N = input().split()
As = input().split()

K = int(K)
N = int(N)

print ("As is")
print (As)
# print (As.split())
A = [0] * len(As)
for i in range(len(As)):
    A[i] = int(As[i])
    
# A = list(map(lambda x: int(x), As))
# print (A)
# overAllMin = float('inf')
# minDist = 0 
# for i in range(N):
#     minDist = 0
#     for j in range(N):
#         # print (i, j)
#         minDist += findDistBetween(A[i], A[j])
#     # find the dist b/w last and first house
#     # minDist2 = minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
#     # minDist = min(minDist, minDist2)
#     print ("Min dist", minDist)
#     # print ("min dist2", minDist2)
#     overAllMin = min(overAllMin, minDist)

# print (overAllMin)
overAllMin = float('inf')
Arev = list(reversed(A))
dist1 = 0 
dist2 = 0 
for i in range(N):
    count = 0
    curHouse = A[i]
    j = i
    flag0 = 0 
    flag1 = 0
    dist1 = 0 
    dist2 = 0
    while count < N:
        if flag0 is 0:
            dist1 += findDistBetween(curHouse, A[j])
        curHouse = A[j]
        j += 1
        flag0 = 0
        count += 1

        if j == N and count < N: 
            # print ("in condition")
            j = 0
            dist1 += minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
            flag0 = 1
        
        # count += 1

        # print ("dist1", dist1)

    count = 0
    curHouse = A[i]
    j = i
    while count < N:
        if flag1 is 0:
            dist2 += findDistBetween(curHouse, A[j])
        curHouse = A[j]
        j -= 1
        count += 1
        flag1 = 0
        if j == -1 and count < N: 
            j = N - 1 
            dist2 += minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1])
            flag1 = 1
        # count += 1 
    # print (dist1, dist2, overAllMin)
    overAllMin = min(dist1, dist2, overAllMin)
        
print (overAllMin)

Submission status: Wrong Answer

Solution code:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k + 2)

for i in range(k - 1):
    distance[1+i] = array[i+1] - array[i]

distance[k] = n-array[k-1] + array[0]
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k + 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Submission status: Wrong Answer

Solution code:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k + 2)

for i in range(k - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = n-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k + 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Submission status: Wrong Answer

Solution code:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k + 2)

for i in range(k - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = 20-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k + 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Submission status: Wrong Answer

Solution code:
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k + 2)

for i in range(k - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = 20-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k + 1):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
List=[]
for i in range(N):
  b=abs(A[i]-A[i-1])
  List.append(b)
SL=sorted(List)
print(K-SL[-1])

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
List=[]
for i in range(N-1):
  b=A[i+1]-A[i]
  List.append(b)
SL=sorted(List)
print(K-SL[-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 1e9
for i in range(N):
    ans = min(ans, min(A[(i+N-1)%N] + K - A[i], A[i] + K - A[(i+1)%N]))

print(ans)

Submission status: Wrong Answer

Solution code:
KN=list(map(int,input().strip().split()))
K=KN[0]
N=KN[1]
A_list=list(map(int,input().strip().split()))

Dis_list=[A_list[n]-A_list[n-1] for n in range(1,N)]
Dis_list.append(K-(A_list[N-1]-A_list[0]))

min_distance=K-max(Dis_list)

print(Dis_list)
print(min_distance)

Submission status: Wrong Answer

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

ans = 0
for i in range(len(An)-1):
    ans = max(ans, An[i+1]-An[i])


ans = max(ans, K - An[-1])

print(K-ans)

Submission status: Wrong Answer

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

RL = []

for i in range(N):
  if AL[i] == 0 or AL[i]<(K/2):
    RL.append(AL[i])
  else:
    RL.append(AL[i] - K)


L = []
l = 0

for i in range(N):
  for j in range(N):
    l = l + abs(RL[j] - RL[i])
  L.append(l)
  l = 0

print(min(L))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

def cal(An):
    if len(An) == 1:
        print(An[0])
    else:
        Ank = [0] + An[:] + [k]
    
        node = [Ank[i+1] - Ank[i]  for i in range(len(Ank) - 1)]
        print(sum(node) - max(node))
        
cal(An)

Submission status: Wrong Answer

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

As = map(int, raw_input().split())

a = N
for i in range(len(As)):
  start = As[i]
  stop = As[i-1]
  a_t = start-stop
  if a_t <= 0: a_t += N
  if a_t < a: a = a_t

print a

Submission status: Wrong Answer

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

half = k/2
ans = 0
for i in range(n):
  if A[i] >= half:
    ans += A[i] - half
  else:
    ans += A[i]
print(int(ans))

Submission status: Wrong Answer

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

half = k/2
ans = 0
for i in range(n):
  if A[i] >= half:
    ans += A[i] - half
  else:
    ans += A[i]
print(ans)

Submission status: Wrong Answer

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

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

Ank = [0] + An[:] + [k]

node = [Ank[i+1] - Ank[i]  for i in range(len(Ank) - 1)]

print(sum(node) - max(node))

Submission status: Wrong Answer

Solution code:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
L, k = map( int, input().split() )
a = list( map( int, input().split() ))
a[-1] = a[-1] - L

ret = L
for i in range( len(a) ):
  ret = min( ret, L - abs(a[i] - a[i-1]))
print( ret )

Submission status: Wrong Answer

Solution code:
import sys


def input() -> str:
    return sys.stdin.readline().rstrip("\r\n")


def main():
    k, n = map(int, input().split())
    houses = [int(s) for s in input().split()]
    houses = [k-houses[-1]] + houses
    distance = 0
    for i in range(1, n+1):
        distance = max(distance, houses[i] - houses[i-1])
    print(k - distance)


if __name__ == "__main__":

    main()

Submission status: Wrong Answer

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

ftlr = a[-1] - a[0]
ftll = k - (a[1] - a[0])
ltfr = k - (a[-1] - a[-2])
ltfl = a[-1] - a[0]
print(min(ftlr,ftll,ltfr,ltfl))

Submission status: Wrong Answer

Solution code:
import sys


def input() -> str:
    return sys.stdin.readline().rstrip("\r\n")


def main():
    k, n = map(int, input().split())
    houses = [int(s) for s in input().split()]
    distance = 0
    for i in range(n):
        distance = max(distance, houses[i] - houses[i-1])
    print(k - distance)


if __name__ == "__main__":

    main()

Submission status: Wrong Answer

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

K = int(K);
N = int(N);
result = 0;

if(A[0] == 0):
    result = A[2] - A[1];

else:
    result = A[2] - A[0];

print(result);

Submission status: Wrong Answer

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


def main():
    import sys
    input = sys.stdin.readline

    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    ans = a[-1] - a[0]

    for i in range(n):
        if (i + i) < n:
            dist = a[i] + (k - a[i + 1])
            ans = min(ans, dist)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
k,n = (int(x) for x in input().split())
a = [int(i) for i in input().split()]
if a[2]-a[0] < 20-a[1]+a[0]:
    print(a[2]-a[0])
else:
    print(20-a[2]+a[1])

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=l[len(l)-1]-l[0]
for j in range(len(l)):
	if l[j]>=K/2:
		l[j]-=K
l.sort()
d2=l[len(l)-1]-l[0]
print(min(d1,d2))

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=l[len(l)-1]-l[0]
for j in range(len(l)):
	if l[j]>K/2:
		l[j]-=K
l.sort()
d2=l[len(l)-1]-l[0]
print(min(d1,d2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
l = [int(x) for x in input().split()]
l = [x for x in l if x > 0]
l.sort()

print(min(l[0] + k - l[-1], l[-1] - l[0]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def start(k, n, a):
    max_distance=0
    for i in range(n-1):
        if(a[i+1]-a[0]>max_distance):
            max_distance = a[i+1]-a[i]
    if(k-a[-1]+a[0]>max_distance):
        max_distance = k-a[-1]+a[0]
    print(k-max_distance)

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

Submission status: Wrong Answer

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

for i in range(1, N-1):
    kyori.append(abs(A[i] - A[i - 1]))
lastR  = abs(A[N-1] - A[N - 2])
last = abs(K - A[N-1])
kyori.append(min(last, lastR))

print(sum(kyori))

Submission status: Wrong Answer

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

ans = 0
max_dis = 0

for i in range(n):
    dis = a[i+1] - a[i]
    max_dis = max(max_dis,dis)
    ans += dis
    
ans -= max_dis
print(ans)

Submission status: Wrong Answer

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

if N <= 1:
  print(0)
 
max_distance = 0
max_distance_idx = 0
for i in range(0, N-1):
  dist = A_list[i+1]-A_list[i]
  if max_distance < dist:
    max_distance = dist
    max_distance_idx = i
 
if max_distance <= (K-A_list[N-1]) + (A_list[0]):
  print(max_distance)
else:
  print((A_list[max_distance_idx]) + (K-A_list[max_distance_idx+1]))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
if(l[0]==0):
  print(max(l)-l[1])
else:
  print(max(l)-l[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
len = []
a = [int(x) for x in input().split()]
for i in range(N):
  if i == N-1:
    if a[0] == 0:
      len.append(K - a[i])
    else:
      len.append(a[i] - a[0])
  else:
    len.append(a[i+1] - a[i])
len.sort(reverse=True)
print(K-len[0])

Submission status: Wrong Answer

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

max_distance = 0
max_distance_idx = 0
for i in range(0, N-1):
  dist = A_list[i+1]-A_list[i]
  if max_distance < dist:
    max_distance = dist
    max_distance_idx = i

if max_distance <= (K-A_list[N-1]) + (A_list[0]):
  print(max_distance)
else:
  print((A_list[max_distance_idx]) + (K-A_list[max_distance_idx+1]))

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(20-A[N-1]+A[0])
print(K-max(d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial,hypot
#import sys
#from fractions import gcd
setrecursionlimit(10**6+1)
input=stdin.readline

k,n=map(int,input().split())
a=[int(i) for i in input().split()]
absdis=[]
for i in range(n):
    absdis.append(min(abs(a[i]-a[i-1]),20-abs(a[i]-a[i-1])))
#print(absdis)
print(sum(absdis)-max(absdis))

Submission status: Wrong Answer

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial,hypot
#import sys
#from fractions import gcd
setrecursionlimit(10**6+1)
input=stdin.readline

k,n=map(int,input().split())
a=[int(i) for i in input().split()]
absdis=[min(abs(a[0]-a[-1]),20-abs(a[0]-a[-1]))]
for i in range(1,n):
    absdis.append(abs(a[i]-a[i-1]))
#print(absdis)
print(sum(absdis)-max(absdis))

Submission status: Wrong Answer

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial,hypot
#import sys
#from fractions import gcd
setrecursionlimit(10**6+1)
input=stdin.readline

k,n=map(int,input().split())
a=[int(i) for i in input().split()]
absdis=[min(abs(a[0]-a[-1]),abs(20-a[0]-a[-1]))]
for i in range(1,n):
    absdis.append(abs(a[i]-a[i-1]))
#print(absdis)
print(sum(absdis)-max(absdis))

Submission status: Wrong Answer

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial,hypot
#import sys
#from fractions import gcd
setrecursionlimit(10**6+1)
input=stdin.readline

k,n=map(int,input().split())
a=[int(i) for i in input().split()]
absdis=[]
for i in range(n):
    absdis.append(abs(a[i]-a[i-1]))
print(sum(absdis)-max(absdis))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0]+K)
maxD = 0
for n in range(N):
  maxD = min(maxD, A[n+1] - A[n])
print(K-maxD)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
val_index = 0
while val_index < len(A):
    if A[val_index] == 0:
        del A[val_index]
        val_index -= 1
    val_index += 1
print(int(sum(A)/len(A)))

Submission status: Wrong Answer

Solution code:
#C traveling salesman around lake

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

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

last = a[0] + k - a[n-1]
if last > ans:
    ans = last

print(k-ans)

Submission status: Wrong Answer

Solution code:
k,n,*a=map(int,open(0).read().split())
a+=[k+a[0]]
print(min(y-x for x,y in zip(a,a[1:])))

Submission status: Wrong Answer

Solution code:
inputText1 = input()
# inputText1 = "20 3"
inputText1List = inputText1.split()
inputNumber1to1 = int(inputText1List[0]) # 20
inputNumber1to2 = int(inputText1List[1]) # 3
inputText2 = input()
# inputText2 = "5 10 15"
inputText2List = inputText2.split()
inputNumber2List = [int(s) for s in inputText2List]
# print(inputNumber2List) #[5, 10, 15]
checkDistance = 0
newList = []
for i in (inputNumber2List):
    if i != 0:
        newList.append(i - inputNumber1to1)

newList.extend(inputNumber2List)

# print(newList) # [-15, -10, -5, 5, 10, 15]
i = 0
count = inputNumber1to2 - 1 # 2(2個っていみ)
print(count,'count')
breakFlg = False
while True:
    distance = 0
    if i + count >= len(newList):
        break
    for j in range(count):
        distance += abs(newList[i + j] - newList[i +j + 1])
        # print(i,j,'i j',distance)
    if checkDistance == 0:
        # print(checkDistance,'checkDistance')
        checkDistance = distance
    else:
        # print(checkDistance,distance,'redistance')
        if checkDistance > distance:
            checkDistance = distance
            # print("haittyo")
    i += 1


print(checkDistance)

Submission status: Wrong Answer

Solution code:
# inputText1 = input()
inputText1 = "20 3"
inputText1List = inputText1.split()
inputNumber1to1 = int(inputText1List[0]) # 20
inputNumber1to2 = int(inputText1List[1]) # 3
# inputText2 = input()
inputText2 = "5 10 15"
inputText2List = inputText2.split()
inputNumber2List = [int(s) for s in inputText2List]
# print(inputNumber2List) #[5, 10, 15]
checkDistance = 0
newList = []
for i in (inputNumber2List):
    if i != 0:
        newList.append(i - inputNumber1to1)

newList.extend(inputNumber2List)

# print(newList) # [-15, -10, -5, 5, 10, 15]
i = 0
count = inputNumber1to2 - 1 # 2(2個っていみ)
print(count,'count')
breakFlg = False
while True:
    distance = 0
    if i + count >= len(newList):
        break
    for j in range(count):
        distance += abs(newList[i + j] - newList[i +j + 1])
        # print(i,j,'i j',distance)
    if checkDistance == 0:
        # print(checkDistance,'checkDistance')
        checkDistance = distance
    else:
        # print(checkDistance,distance,'redistance')
        if checkDistance > distance:
            checkDistance = distance
            # print("haittyo")
    i += 1


print(checkDistance)

Submission status: Wrong Answer

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

for i in range(N - 1):
  if i == N - 1:
    dist = A[0] + K - A[i]
  else:
    dist = A[i+1] - A[i]
  
  if dist > max_dist:
    max_dist = dist
    
print(K - max_dist)

Submission status: Wrong Answer

Solution code:
# 隣り合う長さの最大値をKから引く
circ_length, n_house = map(int, input().split())
places = list(map(int, input().split()))

delta_places = []
for i in range(n_house):
  d = abs(places[i] - places[i-1])
  delta_places.append(d)

result = circ_length - max(delta_places)
print(result)

Submission status: Wrong Answer

Solution code:
#距離順に並べて、隣り合う長さの最大値をKから引く
circ_length, n_house = map(int, input().split())
places_in_order = sorted(list(map(int, input().split())))

delta_places = []
for i in range(n_house):
  d = abs(places_in_order[i] - places_in_order[i-1])
  delta_places.append(d)

result = circ_length - max(delta_places)
print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
readline = sys.stdin.buffer.readline
K, N = map(int, readline().split())
A = list(map(int,input().split()))
N1= A[0]+K-A[N-1]

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

c = min(K-N1,K-N)
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
readline = sys.stdin.buffer.readline
K, N = map(int, readline().split())
A = list(map(int,input().split()))
N1= A[0]+K-A[N-1]

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

print(N,N1)
c = min(K-N1,K-N)
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
readline = sys.stdin.buffer.readline
K, N = map(int, readline().split())
A = list(map(int,input().split()))
N1= A[0]+K-A[N-1]

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

c = min(K-N1,K-N)
print(c)

Submission status: Wrong Answer

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

m=max(A[i+1]-A[i] for i in range(N-1))
print(A[N-1]-A[0] if K>2*m else K-m)

Submission status: Wrong Answer

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

print(k-max_dis)

Submission status: Wrong Answer

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

print(k-max_dis)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().strip().split())
A = list(map(int, input().strip().split()))

# min_=10**6
dA=np.diff(A)
np.append(dA,A[0]+K-A[-1])
skip_dist=np.max(dA)
ans=K-skip_dist

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
K, N = map(int, input().strip().split())
A = list(map(int, input().strip().split()))

# min_=10**6
dA=np.diff(A)
np.append(dA,A[0]+K-A[-1])
skip_dist=max(dA)
ans=K-skip_dist

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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