0 like 0 dislike
2,992 views
| 2,992 views

0 like 0 dislike

Images of ques

Extra test case :

3
5
2 4 5
5 2 4

Video :

by Expert (46,090 points)
edited
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
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 (113,040 points)