|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +public class Main{ |
| 7 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + static StringTokenizer st; |
| 10 | + static StringBuilder sb = new StringBuilder(); |
| 11 | + static int N,K; |
| 12 | + static PriorityQueue<long[]> in; |
| 13 | + static PriorityQueue<long[]> out; |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + N = Integer.parseInt(st.nextToken()); |
| 18 | + K = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + in = new PriorityQueue<>((a,b) -> { |
| 21 | + if(a[0] == b[0]){ //시간이 같으면 |
| 22 | + return Long.compare(a[1], b[1]); //적은 번호에 해당하는 곳 |
| 23 | + }else{ |
| 24 | + return Long.compare(a[0], b[0]); |
| 25 | + } |
| 26 | + }); |
| 27 | + out = new PriorityQueue<>((a,b) -> { |
| 28 | + if(a[0] == b[0]){ //시간이 같으면 |
| 29 | + return Long.compare(b[1], a[1]); //큰 번호에 해당하는 곳 |
| 30 | + }else{ |
| 31 | + return Long.compare(a[0], b[0]); |
| 32 | + } |
| 33 | + }); |
| 34 | + for (int i = 1; i <= K; i++) { |
| 35 | + in.offer(new long[]{0,i}); |
| 36 | + } |
| 37 | + for (int i = 0; i < N; i++) { |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + int num = Integer.parseInt(st.nextToken()); |
| 40 | + int time = Integer.parseInt(st.nextToken()); |
| 41 | + long[] insert = in.poll(); |
| 42 | + insert[0] += time; |
| 43 | + in.offer(insert); |
| 44 | + out.offer(new long[]{insert[0],insert[1],num}); |
| 45 | + } |
| 46 | + |
| 47 | + long ans = 0; |
| 48 | + for (int i = 1; i <= N; i++) { |
| 49 | + ans += i*out.poll()[2]; |
| 50 | + } |
| 51 | + bw.write(ans+""); |
| 52 | + bw.close(); |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | +``` |
0 commit comments