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: Accepted

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

Submission status: Accepted

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

    for i in range(n):
        a.append(a[i] + k)

    ans = 2 * k

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

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    k, n = map(int, input().split())
    a = [int(i) for i in input().split()]
    max = 0
    max = a[0] + (k-a[n-1])
    bf = 0

    for i  in a:
        now = i -bf
        if now > max:
            max = now
        bf = i

    print(k-max)

Submission status: Accepted

Solution code:
def submit():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    # 家間の距離を計算
    gaps = [a[i + 1] - a[i] for i in range(n - 1)]
    gaps.append(a[0] + k - a[n - 1])
    # 一番距離が大きいパスを省けば良い
    print(sum(gaps) - max(gaps))


submit()

Submission status: Accepted

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

Submission status: Accepted

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


K, N = inpl()
A = inpl()
A.append(K + A[0])

ans = K
for a, na in zip(A, A[1:]):
    ans = min(ans, K - (na - a))

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.q = l
        heapq.heapify(self.q)
        return

    def push(self, n):
        heapq.heappush(self.q, n)
        return

    def pop(self):
        return heapq.heappop(self.q)


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    K, N = Scanner.map_int()
    A = Scanner.map_int()
    A.append(0)
    A.append(K)
    A.sort()
    l = [A[i + 1] - A[i] for i in range(len(A)-1)]
    ans = min(K - max(l), A[-2] - A[1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
k, n = map(int, input().split())

a = list(map(int, input().split()))
sub = [abs(k - a[-1] + a[0])]

for i in range(len(a) - 1):
    tmp = abs(a[i + 1] - a[i])
    sub.append(tmp)
print(sum(sub) - max(sub))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	k, n = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	mind = 10**9
	for i in range(n):
		if i == 0:
			mind = min(mind, k - abs(a[i] - a[i+1]), k - abs(a[i] + k - a[-1]))
			continue
		if i == n - 1:
			mind = min(mind, k - abs(k - a[i] + a[0]), k - abs(a[i] - a[i-1]))
			continue
		mind = min(mind, k - abs(a[i] - a[i+1]), k - abs(a[i] - a[i-1]))
	print(mind)


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

maxlen = max(a[0] + k - a[-1],maxlen) 


print(k - maxlen)

Submission status: Accepted

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

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

print(K - ans_sub)

Submission status: Accepted

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

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

B = [None]*N
for i in range(N):
    B[i] = (A[(i+1)%N] - A[i])%K

print(K - max(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC160c


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    for i in a:
        b.append(i - k)
    b.extend(a)
    ans = 10 ** 12
    # print(b)
    for i in range(n):
        #print(i, b[i+n-1], b[i])
        ans = min(ans, b[i+n-1]-b[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(K - max(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:



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

    A.sort()

    x = A[-1] - A[0]
    x = min(x, K - A[1]+A[0])

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

    print(x)


    



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
Alist = sorted(list(map(int, input().split())))
minus = 0
old = Alist[0]
for a in Alist[1:]:
    minus = max(minus, a - old)
    old = a
minus = max(minus, (K - a) + Alist[0])
print(K - minus)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

max_length = A[-1]-A[0]

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

print(max_length)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
k, n = map(int,input().split())
a = [int(i) for i in input().split()]

a.sort()
ans1 = a[-1] - a[0]
ans2 = 10 ** 10
for i in range(n - 1):
    #print(i, i + 1)
    tmp = a[i] + (k - a[i + 1])
    if tmp < ans2:
        ans2 = tmp
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
setrecursionlimit(1000000)

# INF = float('inf')
INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()

    # 番兵
    # if A[0] != 0:
    A.append(K + A[0])

    # min, maxまでの和
    dist = A[-1] - A[0]

    # remove max diff
    diff = 0
    for i in range(len(A) - 1):
        diff = max(diff, A[i + 1] - A[i])
    # print(A)
    # print(dist)
    # print(diff)
    print(dist - diff)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

r = A[N - 1] - A[0]
ans = min(r, ans)

print(ans)

Submission status: Accepted

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

k, n = MAP()
a = LIST()
dis = [a[i+1]-a[i] for i in range(n-1)]
dis.append(k-a[n-1]+a[0])

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = a[-1]-a[0]
for i in range(n-1):
    ans = min(ans,k-(a[i+1]-a[i]))
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict, deque

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

minDist = K + A[0] - A[-1]
for x, y in zip(A, A[1:]):
    minDist = max(minDist, y - x)
print(K - minDist)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K, N = map(int, input().split())
A_list = list(map(int, input().split()))

max_dist = 0
for i in range(N - 1):
    max_dist = max(max_dist, A_list[i + 1] - A_list[i])

max_dist = max(max_dist, K - A_list[N - 1] + A_list[0])
print(K - max_dist)

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
A = sorted([int(i) for i in input().split()])
res = [A[-1]-A[0]]

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

print(min(res))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sa = [0]*len(a)
for i in range(n-1):
  sa[i] = a[i+1] - a[i]
sa[n-1] = a[0] + k - a[n-1]

print(k-max(sa))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K, N = MAP()
A = LIST()
diff = abs(K-A[-1]+A[0])
d = abs(K-A[-1]+A[0])
if N == 2:
	print(min(d, A[1]-A[0]))
	exit()
for i in range(1, N):
	d += abs(A[i]-A[i-1])
	diff = max(diff, abs(A[i]-A[i-1]))
print(d-diff)

Submission status: Accepted

Solution code:
from sys import stdin
K, N = (int(i) for i in input().split())
A = [int(x) for x in stdin.readline().rstrip().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]

dif = [A[0]+(K-A[N-1])]

for i in range(1, N):
    dif.append(A[i]-A[i-1])

print(K-max(dif))

Submission status: Accepted

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








print(k-l)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(K - maxd)

Submission status: Accepted

Solution code:
# coding: utf-8

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

A = list(map(int, input().split()))
A = sorted(A)
# print(A)

max_dif = 0
for i in range(N-1):
    dif = A[i+1] - A[i]
    # print(dif)
    if dif > max_dif:
        max_dif = dif

if A[0] + (K-A[-1]) > max_dif:
    max_dif = A[0] + (K-A[-1])

print(K - max_dif)

Submission status: Accepted

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

for i in range(n):
    a.append(a[i] + k)

ans = float("inf")
for i in range(n):
    dist = a[i + n - 1] - a[i]
    ans = min(ans, dist)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a.append(a[0] + k)
ans = min([k - a[i + 1] + a[i] for i in range(n)])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
k, n = map(int, input().split())
a = list(map(int, input().split()))
m = max((a[i] - a[i - 1] + k) % k for i in range(n))
print(k - m)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K, N = MAP()
A = LIST()
B = []
for i in range(N-1):
    B.append(A[i+1]-A[i])
B.append(K - A[N-1] + A[0])
# print(B)
c = []
for b in B:
    c.append(K-b)
print(min(c))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

K, N = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
diffs = []
prev = 0
for a in A:
    diffs.append(a - prev)
    prev = a
last = K - A[-1] + A[0]
diffs.append(last)

print(K - max(diffs))

Submission status: Accepted

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

dist = []

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

answer = max(dist)

print(k - answer)

Submission status: Accepted

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

if d < k - a[-1] + a[0]:
    d = k - a[-1] + a[0]

print(k - d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
k,n=map(int,input().split())

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

c=k-(a[n-1]-a[0])

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

print(k-c)

Submission status: Accepted

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

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

sa=0
i=1
while i <n:
    if sa<a[i]-a[i-1]:
        sa=a[i]-a[i-1]
    i+=1

sa=max(sa,a[0]+k-a[n-1])


print(k-sa)

Submission status: Accepted

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]+a[0]
print(k-max(b))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
#sys.setrecursionlimit(150000)


def solve(K: int, N: int, A: "List[int]"):
    lmax=0
    A.append(K+A[0])
    for i in range(N):
        lmax=max(lmax, A[i+1]-A[i])
    print(K-lmax)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(K, N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = Alist[0] + (K - Alist[N-1])

for i in range(N-1):
    ans = max(ans, Alist[i+1] - Alist[i])

print(K - ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(K, N, A):
    m = 0
    for i in range(N):
        d = A[i] - A[i-1]
        if d < 0:
            d += K
        m = max(m, d)
    return K - m


def main():
    K, N = read_int_n()
    A = read_int_n()
    print(slv(K, N, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29



def main():
    K,N = isRaw()
    As = isRaw()
    ans = K-As[0]-(K-As[-1])
    for aidx in range(len(As)-1):
        ans = min(ans,K-(As[aidx+1]-As[aidx]))
    return ans

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 20:52:57 2020

@author: over-
"""

K,N=map(int,input().split())
A=input().split()
A=[int(i) for i in A]

L=[]
for i in range(1,N):
    L.append(A[i]-A[i-1])

L.append(A[0]+K-A[-1])
M=max(L)
ans=K-M
#print(A)
print(ans)

Submission status: Accepted

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[0] - a[n-1]

print(sum(b) - max(b))

Submission status: Accepted

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import decimal
import heapq
import collections
import itertools
from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

k, n = map(int, input().split())
d = getInputListInt()

max_dis = 0
reigai = d[0]+(k-d[n-1])
max_dis = max(max_dis, reigai)

for i in range(1, n, 1):
    tmp = d[i]-d[i-1]
    max_dis = max(max_dis, tmp)

print(k-max_dis)

Submission status: Accepted

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

max_diff = A[0]+N-A[-1]
for i in range(1,M):
  max_diff = max(A[i]-A[i-1],max_diff)
ans = N-max_diff
print(ans)

#print(*ans, sep='\n')

Submission status: Accepted

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

Submission status: Accepted

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

a.append(k + a[0])
intervals = [a[i]-a[i-1] for i in range(1, len(a))]
print(k - max(intervals))

Submission status: Accepted

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