#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;
}

Measured memory_bytes: 2860