Woburn Challenge 2017-18 Finals Round - Senior Division
The Party of Extraterrestrial Gangsters has begun its invasion of Earth! Vast armies of PEG soldiers have been deployed down to the surface throughout Scarberia, and the cows and monkeys have engaged them in battle.
Amidst the fighting, however, the aliens have also transported something else to the planet's surface — a bomb with devastating nuclear power! All life in Scarberia, and perhaps the rest of Earth, would surely cease if the bomb were to detonate. Fortunately, the PEG leaders are honourable enough to give their enemies a fighting chance. As such, they've set the bomb to go off after a period of three hours, and implanted a system for defusing it. They've even included an instruction manual along with it!
On the surface of the bomb, there are electrical terminals. There are also wires running amongst the terminals, the -th of which runs between terminals and , and is either black (if ), or is otherwise white (if ). The wires have been arranged such that all pairs of terminals are reachable from one another by following a sequence of wires.
Bo Vine and the Head-Monkey have gotten their hands on the bomb and its accompanying instruction manual. According to the manual, the bomb will turn itself off if the following conditions are all met:
- Each terminal receives an electrical current with some voltage , such that is a positive integer.
- For each black wire (such that ), the greatest common divisor (GCD) of and is equal to .
- For each white wire (such that ), the GCD of and is greater than .
Bo Vine has ordered his cow engineers to prepare the necessary electrical equipment as quickly as possible. Meanwhile, the Head-Monkey has personally taken it upon herself to come up with a set of voltages which will successfully satisfy the conditions to defuse the bomb. However, having realized that PEG is essentially mocking them by dispatching a bomb which may be defused so easily, she's decided to get back at them by demonstrating the monkeys' superior intelligence and successfully defusing the bomb using as little voltage as possible. Help the Head-Monkey determine the minimum possible total voltage (sum of values) required to get the job done. Just make sure to figure it out within three hours!
Subtasks
In test cases worth of the points, .
Input Specification
The first line of input consists of a single integer, .
lines follow, the -th of which consists of three
space-separated integers, , , and , for
.
Output Specification
Output a single integer, the minimum possible total voltage required to defuse the bomb.
Sample Input
7
4 1 1
4 5 0
7 6 1
3 6 1
1 7 0
2 4 1
Sample Output
16
Sample Explanation
It's optimal to send
- a -volt current to terminal ;
- -volt currents to terminals , , and ; and
- -volt currents to terminals , , and .
Comments