AcWing 3493. 最大的和
原题链接
简单
作者:
术
,
2021-05-12 13:29:01
,
所有人可见
,
阅读 267
#include <iostream>
using namespace std;
typedef long long LL;
const int N=1e5+5;
int n,k;
int a[N],b[N];
LL s[N];
LL sum;
int main()
{
cin>>n>>k;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=n; i++)
cin>>b[i];
for(int i=1; i<=n; i++)
{
if(b[i]==0)//只计算非选中状态的,因为选中状态的不需要再加一次
s[i]=s[i-1]+a[i];
else
s[i]=s[i-1],sum+=a[i];;
}
LL res=0;
for(int i=1; i<=n-k+1; i++)
{
int j=i+k-1;
if(s[j]-s[i-1]>res)//i~j中所有元素和
res=s[j]-s[i-1];
}
cout<<sum+res;
//cout << "Hello world!" << endl;
return 0;
}