前缀和内容是 不选取的前缀和,因为题意可以选择一个区间,使得区间内都为可选,之后把所有可选相加。
所以,不能忽略掉原本可选的位置,如果可选,就用0替代,
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n,m;
int a[N] , sa[N];
long long qzh[N];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
long long x2 = 0,res=0;
for(int i = 1; i <= n; i ++ ){
scanf("%1d",&sa[i]);
if(sa[i]){
x2+=a[i]; // x2为所有可选的和
a[i]=0; //如果可选,就用0替代
}
qzh[i]=qzh[i-1]+a[i];
if(i-m>=0 && res<qzh[i]-qzh[i-m]){ // 维护不可选的最大值
res = qzh[i]-qzh[i-m];
}
}
printf("%lld\n",res+x2);
return 0;
}