import java.util.*; import java.lang.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); int k = in.nextInt(); int[] inte = new int[n]; int[] clear = new int[n]; int score = 0; for(int i=0;i<n;i++) inte[i] = in.nextInt(); for(int i=0;i<n;i++) { clear[i] = in.nextInt(); if(clear[i]>0) { score+=inte[i]; inte[i] = 0; } } int[] preSum = new int[n+1]; preSum[n] = 0; for(int i=n-1;i>=n-k;i--) preSum[i] = preSum[i+1]+inte[i]; for(int i=n-k-1;i>=0;i--) preSum[i] = inte[i]+preSum[i+1]-inte[i+k]; // System.out.print(Arrays.toString(preSum)); System.out.print(Arrays.stream(preSum).max().getAsInt()+score); } } }
核心思路:后缀和(前缀和反向)