#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <algorithm>
using namespace std;
// Function to convert a number to its binary representation of digits
string toBinaryDigits(int num) {
string binary = "0000000000"; // 10 digits
while (num > 0) {
int digit = num % 10;
binary[digit] = '1'; // Mark the digit as present
num /= 10;
}
return binary;
}
// Function to check if two binary representations have common digits
bool areDigitsUnique(const string& bin1, const string& bin2) {
for (int i = 0; i < 10; i++) {
if (bin1[i] == '1' && bin2[i] == '1') {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
unordered_map<string, int> maxNumForBinary; // Map to store the maximum value for each binary digit representation
int maxPairSum = -1; // Default answer when no valid pair is found
for (int i = 0; i < n; i++) {
cin >> nums[i];
string currentBinary = toBinaryDigits(nums[i]);
// Iterate over all previously encountered binary digit patterns
for (auto& [binary, maxVal] : maxNumForBinary) {
if (areDigitsUnique(currentBinary, binary)) {
// If the binary representations are unique, calculate the sum
maxPairSum = max(maxPairSum, nums[i] + maxVal);
}
}
// Update the maximum value for the current binary digit pattern
maxNumForBinary[currentBinary] = max(maxNumForBinary[currentBinary], nums[i]);
}
cout << maxPairSum << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byBjb252ZXJ0IGEgbnVtYmVyIHRvIGl0cyBiaW5hcnkgcmVwcmVzZW50YXRpb24gb2YgZGlnaXRzCnN0cmluZyB0b0JpbmFyeURpZ2l0cyhpbnQgbnVtKSB7CiAgICBzdHJpbmcgYmluYXJ5ID0gIjAwMDAwMDAwMDAiOyAvLyAxMCBkaWdpdHMKICAgIHdoaWxlIChudW0gPiAwKSB7CiAgICAgICAgaW50IGRpZ2l0ID0gbnVtICUgMTA7CiAgICAgICA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