AmazonOA #177
Find Minimum Time
Estimated MediumJudge-ready
Topics (AI-labelled)Two PointersGreedy
There is one apple at each of several distinct integer coordinates. Starting from 0, you move 1 unit per second and may turn around at any time; an apple is eaten as soon as you reach it. Find the shortest time to eat at least k apples.
Input
The first line contains n k; the second line contains the coordinates. 1≤n≤100000; 1≤k≤n; coordinates are in −10^8..10^8 and pairwise distinct.
Output
Output a single integer, the answer.
Example 1
Input:
3 3
-20 5 10
Output:
40
Example 2
Input:
2 1
0 5
Output:
0
Example 3
Input:
3 2
-9 -3 4
Output:
9
Example notes
The answers are 40, 0 and 9.
Editorial
Loading solution…