Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
2,365 views
in Online Assessments by Expert (46,090 points) | 2,365 views

3 Answers

0 like 0 dislike
Best answer

Images of ques

Extra test case : 

3
5
2 4 5 
5 2 4 

 

Video :

imageimageimageimageimage

 

by Expert (46,090 points)
edited by
0 like 0 dislike
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#define lli long long int

using namespace std;

void solve(){
   int n;
   cin >> n;
   
   vector<lli>a(n);
   vector<lli>b(n);

   for(int i = 0 ; i < n ; i++){
     cin >> a[i];
   }
   for(int i = 0; i < n ; i++){
     cin >> b[i];
   }

   int k;
   cin >> k;
   
   vector<pair<int,int>>temp;

   for(int i = 0; i < n ; i++){
       temp.push_back({b[i],a[i]});
   }

   sort(temp.begin(),temp.end());
   
   int sum = 0;

   for(int i = 0 ; i < n ; i++){
     if(k >= temp[i].second){
        sum += (temp[i].first * temp[i].second);
        k -= temp[i].second;
     }
     else if(k < temp[i].second){
        sum += (temp[i].first * k);
        k = 0;
     }
     if(k <= 0){
        break;
     }
   }

   cout << sum << "\n";
}
by (140 points)
edited by
0 0
Great Solution!!
0 like 0 dislike

Code C++ : 

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll ; 

int main() {
    
    ll n;
    cin>>n ; 
    ll k;
    cin>>k;
    ll v[n+1]={0};
    ll b[n+1]={0};
    
    ll i = 1 ; 
    while(i<=n){
        cin>>v[i];
        i++;
    }
    
    i = 1 ; 
    while(i<=n){
        cin>>b[i];
        i++;
    }
    
    vector <pair<ll,ll>> pr ; 
    
    i = 1 ; 
    while(i<=n){
        
        pr.push_back({b[i],v[i]});
      
        i++;
    }
    
    
    sort(pr.begin(),pr.end());
    
    
    reverse(pr.begin(),pr.end());
    
    
    ll sum = 0 ; 
    i = 0 ; 
    while(i<=n-1){
        
        cout<<pr[i].first<<" "<<pr[i].second;
        cout<<"\n";
        //sum+=(stor[i]);
        
        if(k>=pr[i].second){
            sum +=(pr[i].first*pr[i].second);
            k = k - pr[i].second ;
          //  pr[i].second = 0 ; 
         
        } else {
            
            
            sum +=(pr[i].first*k);
            k = 0 ; 
            i = 1e18 ; 
        }
        
        
        
        i++;
    }
    
  //  cout<<sum ;
    
    
    
    
     cout<<sum ; 
    
    
    return 0 ; 
}
by Expert (111,530 points)