240713 보석 도둑

Jongleee·2024년 7월 13일
0

TIL

목록 보기
624/737
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer token = new StringTokenizer(br.readLine(), " ");
	int n = Integer.parseInt(token.nextToken());
	int k = Integer.parseInt(token.nextToken());

	PriorityQueue<Jewel> jewelQueue = new PriorityQueue<>(new Comparator<Jewel>() {
		@Override
		public int compare(Jewel o1, Jewel o2) {
			return o1.mass - o2.mass;
		}
	});

	PriorityQueue<Integer> valueQueue = new PriorityQueue<>(new Comparator<Integer>() {
		@Override
		public int compare(Integer o1, Integer o2) {
			return o2 - o1;
		}
	});

	for (int i = 0; i < n; i++) {
		token = new StringTokenizer(br.readLine(), " ");
		int mass = Integer.parseInt(token.nextToken());
		int value = Integer.parseInt(token.nextToken());
		jewelQueue.offer(new Jewel(mass, value));
	}

	int[] bags = new int[k];
	for (int i = 0; i < k; i++) {
		bags[i] = Integer.parseInt(br.readLine());
	}
	Arrays.sort(bags);

	long totalValue = 0;
	for (int i = 0; i < k; i++) {
		int currentBag = bags[i];
		while (!jewelQueue.isEmpty() && jewelQueue.peek().mass <= currentBag) {
			valueQueue.offer(jewelQueue.poll().value);
		}
		if (!valueQueue.isEmpty()) {
			totalValue += valueQueue.poll();
		}
	}
	System.out.println(totalValue);
}

static class Jewel {
	int mass;
	int value;

	public Jewel(int mass, int value) {
		this.mass = mass;
		this.value = value;
	}
}

출처:https://www.acmicpc.net/problem/1202

0개의 댓글