데드라인을 첫번째 , 컵라면 수를 두번째 우선순위로 해서 오름차순으로 정렬한다. 그리고 그리디하게 매초마다 최대의 컵라면 수를 얻기 위한 선택을 하면 된다.
이 때 주의해야 할 점이
ex) 5
3 1
3 1
3 1
4 20
4 20
=> 최대 컵라면을 얻기 위해서는 (3 1) (3 1) (4 20) (4 20) => 42개가 최대가 된다. ((3 1) (3 1) (3 1) (4 20) => 23 X)
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int>> v;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
v.push_back({ a,b });
}
sort(v.begin(), v.end());
int ans = 0;
int sec = 0;
priority_queue<int,vector<int>,greater<int>> q;
for (int i = 0; i < v.size(); i++) {
auto cur = v[i];
if (cur.first > sec) {
q.push(cur.second);
sec++;
}
else if (cur.first == sec) {
if (q.top() < cur.second) {
q.pop();
q.push(cur.second);
}
}
}
while (!q.empty()) {
ans += q.top();
q.pop();
}
cout << ans << '\n';
}