http://acm.hdu.edu.cn/showproblem.php?pid=1355
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 10000
using namespace std; struct node
{
int x,y,w;
bool operator <(const node &a)const
{
return w>a.w;
}
}p[maxn];
int n,m,k; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
int cnt=;
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
int xx;
scanf("%d",&xx);
if(xx)
{
p[cnt].x=i;
p[cnt].y=j;
p[cnt++].w=xx;
}
}
}
sort(p,p+cnt);
__int64 sum=,ans=;
for(int i=; i<cnt; i++)
{
if(i==)
{
sum+=p[i].x;
}
else sum+=abs(p[i].x-p[i-].x)+abs(p[i].y-p[i-].y);
if(sum+p[i].x+<=k)
{
ans+=p[i].w;
sum++;
}
else break;
}
cout<<ans<<endl;
}
return ;
}