Submission
Status:
[P-SSS][SSSSS][SSSSSSSSSS]
Subtask/Task Score:
{0/20}{0/30}{0/50}
Score: 0
User: Nay-O
Problemset: ห้องสมุดเมือง 3M
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-15 21:19:29
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 2e8+5;
priority_queue<pii, vector<pii>, greater<pii>> pq;
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n; cin >> n;
int c =0;
for(int i=0; i < n; i++){
int a, b;
cin >>a>>b;
pq.push({a,1});
pq.push({b,-1});
c+=b-a;
}
int a = c/2;
ll sum =0;
int b =0;
while(!pq.empty()){
b+=pq.top().second;
int x = pq.top().first;
pq.pop();
sum += b*(pq.top().first-x);
if(sum>=a){
cout << x+a-sum+b*(pq.top().first-x)-1;
return 0;
}
}
return 0;
}