Strange Function
Contest: Educational Codeforces Round 85 (Rated for Div. 2)
Difficulty rating: 2500
Tags: binary search, data structures, dp, greedy

Let's denote the following function $$$f$$$. This function takes an array $$$a$$$ of length $$$n$$$ and returns an array. Initially the result is an empty array. For each integer $$$i$$$ from $$$1$$$ to $$$n$$$ we add element $$$a_i$$$ to the end of the resulting array if it is greater than all previous elements (more formally, if $$$a_i > \max\limits_{1 \le j < i}a_j$$$). Some examples of the function $$$f$$$:

1. if $$$a = [3, 1, 2, 7, 7, 3, 6, 7, 8]$$$ then $$$f(a) = [3, 7, 8]$$$;
2. if $$$a = [1]$$$ then $$$f(a) = [1]$$$;
3. if $$$a = [4, 1, 1, 2, 3]$$$ then $$$f(a) = [4]$$$;
4. if $$$a = [1, 3, 1, 2, 6, 8, 7, 7, 4, 11, 10]$$$ then $$$f(a) = [1, 3, 6, 8, 11]$$$.

You are given two arrays: array $$$a_1, a_2, \dots , a_n$$$ and array $$$b_1, b_2, \dots , b_m$$$. You can delete some elements of array $$$a$$$ (possibly zero). To delete the element $$$a_i$$$, you have to pay $$$p_i$$$ coins (the value of $$$p_i$$$ can be negative, then you get $$$|p_i|$$$ coins, if you delete this element). Calculate the minimum number of coins (possibly negative) you have to spend for fulfilling equality $$$f(a) = b$$$.

Input format:
The first line contains one integer $$$n$$$ $$$(1 \le n \le 5 \cdot 10^5)$$$ — the length of array $$$a$$$.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le n)$$$ — the array $$$a$$$.

The third line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ $$$(|p_i| \le 10^9)$$$ — the array $$$p$$$.

The fourth line contains one integer $$$m$$$ $$$(1 \le m \le n)$$$ — the length of array $$$b$$$.

The fifth line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ $$$(1 \le b_i \le n, b_{i-1} < b_i)$$$ — the array $$$b$$$.

Output format:
If the answer exists, in the first line print YES. In the second line, print the minimum number of coins you have to spend for fulfilling equality $$$f(a) = b$$$.

Otherwise in only line print NO.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Example input:
11
4 1 3 3 7 8 7 9 10 7 11
3 5 0 -2 5 3 6 7 8 2 4
3
3 7 10

Example output:
YES
20

Example input:
6
2 1 5 3 6 5
3 -9 0 16 22 -14
4
2 3 5 6

Example output:
NO

Editorial:
The "naive" version of the solution is just dynamic programming: let $$$dp_{i, j}$$$ be the minimum cost of removed elements (or the maximum cost of remaining elements) if we considered first $$$i$$$ elements of $$$a$$$, and the resulting sequence maps to the first $$$j$$$ elements of $$$b$$$. There are two versions of this solution, both working in $$$O(n^2)$$$:
• calculate this dp "as it is", so there are $$$O(nm)$$$ states and $$$O(1)$$$ transitions from each state;
• ensure that the $$$i$$$-th element is taken into $$$f(a)$$$, so there are $$$O(n)$$$ states (since each element appears in $$$b$$$ exactly once, the second state can be deduces from the first one), but up to $$$O(n)$$$ transitions from each state.
It turns out that we can optimize the second approach. Let's calculate the values of $$$dp_i$$$ in ascending order of $$$a_i$$$: first of all, we calculate the values of $$$dp_i$$$ such that $$$a_i = b_1$$$, then transition into states such that $$$a_i = b_2$$$, and so on.
Calculating $$$dp_i$$$ for $$$a_i = b_1$$$ is easy: since the first element of $$$a$$$ is always the first element of $$$f(a)$$$, we should delete all elements before the $$$i$$$-th if we want it to be the first element in $$$f(a)$$$. So, if $$$dp_i$$$ is the maximum possible sum of costs of remaining elements, if we considered the first $$$i$$$ elements of $$$a$$$ (and the $$$i$$$-th element gets included in $$$f(a)$$$), then $$$dp_i = c_i$$$ for indices $$$i$$$ such that $$$a_i = b_1$$$.
Okay, now let's consider advancing from $$$b_k$$$ to $$$b_{k + 1}$$$. If we want to go from $$$dp_i$$$ to $$$dp_j$$$ such that $$$a_i = b_k$$$ and $$$a_j = b_{k + 1}$$$, we should leave the element $$$a_j$$$ in the array and delete some elements between indices $$$i$$$ and $$$j$$$. Which ones should be deleted? First of all, they are all elements with negative deletion cost; but we should also get rid of all elements which could replace $$$a_j$$$ in $$$f(a)$$$ — that is, all elements that are greater than $$$a_i$$$. So the remaining elements are $$$x$$$ which have $$$i < x < j$$$, $$$c_x > 0$$$ and $$$a_x \le a_i$$$, and we should be able to compute the sum of such elements.
Even if we manage to do it in $$$O(\log n)$$$, which is possible, there may be up to $$$O(n^2)$$$ possible pairs of $$$dp_i$$$ and $$$dp_j$$$ to consider. The easiest way to get rid of that is to sort all occurences of $$$b_k$$$ and $$$b_{k + 1}$$$, and process them in ascending order, maintaining the best $$$dp_i$$$ that was already met. That way, each of the elements of $$$a$$$ will be considered at most twice, so this solution runs in $$$O(n \log n)$$$.
We know how to calculate the $$$dp$$$ values now, but how to determine the answer? We should consider all values of $$$dp_i$$$ such that $$$a_i = b_m$$$ and delete all elements with negative costs and all elements that are greater than $$$a_i$$$ from the suffix $$$[i + 1, n]$$$ — so this is another query of the form "compute the sum of $$$c_x$$$ over $$$x$$$ which have $$$i < x < j$$$, $$$c_x > 0$$$ and $$$a_x \le a_i$$$". The most straightforward way to process them in $$$O(\log n)$$$ is to use a persistent segment tree, but since $$$a_i$$$ does not decrease in these queries as we process them, we may maintain the elements we are interested in with a much simpler data structure, for example, Fenwick tree.