Problem:
微生物発電
飯沼博士は磐梯山の噴気孔でふしぎな微生物を見つけました。この微生物の雄と雌１体ずつが合体すると、電気エネルギーを放出します。この微生物を研究すれば、将来のエネルギー危機から我々を救えるかもしれません。
観察を続けると、微生物は合体したときだけ電気エネルギーを発生させることと、合体した微生物のさらなる合体はないことがわかりました。さらに観察を続けると、合体で放出される電気エネルギーは、微生物が体内に持つ未知の粒子（博士はこれを磐梯山にちなんでＢ粒子と名づけました）の量で決まることがわかりました。合体する雄と雌が体内に持つＢ粒子の量をそれぞれ
bm
と
bw
とすると、合体により放出される電気エネルギーは |
bm
-
bw
| × (|
bm
-
bw
| - 30)
2
という式で計算できます。
この発見により、微生物の集団から得られる最大の電気エネルギーが計算できるようになりました。それでは、微生物の集団に含まれる雄と雌の数と、各個体が持つＢ粒子の量が与えられたとき、この微生物の集団から得られる最大の電気エネルギーを計算するプログラムを作成してください。
入力
入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。入力データは以下の形式で与えられる。
M
W
bm
1
bm
2
...
bm
M
bw
1
bw
2
...
bw
W
１行目の
M
と
W
(1 ≤
M,W
≤ 12) はそれぞれ雄と雌の微生物の数である。２行目に、
i
番目の雄が体内に持つＢ粒子の量
bm
i
(0 ≤
bm
i
≤ 50) が与えられる。３行目に、
i
番目の雌が体内に持つＢ粒子の量
bw
i
(0 ≤
bw
i
≤ 50) が与えられる。
データセットの数は 20 を超えない。
出力
データセットごとに、微生物の集団から得られる電気エネルギーの最大値を1行に出力する。
入出力例
入力例
3 3
0 20 30
10 20 30
10 10
32 10 15 8 20 10 6 45 50 41
18 0 37 25 45 11 25 21 32 27
7 3
23 14 39 6 47 16 23
19 37 8
0 0
出力例
12000
53906
11629

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int dp[13][(1<<12)+2];
void solve(int m,int w){
  vector<int>bm(max(m,w)),bw(max(m,w));
  for(int i = 0; max(m,w) > i; i++){
    bm[i] = bw[i] = -1;
  }
  for(int i = 0; m > i; i++){
    cin>>bm[i];
  }
  for(int i = 0; w > i; i++){
    cin>>bw[i];
  }

  
  for(int i = 0; 13 > i; i++){
    for(int j = 0; (1<<12)+1 >= j; j++){
      dp[i][j] = -1;
    }
  }

  dp[0][0] = 0;
  for(int i = 1; max(m,w)+1 > i; i++){
    for(int j = 0; (1<<max(m,w)) > j; j++){
      for(int k = 0; max(m,w) > k; k++){
        if(!(j & (1<<k))){
          int ad = abs(bm[i-1]-bw[k]) * (abs(bm[i-1]-bw[k]) - 30)*(abs(bm[i-1]-bw[k]) - 30);
          if(bm[i-1] == -1 || bw[k] == -1)ad = 0;
          if(dp[i][j+(1<<k)] == -1)dp[i][j+(1<<k)] = dp[i-1][j]+ad;
          else dp[i][j+(1<<k)] = max(dp[i][j+(1<<k)],dp[i-1][j]+ad);
        }
      }
    }
  }
  cout << dp[max(m,w)][(1<<max(m,w))-1] << endl;
  return;
}

int main(){
  int m,w;
  while(cin>>m>>w,m){
    solve(m,w);
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3620
final_rank: 13

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define mod 1000000007
#define ad(a,b) a=(a+b)%mod;
#define X 5010
ll po(ll x,ll y){
	ll res=1;
	for(;y;y>>=1){
		if(y&1)res=res*x%mod;
		x=x*x%mod;
	}
	return res;
}
ll fac[X],ivf[X];
void init(){
	fac[0]=1;
	for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod;
	for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2);
}
ll C(ll n,ll k){
	return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}
bool solve(){
	ll n,m;
	cin>>n>>m;
	if(n==0&&m==0)return 0;
	ll a[12],b[12];
	rep(i,n)cin>>a[i];
	rep(j,m)cin>>b[j];
	ll dp[(1<<12)];
	rep(i,(1<<n))dp[i]=-1e17;
	dp[0]=0;
	rep(j,m){
		vector<ll> id,val;
		rep(i,n)rep(s,(1<<n)){
			if(s&(1<<i))continue;
			id.push_back(s+(1<<i));
			ll dif=abs(a[i]-b[j]);
			ll cost=dif*(dif-30)*(dif-30);
			val.push_back(dp[s]+cost);
		}
		rep(i,id.size()){
			chmax(dp[id[i]],val[i]);
		}
	}
	ll ans=-1e17;
	rep(s,(1<<n))chmax(ans,dp[s]);
	cout<<ans<<endl;
	return 1;
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	while(solve()){}
}
accuracy: 1
time_ms: 10
memory_kb: 3636
final_rank: 12

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
int M, W;
vector<int> a, b;
int dp[1<<12][1<<12];
int calc(int s, int t)
{
    int k = abs(a[s] - b[t]);
    return k * (k - 30) * (k - 30);
}
int dfs(int wbit, int mbit, int pos)
{
    if(pos >= W) return 0;
    if(dp[wbit][mbit] != -1) return dp[wbit][mbit];
    int res = dfs(wbit, mbit, pos + 1);
    if(mbit & (1<<pos))
    {
        for(int i = 0; i < W; i++)
        {
            if(not (wbit & (1<<i)) or a[i] == b[pos]) continue;
            res = max(res, dfs(wbit & ~(1<<i), mbit & ~(1<<pos), pos + 1) + calc(i, pos));
        }
    }
    return dp[wbit][mbit] = res;
}
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> M >> W, M or W)
    {
        memset(dp, -1, sizeof(dp));
        a.resize(M);
        b.resize(W);
        for(int i = 0; i < M; i++) cin >> a[i];
        for(int i = 0; i < W; i++) cin >> b[i];
        if(M > W)
        {
            swap(M, W);
            swap(a, b);
        }
        int ans = 0;
        for(int bit = 0; bit < (1<<W); bit++)
        {
            if(__builtin_popcount(bit) != M) continue;
            ans = max(ans, dfs((1<<M) - 1, bit, 0));
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 68696
final_rank: 19

code_snippet: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define lp(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define ctoi(x) (x - 'a') 
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;

}
LL exp(int a, int b){
    LL res = 1;
    LL sum = a;
    while(b){
        if(b&1)res = (res * sum) % MOD;
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int cul(int a, int b){
    int c = abs(a-b);
    return c * (c - 30) * (c - 30);
}

int M, W;
vector<int> m(M), w(W);
vector<vector<int> > dp;

int func(int used, int place){
    if(place == M)return 0;
    if(dp[place][used] >= 0)return dp[place][used];
    int res = 0;
    REP(i, W){
        if(used & BIT(i))continue;
        res = max(res, cul(m[place], w[i]) + func(used | BIT(i), place + 1));
    }
    return res;
}
int main(){
    while(cin >> M >> W && M){
        m = vector<int>(M);
        w = vector<int>(W);
        REP(i, M)cin>>m[i];
        REP(i, W)cin>>w[i];
        if(m > w){
            swap(m, w);
            swap(M, W);
        }
        dp.resize(M, vector<int>(1<<W, -1));
        cout << func(0, 0) << endl;
        dp.clear();
    }
    return 0;
}
accuracy: 0.1
time_ms: 410
memory_kb: 3188
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
using ll = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int m,w;
  while(cin>>m>>w,m||w){
    vector<Int> as(m),bs(w);
    for(Int i=0;i<m;i++) cin>>as[i];
    for(Int i=0;i<w;i++) cin>>bs[i];

    auto calc=[&](Int i,Int j)->Int{
      Int dif=abs(as[i]-bs[j]);
      return dif*(dif-30)*(dif-30);
    };

    Int s=1<<w;
    const Int INF = 1e15;    
    vector<Int> dp(s,-INF);
    dp[0]=0;
    for(Int i=0;i<m;i++){
      vector<Int> nx(s,-INF);
      for(Int b=0;b<s;b++){
	chmax(nx[b],dp[b]);
	for(Int j=0;j<w;j++){
	  if((b>>j)&1) continue;
	  chmax(nx[b|(1<<j)],dp[b]+calc(i,j));		   
	}
      }
      swap(dp,nx);
    }
    cout<<*max_element(dp.begin(),dp.end())<<endl;
  }  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3204
final_rank: 10

code_snippet: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<cstring>
#include<unordered_map>
#include<set>
#include<string.h>
#define int long long
using namespace std;

int a, b;
int c[12], d[12];
int dp[13][1 << 12];//i???????????§??????j?????£??????????????§???
void saiki(int e, int f) {
	dp[e + 1][f] = dp[e][f];
	for (int i = 0; i < b; i++) {
		if (f&(1ll << i)) {
			dp[e + 1][f - (1ll << i)] = max(dp[e+1][f-(1ll<<i)], dp[e][f] + abs(c[e] - d[i])*(abs(c[e] - d[i]) - 30)*(abs(c[e] - d[i]) - 30));
		}
	}
}
signed main() {
	while (cin >> a >> b, a | b) {
		memset(dp, 0, sizeof(dp));
		for (int e = 0; e < a; e++)cin >> c[e];
		for (int e = 0; e < b; e++)cin >> d[e];
		for (int e = 0; e < a; e++) {
			for (int f = 0; f < (1ll << b); f++) {
				saiki(e, f);
			}
		}
		cout << *max_element(dp[a], dp[a] + (1 << 12)) << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3508
final_rank: 11

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

struct Info{
	Info(){
		index = 0;
		for(int i = 0; i < 12; i++)check[i] = false;
	}

	short table[6],index;
	bool check[12];
};

int table_2[2][2],table_3[6][3],table_4[24][4],table_5[120][5],table_6[720][6];

void func(int M,int W){
	int m_data[M],w_data[W];

	for(int i = 0; i < M; i++)scanf("%d",&m_data[i]);
	for(int i = 0; i < W; i++)scanf("%d",&w_data[i]);

	int maximum = 0,tmp,select_num,num,left_index,right_index,left_tmp,right_tmp,left_max,right_max,limit,loc_left[7],loc_right[7],index,val,calc[7];

	if(M == 1){
		for(int i = 0; i < W; i++){
			tmp = abs(w_data[i] - m_data[0])*(abs(w_data[i] - m_data[0])-30)*(abs(w_data[i] - m_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else if(W == 1){
		for(int i = 0; i < M; i++){
			tmp = abs(m_data[i] - w_data[0])*(abs(m_data[i] - w_data[0])-30)*(abs(m_data[i] - w_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else{
		if(M > W){
			select_num = (M+1)/2;

			limit = pow(2,M);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < M; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				//??°???????????????????????????
				for(int k = 0; k < M; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				if(left_index == 1){
					val = abs(w_data[0] - m_data[loc_left[0]]);
					left_tmp = val*(val-30)*(val-30);
					left_max = max(left_max,left_tmp);
				}else if(left_index == 2){
					calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]];
					for(index = 0; index < 2; index++){
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_2[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 3){
					calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]];
					for(index = 0; index < 6; index++){
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_3[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 4){
					calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],calc[3] = m_data[loc_left[3]];
					for(index = 0; index < 24; index++){
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_4[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 5){
					calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],calc[3] = m_data[loc_left[3]],calc[4] = m_data[loc_left[4]];
					for(index = 0;index < 120; index++){
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_5[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}else{ //left_index == 6
					calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],
													calc[3] = m_data[loc_left[3]],calc[4] = m_data[loc_left[4]],calc[5] = m_data[loc_left[5]];
					for(index = 0;index < 720; index++){
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_6[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}

				if(right_index == 1){
					val = abs(w_data[W-1] - m_data[loc_right[0]]);
					right_tmp = val*(val-30)*(val-30);
					right_max = max(right_max,right_tmp);
				}else if(right_index == 2){
					calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]];
					for(index = 0; index < 2; index++){
						right_tmp = 0;
						for(int p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_2[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 3){
					calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]];
					for(index = 0; index < 6; index++){
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_3[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 4){
					calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],calc[3] = m_data[loc_right[3]];
					for(index = 0; index < 24; index++){
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_4[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 5){
					calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],calc[3] = m_data[loc_right[3]],calc[4] = m_data[loc_right[4]];
					for(index = 0;index < 120; index++){
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_5[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}else{ //right_index == 6
					calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],
													calc[3] = m_data[loc_right[3]],calc[4] = m_data[loc_right[4]],calc[5] = m_data[loc_right[5]];
					for(index = 0;index < 720; index++){
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_6[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}

				maximum = max(maximum,left_max+right_max);
			}

		}else{ //W >= M
			select_num = (W+1)/2;

			limit = pow(2,W);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < W; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				for(int k = 0; k < W; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				if(left_index == 1){
					val = abs(m_data[0] - w_data[loc_left[0]]);
					left_tmp = val*(val-30)*(val-30);
					left_max = max(left_max,left_tmp);
				}else if(left_index == 2){
					calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]];
					for(index = 0; index < 2; index++){
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_2[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 3){
					calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]];
					for(index = 0; index < 6; index++){
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_3[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 4){
					calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],calc[3] = w_data[loc_left[3]];
					for(index = 0; index < 24; index++){
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_4[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 5){
					calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],calc[3] = w_data[loc_left[3]],calc[4] = w_data[loc_left[4]];
					for(index = 0;index < 120; index++){
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_5[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}else{ //left_index == 6
					calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],
													calc[3] = w_data[loc_left[3]],calc[4] = w_data[loc_left[4]],calc[5] = w_data[loc_left[5]];
					for(index = 0;index < 720; index++){
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_6[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}

				if(right_index == 1){
					val = abs(m_data[M-1] - w_data[loc_right[0]]);
					right_tmp = val*(val-30)*(val-30);
					right_max = max(right_max,right_tmp);
				}else if(right_index == 2){
					calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]];
					for(index = 0; index < 2; index++){
						right_tmp = 0;
						for(int p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_2[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 3){
					calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]];
					for(index = 0; index < 6; index++){
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_3[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 4){
					calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],calc[3] = w_data[loc_right[3]];
					for(index = 0; index < 24; index++){
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_4[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 5){
					calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],calc[3] = w_data[loc_right[3]],calc[4] = w_data[loc_right[4]];
					for(index = 0;index < 120; index++){
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_5[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}else{ //right_index == 6
					calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],
													calc[3] = w_data[loc_right[3]],calc[4] = w_data[loc_right[4]],calc[5] = w_data[loc_right[5]];
					for(index = 0;index < 720; index++){
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_6[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}

				maximum = max(maximum,left_max+right_max);
			}
		}
	}
	printf("%d\n",maximum);
}


int main(){

	int M,W,index_2 = 0,index_3 = 0,index_4 = 0, index_5 = 0,index_6 = 0;

	for(int a = 0; a < 2; a++){
		for(int b = 0; b < 2; b++){
			if(a != b){
				table_2[index_2][0] = a,table_2[index_2][1] = b;
				index_2++;
			}
		}
	}

	for(int a = 0; a < 3; a++){
		for(int b = 0; b < 3; b++){
			for(int c = 0; c < 3; c++){
				if(a != b && a != c && b != c){
					table_3[index_3][0] = a,table_3[index_3][1] = b,table_3[index_3][2] = c;
					index_3++;
				}
			}
		}
	}

	for(int a = 0; a < 4; a++){
		for(int b = 0; b < 4; b++){
			for(int c = 0; c < 4; c++){
				for(int d = 0; d < 4; d++){
					if(a != b && a != c && a != d && b != c && b != d && c != d){
						table_4[index_4][0] = a,table_4[index_4][1] = b,table_4[index_4][2] = c,table_4[index_4][3] = d;
						index_4++;
					}
				}
			}
		}
	}

	for(int a = 0; a < 5; a++){
		for(int b = 0; b < 5; b++){
			for(int c = 0; c < 5; c++){
				for(int d = 0; d < 5; d++){
					for(int e = 0; e < 5; e++){
						if(a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e){
							table_5[index_5][0] = a,table_5[index_5][1] = b,table_5[index_5][2] = c,table_5[index_5][3] = d,table_5[index_5][4] = e;
							index_5++;
						}
					}
				}
			}
		}
	}

	for(int a = 0; a < 6; a ++){
		for(int b = 0; b < 6; b++){
			for(int c = 0; c < 6; c++){
				for(int d = 0; d < 6; d++){
					for(int e = 0; e < 6; e++){
						for(int f = 0; f < 6; f++){
							if(a != b && a != c && a != d && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
								table_6[index_6][0] = a,table_6[index_6][1] = b,table_6[index_6][2] = c,table_6[index_6][3] = d,table_6[index_6][4] = e,table_6[index_6][5] = f;
								index_6++;
							}
						}
					}
				}
			}
		}
	}

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

		func(M,W);
	}

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 2940
final_rank: 16

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

struct Info{
	Info(){
		index = 0;
		for(int i = 0; i < 12; i++)check[i] = false;
	}

	short table[6],index;
	bool check[12];
};

int table_2[2][2],table_3[6][3],table_4[24][4],table_5[120][5],table_6[720][6];

void func(int M,int W){
	int m_data[M],w_data[W];

	for(int i = 0; i < M; i++)scanf("%d",&m_data[i]);
	for(int i = 0; i < W; i++)scanf("%d",&w_data[i]);

	int maximum = 0,tmp,select_num,num,left_index,right_index,left_tmp,right_tmp,left_max,right_max,limit,loc_left[7],loc_right[7],index,val,calc[7];

	if(M == 1){
		for(int i = 0; i < W; i++){
			tmp = abs(w_data[i] - m_data[0])*(abs(w_data[i] - m_data[0])-30)*(abs(w_data[i] - m_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else if(W == 1){
		for(int i = 0; i < M; i++){
			tmp = abs(m_data[i] - w_data[0])*(abs(m_data[i] - w_data[0])-30)*(abs(m_data[i] - w_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else{
		if(M > W){
			select_num = (M+1)/2;

			limit = pow(2,M);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < M; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				//??°???????????????????????????
				for(int k = 0; k < M; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				if(left_index == 1){
					val = abs(w_data[0] - m_data[loc_left[0]]);
					left_tmp = val*(val-30)*(val-30);
					left_max = max(left_max,left_tmp);
				}else if(left_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_2[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_3[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],calc[3] = m_data[loc_left[3]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_4[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],calc[3] = m_data[loc_left[3]],calc[4] = m_data[loc_left[4]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_5[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}else{ //left_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],
								calc[3] = m_data[loc_left[3]],calc[4] = m_data[loc_left[4]],calc[5] = m_data[loc_left[5]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_6[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}

				if(right_index == 1){
					val = abs(w_data[W-1] - m_data[loc_right[0]]);
					right_tmp = val*(val-30)*(val-30);
					right_max = max(right_max,right_tmp);
				}else if(right_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]];
						right_tmp = 0;
						for(int p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_2[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_3[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],calc[3] = m_data[loc_right[3]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_4[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],calc[3] = m_data[loc_right[3]],calc[4] = m_data[loc_right[4]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_5[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}else{ //right_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],
								calc[3] = m_data[loc_right[3]],calc[4] = m_data[loc_right[4]],calc[5] = m_data[loc_right[5]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_6[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}

				maximum = max(maximum,left_max+right_max);
			}

		}else{ //W >= M
			select_num = (W+1)/2;

			limit = pow(2,W);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < W; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				for(int k = 0; k < W; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				if(left_index == 1){
					val = abs(m_data[0] - w_data[loc_left[0]]);
					left_tmp = val*(val-30)*(val-30);
					left_max = max(left_max,left_tmp);
				}else if(left_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_2[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_3[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],calc[3] = w_data[loc_left[3]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_4[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],calc[3] = w_data[loc_left[3]],calc[4] = w_data[loc_left[4]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_5[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}else{ //left_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],
								calc[3] = w_data[loc_left[3]],calc[4] = w_data[loc_left[4]],calc[5] = w_data[loc_left[5]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_6[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}

				if(right_index == 1){
					val = abs(m_data[M-1] - w_data[loc_right[0]]);
					right_tmp = val*(val-30)*(val-30);
					right_max = max(right_max,right_tmp);
				}else if(right_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]];
						right_tmp = 0;
						for(int p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_2[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_3[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],calc[3] = w_data[loc_right[3]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_4[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],calc[3] = w_data[loc_right[3]],calc[4] = w_data[loc_right[4]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_5[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}else{ //right_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],
								calc[3] = w_data[loc_right[3]],calc[4] = w_data[loc_right[4]],calc[5] = w_data[loc_right[5]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_6[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}

				maximum = max(maximum,left_max+right_max);
			}
		}
	}
	printf("%d\n",maximum);
}


int main(){

	int M,W,index_2 = 0,index_3 = 0,index_4 = 0, index_5 = 0,index_6 = 0;

	for(int a = 0; a < 2; a++){
		for(int b = 0; b < 2; b++){
			if(a != b){
				table_2[index_2][0] = a,table_2[index_2][1] = b;
				index_2++;
			}
		}
	}

	for(int a = 0; a < 3; a++){
		for(int b = 0; b < 3; b++){
			for(int c = 0; c < 3; c++){
				if(a != b && a != c && b != c){
					table_3[index_3][0] = a,table_3[index_3][1] = b,table_3[index_3][2] = c;
					index_3++;
				}
			}
		}
	}

	for(int a = 0; a < 4; a++){
		for(int b = 0; b < 4; b++){
			for(int c = 0; c < 4; c++){
				for(int d = 0; d < 4; d++){
					if(a != b && a != c && a != d && b != c && b != d && c != d){
						table_4[index_4][0] = a,table_4[index_4][1] = b,table_4[index_4][2] = c,table_4[index_4][3] = d;
						index_4++;
					}
				}
			}
		}
	}

	for(int a = 0; a < 5; a++){
		for(int b = 0; b < 5; b++){
			for(int c = 0; c < 5; c++){
				for(int d = 0; d < 5; d++){
					for(int e = 0; e < 5; e++){
						if(a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e){
							table_5[index_5][0] = a,table_5[index_5][1] = b,table_5[index_5][2] = c,table_5[index_5][3] = d,table_5[index_5][4] = e;
							index_5++;
						}
					}
				}
			}
		}
	}

	for(int a = 0; a < 6; a ++){
		for(int b = 0; b < 6; b++){
			for(int c = 0; c < 6; c++){
				for(int d = 0; d < 6; d++){
					for(int e = 0; e < 6; e++){
						for(int f = 0; f < 6; f++){
							if(a != b && a != c && a != d && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
								table_6[index_6][0] = a,table_6[index_6][1] = b,table_6[index_6][2] = c,table_6[index_6][3] = d,table_6[index_6][4] = e,table_6[index_6][5] = f;
								index_6++;
							}
						}
					}
				}
			}
		}
	}

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

		func(M,W);
	}

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 2860
final_rank: 15

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

struct Info{
	Info(){
		index = 0;
		for(int i = 0; i < 12; i++)check[i] = false;
	}

	short table[6],index;
	bool check[12];
};

int table_2[2][2],table_3[6][3],table_4[24][4],table_5[120][5],table_6[720][6];

void func(int M,int W){
	int m_data[M],w_data[W];

	for(int i = 0; i < M; i++)scanf("%d",&m_data[i]);
	for(int i = 0; i < W; i++)scanf("%d",&w_data[i]);

	int maximum = 0,tmp,select_num,num,left_index,right_index,left_tmp,right_tmp,left_max,right_max,limit,loc_left[7],loc_right[7],index,val,calc[7];

	if(M == 1){
		for(int i = 0; i < W; i++){
			tmp = abs(w_data[i] - m_data[0])*(abs(w_data[i] - m_data[0])-30)*(abs(w_data[i] - m_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else if(W == 1){
		for(int i = 0; i < M; i++){
			tmp = abs(m_data[i] - w_data[0])*(abs(m_data[i] - w_data[0])-30)*(abs(m_data[i] - w_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else{
		if(M > W){
			select_num = (M+1)/2;

			limit = pow(2,M);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < M; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				//??°???????????????????????????
				for(int k = 0; k < M; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				if(left_index == 1){
					val = abs(w_data[0] - m_data[loc_left[0]]);
					left_tmp = val*(val-30)*(val-30);
					left_max = max(left_max,left_tmp);
				}else if(left_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_2[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_3[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],calc[3] = m_data[loc_left[3]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_4[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],calc[3] = m_data[loc_left[3]],calc[4] = m_data[loc_left[4]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_5[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}else{ //left_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = m_data[loc_left[0]],calc[1] = m_data[loc_left[1]],calc[2] = m_data[loc_left[2]],
								calc[3] = m_data[loc_left[3]],calc[4] = m_data[loc_left[4]],calc[5] = m_data[loc_left[5]];
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - calc[table_6[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}

				if(right_index == 1){
					val = abs(w_data[W-1] - m_data[loc_right[0]]);
					right_tmp = val*(val-30)*(val-30);
					right_max = max(right_max,right_tmp);
				}else if(right_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]];
						right_tmp = 0;
						for(int p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_2[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_3[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],calc[3] = m_data[loc_right[3]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_4[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],calc[3] = m_data[loc_right[3]],calc[4] = m_data[loc_right[4]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_5[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}else{ //right_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = m_data[loc_right[0]],calc[1] = m_data[loc_right[1]],calc[2] = m_data[loc_right[2]],
								calc[3] = m_data[loc_right[3]],calc[4] = m_data[loc_right[4]],calc[5] = m_data[loc_right[5]];
						right_tmp = 0;
						for(int  p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - calc[table_6[index][p-((W+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}

				maximum = max(maximum,left_max+right_max);
			}

		}else{ //W >= M
			select_num = (W+1)/2;

			limit = pow(2,W);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < W; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				for(int k = 0; k < W; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				if(left_index == 1){
					val = abs(m_data[0] - w_data[loc_left[0]]);
					left_tmp = val*(val-30)*(val-30);
					left_max = max(left_max,left_tmp);
				}else if(left_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_2[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_3[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],calc[3] = w_data[loc_left[3]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_4[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}

				}else if(left_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],calc[3] = w_data[loc_left[3]],calc[4] = w_data[loc_left[4]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_5[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}else{ //left_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = w_data[loc_left[0]],calc[1] = w_data[loc_left[1]],calc[2] = w_data[loc_left[2]],
								calc[3] = w_data[loc_left[3]],calc[4] = w_data[loc_left[4]],calc[5] = w_data[loc_left[5]];
						left_tmp = 0;
						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - calc[table_6[index][p]]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
					}
				}

				if(right_index == 1){
					val = abs(m_data[M-1] - w_data[loc_right[0]]);
					right_tmp = val*(val-30)*(val-30);
					right_max = max(right_max,right_tmp);
				}else if(right_index == 2){
					for(index = 0; index < 2; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]];
						right_tmp = 0;
						for(int p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_2[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 3){
					for(index = 0; index < 6; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_3[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 4){
					for(index = 0; index < 24; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],calc[3] = w_data[loc_right[3]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_4[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}

				}else if(right_index == 5){
					for(index = 0;index < 120; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],calc[3] = w_data[loc_right[3]],calc[4] = w_data[loc_right[4]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_5[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}else{ //right_index == 6
					for(index = 0;index < 720; index++){
						calc[0] = w_data[loc_right[0]],calc[1] = w_data[loc_right[1]],calc[2] = w_data[loc_right[2]],
								calc[3] = w_data[loc_right[3]],calc[4] = w_data[loc_right[4]],calc[5] = w_data[loc_right[5]];
						right_tmp = 0;
						for(int  p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - calc[table_6[index][p-((M+1)/2)]]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
					}
				}

				maximum = max(maximum,left_max+right_max);
			}
		}
	}
	printf("%d\n",maximum);
}


int main(){

	int M,W,index_2 = 0,index_3 = 0,index_4 = 0, index_5 = 0,index_6 = 0;

	for(int a = 0; a < 6; a ++){
		for(int b = 0; b < 6; b++){
			if(a < 2 && b < 2){
				if(a != b){
					table_2[index_2][0] = a,table_2[index_2][1] = b;
					index_2++;
				}
			}
			for(int c = 0; c < 6; c++){
				if(a < 3 && b < 3 && c < 3){
					if(a != b && a != c && b != c){
						table_3[index_3][0] = a,table_3[index_3][1] = b,table_3[index_3][2] = c;
						index_3++;
					}
				}
				for(int d = 0; d < 6; d++){
					if(a < 4 && b < 4 && c < 4 && d < 4){
						if(a != b && a != c && a != d && b != c && b != d && c != d){
							table_4[index_4][0] = a,table_4[index_4][1] = b,table_4[index_4][2] = c,table_4[index_4][3] = d;
							index_4++;
						}
					}
					for(int e = 0; e < 6; e++){
						if(a < 5 && b < 5 && c < 5 && d < 5 && e < 5){
							if(a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e){
								table_5[index_5][0] = a,table_5[index_5][1] = b,table_5[index_5][2] = c,table_5[index_5][3] = d,table_5[index_5][4] = e;
								index_5++;
							}
						}
						for(int f = 0; f < 6; f++){
							if(a != b && a != c && a != d && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
								table_6[index_6][0] = a,table_6[index_6][1] = b,table_6[index_6][2] = c,table_6[index_6][3] = d,table_6[index_6][4] = e,table_6[index_6][5] = f;
								index_6++;
							}
						}
					}
				}
			}
		}
	}

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

		func(M,W);
	}

	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 2864
final_rank: 17

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

struct Info{
	Info(){
		index = 0;
		for(int i = 0; i < 12; i++)check[i] = false;
	}

	short table[6],index;
	bool check[12];
};

void func(int M,int W){
	int m_data[M],w_data[W];

	for(int i = 0; i < M; i++)scanf("%d",&m_data[i]);
	for(int i = 0; i < W; i++)scanf("%d",&w_data[i]);

	int maximum = 0,tmp,select_num,num,left_index,right_index,left_tmp,right_tmp,left_max,right_max,limit,loc_left[7],loc_right[7],val;

	if(M == 1){
		for(int i = 0; i < W; i++){
			tmp = abs(w_data[i] - m_data[0])*(abs(w_data[i] - m_data[0])-30)*(abs(w_data[i] - m_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else if(W == 1){
		for(int i = 0; i < M; i++){
			tmp = abs(m_data[i] - w_data[0])*(abs(m_data[i] - w_data[0])-30)*(abs(m_data[i] - w_data[0])-30);
			maximum = max(maximum,tmp);
		}
	}else{
		if(M > W){
			select_num = (M+1)/2;

			limit = pow(2,M);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < M; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				//??°???????????????????????????
				for(int k = 0; k < M; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				queue<Info> Q;
				Info current,left_first,right_first;

				for(int k = 0; k < left_index;k++){
					left_first.check[loc_left[k]] = true;
				}

				Q.push(left_first);

				while(!Q.empty()){
					current = Q.front();
					Q.pop();

					if(current.index == left_index){
						left_tmp = 0;
						for(int p = 0; p < (W+1)/2;p++){
							val = abs(w_data[p] - current.table[p]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
						continue;
					}

					for(int p = 0; p < left_index; p++){
						if(current.check[loc_left[p]]){
							Info new_info;
							for(int k = 0; k < 12; k++)new_info.check[k] = current.check[k];
							for(int k = 0; k < current.index;k++)new_info.table[k] = current.table[k];
							new_info.table[current.index] = m_data[loc_left[p]];
							new_info.check[loc_left[p]] = false;
							new_info.index = current.index+1;
							Q.push(new_info);
						}
					}
				}

				for(int k = 0; k < right_index;k++){
					right_first.check[loc_right[k]] = true;
				}

				Q.push(right_first);

				while(!Q.empty()){
					current = Q.front();
					Q.pop();

					if(current.index == right_index){
						right_tmp = 0;
						for(int p = (W+1)/2; p < W;p++){
							val = abs(w_data[p] - current.table[p-((W+1)/2)]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
						continue;
					}

					for(int p = 0; p < right_index; p++){
						if(current.check[loc_right[p]]){
							Info new_info;
							for(int k = 0; k < 12; k++)new_info.check[k] = current.check[k];
							for(int k = 0; k < current.index;k++)new_info.table[k] = current.table[k];
							new_info.table[current.index] = m_data[loc_right[p]];
							new_info.check[loc_right[p]] = false;
							new_info.index = current.index+1;
							Q.push(new_info);
						}
					}
				}

				maximum = max(maximum,left_max+right_max);
			}

		}else{ //W >= M
			select_num = (W+1)/2;

			limit = pow(2,W);
			for(int i = 0; i < limit;i++){
				num = 0;
				for(int k = 0; k < W; k++){
					if(i & (1 << k))num++;
				}
				if(num != select_num)continue;

				left_index = right_index = 0;

				for(int k = 0; k < W; k++){
					if(i & (1 << k)){
						loc_left[left_index++] = k;
					}else{
						loc_right[right_index++] = k;
					}
				}

				left_max = right_max = 0;

				queue<Info> Q;
				Info current,left_first,right_first;

				for(int k = 0; k < left_index;k++){
					left_first.check[loc_left[k]] = true;
				}

				Q.push(left_first);

				while(!Q.empty()){
					current = Q.front();
					Q.pop();

					if(current.index == left_index){
						left_tmp = 0;

						for(int p = 0; p < (M+1)/2;p++){
							val = abs(m_data[p] - current.table[p]);
							left_tmp += val*(val-30)*(val-30);
						}
						left_max = max(left_max,left_tmp);
						continue;
					}

					for(int p = 0; p < left_index; p++){
						if(current.check[loc_left[p]] == true){
							Info new_info;
							for(int k = 0; k < W; k++)new_info.check[k] = current.check[k];
							for(int k = 0; k < current.index;k++)new_info.table[k] = current.table[k];
							new_info.table[current.index] = w_data[loc_left[p]];
							new_info.check[loc_left[p]] = false;
							new_info.index = current.index+1;
							Q.push(new_info);
						}
					}
				}

				for(int k = 0; k < right_index;k++){
					right_first.check[loc_right[k]] = true;
				}

				Q.push(right_first);

				while(!Q.empty()){
					current = Q.front();
					Q.pop();

					if(current.index == right_index){
						right_tmp = 0;
						for(int p = (M+1)/2; p < M;p++){
							val = abs(m_data[p] - current.table[p-((M+1)/2)]);
							right_tmp += val*(val-30)*(val-30);
						}
						right_max = max(right_max,right_tmp);
						continue;
					}

					for(int p = 0; p < right_index; p++){
						if(current.check[loc_right[p]]){
							Info new_info;
							for(int k = 0; k < W; k++)new_info.check[k] = current.check[k];
							for(int k = 0; k < current.index;k++)new_info.table[k] = current.table[k];
							new_info.table[current.index] = w_data[loc_right[p]];
							new_info.check[loc_right[p]] = false;
							new_info.index = current.index+1;
							Q.push(new_info);
						}
					}
				}

				maximum = max(maximum,left_max+right_max);
			}
		}
	}
	printf("%d\n",maximum);
}


int main(){

	int M,W;

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

		func(M,W);
	}

	return 0;
}
accuracy: 1
time_ms: 950
memory_kb: 2916
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
int M,W;
int bm[15];
int bw[15];
int dp[15][1<<15];
int en(int a,int b){
	int x=abs(a-b);
	return x*(x-30)*(x-30);
}
int main()
{
	while(true){
		scanf("%d%d",&M,&W);
		if(M==0&&W==0)break;
		for(int i=0;i<M;i++)scanf("%d",&bm[i]);
		for(int i=0;i<W;i++)scanf("%d",&bw[i]);
		for(int i=0;i<15;i++){
			for(int j=0;j<(1<<15);j++){
				dp[i][j]=0;
			}
		}
		for(int j=0;j<W;j++){
			dp[0][1<<j]=en(bm[0],bw[j]);
		}
		for(int i=1;i<M;i++){
			for(int j=0;j<(1<<W);j++){
				dp[i][j]=max(dp[i][j],dp[i-1][j]);
				for(int k=0;k<W;k++){
					if((j>>k)&1){
						dp[i][j]=max(dp[i][j],dp[i-1][j-(1<<k)]+en(bm[i],bw[k]));
					}
				}
				//printf("%d %d:%d\n",i,j,dp[i][j]);
			}
		}
		int ans=0;
		for(int i=0;i<(1<<W);i++){
			ans=max(ans,dp[M-1][i]);
		}
		printf("%d\n",ans);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 5128
final_rank: 14

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

int main() {
  while(1){
    int m,w,bm[12],bw[12];
    cin >> m >> w;
    if(m==0 && w==0) break;
    for(int i=0;i<m;i++) cin >> bm[i];
    for(int i=0;i<w;i++) cin >> bw[i];
    int dp[(1<<12)]={},ans=0;
    for(int i=0;i<m;i++)
      for(int S=(1<<w)-1;S>=0;S--)
	for(int k=0;k<w;k++){
	  if((S&(1<<k))!=0)continue;
	  int t = abs(bm[i]-bw[k])*(abs(bm[i]-bw[k])-30)*(abs(bm[i]-bw[k])-30);
	  dp[S+(1<<k)] = max(dp[S+(1<<k)],dp[S]+t); 
	  ans = max(ans,dp[S+(1<<k)]);
	}
    cout << ans <<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1184
final_rank: 1

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

using namespace std;
typedef long long ll;


#define SIZE 26
#define INF 1000000000
#define LLINF 1000000000000000000LL

int calc(int m,int w){
  int ans = 0;
  int bm[12],bw[12];
  int dp[13][1<<12] = {0};
  
  for(int i=0;i<m;i++){
    scanf("%d",bm+i);
  }
  
  for(int i=0;i<w;i++){
    scanf("%d",bw+i);
  }

  for(int i=0;i<=w;i++){
    for(int j=0;j<(1<<m);j++){
      dp[i][j] = -INF;
    }
  }

  dp[0][0] = 0;
  
  for(int i=0;i<w;i++){
    for(int j=0;j<(1<<m);j++){
      for(int k=0;k<m;k++){
	dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
	if(j&(1<<k)) continue;
	int d = abs(bw[i]-bm[k]);
	int add = d * (d-30) * (d-30);
	dp[i+1][j+(1<<k)] = max(dp[i+1][j+(1<<k)],dp[i][j] + add);
	ans = max(ans,dp[i+1][j+(1<<k)]);
      }
    }
  }

  return ans;
}

int main(){
  int m,w;
    
  while(1){
    scanf("%d%d",&m,&w);
    if(m==0) break;
    
    printf("%d\n",calc(m,w));
  }
  
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1388
final_rank: 9

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

int main(){
  int m,w,bm[12],bw[12];
  while(1){ 
    cin>>m>>w;
    if(m==0 && w==0)break;
    int dp[13][1<<12]={};
    for(int i=0;i<m;i++)cin>>bm[i];
    for(int i=0;i<w;i++)cin>>bw[i];

    int ans=0;    
    for(int i=0;i<m;i++)
      for(int S=0;S<(1<<w);S++){ 
	//if(i < __builtin_popcount(S))continue;
	for(int l=0;l<w;l++){
	  if((S&(1<<l))!=0)continue;
	
	  int t=abs(bm[i]-bw[l])*(abs(bm[i]-bw[l])-30)*(abs(bm[i]-bw[l])-30);
	  dp[i+1][S+(1<<l)]=max(dp[i+1][S+(1<<l)],dp[i][S]+t);
	  dp[i+1][S] = max(dp[i+1][S],dp[i][S]);  
	  ans=max(ans,dp[i+1][S+(1<<l)]);
	}  
      }
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1376
final_rank: 2

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

int dp[1<<12][13];

int main(){
	int M,W;
	while(scanf("%d%d",&M,&W),M!=0){
		int bm[12],bw[12];
		for(int i=0;i<M;i++) scanf("%d",&bm[i]);
		for(int i=0;i<W;i++) scanf("%d",&bw[i]);
		for(int i=0;i<(1<<12);i++) for(int j=0;j<13;j++) dp[i][j]=-1;
		dp[0][0]=0;
		for(int j=0;j<W;j++) for(int i=0;i<(1<<M);i++){
			if(dp[i][j]<0) continue;
			dp[i][j+1]=max(dp[i][j],dp[i][j+1]);
			for(int k=0;k<M;k++){
				int x=1<<k;
				if(i&x) continue;
				int y=abs(bm[k]-bw[j]);
				dp[i+x][j+1]=max(dp[i][j]+y*(y-30)*(y-30),dp[i+x][j+1]);
			}
		}
		int res=0;
		for(int i=0;i<(1<<M);i++) res=max(res,dp[i][W]);
		printf("%d\n",res);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1396
final_rank: 4

code_snippet: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
using namespace std;

typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef long long ll;
typedef long double ld;

#define rep(i,x) for(int i = 0 ; i < x ; i ++)
#define rep1(i,x) for(int i = 1 ; i <= x ; i ++)
#define rrep(i,x) for(int i = x-1 ; i >= 0 ; i --)
#define rrep1(i,x) for(int i = x ; i > 0 ; i --)
#define fr first
#define sc second
#define pb push_back

const int INF = 1000000000;

int cal(int bm,int bw){
	int d = abs(bm-bw);
	return d*(d-30)*(d-30);
}

int main(){
	while(1){
		int m,w;
		int bm[15],bw[15];
		scanf("%d%d",&m,&w);
		if(m == 0 && w == 0)break;
		rep(i,m)scanf("%d",&bm[i]);
		rep(i,w)scanf("%d",&bw[i]);
		static int dp[15][1 << 12] = {};
		rep(i,15)rep(j,(1<<12)){
			dp[i][j] = 0;
		}
		int ret = 0;
		rep(i,m){
			rep(j,(1<<w)){
				dp[i+1][j] = max ( dp[i+1][j] , dp[i][j] );
				rep(k,w){
					if((j>>k)&1)continue;
					dp[i+1][j|(1<<k)] = max ( dp[i+1][j|(1<<k)] , dp[i][j] + cal(bm[i],bw[k]) );
				}
				ret = max ( ret , dp[i][j] );
			}
		}
		rep(j,(1<<w))ret = max ( ret , dp[m][j] );
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1428
final_rank: 6

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

int n, m;
int a[12], b[12];
int dp[13][1 << 12];

int calc(int i, int j)
{
	int d = abs(a[i] - b[j]);
	return d * (d - 30) * (d - 30);
}

int dfs(int i, int bit)
{
	if (i == n) return 0;
	if (~dp[i][bit]) return dp[i][bit];
	int res = dfs(i + 1, bit);
	for (int j = 0; j < m; j++){
		if (bit & (1 << j)) continue;
		res = max(res, calc(i, j) + dfs(i + 1, bit | (1 << j)));
	}
	return dp[i][bit] = res;
}

int main()
{
	while (cin >> n >> m, n){
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}
		for (int i = 0; i < m; i++){
			cin >> b[i];
		}
		memset(dp, -1, sizeof dp);
		cout << dfs(0, 0) << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1376
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
#include<math.h>
#include<cstdio>
using namespace std;
int M, W;
int BM[12], BW[12];
int DP[13][1 << 12];
int main()
{
	while (true){
		scanf("%d%d", &M, &W);
		if (M == 0 && W == 0)break;
		for (int i = 0; i < M; i++){
			scanf("%d", &BM[i]);
		}
		for (int i = 0; i < W; i++){
			scanf("%d", &BW[i]);
		}
		for (int i = 0; i <= 12; i++)for (int j = 0; j < (1 << 12); j++)DP[i][j] = 0;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j < (1 << W); j++){
				DP[i][j] = DP[i - 1][j];
				for (int k = 0; k < W; k++){
					if ((j >> k) & 1){
						DP[i][j] = max(DP[i][j], DP[i - 1][j - (1 << k)] + abs(BM[i - 1] - BW[k])*(abs(BM[i - 1] - BW[k]) - 30)*(abs(BM[i - 1] - BW[k]) - 30));
					}
					else DP[i][j] = DP[i][j];
				}
			}
		}
		int ans = 0;
		for (int j = 0; j < (1 << W); j++)ans = max(ans, DP[M][j]);
		printf("%d\n", ans);
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1400
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long lli;
 
const lli INF = 1LL<<60;
const lli MAXM = 13;
const lli MAXW = 13;
 
lli M, W;
lli bm[MAXM], bw[MAXW];
lli dp[MAXM][1<<MAXW];
 
lli f(lli bm, lli bw) {
  return llabs(bm - bw) * (llabs(bm - bw) - 30LL) * (llabs(bm - bw) - 30LL);
}
 
int main() {
  while(cin >> M >> W && (M|W)) {
    for(lli i = 0; i < M; ++i) cin >> bm[i];
    for(lli i = 0; i < W; ++i) cin >> bw[i];
    fill(dp[0], dp[MAXM], -INF);
    dp[0][0] = 0;
    for(lli i = 0; i < M; ++i) {
      for(lli j = (1<<W)-1; j >= 0; --j) {
        dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
        for(lli k = 0; k < W; ++k) {
          if(j >> k & 1) {
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-(1<<k)] + f(bm[i], bw[k]));
          }
        }
      }
    }
    lli res = 0;
    for(lli j = (1<<W)-1; j >= 0; --j) {
      res = max(res, dp[M][j]);
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1988
final_rank: 7

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

const lli INF = 1LL<<60;
const lli MAXM = 13;
const lli MAXW = 13;

lli M, W;
lli bm[MAXM], bw[MAXW];
lli dp[MAXM][1<<MAXW];

lli f(lli bm, lli bw) {
  return llabs(bm - bw) * (llabs(bm - bw) - 30LL) * (llabs(bm - bw) - 30LL);
}

int main() {
  while(cin >> M >> W && (M|W)) {
    for(lli i = 0; i < M; ++i) cin >> bm[i];
    for(lli i = 0; i < W; ++i) cin >> bw[i];
    fill(dp[0], dp[MAXM], -INF);
    dp[0][0] = 0;
    for(lli i = 0; i < M; ++i) {
      for(lli j = (1<<W)-1; j >= 0; --j) {
	dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
	for(lli k = 0; k < W; ++k) {
	  if(j >> k & 1) {
	    dp[i+1][j] = max(dp[i+1][j], dp[i][j-(1<<k)] + f(bm[i], bw[k]));
	  }
	}
      }
    }
    lli res = 0;
    for(lli j = (1<<W)-1; j >= 0; --j) {
      res = max(res, dp[M][j]);
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1988
final_rank: 7