Under construction

Still being built. Things may change

RGB트리

Interview

Time limit1.5sMemory limit1024 MB

Summary
Color each bulb of a tree red, green, or blue so adjacent bulbs differ, maximizing the total beauty, and print the best sum and coloring.
Level

Easy3 of 10

Topics
Tree, Dynamic programming
Solved
No attempts yet

문제

The RGB Tree, a landmark of RGB Street, has NN light bulbs. The bulbs are numbered 1 to NN, and each bulb can emit red, green, or blue light. The bulbs are connected in a tree structure, and adjacent bulbs must emit light of different colors. Given the beauty of each bulb for each color, find a coloring that maximizes the sum of beauty.

입력

The first line contains the number of bulbs NN. (1≤N≤500 0001 \leq N \leq 500\,000)

From the second line to the NNth line, each line contains two bulb numbers separated by a space, meaning the two bulbs are adjacent.

From the (N+1)(N+1)th line to the 2N2Nth line, each line contains three integers separated by spaces. On line N+iN+i, rir_i, gig_i, and bib_i are the beauty values of bulb ii when it emits red, green, and blue light. (1≤ri,gi,bi≤1 0001 \leq r_i, g_i, b_i \leq 1\,000)

출력

On the first line, print the maximum possible sum of beauty over all bulbs.

On the second line, print the colors of bulbs 1 to NN in order, without spaces. Print R for red, G for green, and B for blue. If several answers exist, print any of them.

Examples1

  1. Example 1

    Input
    8
    1 2
    1 3
    1 4
    1 5
    5 6
    6 7
    4 8
    69 67 74
    9 22 55
    99 24 29
    53 59 33
    49 12 48
    19 57 24
    17 88 93
    8 85 7
    
    Expected output
    558
    GBRRRGBG