Submission
Status:
[PPPPP][PPPPP][PPPPPPPPPP]
Subtask/Task Score:
{20/20}{30/30}{50/50}
Score: 100
User: foldnut
Problemset: ห้องสมุดเมือง 3M
Language: cpp
Time: 0.003 second
Submitted On: 2025-11-13 21:46:41
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
int n, sm, a[N], b[N];
bool ok(int x){
int t = 0;
for(int i = 0;i<n;i++){
if(x >= b[i]) t += b[i] - a[i];
else if(x >= a[i]) t += x - a[i] + 1;
}
return t >= sm / 2;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0;i<n;i++) cin >> a[i] >> b[i], sm += b[i] - a[i];
if(sm == 1) return cout << a[0], 0;
int l = 0, r = 2e7, ans = -1;
while(l <= r){
int md = (l + r) / 2;
if(ok(md)) ans = md, r = md - 1;
else l = md + 1;
}
cout << ans;
}