Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,555 views
All past online assesments of Virtusa can be found using the tag "virtusa_oa" in the search bar.

Here is the link : https://www.desiqna.in/tag/virtusa_oa
in Online Assessments by Expert (34,270 points) | 1,555 views

1 Answer

0 like 0 dislike

 

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ; 
int main() 
{
    vector<vector<ll>> stations ; 
    ll n ;
    cin>>n ; 
    ll t,s ; 
    cin>>t>>s ; 
    ll i = 0 ;
    while(i<n)
    {
        ll x,y;
        cin>>x>>y ; 
        ll real = t - x ; 
        if(real>=1)
        {
            vector <ll> a ;
            a.push_back(real);
            a.push_back(y);
            stations.push_back(a);
        }
        i++;
    }
    
     priority_queue<ll> pq;
        
        i=0;
        n=stations.size();
        ll ans=0;
        while(s<t && i<n)
        {
                if(s>=stations[i][0])
                {
                        pq.push(stations[i][1]);
                        i++;
                }
                else
                {
                        if(pq.empty())
                        {
                                return -1;
                        }
                        else
                        {
                                ans++;
                                s+=pq.top();
                                pq.pop();
                        }
                }
                
        }
        while(!pq.empty() && s<t)
        {
                s+=pq.top();
                pq.pop();
                ans++;
        }
        
        if(s>=t)
        {
                cout<<ans;
        }
        else
        {
        cout<<"-1";}
    
    
    
    return 0 ; 
}

by Expert (34,270 points)