Codeforces Round 831 (Div. 1 + Div. 2)
B
Jumbo Extra Cheese 2
Codeforces
800
原题
← AC →
翻译中…

Pak Chanek has $n$ two-dimensional slices of cheese. The $i$-th slice of cheese can be represented as a rectangle of dimensions $a_i \times b_i$. We want to arrange them on the two-dimensional plane such that:

翻译中…
  • Each edge of each cheese is parallel to either the x-axis or the y-axis.
  • The bottom edge of each cheese is a segment of the x-axis.
  • No two slices of cheese overlap, but their sides can touch.
  • They form one connected shape.
翻译中…

Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.

翻译中…

Find the minimum possible perimeter of the constructed shape.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. The following lines contain the description of each test case.

翻译中…

The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of slices of cheese Pak Chanek has.

翻译中…

The $i$-th of the next $n$ lines of each test case contains two integers $a_i$ and $b_i$ ($1 \leq a_i,b_i \leq 10^9$) — the dimensions of the $i$-th slice of cheese.

翻译中…

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 a line containing an integer representing the minimum possible perimeter of the constructed shape.

翻译中…

Note

翻译中…

In the first test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.

翻译中…

image

翻译中…

We can calculate that the perimeter of the constructed shape is $2+5+1+1+1+1+3+1+5+1+2+3=26$. It can be shown that we cannot get a smaller perimeter.

翻译中…

Consider the following invalid arrangement.

翻译中…

image

翻译中…

Even though the perimeter of the shape above is $24$, it does not satisfy all conditions of the problem. The bottom edge of the $1 \times 1$ slice of cheese is not a segment of the x-axis.

翻译中…

In the second test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.

翻译中…

image

翻译中…

We can calculate that the perimeter of the constructed shape is $2+2+2+3+2+3+2+2+2+4=24$. It can be shown that we cannot get a smaller perimeter.

翻译中…

样例

Input #1
3
4
4 1
4 5
1 1
2 3
3
2 4
2 6
2 3
1
2 65
Output #1
26
24
134
InputCopy OutputCopy
题目信息
题号
1740B
时间限制
1 s
内存限制
256 MB
标签
计算几何
贪心
排序

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