Submission

Status:

[PPPPP][PPPPP][PPPPPPPPPP]

Subtask/Task Score:

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

Score: 100

User: tha_smith

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

Language: cpp

Time: 0.004 second

Submitted On: 2026-03-02 19:54:00

#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0),cin.tie(0);
	int N,mx=0,mn=INT_MAX; long long total=0;
	vector<pair<int,int>> R;
	cin >> N;
	for(int i=0; i<N; i++) {
		int x,y;
		cin >> x >> y;
		R.push_back({x,y});
		mx = max(y,mx);
		mn = min(x,mn);
		total += (long long)(y-x);
	}
	int l=mn, r=mx, target=floor(total/2), ans=0;
	while(l<=r) {
		int mid = l+(r-l)/2;
		int test=0;
		for(auto n:R) {
			int u = n.first;
			int v  = n.second;
			long long cnt = min(v,mid+1)-u;
			if(cnt>0) {
				test+=cnt;
			}
		}
		if(test>=target) {
			ans=mid;
			r=mid-1;
		}
		else {
			l=mid+1;
		}
	}
	cout << ans;
}