LCC '25 Contest 3 Bonus Problem - Andyman's Christmas Tree (Alternate Version)
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:
- there must exist a simple path of
ornaments that consists of alternating coloured ornaments
What is the minimum cost to turn the tree valid? It is guaranteed there will exist a solution.
Input Specification
The first line contains the spaced integers and
, 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
3 1 3 3 2
GGRRR
1 2
1 3
2 4
2 5
Output for Sample Input 1
3
Explanation of Output for Sample Input 1
By swapping ornaments and
, there exists a path of length
of colour-alternating ornaments (
). The total cost is
.
Sample Input 2
11 5
4 3 3 2 3 4 2 3 2 1 2
RRRRRRRRRRR
1 2
1 3
2 4
2 5
4 6
3 7
3 8
7 9
7 10
8 11
Output for Sample Input 2
5
Explanation of Output for Sample Input 2
By swapping ornaments and
, the colour-alternating path of
makes the tree valid for a minimal cost of
.
Sample Input 3
16 6
1 4 3 3 3 2 2 4 3 3 2 2 1 3 1 4
RGRRGGGRRRRRGRRG
1 2
1 3
1 4
2 5
2 6
2 7
3 8
8 9
8 10
3 11
3 12
11 13
7 14
7 15
4 16
Output for Sample Input 3
4
Explanation of Output for Sample Input 3
By swapping ornaments ,
, and
, the colour-alternating path of
makes the tree valid for a minimal cost of
.
Comments