728x90
1. 링크
https://www.acmicpc.net/problem/14241
2. 난이도(solved.ac 참고)
실버2
3. 풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
n = int(sys.stdin.readline()) | |
a = list(map(int, sys.stdin.readline().split())) | |
a.sort(reverse=True) | |
i = 0 | |
total_score = 0 | |
while len(a) != 1: | |
total_score += a[i] * a[i+1] | |
a[i] += a[i+1] | |
del(a[i+1]) | |
print(total_score) |
합치는 두 슬라임을 합한 값이 새로운 슬라임이 되고, 전체 점수는 합치는 두 슬라입을 곱한 값이므로
- 총 점수 값에 (지금 값 * 다음 값)을 넣어준 뒤
- 두 개를 더한 값을 i번째 인덱스에 두고
- i+1번째의 값을 삭제했다.
이렇게 하면 곱한 값은 계속 더해지고, 더한 값은 0번째 인덱스에 계속 업데이트 되면서 그럴 때마다 다음 인덱스가 삭제된다. 결국 while문의 조건에 따라 값이 하나만 남았을 때 종료하고 그 때의 총 점수를 출력함으로써 해결하였다.
'CS > 알고리즘' 카테고리의 다른 글
[Python] BOJ(백준) 1931번 - 회의실 배정 (0) | 2021.06.29 |
---|---|
[Python] BOJ(백준) 11047번 - 동전 0 (0) | 2021.06.29 |
[Python] BOJ(백준) 2217번 - 로프 (+ sys.stdin.readline의 중요성) (0) | 2021.06.28 |
[Python] BOJ(백준) 9237번 - 이장님 초대 (0) | 2021.06.27 |
[Python] BOJ(백준) 11399번- ATM (0) | 2021.06.27 |