Problem:
Problem G:
Oil Company
Irving & Cohen Petroleum Corporation has decided to develop a new oil field in an area. A preliminary survey
has been done and they created a detailed grid map of the area which indicates the reserve of oil.
They are now planning to construct mining plants on several grid blocks according this map, but they decided
not to place any two plants on adjacent positions to avoid spreading of fire in case of blaze. Two blocks are
considered to be adjacent when they have a common edge. You are one of the programmers working for the
company and your task is to write a program which calculates the maximum amount of oil they can mine, given
the map of the reserve.
Input
The first line of the input specifies N, the number of test cases. Then
N
test cases follow, each of which looks
like the following:
W H
r
1,1
r
2,1
. . .
r
W
,1
...
r
1,
H
r
2,
H
. . .
r
W
,
H
The first line of a test case contains two integers
W
and
H
(1 ≤
W
,
H
≤ 20). They specifies the dimension of
the area. The next
H
lines, each of which contains
W
integers, represent the map of the area. Each integer
r
x
,
y
(0 ≤
r
x
,
y
< 10000) indicates the oil reserve at the grid block (
x
,
y
).
Output
For each test case, output the case number (starting from 1) and the maximum possible amount of mining in a
line. Refer to the sample output section about the format.
Sample Input
2
2 2
2 3
3 5
3 2
4 1 1
2 1 4
Output for the Sample Input
Case 1: 7
Case 2: 8

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 W,H;
int POW[21],dp[1048576],next_dp[1048576];
int table[20][20];
vector<int> V;
int case_num = 1;
 
void recursive(int array[],int index){
 
    if(index == W){
        int code = 0;
        for(int i = 0; i < W; i++){
            if(array[i] == 1)code += POW[i];
        }
        V.push_back(code);
        return;
    }
 
    if(array[index-1] == 1){
        int next_array[W];
        for(int i = 0; i < index; i++)next_array[i] = array[i];
        next_array[index] = 0;
        recursive(next_array,index+1);
    }else{
 
        int next_array1[W];
        for(int i = 0; i < index; i++)next_array1[i] = array[i];
        next_array1[index] = 1;
        recursive(next_array1,index+1);
 
        int next_array2[W];
        for(int i = 0; i < index; i++)next_array2[i] = array[i];
        next_array2[index] = 0;
        recursive(next_array2,index+1);
    }
}
 
void func(){
 
    scanf("%d %d",&W,&H);
    if(W > H){
 
        for(int row = 0; row < H; row++){
            for(int col = 0; col < W; col++)scanf("%d",&table[col][row]);
        }
        swap(W,H);
    }else{
        for(int row = 0; row < H; row++){
            for(int col = 0; col < W; col++)scanf("%d",&table[row][col]);
        }
    }
 
    int first_array0[W],first_array1[W];
 
    V.clear();
 
    first_array0[0] = 0;
    recursive(first_array0,1);
    first_array1[0] = 1;
    recursive(first_array1,1);
 
 
    sort(V.begin(),V.end());
    V.erase(unique(V.begin(),V.end()),V.end());
 
 
    for(int i = 0; i < V.size(); i++){
        dp[V[i]] = 0;
        next_dp[V[i]] = 0;
    }
 
    int state,sum,maximum;
 
    for(int row = 0; row < H; row++){
 
        for(int i = 0; i < V.size(); i++){
            state = V[i];
            sum = 0;
            for(int loop = 0; loop < W; loop++){
                if(state & (1 << loop)){
                    sum += table[row][loop];
                }
            }
            next_dp[state] = sum;
 
            maximum = 0;
            for(int k = 0; k < V.size(); k++){
                if((state&V[k])==0){
                    maximum = max(maximum,dp[V[k]]);
                }
            }
            next_dp[state] += maximum;
        }
        for(int i = 0; i < V.size(); i++){
            dp[V[i]] = next_dp[V[i]];
        }
    }
 
    int ans = 0;
    for(int i = 0; i < V.size(); i++){
        ans = max(ans,dp[V[i]]);
    }
 
    printf("Case %d: %d\n",case_num++,ans);
 
}
 
int main(){
 
    for(int i = 0; i < 21; i++)POW[i] = pow(2,i);
 
    int data_set;
    scanf("%d",&data_set);
 
    for(int i = 0; i < data_set; i++)func();
 
    return 0;
}
accuracy: 1
time_ms: 7870
memory_kb: 4860
final_rank: 15

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 W,H;
int POW[21],dp[1048576],next_dp[1048576];
int table[20][20];
vector<int> V;
int case_num = 1;

void recursive(int array[],int index){

	if(index == W){
		int code = 0;
		for(int i = 0; i < W; i++){
			if(array[i] == 1)code += POW[i];
		}
		V.push_back(code);
		return;
	}

	if(array[index-1] == 1){
		int next_array[W];
		for(int i = 0; i < index; i++)next_array[i] = array[i];
		next_array[index] = 0;
		recursive(next_array,index+1);
	}else{

		int next_array1[W];
		for(int i = 0; i < index; i++)next_array1[i] = array[i];
		next_array1[index] = 1;
		recursive(next_array1,index+1);

		int next_array2[W];
		for(int i = 0; i < index; i++)next_array2[i] = array[i];
		next_array2[index] = 0;
		recursive(next_array2,index+1);
	}
}

void func(){

	scanf("%d %d",&W,&H);
	if(W > H){

		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++)scanf("%d",&table[col][row]);
		}
		swap(W,H);
	}else{
		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++)scanf("%d",&table[row][col]);
		}
	}

	int first_array0[W],first_array1[W];

	V.clear();

	first_array0[0] = 0;
	recursive(first_array0,1);
	first_array1[0] = 1;
	recursive(first_array1,1);


	sort(V.begin(),V.end());
	V.erase(unique(V.begin(),V.end()),V.end());


	for(int i = 0; i < V.size(); i++){
		dp[V[i]] = 0;
		next_dp[V[i]] = 0;
	}

	int state,sum,maximum;

	for(int row = 0; row < H; row++){

		for(int i = 0; i < V.size(); i++){
			state = V[i];
			sum = 0;
			for(int loop = 0; loop < W; loop++){
				if(state & (1 << loop)){
					sum += table[row][loop];
				}
			}
			next_dp[state] = sum;

			maximum = 0;
			for(int k = 0; k < V.size(); k++){
				if((state&V[k])==0){
					maximum = max(maximum,dp[V[k]]);
				}
			}
			next_dp[state] += maximum;
		}
		for(int i = 0; i < V.size(); i++){
			dp[V[i]] = next_dp[V[i]];
		}
	}

	int ans = 0;
	for(int i = 0; i < V.size(); i++){
		ans = max(ans,dp[V[i]]);
	}

	printf("Case %d: %d\n",case_num++,ans);

}

int main(){

	for(int i = 0; i < 21; i++)POW[i] = pow(2,i);

	int data_set;
	scanf("%d",&data_set);

	for(int i = 0; i < data_set; i++)func();

	return 0;
}
accuracy: 1
time_ms: 7670
memory_kb: 5008
final_rank: 14

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

int dp[2][1<<21];
int main() {
  int T,tt=1;
  cin >> T;
  while(T--) {
    int n,m;
    cin >> m >> n;
    int w=m+1;
    int a[n][m];
    for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin >> a[i][j];
    int k=0,l=1;
    memset(dp,0,sizeof(dp));
    int ans=0;
    for(int i=0; i<n*m; i++) {
      int x=i/m,y=i%m;
      memset(dp[l],0,sizeof(dp[l]));
      for(int t=0; t<(1<<w); t++) {
        int s=t|(1<<w);s/=2;
        if(!((y&&(t&(1<<(w-1))))||(t&2))) {
          if(dp[l][s]<dp[k][t]+a[x][y]) dp[l][s]=dp[k][t]+a[x][y];
          if(ans<dp[l][s]) ans=dp[l][s];
        }
        if(dp[l][t/2]<dp[k][t]) dp[l][t/2]=dp[k][t];
      }
      swap(k,l);
    }
    cout << "Case " << tt++ << ": " << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 6550
memory_kb: 19484
final_rank: 20

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int dp[1 << 20], prev_[1 << 20], a[20][20], H, W;
int main() {
	int T; cin >> T;
	for (int i = 1; i <= T; i++) {
		cin >> W >> H;
		for (int i = 0; i < (1 << W); i++)dp[i] = -1LL << 30;
		for (int i = 0; i < (1 << W); i++)prev_[i] = -1LL << 30;
		for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++)cin >> a[i][j]; }
		prev_[0] = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				for (int k = 0; k < (1 << W); k++) {
					if (prev_[k] <= (-1LL << 20))continue;
					int bit[20]; for (int l = 0; l < W; l++)bit[l] = (k / (1 << l)) % 2;
					if (bit[j] != 1 && (j == 0 || bit[j - 1] != 1)) {
						dp[k + (1 << j)] = max(dp[k + (1 << j)], prev_[k] + a[i][j]);
					}
					dp[k - (1 << j)*bit[j]] = max(dp[k - (1 << j)*bit[j]], prev_[k]);
				}
				for (int k = 0; k < (1 << W); k++) { prev_[k] = dp[k]; dp[k] = -1LL << 30; }
			}
		}
		int maxn = 0; for (int i = 0; i < (1 << W); i++)maxn = max(maxn, prev_[i]);
		cout << "Case " << i << ": " << maxn << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 3470
memory_kb: 11268
final_rank: 10

code_snippet: #include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int Q, H, W, r, a[409], pre[1048588], dp[1048588];
int main() {
	cin >> Q;
	int cnt = 0;
	while (Q--) {
		cin >> W >> H; r = (1 << W) - 1;
		for (int i = 0; i < H * W; i++) cin >> a[i];
		fill(pre, pre + (1 << W), -999999999); pre[0] = 0; pre[1] = a[0];
		for (int i = 0; i < H * W - 1; i++) {
			fill(dp, dp + (1 << W), -999999999);
			for (int j = 0; j < 1 << W; j++) {
				if (pre[j] >= 0) {
					dp[(j * 2) & r] = max(dp[(j * 2) & r], pre[j]);
					bool flag = true;
					if (i % W != W - 1 && (j & 1)) flag = false;
					if (j & (1 << (W - 1))) flag = false;
					if (flag) {
						dp[(j * 2 + 1) & r] = max(dp[(j * 2 + 1) & r], pre[j] + a[i + 1]);
					}
				}
			}
			for (int j = 0; j < 1 << W; j++) pre[j] = dp[j];
		}
		int ret = *max_element(pre, pre + (1 << W));
		cout << "Case " << ++cnt << ": " << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 1660
memory_kb: 11136
final_rank: 5

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

using namespace std;

const void chmax(int &a, int b)
{
  a = max(a, b);
}

int main()
{
  int Q, W, H, r[20][20];
  int dp[2][1 << 20];

  cin >> Q;
  for(int c = 1; c <= Q; c++) {
    cin >> W >> H;
    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        cin >> r[i][j];
      }
    }

    memset(dp, -1, sizeof(dp));
    int *now = dp[0], *nxt = dp[1];
    int mask = (1 << W) - 1;
    now[0] = 0;

    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        for(int k = 0; k < 1 << W; k++) {
          if(now[k] == -1) continue;
          int tugi = (k << 1) & mask;
          chmax(nxt[tugi], now[k]);
          if(j > 0 && k & 1) continue;
          if((k >> (W - 1)) & 1) continue;
          chmax(nxt[tugi | 1], now[k] + r[i][j]);
        }
        swap(now, nxt);
        for(int k = 0; k < 1 << W; k++) nxt[k] = -1;
      }
    }
    cout << "Case " << c << ": " << *max_element(now, now + (1 << W)) << endl;
  }
}
accuracy: 1
time_ms: 1830
memory_kb: 11260
final_rank: 6

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
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.txt"


vector<vector<int>>memo;
int main() {
	int N; cin >> N;
	for (int num = 0; num < N;++num){
		int W, H; cin >> W >> H;
		vector<vector<int>>field(H, vector<int>(W));
		for (int y = 0; y < H; ++y) {
			for (int x = 0; x < W; ++x) {
				cin >> field[y][x];
			}
		}
		memo = vector<vector<int>>(2, vector<int>(1 << W));
		for (int y = 0; y < H; ++y) {
			for (int x = 0; x < W; ++x) {
				
				const int i = y*W + x;
				int tar = i & 1, cur = tar ^ 1;
				for (int i = 0; i < (1 << W); ++i) {
					memo[cur][i] = 0;
				}
				if (i == 0)assert(tar == 0 && cur == 1);
				for (int pre = 0; pre < (1 << W); ++pre) {
					if (((x==0)||!(pre & 1)) && !(pre&(1 << (W-1)))) {
						memo[cur][((pre << 1) + 1) & ((1 << W) - 1)] = max(memo[cur][((pre << 1) + 1) & ((1 << W) - 1)], memo[tar][pre] + field[y][x]);
					}
					memo[cur][(pre << 1)&((1<<W)-1)] = max(memo[cur][(pre << 1)&((1 << W) - 1)], memo[tar][pre]);
				}
			}
		}
		int ans=0;
		for (int i = 0; i < (1 << W); ++i) {
			ans = max(ans, memo[(W*H) % 2][i]);
		}
		cout << "Case " << num+1 << ": " << ans << endl;
		
	}
	return 0;
}
accuracy: 1
time_ms: 3430
memory_kb: 19248
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
int dp[2][1<<21];
void Main() {
  int T,tt=1;
  R T;
  while(T--) {
    int n,m;
    cin >> m >> n;
    int w=m+1;
    int a[n][m];
    rep(i,n)rep(j,m) R a[i][j];
    int k=0,l=1;
    rep(t,1<<w)dp[0][t]=dp[1][t]=0;
    int ans=0;
    rep(i,n*m) {
      int x=i/m,y=i%m;
      rep(t,1<<w)dp[l][t]=0;
      rep(t,1<<w) {
        int s=t|(1<<w);s/=2;
        if(!((y&&(t&(1<<(w-1))))||(t&2))) {
          if(dp[l][s]<dp[k][t]+a[x][y]) dp[l][s]=dp[k][t]+a[x][y];
          if(ans<dp[l][s]) ans=dp[l][s];
        }
        if(dp[l][t/2]<dp[k][t]) dp[l][t/2]=dp[k][t];
      }
      k=l;l^=1;
    }
    cout << "Case " << tt++ << ": " << ans << endl;
  }
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}
accuracy: 1
time_ms: 6410
memory_kb: 17584
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int dp[2][76866],dg[76866],bit[76866],pos[1<<20],n,cnt=0,dgtmp=0,tmp=1;
int main(){
	for(int i=0;i<(1<<20);i++){
		int f=0;
		for(int j=0,b1=1,b2=2;j<20;j++,b1<<=1,b2<<=1)if((i&b1)&&(i&b2))f++;
		if(f<2){
			pos[i]=cnt;
			if(i==tmp){
				dgtmp++;
				tmp<<=1;
			}
			dg[cnt]=dgtmp;
			bit[cnt]=i;
			cnt++;
		}
	}
	cin>>n;
	for(int cs=1;cs<=n;cs++){
		int ans=0;
		int w,h;cin>>w>>h;
		int p=(1<<(w-1)),v[400];
		for(int i=0;i<w*h;i++)cin>>v[i];
		for(int i=0;i<76866;i++)dp[0][i]=dp[1][i]=-1;
		dp[0][0]=0;
		for(int step=0;step<=w*h;step++){
			int now=step%2,nxt=(step+1)%2;
			if(step==w*h){
				for(int i=0;i<76866;i++){
					if(w<dg[i])break;
					ans=max(ans,dp[now][i]);
				}
				break;
			}
			for(int i=0;i<76866;i++){
				if(w<dg[i])break;
				if(dp[now][i]<0)continue;
				int flg=bit[i];
				if(((flg&p)==0)&&(((flg&1)==0)||((step%w)==0))){
					int nxtflg=(flg<<1)+1;
					dp[nxt][pos[nxtflg]]=max(dp[nxt][pos[nxtflg]],dp[now][i]+v[step]);
				}
				if(flg&p){
					int nxtflg=((flg^p)<<1);
					dp[nxt][pos[nxtflg]]=max(dp[nxt][pos[nxtflg]],dp[now][i]);
				}
				else{
					int nxtflg=(flg<<1);
					dp[nxt][pos[nxtflg]]=max(dp[nxt][pos[nxtflg]],dp[now][i]);
				}
				dp[now][i]=-1;
			}
		}
		cout<<"Case "<<cs<<": "<<ans<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 5284
final_rank: 2

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

int dp[2][76866];
int dg[76866];
int bit[76866];
int pos[1<<20];

void init(){
	int cnt=0;
	int dgtmp=0,tmp=1;
	for(int i=0;i<(1<<20);i++){
		int f=0;
		for(int j=0,b1=1,b2=2;j<20;j++,b1<<=1,b2<<=1){
			if((i&b1)&&(i&b2))f++;
		}
		if(f<2){
			pos[i]=cnt;
			if(i==tmp){
				dgtmp++;
				tmp<<=1;
			}
			dg[cnt]=dgtmp;
			bit[cnt]=i;
			cnt++;
		}
	}
}

int main(){
	
	init();
	
	int n;cin>>n;
	for(int cs=1;cs<=n;cs++){
		int ans=0;
		int w,h;cin>>w>>h;
		
		int p=(1<<(w-1));
		
		int v[400];
		for(int i=0;i<w*h;i++)cin>>v[i];
		
		for(int i=0;i<76866;i++){
			dp[0][i]=-1;
			dp[1][i]=-1;
		}
		
		dp[0][0]=0;
		
		for(int step=0;step<=w*h;step++){
			int now=step%2,nxt=(step+1)%2;
			
			if(step==w*h){
				for(int i=0;i<76866;i++){
					if(w<dg[i])break;
					ans=max(ans,dp[now][i]);
				}
				break;
			}
			
			for(int i=0;i<76866;i++){
				
				if(w<dg[i])break;
				if(dp[now][i]<0)continue;
				
				int flg=bit[i];
				//??????????????????
				
				if(((flg&p)==0)&&(((flg&1)==0)||((step%w)==0))){
					
					int nxtflg=(flg<<1)+1;
					dp[nxt][pos[nxtflg]]=max(dp[nxt][pos[nxtflg]],dp[now][i]+v[step]);
				}
				
				if(flg&p){
					int nxtflg=((flg^p)<<1);
					dp[nxt][pos[nxtflg]]=max(dp[nxt][pos[nxtflg]],dp[now][i]);
				}
				else{
					int nxtflg=(flg<<1);
					dp[nxt][pos[nxtflg]]=max(dp[nxt][pos[nxtflg]],dp[now][i]);
				}
				
				
				//???????????§??????
				
				dp[now][i]=-1;
			}
			
		}
		
		cout<<"Case "<<cs<<": "<<ans<<endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 5284
final_rank: 3

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_W = 20;
const int MAX_H = 20;
const int MAX_BITS = 1 << MAX_W;

/* typedef */

/* global variables */

int n, h, w;
int rs[MAX_H][MAX_W], dp[2][MAX_BITS];

/* subroutines */

/* main */

int main() {
  cin >> n;

  for (int cn = 1; cn <= n; cn++) {
    cin >> w >> h;
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) cin >> rs[y][x];

    memset(dp, 0, sizeof(dp));

    int prv = 0, cur = 1;
    int wbits = (1 << w) - 1, bw = 1 << (w - 1);

    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
	memset(dp[cur], 0, sizeof(int) * (wbits + 1));

	for (int bits = 0; bits <= wbits; bits++) {
	  int cbits = (bits << 1) & wbits;

	  if (dp[cur][cbits] < dp[prv][bits])
	    dp[cur][cbits] = dp[prv][bits];

	  if ((y == 0 || ! (bits & bw)) && (x == 0 || ! (bits & 1))) {
	    int ndp = dp[prv][bits] + rs[y][x];
	    if (dp[cur][cbits | 1] < ndp)
	      dp[cur][cbits | 1] = ndp;
	  }
	}

	prv ^= 1;
	cur ^= 1;
      }

    int max_dp = 0;
    for (int bits = 0; bits <= wbits; bits++)
      if (max_dp < dp[prv][bits]) max_dp = dp[prv][bits];

    printf("Case %d: %d\n", cn, max_dp);
  }

  return 0;
}
accuracy: 1
time_ms: 2730
memory_kb: 9344
final_rank: 8

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int D_MAX_V=1010;
const int D_v_size=1010;
struct D_wolf{
	int t,c,r;
	D_wolf(){t=c=r=0;}
	D_wolf(int t1,int c1,int r1){
		t=t1;c=c1;r=r1;
	}
};
vector<D_wolf>D_G[D_MAX_V];
int D_level[D_MAX_V];
int D_iter[D_MAX_V];

void add_edge(int from,int to,int cap){
	D_G[from].push_back(D_wolf(to,cap,D_G[to].size()));
	D_G[to].push_back(D_wolf(from,0,D_G[from].size()-1));
}
void D_bfs(int s){
	for(int i=0;i<D_v_size;i++)D_level[i]=-1;
	queue<int> Q;
	D_level[s]=0;
	Q.push(s);
	while(Q.size()){
		int v=Q.front();
		Q.pop();
		for(int i=0;i<D_G[v].size();i++){
			if(D_G[v][i].c>0&&D_level[D_G[v][i].t]<0){
				D_level[D_G[v][i].t]=D_level[v]+1;
				Q.push(D_G[v][i].t);
			}
		}
	}
}
int D_dfs(int v,int t,int f){
	if(v==t)return f;
	for(;D_iter[v]<D_G[v].size();D_iter[v]++){
		int i=D_iter[v];
		if(D_G[v][i].c>0&&D_level[v]<D_level[D_G[v][i].t]){
			int d=D_dfs(D_G[v][i].t,t,min(f,D_G[v][i].c));
			if(d>0){
				D_G[v][i].c-=d;
				D_G[D_G[v][i].t][D_G[v][i].r].c+=d;
				return d;
			}
		}
	}
	return 0;
}
int max_flow(int s,int t){
	int flow=0;
	for(;;){
		D_bfs(s);
		if(D_level[t]<0)return flow;
		for(int i=0;i<D_v_size;i++)D_iter[i]=0;
		int f;
		while((f=D_dfs(s,t,99999999))>0){flow+=f;}
	}
	return 0;
}
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int m[30][30];
int main(){
	int T;scanf("%d",&T);
	for(int tc=1;tc<=T;tc++){
		int a,b;scanf("%d%d",&b,&a);
		for(int i=0;i<a;i++)for(int j=0;j<b;j++)scanf("%d",&m[i][j]);
		int s=a*b;
		int t=a*b+1;
		for(int i=0;i<D_MAX_V;i++){
			D_G[i].clear();
			D_level[i]=D_iter[i]=0;
		}
		int INF=99999999;
		int sum=0;
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				sum+=m[i][j];
				if((i+j)%2==0){
					add_edge(s,i*b+j,m[i][j]);
					for(int k=0;k<4;k++){
						if(0<=i+dx[k]&&i+dx[k]<a&&0<=j+dy[k]&&j+dy[k]<b)
							add_edge(i*b+j,(i+dx[k])*b+j+dy[k],INF);
					}
				}else add_edge(i*b+j,t,m[i][j]);
			}
		}
		printf("Case %d: %d\n",tc,sum-max_flow(s,t));
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1188
final_rank: 1

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) 
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }


int N;
int n, m;
int fi[25][25];
int now[(1<<20)+10], next[(1<<20)+10];

int main() {
    //freopen( "/Users/macuser/Dropbox/Contest/input.in", "r", stdin );
    cin >> N;
    for (int it = 0; it < N; ++it) {
        cin >> m >> n;
        for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> fi[i][j];
        memset(now, -1, sizeof(now));
        memset(next, -1, sizeof(next));
        
        int res = 0;
        now[0] = 0;
        for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
            memset(next, -1, sizeof(next));
            for (int bit = 0; bit < (1<<m); ++bit) {
                if (now[bit] == -1) continue;
                chmax(next[bit&~(1<<j)], now[bit]);
                if ( !(bit & (1<<j)) && (j == 0 || !(bit & (1<<(j-1)))) ) chmax(next[bit|(1<<j)], now[bit] + fi[i][j]);
            }
            for (int bit = 0; bit < (1<<m); ++bit) { 
                now[bit] = next[bit]; 
                chmax(res, now[bit]); 
                //cout << i << ", " << j << ", " << bitset<3>(bit) << " : " << now[bit] << endl;
            }
        }
        
        cout << "Case " << it+1 << ": " << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 3240
memory_kb: 9360
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>
#include<cmath>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define MAX (1<<20)

using namespace std;

typedef long long ll;

ll dp[MAX][2],ans;
int N,H,W,r[20][20],CNT=1;

int main(){
  cin >> N;
  while(N--){
    ans = 0;
    cin >> W >> H;
    rep(i,H)rep(j,W)cin >> r[i][j];
    rep(i,MAX)rep(j,2)dp[i][j] = 0;
    rep(i,(1<<W)){
      ll cost = 0;
      bool out = false;
      rep(j,W){
	if( ( (i>>j)&1 ) && ( j-1>=0 && ((i>>(j-1))&1) ) ){
	  out = true;
	  break;
	}
	if( (i>>j)&1 )cost += r[0][j];
      }
      if(out)continue;
      dp[i][0] = cost;
      ans = max(ans,cost);
    }
    int bitmask = (1<<W)-1;
    bool phase = 0;
    REP(y,1,H){
      rep(x,W){
	rep(state,(1<<W)){
	  // not use
	  int nstate = (state>>1)&bitmask;
	  dp[nstate][!phase] = max(dp[nstate][!phase],dp[state][phase]);
	  ans = max(ans,dp[nstate][!phase]);

	  // use
	  if( (state&1) || ( ( (state>>(W-1))&1 ) && x-1>=0  ) )continue;
	  nstate |= (1<<(W-1));
	  dp[nstate][!phase] = max(dp[nstate][!phase],dp[state][phase]+r[y][x]);
	  ans = max(ans,dp[nstate][!phase]);
	}
	rep(i,(1<<W))dp[i][phase] = 0;
	phase = !phase;
      }
    }

    cout << "Case " << CNT++ << ": " << ans << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 5170
memory_kb: 17548
final_rank: 17

code_snippet: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<climits>
#include<cassert>
#include<bitset>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX (1<<20)

using namespace std;

typedef long long ll;

ll dp[MAX][2],ans;
int N,H,W,r[20][20],CNT=1;

int main(){
  cin >> N;
  while(N--){
    ans = 0;
    cin >> W >> H;
    rep(i,H)rep(j,W)cin >> r[i][j];
    rep(i,MAX)rep(j,2)dp[i][j] = 0;
    rep(i,(1<<W)){
      ll cost = 0;
      bool out = false;
      rep(j,W){
	if( ( (i>>j)&1 ) && ( j-1>=0 && ((i>>(j-1))&1) ) ){
	  out = true;
	  break;
	}
	if( (i>>j)&1 )cost += r[0][j];
      }
      if(out)continue;
      dp[i][0] = cost;
      ans = max(ans,cost);
    }
    int bitmask = (1<<W)-1;
    bool phase = 0;
    REP(y,1,H){
      rep(x,W){
	rep(state,(1<<W)){
	  // not use
	  int nstate = (state>>1)&bitmask;
	  dp[nstate][!phase] = max(dp[nstate][!phase],dp[state][phase]);
	  ans = max(ans,dp[nstate][!phase]);

	  // use y                      x
	  if( (state&1) || ( ( (state>>(W-1))&1 ) && x-1>=0  ) )continue;
	  nstate |= (1<<(W-1));
	  dp[nstate][!phase] = max(dp[nstate][!phase],dp[state][phase]+r[y][x]);
	  ans = max(ans,dp[nstate][!phase]);
	}
	rep(i,(1<<W))dp[i][phase] = 0;
	phase = !phase;
      }
    }

    cout << "Case " << CNT++ << ": " << ans << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 5170
memory_kb: 17548
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int dp[2][1<<20];

int main(void){

	int n;
	cin >> n;
	for(int tc=1;tc<=n;tc++){
		int h,w;
		cin >> w >> h;

		int r[20][20];
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)cin >> r[i][j];

		fill(dp[0],dp[2],0);
		for(int i=0;i<h*w;i++){
			for (int j=0;j<(1<<w);j++)dp[(i+1)&1][j]=0;
			for(int S=0;S<(1<<w);S++){
				int x=i%w,y=i/w,nx=(S<<1)&((1<<w)-1);
				if((x==0 && (S>>(w-1)&1)) || (y==0 && (S&1)) || (x!=0 && y!=0 && ((S&1) || (S>>(w-1)&1)))){
					dp[(i+1)&1][nx]=max(dp[(i+1)&1][nx],dp[i&1][S]);
				}
				else {	
					dp[(i+1)&1][nx]=max(dp[(i+1)&1][nx],dp[i&1][S]);					
					dp[(i+1)&1][nx|1]=max(dp[(i+1)&1][nx|1],dp[i&1][S]+r[y][x]);
				}
			}
		}
		int ans=0;
		for(int i=0;i<(1<<w);i++)ans=max(ans,dp[(h*w)&1][i]);
		cout << "Case " << tc << ": " << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 6630
memory_kb: 9352
final_rank: 16

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int w,h,a[20][20];
int dp[2][1<<20];

void solve(){
  fill(dp[0],dp[2],0);
  
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      for(int k=0;k<(1<<w);k++){
	int newbit = (k<<1)&((1<<w)-1);

	if((j == 0 || (k & 1) == 0) && (i == 0 || (k & (1 << (w-1))) == 0)){
	  dp[1][newbit|1] = max(dp[1][newbit|1], dp[0][k] + a[i][j]);
	}

	dp[1][newbit] = max(dp[1][newbit], dp[0][k]);
      }

      for(int k=0;k<(1<<w);k++){
	dp[0][k] = dp[1][k];
	dp[1][k] = 0;
      }
    }
  }

  int ans = 0;
  for(int i=0;i<(1<<w);i++) ans = max(ans, dp[0][i]);
  cout << ans << endl;	
}

int main(){
  int T;
  cin >> T;
  for(int t=1;t<=T;t++){
    cin >> w >> h;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin >> a[i][j];

    cout << "Case " << t << ": " << flush;
    solve();
  }
}
accuracy: 1
time_ms: 5440
memory_kb: 9352
final_rank: 11

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
  int T; cin >>T;
  REP(casenum, T){
    printf("Case %d: ", casenum + 1);
    int W, H;
    cin >> W >> H;
    int r[20][20];
    if(H <= W){
      REP(y, H) REP(x, W) cin >> r[y][x];
    }else{
      swap(H, W);
      REP(x, W) REP(y, H) cin >> r[y][x];
    }
    static int dp[2][1 << 20] = {};
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 0;
    dp[0][1] = r[0][0];
    int prevN = 1;
    FOR(i, 1, W + H - 1){
      vector<int> v;
      REP(y, H) REP(x, W){
        if(x + y == i) v.push_back(r[y][x]);
      }
      //printf("v[%d]: ", i); debug(v.begin(), v.end());
      int N = v.size();
      vector<int> sum(1 << N, 0);
      for(int S = 0; S < 1 << N; S++){
        for(int i = 0; i < N; i++){
          if(S >> i & 1){
            sum[S] += v[i];
          }
        }
      }
      for(int S = 0; S < 1 << N; S++){
          if(prevN <= N){
            int mask = ((1 << prevN) - 1);
            int S0 = S & mask;
            int S1 = (S >> 1) & mask;
            int PS = ~(S0 | S1) & mask;
            //printf("-> S: %d S0: %d S1: %d PS: %d\n", S, S0, S1, PS);
            dp[i & 1][S] = max(dp[i & 1][S], dp[(i - 1) & 1][PS] + sum[S]);
          }else{
            int mask = ((1 << prevN) - 1);
            int S0 = S & mask;
            int S1 = (S << 1) & mask;
            int PS = ~(S0 | S1) & mask;
            //printf("<- S: %d S0: %d S1: %d PS: %d\n", S, S0, S1, PS);
            dp[i & 1][S] = max(dp[i & 1][S], dp[(i - 1) & 1][PS] + sum[S]);
          }
        }
      // dp[S] = max({dp[T]| T \subset S})
      REP(j, N)REP(S, 1 << N){
        dp[i & 1][S | 1 << j] = max(dp[i & 1][S | 1 << j], dp[i & 1][S]);
      }
      memset(dp[(i - 1) & 1], 0, sizeof(dp[(i - 1) & 1]));
      /*
      printf("dp[%d]: ", i);
      debug(dp[i], dp[i] + (1 << N));
      */
      prevN = N;
    }
    cout << max(dp[(W + H - 2) & 1][0], dp[(W + H - 2) & 1][1]) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 730
memory_kb: 15408
final_rank: 7

code_snippet: #include<cstdio>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int w,h,a[20][20];

int idx[1<<20]; // 2^w -> Fibonacci ID

int dp[20][4][76866]; // partial memoization (got MLE!!)
int dfs(int i,int j,int S){
	if(i==h) return 0;
	if(j==w) return dfs(i+1,0,S);

	int dummy=-1;
	int &res=(j%5==0?dp[i][j/5][idx[S]]:dummy);

	if(res!=-1) return res;

	// not choose a[i][j]
	res=dfs(i,j+1,S&~(1<<j));
	// choose a[i][j]
	if((S>>j&1)==0 && (j==0 || (S>>j-1&1)==0)){
		res=max(res,dfs(i,j+1,S|1<<j)+a[i][j]);
	}
	return res;
}

int solve(){
	scanf("%d%d",&w,&h);
	rep(i,h) rep(j,w) scanf("%d",a[i]+j);

	int n=0;
	rep(S,1<<w){
		int adj=0;
		rep(j,w-1) if((S>>j&1) && (S>>j+1&1)) adj++;
		if(adj<=1) idx[S]=n++;
	}

	rep(i,h) rep(j,4) rep(k,n) dp[i][j][k]=-1;

	return dfs(0,0,0);
}

int main(){
	int T; scanf("%d",&T);
	for(int cas=1;cas<=T;cas++) printf("Case %d: %d\n",cas,solve());
	return 0;
}
accuracy: 1
time_ms: 760
memory_kb: 27988
final_rank: 12

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int dNum;
    cin >> dNum;

    for(int d=1; d<=dNum; ++d){
        int h, w;
        cin >> w >> h;

        vector<vector<int> > r(h, vector<int>(w));
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> r[i][j];
            }
        }

        vector<int> dp(1<<w, -1);
        dp[0] = 0;
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                vector<int> next(1<<w, -1);
                for(int i=0; i<(1<<w); ++i){
                    if(dp[i] == -1)
                        continue;

                    bitset<21> bs = i;
                    bs <<= 1;

                    if(!bs[w] && (x == 0 || !bs[1])){
                        bs &= (1<<w) - 1;
                        bs[0] = true;
                        next[bs.to_ulong()] = max(next[bs.to_ulong()], dp[i] + r[y][x]);
                        bs[0] = false;
                    }

                    bs &= (1<<w) - 1;
                    next[bs.to_ulong()] = max(next[bs.to_ulong()], dp[i]);
                }
                dp.swap(next);
            }
        }

        int ret = 0;
        for(int i=0; i<(1<<w); ++i)
            ret = max(ret, dp[i]);
        cout << "Case " << d << ": " << ret << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 1660
memory_kb: 9384
final_rank: 4