一个直观的想法是,枚举最小的是谁,然后二分找到另外一个序列对应位置更新答案,复杂度 \(O(NlogN)\)
实际上不需要二分,因为每次当最大的变大之后,原来不行的最小值现在也一定不行,指针移动是单调的,直接 \(O(N)\) 扫描即可
#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int>pii;
inline int read(){char c,p=0;int w;
while(isspace(c=getchar()));if(c=='-')p=1,c=getchar();
for(w=c&15;isdigit(c=getchar());w=w*10+(c&15));return p?-w:w;
}
template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;}
template<typename T,typename U>inline char smax(T&x,const U&y){return x<y?x=y,1:0;}
const int N=1e5+5;
int n,w,a[N],b[N];
int main(){
n=read(),w=read();
REP(i,1,n)a[n-i+1]=read();
REP(i,1,n)b[n-i+1]=read();
int p=1,q=1;
ll s1=0,s2=0,c=0,ans=0;
while(p<=n&&q<=n)if(s1>=s2)
while(s1>=s2&&q<=n)c+=w,s2+=b[q++],smax(ans,min(s1,s2)-c);
else
while(s1<=s2&&p<=n)c+=w,s1+=a[p++],smax(ans,min(s1,s2)-c);
cout<<ans;
return 0;
}