No.370 Road Cleaning
タグ : / 解いたユーザー数 266
作問者 :
TANIGUCHI Kousuke
/ テスター :
🍡yurahuna
Problem Statement
Today is the school's off-campus cleanup day, and each person is supposed to pick up at least $N$ pieces of trash lying on the road near the school.
Kenta is initially at position $0$. Given the positions $D_i$ of $M$ pieces of trash,
find the minimum travel distance needed to pick up $N$ pieces of trash.
Input
$N\ M$ $D_1$ $D_2$ ... $D_m$
$ 1 \le N \le M \le 1000$
$ -10000 \le D_i \le 10000 $
For $ i \ne j $, $ D_i \ne D_j $.
Output
Output the minimum travel distance Kenta needs to pick up $N$ pieces of trash.
Sample
Sample 1
Input
3 5 1 2 3 4 5
Output
3
Pick up the trash at positions 1, 2, 3 in order.
Sample 2
Input
2 4 -5 -1 2 5
Output
4
The shortest way is to first pick up the trash at position -1, then turn back and pick up the trash at position 2.
Sample 3
Input
2 5 -3 -1 0 2 4
Output
1
There may be trash at the initial position 0. In this case it can be picked up without moving, so you then pick up the trash at -1 and finish.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。