Problem statement
There is a circular lake of $ K $ m per lap, around which there are $ N $ houses. The $ i $ th house is located $ A_i $ meters clockwise from the northern end of the lake. You can only move between houses along the circumference of the lake. Find the shortest travel distance to start from one of the homes and visit all N homes.
C - Traveling Salesman around Lake
Houses can be thought of as being lined up on the circumference, so the distance between adjacent houses should be calculated and the sum of those excluding the smallest one should be taken.
K, N = map(int,input().split())
A = list(map(int, input().split()))
li = []
for i in range(N - 1):
li.append(A[i + 1] - A[i]) #Add the difference between the elements of array A to li
li.append(abs(A[0] + (K - A[-1]))) #Only the first and last terms of the array are added separately
print(sum(li) - max(li))
Recommended Posts