Submission

Status:

[PPPPP][-SSSS][SSSSSSSSSS]

Subtask/Task Score:

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

Score: 20

User: kittipos

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

Language: cpp

Time: 0.003 second

Submitted On: 2026-03-04 14:17:32

#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);
        }

      //  cout << "center: " << center << ", count: " << count << endl;

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

    return 0;
}