#include #include #include #include #include #include #define mp make_pair #define INF 1000000000 #define MAXN 200050 #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)<(y)?(x):(y)) #define P pair #define LL long long using namespace std; int a[MAXN]; LL buy[MAXN],sell[MAXN]; int len[MAXN]; LL now[MAXN]; int q[MAXN]; void work(){ int st=0,ed=0; int n,c; scanf("%d%d",&n,&c); for(int i=1;i<=n;++i)scanf("%d",&a[i]); for(int i=0;i<=n;++i)scanf("%I64d%I64d",&buy[i],&sell[i]); now[0]=c; q[ed++]=0; LL ans=c*buy[0]; for(int i=1;i<=n;++i){ int dis=a[i]-a[i-1]; LL sum=c-dis; while(dis){ int x=q[st]; if(now[x]>dis){ now[x]-=dis; dis=0; }else{ dis-=now[x]; now[x]=0; st++; } } int era=0; while(st=buy[i]){ flag=true; ans-=now[x]*buy[x]; sum-=now[x]; now[x]=0; ed--; } } q[ed++]=i; now[i]=c-sum; ans+=(c-sum)*buy[i]; } while(st