Halcyon
시간 제한10초메모리 제한1024 MB
같은 n개 정점 위의 두 가중치 트리가 주어질 때, 각 k에 대해 첫 번째 트리에서 k개, 두 번째 트리에서 n-1-k개의 간선을 사용하는 최소 가중치 신장 트리의 무게를 구하고 불가능하면 -1을 출력한다.
문제
Minseok and Martin have two weighted trees . They share the same vertex set of size , where we index each vertex with integers from .
For a given , Minseok selects edges from , and Martin selects edges from . The union of their selected edges should form a tree. If this is possible, they should minimize the total weight of selected edges.
입력
In the first line, a single integer denoting the number of vertices in both trees is given.
In the next lines, description of the first tree is given. Each of the lines contains three integers , which indicates there is an edge connecting two vertices with weight .
In the next lines, description of the second tree is given in the same format.
출력
For all , print the minimum total weight, or print -1 if it is impossible.
제한
- ()