P5638 光骓者的荣耀(前缀)C/C++

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include <algorithm>
#include<string.h>
#include<math.h>
#define llu unsigned long long
using namespace std;

int main()
{
    
    
	cout << fixed << setprecision(0);
	//cout << setw(8) << setiosflags(ios::left);
	long long  n,k;
	cin >> n >> k ;
	long long a[n+5],ans=0;
	a[0]=0;
	for(long long i=1;i<n;i++)
	{
    
    
		long long x;
		cin >> x ;
		a[i]=a[i-1]+x ;
	}
	
	if(k==0){
    
    //传送器一个城市都跳不过去,那就一个一个慢慢走吧
		cout << a[n-1] << endl ;
	}
	
	else if(k<n-1){
    
    //不可以直接跳完,那就求出一个最大能跳的距离跳过去
		for(long long i=k;i<n;i++)
		{
    
    
			ans= ans>a[i]-a[i-k] ? ans:a[i]-a[i-k];
		}
		cout << a[n-1]-ans << endl ;
	}
	//可以直接跳完,直接跳过去
	else cout << "0" << endl ;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wangyurenwls/article/details/118543270