Programming Duel Tournament
Time limit2sMemory limit256 MB
Choose a duel schedule for N contestants, where the higher skill always wins and each contestant duels at most L_i times, to maximize total duel XOR interest minus fatigue.
- Level
Hard8 of 10
- Topics
- Greedy, Tree, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
Onjo runs a programming duel tournament with contestants. The tournament is duels, each one between two contestants. Contestant has skill , and no two contestants have the same skill. When two contestants duel, the one with the higher skill wins, and the loser is eliminated and duels no more. Each duel eliminates one contestant, so after duels a single contestant is left, and that contestant is the champion.
A duel between a contestant of skill and a contestant of skill has interest , where is bitwise XOR. Contestant gains fatigue for every duel they take part in, so a contestant who duels times ends with total fatigue . Contestant can duel at most times.
The fun of the tournament is the total interest of all duels minus the total fatigue of all contestants. The fun can be negative. Who meets whom, and in what order, changes the fun.
For example, take three contestants with skills , , , with equal to , , and equal to , , . First contestant 1 duels contestant 2. Contestant 2 has the higher skill and wins, so contestant 1 is eliminated, and the interest of that duel is . Then contestant 2 duels contestant 3. Contestant 3 wins and contestant 2 is eliminated, and the interest of that duel is . The total interest is . The three contestants dueled 1, 2 and 1 times, so the total fatigue is , and the fun is . Nobody dueled more than times, so every condition holds, and no schedule reaches a larger fun.
Given , through , through , and through , find the largest fun the tournament can reach while every condition holds.
Input
The first line has the number of contestants . ()
The second line has integers through , separated by spaces. () All are distinct.
The third line has integers through , separated by spaces. ()
The fourth line has integers through , separated by spaces. ()
Output
Print the largest fun the tournament can reach while every condition holds.