A median of an array of integers of length nn is the number standing on the ⌈n2⌉⌈n2⌉ (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting with 11. For example, a median of the array [2,6,4,1,3,5][2,6,4,1,3,5] is equal to 33. There exist some other definitions of the median, but in this problem, we will use the described one.
Given two integers nn and kk and non-decreasing array of nknk integers. Divide all numbers into kk arrays of size nn, such that each number belongs to exactly one array.
You want the sum of medians of all kk arrays to be the maximum possible. Find this maximum possible sum.
Input
The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The next 2t2t lines contain descriptions of test cases.
The first line of the description of each test case contains two integers nn, kk (1≤n,k≤10001≤n,k≤1000).
The second line of the description of each test case contains nknk integers a1,a2,…,anka1,a2,…,ank (0≤ai≤1090≤ai≤109) — given array. It is guaranteed that the array is non-decreasing: a1≤a2≤…≤anka1≤a2≤…≤ank.
It is guaranteed that the sum of nknk for all test cases does not exceed 2⋅1052⋅105.
Output
For each test case print a single integer — the maximum possible sum of medians of all kk arrays.
Example
6 2 4 0 24 34 58 62 64 69 78 2 2 27 61 81 91 4 3 2 4 16 18 21 27 36 53 82 91 92 95 3 4 3 11 12 22 33 35 38 67 69 71 94 99 2 1 11 41 3 3 1 1 1 1 1 1 1 1 1
165 108 145 234 11 3
要求输入n组数据,每个m个,就从最大的书开始,每最大的m个记录第m/2个数,依次记录,直到有n个,最后相加
代码
#include<stdio.h> #include<string.h> int main(){ int n; long long s[100005]; scanf("%d",&n); while(n--){ int a,m; scanf("%d%d",&a,&m); for(int i=1;i<=a*m;i++){ scanf("%lld",&s[i]); } int b=0,t=a*m-a/2; for(int j=1;j<=m;j++){ b+=s[t]; t=t-a/2-1; } printf("%d\n",b); } return 0; }
浙公网安备 33010602011771号