This page is still under construction.

Parts of this page are still being built. What you see may change.

Programming Duel Tournament

Time limit2sMemory limit256 MB

Summary
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 NN contestants. The tournament is N−1N-1 duels, each one between two contestants. Contestant ii has skill AiA_i, 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 N−1N-1 duels a single contestant is left, and that contestant is the champion.

A duel between a contestant of skill xx and a contestant of skill yy has interest x⊕yx \oplus y, where ⊕\oplus is bitwise XOR. Contestant ii gains HiH_i fatigue for every duel they take part in, so a contestant who duels kk times ends with total fatigue Hi×kH_i \times k. Contestant ii can duel at most LiL_i 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 11, 33, 55, with HH equal to 66, 22, 44 and LL equal to 22, 22, 22. 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 A1⊕A2=1⊕3=2A_1 \oplus A_2 = 1 \oplus 3 = 2. Then contestant 2 duels contestant 3. Contestant 3 wins and contestant 2 is eliminated, and the interest of that duel is A2⊕A3=3⊕5=6A_2 \oplus A_3 = 3 \oplus 5 = 6. The total interest is 2+6=82 + 6 = 8. The three contestants dueled 1, 2 and 1 times, so the total fatigue is 6×1+2×2+4×1=146 \times 1 + 2 \times 2 + 4 \times 1 = 14, and the fun is 8−14=−68 - 14 = -6. Nobody dueled more than LiL_i times, so every condition holds, and no schedule reaches a larger fun.

Given NN, A1A_1 through ANA_N, H1H_1 through HNH_N, and L1L_1 through LNL_N, find the largest fun the tournament can reach while every condition holds.

Input

The first line has the number of contestants NN. (2≤N≤3002 \le N \le 300)

The second line has NN integers A1A_1 through ANA_N, separated by spaces. (1≤Ai≤1061 \le A_i \le 10^6) All AiA_i are distinct.

The third line has NN integers H1H_1 through HNH_N, separated by spaces. (1≤Hi≤1061 \le H_i \le 10^6)

The fourth line has NN integers L1L_1 through LNL_N, separated by spaces. (2≤Li≤N−12 \le L_i \le N-1)

Output

Print the largest fun the tournament can reach while every condition holds.

Examples3

  1. Example 1

    Input
    3
    1 3 5
    6 2 4
    2 2 2
    
    Expected output
    -6
    
  2. Example 2

    Input
    3
    2 4 8
    1 1 1
    2 2 2
    
    Expected output
    18
    
  3. Example 3

    Input
    4
    1 2 3 4
    5 5 5 5
    2 2 2 2
    
    Expected output
    -14