CCCPaste

Untitled

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1005;

int main() {
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    
    while(t--) {
        int n;
        cin >> n;
        int result = 0;
        vector dist(n), gas(n);
        for(int i = 0; i < n; i++) {
            cin >> dist[i] >> gas[i];
        }
        int city_distance, fuel;
        cin >> city_distance >> fuel;
        
        vector> gas_stations;
        priority_queue gas_available;
        for(int i = 0; i < n; i++) {
            gas_stations.push_back(make_pair(city_distance - dist[i], gas[i]));
        }
        gas_stations.push_back(make_pair(city_distance, 0));
        sort(gas_stations.begin(), gas_stations.end());
        int location = 0;
        bool ok = true;
        for(int i = 0; i < gas_stations.size(); i++) {
            int distance_from_gas_station = gas_stations[i].first - location;
            
            if(distance_from_gas_station > fuel) {
                while(distance_from_gas_station > fuel) {
                    if(gas_available.empty()) {
                        cout << -1 << endl;
                        ok = false;
                        break;
                    }
                    int max_available_gas = gas_available.top();
                    gas_available.pop();
                    fuel += max_available_gas;
                    result++;
                }
                if(!ok) {
                    break;
                }
            }
            fuel -= distance_from_gas_station;
            location = gas_stations[i].first;
            gas_available.push(gas_stations[i].second);
            
        }
        if(ok) {
            cout << result << endl;
        }
       
    }
    

    return 0;
}