# 适者

1. 递归处理左右区间
2. 右区间按$a[]$排升序，即自变量从小到大
3. 左区间加入单调队列
4. 两个指针扫一下，把左区间的信息加到右区间上
5. 整个区间按$d[]$排序，即按斜率排升序（注意这里斜率取负数）
#include<set>
#include<map>
#include<list>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mes(x,y) memset(x,y,sizeof(x))
#define mpy(x,y) memcpy(x,y,sizeof(x))
#define Maxn 300000
#define INF 2147483647
#define LLINF 4611686018427387903
long long f=1,x=0;char ch;
while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return f*x;
}
struct Monster{
long long a,d,c,k;
}x[Maxn+1],tmp[Maxn+1];
long long n,ATK,ans,q[Maxn+1],sumd[Maxn+1];
bool cmp1(Monster p1,Monster p2){
return p1.d*p2.a<p2.d*p1.a;
}
bool cmp2(Monster p1,Monster p2){
return p1.a<p2.a;
}
void sort1(long long l,long long r) {
long long mid=(l+r)/2,p1=l,p2=mid+1,cur=l-1;
while(p1<mid&&p2<r){
if(x[p1].a<x[p2].a)tmp[++cur]=x[p1],p1++;
else tmp[++cur]=x[p2],p2++;
}
while(p1<=mid)tmp[++cur]=x[p1],p1++;
while(p2<=r)tmp[++cur]=x[p2],p2++;
for(long long i=l;i<=r;i++)x[i]=tmp[i];
}
void sort2(long long l,long long r) {
long long mid=(l+r)/2,p1=l,p2=mid+1,cur=l-1;
while(p1<mid&&p2<r){
if(x[p1].d<x[p2].d)tmp[++cur]=x[p1],p1++;
else tmp[++cur]=x[p2],p2++;
}
while(p1<=mid)tmp[++cur]=x[p1],p1++;
while(p2<=r)tmp[++cur]=x[p2],p2++;
for(long long i=l;i<=r;i++)x[i]=tmp[i];
}
long long calc(long long p1,long long p2) {
return x[p1].c+x[p2].c+x[p1].d*x[p2].a;
}

void solve(long long l,long long r){
if(l==r)return;
long long mid=(l+r)/2;
solve(l,mid);
solve(mid+1,r);
sort2(mid+1,r);
for(long long i=l+1;i<=mid;i++){
q[++tail]=i;
}
for(long long i=mid+1;i<=r;i++){
}
sort1(l,r);
}
int main(){
for(long long i=1;i<=n;i++)x[i].d=(x[i].d+ATK-1)/ATK;
std::sort(x+1,x+n+1,cmp1);
long long cur=0,sum=0;
for(long long i=1;i<=n;i++)sumd[i]=sumd[i-1]+x[i].d;
for(long long i=n;i>=1;i--){
x[i].c=x[i].d*cur+x[i].a*(sumd[i]-1);
sum+=x[i].d*cur+x[i].a*(x[i].d-1);
cur+=x[i].a;
}
for(long long i=1;i<=n;i++)x[i].d=-x[i].d;
solve(1,n);
ans=LLINF;
for(long long i=1;i<=n;i++)ans=std::min(ans,sum-x[i].k);
printf("%lld\n",ans);
}