This page is still under construction.

Parts of this page are still being built. What you see may change.

Premier League

Time limit2sMemory limit512 MB

Summary
Assign every Pokemon to Andy or Jordan, then pay for battles whose two Pokemon have different owners, minimizing total cost.
Level

Medium7 of 10

Topics
Graph, Minimum spanning tree, Greedy, Union-find
Solved
No attempts yet

Problem

Andrew and Jordan are massive Pokemon fans, and have many friends who are also huge fans of Pokemon as well. However, both of them are very competitive! To satisfy their competitive nature, you are going to host a mini premier draft tournament, the Premier League! Both Andy and Jordan have submitted bids to rent out each Pokemon from your inventory, and then will begin use their rented Pokemon in a series of battles! To ensure that the battles are fair, you set up a schedule of battles before the fans submitted their bids. Note that if a battle is between two Pokemon owned by the same trainer, the battle will not occur because the trainer will win either way.

As the tournament organizer, you don't really care who wins. However, you do care about the cost to host this tournament. While you fortunately have access to a gym where the battles can take place, you also know that the gym charges a battle fee to cover preparation and post-battle healing. Furthermore, acquiring each Pokemon costs a good amount of money! Even though Andy and Jordan each want as many Pokemon as possible, both may be willing to pay different prices for each individual Pokemon.

Now that the bids are in, you have to decide which fan wins the bid for each Pokemon. Given the battle schedule, the cost of each Pokemon for you, how much Andy and Jordan are willing to pay for each Pokemon, as well as the costs of the battles, determine the cheapest total cost for you to organize this extravagant showdown! Bear in mind all Pokemon need to be assigned to either Andy or Jordan.

Input

The first line of input contains 1≤n≤1021 \le n \le 10^2, the number of Pokemon that are available for purchase. The following nn lines each contain 33 integers 1≤c_i,a_i,j_i≤1051 \le c\_i, a\_i, j\_i \le 10^5, where a_i,j_i≤c_ia\_i, j\_i \le c\_i, which are the cost for you to acquire the ithi^{th} Pokemon, Andy's bid for the ithi^{th} Pokemon, and Jordan's bid for the ithi^{th} Pokemon, respectively.

The next line contains 1≤b≤5⋅1021 \le b \le 5 \cdot 10^2, the number of battles to occur. After that, the next bb lines each contain three integers 1≤a_j,b_j≤n,1≤c_j≤1051 \le a\_j, b\_j \le n, 1 \le c\_j \le 10^5, signifying that the jthj^{th} battle will occur between Pokemon a_ja\_j and b_jb\_j and will cost c_jc\_j if it occurs. Note that there may be multiple battles between the same pairs of Pokemon.

Output

Output a single number, the minimum total cost to run this tournament. Note that while many Pokemon may be expensive, you can offset the costs with Andy and Jordan's bids.

Examples1

  1. Example 1

    Input
    4
    300 200 100
    300 250 150
    300 100 200
    300 100 260
    3
    1 2 500
    3 4 500
    2 3 5
    
    Expected output
    295