题目描述
参考题解
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e5+5;
typedef long long LL;
int n, k;
int a[N];
bool st[N];
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; ++ i)
{
scanf("%d", a+i);
}
LL sum = 0;
for(int i = 1; i <= n; ++ i)
{
scanf("%d", st+i);
if(st[i])
{
sum += a[i]; //所有可选中元素的和
}
}
LL ans = 0;
//接下来处理过程中,sum表示所有可选中或者在区间中的和
for(int i = 1; i <= n; ++ i)
{
if(!st[i])
{
sum += a[i]; //从不可选中变成可选中
}
if(i > k) //长度达到k+1,左端点出去
{
if(!st[i-k]) //原本状态不可选中
{
sum -= a[i-k];
}
}
ans = max(ans, sum);
}
cout << ans << endl;
return 0;
}
原文:https://www.cnblogs.com/chaosliang/p/14764051.html