#K47092. Total Installation Cost
Total Installation Cost
Total Installation Cost
You are given a target package identifier and a list of packages. Each package is characterized by a unique id, an installation cost (price), and a list of dependencies. The total installation cost of a package is defined recursively as its own cost plus the total cost of all its dependencies. Formally, if (C(p)) is the cost of package (p), (P(p)) is its price, and (D(p)) is the set of its dependencies, then: [ C(p) = P(p) + \sum_{d \in D(p)} C(d) ] Your task is to compute (C(target)) for a given target package.
inputFormat
The input is read from standard input (stdin) with the following format:
- The first line contains the target package id (a string).
- The second line contains an integer (n) representing the number of packages.
- Each of the following (n) lines describes a package in the format:
id price k dep1 dep2 ... depk
where:
- id is the package identifier (a string),
- price is the installation cost (an integer),
- k is the number of dependencies (an integer),
- followed by k dependency identifiers (strings).
outputFormat
Print a single integer to standard output (stdout) representing the total installation cost of the target package, computed recursively including the costs of all its dependencies.## sample
B
3
A 50 0
B 30 1 A
C 40 1 B
80