#P5034. Maximizing Safety Index for Town Conversion

    ID: 18272 Type: Default 1000ms 256MiB

Maximizing Safety Index for Town Conversion

Maximizing Safety Index for Town Conversion

In the town there is one mayor and n−1 town members. Each town member has a direct supervisor f_i. For each town member, its distance from the mayor is defined as (d_i), which equals the number of supervisors on the path from the mayor plus one. In other words, if a town member is already a direct subordinate of the mayor then (d_i = 1). The mayor wants every town member to become his direct subordinate (i.e. have a distance equal to 1).

The mayor can perform a conversion operation once per minute. At minute (t) (with (t > 0) and (t) starting at 1), he selects one town member (who is not yet his direct subordinate) and converts that member to a direct subordinate. When converting a town member, the mayor gains a safety index equal to ((d_i + t) \mathbin{&} k), where (&) denotes the bitwise AND operation and (d_i) is the current distance of that town member at the moment of conversion.

Important: When a town member is converted, all of his own subordinates (if any) move along with him. That is, their relative supervisor relations remain unchanged, though the overall distance from the mayor may be reduced.

The task is to determine the maximum total safety index that can be obtained when all town members become direct subordinates (i.e. when every town member’s distance is 1).

Observation and Hint:
A town member who is not initially a direct subordinate (i.e. (d_i > 1)) must be converted. However, if an ancestor (in the tree of supervision, where the mayor is the root) is converted before a descendant, the descendant’s distance will drop to 2 regardless of its original distance. Therefore, in order to collect the maximum safety index, it is optimal to convert town members in each branch from the bottom up (i.e. convert descendants before their ancestors).

If a town member has an original distance of (d_i) and is converted at minute (t) (before his non–mayoral supervisor is converted) then the safety index gained for that member is

[ (d_i+t) \mathbin{&} k ]

When multiple town members need conversion (say, a total of (m) members) the conversion minutes will be (t = 1,2,\ldots,m). Under the bottom-up constraint (i.e. every descendant must be converted before his ancestor), an optimal strategy is to order the conversions so that in every conversion pair (descendant, ancestor) the descendant is converted earlier. In other words, it is beneficial to delay the conversion of a town member with a lower current distance in order to assign it a larger minute value (t}, while guaranteeing that all of its descendants (which naturally have a higher original distance) are converted earlier.

The problem can be solved as follows:

  1. Compute the initial distance \(d_i\) for each town member from the mayor. The mayor is considered as node 1 and has no conversion operation. Only town members with \(d_i > 1\) (i.e. whose direct supervisor is not the mayor) require conversion.
  2. For all town members that require conversion, note that converting a town member before its (non–mayoral) supervisor ensures that the town member’s distance remains equal to its original \(d_i\). Thus, the strategy is to convert the members in each branch in descending order of their \(d_i\) (i.e. from the deepest nodes upward).
  3. Assume there are \(m\) members to convert and let the conversion times be \(t = 1, 2, \ldots, m\). In each branch the constraint forces the descendant to be assigned a smaller minute value than its ancestor. Hence, the optimal ordering is the unique ordering that sorts all convertible nodes in descending order of \(d_i\) (ties can be broken arbitrarily).
  4. The total maximum safety index is then the sum over all nodes that need conversion of \((d_i + t_i) \mathbin{\&} k\), where \(t_i\) is the minute assigned to that conversion in the sorted order.

Input Format:
The first line contains two integers \(n\) and \(k\) \((2 \le n \le 10^5, 0 \le k \le 10^9)\), where \(n\) is the total number of nodes (1 mayor and \(n-1\) town members), and \(k\) is the parameter used in the bitwise AND operation.
The second line contains \(n-1\) integers \(f_2, f_3, \ldots, f_n\), where \(f_i\) \((1 \le f_i < i)\) is the direct supervisor of the \(i\)th town member. Note that a town member with \(f_i = 1\) is already a direct subordinate of the mayor and does not require conversion.

Output Format:
Output a single integer: the maximum total safety index that can be achieved after converting all necessary town members.

inputFormat

The input begins with a line containing two integers (n) and (k).

The second line contains (n-1) integers: (f_2, f_3, \ldots, f_n), where each (f_i) is the direct supervisor of the (i)th town member (1-based indexing).

Note: Town members for which (f_i = 1) are already direct subordinates and do not need to be converted.

outputFormat

Output a single integer — the maximum total safety index that can be attained after all necessary conversions.

sample

4 7
1 2 2
7