An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, where ai is the number of details of thei-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it).
A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?
The first line contains three integers n, m, k (1 ≤ n ≤ 105, 1 ≤ m ≤ 5, 0 ≤ k ≤ 109) — the number of droids, the number of detail types and the number of available shots, respectively.
Next n lines follow describing the droids. Each line contains m integers a1, a2, ..., am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.
Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.
If there are multiple optimal solutions, print any of them.
It is not necessary to make exactly k shots, the number of shots can be less.
5 2 4 4 0 1 2 2 1 0 2 1 3
2 2
3 2 4 1 2 1 3 2 2
1 3
In the first test the second, third and fourth droids will be destroyed.
In the second test the first and second droids will be destroyed.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
struct Node
{
int l,r,maxx[6];
}node[maxn<<2];
int a[maxn][6];
int n,m,k;
int tmp[6],ans[6];
void pushup(int rt)
{
int i;
FRE(i,1,m)
node[rt].maxx[i]=max( node[rt<<1].maxx[i] , node[rt<<1|1].maxx[i] );
}
void build(int rt,int l,int r) //建线段树
{
int i;
node[rt].l=l;
node[rt].r=r;
if (l==r)
{
FRE(i,1,m)
node[rt].maxx[i]=a[r][i];
return ;
}
int mid=(l+r)/2;
build(lson);
build(rson);
pushup(rt);
return ;
}
void query(int rt,int l,int r) //区间查询
{
if (l<=node[rt].l&&node[rt].r<=r)
{
int i;
FRE(i,1,m)
tmp[i]=max(tmp[i],node[rt].maxx[i]);
return ;
}
int mid=(node[rt].l+node[rt].r)>>1;
if (mid>=r)
query(rt<<1,l,r);
else if (mid<l)
query(rt<<1|1,l,r);
else
{
query(lson);
query(rson);
}
return ;
}
bool ok(int mid)
{
bool flag=false;
int i,j;
FRE(i,1,n-mid+1) //枚举起点
{
int num=0;
query(1,i,i+mid-1);
FRE(j,1,m)
num+=tmp[j];
// printf("num=%d\n",num);
if (num<=k)
{
flag=true;
memcpy(ans,tmp,sizeof(tmp));
break;
}
mem(tmp,0);
}
if (flag) return true;
return false;
}
void solve() //二分区间长度
{
int l=1,r=n;
while (l<=r)
{
int mid=(l+r)>>1;
if (ok(mid))
l=mid+1;
else
r=mid-1;
}
int i;
pf("%d",ans[1]);
FRE(i,2,m)
pf(" %d",ans[i]);
pf("\n");
}
int main()
{
int i,j;
while (~sfff(n,m,k))
{
FRE(i,1,n)
FRE(j,1,m)
scanf("%d",&a[i][j]);
build(1,1,n);
solve();
}
return 0;
}