Home 백준 ATM (11399)
Post
Cancel

백준 ATM (11399)

백준 ATM(11399번)

그리디 문제이다.

스크린샷 2022-01-31 오후 5 58 19

돈을 인출하는데 가장 적게 걸리는 시간순으로 정렬을 한 후 개인별로 대기해야하는 시간을 계산 해준 후 다 더해서 구하면 끝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.*;  
import java.util.*;  
  
class Time implements Comparable<Time>{  
    int time;  
  
    public Time(int time) {  
        this.time = time;  
    }  
  
    @Override  
    public int compareTo(Time tm) {  
        return (this.time > tm.time) ?  1 : -1;  
    }  
}  
  
  
public class Main {  
  
    static int arr[];  
  
    public static void main(String[] args) throws NumberFormatException, IOException {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));  
  
        int N = Integer.parseInt(br.readLine());  
  
        Time [] arr = new Time[N];  
  
        StringTokenizer st = new StringTokenizer(br.readLine());  
  
        for(int i=0; i<N; i++)  
            arr[i] = new Time(Integer.parseInt(st.nextToken()));  
  
  
        Arrays.sort(arr);  
  
        int sum = 0;  
        int ans = arr[0].time;  
  
        for(int i=0; i<N-1; i++) {  
            sum = arr[i].time + arr[i+1].time;  
            arr[i+1].time = sum;  
            ans += sum;  
        }  
  
  
        bw.write(ans + "\n" );  
        bw.close();  
  
    }  
  
}
This post is licensed under CC BY 4.0 by the author.