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-08 22:43:35
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0),cin.tie(0);
int N,mn=INT_MAX,mx=0; ll total=0; vector<pair<int,int>> R;
cin >> N;
for(int i=0; i<N; i++) {
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),l=mn,r=mx,ans=0;
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;
}