
➤ Problem Link : MSCHED
👉 Hint : Sort and apply Greedy Algorithm
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
bool comp(pair<int,int> a,pair<int,int> b)
{
if(a.first!=b.first)
return a.first>b.first;
return a.second<b.second;
}
int main()
{
int n,g,d;
long long int ans=0;
cin>>n;
bool arr[n+1];
memset(arr,0,sizeof(arr));
vector<pair<int,int> > v;
for(int i=0;i<n;i++)
{
cin>>g>>d;
v.push_back(make_pair(g,d));
}
sort(v.begin(),v.end(),comp);
for(int i=0;i<n;i++)
{
d=v[i].second;
if(d>n)
d=n;
for(int j=d;j>0;j--)
if(!arr[j])
{
arr[j]=1;
ans+=v[i].first;
break;
}
}
cout<<ans<<endl;
}
I am a Member Technical at D.E. Shaw India Pvt. Ltd. CodingWithArt is a blog where you find tricks and solutions to challenging coding and development problems. I aim to build a universal tech platform for every enthusiast out there.