AtCoder Beginner Contest 160
C
Traveling Salesman around Lake
AtCoder
⌀ 62
原题
← BD →
翻译中…

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.

翻译中…

样例

Input #1
20 3
5 10 15
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$.
Input #2
20 3
0 5 15
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$.
题目信息
题号
abc160_c
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。