【BZOJ #4977】【[Lydsy1708月赛】 跳伞求生(模拟费用流)
把排序后从小到大处理即可
维护一下退流即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
priority_queue<int> q;
cs int N=100005;
ll ans;
int n,m,a[N];
pii b[N];
inline void ins1(pii x){
q.push(x.se-x.fi);
}
inline void ins2(int x){
if(q.size()){
ans+=q.top()+x,q.pop(),q.push(-x);
}
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read(),m=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int j=1;j<=m;j++)b[j].fi=read(),b[j].se=read();
sort(a+1,a+n+1),sort(b+1,b+m+1);
for(int i=1,j=1;i<=n;i++){
while(j<=m&&b[j].fi<a[i])ins1(b[j]),j++;
ins2(a[i]);
}
cout<<ans<<'\n';
}