問題一覧 > 通常問題

No.370 Road Cleaning

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 266
作問者 : TANIGUCHI Kousuke / テスター : 🍡yurahuna
お気に入りにしたユーザー ProblemId : 780 / 自分の提出
問題文最終更新日: 2026-06-25 01:33:10
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 137 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。