题目链接
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int N = 100010;
#define int long long
typedef pair<int,int>PII;
int v,n,m;
PII a[N];
int s1[N],s2[N];
signed main()
{
scanf("%lld%lld%lld",&v,&n,&m);
int x=m&1;m>>=1;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].first,&a[i].second);
}
sort(a+1,a+n+1);
priority_queue<int>heap;
for(int i=1;i<=n;i++)
{
heap.push(a[i].second);s1[i]=s1[i-1]+a[i].second;
if(heap.size()>m-1+x)
{
s1[i]-=heap.top();
heap.pop();
}
}
while(heap.size())heap.pop();
for(int i=n;i>=1;i--)
{
heap.push(a[i].second);s2[i]=s2[i+1]+a[i].second;
if(heap.size()>m)
{
s2[i]-=heap.top();
heap.pop();
}
}
int res=0;
if(x&1)
{
for(int i=m+1;i<=n-m;i++)
{
if(s1[i-1]+s2[i+1]+a[i].second<=v)res=a[i].first;
}
printf("%lld\n",res);
}
else
{
for(int i=m;i<=n-m;i++)
{
int l=i+1,r=n-m+1;
while(l<=r)
{
int mid=l+r>>1;
if(s1[i-1]+s2[mid]+a[i].second<=v)l=mid+1;
else r=mid-1;
}
if(r>i)res=max(a[i].first+a[r].first,res);
}
printf("%lld\n",res/2);
}
}