PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000062. Bầu cử tổng thống (Presidential Elections)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bầu cử tổng thống (Presidential Elections)

Presidential Elections

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

In Marecia, two political parties compete in a presidential election. The winner is not determined by a nationwide count of the popular vote. Instead, each state owns a fixed number of delegates. Every delegate from a state votes for the candidate who wins the popular vote in that state. A candidate must receive strictly more than half of all delegates to win the presidency.

For each state ii, you know the number CiC_i of citizens definitely voting for you, the number FiF_i definitely voting for the opponent, and the number UiU_i of undecided citizens. You may persuade undecided citizens to vote for you, at a cost of one per citizen. To guarantee victory, assume every undecided citizen whom you do not persuade votes for the opponent.

You win a state, and therefore all its DiD_i delegates, only if your popular vote in that state is strictly greater than your opponent's. A state-level tie is awarded to the opponent, as is a tie in the national delegate count.

Determine the minimum total number of undecided citizens you must persuade to guarantee winning the presidential election. If victory is impossible even when everyone you can persuade votes for you, report that no such plan exists.

Input

The first line contains an integer SS, the number of states. Each of the following SS lines contains four integers Di,Ci,Fi,UiD_i,C_i,F_i,U_i: the delegate count, your committed voters, the opponent's committed voters, and the undecided voters, respectively. All values are nonnegative, Di≥1D_i\ge1, and Ci+Fi+Ui≥1C_i+F_i+U_i\ge1.

Output

Output the minimum number of citizens who must be persuaded, as a nonnegative integer. If winning is impossible, output exactly impossible in lowercase.

Subtasks

  • Subtask 1 (20 points): 1≤S≤121\le S\le12, 1≤∑Di≤1201\le\sum D_i\le120, 1≤∑(Ci+Fi+Ui)≤200001\le\sum(C_i+F_i+U_i)\le20000; for each state, Di≥1D_i\ge1, Ci,Fi,Ui≥0C_i,F_i,U_i\ge0, and Ci+Fi+Ui≥1C_i+F_i+U_i\ge1.

  • Subtask 2 (30 points): 1≤S≤1001\le S\le100, 1≤∑Di≤8001\le\sum D_i\le800, 1≤∑(Ci+Fi+Ui)≤1071\le\sum(C_i+F_i+U_i)\le10^7; for each state, Di≥1D_i\ge1, Ci,Fi,Ui≥0C_i,F_i,U_i\ge0, and Ci+Fi+Ui≥1C_i+F_i+U_i\ge1.

  • Subtask 3 (50 points): 1≤S≤20161\le S\le2016, 1≤∑Di≤20161\le\sum D_i\le2016, 1≤∑(Ci+Fi+Ui)≤1091\le\sum(C_i+F_i+U_i)\le10^9; for each state, Di≥1D_i\ge1, Ci,Fi,Ui≥0C_i,F_i,U_i\ge0, and Ci+Fi+Ui≥1C_i+F_i+U_i\ge1.

Examples

Input

3
7 2401 3299 0
6 2401 2399 0
2 750 750 99

Output

50

Explanation

There are 1515 delegates, so you need at least 88. State 1 has 77 delegates but cannot be won: your 24012401 committed voters lose to 32993299 and no undecided voters remain. State 2 gives you 66 delegates for free, since 2401>23992401>2399. In state 3, persuading xx of the 9999 undecided voters results in 750+x750+x votes for you and 750+(99−x)750+(99-x) for the opponent. You win exactly when 2x>992x>99, so the minimum is x=50x=50. Those two delegates take you to 88 nationwide, and fewer than 5050 persuaded citizens cannot suffice.