题解 | 空间跃迁

空间跃迁

https://www.nowcoder.com/practice/e6ff2b0bf309412fb491e3276fb9f305

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,k;
    cin>>n>>k;
    vector<int>a(n+1);
    vector<long long>pre(n+1,0);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        pre[i]=pre[i-1]+a[i];
    }
    long long max_=0;
    for(int i=0;i<n;i++){
        int t=min(n,i+k);
        max_=max(pre[t]-pre[i],max_);
    }
    cout<<pre[n]-max_;
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务