inputs: Solve in Python:
You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree. 

You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.

You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \neq y, y \neq z, x \neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.

You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.


-----Input-----

The first line contains one integer $n$ $(3 \le n \le 100\,000)$ — the number of vertices.

The second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \dots, c_{1, n}$ $(1 \le c_{1, i} \le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.

The third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \dots, c_{2, n}$ $(1 \le c_{2, i} \le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.

The fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \dots, c_{3, n}$ $(1 \le c_{3, i} \le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.

Then $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \le u_j, v_j \le n, u_j \neq v_j)$ — the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.


-----Output-----

If there is no good painting, print $-1$.

Otherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 3)$, where the $i$-th integer should denote the color of the...
targets: from collections import defaultdict, deque
from itertools import permutations


class Graph:
    def __init__(self):
        self.E = {}
        self.V = defaultdict(list)

    def put(self, v1, v2):
        if v1 not in self.E:
            self.E[v1] = 1
        if v2 not in self.E:
            self.E[v2] = 1
        self.V[v1].append(v2)
        self.V[v2].append(v1)

    def _adj(self, v1):
        return self.V[v1]

    def bfs(self, v):
        visited = set([v])
        path = [v]
        q = deque([v])
        while q:
            v1 = q.pop()
            for v2 in self._adj(v1):
                if v2 not in visited:
                    visited.add(v2)
                    path.append(v2)
                    q.appendleft(v2)

        return path


def __starting_point():
    n = int(input())
    cp = []
    for _ in range(3):
        cp.append(list(map(int, input().split(' '))))

    inv = False
    vert = defaultdict(int)
    graph = Graph()
    for _ in range(n - 1):
        v1, v2 = list(map(int, input().split(' ')))
        vert[v1] += 1
        if vert[v1] > 2:
            inv = True
            break
        vert[v2] += 1
        if vert[v2] > 2:
            inv = True
            break

        graph.put(v1, v2)

    if inv:
        print(-1)
    else:
        for key in vert:
            if vert[key] == 1:
                start = key
                break

        path = graph.bfs(start)

        min_cost = float('inf')
        min_cost_perm = (0, 1, 2)
        for p in permutations([0, 1, 2]):
            cur_cost = 0
            for i, v in enumerate(path):
                cur_cost += cp[p[i % 3]][v - 1]

            if cur_cost < min_cost:
                min_cost_perm = p
                min_cost = cur_cost

        # print(path, graph.V)
        ans = [0]*n
        for i, v in enumerate(path):
            ans[v - 1] = min_cost_perm[i % 3] + 1

        print(min_cost)
        print(' '.join(map(str, ans)))

__starting_point()