Codeforces Round 982 (Div. 2) |
---|
Finished |
You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $$$n$$$ stamps. Each stamp is a rectangle of width $$$w_i$$$ and height $$$h_i$$$.
You will use each stamp exactly once to color a rectangle of the same size as the stamp on the grid in black. You cannot rotate the stamp, and for each cell, the stamp must either cover it fully or not cover it at all. You can use the stamp at any position on the grid, even if some or all of the cells covered by the stamping area are already black.
What is the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used?
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$).
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$w_i$$$ and $$$h_i$$$ ($$$1 \le w_i, h_i \le 100$$$).
For each test case, output a single integer — the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used.
551 52 43 34 25 132 21 11 213 23100 100100 100100 10041 42 31 53 2
20 8 10 400 16
In the first test case, the stamps can be used as shown on the left. Each stamp is highlighted in its own color for clarity.
After all these stamps are used, there is one black region (as shown on the right), and its perimeter is $$$20$$$. It can be shown that there is no way of using the stamps that yields a lower total perimeter.
In the second test case, the second and third stamps can be used entirely inside the first one, so the minimum perimeter is equal to $$$8$$$.
Name |
---|