#P1233. 木棍加工

木棍加工

Description

There are nn wooden sticks in total, and the length and width of each stick are known. The sticks can be processed by a machine one after another. Before processing a stick, the machine may need setup time. The setup time is defined as follows:

  • The setup time for the first stick is 11 minute.
  • If the just-processed stick has length ll and width ww, then for the next stick with length lil_i and width wiw_i, if llil \ge l_i and wwiw \ge w_i, no setup time is needed; otherwise, 11 minute of setup time is needed.

Compute the minimum total setup time needed to process all nn sticks. For example, if you have 55 sticks with lengths and widths (4,9),(5,2),(2,1),(3,5),(1,4)(4, 9), (5, 2), (2, 1), (3, 5), (1, 4), the minimum setup time is 22 (process in the order (4,9),(3,5),(1,4),(5,2),(2,1)(4, 9), (3, 5), (1, 4), (5, 2), (2, 1)).

Input Format

The first line contains an integer nn (n5000n \le 5000).

The second line contains 2n2n integers: l1,w1,l2,w2,,ln,wnl_1, w_1, l_2, w_2, \ldots, l_n, w_n. The values of ll and ww do not exceed 1000010000, and adjacent numbers are separated by spaces.

Output Format

A single line containing one integer: the minimum total setup time required.

5
4 9 5 2 2 1 3 5 1 4

2

Hint

For 100%100 \% of the testdata, 1n50001 \le n \le 5000, 1li,wi1041 \le l_i, w_i \le 10^4.

Translated by ChatGPT 5