Galactic Reconstruction
시간 제한1초메모리 제한2048 MB
제안된 워프 게이트를 순서대로 처리하면서 각 집단의 재산을 관리하고, 각 제안이 BUILT인지 IMPOSSIBLE인지 UNNECESSARY인지 판정한다.
문제
A technological disaster has struck an intergalactic civilization. For unknown reasons, all warp drives used to travel between colonies have stopped working. However, they have a new warp drive technology they can employ to connect colonies and form new clusters of colonies.
Initially, there are colonies numbered through , with the -th colony having an initial wealth of . Each colony initially belongs to its own cluster of size , with the wealth of the cluster being the wealth of its constituent colony. Clusters can be combined into a larger cluster by building a warp gate that connects two colonies belonging to two different clusters. That is, if cluster of size contains colonies and cluster of size contains colonies , then building a warp gate connecting any and would combine clusters and to form a single cluster of size , containing .
There is an ordered list of warp gates that have been proposed to be built. The -th proposal suggests building a warp gate that connects colonies and at a cost of . If colonies and belong to different clusters, and both of the clusters they belong to individually have a total wealth of least , then the warp gate between and will be built. Before building the warp gate, the clusters that and belong to will both spend to build the warp gate. This means that both clusters each decrease their total wealth by before the two clusters are combined. If and already belong to the same cluster, or one or both of the clusters containing and don't have a total wealth of at least , the warp gate will not be built, and both clusters will maintain their wealth.
The proposals are reviewed in the order they appear in the list, and the next proposal is not reviewed until the previous warp gate is either built, or determined not to be built. Your task is to determine which warp gates will be BUILT, which are IMPOSSIBLE because one or both of the colonies lack the necessary wealth, and which are UNNECESSARY because the two colonies already belong to the same cluster. If it is both impossible and unnecessary to build a proposed warp gate, you should only report UNNECESSARY.
For example, consider a scenario where colonies and are connected by a warp gate (they form a cluster) with a total wealth of , and and also make up a cluster with a wealth of . If building a warp gate connecting and were proposed at a cost of wealth, the cost of this warp gate would have to be paid by both the cluster made up of and as well as the cluster made up of and , if it were to be built. Since both clusters have at least wealth, the warp gate is built and now colonies and form a cluster with a total wealth of . Afterward, if it were proposed to build a warp gate between colonies and , it would be reported unnecessary because colonies and already belong to the same cluster. Finally, if it were proposed to build a warp gate between colonies and at a cost of wealth, regardless of 's cluster's wealth, it would be impossible because 's cluster only has wealth.
입력
The first line contains two integers and , the number of colonies and the number of warp gate proposals, respectively.
The second line contains space separated integers where the -th integer is the wealth of the -th colony, .
Then follows lines, the -th of which contains three integers , and where and denote the colony numbers to connect with the -th proposed warp gate (where ) and being the cost.
출력
For each of the proposed warp gates, output a line containing:
BUILTif the warp gate can and should be built.UNNECESSARYif the colonies that would be connected by the warp gate already belong to the same cluster.IMPOSSIBLEif it cannot be built because one or both of the clusters lack the necessary wealth to built the warp gate.
Note: If it is both unnecessary and impossible to build a warp gate, you should only report UNNECESSARY.