[Java] 백준 과제

정석·2024년 9월 17일

알고리즘 학습

목록 보기
67/67
post-thumbnail

문제

풀이

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		StringTokenizer st;
		List<int[]> list = new ArrayList<>();
		for(int i=0; i<n; i++) {
			st = new StringTokenizer(br.readLine());
			int d = Integer.parseInt(st.nextToken());
			int w = Integer.parseInt(st.nextToken());
			
			list.add(new int[] {d,w});
		}
		
		Collections.sort(list, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return o2[1]-o1[1];
			}
		});
		
		int[] check = new int[1001];
		for(int[] p : list) {
			for(int i=p[0]; i>0; i--) {
				if(check[i]==0) {
					check[i] = p[1];
					break;
				}
			}
		}
		
		int total =0;
		for(int num : check) {
			total += num;
		}
		System.out.println(total);
	}
}
  • 다음에 다시 풀어보기로

0개의 댓글