Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: dddrrrr

Problemset: วิศวกรรมข้อมูล

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-15 20:57:34

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

int cnt1 = 0;
string binary(int x){
	string ans = "";
	if(x == 0)return "0";
	while(x > 0){
		if(x%2==1)cnt1++;
		ans += (x%2)+'0';
		x /= 2;
	}
	reverse(ans.begin() ,ans.end());
	return ans;
}

long long dec(string& str){
	long long sum = 0 ,k=0;
	for(int i=str.size()-1 ;i>=0 ;i--){
		if(str[i] == '1')sum +=(1LL << k);
		k++;
	}
	return sum;
}

long long mx = -1;
void backtrack(vector <string>& vec ,vector <string>& path ,vector <bool>& chosen ){
	if(path.size() == vec.size()){
		string str = "";
		for(auto i : path)str += i;
		
		long long val = dec(str);
		mx = max(mx ,val);
		return ;
	}
	
	for(int i=0 ;i<vec.size() ;i++){
		if(chosen[i])continue;
		
		chosen[i] = true;
		path.emplace_back(vec[i]);
		backtrack(vec ,path ,chosen);
		chosen[i] = false;
		path.pop_back(); 
	}
}


int main(){
	cin.tie(0)->sync_with_stdio(0);
	
	
	int n;
	cin >> n;
	vector <string> vec(n);
	
	int i=0;
	while(i<n){
		int x;cin >> x;
		vec[i] = binary(x);
		i++;
	}
	
	
	vector <bool> chosen(n ,false);
	vector <string> path;

	backtrack(vec ,path ,chosen);
	cout << mx;
	return 0;
	
}