CodeForces 366B - Dima and To-do List

来源:互联网 发布:您的淘宝账户已被限制 编辑:程序博客网 时间:2024/05/17 03:27

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.

Inna and Dima are now in one room. Inna tells Dima off for everything hedoes in her presence. After Inna tells him off for something, she goes to anotherroom, 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 forthe next task he does in her presence and goes to another room again. Itcontinues until Dima is through with his tasks.

Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dimaloves order, so he does tasks consecutively, starting from some task. Forexample, if Dima has 6 tasks to do in total, then, if he starts fromthe 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.

Inna tells Dima off (only lovingly and appropriately!) so often andsystematically that he's very well learned the power with which she tells himoff for each task. Help Dima choose the first task so that in total he getstold off with as little power as possible.

Input

The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integersa1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the roomwhile he is doing the i-th task.

It is guaranteed that n is divisible by k.

Output

In a single line print the number of the task Dima should start with toget 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.

Sample test(s)

input

6 2
3 2 1 6 5 4

output

1

input

10 5
1 3 5 7 9 9 4 1 8 5

output

3

Note

Explanation of the first example.

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 shetells him off for the fifth task with power 5. Thus, Dima gets told off withtotal 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.

Explanation of the second example.

In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling offsessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is tostart from task 3 or 8, 3 has a smaller number, so the answer is 3.

 

思路:

排序,求和,注意先出现的排在前面就行了


代码:#define _CRT_SECURE_NO_WARNINGS#include<cstdio>#include<cstring>#include<iostream>#include <algorithm>using namespace std;const int N = 100005;int max(int a, int b){if (a > b)return a;elsereturn b;}struct node{int value;int id;node(){value = 0;}}a[N];bool cmp(node a, node b){if (a.value == b.value)return a.id < b.id;elsereturn a.value<b.value;}int main(){int n, k;int i, j;memset(a, 0, sizeof(a));scanf("%d%d", &n, &k);for (i = 0; i < k; i++)a[i].id = i + 1;for (i = 0; i<n; i++){int temp;scanf("%d", &temp);a[i%k].value += temp;}sort(a, a + k, cmp);printf("%d\n", a[0].id);return 0;}


0 0
原创粉丝点击