366B.md (2991B)
1 Source: https://codeforces.com/problemset/problem/366/B 2 3 B. Dima and To-do List 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong. 22 23 Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete *k* - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks. 24 25 Overall, Dima has *n* tasks to do, each task has a unique number from 1 to *n*. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one. 26 27 Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible. 28 29 Input 30 31 The first line of the input contains two integers *n*, *k* (1 ≤ *k* ≤ *n* ≤ 105). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ 103), where *a**i* is the power Inna tells Dima off with if she is present in the room while he is doing the *i*-th task. 32 33 It is guaranteed that *n* is divisible by *k*. 34 35 Output 36 37 In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do. 38 39 Examples 40 41 Input 42 43 ``` 44 6 2 45 3 2 1 6 5 4 46 ``` 47 48 Output 49 50 ``` 51 1 52 ``` 53 54 Input 55 56 ``` 57 10 5 58 1 3 5 7 9 9 4 1 8 5 59 ``` 60 61 Output 62 63 ``` 64 3 65 ``` 66 67 Note 68 69 Explanation of the first example. 70 71 If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as *k* = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12. 72 73 Explanation of the second example. 74 75 In the second example *k* = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.