#include <bits/stdc++.h>

using namespace std;

#define ll long long int

ll space_req{0}, no_mov, result{0};

ll most_rating(vector &space, vector &rating, ll space_req, ll no_mov);

int main()

{

``````int t{0};

cin >> t;

while (t > 0)

{

cin >> no_mov >> space_req;

vector<ll> space(no_mov, 0), rating(no_mov, 0);

for (ll i = 0; i < no_mov; i++)

{

cin >> space[i] >> rating[i];

}

result = most_rating(space, rating, space_req, no_mov);

cout << result << endl;

t--;

}

return 0;
``````

}

ll most_rating(vector &space, vector &rating, ll space_req, ll no_mov)

{

``````ll max_rating = *max_element(rating.begin(), rating.end());

ll temp{0}, temp2{0};

for (ll i = 0; i < no_mov; i++)

{

if (max_rating == rating[i])

{

temp = i;

}

}

if (space[temp] <= space_req)

{

return rating[temp];

}

else

{

for (ll i = 0; i < no_mov; i++)

{

if (space[i] == space_req)

{

temp2 = i;

}

}

for (ll i = 0; i < no_mov; i++)

{

if (((rating[i]) >= (rating[temp2])) && (space[temp2] >= space[i]))

{

return rating[i];

}

}

}
``````

}

Why you have complicated the solution. Just select the maximum value of IMDB rating and checking space requirements while taking user input and that is the answer.

Thanks