Problem:
Problem C: Live Schedule
YOKARI TAMURAは全国的に有名なアーティストである。今月YOKARIは D 日間にわたってライブツアーを行う。ツアーのスケジュールの決定においてこの国を C 種類の地域でわける。YOKARIがある地域でライブを行うことにより利益を得られ、これは正の整数で表される。YOKARIは原則として 1 日に最大 1 つまでライブを行う。ただし、ある地域でライブを行った後、隣接する地域でライブを行える場合はその地域で同じ日に再びライブを行うことができる。この条件を満たす限り、地域を移動しながら何度もライブを行うことができる。また、同じ日に同じ地域でライブを 2 度以上行うことはできない。さらに、同じ日に 2 回以上のライブを行う日の数はツアー期間中合計 X 以下でなければならない。
あなたはYOKARIの関係者であり、彼女のライブツアーのスケジュールを仮決めしなくてはならない。どの日にどの地域でライブを行えばもっとも有益だろうか。ただし 1 回のライブごとにYOKARIには非負の整数で表される負担がかかり、ツアー期間中の合計負担は W 以内でなければならない。あなたの仕事は各ライブでYOKARIにかかる負担と期待される利益を読み込み、スケジュールを仮決めして、利益合計の最大値を出力することである。
Input
入力は複数のテストケースからなる。
ひとつのテストケースは以下の形式に従う。
C D W X
E
1,1
E
1,1
… E
1,D
E
2,1
E
2,1
… E
2,D
…
E
C,1
E
C,2
… E
C,D
F
1,1
F
1,1
… F
1,D
F
2,1
F
2,1
… F
2,D
…
F
C,1
F
C,2
… F
C,D
C は地域の種類数、D はツアー期間の長さ、W はこのツアーでYOKARIに許容される負担の合計の最大値、X はツアー期間中にライブを同じ日に 2 度以上行える合計日数の上限である。
E
i,j
( 1 ≤ i ≤ C かつ 1 ≤ j ≤ D ) は地域 i で j 日目にライブを行うことで期待される利益である。E
i,j
が 0 のとき地域 i で j 日目にライブを行えないことを示す。
F
i,j
( 1 ≤ i ≤ C かつ 1 ≤ j ≤ D ) は地域 i で j 日目にライブを行うことでYOKARIにかかる負担である。E
i,j
が 0 のとき、この値は0である。
地域 i は地域 i + 1 と i - 1 それぞれに隣接する。ただし地域 1 と地域 C ( C > 2 ) は隣接しない。
入力の終わりは、4個の0がそれぞれ一文字の空白で区切られる一行で示される。
Constraints
入力はすべて整数
1 ≤ C ≤ 15
1 ≤ D ≤ 30
0 ≤ W ≤ 50
0 ≤ X ≤ 5
0 ≤ E
i,j
≤ 1,000
0 ≤ F
i,j
≤ 10
テストケースの数は 100 を超えない。
Output
各ケースに付き、スケジュールを仮決めして期待される利益合計の最大値を 1 行に出力せよ。
Sample Input
5 5 10 2
1 1 0 1 1
0 9 1 0 1
1 1 1 9 1
1 1 9 0 1
1 1 1 1 0
1 1 0 1 1
0 9 1 0 1
1 1 1 9 1
1 1 1 0 1
1 1 1 1 0
1 1 10 0
3
7
1 1 5 0
3
6
1 2 10 1
6 7
5 6
2 1 10 1
4
8
3
7
2 1 10 0
4
8
3
7
2 1 5 0
4
8
3
6
0 0 0 0
Sample Output
18
3
0
7
12
8
4

Submitted solutions:

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int C,D,W,X;
int E_table[16][30],F_table[16][30],G_table[16][30];
int dp[31][51][6];

bool rangeCheck(int area){
	if(area >= 0 && area <= C-1){
		return true;
	}else{
		return false;
	}
}


void func(){

	for(int area = 1; area <= C; area++){
		for(int day = 0; day < D; day++){
			scanf("%d",&E_table[area][day]);
			if(E_table[area][day] == 0){
				G_table[area][day] = 0;
			}else{
				G_table[area][day] = 1;
			}
		}
	}

	for(int area = 1; area <= C; area++){
		for(int day = 0; day < D; day++)scanf("%d",&F_table[area][day]);
	}

	for(int day = 0; day <= D-1; day++){
		for(int area = 1; area <= C; area++){
			E_table[area][day] += E_table[area-1][day];
			F_table[area][day] += F_table[area-1][day];
			G_table[area][day] += G_table[area-1][day];
		}
	}

	for(int day = 0; day <= D; day++){
		for(int futan = 0; futan <= W; futan++){
			for(int num = 0; num <= X; num++){
				dp[day][futan][num] = -1;
			}
		}
	}

	dp[0][0][0] = 0;

	int ans = 0;
	int tmp_futan,tmp_E,tmp_G;

	for(int day = 0; day <= D-1; day++){
		for(int futan = 0; futan <= W; futan++){
			for(int num = 0; num <= X; num++){
				if(dp[day][futan][num] == -1)continue;

				dp[day+1][futan][num] = max(dp[day+1][futan][num],dp[day][futan][num]);

				for(int area = 1; area <= C; area++){
					tmp_E = E_table[area][day]-E_table[area-1][day];
					tmp_futan = F_table[area][day]-F_table[area-1][day];

					if(tmp_E == 0 || futan+tmp_futan > W)continue;

					dp[day+1][futan+tmp_futan][num] = max(dp[day+1][futan+tmp_futan][num],dp[day][futan][num]+tmp_E);
					ans = max(ans,dp[day+1][futan+tmp_futan][num]);

					if(num == X)continue;

					for(int count = 2; area+count <= C+1; count++){
						tmp_G = G_table[area+count-1][day]-G_table[area-1][day];
						if(tmp_G != count)break;

						tmp_futan = F_table[area+count-1][day]-F_table[area-1][day];
						if(futan+tmp_futan > W)break;;

						tmp_E = E_table[area+count-1][day]-E_table[area-1][day];

						dp[day+1][futan+tmp_futan][num+1] = max(dp[day+1][futan+tmp_futan][num+1],dp[day][futan][num]+tmp_E);
						ans = max(ans,dp[day+1][futan+tmp_futan][num+1]);
					}
				}
			}
		}
	}

	printf("%d\n",ans);
}

int main(){

	for(int day = 0; day <= 29; day++){
		E_table[0][day] = 0;
		F_table[0][day] = 0;
		G_table[0][day] = 0;
	}

	while(true){
		scanf("%d %d %d %d",&C,&D,&W,&X);
		if(C == 0 && D == 0 && W == 0 && X == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3172
final_rank: 9

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int C,D,W,X;
int E_table[15][30],F_table[15][30];
int dp[31][51][6];

bool rangeCheck(int area){
	if(area >= 0 && area <= C-1){
		return true;
	}else{
		return false;
	}
}


void func(){

	for(int area = 0; area < C; area++){
		for(int day = 0; day < D; day++)scanf("%d",&E_table[area][day]);
	}

	for(int area = 0; area < C; area++){
		for(int day = 0; day < D; day++)scanf("%d",&F_table[area][day]);
	}

	for(int day = 0; day <= D; day++){
		for(int futan = 0; futan <= W; futan++){
			for(int num = 0; num <= X; num++){
				dp[day][futan][num] = -1;
			}
		}
	}

	dp[0][0][0] = 0;

	int ans = 0;
	bool FLG;
	int tmp_futan,tmp_E;

	for(int day = 0; day <= D-1; day++){
		for(int futan = 0; futan <= W; futan++){
			for(int num = 0; num <= X; num++){
				if(dp[day][futan][num] == -1)continue;

				dp[day+1][futan][num] = max(dp[day+1][futan][num],dp[day][futan][num]);

				for(int area = 0; area <= C-1; area++){
					if(E_table[area][day] == 0 || futan+F_table[area][day] > W)continue;

					dp[day+1][futan+F_table[area][day]][num] = max(dp[day+1][futan+F_table[area][day]][num],dp[day][futan][num]+E_table[area][day]);
					ans = max(ans,dp[day+1][futan+F_table[area][day]][num]);

					if(num == X)continue;

					for(int count = 2; area+count <= C; count++){
						FLG = true;
						tmp_futan = 0;
						tmp_E = 0;
						for(int i = 0; i < count; i++){
							if(rangeCheck(area+i) == false || E_table[area+i][day] == 0){
								FLG = false;
								break;
							}
							tmp_futan += F_table[area+i][day];
							tmp_E += E_table[area+i][day];
						}

						if(FLG == false || futan+tmp_futan > W)continue;

						dp[day+1][futan+tmp_futan][num+1] = max(dp[day+1][futan+tmp_futan][num+1],dp[day][futan][num]+tmp_E);
						ans = max(ans,dp[day+1][futan+tmp_futan][num+1]);
					}
				}
			}
		}
	}

	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d %d %d %d",&C,&D,&W,&X);
		if(C == 0 && D == 0 && W == 0 && X == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 3172
final_rank: 14

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
int C, D, W, X, a[33][17], b[33][17], dp[33][55][7];
int solve(int d, int s, int x) {
	if (d == D) return 0;
	if (dp[d][s][x]) return dp[d][s][x] - 1;
	int ret = solve(d + 1, s, x);
	for (int i = 0; i < C; i++) {
		if (s + b[d][i] <= W && a[d][i] != 0) {
			int res = solve(d + 1, s + b[d][i], x);
			ret = max(ret, res + a[d][i]);
		}
	}
	if (x != X) {
		for (int i = 0; i < C; i++) {
			int sum = 0, prof = 0;
			for (int j = i; j < C; j++) {
				prof += a[d][j];
				sum += b[d][j];
				if (a[d][j] == 0 || s + sum > W) break;
				int res = solve(d + 1, s + sum, x + 1);
				ret = max(ret, res + prof);
			}
		}
	}
	dp[d][s][x] = ret + 1;
	return ret;
}
int main() {
	while (cin >> C >> D >> W >> X, C) {
		for (int i = 0; i < C; i++) {
			for (int j = 0; j < D; j++) {
				cin >> a[j][i];
			}
		}
		for (int i = 0; i < C; i++) {
			for (int j = 0; j < D; j++) {
				cin >> b[j][i];
			}
		}
		for (int i = 0; i <= D; i++) {
			for (int j = 0; j <= W; j++) {
				for (int k = 0; k <= X; k++) {
					dp[i][j][k] = 0;
				}
			}
		}
		int ret = solve(0, 0, 0);
		cout << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3064
final_rank: 7

code_snippet: #include<iostream>
using namespace std;
int dp[32][52][10],C,D,W,X,e[16][32],f[16][32];
int solve(int d,int w,int p){
	if(w>W)return -999999999;
	if(p>X)return -999999999;
	if(d==D)return 0;
	if(dp[d][w][p]>=0)return dp[d][w][p];
	int maxn=solve(d+1,w,p);
	for(int i=0;i<C;i++){
		for(int j=i;j<C;j++){
			bool OK=false;for(int k=i;k<=j;k++){if(e[k][d]==0)OK=true;}
			if(OK==true)continue;
			int s=0;for(int k=i;k<=j;k++)s+=e[k][d];
			int t=0;for(int k=i;k<=j;k++)t+=f[k][d];
			int u=0;if(j-i>=1)u=1;
			maxn=max(maxn,solve(d+1,w+t,p+u)+s);
		}
	}
	dp[d][w][p]=maxn;
	return maxn;
}
int main(){
	while(true){
		cin>>C>>D>>W>>X;if(C+D+W+X==0)break;
		for(int i=0;i<=D;i++){for(int j=0;j<=W;j++){for(int k=0;k<=X;k++)dp[i][j][k]=(-1LL<<30);}}
		for(int i=0;i<C;i++){for(int j=0;j<D;j++)cin>>e[i][j];}
		for(int i=0;i<C;i++){for(int j=0;j<D;j++)cin>>f[i][j];}
		cout<<solve(0,0,0)<<endl;
	}
}
accuracy: 1
time_ms: 800
memory_kb: 3060
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
	int c,d,w,x;
	while(cin>>c>>d>>w>>x,c+d+w+x){
		vvi dp(x+1,vi(w+1,-inf));
		dp[0][0]=0;
		vvi e(c,vi(d));
		vvi f(c,vi(d));
		rep(i,c)rep(j,d)cin>>e[i][j];
		rep(i,c)rep(j,d)cin>>f[i][j];
		rep(i,d){
			vvi ndp=dp;
			rep(j,c)if(e[j][i]){
				int sume=0,sumf=0;
				loop(k,j,c){
					if(e[k][i]==0)break;
					sume+=e[k][i];
					sumf+=f[k][i];
					rep(l,x+1)rep(q,w+1)if(dp[l][q]!=-inf){
						int L=(j==k?l:l+1);
						if(L>=x+1||q+sumf>=w+1)continue;
						ndp[L][q+sumf]=max(ndp[L][q+sumf],dp[l][q]+sume);
					}
				}
			}
			dp=ndp;
//			rep(j,x+1){
//				rep(k,w+1)cout<<" "<<(dp[j][k]==-inf?0:dp[j][k]);cout<<endl;
//			}
//			cout<<endl;
		}
		int out=0;
		rep(i,x+1)rep(j,w+1)out=max(out,dp[i][j]);
		cout<<out<<endl;
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3176
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

int C,D,W,X;
int E[15][30],F[15][30];

int dp[31][51][6];
int dfs(int day, int cost, int x)
{
    if(dp[day][cost][x]>=0) return dp[day][cost][x];
    if(day==D) return 0;

    int ret=0;

    // ??????????????????
    ret=max(ret,dfs(day+1,cost,x));

    rep(place,C)
    {
        if(E[place][day]==0) continue;

        // 1????????????
        if(cost+F[place][day]<=W) ret=max(ret,dfs(day+1,cost+F[place][day],x)+E[place][day]);

        // ?????°????????????
        if(x==X) continue;

        int tcost=cost+F[place][day];
        int tE=E[place][day];
        int now=place-1;
        while(now>=0)
        {
            if(E[now][day]==0) break;
            if(tcost+F[now][day]>W) break;

            tcost+=F[now][day];
            tE+=E[now][day];
            ret=max(ret,dfs(day+1,tcost,x+1)+tE);
            --now;
        }

        tcost=cost+F[place][day];
        tE=E[place][day];
        now=place+1;
        while(now<C)
        {
            if(E[now][day]==0) break;
            if(tcost+F[now][day]>W) break;

            tcost+=F[now][day];
            tE+=E[now][day];
            ret=max(ret,dfs(day+1,tcost,x+1)+tE);
            ++now;
        }
    }

    return dp[day][cost][x]=ret;
}

int main()
{
    while(scanf(" %d %d %d %d",&C,&D,&W,&X),C)
    {
        rep(i,C)rep(j,D) scanf(" %d", &E[i][j]);
        rep(i,C)rep(j,D) scanf(" %d", &F[i][j]);
        memset(dp,-1,sizeof(dp));
        printf("%d\n", dfs(0,0,0));
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 3124
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

int C,D,W,X;
int E[15][30],F[15][30];

int dp[31][51][6];
int dfs(int day, int cost, int x)
{
    if(dp[day][cost][x]>=0) return dp[day][cost][x];
    if(day==D) return 0;

    int ret=0;

    // ??????????????????
    ret=max(ret,dfs(day+1,cost,x));

    rep(place,C)
    {
        if(E[place][day]==0) continue;

        // 1????????????
        if(cost+F[place][day]<=W) ret=max(ret,dfs(day+1,cost+F[place][day],x)+E[place][day]);

        // ?????°????????????
        if(x==X) continue;

        int tcost=cost+F[place][day];
        int tE=E[place][day];
        int now=place-1;
        while(now>=0)
        {
            if(E[now][day]==0) break;
            if(tcost+F[now][day]>W) break;

            tcost+=F[now][day];
            tE+=E[now][day];
            ret=max(ret,dfs(day+1,tcost,x+1)+tE);

            --now;
        }

        tcost=cost+F[place][day];
        tE=E[place][day];
        now=place+1;
        while(now<C)
        {
            if(E[now][day]==0) break;
            if(tcost+F[now][day]>W) break;

            tcost+=F[now][day];
            tE+=E[now][day];
            ret=max(ret,dfs(day+1,tcost,x+1)+tE);
            ++now;
        }
    }

    dp[day][cost][x]=ret;
    return ret;
}

int main()
{
    while(scanf(" %d %d %d %d",&C,&D,&W,&X),C)
    {
        rep(i,C)rep(j,D) scanf(" %d", &E[i][j]);
        rep(i,C)rep(j,D) scanf(" %d", &F[i][j]);
        memset(dp,-1,sizeof(dp));
        printf("%d\n", dfs(0,0,0));
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 3232
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

int C,D,W,X;
int E[15][30],F[15][30];

int dp[31][51][6];
int dfs(int day, int cost, int x)
{
    if(dp[day][cost][x]>=0) return dp[day][cost][x];
    if(day==D) return 0;

    int ret=0;

    // ??????????????????
    ret=max(ret,dfs(day+1,cost,x));

    rep(place,C)
    {
        if(E[place][day]==0) continue;

        // 1????????????
        if(cost+F[place][day]<=W) ret=max(ret,dfs(day+1,cost+F[place][day],x)+E[place][day]);

        // ?????°????????????
        if(x==X) continue;

        int tcost=cost+F[place][day];
        int tE=E[place][day];
        int now=place-1;
        while(now>=0)
        {
            if(E[now][day]==0) break;
            if(tcost+F[now][day]>W) break;

            tcost+=F[now][day];
            tE+=E[now][day];
            ret=max(ret,dfs(day+1,tcost,x+1)+tE);

            --now;
        }

        tcost=cost+F[place][day];
        tE=E[place][day];
        now=place+1;
        while(now<C)
        {
            if(E[now][day]==0) break;
            if(tcost+F[now][day]>W) break;

            tcost+=F[now][day];
            tE+=E[now][day];
            ret=max(ret,dfs(day+1,tcost,x+1)+tE);
            ++now;
        }
    }

    dp[day][cost][x]=ret;
    return ret;
}

int main()
{
    while(scanf(" %d %d %d %d",&C,&D,&W,&X),C)
    {
        rep(i,C)rep(j,D) scanf(" %d", &E[i][j]);
        rep(i,C)rep(j,D) scanf(" %d", &F[i][j]);

        memset(dp,-1,sizeof(dp));
        int ans=0;
        ans=max(ans,dfs(0,0,0));
        printf("%d\n", ans);
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 3244
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

int C,D,W,X;
int E[15][30],F[15][30];

int dp[31][51][15][6];
int dfs(int day, int cost, int place, int two)
{
    if(dp[day][cost][place][two]>=0) return dp[day][cost][place][two];
    if(day==D) return 0;

    int ret=0;

    // ??????????????????
    rep(i,C) ret=max(ret,dfs(day+1,cost,i,two));

    if(E[place][day]>0)
    {
        // 1????????????
        if(cost+F[place][day]<=W)
        {
            rep(i,C) ret=max(ret,dfs(day+1,cost+F[place][day],i,two)+E[place][day]);
        }

        // ?????°????????????
        if(two<X)
        {
            int tcost=cost+F[place][day];
            int tE=E[place][day];
            int now=place-1;
            while(now>=0)
            {
                if(E[now][day]==0) break;
                if(tcost+F[now][day]>W) break;

                tcost+=F[now][day];
                tE+=E[now][day];
                rep(i,C) ret=max(ret,dfs(day+1,tcost,i,two+1)+tE);

                --now;
            }

            tcost=cost+F[place][day];
            tE=E[place][day];
            now=place+1;
            while(now<C)
            {
                if(E[now][day]==0) break;
                if(tcost+F[now][day]>W) break;

                tcost+=F[now][day];
                tE+=E[now][day];
                rep(i,C) ret=max(ret,dfs(day+1,tcost,i,two+1)+tE);
                ++now;
            }
        }
    }

    dp[day][cost][place][two]=ret;
    return ret;
}

int main()
{
    while(scanf(" %d %d %d %d",&C,&D,&W,&X),C)
    {
        rep(i,C)rep(j,D) scanf(" %d", &E[i][j]);
        rep(i,C)rep(j,D) scanf(" %d", &F[i][j]);

        memset(dp,-1,sizeof(dp));
        int ans=0;
        rep(i,C) ans=max(ans,dfs(0,0,i,0));
        printf("%d\n", ans);
    }
    return 0;
}
accuracy: 1
time_ms: 2260
memory_kb: 3780
final_rank: 20

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int dp[31][51][6];
int main() {
	while (1) {
		for (int i = 0; i < 31; ++i) {
			for (int j = 0; j < 51; ++j) {
				for (int k = 0; k < 6; ++k) {
					dp[i][j][k] = 0;
				}
			}
		}
		int C, D, W, X; cin >> C >> D >> W >> X;
		if (!C)break;
		vector<vector<int>>es(C, vector<int>(D));
		for (int i = 0; i < C; ++i) {
			for (int j = 0; j < D; ++j) {
				cin >> es[i][j];
			}
		}
		vector<vector<int>>fs(C, vector<int>(D));
		for (int i = 0; i < C; ++i) {
			for (int j = 0; j < D; ++j) {
				cin >> fs[i][j];
			}
		}
		for (int d = 0; d < D; ++d) {
			for (int w = 0; w <= W; ++w) {
				for (int k = 0; k <= X; ++k) {
					dp[d + 1][w][k] = max(dp[d + 1][w][k], dp[d][w][k]);
					for (int l = 0; l < C; ++l) {
						int esum = 0;
						int fsum = 0;
						for (int r = l; r < C; ++r) {
							if (es[r][d]) {
								esum += es[r][d];
								fsum += fs[r][d];
							}
							else {
								break;
							}
							const int nextw = w + fsum;
							const int nexte = dp[d][w][k] + esum;
							const int nextk = k + (l == r ? 0 : 1);
							if (nextw <= W&&nextk <= X) {
								dp[d + 1][nextw][nextk] = max(dp[d + 1][nextw][nextk], nexte);
							}
						}
					}
				}
			}
		}
		int ans = 0;
		for (int w = 0; w <= W; ++w) {
			for (int k = 0; k <= X; ++k) {
				ans = max(dp[D][w][k], ans);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3164
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
int E[50][50],F[50][50],mem[31][51][6],used[31][51][6];
int D,c,X,W;

int dfs(int d,int cost,int x){
  if(D==d)return 0;
  if(used[d][cost][x]) return mem[d][cost][x];
  used[d][cost][x]=1;
  
  int res=dfs(d+1,cost,x);  

  for(int i=0;i<c;i++){
    int ncost=cost+F[i][d];
    if(ncost>W)continue;
    if(!E[i][d]||ncost>W)continue;
    res=max(res,dfs(d+1,ncost,x)+E[i][d]);
    
    if(x>=X)continue;
    int ee=E[i][d],cc=ncost;
    for(int j=i+1;j<c;j++){
      ee+=E[j][d],cc+=F[j][d];
      if(!E[j][d]||cc>W)break;
      res=max(res,dfs(d+1,cc,x+1)+ee);
    }
    
    ee=E[i][d],cc=ncost;
    for(int j=i-1;j>=0;j--){
      ee+=E[j][d],cc+=F[j][d];
      if(!E[j][d]||cc>W)break;
      res=max(res,dfs(d+1,cc,x+1)+ee); 
    }
  }
  return mem[d][cost][x]=res;
}

int main(){
  while(1){
    cin>>c>>D>>W>>X;
    if(!c&&!D&&!W&&!X) break;
    memset(used,0,sizeof(used)), memset(mem,0,sizeof(mem));
    for(int i=0;i<c;i++)for(int j=0;j<D;j++) cin>>E[i][j];
    for(int i=0;i<c;i++)for(int j=0;j<D;j++) cin>>F[i][j];
    cout << dfs(0,0,0)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 1252
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
int E[50][50],F[50][50],mem[31][51][6],used[31][51][6];
int D,c,X,W;

int dfs(int d,int cost,int x){
  if(D==d)return 0;
  if(used[d][cost][x]) return mem[d][cost][x];
  used[d][cost][x]=1;
  
  int res=dfs(d+1,cost,x);  

  for(int i=0;i<c;i++){
    int ncost=cost+F[i][d];
    if(ncost>W)continue;
    if(!E[i][d]||ncost>W)continue;
    res=max(res,dfs(d+1,ncost,x)+E[i][d]);
    
    if(x>=X)continue;
    int ee=E[i][d],cc=cost+F[i][d];
    for(int j=i+1;j<c;j++){
      ee+=E[j][d],cc+=F[j][d];
      if(!E[j][d]||cc>W)break;
      res=max(res,dfs(d+1,cc,x+1)+ee);
    }
    
    ee=E[i][d],cc=cost+F[i][d];
    for(int j=i-1;j>=0;j--){
      ee+=E[j][d],cc+=F[j][d];
      if(!E[j][d]||cc>W)break;
      res=max(res,dfs(d+1,cc,x+1)+ee); 
    }
  }
  return mem[d][cost][x]=res;
}

int main(){
  while(1){
    cin>>c>>D>>W>>X;
    if(!c&&!D&&!W&&!X) break;
    memset(used,0,sizeof(used)), memset(mem,0,sizeof(mem));
    for(int i=0;i<c;i++)for(int j=0;j<D;j++) cin>>E[i][j];
    for(int i=0;i<c;i++)for(int j=0;j<D;j++) cin>>F[i][j];
    cout << dfs(0,0,0)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 490
memory_kb: 1252
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;

const int inf = (1 << 25);

int C, D, W, X;
int E[16][32], F[16][32];

int dp[32][64][8];

int solve(int d, int w, int x)
{
  if(w > W || x > X) return -inf;
  if(d == D) return 0;  
  if(~dp[d][w][x]) return dp[d][w][x];
  int ret = solve(d+1, w, x);
  for(int i = 0; i < C; i++){
    int le = 0, lf = 0, re = 0, rf = 0;
    for(int j = 0; j < C; j++){
      if(i - j >= 0 && le >= 0){
	le += E[i-j][d], lf += F[i-j][d];
	ret = max(ret, solve(d+1, w+lf, x+(j!=0)) + le);
      }
      if(i + j < C && re >= 0){
	re += E[i+j][d], rf += F[i+j][d];
	ret = max(ret, solve(d+1, w+rf, x+(j!=0)) + re);
      }
    }
  }
  return dp[d][w][x] = ret;
}

int main()
{
  while(cin >> C >> D >> W >> X, C || D || W || X){
    memset(E, 0, sizeof(E));
    memset(F, 0, sizeof(F));
    for(int i = 0; i < C; i++){
      for(int j = 0; j < D; j++) cin >> E[i][j], E[i][j] = (E[i][j] == 0 ? -inf : E[i][j]);
    }
    for(int i = 0; i < C; i++){
      for(int j = 0; j < D; j++) cin >> F[i][j], F[i][j] = (E[i][j] == -inf ? inf : F[i][j]);
    }
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0, 0) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 900
memory_kb: 1236
final_rank: 6

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
int C, D, W, X;
int E[16][31], F[16][31];
int memo[31][51][6];
const int dx[] = {-1, 1};
 
int solve(int d, int w, int x)
{
    if (d == D) return 0;
 
    int &res = memo[d][w][x];
    if (res != -1) return res;
   
    res = solve(d+1, w, x);
 
    for (int i = 0; i < C; i++) {
        if (E[i][d] == 0) continue;
        if (w + F[i][d] <= W) {
            res = max(res, solve(d+1, w + F[i][d], x) + E[i][d]);      
        }    
        if (x + 1 > X) continue;
        for (int j = 0; j < 2; j++) {
            int nx = i, cost = 0, f = 0;
            while (1) {
                if (nx < 0 || nx >= C || E[nx][d] == 0) break;
                f += F[nx][d];
                cost += E[nx][d];
                if (w + f <= W) {
                    res = max(res, solve(d+1, w + f, x+1) + cost);
                }
                nx += dx[j];
            }
        }   
    }
    return res;
}
 
int main()
{
    while (cin >> C >> D >> W >> X, C) {
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> E[i][j];
            }
        }
 
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> F[i][j];
            }
        }
 
        memset(memo, -1, sizeof(memo));
     
        int res = solve(0, 0, 0);
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 3116
final_rank: 17

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
int C, D, W, X;
int E[16][31], F[16][31];
int memo[31][51][6];
const int dx[] = {-1, 1};
 
int solve(int d, int w, int x)
{
    if (d == D) return 0;
 
    int &res = memo[d][w][x];
    if (res != -1) return res;
   
    res = solve(d+1, w, x);
 
    for (int i = 0; i < C; i++) {
        if (w + F[i][d] <= W && E[i][d] != 0) {
            res = max(res, solve(d+1, w + F[i][d], x) + E[i][d]);      
        }    
        if (x + 1 > X) continue;
        for (int j = 0; j < 2; j++) {
            int nx = i, cost = 0, f = 0;
            while (1) {
                if (nx < 0 || nx >= C || E[nx][d] == 0) break;
                f += F[nx][d];
                cost += E[nx][d];
                if (w + f <= W) {
                    res = max(res, solve(d+1, w + f, x+1) + cost);
                }
                nx += dx[j];
            }
        }   
    }
    return res;
}
 
int main()
{
    while (cin >> C >> D >> W >> X, C) {
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> E[i][j];
            }
        }
 
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> F[i][j];
            }
        }
 
        memset(memo, -1, sizeof(memo));
     
        int res = solve(0, 0, 0);
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 3116
final_rank: 18

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
int C, D, W, X;
int E[16][31], F[16][31];
int memo[31][51][6];
const int dx[] = {-1, 1};
 
int solve(int d, int w, int x)
{
    if (d == D) return 0;
 
    int &res = memo[d][w][x];
    if (res != -1) return res;
   
    res = solve(d+1, w, x);
 
    for (int i = 0; i < C; i++) {
        if (w + F[i][d] <= W && E[i][d] != 0) {
            res = max(res, solve(d+1, w + F[i][d], x) + E[i][d]);      
        }    
    }
 
    if (x+1 <= X) {
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < 2; j++) {
                int nx = i, cost = 0, f = 0;
                while (1) {
                    if (nx < 0 || nx >= C || E[nx][d] == 0) break;
                    f += F[nx][d];
                    cost += E[nx][d];
                    if (w + f <= W) {
                        res = max(res, solve(d+1, w + f, x+1) + cost);
                    }
                    nx += dx[j];
                }
            }   
        }
    }
    return res;
}
 
int main()
{
    while (cin >> C >> D >> W >> X, C) {
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> E[i][j];
            }
        }
 
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> F[i][j];
            }
        }
 
        memset(memo, -1, sizeof(memo));
     
        int res = solve(0, 0, 0);
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 3028
final_rank: 15

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
ll C, D, W, X;
ll E[16][31], F[16][31];
ll memo[31][51][6];
const int dx[] = {-1, 1};
 
ll solve(ll d, ll w, ll x)
{
    if (d == D) return 0;
 
    ll &res = memo[d][w][x];
    if (res != -1) return res;
   
    res = 0;
    res = max(res, solve(d+1, w, x));
 
    for (int i = 0; i < C; i++) {
        if (w + F[i][d] <= W && E[i][d] != 0) {
            res = max(res, solve(d+1, w + F[i][d], x) + E[i][d]);      
        }    
    }
 
    if (x+1 <= X) {
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < 2; j++) {
                ll nx = i, cost = 0, f = 0;
                while (1) {
                    if (nx < 0 || nx >= C || E[nx][d] == 0) break;
                    f += F[nx][d];
                    cost += E[nx][d];
                    if (w + f <= W) {
                        res = max(res, solve(d+1, w + f, x+1) + cost);
                    }
                    nx += dx[j];
                }
            }   
        }
    }
    return res;
}
 
int main()
{
    while (cin >> C >> D >> W >> X, C) {
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> E[i][j];
            }
        }
 
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < D; j++) {
                cin >> F[i][j];
            }
        }
 
        memset(memo, -1, sizeof(memo));
     
        ll res = solve(0, 0, 0);
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 3100
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int main() {
  ll n,m,w,x;
  while(cin >> m >> n >> w >> x && n) {
    ll e[n][m],f[n][m];
    for(int i=0; i<m; i++) {
      for(int j=0; j<n; j++) cin >> e[j][i];
    }
    for(int i=0; i<m; i++) {
      for(int j=0; j<n; j++) cin >> f[j][i];
    }
    ll dp[n+1][w+1][x+1],ans=0;
    for(int i=0;i<=n;i++)for(int j=0;j<=w;j++)for(int k=0;k<=x;k++)dp[i][j][k]=-1;
    dp[0][0][0]=0;
    for(int t=0; t<n; t++) {
      for(int l=0; l<=w; l++) {
        for(int k=0; k<=x; k++) {
          if(dp[t][l][k]==-1) continue;
          dp[t+1][l][k]=max(dp[t+1][l][k],dp[t][l][k]);
          for(int i=0; i<m; i++) {
            ll z=0,y=0,xx=0;
            for(int j=i; j<m; j++) {
              if(!e[t][j]) break;
              if(j!=i) y=1;
              xx+=e[t][j];
              z+=f[t][j];
              if(z+l<=w&&k+y<=x) dp[t+1][z+l][k+y]=max(dp[t+1][z+l][k+y],dp[t][l][k]+xx);
            }
          }
        }
      }
    }
    for(int l=0;l<=w;l++)for(int k=0;k<=x;k++)ans=max(ans,dp[n][l][k]);
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 1232
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll C, D, W, X;
ll E[16][31], F[16][31];
ll memo[31][51][6];
const int dx[] = {-1, 1};

ll solve(ll d, ll w, ll x)
{
  if (d == D) return 0;

  ll &res = memo[d][w][x];
  if (res != -1) return res;
  
  res = 0;
  res = max(res, solve(d+1, w, x));

  for (int i = 0; i < C; i++) {
    if (w + F[i][d] <= W && E[i][d] != 0) {
      res = max(res, solve(d+1, w + F[i][d], x) + E[i][d]);      
    }    
  }

  if (x+1 <= X) {
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < 2; j++) {
	ll nx = i, cost = 0, f = 0;
	while (1) {
	  if (nx < 0 || nx >= C || E[nx][d] == 0) break;
	  f += F[nx][d];
	  cost += E[nx][d];
	  if (w + f <= W) {
	    res = max(res, solve(d+1, w + f, x+1) + cost);
	  }
	  nx += dx[j];
	}
      }   
    }
  }
  return res;
}

int main()
{
  while (cin >> C >> D >> W >> X, C) {
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < D; j++) {
	cin >> E[i][j];
      }
    }

    for (int i = 0; i < C; i++) {
      for (int j = 0; j < D; j++) {
	cin >> F[i][j];
      }
    }

    memset(memo, -1, sizeof(memo));
    
    ll res = solve(0, 0, 0);
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 1244
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll n,m,w,x;
  while(cin >> m >> n >> w >> x && n) {
    ll e[n][m],f[n][m];
    for(int i=0; i<m; i++) {
      for(int j=0; j<n; j++) cin >> e[j][i];
    }
    for(int i=0; i<m; i++) {
      for(int j=0; j<n; j++) cin >> f[j][i];
    }
    ll dp[n+1][w+1][x+1],ans=0;
    for(int i=0;i<=n;i++)for(int j=0;j<=w;j++)for(int k=0;k<=x;k++)dp[i][j][k]=-1;
    dp[0][0][0]=0;
    for(int t=0; t<n; t++) {
      for(int l=0; l<=w; l++) {
        for(int k=0; k<=x; k++) {
          if(dp[t][l][k]==-1) continue;
          dp[t+1][l][k]=max(dp[t+1][l][k],dp[t][l][k]);
          for(int i=0; i<m; i++) {
            ll z=0,y=0,xx=0;
            for(int j=i; j<m; j++) {
              if(!e[t][j]) break;
              if(j!=i) y=1;
              xx+=e[t][j];
              z+=f[t][j];
              if(z+l<=w&&k+y<=x) dp[t+1][z+l][k+y]=max(dp[t+1][z+l][k+y],dp[t][l][k]+xx);
            }
          }
        }
      }
    }
    for(int l=0;l<=w;l++)for(int k=0;k<=x;k++)ans=max(ans,dp[n][l][k]);
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 1236
final_rank: 2