# 도서관
# https://www.acmicpc.net/problem/1461 (탐욕)
def run(M, books):
books.sort() # ((먼 책 순서로 선택해서)) 걸음수를 계산한다
books_right = [x for x in books if x > 0]
books_left = [x for x in books if x < 0]
max_walk = max(min(books) * (-1), max(books))
walk = 0
while books_right:
walk += books_right.pop() * (2)
for _ in range(M - 1):
if books_right:
books_right.pop()
while books_left:
walk += books_left.pop(0) * (-2)
for _ in range(M - 1):
if books_left:
books_left.pop(0)
return walk - max_walk
N, M = map(int, input().split())
books = list(map(int, input().split()))
print(run(M, books))
'BOJ 알고리즘 (패캠) > 탐욕, 백트래킹' 카테고리의 다른 글
9663번: N-Queen (백트래킹) (0) | 2020.10.25 |
---|---|
1781번: 컵라면 (탐욕) (0) | 2020.10.24 |
2212번: 센서 (탐욕) (0) | 2020.10.23 |
1092번: 배 (탐욕) (0) | 2020.10.23 |
2012번: 등수 매기기 (탐욕) (0) | 2020.10.23 |