AcWing 3493. 最大的和
原题链接
简单
作者:
英特耐雄纳尔
,
2021-05-18 16:27:28
,
所有人可见
,
阅读 245
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long LL; //爆int
LL arr[N],b[N];
int st[N];
LL res,n,k;
LL xuan_sum;
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++) scanf("%lld",&arr[i]);
for (int i = 1; i <= n; i ++ ) scanf("%d", &st[i]);
// for (int i = 1; i <= n; i ++ )
// {
// if(st[i]) xuan_sum+=arr[i];
// else
// {
// b[i]=arr[i]; //这里不能变赋值边进行前缀和,否则如果没有执行就会导致前一个b数组值为0
// b[i]+=b[i-1];
// }
// }
for (int i = 1; i <= n; i ++ )
{
if(st[i]) xuan_sum+=arr[i];
else
{
b[i]=arr[i];
}
}
for (int i = 1; i <= n; i ++ ) b[i]+=b[i-1];
for (int i = 1; i+k-1 <= n; i ++ )
{
int j=i+k-1;
res = max(res,b[j]-b[i-1]);
}
//cout<<xuan_sum<<endl;
res+=xuan_sum;
cout<<res;
}