[백준] 13164 - 행복 유치원 / Python / 골드 5

KimYoungWoong·2022년 10월 2일
0

BOJ

목록 보기
15/31
post-thumbnail

🚩문제 주소


📄풀이


i번째 원생과 i+1번째 원생의 키 차이를 저장한 배열을 내림차순으로 정렬해줍니다.

이 배열을 작은 순서대로 k만큼 남게 슬라이싱을 해준 후, sum을 이용해 전부 더해준 뒤 출력합니다.



👨‍💻코드


n, k = map(int, input().split())
people = list(map(int, input().split()))
diff = sorted([people[i+1]-people[i] for i in range(0, n-1)], reverse=True)

answer = diff[k-1:]
print(sum(answer))

profile
블로그 이전했습니다!! https://highero.tistory.com

0개의 댓글