#P6621. Magical Pricing Adjustment
Magical Pricing Adjustment
Magical Pricing Adjustment
You are given a magic shop with n gifts numbered from 1 to n. The i-th gift has a charm value \(c_i\) and an initial price \(v_i\). Two friends wish to buy a set of gifts satisfying a very peculiar requirement. Let \(S = \{s_1,s_2,\dots,s_p\}\) be the set of chosen gifts. They require that for every nonempty subset \(T \subseteq S\), the XOR of the charm values is nonzero:
[ c_{t_1} \oplus c_{t_2} \oplus \cdots \oplus c_{t_q} \neq 0 \quad (\forall ; \emptyset \neq T \subseteq S). ]
In other words, the set \(S\) is valid if every nonempty subset of \(S\) has a nonzero XOR of charm values. It can be shown that this condition is equivalent to requiring that all charm values of the chosen gifts are nonzero and linearly independent (over \(\mathbb{F}_2\)). Therefore, among all valid gift sets, the friends want to choose one with as many gifts as possible (i.e. a basis for the subspace spanned by the \(c_i\)).
Since there may be many valid gift sets, the shop owner has pre-selected two disjoint valid sets \(A\) and \(B\), each containing exactly \(k\) gifts (note that \(k\) is the maximum possible size of a valid set). One friend prefers set \(A\) while the other favors set \(B\>. In order to convince the other to choose \(A\), the friend with preference for \(A\) agrees only if, after a magical price adjustment, \(A\) becomes the unique minimum-price valid set and \(B\) becomes the unique maximum-price valid set.
The magical price adjustment works as follows. For any gift \(i\), you can change its price from \(v_i\) to any integer \(x\) by spending \((x-v_i)^2\) units of magical power; each gift’s price can be adjusted at most once.
Your task is to compute the minimum total magical power required to adjust the prices so that after adjustment:
- The valid set \(A\) has the smallest total price among all valid sets, and
- The valid set \(B\) has the largest total price among all valid sets.
It turns out that a simple sufficient assignment exists when \(A\) and \(B\) are disjoint. Namely, let k be the size of each valid set and define three integers \(L, M, H\) with
[ L < M < H \quad \text{and} \quad M = L+1,; H = L+2. ]
Assign the new price (x_i) as follows:
- If \(i \in A\), assign \(x_i = L\).
- If \(i \in B\), assign \(x_i = H\>.
- If \(i \notin A \cup B\), assign \(x_i = M\>.
Then the total price of set \(A\) becomes \(k \times L\) and any other valid set will have a total price at least \(k \times L + (M - L)\) so that \(A\) is the unique minimum-price valid set. Similarly, the total price of set \(B\) is \(k \times H\) and any other valid set has a total price at most \(k \times H - (H - M)\> making \(B\) uniquely maximum.
The cost to change the price of gift \(i\) is \((x_i - v_i)^2\). Thus, if we denote by SA, SB and SO the sets of indices in \(A\), \(B\) and in \(\{1,2,\dots,n\} \setminus (A\cup B)\) respectively, the total magical cost is:
[ F(L)=\sum_{i\in A}(L-v_i)^2+\sum_{i\in B}((L+2)-v_i)^2+\sum_{i\notin A\cup B}((L+1)-v_i)^2. ]
Your goal is to choose an integer \(L\) so that \(F(L)\) is minimized. Output the minimum total magical power required.
inputFormat
The input consists of the following lines:
- An integer
n
— the number of gifts. n
space‐separated integers:c1 c2 ... cn
representing the charm values of the gifts. (\(1 \le c_i \le 10^9\); note that \(c_i\) are guaranteed to be nonzero but are otherwise arbitrary.)n
space‐separated integers:v1 v2 ... vn
representing the initial prices of the gifts. (\(|v_i| \le 10^9\))- An integer
k
which is the size of the valid sets (i.e. the maximum number of gifts that can be chosen). k
distinct space‐separated integers representing the indices (1-indexed) of gifts in setA
.- An integer
k
(again) representing the size of the valid sets. k
distinct space‐separated integers representing the indices (1-indexed) of gifts in setB
.
It is guaranteed that A
and B
are disjoint valid sets.
outputFormat
Output a single integer, the minimum total magical power required to adjust the prices so that set A
becomes the unique minimum-price valid set and set B
becomes the unique maximum-price valid set.
sample
5
1 2 5 6 7
3 1 4 1 5
2
2 3
2
4 5
15