SOURCE

const A = [0,1,0,1,0,1,0,1];
const B = [1,0,1,2,1,1,7,5];
const K = 3;
let left = 0;
let count = 0;
let max = 0;
const n = A.length;
for(let i=0;i<=n-K;i++){
    let sum = (B.slice(i,i+K)).reduce((a,b)=>a+b);
    for(let j=i-1;j>=0;j--){
        if(A[j]===1){
            sum+=B[j];
        }
    }
    for(let j=i+K;j<n;j++){
        if(A[j]===1){
            sum+=B[j];
        }
    }
    if(sum>max){
        max = sum;
    }
}
console.log(max);
console 命令行工具 X clear

                    
>
console