/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君は、駐車場の管理人をしています。この駐車場は一直線に伸びた数直線上にあり、座標 0 から座標 M までの範囲が駐車可能エリアとなっています。
現在、 N 台の車が駐車されており、 i 番目の車は座標 A_i の位置にあります。しかし、いくつかの車は駐車可能エリアの外に停められてしまっている可能性があります。
高橋君は、すべての車を駐車可能エリア内(座標 0 以上 M 以下)に移動させる必要があります。各車は他の車とは独立に移動させることができ、 1 台の車を距離 1 だけ移動させるのに 1 円のコストがかかります。すなわち、座標 a にある車を座標 b に移動させるコストは |a - b| 円です。
すべての車を座標 0 以上 M 以下の範囲内に収めるために必要な最小の合計コストを求めてください。
ただし、移動前・移動後ともに、複数の車が同じ座標に存在することは許されます。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^9
- -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数である
入力
N M A_1 A_2 \ldots A_N
- 1 行目には、車の台数を表す整数 N と、駐車可能エリアの上限座標を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各車の現在の座標を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
すべての車を座標 0 以上 M 以下の範囲内に収めるために必要な最小の合計コストを 1 行で出力せよ。
入力例 1
3 10 -2 5 12
出力例 1
4
入力例 2
4 5 0 2 5 3
出力例 2
0
入力例 3
10 100 -200 -50 -1 0 20 60 99 100 101 150
出力例 3
302
入力例 4
50 1000 -5000 -1000 -1 0 1 2 10 50 200 500 999 1000 1001 1200 1500 2000 3000 10000 123 456 789 100 900 250 750 333 666 111 222 444 -250 -750 -333 -666 -111 -222 -444 0 1000 -1 1001 500 1500 500000000 -500000000 1000000000 -1000000000 999 1 1002
出力例 4
3000019982
入力例 5
1 1000000000 -1000000000
出力例 5
1000000000
Score : 233 pts
Problem Statement
Takahashi is a parking lot manager. The parking lot lies on a straight number line, and the parkable area ranges from coordinate 0 to coordinate M.
Currently, N cars are parked, and the i-th car is at coordinate A_i. However, some cars may have been parked outside the parkable area.
Takahashi needs to move all cars into the parkable area (coordinates between 0 and M, inclusive). Each car can be moved independently of the others, and it costs 1 yen to move one car a distance of 1. That is, the cost of moving a car from coordinate a to coordinate b is |a - b| yen.
Find the minimum total cost required to move all cars into the range of coordinates 0 or greater and M or less.
Note that multiple cars are allowed to occupy the same coordinate, both before and after moving.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^9
- -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N M A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of cars and an integer M representing the upper limit coordinate of the parkable area, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the current coordinates of each car, separated by spaces.
Output
Print on one line the minimum total cost required to move all cars into the range of coordinates 0 or greater and M or less.
Sample Input 1
3 10 -2 5 12
Sample Output 1
4
Sample Input 2
4 5 0 2 5 3
Sample Output 2
0
Sample Input 3
10 100 -200 -50 -1 0 20 60 99 100 101 150
Sample Output 3
302
Sample Input 4
50 1000 -5000 -1000 -1 0 1 2 10 50 200 500 999 1000 1001 1200 1500 2000 3000 10000 123 456 789 100 900 250 750 333 666 111 222 444 -250 -750 -333 -666 -111 -222 -444 0 1000 -1 1001 500 1500 500000000 -500000000 1000000000 -1000000000 999 1 1002
Sample Output 4
3000019982
Sample Input 5
1 1000000000 -1000000000
Sample Output 5
1000000000