설명

첫 번째 줄에 최대로 벌 수 있는 수입을 출력한다.

 

 

 

 

풀이

int j=0을 밖으로 빼준다. 그래야지 계속 증가한다.

import java.util.*;
class Lecture implements Comparable<Lecture>{
    public int money;
	public int time;
    Lecture(int money, int time) {
        this.money = money;
        this.time = time;
    }
    @Override
    public int compareTo(Lecture ob){
        return ob.time-this.time;
    }
}

class Main {
	static int n, max=Integer.MIN_VALUE;

	public void solution(ArrayList<Lecture> arr) {
		int sum=0;
		PriorityQueue<Integer> pq= new PriorityQueue<>(Collections.reverseOrder());
		Collections.sort(arr);
		
		int j=0;
		for(int i=max; i>0; i--) {
			for( ; j<arr.size(); j++) {
				if(arr.get(j).time<i) break;
				pq.add(arr.get(j).money);
			}
			if(!pq.isEmpty()) {
				sum+=pq.poll();
			}
		}
		System.out.println(sum);
		
	}
	
	public static void main(String[] args){
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		n=kb.nextInt();
		
		ArrayList<Lecture> arr = new ArrayList<>();
		
		for(int i=0; i<n; i++){
			int m=kb.nextInt();
			int d=kb.nextInt();
			arr.add(new Lecture(m, d));
			if(d>max) {max=d;}
		}
		T.solution(arr);
	}
}

 

'알고리즘기초 > Greedy' 카테고리의 다른 글

03. 결혼식  (0) 2022.10.01
02. 회의실배정  (0) 2022.10.01
01. 씨름선수  (0) 2022.10.01

+ Recent posts