Submission

Status:

[PPPPP][PPPPP][PPPPPPPPPP]

Subtask/Task Score:

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

Score: 100

User: Quaoar

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-11-24 11:32:54

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[102] , b[102];
ll mx = -1 , mn = 2e7 + 2 , ans;
ll books = 0;
int n;

bool smth(int x){
    ll book = 0;
    for (int i = 0 ; i < n ; i++){
        if (x >= b[i]){
            book += b[i] - a[i];
        } else if (x >= a[i]){
            book += x - a[i] + 1;
        }
    }
    return book >= books / 2;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i = 0 ; i < n ; i++){
        cin >> a[i] >> b[i];
        books += b[i] - a[i];
        mx = max(mx , b[i]);
        mn = min(mn , a[i]);
    }


    int l = mn;
    int r = mx;
    //binary search
    while (l <= r){
        int md = (l + r) / 2;
        if (smth(md)){
            ans = md;
            r = md - 1;
        } else {
            l = md + 1;
        }
    }
    cout << ans;
}