#P5924. [IOI 2004] Phidias 菲迪亚斯神

[IOI 2004] Phidias 菲迪亚斯神

Description

For this statue, he needs rectangular marble slabs of sizes $W_1\times H_1, W_2\times H_2, \ldots, W_N \times H_N$.

Recently, Phidias obtained a rectangular marble block. He wants to cut it into the required sizes.

A slab, or any piece cut from it, can be cut all the way through by a vertical (or horizontal) straight cut into two rectangular slabs. The two resulting slabs must both have integer widths and heights.

The slab can only be cut in this way, and slabs cannot be glued together into a larger slab.

Because the slab has a pattern, it cannot be rotated.

If Phidias cuts out a slab of size A×BA\times B, then it cannot be used as a B×AB\times A slab unless A=BA = B. For each required slab size, Phidias may cut out zero or more slabs. After all cuts are finished, any produced slab whose size is not among the required sizes becomes waste.

Phidias wants to know how to cut the original slab so that the total waste area is minimized.

For example, in the figure below, the original slab has width 2121 and height 1111, and the required sizes are 10×410\times4, 6×26\times2, 7×57\times5, and 15×1015\times10. Then the minimum total waste area is 1010. The figure also shows a cutting plan that achieves the minimum waste area of 1010:

Your task is to write a program that, given the size of the original slab and the required slab sizes, computes the minimum total waste area.

Input Format

The first line contains two integers.

The first integer WW is the width of the original slab, and the second integer HH is the height of the original slab.

The second line contains an integer NN, the number of required slab types. The next NN lines give the sizes of the required slabs.

Each line contains two integers: the first is the required slab width WiW_i, and the second is the required slab height HiH_i.

Output Format

Output one line containing exactly one integer, the minimum total waste area.

21 11
4
10 4
6 2
7 5
15 10
10

Hint

Constraints: for 100%100\% of the testdata, 1W,H6001\le W, H\le 600, 0N2000\le N\le 200, 1WiW1 \le W_i \le W, 1HiH1 \le H_i \le H.

Translated by ChatGPT 5