fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. // Function to convert a number to its binary representation of digits
  10. string toBinaryDigits(int num) {
  11. string binary = "0000000000"; // 10 digits
  12. while (num > 0) {
  13. int digit = num % 10;
  14. binary[digit] = '1'; // Mark the digit as present
  15. num /= 10;
  16. }
  17. return binary;
  18. }
  19.  
  20. // Function to check if two binary representations have common digits
  21. bool areDigitsUnique(const string& bin1, const string& bin2) {
  22. for (int i = 0; i < 10; i++) {
  23. if (bin1[i] == '1' && bin2[i] == '1') {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29.  
  30. int main() {
  31. int n;
  32. cin >> n;
  33.  
  34. vector<int> nums(n);
  35. unordered_map<string, int> maxNumForBinary; // Map to store the maximum value for each binary digit representation
  36. int maxPairSum = -1; // Default answer when no valid pair is found
  37.  
  38. for (int i = 0; i < n; i++) {
  39. cin >> nums[i];
  40. string currentBinary = toBinaryDigits(nums[i]);
  41.  
  42. // Iterate over all previously encountered binary digit patterns
  43. for (auto& [binary, maxVal] : maxNumForBinary) {
  44. if (areDigitsUnique(currentBinary, binary)) {
  45. // If the binary representations are unique, calculate the sum
  46. maxPairSum = max(maxPairSum, nums[i] + maxVal);
  47. }
  48. }
  49.  
  50. // Update the maximum value for the current binary digit pattern
  51. maxNumForBinary[currentBinary] = max(maxNumForBinary[currentBinary], nums[i]);
  52. }
  53.  
  54. cout << maxPairSum << endl;
  55. return 0;
  56. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
0