Algorithm/문제 풀이
BOJ - ATM
개발자-H
2021. 8. 16. 18:41
https://www.acmicpc.net/problem/11399
11399번: ATM
첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)
www.acmicpc.net
- 그리디 알고리즘이다.
- 정렬 후 누적 값을 출력하면된다.
- 정렬에 PriorityQueue를 사용하였다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
PriorityQueue<Integer> queue = new PriorityQueue<>();
int numOfPeople = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
for(String time : s) {
queue.offer(Integer.parseInt(time));
}
int sum = 0;
int last = 0;
while(!queue.isEmpty()) {
Integer poll = queue.poll();
sum += last + poll;
last += poll;
}
System.out.println(sum);
}
}