#QHD0000062. Bầu cử tổng thống (Presidential Elections)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 , you know the number of citizens definitely voting for you, the number definitely voting for the opponent, and the number 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 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 , the number of states. Each of the following lines contains four integers : the delegate count, your committed voters, the opponent's committed voters, and the undecided voters, respectively. All values are nonnegative, , and .
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): , , ; for each state, , , and .
-
Subtask 2 (30 points): , , ; for each state, , , and .
-
Subtask 3 (50 points): , , ; for each state, , , and .
Examples
Input
3
7 2401 3299 0
6 2401 2399 0
2 750 750 99
Output
50
Explanation
There are delegates, so you need at least . State 1 has delegates but cannot be won: your committed voters lose to and no undecided voters remain. State 2 gives you delegates for free, since . In state 3, persuading of the undecided voters results in votes for you and for the opponent. You win exactly when , so the minimum is . Those two delegates take you to nationwide, and fewer than persuaded citizens cannot suffice.