Hackerank question how to solve

Here’s my solution-

#include<bits/stdc++.h>
#define ll long long int
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std; 
void solve(){
 	  //input
	  ll n;
	  cin>>n;
	  ll arr[n];
	  ll brr[n];
	  ll i;
	  for(i=0;i<n;i++){
		  cin>>arr[i];
	  }
	  cin>>n;
	  for(i=0;i<n;i++){
		  cin>>brr[i];
	  }
	  ll h;
	  cin>>h;
	  vector<pair<ll,ll>>v;
	  for(i=0;i<n;i++){
		  v.pb({brr[i],arr[i]});
	  }
	  sort(v.rbegin(),v.rend());
	  priority_queue<ll,vector<ll>,greater<ll>>pq;
	  ll x=0;
	  ll y=0;
	  for(auto it:v){
		pq.push(it.second);
		x+=it.second;
		if(pq.size()>h){
			x-=pq.top();
			pq.pop();
	    }
	    y=max(y,x*it.first);  
	  }
	  cout<<y<<endl;
} 
int32_t main()
{	
     fio;
   	  solve();
}

Similar question of leetcode.

1 Like