评测记录 复杂度 O(nlogn) 不知道为什么会T
#include<bits/stdc++.h>
#define gc getchar
#define pc putchar
#define ll long long
#define w(x) write(x),putchar(' ')
using namespace std;
inline int r(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)){
if(ch=='-') f=-1,ch=gc();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);//x=x*10+(ch-'0')
ch=gc();
}
return x*f;
}
inline void write(int x){
if(x>=10) write(x/10);
pc(x%10+'0');
}
struct node{
int w,v;
}b[2000005];
bool cmp(node a,node b){
return a.v>b.v;
}
int cl,cr,n,m,wm;
vector<int>a;
ll sum,ls;
bool vis[1000005];
priority_queue<int>q;
int f(int l,int r,int x){
int mid=(l+r)/2;
if(x==a[mid]) return mid;
if(a[mid]>x){
if(a[mid-1]<x) return mid;
else return f(l,mid-1,x);
}
else{
if(a[mid+1]>x) return mid+1;
else return f(mid+1,r,x);
}
}
signed main(){
// freopen("P11268.in","r",stdin);
// freopen("P11268.out","w",stdout);
n=r(); m=r(); cr=m; a.push_back(0);
for(int i=1;i<=n;i++){
sum+=b[i].w=r();
a.push_back(b[i].w);
b[i].v=a[i]-r();
}
for(int i=n+1;i<=m+n;i++){
b[i].w=r();
b[i].v=r();
}
sort(a.begin()+1,a.end());
sort(b+1,b+n+m+1,cmp);
for(int i=1;i<=n+m;i++){
if(a.size()==1) break;
cl=1,cr=a.size()-1;
if(b[i].w>a[cr]) continue;
else{
wm=f(cl,cr,b[i].w);
sum-=b[i].v;
a.erase(a.begin()+wm);
}
}
printf("%lld",sum);
return 0;
}