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.002 second

Submitted On: 2026-03-07 23:18:34

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

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