The maximum of the sum (Rakutani AT2412)

The meaning of problems

Read the number of columns n integers a1, a2, ..., an, and a positive integer k (1 <= k <= n), and the requested output continuously arranged maximum integer k

Entry

The first line is a positive integer n (1 <= n <= 100000) and a positive integer k (1 <= k <= n) after the second row 1 + i (1 <= i <= n) to the last row number of columns

Export

Only one line, including a maximum value only.

Sample input

5 3 2 5 -4 10

Sample Output

11


This question is more difficult in the valley, but Los popularity - eh, so that the prefix direct manual and friends on AC -

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,k,ans;
 4 int a[100010];
 5 int main()
 6 {
 7     cin>>n>>k;
 8     for(int i=1;i<=n;i++)
 9     {
10         cin>>a[i];
11         a[i]+=a[i-1];
12     }
13     for(int i=k;i<=n;i++)
14         ans=max(ans,a[i]-a[i-k]);
15     cout<<ans<<endl;
16     return 0;
17 }

 

Guess you like

Origin www.cnblogs.com/ljy-endl/p/11330573.html