rt,第三个样例没过,但AC,建议加强数据
code:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005];
ll b[100005];
ll cnt=0;
struct people{
ll x,r;
}p[100005];
bool cmp(people a,people b)
{
return a.x<b.x;
}
int main()
{
ll l,n;
scanf("%lld%lld",&l,&n);
for(int i=1;i<=n;i++)
scanf("%lld%lld",&p[i].x,&p[i].r);
sort(p+1,p+n+1,cmp);
for(int i=1;i<=n;i++)
{
a[i]=a[i-1]+cnt*(p[i].x-p[i-1].x);
cnt+=p[i].r;
}
cnt=0;
ll ans=LONG_MAX;
for(int i=n;i>=1;i--)
{
b[i]=b[i+1]+cnt*(p[i+1].x-p[i].x);
ans=min(ans,b[i]+a[i]);
cnt+=p[i].r;
}
printf("%lld\n",ans);
return 0;
}