Submission

Status:

[PPPPP][PPPPP][PPPPPPPPPP]

Subtask/Task Score:

{20/20}{30/30}{50/50}

Score: 100

User: Quaoar

Problemset: ห้องสมุดเมือง 3M

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-10 19:15:53

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector <pair<int , int>> arr;
ll books;
ll pos;
bool smth(ll book){
    ll sum = 0;
    for (int i = 0 ; i < n ; i++){
        if (book >= arr[i].second){
            sum += arr[i].second - arr[i].first;
        } else if (book >= arr[i].first){
            sum += book - arr[i].first + 1;
        }
    }
    // cout << "Sum" << sum << " " << pos;
    return sum >= pos;
}


int main(){     
    cin >> n;
    int mx = -1;
    int mn = INT_MAX;
    for (int i = 0 ; i < n ; i++){
        int a , b;
        cin >> a >> b;
        mx = max(b, mx);
        mn = min(a , mn);
        arr.push_back({a,b});
        books += b - a;
    }   
    int l = mn;
    int r = mx;
    int ans;
    pos = books / 2;
    while (l <= r){
        int md = (l + r) / 2;
        // cout << "md : " << md << "\n";
        if (smth(md)){
            ans = md;
            r = md - 1;
        } else {
            l = md + 1;
        }
    }
    
    cout << ans;


    return 0;
}