Submission

Status:

[-SSSS][SSSSS][SSSSSSSSSS]

Subtask/Task Score:

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

Score: 0

User: kittipos

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

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-04 14:30:54

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<pair<int, int>> books;
    books.resize(n);
    int most = 0;
    int all = 0;
    for (int i = 0; i < n; i++) {
        cin >> books[i].first >> books[i].second;
        most = max({ most, books[i].second, books[i].first });
        all += books[i].second - books[i].first;
    }


    int left = 0;
    int right = most;
    while (left + 1 < right) {
        int center = left + (right - left) / 2;
        int count = 0;
        for (int i = 0; i < books.size(); i++) {
            pair<int, int > b = books[i];
            count += max(0, center - b.first + 1);
            count -= max(0, center - b.second + 1);
        }


        if (count - 1 <= all / 2) {
            left = center;
        }
        else {
            right = center;
        }
    }

    cout << left << endl;

    return 0;
}