PDP-27 (2015) - Phase C' - 2 (efimerides)

View as PDF

Submit solution

Points: 30 (partial)
Time limit: 1.0s
Memory limit: 16M

Author:
Problem types
Allowed languages
C, C++, Java, Pascal, Python

Newspapers

Peter distributes the daily press. He needs to visit N sales points, placed on a circular route. He knows that the i-th sales point earns him A\mathbf{_i}. However, if he starts visiting the N points sequentially, then in some areas, he will reach all sales points on time, while in others, the press will be delayed.

Peter decides to start from a certain sales point i, leave the newspapers there, and then, moving cyclically, skip K-1 points and go to the next point to meet the demand there. He leaves newspapers every K points, starting from i. The only problem is that it is difficult for him to remember where he has been and where not. Therefore, he decides to stop when he reaches a point where he has already left newspapers. Thus, it is possible that when he stops, he has not visited all the sales points.

Problem

Write a program in one of the languages of the IOI which reads the values of N and K and the earnings A_i and finds the maximum total profit Peter can have by following this procedure.

Input Files:

The input files named efimerides.in are text files consisting of two lines: The first line contains two integers separated by a single space: the values of N and K (2 \le K \le N \le 1.000.000). The second line contains N integers A\mathbf{_i} (1 \le A_i \le 1.000.000), separated in pairs by a single space: the terms of the sequence. The sum of all A_i will not exceed 1.000.000.000.

Output Files:

The output files named efimerides.out are text files consisting of exactly one line containing exactly one integer: Peter's maximum possible profit.

Examples of Input - Output Files:
1st

STDIN (efimerides.in)

5 2
1 2 3 4 5

STDOUT (efimerides.out)

15

pdp-2015-C2-fig-1.svg

Explanation of the first example:

In the first example, no matter where Peter starts, he will visit all the sales points. Therefore, his total profit will be equal to the total sum. A possible route with this total profit is: 1 + 3 + 5 + 2 + 4 = 15.


2nd

STDIN (efimerides.in)

6 3
4 1 3 6 2 7

STDOUT (efimerides.out)

10

pdp-2015-C2-fig-2.svg

Explanation of the second example:

In the second example, the starting point matters. For example, if he starts from 2, he will have a profit of 2 + 1 = 3, while if he starts from 4, he will have a profit of 4 + 6 = 10. The maximum possible profit is 10, and he can achieve it in different ways, for example, 7 + 3 = 10.

Formatting: In the output, each line terminates with a newline character.
Maximum execution time: 1 sec.
Maximum available memory: 16 MB.


Comments

There are no comments at the moment.