People in love always feel humble. Sometimes, Rikka is worried about whether she deserves love from Yuta.
Stable marriage problem is an interesting theoretical model which has a strong connection with the real world. Given n men and n women, where each person has ranked all members of the opposite sex in order of preference. We use a permutation p of length n to represent a match that the ith man gets married with the pith woman. A match is stable if and only if there are no two people of opposite sexes who would both rather have each other than their current partners, i.e., ∀i≠j,¬(ri(pj,pi)∧rpj(i,j)) where ra(b,c) represents whether person a loves b more than c.
Rikka wants to resolve the confusion in her mind by considering a special case of the stable marriage problem. Rikka uses a feature integer to represents a person's character, and for two persons with feature integers equal to x and y, Rikka defines the suitable index of them as x⊕y, where ⊕ represents binary exclusive-or.
Given n men with feature integers a1,…,an and n women with feature integers b1,…,bn. For the ith man, he loves the jth woman more than the kth woman if and only if ai⊕bj>ai⊕bk; for the ith woman, she loves the jth man more than the kth man if and only if bi⊕aj>bi⊕ak.
Rikka wants to calculate the best stable match for this problem, i.e., let P be the set of all stable match, she wants to calculate maxp∈P(∑ni=1(ai⊕bpi)). Since n is quite large, this problem is too difficult for Rikka, could you please help her find the answer?