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: Time Limit Exceeded

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

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)

min_index = [i for i, v in enumerate(dist) if v == min(dist)]

right = 0
left = 0
result = []
for start_index in min_index:
  for n in range(0, len(dist)-1):
    x = start_index + n
    y = start_index - n
    if x > len(dist)-1:
      x = (len(dist) - n - start_index) * -1
    if y < 0:
      y = len(dist)-n
    right += dist[x]
    left += dist[y]
    if len(result) > 0 and (min(result) < right and min(result) < left):
      break
  result.append(min(right, left))
  
print(min(result))

Submission status: Time Limit Exceeded

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

    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])

    d = tuple(d)
    
    most_min_path = 10000

    for i in range(n):
        if i == 0:
            normal_ck = sum(d) - d[i] - d[-1]
            reverse_ck = sum(d) - d[i+1]
        elif i == n - 1:
            normal_ck = sum(d) - d[i]
            reverse_ck = sum(d) - d[i+1] - d[0]
        else:
            normal_ck = sum(d) - d[i]
            reverse_ck = sum(d) - d[i+1]

        min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
        if most_min_path > min_ck:
            most_min_path = min_ck

    print(most_min_path)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def ai_time(K, A, ai):
  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):
  long_lst = [i for i in A if i < ai]
  if not long_lst:
    return max(A) - ai
  else:
    return max(long_lst) - ai + K

def ai_time_l(K, A, ai):
  long_lst = [i for i in A if i > ai]
  if not long_lst:
    return ai - min(A)
  else:
    return K - ai + min(long_lst)
  
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
min_time = 10 ** 6 + 1
for ai in A:
  t = ai_time(K, A, ai)
  if t < min_time:
    min_time = t
print(min_time)

Submission status: Time Limit Exceeded

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=0
    for i in range(0,len(A)-1): #i番目の家からスタート
        X += A[i+1] - A[i]
        if X > X_min:
            i=0
            break
    if i == len(A)-2:
        X_min = X
    A=around(K,A)
#     print(X_min)
    
print(X_min)

Submission status: Time Limit Exceeded

Solution code:
K, N = map(int,input().split())
i = list(map(int, input().split()))
ans_list = []
for x in range(N):
    ans_list = [(max(list(map(lambda z: (z - i[x])%K , i)))-min(list(map(lambda z: (z - i[x])%K , i)))) for x in range(N)]
print(min(ans_list))

Submission status: Time Limit Exceeded

Solution code:
k, n = map(int, input().split())
a_list = list(map(int, input().split()))
ans = k
for i in a_list:
    seq_list = [0] * n
    if i == 0:
        for j in range(n):
            if a_list[j] > k / 2:
                seq_list[j] = a_list[j] - k
            else:
                seq_list[j] = a_list[j]

    else:
        for j in range(n):
            seq_list[j] = a_list[j] - i
    max_a = max(seq_list)
    min_a = min(seq_list)
    seq_dis = max_a - min_a
    ans = min(seq_dis, ans)
print(ans)

Submission status: Time Limit Exceeded

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

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)

min_index = [i for i, v in enumerate(dist) if v == min(dist)]

right = 0
left = 0
result = []
for start_index in min_index:
  for n in range(0, len(dist)-1):
    x = start_index + n
    y = start_index - n
    if x > len(dist)-1:
      x = (len(dist) - n - start_index) * -1
    if y < 0:
      y = len(dist)-n
    right += dist[x]
    left += dist[y]
  result.append(min(right, left))
  
print(min(result))

Submission status: Time Limit Exceeded

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

all_length = k*2
point = []
for i in range(n):
    a_left  = k-a[i]
    a_right = k+a[i]
    point.append(a_left)
    point.append(a_right)
point = list(set(point))
point.sort()

num = len(point)
num = round(num/2)
distance = []
for i in range(num):
    dis = 0
    for j in range(n-1):
        dis += point[i+j+1] - point[i+j]
    distance.append(dis)
mindis = min(distance)
print(mindis)

Submission status: Time Limit Exceeded

Solution code:
K, N = map(int,input().split())
N = N-1
A = list(map(int, input().split()))
A_d = []
for i in range(0, len(A)-1):
    A_d.append(A[i+1]-A[i])
A_d.append(K-A[-1]+A[0])
A_d2 = (A_d+A_d[1:])*2
res = K
for i in range(len(A_d2)-1):
    if res > sum(A_d2[i:i+N]):
        res = sum(A_d2[i:i+N])
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

ans = [S[-1] - S[0]]
for i in range(N-1):
    s = max(S) - K
    del S[-1]
    S.appendleft(s)
    ans.append(S[-1] - S[0])


print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
def calc(array, start, k):
    ri = start
    li = start
    
    if start == 0:
        eri = len(array) - 1
        eli = start + 1
    elif start == len(array) - 1:
        eri = start - 1
        eli = 0
    else:
        eri = start - 1
        eli = start + 1
    
    right = 0
    left = 0

    while True:
        if ri + 1 == len(array):
            right += k - array[ri] + array[0]
            ri = 0
        else:
            right += array[ri + 1] - array[ri]
            ri += 1

        if li - 1 < 0:
            left += k - array[-1] + array[li]
            li = len(array) - 1
        else:
            left += array[li] - array[li - 1]
            li -= 1
        #print("start = {}: ri = {}, li = {}".format(start, ri, li))
        #print("right = {}, left = {}".format(right, left))

        if ri == eri and li == eli:
            break

    return min(right, left)

def main():
    tmp = input().split(" ")
    k = int(tmp[0])

    a = input().split(" ")
    a = [int(i) for i in a]

    ans = 300000000
    for i in range(len(a)):
        ans = min(ans, calc(a, i, k))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])

    d = tuple(d)

    most_min_path = 10000

    for i in range(n):
        if i == 0:
            normal_ck = sum(d) - d[i] - d[-1]
            reverse_ck = sum(d) - d[i+1]
        elif i == n - 1:
            normal_ck = sum(d) - d[i]
            reverse_ck = sum(d) - d[i+1] - d[0]
        else:
            normal_ck = sum(d) - d[i]
            reverse_ck = sum(d) - d[i+1]

        min_ck = normal_ck if normal_ck < reverse_ck else reverse_ck
        if most_min_path > min_ck:
            most_min_path = min_ck

    print(most_min_path)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
kn = list(map(int,input().split()))
K = kn[0]
N = kn[1]
As = list(map(int,input().split()))

subtract_list_r = np.array([])
subtract_list_l = np.array([])
#subtract_list = np.append(subtract_list,As[0]-As[N-1])
subtract_value_r = 0
subtract_value_l = 0
for i in range(1,N-1):
    subtract_value_r = K-(As[i]-As[i-1])
    subtract_value_l = K-(As[i+1]-As[i])
    subtract_list_r = np.append(subtract_list_r,subtract_value_r)
    subtract_list_l = np.append(subtract_list_l,subtract_value_l)


subtract_list_r = np.append(subtract_list_r,As[N-1]-As[0])
subtract_list_l = np.append(subtract_list_l,K-(As[1]-As[0]))
subtract_list_r = np.append(subtract_list_r,K-(As[N-1]-As[N-2]))
subtract_list_l = np.append(subtract_list_l,abs(As[0]-As[N-1]))
r_min = min(subtract_list_r)
l_min = min(subtract_list_l)

print("{}".format(int(min([r_min,l_min]))))

Submission status: Time Limit Exceeded

Solution code:
k, n = map(int, input().split())
a_list = input().split()  # sはlist

ans = 0
len = len(a_list)
for i in range(len):
    for j in range(len-i-1):
        dis=abs(int(a_list[i])-int(a_list[j+i+1]))
        ans = max(ans, min(dis,k-dis))
print(ans)

Submission status: Time Limit Exceeded

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

    d = [a[i] - a[i-1] for i in range(1, n)]
    d.insert(0, a[0])
    d.append(k - a[-1])

    d = tuple(d)

    most_min_path = 10000

    for i in range(n):
        if i == 0:
            normal_ck = sum(d) - d[i] - d[-1]
            reverse_ck = sum(d) - d[i+1]
        elif i == n - 1:
            normal_ck = sum(d) - d[i]
            reverse_ck = sum(d) - d[i+1] - d[0]
        else:
            normal_ck = sum(d) - d[i]
            reverse_ck = sum(d) - d[i+1]

        if normal_ck > reverse_ck:
            min_ck = reverse_ck
        else:
            min_ck = normal_ck

        if most_min_path > min_ck:
            most_min_path = min_ck

    print(most_min_path)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def permutations(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = set(L)

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutations(data):
                ret.append([i] + j)

        return ret

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

result = []

round1 = permutations(a)

for c in round1:
    total = 0
    for b in range(len(c)-1):
        
        if c[b] ==0:
            total2 = k - c[b+1]
            total3 =abs(c[b+1] - c[b])
            if total2 <= total3:
                total3 = total2
        else:
            total3 =abs(c[b+1] - c[b])
        
        total = total3 + total
    result.append(total)
print(sorted(result)[0])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

b = []
  
for i in range(n-1):
    b.append(a[i+1] - a[i])
    
b.append(a[0] + k -a[n-1])
 
lans=0
ans = 999999999999
 
for i in range(len(b)):
    lans = 0
    for j in range(len(b)):
        if i != j:
            lans = b[j] + lans
    ans = min(ans,lans)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def ai_time(K, A, ai):
  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):
  long_lst = [i for i in A if i < ai]
  if not long_lst:
    return max(A) - ai
  else:
    return max(long_lst) - ai + K

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

Submission status: Time Limit Exceeded

Solution code:
k, n = map(int, input().split())
a_list = input().split()  # sはlist

dis_list = []
for i in range(len(a_list)):
    for j in range(len(a_list)-i-1):
        dis=abs(int(a_list[i])-int(a_list[j+i+1]))
        dis_list.append(min(dis,k-dis))
print(max(dis_list))

Submission status: Time Limit Exceeded

Solution code:
K, N = map(int,input().split())
i = list(map(int, input().split()))
ans_list = []
for x in range(N):
    list_i = list(map(lambda z: (z - i[x])%K , i))
    ans_list.append(max(list_i)-min(list_i))
print(min(ans_list))

Submission status: Time Limit Exceeded

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

d = [a[i] - a[i-1] for i in range(1, n)]
d.insert(0, a[0])
d.append(k - a[-1])

d = tuple(d)

most_min_path = 10000

for i in range(n):
    if i == 0:
        normal_ck = sum(d) - d[i] - d[-1]
        reverse_ck = sum(d) - d[i+1]
    elif i == n - 1:
        normal_ck = sum(d) - d[i]
        reverse_ck = sum(d) - d[i+1] - d[0]
    else:
        normal_ck = sum(d) - d[i]
        reverse_ck = sum(d) - d[i+1]
    if normal_ck > reverse_ck:
        min_ck = reverse_ck
    else:
        min_ck = normal_ck
    
    if most_min_path > min_ck:
        most_min_path = min_ck

print(most_min_path)

Submission status: Time Limit Exceeded

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

if A[0] != 0:
    B = A + [K] 
else:
    B = A
#print(B)

for i in range(N):
    B.append(A[i] + K)
#print(B)

ans = 10000000
C =[]
for i in range(N):
    cnt = 0
    for j in range(i, i + N - 1):
        cnt +=B[j+1] - B[j]
    ans = min(ans,cnt)

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(1, n+1):
    if i == 1:
        distance = k - (a[0] + (k - a[-1]))
        mindistance = distance
        a.append(a[0])
        a.pop(0)
    else:
        distance = k - (a[0] - a[-1])
        if distance < mindistance:
            mindistance = distance
        a.append(a[0])
        a.pop(0)

print(mindistance)

Submission status: Time Limit Exceeded

Solution code:
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def rev(x, limit):
    s = 0
    for i in range(len(x) - 1):
        if x[i] == 0:
            x[i] = limit
        s += abs(x[i] - x[i + 1])
        # print(s)
    return s


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    daf = a[-1] - a[0]
    minn = min(daf, int(10e10))
    for i in range(1, n):
        grab = [a[i]] + a[:i] + a[i + 1:]
        #print(grab)
        minn = min(rev(grab, k), minn)
    print(minn)


if __name__ == "__main__":
    #IOE()
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
data = input().split()

round = int(data[0])
cnt_house = int(data[1])

distance = input().split()

min_dist = None

for start in range(cnt_house):
  move_dist = 0
    
  for i in range(cnt_house - 1):
    if start + i == cnt_house - 1:
      move_dist += round - int(distance[start + i]) + int(distance[start + i - cnt_house + 1])
    elif start + i < cnt_house - 1:
      move_dist += int(distance[start + i + 1]) - int(distance[start + i])
    else:
      move_dist += int(distance[start + i - cnt_house + 1]) - int(distance[start + i - cnt_house])
    
    if min_dist is not None and min_dist < move_dist:
      break
  
  if min_dist is None or min_dist > move_dist:
    min_dist = move_dist

print(min_dist)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = deque()
for i in range(n):
    for j in range(1,n):
        if a[j]-a[i] >= abs(a[j]-a[i]-k):
            ans.append(abs(a[j]-a[i]-k))
        else:
            ans.append(a[j]-a[i])
print(max(ans))

Submission status: Time Limit Exceeded

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

cost = 10**9
y = min(a)
for i in range(n):
    cost_r = 0
    cost_l = 0
    r = a[i:] + a[:i]
    l = list(reversed(a[:i+1])) + list(reversed(a[i+1:]))
    for j in range(n-1):
        if r[j+1] < r[j]:
            cost_r += k - r[j]
            if l[j+1] < l[j]:
                cost_l += k - l[j]
                continue
            cost_l += l[j+1] - l[j]
            continue
        if l[j+1] < l[j]:
            cost_l += k - l[j]
            cost_r += r[j+1] - r[j]
            continue
        cost_r += r[j+1] - r[j]
        cost_l += l[j+1] - l[j]
    #print(cost_r,cost_l)
    #print(r,l)
    cost = min(cost,cost_l,cost_r)
print(cost)

Submission status: Time Limit Exceeded

Solution code:
input_first = input()
input_second = input()
first = []
for li in range(2):
    first = input_first.rstrip().split(' ')
n = int(first[1])
for ki in range(n):
    second = input_second.rstrip().split(' ')
Distance = []
for i in range(n):
    Distance.append(abs(int(second[i])-int(second[i-1])))
Distance[0] = int(first[0]) - Distance[0]
print(int(first[0]) - max(Distance))

Submission status: Time Limit Exceeded

Solution code:
k, n = map(int, input().split())
a_list = input().split()  # sはlist

dis_list = []
for i in range(len(a_list)):
    for j in range(len(a_list)-i-1):
        a=a_list[i]
        b=a_list[j+i+1]
        dis=max(int(a)-int(b),int(b)-int(a))
        dis_list.append(min(dis,k-dis))
print(max(dis_list))

Submission status: Time Limit Exceeded

Solution code:
k, n = input().split()
k = int(k)
n = int(n)
a = list(map(int, input().split()))
l = []
m = 100000
for i in range(n-1):
    l.append(abs(a[i] - a[i+1]))
l.append(k - a[n-1] + a[0])
m = 1000
for i in range(n):    
    tmp = 0
    if l[i] >= l[-1]:
        for j in range(i, n):
            tmp += l[j]
        for j in range(i-1):
            tmp += l[j]
    else:
        for j in range(i-1, 0, -1):
            tmp += l[j]
        for j in range(n-1, i, -1):
            tmp += l[j]
            
    if m > tmp:
        m = tmp
print(m)

Submission status: Time Limit Exceeded

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

mindistance = 10000000

for i in range(1, n+1):
    if i == 1:
        distance = k - (a[0] + (k - a[-1]))
        if distance < mindistance:
            mindistance = distance
        a.append(a[0])
        a.pop(0)
    else:
        distance = k - (a[0] - a[-1])
        if distance < mindistance:
            mindistance = distance
        a.append(a[0])
        a.pop(0)

print(mindistance)

Submission status: Time Limit Exceeded

Solution code:
K, N = map(int,input().split())
i = list(map(int, input().split()))
ans = K
for x in range(N):
    list_i = list(map(lambda z: (z - i[x])%K , i))
    ans_p = max(list_i)-min(list_i)
    if ans_p < ans:
        ans = ans_p
print(ans)

Submission status: Time Limit Exceeded

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

distList = []

for idx, dist in enumerate(As):
  if idx == 0:
    distList.append(K - As[-1] + dist)
  else:
    distList.append(dist - As[idx - 1])

min_dist = 0
for idx, dist in enumerate(distList):
  target_dist = 0
  for i in range(N - 1):
    if idx == 0:
      target_dist += distList[idx - i]
      min_dist = target_dist
    else:
      target_dist += distList[idx - i]
  if target_dist < min_dist:
    min_dist = target_dist
print(min_dist)

Submission status: Time Limit Exceeded

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

for i in range(1, n):
    d.append(a[i] - a[i-1])
    
d.append(k - a[-1])
d = tuple(d)

most_min_path = 10000

for i in range(n):
    if i == 0:
        normal_ck = sum(d) - d[i] - d[-1]
        reverse_ck = sum(d) - d[i+1]
    elif i == n - 1:
        normal_ck = sum(d) - d[i]
        reverse_ck = sum(d) - d[i+1] - d[0]
    else:
        normal_ck = sum(d) - d[i]
        reverse_ck = sum(d) - d[i+1]
    if normal_ck > reverse_ck:
        min_ck = reverse_ck
    else:
        min_ck = normal_ck
    
    if most_min_path > min_ck:
        most_min_path = min_ck

print(most_min_path)

Submission status: Time Limit Exceeded

Solution code:
import itertools
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.append(k)
a.reverse()
l = []
for i in range(n):
  l.append(a[i]-a[i+1])
k = []
for i in itertools.permutations(l, r=n-1):
  k.append(sum(i))
print(min(k))

Submission status: Time Limit Exceeded

Solution code:
def permutations(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutations(data):
                ret.append([i] + j)

        return ret

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

result = []

round1 = permutations(a)

for c in round1:
    total = 0
    for b in range(len(c)-1):
        
        
        if c[b] ==0:
            total2 = k - c[b+1]
            total3 =abs(c[b+1] - c[b])
            if total2 <= total3:
                total3 = total2
        else:
            total3 =abs(c[b+1] - c[b])
        
        total = total3 + total
    result.append(total)
print(sorted(result)[0])

Submission status: Time Limit Exceeded

Solution code:
K, N = map(int,input().split())
i = list(map(int, input().split()))
ans = K
for x in range(N):
    list_i = list(map(lambda z: (z - i[x])%K , i))
    if (max(list_i)-min(list_i)) < ans:
        ans = max(list_i)-min(list_i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def minLake(k, a, b):
    result = 0
    for i in range(len(a) - 1):
        result += min(abs(a[i] - a[i + 1]), abs(a[i] - a[i - 1]), min(b))
    return result

k, n = input().split()
a = input().split()
a = list(map(int, a))
a = a[:int(n)]
a = np.array(a)
b = [int(k) - i for i in a]

print(minLake(k, a, b))

Submission status: Time Limit Exceeded

Solution code:
K,N = map(int,input().split())
li = list(map(int,input().split()))
#反時計回りか時計回りの合計距離を、個数出して比較
for i in range(N):#i番目の家を出発地にした時の最小距離を順に求めていく
  iti_t=i#初期位置
  iti_h=i#初期位置
  lon_t=0
  lon_h=0
  for j in range(N-1):#初期位置から進めていく
    if iti_t !=(N-1):
      lon_t+=li[iti_t+1]-li[iti_t]
      iti_t+=1
    else:#位置がN-1のとき
      lon_t+=li[0]+K-li[N-1]
      iti_t=0
    if iti_h !=0:
      lon_h+=li[iti_h]-li[iti_h-1]
      iti_h-=1
    else:#位置が0のとき
      lon_h+=li[0]+K-li[N-1]
      iti_h=N-1
  if i==0:
    res=min([lon_h,lon_t])
  else:
    res=min(res,min([lon_h,lon_t]))
print(res)

Submission status: Time Limit Exceeded

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

dist = []
dist.append([k-a[len(a)-1]+a[0], a[1]-a[0]])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append([a[num]-a[num-1], k-a[num]+a[0]])
  else:
  	dist.append([a[num]-a[num-1], a[num+1]-a[num]])
#print(dist)

start = 0
result = []
while(start!=len(dist)):
  right = 0
  left = 0
  for x in range(0, len(dist)-1):
    n = start+x
    if n > len(dist)-1:
      n = (len(dist) - x - start) * -1
    right += dist[n][1]
  for y in range(0, len(dist)-1):
    n = start-y
    if n < 0:
      n = len(dist)-y
    left += dist[n][0]
  result.append(min(right, left))
#  print(right, left)
  start += 1
  
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
k, n = map(int, input().split())
 
An = list(map(int, input().split()))
 
mn = 0
Amax = max(An)
Amin = min(An)
for i in range(len(An)):
    cal = 0
    A = An[i:]  + An[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
        
        if mn > 0 and cal > mn:
            break
    
    if mn ==0 or mn > cal:
        mn = cal
        
rAn = An[:]
rAn.reverse()
for i in range(len(rAn)):
    cal = 0
    A = rAn[i:]  + rAn[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
            
        if mn > 0 and cal > mn:
            break
 
    if mn ==0 or mn > cal:
        mn = cal
print(mn)

Submission status: Time Limit Exceeded

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

# a = 1000000
# for i in range(len(lists)):
#     if abs(K - lists[i]) == 0:
#         continue
#     elif abs(K - lists[i]) < a:
#         a = abs(K - lists[i])
# b = 1000000
# for i in range(len(lists)):
#     for j in range(len(lists)):
#         if abs(lists[i] - lists[j]) == 0:
#             continue
#         elif abs(lists[i] - lists[j]) < b:
#             b = abs(lists[i] - lists[j])

a = []
for i in range(len(lists)):
    if abs(K - lists[i]) == 0:
        continue
    else:
        a.append(abs(K - lists[i]))
b = []
for i in range(len(lists)):
    for j in range(len(lists)):
        if abs(lists[i] - lists[j]) == 0:
            continue
        else:
            b.append(abs(lists[i] - lists[j]))
a = min(a)
b = min(b)

c = a+b
print(c)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
data = input().split()

round = int(data[0])
cnt_house = int(data[1])

distance = input().split()

min_dist = None

for start in range(cnt_house):
  move_dist = 0
    
  for i in range(cnt_house - 1):
    if start + i == cnt_house - 1:
      move_dist += round - int(distance[start + i]) + int(distance[start + i - cnt_house + 1])
    elif start + i < cnt_house - 1:
      move_dist += int(distance[start + i + 1]) - int(distance[start + i])
    else:
      move_dist += int(distance[start + i - cnt_house + 1]) - int(distance[start + i - cnt_house])
  
  if min_dist is None or min_dist > move_dist:
    min_dist = move_dist

print(min_dist)

Submission status: Time Limit Exceeded

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

dist_min = None
for direction in [True, False]:
    for i in range(len(A)):
        if direction:
            X = A[i:] + A[:i]
        else:
            X = A[:i + 1][::-1] + A[i + 1:][::-1]

        total_distance = sum(
            [
                abs(
                    min(
                        X[i] - X[i + 1],
                        K - (X[i] - X[i + 1])
                    )
                )
                for i in range(len(X) - 1)
            ]
        )

        if dist_min is None or total_distance < dist_min:
            dist_min = total_distance

print(dist_min)

Submission status: Time Limit Exceeded

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

T = K // 2
Ad = []
for a in A:
  Ad.append(a)
  if a > T:
    Ad.append(a - K)

Ad = sorted(Ad)
if DEBUG: print(Ad)

min_dist = None
for i in range(len(Ad) - N + 1):
  dist = 0
  for j in range(N-1):
    k = i + j
    dist += abs(Ad[k] - Ad[k+1])
    if DEBUG: print(k, Ad[k], Ad[k+1], Ad[k] - Ad[k+1])

  if DEBUG: print(dist)
  if min_dist is None or dist < min_dist:
    min_dist = dist

print(min_dist)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def minLake(k, a, b):
    result = 0
    for i in range(len(a) - 1):
        result += min(abs(a[i] - a[i + 1]), abs(a[i] - a[i - 1]), min(b))
    return result

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

print(minLake(k, a, b))

Submission status: Time Limit Exceeded

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

all_length = k*2
point = []
for i in range(n):
    a_left  = k-a[i]
    a_right = k+a[i]
    point.append(a_left)
    point.append(a_right)
point = list(set(point))
point.sort()

num = len(point)
distance = []
for i in range(num-2):
    dis = 0
    for j in range(n-1):
        dis += point[i+j+1] - point[i+j]
    distance.append(dis)
mindis = min(distance)
print(mindis)

Submission status: Time Limit Exceeded

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

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

min_distance = -1
length = len(a_list)
for i in range(length):
    total_distance = 0
    for j in range(length):
        if i == j:
            continue
        elif i < j:
            distance1 = a_list[j] - a_list[i]
            distance2 = a_list[i] - a_list[j] + 20 
        else:
            distance1 = a_list[i] - a_list[j]
            distance2 = a_list[j] - a_list[i] + 20 
           
        total_distance += min(distance1, distance2)
   
    if min_distance > total_distance:
        min_distance = total_distance
    elif min_distance == -1:
        min_distance = total_distance
        
print(min_distance)