Description
Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's clear that we may get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?
Input
The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.
Output
For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.
Sample Input
1
2 3
1 2 3
2 2 3
Sample Output
3 3 4
#include"iostream"
#include"algorithm"
#include"ctime"
#include"cstdio"
#include"cctype"
using namespace std;
#define maxx 2008
int a[maxx],b[maxx],sum[maxx];
int main()
{
int i,j,k,t,n,m,temp;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(i=;i<m;i++)
{
sort(a,a+n);
for(j=;j<n;j++)
scanf("%d",&b[j]);
for(j=;j<n;j++)
sum[j]=a[j]+b[];
make_heap(sum,sum+n);
for(j=;j<n;j++)
for(k=;k<n;k++)
{
temp=b[j]+a[k];
if(temp>=sum[])
break;
pop_heap(sum,sum+n);
sum[n-]=temp;
push_heap(sum,sum+n);
}
for(j=;j<n;j++)
a[j]=sum[j];
}
sort(a,a+n);
printf("%d",a[]);
for(j=;j<n;j++)
printf(" %d",a[j]);
printf("\n");
printf("time :%d\n",clock());
}
return ;
}