LCC '25 Contest 3 S5 - Andyman's Christmas Tree
View as PDF has a wonderful Christmas tree! His Christmas tree has ornaments, where
branches connect the ornaments together on the tree, such that there is exactly one path of branches from any ornament to any other ornament. The ornaments are numbered from
to
, where the
ornament is either green or red, and has a cost
to swap its colour.
would like to swap some of the ornaments to make his tree valid. A tree is considered valid if:
- for every pair of red ornaments
and
, the path on the tree from ornament
to
must consist of all red ornaments
What is the minimum cost to turn the tree valid?
Input Specification
The first line contains the integer
, the number of ornaments on the tree.
The next line contains spaced integers
, where the cost of swapping the colour of the
ornament is
.
The next line contains a string of length , where the
character is either
G to indicate that the ornament is green, or
R to indicate that it's red.
The next lines contains
spaced integers
and
, denoting a branch connecting the ornaments
and
.
Output Specification
On a single line, output the minimal cost to turn the tree valid.
Sample Input 1
5
4 2 1 3 3
GGRRR
1 2
1 3
2 4
2 5
Output for Sample Input 1
3
Explanation of Output for Sample Input 1
A suboptimal way to make the tree valid is to swap ornaments and
for a total cost of
.
Swapping ornaments and
for a total cost of
is better, but not the most optimal. Notice that even with just a singular red ornament, it meets the conditions of being a valid tree as there is no pair of red ornaments that violates the requirement of being a valid tree.
However, the most optimal is to swap ornaments and
for a total cost of
.
Sample Input 2
8
3 1 4 2 1 2 2 1
RGGGRRRR
1 2
1 3
2 4
4 5
2 6
3 7
3 8
Output for Sample Input 2
5
Explanation of Output for Sample Input 2
By swapping nodes ,
,
, and
, the tree can be made valid for a minimal total cost of
.
Comments