Language: C++

#include <iostream>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <climits>
#include <numeric>
#include <queue>
#include <cmath>
#include <iomanip>
#include <array>
#include <string>
#include <stack>
#include <cassert>
#include <memory>
#include <random>

long long int smallest_sum(const std::vector<int>& xor_digits, const int pos, const long long int pile1, const long long int pile2) {
	if (pos == xor_digits.size()) {
		return pile1 + pile2;
	}
	if (xor_digits[pos] == 0) {
		return smallest_sum(xor_digits, pos + 1, pile1 << 1, pile2 << 1);
	}
	else {
		return smallest_sum(xor_digits, pos + 1, pile1 << 1, (pile2 << 1) + 1);
	}
}
long long int smallest_value(const std::vector<int>& xor_digits, const int pos, const long long int pile1) {
	if (pos == xor_digits.size())
		return pile1;
	return smallest_value(xor_digits, pos + 1, pile1 << 1);
}
long long int greatest_sum(const std::vector<int>& xor_digits, const int pos, const long long int pile1, const long long int pile2, const long long int lower) {
	if (pos == xor_digits.size()) {
		return pile1 + pile2;
	}
	if (xor_digits[pos] == 0 && smallest_value(xor_digits, pos + 1, (pile1 << 1) + 1) <= lower) {
		return greatest_sum(xor_digits, pos + 1, (pile1 << 1) + 1, (pile2 << 1) + 1, lower);
	}
	else {
		return greatest_sum(xor_digits, pos + 1, pile1 << 1, (pile2 << 1) + 1, lower);
	}
}
long long int smaller(const std::vector<int>& xor_digits, const int pos, const long long int pile1) {
	if (pos == xor_digits.size()) {
		return pile1;
	}
	if (xor_digits[pos] == 0) {
		return smaller(xor_digits, pos + 1, pile1 << 1);
	}
	else {
		return smaller(xor_digits, pos + 1, (pile1 << 1) + 1);
	}
}
std::pair<long long int, long long int> make(const std::vector<int>& xor_digits, const int pos, const long long int pile1, const long long int pile2, const long long int sum, const long long int lower) {
	if (pos == xor_digits.size()) return std::make_pair(pile1, pile2);
	if (xor_digits[pos] == 0) {
		const auto s = greatest_sum(xor_digits, pos + 1, (pile1 << 1), (pile2 << 1), lower);
		if (smallest_value(xor_digits, pos + 1, (pile2 << 1) + 1)  > sum || s >= sum) {
			return make(xor_digits, pos + 1, pile1 << 1, pile2 << 1, sum, lower);

		}
		else {
			return make(xor_digits, pos + 1, (pile1 << 1) + 1, (pile2 << 1) + 1, sum, lower);
		}
	}
	else {
		const auto s = greatest_sum(xor_digits, pos + 1, (pile1 << 1) + 1, pile2 << 1, lower);
		if (smallest_value(xor_digits, pos + 1, (pile1 << 1) + 1) <= lower && s >= sum) {
			return make(xor_digits, pos + 1, (pile1 << 1) + 1, pile2 << 1, sum, lower);
		}
		else {
			return make(xor_digits, pos + 1, pile1 << 1, (pile2 << 1) + 1, sum, lower);
		}
	}
}
std::vector<int> to_digits(long long int value) {
	std::vector<int> result(55);
	for (auto &r: result) {
		r = value & 1;
		value >>= 1;
	}
	std::reverse(result.begin(), result.end());
	return result;
}
int main() {
	int n; std::cin >> n;
	std::vector<long long int> piles(n);
	for (auto& a : piles) std::cin >> a;
	const auto grandy = std::accumulate(piles.begin(), piles.end(), 0LL, [](const long long int acc, const long long int v) {return acc ^ v; });
	const auto xor = grandy ^ piles[0] ^ piles[1];
	const auto sum = piles[0] + piles[1];
	const auto digits = to_digits(xor);
	const auto result = make(digits, 0, 0, 0, sum, piles[0]);
	if (result.first + result.second != sum || result.first > piles[0] || result.first == 0) {
		std::cout << "-1\n";
	}
	else {
		std::cout << piles[0] - result.first << '\n';
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:98:20: error: expected unqualified-id before 'xor' token
   98 |         const auto xor = grandy ^ piles[0] ^ piles[1];
      |                    ^~~
a.cc:100:39: error: expected primary-expression before 'xor' token
  100 |         const auto digits = to_digits(xor);
      |                                       ^~~
a.cc:100:42: error: expected primary-expression before ')' token
  100 |         const auto digits = to_digits(xor);
      |                                          ^