Algorithm/[BOJ] - JAVA
[백준] 11399 : ATM JAVA 풀이
Codew
2022. 10. 28. 20:55
그리디 알고리즘의 아주 기초적인 문제였다.
1. 인덱스와 소요시간을 저장하는 클래스 Person을 생성함
2. Person의 ArrayList를 생성한 뒤 갑을 입력해줌
3. ArrayList를 소요시간순으로 정렬함
4. 정렬된 ArrayList를 이용해 소요시간의 누적합을 구하고 출력함
import java.io.*;
import java.util.*;
public class Main {
static class Person{
int idx;
int time;
Person(int idx, int time){
this.idx = idx;
this.time = time;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine()," ");
ArrayList<Person> list = new ArrayList<>();
int total = 0;
for(int i=1;i<=N;i++) {
list.add(new Person(i, Integer.parseInt(st.nextToken())));
}
list.sort((o1, o2)->{
return o1.time-o2.time;
});
for(int i=0;i<N;i++) {
for(int j=0;j<=i;j++) {
total += list.get(j).time;
}
}
System.out.println(total);
}
}