Woburn Challenge 2017-18 Round 3 - Senior Division

You own a group of
webpages dedicated solely
to the art of dynamic programming. Each page
includes links to
other distinct pages, the
-th of which is
page
. The total number of links
is at most
. You consider a given page
to be
"accessible" from a given page
if it's possible to follow a sequence
of 0 or more links starting from page
and ending at page
.
Unfortunately, your pages aren't getting as much traffic as you'd like. It's clear that the most promising way of encouraging more people to come across your pages is to somehow cause them to appear earlier in the search results when someone searches for "dynamic programming" on the internet's most popular search engine.
Each page is assigned an integral "relevancy score" for a given search
query such as "dynamic programming", and pages with higher scores then
appear earlier in the results for that query. Page 's initial
relevancy score is
. Fortunately, if
you donate
dollars to the search
engine company, you can increase page
's score by
! You can choose
to do so
or more times for any page.
You're not yet sure about how much money you'd like to spend on helping
to make your pages "more relevant", so you'll consider a series of
independent questions. For the
-th question,
you'd like to determine the minimum amount of money you'd need to spend
in order to make all
of your pages "accessible" from search results
with relevancy scores no smaller than
.
In other words, after you finish increasing all of the pages'
scores as necessary, for each page
, there must
exist at least one page
such that page
's final relevancy score
is greater than or equal to
and page
is accessible from page
. Note that all
questions are independent, with changes to pages'
scores not carrying over between them.
Subtasks
In test cases worth of the points,
,
, and
.
In test cases worth another of the points,
.
Input Specification
The first line of input consists of a single integer, .
lines follow, the
-th of which consists of three space-separated
integers,
,
, and
, followed by
more
integers
, for
.
The next line consists of a single integer, .
lines follow, the
-th of which consists of a single integer,
, for
.
Output Specification
Output lines, the
-th of which should consist of a single real
number, the minimum cost (in dollars) required such that all
pages
are accessible from search results with relevancy scores no smaller than
.
Sample Input
5
9 3 1 4
11 1 0
8 2 1 1
4 2 1 3
7 2 1 2
3
10
12
1
Sample Output
9
18
0
Sample Explanation
If page 's relevancy score is increased to
(for a cost of
) and
page
's relevancy score is also increased to
(for a cost of
),
then all
pages will be reachable from search results with relevancy
scores no smaller than
. Pages
,
, and
will have sufficiently high
relevancy score themselves, and page
links to page
which then links
to page
. This total cost of
is optimal.
Comments