http://codeforces.com/contest/961/problem/B
做了生涯以来次数最多的前缀和问题,,,cf上关于前缀和真的是相当重视了
利用前缀和来保存当前状态的值,解决一个连续区间取值的问题真是再合适不过了
#include#include#include#include#include#include#include#include#include#define ll long long#define mod 1000000007#define inf 0x3f3f3f3fusing namespace std;#define ll long longint main(){int n, k;int a[100005];int t[100005];ll sum[100005];ll ans &#61; 0;scanf("%d%d",&n,&k);for(int i &#61; 1; i <&#61; n; i &#43;&#43;){scanf("%d",&a[i]);}for(int i &#61; 1; i <&#61; n; i &#43;&#43;){scanf("%d",&t[i]);if(t[i]){ans &#43;&#61; a[i];}if(! t[i]){sum[i] &#61; sum[i-1] &#43; a[i];}else sum[i] &#61; sum[i-1];}ll maxy &#61; -1;for(int i &#61; 1; i <&#61; n; i &#43;&#43;){if(i <&#61; k) maxy &#61; max(maxy, sum[i]);else maxy &#61; max(maxy, sum[i] - sum[i-k]);}printf("%I64d\n",ans &#43; maxy);return 0;}