The lord also ought to act toward his faithful vassal reciprocally in all these things.
- Fulbert of Chartres, letter to William, Duke of Aquitaine (1020)
You are an official responsible for the roads in a kingdom, and the highwaymen who have recently begun to infest them are giving you a headache. To address this situation, the king intends to order the lords under his command to provide knights and form guard units to maintain order and safety on the roads. However, the number of knights each lord can provide varies, so it may not be possible to assign knights to every road.
There are lords, including the king, numbered from to . Lord is the king. Each lord other than the king serves exactly one liege and is that liege's vassal. Starting from any lord and repeatedly following their liege always leads to the king.
Each lord governs a piece of land called an estate. The liege of lord is lord , and each lord other than the king has their estate connected to their liege's estate by one road.
The kingdom may also have roads connecting the estates of lords who are not each other's liege and vassal. However, the king wishes to prioritize protecting travel between lieges and vassals. Therefore, guard units may be assigned only to the roads connecting a lord's estate to their liege's estate. All other roads are ignored in this problem.
A guard unit assigned to a road consists of two knights, one provided by each lord at its endpoints. Lord can provide knights to help maintain order. Each knight may join at most one guard unit, and at most one guard unit may be assigned to each road. Not every knight needs to be assigned.
A road with a guard unit assigned to it is safe. Find the maximum possible number of safe roads among the eligible roads.
The first line contains the number of lords .
The second line contains , separated by spaces.
Print the maximum possible number of safe roads.
In the first example, guard units can be assigned to the roads between estates and , between estates and , and between estates and . Every eligible road is incident to estate or , and these two lords can provide a total of only knights. Therefore, guard units cannot be assigned to or more roads.
The third line contains , separated by spaces.
In the second example, every eligible road is incident to the king's estate. Since the king cannot provide any knights, no guard unit can be assigned.