#P2900. [USACO08MAR] Land Acquisition G

[USACO08MAR] Land Acquisition G

Description

Farmer John is planning to expand his farm and is considering buying NN rectangular plots.

If FJ buys a single plot, the price equals the area of the plot. But he can choose to acquire a group of plots together, where the price of the group is the maximum length times the maximum width among those plots. For example, if FJ acquires a 3×53 \times 5 plot and a 5×35 \times 3 plot together, he only pays 5×5=255 \times 5=25, which is cheaper than buying them separately.

FJ wants to buy all the plots. He found that partitioning the plots into different groups can save money. Given the dimensions of each plot, please help him compute the minimum total cost to purchase all plots.

Input Format

The first line contains an integer NN (1N5×1041 \leq N \leq 5 \times 10^4).

Each of the next NN lines contains two integers wiw_i and lil_i, denoting the length and the width of the ii-th plot. It is guaranteed that both the length and the width do not exceed 10610^6.

Output Format

Output the minimum cost to buy all the plots.

4 
100 1 
15 15 
20 5 
1 100 

500 

Hint

Divide all plots into three groups:

  • The first plot is in the first group, costing 100×1=100100 \times 1=100.
  • The second and third plots are in the second group, costing 20×15=30020 \times 15=300.
  • The fourth plot is in the third group, costing 1×100=1001 \times 100=100.

The total cost is 500500, and it can be proven that no better plan exists.

Translated by ChatGPT 5