QAQ
O(N)暴力算,记录前缀和与前缀个数即可
#include <cstdio>
#include <iostream>
using namespace std;
typedef unsigned long long ull;
ull num[200000],sum[200000];
struct node{
ull dx,ri;
}a[200000];
int main()
{
ull n,l;
scanf("%lld%lld",&l,&n);
for(int i=1;i<=n;i++)
scanf("%lld%lld",&a[i].dx,&a[i].ri),num[i]=num[i-1]+a[i].ri,sum[i]=sum[i-1]+a[i].ri*a[i].dx;
ull maxn=sum[n];
ull z=num[n];
ull minn=((ull)2<<61);
for(int i=1;i<=n;i++)
{
ull ans1=num[i]*a[i].dx-sum[i];
ull ans2=(maxn-sum[i])-(z-num[i])*a[i].dx;
minn=min(minn,ans1+ans2);
}
printf("%lld",minn);
return 0;
}