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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

dist = []
for i in range(N):
  dist.append(l[i-1]-l[i])

m = min(dist)

print(K-m)

Submission status: Wrong Answer

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

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

print(K-max(dist))

Submission status: Wrong Answer

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

dist = []
for i in range(0, len(A)-1):
    dist.append(A[i+1]-A[i])
dist.append(A[len(A)-1]-A[0])
print(dist)

print(K-max(dist))

Submission status: Wrong Answer

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

a_l.sort()
print(a_l[-1]-a_l[0])

Submission status: Wrong Answer

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

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

print(d)
print(sum(d) - max(d))

Submission status: Wrong Answer

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

max_d = 0
for i in range(0, N-1):
    if A[i] == 0:
        pass
    else:
        d = abs(A[i] - A[len(A)-1])
        max_d = max(d, max_d)

print(K - max_d)

Submission status: Wrong Answer

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

max_d = 0
for i in range(0, N-1):
    if A[i] == 0:
        pass
    else:
        d = abs(A[i] - A[len(A)-1])
        max_d = max(d, max_d)

print(max_d)

Submission status: Wrong Answer

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

dis = []

for i in range(len(A)):
    if i+1 == len(A):
        break
    elif abs(A[i-1] - A[i]) > abs(A[i+1] - A[i]):
        dis.append(abs(A[i+1] - A[i]))
    elif abs(A[i-1] - A[i]) == abs(A[i+1] - A[i]):
        dis.append(abs(A[i-1] - A[i]))
    else:
        dis.append(abs(A[i-1] - A[i]))

        
print(sum(dis))

Submission status: Wrong Answer

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

if A[0] == 0:
    d = abs(A[1] - A[len(A)-1])
else:
    d = abs(A[0] - A[len(A)-1])

print(d)

Submission status: Wrong Answer

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

d = 0
if A[0] == 0:
    for i in range(1, len(A)-1):
        d = d + A[i+1] - A[i]
else:
    for i in range(0, len(A)-1):
        d = d + A[i+1] - A[i]

print(d)

Submission status: Wrong Answer

Solution code:
m,n=map(int,input().split())
li=list(map(int,input().split()))
M=0
for i in range(n-1):
    if li[i+1]-li[i]>M:
        M=li[i+1]-li[i]
if m-li[-1]>M:
    M=m-li[-1]
print(m-M)

Submission status: Wrong Answer

Solution code:
N, K =map(int, input().split())
ans=N%K
print(min(ans, K-ans))

Submission status: Wrong Answer

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

print(a)

Submission status: Wrong Answer

Solution code:
m,n=map(int,input().split())
li=list(map(int,input().split()))
M=0
for i in range(n-1):
    if li[i+1]-li[i]>M:
        M=li[i+1]-li[i]
if m-li[-1]>M:
    M=m-li[-1]
print(M)

Submission status: Wrong Answer

Solution code:
K,N = (int(x) for x in input().split())
al = [int(x) for x in input().split()]
d = [al[x+1]-al[x] for x in range(0, N-1)]
d.append(K-al[-1]-al[0])
print(sum(d)-max(d))

Submission status: Wrong Answer

Solution code:
K,N = (int(x) for x in input().split())
al = [int(x) for x in input().split()]
d = [al[x+1]-al[x] for x in range(0, N-1)]
d.append(al[-1]-al[0])
print(sum(d)-max(d))

Submission status: Wrong Answer

Solution code:
K,N = (int(x) for x in input().split())
al = [int(x) for x in input().split()]
d = [al[x+1]-al[x] for x in range(0, N-1)]
d.append(al[-1]-al[0])
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()))
b=[]
for i in range(N-1):
  b.append(K-(A[i+1]-A[i]))
  b.append(A[N-1]-A[i])

print(min(b))

Submission status: Wrong Answer

Solution code:
K,N  = map(int,input().split())
Alist = list(map(int,input().split()))
p = 10**8
p = min(max((K-abs(Alist[0]-Alist[N-1])),abs(Alist[0]-Alist[N-1])),max((K-abs(Alist[0]-Alist[1])),abs(Alist[0]-Alist[1])))
for i in range(1,N-1):
    p = min(max((K-abs(Alist[i]-Alist[i-1])),abs(Alist[i]-Alist[i-1])),max((K-abs(Alist[i]-Alist[i+1])),abs(Alist[i]-Alist[i+1])))
p = min(max((K-abs(Alist[N-1]-Alist[N-2])),abs(Alist[N-1]-Alist[N-2])),max((K-abs(Alist[N-1]-Alist[0])),abs(Alist[N-1]-Alist[0])))
print(p)

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):
  if A[i]==0:
    b.append(A[N-1])
  else:
    b.append(K-(A[i+1]-A[i]))

print(min(b))

Submission status: Wrong Answer

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

if A[0] == 0:
    del A[0]

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

Submission status: Wrong Answer

Solution code:
K,N = map(int, input().split())
Homes = list(map(int, input().split()))
nlist = list()
minH = min(Homes)
for i in range(len(Homes)):
    nlist.append(Homes[i] - minH)
nlist.pop(0)

route1 = max(nlist)
route2 = K - min(nlist)
route3 = min(nlist) - (max(nlist) - K)

flist = [route1,route2, route3]

print(min(flist))

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
A = list(map(int, input().split()))
a = A[0]
sub = []
for _ in range(N):
    p = A.pop()
    sub.append(K-p)
    K = p
sub[0] += a
print(sub)
sub.remove(max(sub))
print(sum(sub))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for _ in range(N):
    p = A.pop()
    sub.append(K-p)
    K = p

print(sub)
sub.remove(max(sub))
print(sum(sub))

Submission status: Wrong Answer

Solution code:
K, N = map(int,input().split())
A = list(map(int, input().split()))
sub = []
for _ in range(N):
    p = A.pop()
    sub.append(K-p)
    K = p
sub.remove(max(sub))
print(sum(sub))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import deque, defaultdict, Counter
import math
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    k, n = wi()
    A = wi()
    B = A.copy()
    C = A + B
    # print(C)
    ans = 10**20
    for i in range(n):
        if A[0] != 0:
            if i == 0:
                ans = min(ans, abs(C[i] - C[i+n-1]), k - abs(C[i] - C[i+n-1]))
            else:
                ans = min(ans, k - abs(C[i] - C[i+1]),
                          k - abs(C[i] - C[i+n-1]))

        else:
            if i == 0:
                ans = min(ans, A[n-1] - A[0], k - (A[1] - A[0]))
            else:
                if i == n-1:
                    ans = min(ans, abs(C[i] - C[i+1]),
                              k - abs(C[i] - C[i+n-1]))
                else:
                    ans = min(ans, k-abs(C[i] - C[i+1]),
                              k - abs(C[i] - C[i+n-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import deque, defaultdict, Counter
import math
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    k, n = wi()
    A = wi()
    if A[0] == 0:
        print(min(A[-1] - A[1], k-(A[-1] - A[1])))
    else:
        print(min(A[-1] - A[0], k-(A[-1] - A[0])))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import deque, defaultdict, Counter
import math
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    k, n = wi()
    A = wi()
    if A[0] == 0:
        print(min(abs(A[-1] - A[1]), k-abs(A[-1] - A[1])))
    else:
        print(min(abs(A[-1] - A[0]), k-abs(A[-1] - A[0])))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
A = [0] + A + [K] # len = N+2

diff = []
for i in range(len(A)):
    if i < N+1:
        diff.append(A[i+1] - A[i])

longest_interval = diff.index(max(diff))
diff.pop(longest_interval)
ans = sum(diff)

print(ans)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
A = A + [K] # len = N+1

diff = []
for i in range(len(A)):
    if i < N:
        diff.append(A[i+1] - A[i])

longest_interval = diff.index(max(diff))
diff.pop(longest_interval)
ans = sum(diff)

print(ans)

Submission status: Wrong Answer

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

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

check_l.append(k+a[0]-a[n-1])
print(max(check_l))

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 = a[i+1]-a[i]
    D.append(d)
d1 = 20-a[-1]+a[0]
print(k-max(max(D),d1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
K, N = map(int,input().split())
a = list(map(int,input().split()))
x = []
for i in range(N):
    for u in range(i+1,N):
        if a[i] - a[u] < 10:
            x.append(abs(a[i] - a[u]))
        else:
            x.append(K/2 -abs(a[i] - a[u]))
x.sort()
print(K-x[-1])

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=np.array(a)
a1=abs(a[1:]-a[:-1])
a2=list(a1)
a2.append(abs(a[0]-a[-1])%k)
print(k-max(a1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

tmp = 0

for i in range(1, len(a)):
    if a[i]-a[i-1] >=0:
        tmp = a[i] - a[i-1]

if a[0]+k-a[len(a)-1] > tmp:
    tmp = a[0]+k-a[len(a)-1]

print(k-tmp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
k,n = map(int,input().split())
a = np.array(input().split(),dtype=int)
max = 0
for i in range(n):
    if max < a[i]-a[i-1]:
        max = a[i]-a[i-1]

print(k-max)

Submission status: Wrong Answer

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


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = max(homes[-1] - homes[0], k + homes[0] - homes[-1])
    if max_distance < distance:
        max_distance = distance
    return max_distance


print(k - max_distance_homes(home_positions))

Submission status: Wrong Answer

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


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = max(homes[-1] - homes[0], k + homes[0] - homes[-1])
    if max_distance < distance:
        max_distance = distance
    return max_distance

Submission status: Wrong Answer

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

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(a[0]+k-a[n-1])

print(max(d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

data = input().split()
house = [int(s) for s in data]

h_distance = [house[0]]
for i in range(1, n):
    h_distance.append(house[i] - house[i - 1])

m_distance = max(h_distance)
total_d = 0
for i in h_distance:
    total_d += i

print(total_d - m_distance)

Submission status: Wrong Answer

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

diff = 0
total = 0
for i in range(n-1):
    diff = max(abs(a[i] - a[i+1]), diff)
    total += abs(a[i] - a[i+1])
diff = max(abs(a[n-1] - k), diff)
total += abs(a[n-1] - k) - diff
print(total)

Submission status: Wrong Answer

Solution code:
data = input().split()
k = int(data[0])
n = int(data[1])
 
data = input().split()
house = [int(s) for s in data]
 
ans = min(house[len(house) - 1], k - house[0])
print(ans)

Submission status: Wrong Answer

Solution code:
data = input().split()
k = int(data[0])
n = int(data[1])
 
data = input().split()
house = [int(s) for s in data]
 
ans = k - max(house[0], k - house[len(house)-1])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K, N = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt1 = max(A_arr) - min(A_arr)
    A_arr_South = [_+K/2 if _ < K/2 else _-K/2 for _ in A_arr]
    cnt2 = int(max(A_arr_South) - min(A_arr_South))
    cnt = min(cnt1,cnt2)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K, N = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt1 = max(A_arr) - min(A_arr)
    A_arr_dist = [_-K if K-_<_ else _ for _ in A_arr]
    cnt2 = max(A_arr_dist) - min(A_arr_dist)
    cnt = min(cnt1,cnt2)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Wrong Answer

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

kyori = k + a[0] - a[n-1]

for i in range(n):
    if kyori < a[i] - a[i-1]:
        kyori = a[i] - a[i-1]
print(kyori)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
array = list(map(int,input().split()))
max_length = 0
sum_length = 0
for i in range(n - 1):
  length = array[i + 1] - array[i]
  sum_length += length
  if length > max_length:
    max_length = length
last_length = k - array[n - 1]
sum_length += last_length
max_length = max(max_length, last_length)
print(sum_length - max_length)

Submission status: Wrong Answer

Solution code:
X = input().split()
K = int(X[0])
N = int(X[1])
Y = input().split()
Z = []
for i in range(N):
    Z.append(int(Y[i]))
result = Z[0]+ K - Z[N-1]

for i in range(N):
    if result < Z[i] - Z[i-1]:
        result = Z[i] - Z[i -1]
    i = i + 1
print(result)

Submission status: Wrong Answer

Solution code:
X = input().split()
K = int(X[0])
N = int(X[1])
Y = input().split()
Z = []
for i in range(N):
    Z.append(int(Y[i]))
n1 = Z[N-1] - Z[0]
n2 = K - n1

if n1 < n2:
    print(n1)
else:
    print(n2)

Submission status: Wrong Answer

Solution code:
K, N = map(int, input().split())
lst = [int(x) for x in input().split()]
a = 0
for i in range(N - 1):
    a = max(a, lst[i + 1] - lst[i])
print(K - a - lst[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=list(map(int,input().split()))
A+=[A[0]+K]
max_gap=max(y-x for x,y in zip(A,A[1:]))
print(max_gap)
print(K-max_gap)

Submission status: Wrong Answer

Solution code:
length, num = map(int, input().split())
list1 = [int(i) for i in input().split()]
list2 = []
list2.append(list1[0])
for j in range(1,num):
    list2.append(list1[j] - list1[j-1])
list2.append(length - list1[num-1])
list2.pop(list2.index(max(list2)))
print(sum(list2))

Submission status: Wrong Answer

Solution code:
length, num = map(int, input().split())
list1 = [int(i) for i in input().split()]
list2 = []
for j in range(num-1):
    list2.append(list1[j+1] - list1[j])
list2.append(length - list1[num-1])
list2.pop(list2.index(max(list2)))
print(sum(list2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

dist = []
for i in range(N - 1):
    dist.append(An[i + 1] - An[i])

result = 0
if max(dist) >= (K - An[N - 1]):
    result = sum(dist) + (K - An[N - 1]) - max(dist)
else:
    result = sum(dist)

print(result)

Submission status: Wrong Answer

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

dist = []
for i in range(N - 1):
    dist.append(An[i + 1] - An[i])

result = 0
if max(dist) > (K - An[N - 1]):
    result = sum(dist) + (K - An[N - 1]) - max(dist)
else:
    result = sum(dist)

print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
K,N=map(int,input().split())
A=[int(x) for x in input().split()]
 
m=[]
if N==2:
   if A[1]-A[0]<=K/2:
       ans=A[1]-A[0]
   else:
       ans=K+A[0]-A[1]
    
else:
    for i in range(N-2):
        m.append(A[i+1]-A[i])
    m.append(K+A[0]-A[N-1])
    ans=K-max(m)
print(ans)

Submission status: Wrong Answer

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

for i in range(k-1):
  s=l[i+1]-l[i]
  if s>max:
    max=s

s=n-l[-1]+l[0]
if s>max:
  max=s
print(max)

Submission status: Wrong Answer

Solution code:
k, n = map(int, input().split())
li = sorted(list(map(int, input().split())))
res = 0
if k - li[-1] + li[0] > li[1] - li[0]:
    res += li[1] - li[0]
    end = n - 1
    start = 1
else:
    res += k - li[-1] + li[0]
    end = n - 2
    start = 0

for i in range(start, end):
    res += li[i+1] - li[i]
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

key = k - a[-1]
l = []
ans = 0
for i in range(n-1):
    ans += abs(a[i]-a[i+1])
    l.append(abs(a[i]-a[i+1]))
ans += key
if key >= max(l):
    print(ans-key)
    quit()
else:
    print(ans-max(l))

Submission status: Wrong Answer

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

key = k - a[-1]
l = []
ans = 0
for i in range(n-1):
    ans += abs(a[i]-a[i+1])
    l.append(abs(a[i]-a[i+1]))
ans += key
ans -= max(l)
print(ans)

Submission status: Wrong Answer

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

print(a[n-1]-a[0])

Submission status: Wrong Answer

Solution code:
s = input()
count = 0
j = len(s)
for i in range(j//2):
  if s[i] != s[j-i-1]:
    count += 1 
print(count)

Submission status: Wrong Answer

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

m=[]
if N==2:
   if A[1]-A[0]<=K/2:
       ans=A[1]-A[0]
   else:
       ans=K+A[0]-A[1]
    
else:
    for i in range(N-2):
        m.append(A[i+1]-A[i])
    if A[N-1]-A[0]>=(K/2):
        m.append(K+A[0]-A[N-1])
    else:
        m.append(A[N-1]-A[0])
    ans=K-max(m)
print(ans)