Under construction

Still being built. Things may change

AtCoder Quality Problem

Time limit2sMemory limit512 MB

Summary
Given costs to paint each of the 2^n subsets red or blue, find the minimum total cost so that the union of any two same-colored subsets has that same color.
Level

Hard9 of 10

Topics
Bit manipulation, Graph, Dynamic programming
Solved
No attempts yet

Statement

You have a set SS of nn elements. You want to paint each subset of SS either red or blue. For each subset ss of SS, the cost to paint it red is RsR_s, and the cost to paint it blue is BsB_s.

Note: you paint subsets, not the elements.

There is one requirement:

  • If aa and bb are two subsets of the same color, then the subset a∪ba \cup b has the same color as aa and bb.

Find the minimum total cost to paint all 2n2^n subsets.

Input

The first line contains a single integer nn (0≤n≤200 \le n \le 20), the number of elements.

The second line contains 2n2^n integers R0,R1,…,R2n−1R_0, R_1, \ldots, R_{2^n-1} (−109≤Ri≤109-10^9 \le R_i \le 10^9), the costs to paint subsets red.

The third line contains 2n2^n integers B0,B1,…,B2n−1B_0, B_1, \ldots, B_{2^n-1} (−109≤Bi≤109-10^9 \le B_i \le 10^9), the costs to paint subsets blue.

Subset ii (0≤i<2n0 \le i < 2^n) consists of the elements jj for which the jj-th bit in the binary representation of ii is 1.

Output

Print one integer: the minimum cost to paint all subsets.

Examples5

  1. Example 1

    Input
    2
    -5 9 9 -5
    10 -8 -6 3
    
    Expected output
    -16
    
  2. Example 2

    Input
    3
    -15 19 19 -5 30 -3 -16 13
    29 -6 -14 -7 24 -5 18 11
    
    Expected output
    -22
    
  3. Example 3

    Input
    0
    -129363358
    227605714
    
    Expected output
    -129363358
    
  4. Example 4

    Input
    1
    -120923470 -355154745
    -18478014 104068715
    
    Expected output
    -476078215
    
  5. Example 5

    Input
    3
    41 38 35 12 5 15 42 18
    37 35 39 13 10 14 11 19
    
    Expected output
    173