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: Runtime Error

Solution code:
import sys
lines = sys.stsin.readlines()
K, N = map(int, lines[0].srtip().split())
As = map(int, lines[1].strip().split())

range = max(As)-min(As)

if range*2<K:
  print(range)
else:
  print(K-range)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

print(a-max(dist))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
k, n = [int(i) for i in input().strip().split()]
arr = [int(i) for i in input().strip().split()] + [k + arr[0]]
max_d = 0
for i in range(1, n + 1):
    max_d = max(max_d, abs(arr[i] - arr[i - 1]))
print(k - max_d)

Submission status: Runtime Error

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

RL = []

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

L = []
l = 0

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

print(min(L))

Submission status: Runtime Error

Solution code:
K = int(input()) #20 #メートル
N = int(input()) #3 #件数
AL = [int(i) for i in input().split()] #[0,5,15]

RL = []

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

L = []
l = 0

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

print(min(L))

Submission status: Runtime Error

Solution code:
K = int(input()) #10 #メートル
N = int(input()) #3 #件数
AL = [int(i) for i in input().split()] #[0,5,15]

RL = []

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

L = []
l = 0

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

print(min(L))

Submission status: Runtime Error

Solution code:
def main():
    k,n = map(int,input().split())
    posn = list(map(int,input().split()))
    ans = []
    
    if(k<2 and k>1000000):
        main()
    if(n<2 and n>200000):
        main()
        
    l = posn.sort()
    if(l!=posn):
        main()
        
    
    
    #clockwise
    for i in range(n):
        count = 1
        dist = 0
        current = posn[i]
        
        for j in range(k):
            current+=1
            dist+=1
            if(posn.count(current)==1):
                
                count+=1
            if(current == k):
                current = 0
            if(count==n):
                break
            
        ans.append(dist)
    
    #anti-clockwise
    for i in range(n):
        count = 1
        dist = 0
        current = posn[i]
        
        for j in range(k):
            current-=1
            dist+=1
            if(posn.count(current)==1):
                count+=1
            if(current == 0):
                current = 20
            if(count==n):
                break
            
        ans.append(dist)
    
    
        
    print(min(ans))
    
main()

Submission status: Runtime Error

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

for i in range(n):
  if i == 0:
    dif_list.append(k-list[-1]+list[0])
  else:
    dif_list.append(list[i]-list[i-1])

print(k-max(dif_list))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

A.append(A[0]+K)

print(A)

for i in range(K):
  L = A[i+1]-A[i]
ans = K-max(L)

print(ans)

Submission status: Runtime Error

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

A.append(A[0]+K)

for i in K:
  L = A[i+1]-A[i]
ans = K-max(L)

print(ans)

Submission status: Runtime Error

Solution code:
def solve():
  K = int(input())
  N = int(input())
  vals = [None] * N
  ans = 99999999
  for i in range(N):
    vals[i] = int(input())
  for i in range(N):
    cc = -1
    if i == 0:
      cc = vals[N-1] - vals[0]
    else:
      cc = vals[i-1] + K - vals[i]
    ans = min(ans, cc)
  print(ans)
solve()

Submission status: Runtime Error

Solution code:
import numpy as np
K,N = map(int,input().split())
li = np.array(list(map(int,input().split())))
maxd=(K-li[N-1])+li[0]
for i in range(N-1):#i番目の家を出発地にした時の最小距離を順に求めていく
  maxd=max(maxd, li[i+1]-li[i]) 
print(K-maxd)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from fractions import gcd
A, B, C, D = map(int, input().split())

if A % C ==0:ca = A//C-1
else: ca = A//C
cb = B//C


if A % D ==0: ad = A//D-1
else:da = A//D 
db = B//D

lcm = C * D //gcd(C,D)
if A % lcm==0: cda = A//lcm-1
else:cda = A//lcm
cdb = B//lcm

print(B-A+1-((cb-ca)+(db-da)-(cdb-cda)))

Submission status: Runtime Error

Solution code:
def main():
    K, N = [int(_) for _ in input().split()]
    A = map(int, input().split())
    mx = 0

    for i in range(N-1):
        dis = A[i+1]-A[i]
        if dis > mx:
            mx = dis
    se = A[N-1] - A[0]
    if (k-(se)) > mx:
        mx = k - se
    print(mx)
main()

Submission status: Runtime Error

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

per_dist = list(A[i+1]-A[i] for i in range(0,N-1))
last_dist = A[0] + K - A[N - 1]
per_dist = per_dist.append(last_dist)
max_dist = max(per_dist)
print(K - max_dist)

Submission status: Runtime Error

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

per_dist = list(A[i+1]-A[i] for i in range(0,N-1))
last_dist = A[0] + K - A[N]
per_dist = per_dist.append(last_dist)
max_dist = max(per_dist)
print(K - max_dist)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
a += k
for i in range(n+1):
  l = max(l, a[i+1]-a[i])

print(k-l)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

max = K-A[-1]+A[0]
for i in range(N-1):
    if last < A[i+1]-A[i]:
        max = A[i+1]-A[i]

print(K-max)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
k, n = map(int, input().split())
l = list(map(int, input().split()))
d = []
for i in range(n):
  if i+1<n:
    d.append(abs(l[i+1]-l[i]))
  else:
    if 0 in l:
      d.append(abs(k - l[i]))
    else:
      d.append(abs(k - l[i] + l[0])
print(sum(d)-max(d))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

box = []
for i in range(N):
    d = abs(A[i-1]-A[i])
    if d>K/2:
        d = K - d
    box.append(d)
print(K-max(d))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
,N = map(int, input().split())
AN = max = i = j = 0
c = []

A = [int(N) for N in input().split()]
while i+1 != N:
    c.append(A[i+1] - A[i])
    if c[i] > max:
        max = c[i]
    i = i+1

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

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

while j != N:
    AN = AN + c[j]
    j = j + 1

AN = AN - max

print(AN)

Submission status: Runtime Error

Solution code:
import sys
from copy import deepcopy
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def cal(a,b,k):
    return min(abs(a-b), k-abs(a-b))

def cal2(l):
    n = len(l)
    ans = 0
    for i in range(n-1):
        ans += l[i+1] - l[i]
    return ans
def main():
    k,n = map(int, input().split())
    As = list(map(int, input().split()))
    le = list()
    for i in range(n):
        if i == n-1:
            le.append(cal(As[i], As[0],k))
        else:
            le.append(As[i], As[i+1])
    print(k-max(le))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
from copy import deepcopy
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def cal(a,b):
    return min(abs(a-b), k-abs(a-b))

def cal2(l):
    n = len(l)
    ans = 0
    for i in range(n-1):
        ans += l[i+1] - l[i]
    return ans
def main():
    k,n = map(int, input().split())
    As = list(map(int, input().split()))
    le = list()
    for i in range(n):
        if i == n-1:
            le.append(cal(As[i], As[0]))
        else:
            le.append(As[i], As[i+1])
    print(k-max(le))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main()
{
  int64_t n, k;
  cin >> k >> n;
  vector<int64_t> li(n * 2);
  for (int i = 0; i < n; ++i)
  {
    cin >> li[i];
    li[i + n] = li[i] - k;
  }
  sort(li.begin(), li.end());
  int64_t ans = 10e9;
  for (int i = 0; i < 2 * n - (n - 1); ++i)
    ans = min(ans, abs(li[i] - li[i + n - 1]));
  cout << ans << "\n";
}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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


if 0 in house_list:
    print(min(house_list[-1] - house_list[1], house_list[-1] - house_list[0]))
else:
    print(min(house_list[-1] - house_list[0]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
 
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(K - max_gap)

Submission status: Runtime Error

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

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


if 0 in house_list:
    print(min(house_list[-1] - house_list[1], house_list[-1] - house_list[0]))
else:
    print(min(house_list[-1] - house_list[1]))

Submission status: Runtime Error

Solution code:
K,N = map(int, input().split())
A = [int(N) for N in input().split()]
#print(A[0])
max1 = 0
max2 = 0
for i in range(0,N-1):
    max1 = A[i+1]-A[i]
    if max1>max2:
        max2=max1
if a[0]==0:
    lastlength = 20-A[N-1]
    if lastlength >max2:
        max2=lastlength
else:
    lastlength = A[N-1]-A[0]
    if lastlength>max2:
        max2=lastlength
print(20-max2)

Submission status: Runtime Error

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

house_list = list(map(int, input().split()))
print(house_list)


if 0 in house_list:
    print(min(house_list[-1] - house_list[1], house_list[-1] - house_list[0]))
else:
    print(min(house_list[-1] - house_list[1]))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
def main():
    H , W = map(int, input().split())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    value_temp = 0
    value_list = []
    i = 0
    for A in A_list :
        if i == 0 :
            A00 = A
            A0 = A
            i = i + 1
            continue
        else :
            value_temp = A - A0
            A0 = A
            value_list.append(value_temp)
            #print(str(value_temp))
            #print(str(A))
    if W > 2 :
        value_temp = H - A0
        value_list.append(value_temp)
	    value_temp = A00
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    elif W == 2 :
        value_temp = H - A0
        value_list.append(value_temp)
        value_temp = A00
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    else :
        ans = sum(value_list)

    #print(str(value_list))

    print (str(ans))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
k,m = map(int,input().split())
a = input().split()
a =[int(s) for s in a]

a.insert(0,k)

dis = []

for i in range(m):
  part = int(a[i]) - int(a[i+1])
  dis.append(part)
  del part

dis.append(int(a[m-1]))
distance = dis*2

c =[]
for j in range(m):
  c = int(sum(distance[j:j+5]))

print(min(c))

Submission status: Runtime Error

Solution code:
K = int(input())
N = int(input())
A = []

for i in range(N):
    A.append(int(input()))

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

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

B.sort()

Big = B[N-1]

print(K-Big)

Submission status: Runtime Error

Solution code:
def around(K,A):
    N2 = A.pop(0)
    A.append(N2+K)
    return A

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

X = 0
X_min = 1000000000
for j in range(0,len(A)):
    X=
    X = A[len(A)-1] - A[0]
    if X_min > X:
        X_min = X
    A=around(K,A)
    
print(X_min)

Submission status: Runtime Error

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

l=[]
distance=0
for i in range(1,n):
    distance += (a[-i]-a[-i-1])
l.append(distance)
# print(l)

a_minus = [i-k for i in a if i != 0]
a_minus.sort(reverse=True)
# print(a_minus)

for j in range(n-1):
    tmp = a[:(-j-1)]
    tmp.extend(a_minus[:n-len(tmp)])
    tmp.sort(reverse=True)
    # print(tmp)
    d=0
    for k in range(len(tmp)-1):
        if tmp[k]>0:
            if tmp[k+1]>0:
                # print("++")
                d+=abs(tmp[k])-abs(tmp[k+1])
            else:
                d+=abs(tmp[k])+abs(tmp[k+1])
        else:
            # print("--")
            d+=abs(abs(tmp[k])-abs(tmp[k+1]))
    l.append(d)
print(min(d))

Submission status: Runtime Error

Solution code:
def main():
    H , W = map(int, input().split())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    value_temp = 0
    value_list = []
    i = 0
    for A in A_list :
        if i == 0 :
            A00 = A
            A0 = A
            i = i + 1
            continue
        else :
            value_temp = A - A0
            A0 = A
            value_list.append(value_temp)
            #print(str(value_temp))
            #print(str(A))
    if W > 2 :
        value_temp = H - A0
        value_list.append(value_temp)
	value_temp = A00
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    elif W == 2 :
        value_temp = H - A0
        value_list.append(value_temp)
        value_temp = A00
        value_list.append(value_temp)
        max_value = max(value_list)
        ans = sum(value_list) - max_value
    else :
        ans = sum(value_list)

    #print(str(value_list))

    print (str(ans))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
k,m = map(int,input().split())
a = input().split()
a =[int(s) for s in a]

a.insert(0,k)

dis = []

for i in range(m):
  part = int(a[i]) - int(a[i+1])
  dis.append(part)
  del part

dis.append(int(a[m-1]))
distance = dis*2

c =[]
for j in range(m):
  c = sum(distance[j:j+5])

print(min(c))

Submission status: Runtime Error

Solution code:

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

A_sorted=sorted(A)

if sorted_A[N-1]-sorted_A[0] <K/2:
    print(sorted_A[N-1]-sorted_A[0])

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

    a=A_sorted[N-1]-A_sorted[0]
    b=K-a

    print(min(a, b))

Submission status: Runtime Error

Solution code:
K, N = input().split()
i, lists = 0, []
while i < N:
    lists.append(eval(input()))
    i += 1
if 0 in lists:
    lists.remove(0) 
print(max(lists) - min(lists))

Submission status: Runtime Error

Solution code:
K, N = input().split()
i, lists = 0, []
while i < int(N):
    lists.append(eval(input()))
    i += 1
if 0 in lists:
   lists.remove(0) 
print(max(lists) - min(lists))

Submission status: Runtime Error

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

foK,N = map(int,input().split())
A = list(map(int,input().split()))
ans = []

for i in range(N-1):
  now = i
  pre = now-1
  post = now +1
  #nowとpreの距離を求める
  if now == 0:
    preDic = (K-A[N-1])+A[now]
  else:
    preDic = A[now]-A[pre]
  postDic = A[post]-A[now]  
  if preDic >= postDic:#時計回りに、i-1の位置まで進む
    ans.append(K-preDic)
  else:
    ans.append(K-postDic)

    
print(min(ans))

Submission status: Runtime Error

Solution code:
K, N = input().split()
A = []
for i in range(N):
    A.append(int(input()))
if 0 in A:
    A.remove(0)
print(max(A)-min(A))

Submission status: Runtime Error

Solution code:
K, N = int(input()), int(input())
A = []
for i in range(N):
    A.append(int(input()))
if 0 in A:
    A.remove(0)
print(max(A)-min(A))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

r=0
s=0

i=0
while(i<N-1):
  r+=list[i+1]-list[i]
  i+=1

j=0

while(j<N-2):
  s+=list[j+1]-list[j]
  j+=1
s+=K-list[N-1]


if(r<s):
  print(r)
else():
  print(s)

Submission status: Runtime Error

Solution code:
K, N, i, lists = input().split(), 0, []
while i < N:
    lists.append(eval(input()))
    i += 1
if 0 in lists:
   lists.remove(0) 
print(max(lists) - min(lists))

Submission status: Runtime Error

Solution code:
K, N = input().split()
A = list(map(int, input().split()))
    if 0 in A:
        A.remove(0)
x = max(A) - min(A)
print(x)

Submission status: Runtime Error

Solution code:
1. K, N = input().split()
2. i, lists = 0, []
3. while i < N:
4.     lists.append(eval(input()))
5.     i += 1
6. if 0 in lists:
7.    lists.remove(0) 
8. print(max(lists) - min(lists))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
def ai_time(K, A, ai, max_a, min_a):
  r = ai_time_r(K, A, ai)
  l = ai_time_r(K, A, ai)
  return min([r, l])
 
def ai_time_r(K, A, ai, max_a):
  long_lst = [i for i in A if i < ai]
  if long_lst:
    return max(long_lst) - ai + K
  else:
    return max_a - ai

def ai_time_l(K, A, ai, min_a):
  long_lst = [i for i in A if i > ai]
  if long_lst:
    return K - ai + min(long_lst)
  else:
    return ai - min_a
  
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
max_a = max(A)
min_a = min(A)
res = min([ai_time(K, A, ai, max_a, min_a) for ai in A])
print(res)

Submission status: Runtime Error

Solution code:
k, n = map(int, input().split())
li = list(map(int, input().split()))
res = 0
indt = []
t, ind = float("inf"), 0
for i in range(n):
    try:
        if li[i+1] - li[i] < t:
            t = li[i+1] - li[i]
            ind = i
        elif li[i+1] - li[i] == t:
            indt.append(i)
    except:
        if k - li[-1] + li[0] < t:
            t = k - li[-1] + li[0]
            ind = i
        elif k - li[-1] + li[0] == t:
            indt.append(i)
temp = ind + 1
while temp != ind:
    if temp == 0:
        res += k - li[-1] + li[i]
        continue
    res += li[temp] - li[temp - 1]
    temp = (temp + 1) % n
r = res
# print(indt)
for i in indt:
    res = 0
    temp = i + 1
    ind = i
    while temp != ind:
        if temp == 0:
            res += k - li[-1] + li[0]
            temp = (temp + 1) % n
            # print(res, k - li[-1] + li[i])
            continue
        res += li[temp] - li[temp - 1]
        temp = (temp + 1) % n
        # print(res)
    r = min(r, res)
print(r)

Submission status: Runtime Error

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

x[N] = K - x[N]

for i in range(N):
  if x[i] > x[i+1]:
    x[i+1] = x[i]

print(K-x[N])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
K, N = input().split()
i, lists = 0, []
while i < N:
    A = eval(input())
    lists.append(A)
    i += 1
if 0 in lists:
   lists.remove(0)
x = max(lists) - min(lists)
print(x)

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np

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

routes = []
cycle = itertools.cycle(AS)
for i in range(0, N):
    route = []
    l = 0
    for j in range(0, N):
        d = next(cycle)
        if d < l:
            d = K + d
        l = d
        route.append(d)

    routes.append(route)
    next(cycle)

def distance(route):
    route = np.array(route)
    return sum(np.diff(route, n=1))

print(min(list(map(distance, routes))))

Submission status: Runtime Error

Solution code:
K, N = input().split()
A = map(int, input().split())
if 0 in A:
    A.remove(0)
x = max(A) - min(A)
print(x)

Submission status: Runtime Error

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

a1 = max(A)-min(A)

max_v = 0
for i in range(len(A)-1):
    A_i = None
    if (A[i+1]-A[i])>max_v:
        max_v = A[i+1]-A[i]
        A_i = i+1

a2 = A[A_i-1]+abs(K-A[A_i])
print(a2) if a1>a2 else print(a1)

Submission status: Runtime Error

Solution code:
K,N = map(int,input().split())
a = list(map(int,input().split()))
c = a[1:]-a[:-1]
d = max(c)
print(K - d)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int k, n;
	cin >> k >> n;
	int arr[n];
	for(int i = 0 ;i < n; i++)
	{
		cin >> arr[i];
	}
	int sum = 0;
	sort(arr, arr + n);
    for (int i = 0; i < n; i++)
    {	
        
        if(i != 0 && i != n - 1)
        {
            sum += min(arr[i] - arr[i - 1], arr[i+1] - arr[i]);
        }
        else if(i == 0)
        {
            sum += min(k - arr[n - 1], arr[1] - arr[0]);
        }
    }
    cout << sum << endl;
	
}

Submission status: Runtime Error

Solution code:
K, N = input().split()
A = map(int, input().split())
if 2  <= K <= 10 ** 6 and 2 <= N <= 2 * 10 ** 5:
    if 0 in A:
        A.remove(0)
    x = max(A) - min(A)
    print(x)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

# 時計回り
r_ans = 10**9
if a[0] + a[n-1] >= k:
    r_ans = a[n-1] - a[0]
    # print(a[n-1] - a[0])

# 反時計回り
else:
    max_n = 0
    for i in range(n-1):
        diff = abs(a[i] - a[i+1])
        max_n = max(diff, max_n)
    # print(k - max_n)
    l_ans = k - max_n

print(min(r_ans, l_ans))

Submission status: Runtime Error

Solution code:
k, n = input().split()
a = input().split()
max = 0
for i in range(len(a)):
    if i==0:
        continue
    d = a[i]-a[i-1]
    if d>max:
        max=d
print(k-max)

Submission status: Runtime Error

Solution code:
K = int(input())
N = int(input())
lists = [int(x) for x in input().split()]
lists.sort()
sor = []
for i in range(N-1):
    sor.append(lists[i+1]-lists[i])
sor.append(K-lists[N-1]+lists[0])
print(K-max(sor))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
D = np.zeros((N,N))
for i in range(N-1):
  for j in range(i+1,N):
    a = abs(A[i]-A[j])
    D[i][j] = min(a, K-a)
print (K-np.max(D))

Submission status: Runtime Error

Solution code:
k,n=map(int,input().split()) l=list(map(int,input().split())) ans=[] for i in range(len(l)):   ans.append(sum(min(abs(l[j]-l[i]),abs(k-l[j])) for j in range(len(l)) if i!=j)) print(min(ans))

Submission status: Runtime Error

Solution code:
num = int(input())
re = 0
while num >= 500:
    re+=1000
    num-=500
while num >=5:
    re+=5
    num-=5
print(re)

Submission status: Runtime Error

Solution code:
X = int(input())

a = int(X / 500)*1000 + int((X%500)/5)*5
print(a)

Submission status: Runtime Error

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

ans = 0
for i in range(N - 1):
    S = [A[i + 1] - A[i]]
    S.append((K - A[N - 1]) + A[0])
    m = sorted(S)
    for n in range(N - 1):
        ans += m[n]
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def loopf(i, k):

    if i == len(A) - 1:
        return K -k

        # if K - A[i] + A[0] >= k:
            # k = K - A[i] + A[0]

        return K - k

    if A[i + 1] - A[i] >= k:
        k = A[i + 1] - A[i]

    return loopf(i + 1, k)


print(loopf(0, 0))

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout  

n = stdin.readline() 
arr = [int(x) for x in stdin.readline().split()] 
K=arr[0]
N=arr[1]
n = stdin.readline() 
A = [int(x) for x in stdin.readline().split()] 
dist=[]
for i in range(0,N-1):
    dist.append(A[i+1]-A[i])
dist.append(abs(A[0]-A[-1]+K))

m=max(dist)
for idx,val in enumerate(dist):
    if(val==m):
        dist[idx]=0
        break
print(sum(dist))