์•Œ๊ณ ๋ฆฌ์ฆ˜ ๐Ÿ“š/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1461. ๋„์„œ๊ด€ | ํŒŒ์ด์ฌ

leejaejae 2024. 7. 19. 16:00

๋ฐฑ์ค€ 1461. ๋„์„œ๊ด€ - ๊ณจ๋“œIV

๋ฌธ์ œ ์„ค๋ช…

์ผ์ง์„  ์ขŒํ‘œ๊ฐ€ ์žˆ๊ณ , 0 ์œ„์น˜์— ์–ด์งˆ๋Ÿฌ์ง„ ์ฑ…๋“ค์„ ๊ฐ๊ฐ ์›๋ž˜ ์ž๋ฆฌ์— ๊ฐ€์ ธ๋‹ค ๋‘ฌ์•ผ ํ•˜๋Š”๋ฐ ์ตœ์†Œํ•œ์œผ๋กœ ์›€์ง์—ฌ์•ผ ํ•˜๊ณ , ์ตœ์†Œํ•œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ

- ํ˜„์žฌ ์œ„์น˜: 0
- ํ•œ๋ฒˆ์— m๊ถŒ



ํŒŒ์ด์ฌ ์ฝ”๋“œ

n, m = map(int, input().split())
books = list(map(int, input().split()))

pos_li = []
neg_li = []
furthest = 0
result = 0

for book in books:
    furthest = max(abs(book), furthest)
    if book > 0:
        pos_li.append(book)
    else:
        neg_li.append(abs(book))

pos_li.sort(reverse=1)
neg_li.sort(reverse=1)

for i in range(0, len(pos_li), m):
    result += pos_li[i] * 2

for i in range(0, len(neg_li), m):
    result += neg_li[i] * 2

print(result - furthest)