Electoral systems across the world can vary widely. In some systems, such as winner-take-all, the winner is determined by the plurality of votes——the candidate that receives the most votes wins, and the loser(s) do not get a position.
Such elections can have "wasted votes." Conceptually, a wasted vote is a vote that did not affect the election outcome. While the exact definition of a wasted vote varies, we'll adopt the following definition: in an election with

voters, every vote for a losing candidate is wasted (these are called
lost votes), and every vote for a winning candidate beyond the strict majority of

votes the candidate needs to win is wasted (these are called
excess votes). For this problem we'll consider a two-party system (let's call the parties

and

) with elections that always involve one candidate from each party.
Let's illustrate wasted votes with a simple example between two candidates in a district. Suppose that the candidate for party

receives

votes and the candidate for party

receives

votes. All

votes for party

are wasted (lost votes for

), and

votes for party

are wasted (excess votes for

). This is because

needs

(
%2F2%5Crfloor%20%2B%201&preview=true)
) votes to win (over

), so the remaining

are wasted.
Political scientists use wasted votes to compute the
efficiency gap, a single number that summarizes wasted votes. Suppose we have a number of races in different districts, where each district elects one person. Across all districts there are

total votes cast, with

total wasted votes for party

and

total wasted votes for party

. Then the efficiency gap is:
%20%3D%20%5Cfrac%7B%7Cw_A%20%E2%88%92%20w_B%7C%7D%7BV%7D&preview=true)
.
A low efficiency gap indicates that the elections are competitive, and that the number of candidates elected from each party is representative of the total voting share for each party. When the efficiency gap is high, this can be an indication of gerrymandering. Gerrymandering refers to organizing voting districts in a way that favors a particular political outcome. Two common ways of doing this are to "pack" similar voters into districts, or "crack" them across multiple districts; both ways tend to diminish those voters' influence in electing candidates they would like to win.
In an election, districts are made up of precincts. A precinct is an indivisible group of voters. The votes for all precincts in a district are added together to find the results for that district. In this problem you are given a description of a number of precincts: the party vote totals for each precinct, and how those precincts have been grouped into districts. For each district, determine the party that wins and the wasted votes for each party. Then determine the efficiency gap between the two parties over all the districts.
The input describes one election. The first line contains two integers

and

, where

and
&preview=true)
. These indicate, respectively, the number of voting precincts and districts. Following this are

lines describing the precincts. Line

contains

numbers: the district

that precinct

is assigned to
&preview=true)
, the number of votes for the candidate from party
&preview=true)
, and the number of votes for the candidate from party
&preview=true)
. It is guaranteed that:
1. for each precinct

,
2. each district is assigned at least one precinct, and
3. there are no ties within any district.