Problem 2168 defensive position IAccept: 128 submit: 392
Time Limit: 3000 msec memory limit: 32768 kb Problem Description
There are a total of N soldiers in the army, each of which has its own capacity index XI. During a drill, the headquarters determined m locations to be defended, sorted by importance from low to high, in turn, numbers 1 to m are used to mark the importance of each location. The headquarters will select M soldiers to go to the designated location for defensive tasks in turn, x is the reference index for x when X is important for defense. Now, the soldiers are in a row. Please select M soldiers in a row to participate in the defense in sequence, so that the total reference index value is the largest.
Input
The input contains multiple groups of data.
The first line of the input has two integers n, m (1 <= n <= 000000,1 <= m <= 1000 ), the N integers in the second row indicate the capability index XI (1 <= xi <= 1000) of each soldier ).
For 30% of data, 1 <= m <= n <= 1000.
Output
Returns an integer that is the sum of the largest reference indexes.
Sample input5 32 1 3 1 4 sample output17 // assume that the current capacity index of the first 1-M soldiers is 1 * A [1] + 2 * A [2] + 3 * [3] + ..... + M * A [m], it is easy to conclude that the first 2-m + 1 soldier's capability index Y is: X + A [M + 1] * m-(A [1] + A [2] + ..... A [m]); then, we only need to pre-process the input, so that we can enumerate all the situations of M soldiers in a row, and then take the maximum value.
# Include <cstdio> # include <algorithm> using namespace STD; # define maxn limit 10int A [maxn], B [maxn]; int main () {int n, m, I; _ int64 ans, X, temp; while (scanf ("% d", & N, & M )! = EOF) {ans = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & A [I]); B [I] = B [I-1] + A [I]; // Save the number of I before and if (I <= m) ans + = A [I] * I; // capability index of 1-M soldiers} X = ans; for (I = m + 1; I <= N; I ++) {temp = x + (m + 1) * A [I]; // Add a [I] temp-= B [I]-B [i-m-1]; // because B [I] contains a [I], it is equal to this one more time, so add x = temp; // transfer to the next phase ans = max (temp, ANS); // continuously update to get the maximum value} printf ("% i64d \ n", ANS);} return 0 ;}