#P8877. [传智杯 #5 初赛] I-不散的宴会

[传智杯 #5 初赛] I-不散的宴会

Background

The school is organizing a banquet.

Lianzi and Meili found that the school’s structure is very complex. Among students, there are relationships of departments and supervisors. Inside each department, the supervisor relationship forms a single chain. The student with the highest rank in one department may also be restricted by some student in another department.

Lianzi and Meili also attend the banquet. But they have their own ways to judge the attending students. For example, they like some departments more and are not interested in others. They also have different opinions about students at different ranks.

As described in a classic problem, every student does not want to attend the banquet together with their direct supervisor.

Meili wants to know, in the best case, how many students attending the banquet are liked by her.

Problem Description

The student society can be seen as a node array arranged into an isosceles right triangle. This node array has nn rows, and row ii has ii nodes. We label the node in row ii and column jj as (i,j)(i,j).

  • These nodes have weights. Specifically, the weight of node (i,j)(i,j) is ricjr_i\oplus c_j, where rr and cc are given 0101 sequences, and \oplus is the bitwise XOR operation.
  • These nodes are connected by edges. Specifically, for 1i<n1\le i< n, 1ji1\le j\le i, there is an edge connecting (i,j)(i,j) and (i+1,j)(i+1,j). In addition, for 2in2\le i\le n, there is also an edge connecting (i,i)(i,i) and (i1,ai)(i-1,a_i). Here, aa is a given sequence.

Now you need to choose some nodes such that no two chosen nodes are connected by an edge, and maximize the sum of weights of the chosen nodes.

The figure below shows an example with n=8n=8. Black nodes have weight 11, and white nodes have weight 00.

Note: The picture only symbolically shows part of the values of rir_i and cic_i. The actual values in the picture are $\def\t{,\allowbreak}r=\{1\t 1\t 0\t 1\t 0\t 0\t 0\t 1\}\t c=\{0\t 0\t 1\t 0\t 1\t 1\t 0\t 0\}$.

Input Format

  • The first line contains a positive integer nn, describing the size of the node array.
  • The second line contains nn integers 00 or 11, describing the values of rir_i.
  • The third line contains nn integers 00 or 11, describing the values of cic_i.
  • The fourth line contains n1n-1 positive integers, where the ii-th number describes the value of ai+1a_{i+1}.

Output Format

  • Output one integer in a single line, describing the maximum possible sum of weights of the selected nodes.
8
1 1 0 1 0 0 0 1
0 0 1 0 1 1 0 0
1 1 3 2 2 1 4
14

Hint

Sample Explanation

One possible selection is shown in the figure below. The orange-red blocks indicate the selected nodes.

Constraints and Notes

For all testdata, it is guaranteed that 1n1061\le n\le 10^6, ri{0,1}r_i\in\{0,1\}, ci{0,1}c_i\in\{0,1\}, 1ai<i1\le a_i<i.

Translated by ChatGPT 5