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:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    K, N = MI()
    A = LI()
    A.append(K)
    N = N + 1
    diff = -1
    target = -1

    for i in range(N-1):
        v = abs(A[i] - A[i+1])

        if diff < v:
            diff = v
            target = i

    a = 0
    for i in range(target+1, N-1):
        a += abs(A[i] - A[i+1])
        # print(i, i + 1)

    for i in range(0, target):
        a += abs(A[i] - A[i+1])
        # print(i, i + 1)

    b = 0
    for i in range(target, 0, -1):
        b += abs(A[i] - A[i-1])
        # print(i, i - 1)

    for i in range(N-1, target+1, -1):
        b += abs(A[i] - A[i-1])
        # print(i, i - 1)

    # print(a, b)
    print(min(a, b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
k,n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.append(k)
ans = 0
tmp = 0
for i in range(1,n+1):
    dif = a[i] - a[i-1]
    ans += dif
    tmp = max(tmp, dif)

print(ans - tmp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
if n == 2:
    a1, a2 = A
    print(min(a2 - a1, k + a1 - a2))
A[-1] -= k
maximum = 0
for i in range(n):
    x = A[i] - A[i-1]
    if x > maximum:
        maximum = x
print(k - maximum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(len(a)):
    if i == len(a) - 1:
        kyori.append(k - a[i])
    else:
        kyori.append(a[i+1] - a[i])
    
print(sum(kyori)-max(kyori))

Submission status: Wrong Answer

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

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

tosa.sort()
result = sum(tosa[:n-1])
print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = [int(i) for i in input().split()]
a = np.array([int(i) for i in input().split()])

b = k-np.max(a)

print(np.max(a-b))

Submission status: Wrong Answer

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

if l[0]==0:
    l.pop(0)
    n-=1

a=l[n-1]-l[0]
b=k-a

print(min(a,b))

Submission status: Wrong Answer

Solution code:
import sys
import os
from io import BytesIO, IOBase

#########################
# imgur.com/Pkt7iIf.png #
#########################

# returns the list of prime numbers less than or equal to n:
'''def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r'''

# returns all the divisors of a number n(takes an additional parameter start):
'''def divs(n, start=1):
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors'''

# returns the number of factors of a given number if a primes list is given:
'''def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
    return divs_number'''

# returns the leftmost and rightmost positions of x in a given list d(if x isnot present then returns (-1,-1)):
'''def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)'''

# returns (b**p)%m 
'''def modpow(b,p,m):
    res=1
    b=b%m
    while(p):
        if p&1:
            res=(res*b)%m
        b=(b*b)%m
        p>>=1
    return res'''

# if m is a prime this can be used to determine (1//a)%m or modular multiplicative inverse of a number a
'''def mod_inv(a,m):
    return modpow(a,m-2,m)'''

# returns the ncr%m for (if m is a prime number) for very large n and r
'''def ncr(n,r,m):
    res=1
    if r==0:
        return 1
    if n-r<r:
        r=n-r
    p,k=1,1
    while(r):
        res=((res%m)*(((n%m)*mod_inv(r,m))%m))%m
        n-=1
        r-=1
    return res'''

# returns ncr%m (if m is a prime number and there should be a list fact which stores the factorial values upto n):
'''def ncrlis(n,r,m,fact):
    return (fact[n]*(mod_inv(fact[r],m)*mod_inv(fact[n-r],m))%m)%m'''

#count xor of numbers from 1 to n:
def xor1_n(n):
    d={0:n,1:1,2:n+1,3:0}
    return d[n&3]

def ceil(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def ddd(): return collections.defaultdict(collections.defaultdict(int))
 

# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
			while True:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					if not b:
							break
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines = 0
			return self.buffer.read()
 
	def readline(self):
			while self.newlines == 0:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					self.newlines = b.count(b"\n") + (not b)
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines -= 1
			return self.buffer.readline()
 
	def flush(self):
			if self.writable:
					os.write(self._fd, self.buffer.getvalue())
					self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
			self.buffer = FastIO(file)
			self.flush = self.buffer.flush
			self.writable = self.buffer.writable
			self.write = lambda s: self.buffer.write(s.encode("ascii"))
			self.read = lambda: self.buffer.read().decode("ascii")
			self.readline = lambda: self.buffer.readline().decode("ascii")
 

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#import collections
#from collections import deque
#import math
#import time
#import itertools
#import timeit
#import random
#from bisect import bisect_left as bl
#from bisect import bisect_right as br
#from bisect import insort_left as il
#from collections import OrderedDict

for _ in range(1):
    k,n=mi()
    a=li()
    mx=k-a[-1]
    res=k-a[-1]
    for i in range(n-1):
        res+=a[i+1]-a[i]
        mx=max(mx,a[i+1]-a[i])
    print(res-mx)

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
arr = list(map(int, input().split()))
diff = [0] * n


for i in range(1, n):
  diff[i] = arr[i] - arr[i-1]
diff[0] = k - arr[-1]

print(np.sum(diff) - max(diff))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    k, n = na()
    mid = k // 2
    ans = 0
    for num in na():
        if num > mid:
            num = k - num
        elif num == mid:
            num = 0
        ans += num
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
import itertools
import math

k,n=map(int,input().split())
a=list(map(int,input().split()))
sum=0
max_root=0
tmp = k-(a[-1]-a[0])
max_root=tmp
sum+=tmp
for i in range(len(a)-1):
    tmp = a[i+1]-a[i]
    if tmp > max_root:
        tmp=max_root
    sum+=tmp

print(sum-max_root)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split())) + [k]

    maxi = 0
    ind = 0
    for i in range(n):
        if maxi < a[i+1] - a[i]:
            maxi = a[i+1] - a[i]
            ind = i

    for i in range(ind+1, n):
        yield a[i+1] - a[i]

    for i in range(0, ind):
        yield a[i+1] - a[i]


print(sum(main()))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import tee


def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

if A[0] == 0:
    end = K
else:
    end = A[0]
max_length = max([abs(p - q) for p, q in pairwise(A + [end])])

print(K-max_length)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

for i in range(N-1):
  m = min(m, A[i+1] - A[i])

print(K - m)

Submission status: Wrong Answer

Solution code:
k,n = map(int,input().split())
al = input().split()
al2 = [int(i) for i in al]
distance = []
for i in range(len(al)-1):
    distance.append(al2[i+1]-al2[i])
distance.append(k-al2[-1])
distance.remove(max(distance))
print(sum(distance))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K - A[N-1]
	A[1:] = A[1:]-A[:N-1]
	np.sort(A)
	print(sum(A)+A_LAST-max(A[N-1],A_LAST))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

m = 10 ** 12
for i in range(1,len(a)-1):
    r = k - abs(a[i] - a[i-1])
    l = k - abs(a[i] - a[i+1])
    print(r,l)
    if min(r,l) <= m:
        m = min(r,l)

r = abs(a[-1] - a[0])
l = k - abs(a[1] - a[0])
if min(r,l) <= m:
    m = min(r,l)

r = k - abs(a[-1] - a[-2])
l = abs(a[-1] - a[0])
if min(r,l) <= m:
    m = min(r,l)
    
print(m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
diff_A = []
for n in range(N-1):
    diff_A.append(A[n+1] - A[n])
diff_A.append(K - A[-1])
print(sum(diff_A)-max(diff_A))

Submission status: Wrong Answer

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

distance = [0]*n
a.append(k)

for i in range(n):
  distance[i] = a[i+1] - a[i]

distance.sort(reverse=True)

print(sum(distance[1:]))

Submission status: Wrong Answer

Solution code:
import sys
import math
import numpy

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

sorted(A)

total = 0

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

total2 = 0
for i in range(1,N):
    if i == 1 :
        total2 += K - A[N-1] + A[0]
    else:
        total2 += A[i-1] - A[i-2]

print(min(total,total2))

Submission status: Wrong Answer

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

li_diff = []
for i in range(len(li) - 1):
    li_diff.append(li[i + 1] - li[i])
li_diff.append(k - li[len(li) - 1])

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

Submission status: Wrong Answer

Solution code:
import math

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

path = 0
lastPosition = 0

for i in range(KN[1]):
    if (i == 0):
        path = A[0]
        lastPosition = 0
    else:
        currentPosition = A[i]
        if (path < currentPosition - lastPosition):
            path = currentPosition - lastPosition
        lastPosition = currentPosition

print(KN[0] - path)

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a = np.array(list(map(int, input().split())) + [k])

diff = np.diff(a)
print(np.sum(diff) - np.max(diff))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

k, n = map(int, input().split())
a_list = [int(i) for i in input().split()]
a_list += [a_list[0]+k]

a_list = np.array(a_list)

dif = a_list[1:] - a_list[:-1]
dif_max = np.max(dif)

print(dif_max)

Submission status: Wrong Answer

Solution code:
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
Ar = [i for i in A]
for i,a in enumerate(A):
    if K - a < a:
        Ar[i] = a - K
print(min(max(A) - min(A), max(Ar) - min(Ar)))

Submission status: Wrong Answer

Solution code:
def main():
    k,n = tuple([int(t)for t in input().split()])
    a = [int(t)for t in input().split()]

    l = 0
    dist = []
    for a_i in a[1:]:
        dist.append(a_i-l)
        l = a_i
    dist.append(k-a[-1]+a[0])
    print(dist)
    print(k-max(dist))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

k,n = miinps()
a = lmiinps()

l = -1

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

if a[0] == 0:
    l = max(l,k-a[n-1])
else:
    l = max(l,a[n-1]-a[0])

print(k-l)

Submission status: Wrong Answer

Solution code:
def main():
    K,N = map(int,input().split())
    A = list(map(int,input().split()))
    Aa = []
    for i in range(1,N):
        Aa.append(A[i]-A[i-1])
    Aa.append(K-A[N-1])
    print(sum(Aa)-max(Aa))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

a.append(k + a[0])

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

print(a[-2] - min(b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
if s[2] == s[3] and s[4] == s[5]:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
import numpy as np

k,n = mi()
a=np.array(li())

b = a[1:] - a[:n-1]
b_max = np.max(b)
c = a[0] + k-a[n-1]

print(max(b_max,c))

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for a in A:
    if a > K // 2:
        ans += a - K // 2
    else:
        ans += a

print(ans)

Submission status: Wrong Answer

Solution code:
K,N = map(int,input().split())
A = list(map(int,input().split()))
X = 0
Y = 0
for i in range(N):
    if A[i] == 0:
        A[i] = K
A.sort()
b = A.copy()
for i in range(N-1):
    #時計回り
    X +=  b[i+1]-b[i]
c = A.copy()
c.reverse()
for j in range(N-1):
    Y += A[i+1]-A[i]
print(min(X,Y))

Submission status: Wrong Answer

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

max_i = 0
max_a = 0
for i in range(len(A)-1):
    a = A[i+1] - A[i]
    if max_a < a:
        max_a = a
        max_i = i
        
ans = A[-1] - A[max_i]
print ans

Submission status: Wrong Answer

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

mini, maxi = min(A), max(A)
A_ = []
for a in A:
    if a != maxi:
        A_.append(a + K)
    else:
        A_.append(a)

mini2, maxi2 = min(A_), max(A_)

ans = min(maxi - mini, maxi2 - mini2)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

mi = min(A)
mx = max(A)
ret = mx-mi
for i in range(N):
    if A[i] > abs(A[i]-K):
        A[i] -= K

mi = min(A)
mx = max(A)
ret = min(ret, mx-mi)
print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = min(A[N - 1] - A[0], K - A[N - 1] + A[N - 2])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# int(input()) # 入力が1つ
k, n = map(int, input().split()) # 入力が複数
houses = list(input().split())
houses = [int(h) for h in houses]
houses.sort()
houses.append(k)
max_dist = 0
total = 0
for i in range(n):
    d = houses[i + 1] - houses[i]
    max_dist = max(max_dist, d)
    total += d

ans = total - max_dist
print(ans)

Submission status: Wrong Answer

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

B = [A[i+1] - A[i] for i in range(len(A)-1)]
B.append(K-A[-1] + A[0])

print(B)
print(K - max(B))

Submission status: Wrong Answer

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

main()

Submission status: Wrong Answer

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

kyori = 0
if A[0] == 0:
    kyori = k-A[-1]
else:
    kyori = A[-1]-A[0]

for i in range(n-1):
    kyori = max(kyori, A[i+1]-A[i])
print(k-kyori)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    k,n = map(int,input().split())
    arr = list(map(int,input().split()))
    ans = float('inf')
    prev = 0
    for i in range(n):
        if i == n-1:
            if prev == 0:
                total = arr[-1]-arr[i]
            else:
                total = k-arr[i]+prev
        else:
            if prev == 0:
                total = arr[-1]-arr[i]
            else:
                total = min(k-arr[i]+prev,arr[i]+k-arr[i+1])

        prev += arr[i]
            
        ans = min(ans,total)

    print(ans)
                            
main()

Submission status: Wrong Answer

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

a.sort()
ans = a[n-1]-a[0]
for i in range(n):
    tmp = a[i-1]-a[i]
    tmp%=20
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

ans = 0

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

ans2 = ans
ans2=ans2-2*a[n-1]+a[n-2]+a[0]+k
print(min(ans,ans2))

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):
	cur_dist = a[i+1] - a[i]
	if cur_dist > max_dist:
		max_dist = cur_dist

cur_dist = 20 + a[0] - a[-1]
if cur_dist > max_dist:
	max_dist = cur_dist

print(k - max_dist)

Submission status: Wrong Answer

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

tmp1 = A[-1] - A[0]
tmp2 = A[-1] - (20 - A[0] - A[-1])

print(min(tmp1, tmp2))
# print(A[-1]-A[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

k,n = mi()
a = li()

b = 0
tmp = 0
am = a[0]
for i in range(1,n):
    tmp = a[i] -a[i-1]
    am = max(am,tmp)
am = max(am,k -  a[-1])

print(k-am)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(sum(dis) - max(dis))

Submission status: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): #startline-------------------------------------------
    k,n = map(int,input().split())
    a = list(map(int,input().split()))
    anslist = []
    a.append(k)
    for i in range(n):
        anslist.append(abs(a[i+1]-a[i]))
    print(sum(anslist)-max(anslist))
if __name__ == "__main__":
    main() #endline===============================================

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-1):
     b[i] = a[i+1] - a[i]
b[n-1] = k - a[n-1]
print(k-max(b))

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
k,n = map(int,stdin.readline().rstrip().split())
a = list(map(int,stdin.readline().rstrip().split()))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

a.sort()
print(a)
print(sum(a[:-1]))

Submission status: Wrong Answer

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

c.sort(reverse=True)

print(sum(c[1:]))

Submission status: Wrong Answer

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

max_l = k-a[-1]+a[0]


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



print(max_l)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
A = list(map(int, input().split()))
A[-1] -= k
maximum = 0
for i in range(n):
    x = A[i] - A[i-1]
    if x > maximum:
        maximum = x
print(k - maximum)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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