Newspapers
Peter distributes the daily press. He needs to visit N sales points, placed on a circular route. He knows that the -th sales point earns him A. However, if he starts visiting the 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 , leave the newspapers there, and then, moving cyclically, skip points and go to the next point to meet the demand there. He leaves newspapers every K points, starting from . 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 and and the earnings 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 . The second line contains integers A , separated in pairs by a single space: the terms of the sequence. The sum of all will not exceed .
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
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: .
2nd
STDIN (efimerides.in)
6 3
4 1 3 6 2 7
STDOUT (efimerides.out)
10
Explanation of the second example:
In the second example, the starting point matters. For example, if he starts from , he will have a profit of , while if he starts from , he will have a profit of . The maximum possible profit is , and he can achieve it in different ways, for example, .
Formatting: In the output, each line terminates with a newline
character.
Maximum execution time: 1 sec.
Maximum available memory: 16 MB.
Comments