
์ธ์ค์ด๋ ๋์๊ด์์ ์ผํ๋ค. ๋์๊ด์ ๊ฐ๋ฐฉ์๊ฐ์ด ๋๋์ ์ธ์ค์ด๋ ์ฌ๋๋ค์ด ๋ง๊ตฌ ๋์ ์ฑ ์ ๋ค์ ๊ฐ์ ธ๋ค ๋์์ผ ํ๋ค. ์ธ์ค์ด๋ ํ์ฌ 0์ ์๊ณ , ์ฌ๋๋ค์ด ๋ง๊ตฌ ๋์ ์ฑ ๋ ์ ๋ถ 0์ ์๋ค. ๊ฐ ์ฑ ๋ค์ ์๋ ์์น๊ฐ ์ฃผ์ด์ง ๋, ์ฑ ์ ๋ชจ๋ ์ ์๋ฆฌ์ ๋๋ ๋ ๋๋ ์ต์ ๊ฑธ์ ์๋ฅผ ๊ณ์ฐํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ์ธ์ค์ด๋ ํ ๊ฑธ์์ ์ขํ 1์นธ์ฉ ๊ฐ๋ฉฐ, ์ฑ ์ ์๋ ์์น๋ ์ ์ ์ขํ์ด๋ค. ์ฑ ์ ๋ชจ๋ ์ ์๋ฆฌ์ ๋๋ ํ์๋ ๋ค์ 0์ผ๋ก ๋์์ฌ ํ์๋ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ธ์ค์ด๋ ํ ๋ฒ์ ์ต๋ M๊ถ์ ์ฑ ์ ๋ค ์ ์๋ค.
7 2
-37 2 -6 -39 -29 11 -28
์ฒซ์งธ ์ค์ ์ ๋ต์ ์ถ๋ ฅํ๋ค.
131
์ฐ์ ์ฑ
๋ค์ด 0์ ์์ผ๋ฏ๋ก, ๋ง์ง๋ง์ ์ ์ธํ๊ณ ์๋ M๊ถ์ ๊ฐ์ ธ๋ค ๋๊ณ ๋ค์ 0์ผ๋ก ๋์์์ผ ํฉ๋๋ค.
์ฆ, ๋จ ํ๋ฒ์ ํธ๋๋ก ์ด๋์ด ๊ฐ๋ฅํฉ๋๋ค. ์ต์๋ก ๊ฑธ์ํ๊ธฐ ์ํด์ ์ด ํธ๋๋ฅผ ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ๋ก ์ฌ์ฉํด์ผ ํฉ๋๋ค. ๋ฐ๋ผ์, ์ ๋๊ฐ์ด ๊ฐ์ฅ ํฐ ๊ฐ์ ๋ง์ง๋ง์ผ๋ก ํ์ฌ, ๊ฐ์ฅ ๋จผ ๊ณณ์์ ๋๋ด๊ณ ๋์์ค์ง ์๋ ๊ฒฝ์ฐ๊ฐ ์ต์๋ก ๊ฑธ์ํ๋ ๊ฒฝ์ฐ์ ๋๋ค.
๋๋จธ์ง์ ๊ฒฝ์ฐ๋ ์๋ณต์ผ๋ก ์ด๋ํด์ผ ํ๋ฏ๋ก, ๊ฐ ๊ฑธ์๋ง๋ค ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ * 2 ๋งํผ ๊ฑธ์ํฉ๋๋ค. ์ฆ, ๋งค ์๊ฐ ์ต์ ์ ์ ํ์ธ ์ต๋ํ ๋จผ ๊ณณ์ ๊ฐ์ M๊ถ์ ๋ชจ๋ ์ฒ๋ฆฌํ๊ณ ๋์์ค๋ ๋ฐฉ์์ ํํฉ๋๋ค.
๋ง์ฝ ๋จผ ๊ณณ๋ถํฐ ์ฒ๋ฆฌํ์ง ์์ผ๋ฉด, ๋ฉ๋ฆฌ ๋ ๋ฒ ๊ฐ์ผํ๋ ๋นํจ์จ์ด ์๊น๋๋ค. ๋ ํ ๋ฒ์ ์ต๋ํ ๋ง์ด ์ฑ ์ ๊ฐ์ ธ๋ค ๋์์ผ ์๋ณต ํ์๋ฅผ ์ต์ํํด์ ๊ฑฐ๋ฆฌ๋ฅผ ์ต์ํํ ์ ์์ต๋๋ค. ์ด ๋ ๊ฐ์ง ์ ํ ์กฐ๊ฑด์ด ์ต์ ์ ํ๋จ์ด๊ณ ์ด๋ฌํ ์ ๋ต์ด ๊ทธ๋ฆฌ๋์ ๋๋ค.
์ด์ ๋ฐ๋ผ ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ , ๊ทธ๋ฆฌ๋๋ฅผ ์ํ ์ ์ฒ๋ฆฌ๋ก ๊ฑฐ๋ฆฌ ์์ผ๋ก ์์น๋ฅผ ์ ๋ ฌํด์ผ ํฉ๋๋ค. ์ฑ ์์น๋ฅผ ์์๊ฐ๊ณผ ์์๊ฐ์ผ๋ก ๋๋๋๋ค. ์ด์ ๋ ์ ๋ ฅ ์๋ฅผ ๋ค์ด ์ค๋ช ํ๊ฒ ์ต๋๋ค.
// ์ฐ์ ์์น๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด ๋ณด๊ฒ ์ต๋๋ค. [-39, -37, -29, -28, -6, 2, 11]
const locations = input[1].split(' ').map(Number).sort((a, b) => a - b);
๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ์ ํธ๋ ์ฒ๋ฆฌ๋ฅผ ์๊ฐํ์ง ์๊ณ , ์์น ์์๋๋ก ๋ค๋
์ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.

์์น๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ 2๊ถ์ฉ ๋๊ณ ์จ๋ค๊ณ ํ์ ๋, -6 ๋ถ๋ถ์์๋ 2๊ถ์ ๊ฐ์ ธ๋ค ๋์ ์ ์์ต๋๋ค. ๋ค์ ์ฑ
์์น์ธ 2 ๋ ์์๋ก ๋ฐ๋๋ฏ๋ก 0์ ์ง๋ 2๋ฅผ ์๋ก ๊ฐ์ผํฉ๋๋ค. ์ด๋ฅผ ๊ณ ๋ คํ๊ธฐ ์ํด ์์น๋ฅผ ์์๊ฐ๊ณผ ์์๊ฐ์ผ๋ก ๋๋์ด ์๊ฐํฉ๋๋ค.
์ด ๋, ์์๋ ์ค๋ฆ์ฐจ์์ผ๋ก, ์์๋ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ผ ๊ฐ์ฅ ๋จผ ๊ฑธ์ ์์ผ๋ก ์ ๋ ฌ๋ฉ๋๋ค.
const locations = input[1].split(' ').map(Number);
// ์์/์์ ์์น๋ก ๋๋๊ณ ์ ๋ ฌ
const negativeBooks = locations.filter(x => x < 0).sort((a, b) => a - b); // ์์ ์์: [-39, -37, -29, -28, -6]
const positiveBooks = locations.filter(x => x > 0).sort((a, b) => b - a); // ํฐ ์์: [11, 2]
๊ทธ๋ฆฌ๊ณ ์ด์ ๊ฐ๊ฐ์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋๋ฉด์ ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ๋ถํฐ M๊ถ์ฉ ๊ฐ์ ธ๋ค ๋์ผ๋ฉฐ ๊ฑธ์ ์๋ฅผ ์ธ์ด์ค๋๋ค.(๊ทธ๋ฆฌ๋ ์ ๋ต)
const distances = [];
// ๊ฐ ๋ฐฉํฅ์์ M๊ถ์ฉ ๋ฌถ์ด ๊ฐ์ฅ ๋ฉ๋ฆฌ ์๋ ๊ฑฐ๋ฆฌ๋ง ๊ธฐ๋ก
for (let i = 0; i < negativeBooks.length; i += M) {
distances.push(Math.abs(negativeBooks[i]));
}
for (let i = 0; i < positiveBooks.length; i += M) {
distances.push(positiveBooks[i]);
}
๊ฑธ์ ์์ ๋ฐฐ์ด์์ ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ๋ง ํธ๋๋ก ์ด๋ํ๊ณ ๋๋จธ์ง๋ ์๋ณต์ผ๋ก ์ด๋ํ๋๋ก ์ฒ๋ฆฌํฉ๋๋ค.
const farthest = Math.max(...distances);
// ์ ๋ถ 2๋ฅผ ๊ณฑํด์ ์๋ณต์ผ๋ก ์ฒ๋ฆฌํ๊ณ max๊ฐ์ ๋นผ์ ํธ๋๋ก ์ฒ๋ฆฌํฉ๋๋ค.
const totalDistance = distances.reduce((sum, d) => sum + d * 2, 0) - farthest;
๋ค ์ ๊ณ ๋ณด๋ ๊ฑฐ๋ฆฌ ๋์ ์ ๋ฐ๋ก ๊ณ์ฐํ ํ์๊ฐ ์์ด ๋ณด์ ๋๋ค. ๋ฐ๋ก๋ฐ๋ก ๊ณ์ฐํ๋๋ก ๋ฆฌํฉํ ๋งํด ๋ณด๊ฒ ์ต๋๋ค.
let totalDistance = 0;
let farthest = 0;
for (let i = 0; i < negativeBooks.length; i += M) {
const distance = Math.abs(negativeBooks[i]);
totalDistance += distance * 2;
// ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ฉด์ ๋น๊ตํ์ฌ ์ต๋๊ฐ๋ ์ฐพ์ต๋๋ค.
if (distance > farthest) farthest = distance;
}
for (let i = 0; i < positiveBooks.length; i += M) {
const distance = positiveBooks[i];
totalDistance += distance * 2;
// ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ฉด์ ๋น๊ตํ์ฌ ์ต๋๊ฐ๋ ์ฐพ์ต๋๋ค.
if (distance > farthest) farthest = distance;
}
// ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ ๊ฐ์ ํธ๋๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด ํ ๋ฒ ๋บด์ค๋๋ค.
totalDistance -= farthest;
console.log(totalDistance);
์ฑ๋ฅ์ ์ด์ ์ ์์ง๋ง, ๋ฆฌํฉํ ๋ง์ผ๋ก ์ข ๋ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ก์ต๋๋ค.
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [N, M] = input[0].split(' ').map(Number);
const locations = input[1].split(' ').map(Number);
const negativeBooks = locations.filter(x => x < 0).sort((a, b) => a - b); // ์ผ์ชฝ ์ฑ
: ์์ ์์
const positiveBooks = locations.filter(x => x > 0).sort((a, b) => b - a); // ์ค๋ฅธ์ชฝ ์ฑ
: ํฐ ์์
let totalDistance = 0;
let farthest = 0;
for (let i = 0; i < negativeBooks.length; i += M) {
const distance = Math.abs(negativeBooks[i]);
totalDistance += distance * 2;
if (distance > farthest) farthest = distance;
}
for (let i = 0; i < positiveBooks.length; i += M) {
const distance = positiveBooks[i];
totalDistance += distance * 2;
if (distance > farthest) farthest = distance;
}
totalDistance -= farthest;
console.log(totalDistance);