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:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
lmax = 0
point = 0
for i in range(n - 1):
    tmp = a[i + 1] - a[i]
    lmax = max(tmp, lmax)
lmax = max(lmax, a[0] + (k - a[-1]))
print(k - lmax)

Submission status: Accepted

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

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

print(K - max_length)

Submission status: Accepted

Solution code:
def main():
    K, N = map(int, input().split())
    AN = list(map(int, input().split()))

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

    print(K - max_diff)


main()

Submission status: Accepted

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

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

print(k-max(kyori))

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

def combinations_count(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]



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

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

ans = K - max(D)

print(ans)

Submission status: Accepted

Solution code:
k,n = map(int,input().split())
ll = list(map(int,input().split()))
tmp = [k-ll[-1]+ll[0]]
#print(tmp)
for i,l in enumerate(ll):
    if i == 0:
        continue
    tmp.append(ll[i]-ll[i-1])
#print(tmp)
m = max(tmp)
print(k-m)

Submission status: Accepted

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

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

print(k-ans)

Submission status: Accepted

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


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

    max_D = max(D)

    print(sum(D) - max(D))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())
A = list(map(int,input().split()))


now = 0
now_dis = K - A[-1] + A[0] 
for i in A:
  dis = i - now
  if dis > now_dis:
    now_dis = dis
  now = i
print(K-now_dis)

Submission status: Accepted

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

l = l + [i + K for i in l]

ans = 10**6
for i in range(N-1,2*N):
    ans = min(ans,l[i]-l[i-N+1])

print(ans)

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
a = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
sa = 0

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

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

print(k-sa)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
As = list(map(int, input().split()))
Ds = []
past = 0
for i, a in enumerate(As):
    if not i==0:
        Ds.append(a-past)
    past = a
Ds.append(As[0]+K-As[-1])
print(K-max(Ds))

Submission status: Accepted

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

ls = []
for i in range(n-1):
  ls.append(la[i+1] - la[i])
ls.append(k + la[0] - la[-1])

print(k - max(ls))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def solv(K, N, A):
    max_d = 0
    for i in range(N - 1):
        d = A[i + 1] - A[i]
        if d > max_d:
            max_d = d
    d = K + A[0] - A[-1]
    if d > max_d:
        max_d = d
    return K - max_d


if __name__ == "__main__":

    # K N
    # A_1 A_2 ... A_N
    #
    # 99
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    ans = solv(K, N, A)

    print(ans)

Submission status: Accepted

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

max_distance = 0
for i in range(1,len(A)):
  max_distance = max(max_distance,A[i] - A[i-1])

ans = K - max_distance

print(ans)

Submission status: Accepted

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

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

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

print(K-sa)

Submission status: Accepted

Solution code:

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

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

ans.append(k-a[-1]+a[0])
#print(ans)
ans.sort()

print(sum(ans[:-1]))

Submission status: Accepted

Solution code:
K, N = map(lambda x: int(x), input().split(" "))
As = list(map(lambda x: int(x), input().split(" ")))

result_list = []
for i in range(len(As)-1):
    result_list.append(As[i+1]-As[i])
result_list.append(K-As[-1]+As[0])

result = sum(result_list) - max(result_list)

print(result)

Submission status: Accepted

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


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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k - max_dist)

Submission status: Accepted

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

A_diff = list([0] for _ in range(N))

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

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

ans = K - max(A_diff)

print(ans)

Submission status: Accepted

Solution code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
s, e = A[0], A[-1]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k , n = map(int,input().split())
a = list(map(int,input().split()))
a.append(k+a[0])
a.sort()

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

Submission status: Accepted

Solution code:
k, n = map(int, input().split())
A = sorted(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:
K,N = map(int, input().split())
A = list(map(int, input().split()))

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

mi=max(li)
print(K-mi)

Submission status: Accepted

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


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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())
A = list(map(int, input().split()))
dist = []
for i in range(N-1):
  dist.append(A[i+1]-A[i])
dist.append(K-A[-1]+A[0])
print(sum(dist) - max(dist))

Submission status: Accepted

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

Submission status: Accepted

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

def solve(K, N, A):
    A.append(A[0] + K)
    B = [A[(i+1)] - A[i] for i in range(N)]
    B = sorted(B)
    return sum(B[:-1])


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Submission status: Accepted

Solution code:
# Code for C - Traveling Salesman around Lake
# Use input() to fetch data from STDIN
[k, n] = list(map(int, input().split()))
a = list(map(int, input().split()))

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

print(k - max(s))

Submission status: Accepted

Solution code:
import sys
import math
import collections
from collections import deque
#x = int(input())
k, n = map(int, sys.stdin.readline().split())
#s = input()
a = list(map(int, sys.stdin.readline().split()))

d = []

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

d.append(k-a[n-1]+a[0])
e = sum(d)
f = max(d)
print(e-f)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():

    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A = sorted(A)
    max_dist = A[0] + (K - A[-1])
    for i in range(N - 1):
        if max_dist < A[i + 1] - A[i]:
            max_dist = A[i + 1] - A[i]
    print(K - max_dist)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
k,n = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().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(sum(b) - max(b))

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

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

Submission status: Accepted

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

# process
A.sort()

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

maxa = max(maxa, A[0]+K-A[N-1])

# output
print(K-maxa)

Submission status: Accepted

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

Submission status: Accepted

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

A = A + [K + A[0]]
D = []
for i in range(N):
  D.append(A[i+1] - A[i])
ans = K - max(D)
print(ans)

Submission status: Accepted

Solution code:
kn = list(map(int,input().split()))
k = kn[0]
n = kn[1]
A = list(map(int,input().split()))

def find_max(a,n,k):
    m = a[0] + (k-a[n-1])
    for i in range(1,n):
        if a[i]-a[i-1] > m:
            m = a[i]-a[i-1]
    return m

print(k-find_max(A,n,k))

Submission status: Accepted

Solution code:
K,N,*A = map(int,open(0).read().split())
l = []
for i in range(N-1):
    l.append(A[i+1]-A[i])
l.append(K-A[-1] + A[0])   
s=sorted(l)
print(sum(l)-s[-1])

Submission status: Accepted

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

Submission status: Accepted

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

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

kyo.append((k - a[-1]) + a[0])
kyo.sort()
print(sum(kyo[:-1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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(K-max(B))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max_diff=-1
diff=-1
for i in range(len(arr)):
    if i==0:
        diff= k-arr[-1]+arr[0]
    else:
        diff= arr[i]-arr[i-1]
    if diff>max_diff:
        max_diff=diff
print(k-max_diff)

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key



__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':
    K, N = mulinputs()
    datas = list(mulinputs())

    dist = [0] * N
    dist[0] = K - datas[-1] + datas[0]
    i = 1
    while i < N:
        dist[i] = datas[i] - datas[i-1]
        i += 1

    # 出力
    max_idou = max(dist)
    print(K - max_idou)

Submission status: Accepted

Solution code:
K, N = [int(i) for i in input().split()]
An = sorted([int(i) for i in input().split()])
dAn = [An[0] + K - An[-1]]
for n in range(N-1):
  dAn.append(An[n+1] - An[n])
print( K - max(dAn))

Submission status: Accepted

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

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):
    max_len = 0
    for i in range(len(A)):
        if i == 0:
            max_len = A[0] + (K - A[N-1])
        else:
            if A[i] - A[i-1] > max_len:
                max_len = A[i] - A[i-1]
    print(K - max_len)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())
data = list(map(int, input().split()))
max_value = 0

for i in range(n):
    if i != n - 1:
        max_value = max(max_value, data[i + 1] - data[i])
    else:
        max_value = max(max_value, k - data[i] + data[0])

print(k - max_value)

Submission status: Accepted

Solution code:
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''
#codeforces
gi = lambda : list(map(int,input().split()))
m, n = gi()
l = gi()
ans = m - (m - l[-1] + l[0])
for k in range(1, n):
	ans = min(ans, m - (l[k] - l[k - 1]))
print(ans)

Submission status: Accepted

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

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

print(K - max(diffA))

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,-1,-1):
    temp=a[i-1]+(k-a[i])
    #print(temp)
    if temp>0 and temp<ans:ans=temp
print(ans)

Submission status: Accepted

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

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

B = [A[i] - A[i+1] for i in range(N-1)]

B.append(A[N-1]-K-A[0])

print(K + min(B))

Submission status: Accepted

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


def solve(K: int, N: int, A: "List[int]"):    
    return K - max(a2 - a1 for a1, a2 in zip(A, A[1:]+[K+A[0]]))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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]"
    print(solve(K, N, A))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    K,N = map(int, readline().split())
    A = [int(i) for i in readline().split()]

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

    d = max(d, K - A[-1] + A[0])

    print(K - d)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

print(k-long)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
poses=list(map(int,input().split()))
print(k-max(k-poses[-1]+poses[0],max(pos1-pos0 for pos1,pos0 in zip(poses[1:],poses))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(K-dist_max)

Submission status: Accepted

Solution code:
#


def main():
    k, n  = map(int, input().split(" "))
    a = [int(s) for s in input().split(" ")]
    max_distance = a[0] + k - a[-1]
    p = 0
    for t in a:
        if t - p > max_distance:
            max_distance = t - p
        p = t
    print(k - max_distance)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# li = [[0] * 4 for i in range(3)]

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

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

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

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

minn = max(minn, A[0] + K - A[N - 1])

print(K - minn)

Submission status: Accepted

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

mx = (k - a[n - 1]) + a[0]

for i in range(n - 1):
    sa = a[i + 1] - a[i]
    if sa > mx:
        mx = sa

print(k - mx)

Submission status: Accepted

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

Submission status: Accepted

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

#dists = []
for i in range(n-1):
    d = a_list[i+1] - a_list[i]
    if i == 0:
        maxd = d
    else:
        maxd = max(d, maxd)
    #dists.append(d)

#dists.append(k- a_list[n-1])
maxd = max(k-a_list[n-1]+a_list[0], maxd)
out = k-maxd
print(out)

Submission status: Accepted

Solution code:
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 = list(map(int, input().split()))
A = list(map(int, input().split()))
intervals = [A[i+1] - A[i] for i in range(len(A)-1)] + [A[0] + (K - A[-1])]
print(K-max(intervals))

Submission status: Accepted

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

A = [int(_) for _ in input().split()]


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

Submission status: Accepted

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

for i in range(N-1):
  if A[i+1]-A[i] > diff:
    diff = A[i+1]-A[i]

if K-A[N-1]+A[0] > diff:
  diff = K-A[N-1]+A[0]

print(K-diff)

Submission status: Accepted

Solution code:
def main():
    k, n = [int(i) for i in input().split()]
    pos = [int(i) for i in input().split()]
    res = k - pos[-1] + pos[0]
    for i, j in zip(pos[:-1], pos[1:]):
        if res < j -i:
            res = j-i
    print(k - res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

D = []

for i in range(N):
    if i != N - 1:
        D.append(A[i + 1] - A[i])
    else:
        D.append(A[0] + (K - A[i]))

D = sorted(D)

m = max(D)

print(K - m)

Submission status: Accepted

Solution code:
K,N=map(int,list(input().split(" ")))
A=list(map(int,input().split(" ")))
A.sort()
dis=[]
for i in range(N):
    if i==N-1:
        dis.append(A[0]+K-A[i])
    else:
        dis.append(A[i+1]-A[i])
m=max(dis)
print(K-m)

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools
# いくつか入力
k,n = (int(i) for i in input().split())  

# 空白区切りの数列をリストに代入
a = [int(i) for i in input().split()] 

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

maxd = max(maxd,k-b[n-1]+b[0])

print(k-maxd)

Submission status: Accepted

Solution code:
def C():
    K, N = map(int, input().split())
    A = [int(n) for n in input().split()]
    minimum_distance = A[-1] - A[0]
    for i in range(1, N):
        minimum_distance = min(minimum_distance, K - A[-i] + A[-i-1])
    print(minimum_distance)
            
C()

Submission status: Accepted

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

A+=[A[0]]
B=[None]*N
for i in range(0, N):
    if i<=N-2:
        B[i]=A[i+1]-A[i]
    if i==N-1:
        B[i]=A[i+1]+K-A[i]
#print(B)
print(sum(B)-max(B))

Submission status: Accepted

Solution code:
from operator import add
from functools import reduce

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

dists = [aa[0] + k - aa[-1]]
for i in range(1, len(aa)):
    dists.append(aa[i] - aa[i - 1])

print(k - max(dists))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(K - max_distance)