Home 백준 보석도둑 (1202)
Post
Cancel

백준 보석도둑 (1202)

백준 보석도둑 (1202)

1

  • 가방의 무게가 낮은 순으로 정렬
  • 보석의 무게가 낮은 순으로 정렬
  • 가방에 담을 수 있는 보석들을 우선순위 큐에 집어 넣는다 (보석 가격 높은 순)
  • 가방을 다 사용했으면 가방의 수 만큼 보석을 꺼내서 더해준다.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.*;
import java.util.*;

class FastScanner {
	BufferedReader br;
	StringTokenizer st;
	
	public FastScanner() {
		br = new BufferedReader(new InputStreamReader(System.in));
	}
	
	String next() {
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}
	
	int nextInt() {
		return Integer.parseInt(next());
	}
	
	long nextLong() {
		return Long.parseLong(next());
	}
}

class Jewel implements Comparable<Jewel>{
	int weight;
	int price;
	
	public Jewel(int weight, int price) {
		this.weight = weight;
		this.price = price;;
	}
	
	@Override
	public int compareTo(Jewel other) {
		return this.weight - other.weight;
	}
}


public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		FastScanner sc = new FastScanner();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int jewel_num = sc.nextInt();
		int bag_num = sc.nextInt();
		Jewel [] jewel = new Jewel[jewel_num];
		int [] bag = new int[bag_num];
		
		for(int i=0; i<jewel_num; i++)
			jewel[i] = new Jewel(sc.nextInt(), sc.nextInt());
		
		for(int i=0; i<bag_num; i++)
			bag[i] = sc.nextInt();
		
		// 내림차순 
		Arrays.sort(jewel);
		Arrays.sort(bag);
		
		// 높은 숫자 우선순위 
		Queue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());	// 가방의 무게보다 같거나 작은 보석들을 담기 위한 큐
		
		long sum = 0;
		
		int idx = 0;
	
		for(int weight : bag) {
			
			for(; idx < jewel_num; ) {
				
				if(weight >= jewel[idx].weight) 
					queue.add(jewel[idx++].price);
				else break;
			}
			
			if(!queue.isEmpty()) 	// 가방의 무게보다 더 무거운 보석들만 있는지 체크
				sum+= queue.poll();	// 넣을때 큰 가격부터 꺼내기 위해 음수로 넣는다.
			
			
		}
		
		
		bw.write(Long.toString(sum));
		bw.close();

			
		}	
}

This post is licensed under CC BY 4.0 by the author.