rt,写的单调栈,luogu 民间数据 90,CCF 的数据 100。
主要是不知道哪炸了。
代码:
#include<cstdio>
#include<stack>
using namespace std;
#define int long long
const int N=1e5+10;
int a[N],v[N];
int n,d;
int sum;
int pre[N];
int qz[N];
int ans,now;
stack<int>q;
signed main(){
//freopen("road.in","r",stdin);
//freopen("road.out","w",stdout);
scanf("%lld%lld",&n,&d);
for(int i=1;i<=n-1;i++){
scanf("%lld",&v[i]);
sum+=v[i];
qz[i]=qz[i-1]+v[i];
}
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=n-1;i>=1;i--){
while(!q.empty()&&a[q.top()]>=a[i]) q.pop();
if(q.empty()) pre[i]=0;
else pre[i]=q.top();
q.push(i);
}
for(int i=1;i<=n-1;){
if(pre[i]==0){
int need=qz[n-1]-qz[i-1]-now;
int qwq;
if(need%d==0) qwq=need/d;
else qwq=need/d+1;
now=max(1ll*0,qwq*d-need);
ans+=max(qwq,1ll*0)*a[i];
break;
}
else{
int need=qz[pre[i]-1]-qz[i-1]-now;
int qwq;
if(need%d==0) qwq=need/d;
else qwq=need/d+1;
now=max(1ll*0,qwq*d-need);
ans+=max(qwq,1ll*0)*a[i];
i=pre[i];
}
}
printf("%lld",ans);
return 0;
}
/*
5 4
10 10 10 10
9 8 9 6 5
5 4
10 20 30 40
9 8 9 6 5
*/