반응형
https://www.acmicpc.net/problem/1461
import bisect
n, m = map(int, input().split())
books = list(map(int, input().split()))
books.sort()
i = bisect.bisect_left(books, 0)
negatives = books[:i]
positives = books[i:]
answer = 0
if negatives:
pos = 0
while pos <= len(negatives)-1:
if pos == 0:
answer += (books[pos] * -1)
else:
answer += (books[pos] * -2)
pos += m
if positives:
pos = len(positives) - 1
while pos >= 0:
if pos == len(positives) - 1:
answer += positives[pos]
else:
answer += positives[pos] * 2
pos -= m
if negatives and positives:
answer += min(positives[-1], -negatives[0])
print(answer)
댓글