This page is still under construction.

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

Maximum Flow

Time limit2sMemory limit512 MB

Summary
Given two paths of n vertices each plus 2n+1 cross edges with huge capacities, find the max flow from (0,0) to (1,n).
Level

Hard9 of 10

Topics
Graph, Shortest path, Greedy, Dynamic programming
Solved
No attempts yet

Problem

Bobo has an undirected graph with (2n+2)(2n + 2) vertices labeled with the following pairs of integers: (0,0),(0,1),…,(0,n),(1,0),(1,1),…,(1,n)(0, 0), (0, 1), \dots, (0, n), (1, 0), (1, 1), \dots, (1, n). The graph has three classes of edges.

  • The edges of the first class connect vertices (0,i−1)(0, i - 1) and (0,i)(0, i) with capacity aia_i for i∈{1,2,…,n}i \in \{1, 2, \dots, n\}.
  • The edges of the second class connect vertices (1,i−1)(1, i - 1) and (1,i)(1, i) with capacity bib_i for i∈{1,2,…,n}i \in \{1, 2, \dots, n\}.
  • The edges of the third class connect vertices (0,⌊i−12⌋)(0, \lfloor\frac{i - 1}{2} \rfloor) and (1,⌊i2⌋)(1, \lfloor \frac{i}{2}\rfloor) with capacity cic_i for i∈{1,2,…,2n+1}i \in \{1, 2, \dots, 2n + 1\}.

Bobo would like to find the maximum flow from vertex (0,0)(0, 0) to vertex (1,n)(1, n).

Input

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains an integer nn (1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n.

The third line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n.

The fourth line contains (2n+1)(2n + 1) integers c1,c2,…,c2n+1c_1, c_2, \dots, c_{2n + 1}.

The constraints are: 1≤ai,bi,ci≤1091 \leq a_i, b_i, c_i \leq 10^9.

It is guaranteed that the number of test cases does not exceed 10510^5, and the sum of all nn does not exceed 5⋅1055 \cdot 10^5.

Output

For each test case, output an integer which denotes the maximum flow.

Examples1

  1. Example 1

    Input
    1
    2
    2
    1 3 1
    3
    1 4 7
    2 5 8
    2 3 3 2 1 2 4
    
    Expected output
    5
    6