https://www.acmicpc.net/problem/11399
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B11399 {
public static void main(String[] args)throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(bf.readLine());
int[] time=new int[N];
StringTokenizer st = new StringTokenizer(bf.readLine());
for(int i=0;i<N;i++){
time[i]=Integer.parseInt(st.nextToken());
}
Arrays.sort(time);
int answer=0;
for(int i=0;i<N;i++){
answer+=time[i]*(N-i);
}
System.out.println(answer);
bf.close();
}
}
내가 푼 방식이 그리디인지는 모르겠는데 아무튼 가장 적게 기다려서 하려면 짧게 거리는 사람들이 먼저 해야하기 때문에 정렬하여 합산하는 방식으로 하였다.
'JAVA' 카테고리의 다른 글
구름톤 챌린지 1일차 (0) | 2023.08.14 |
---|---|
[리트코드/자바] 1732.find the highest altitude (0) | 2023.07.20 |
[리트코드/자바] 1431.kids with the greatest number of candies (0) | 2023.07.17 |
[리트코드/자바] Merge Strings Alternately (0) | 2023.07.16 |
[프로그래머스/자바] Lv.0 삼각형의 완성조건(2) (0) | 2023.07.14 |
댓글