Warring States

Process alliance and war records between groups, merging by sum for alliances and subtracting troops for wars, then report surviving groups sorted by troop count.

Medium5Union-findImplementationSortingSimulationInterviewNo attempts yetTime limit1sMemory limit128 MB

Problem

There are NN nations, numbered from 11 to NN. Each nation holds a number of troops that represents its strength.

MM records are given in order. Each record is either an alliance or a war.

  • Alliance: when two nations form an alliance, the groups containing them merge into one, and the merged troops equal the sum of the two groups.
  • War: when the groups containing two nations go to war, the stronger group wins and the losing group becomes its subordinate, so the two merge into one. The remaining troops equal the winner's troops minus the loser's troops. If both groups have equal troops, both groups perish.

Every nation keeps its promises, so an ally of my ally is also my ally, and when one member of a group starts a war, the whole group fights together. A subordinate group is treated like an ally, so every nation on the losing side becomes a subordinate of the winning group. Nations tied by alliance or subordination count as a single nation.

After all records are processed, find the number of surviving nations and their remaining troops, and print the troop counts in ascending order.

Input

The first line contains the number of nations NN and the number of records MM. (1N100,0001 \le N \le 100{,}000, 1M100,0001 \le M \le 100{,}000)

Each of the next NN lines contains the troops AiA_i of nation ii as a positive integer. (1Ai10,0001 \le A_i \le 10{,}000)

Each of the next MM lines contains a record as three integers OO, PP, and QQ. If O=1O = 1, nations PP and QQ form an alliance, and if O=2O = 2, nations PP and QQ go to war. (1P,QN1 \le P, Q \le N)

No record pairs two nations that already belong to the same group, and no record contains a nation that has already perished.

Output

Print the number of surviving nations on the first line.

If at least one nation survives, print the remaining troops of each surviving nation on the next line in ascending order, separated by spaces. If no nation survives, print only 00 on the first line.