Order Capital Round 1 (Codeforces Round 1038, Div. 1 + Div. 2)
B
Pile Shuffling
Codeforces
1100
原题
← AC →
翻译中…

You are given $n$ binary piles, the $i$-th of which consists of $a_i$ zeros on the top and $b_i$ ones on the bottom.

翻译中…

In one operation, you can take the top element of any pile and move it to any position in any pile, including the pile it was taken from.

翻译中…

image Solution of the first example test case.

翻译中…

Calculate the minimum number of operations required to make it so that the $i$-th pile consists of $c_i$ zeros on the top and $d_i$ ones on the bottom.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of piles.

翻译中…

Then $n$ lines follow, the $i$-th containing four integers $a_i$, $b_i$, $c_i$, $d_i$ ($0 \leq a_i, b_i, c_i, d_i \leq 10^9$) — the original and target state of the $i$-th pile.

翻译中…

It is guaranteed that there exists a sequence of operations that transforms the piles into the target state.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output one integer — the minimum number of operations required to achieve the target state.

翻译中…

Note

翻译中…

The solution of the first test case is shown in the statement.

翻译中…

In the second test case, an optimal solution is to do the following:

翻译中…

image

翻译中…

In the third test case, all piles are already in their target state.

翻译中…

样例

Input #1
3
2
1 3 1 2
1 1 1 2
3
2 0 2 2
0 1 1 0
1 1 0 0
3
1 2 1 2
3 4 3 4
0 0 0 0
Output #1
2
3
0
InputCopy OutputCopy
题目信息
题号
2122B
时间限制
1 s
内存限制
256 MB
标签
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。