Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
2
Indexable
Never
//banbong bay
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

int n, x, y;
int diem;
int arr[11], vs[11];

void check(int j){
	x = -1; y = n;
	for(int k = j-1; k >=0; k--){
		if(vs[k]==0){
			x = k; break;
		}
	}
	for(int k = j+1; k < n; k++){
		if(vs[k]==0){
			y = k; break;
		}
	
	}
}

void backtrack(int i, int score){
	if(i == n-2){
		int tmp=0;
		for(int j = 0; j < n; j++){
			if(vs[j]==0){
				tmp = arr[j]>tmp?arr[j]:tmp;
			}
		}
		//diem = diem>score+2*tmp? diem: score+2*tmp;
		diem = max(diem, score+tmp*2);
		return;
	}
	for(int j = 0; j < n; j++){
		if(vs[j]==0){
			vs[j] = 1;
			check(j);
			if(x !=-1 && y < n )
				backtrack(i+1, score+arr[x]*arr[y]);
			else if(x==-1 && y<n )
				backtrack(i+1, score+arr[y]);
			else if(x!=-1&& y==n )
				backtrack(i+1, score+arr[x]);
			else backtrack(i+1, score+arr[j]);
			vs[j] = 0;
		}	
	}
}



int main(){
	ios::sync_with_stdio(false);
	//freopen("ip.txt", "r", stdin);
	int t;
	cin >> t;
	for(int tc =1; tc <= t; tc++){
		cin >> n;
		for(int i = 0; i < n; i++){
			cin >> arr[i];
			vs[i] = 0;
		}
		diem = 0;
		backtrack(0,0);

	cout << "Case #" << tc << " " <<endl << diem << endl;
	}
	return 0;
}