IOI '04 P4 - Phidias

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 16M

Problem type
IOI '04 - Athens, Greece

Famous ancient Greek sculptor Phidias is making preparations to build another marvelous monument. For this purpose he needs rectangular marble plates of sizes W_1 \times H_1, W_2 \times H_2, \dots, W_N \times H_N.

Recently, Phidias has received a large rectangular marble slab. He wants to cut the slab to obtain plates of the desired sizes. Any piece of marble (the slab or the plates cut from it) can be cut either horizontally or vertically into two rectangular plates with integral widths and heights, cutting completely through that piece. This is the only way to cut pieces and pieces cannot be joined together. Since the marble has a pattern on it, the plates cannot be rotated: if Phidias cuts a plate of size A \times B then it cannot be used as a plate of size B \times A unless A = B. He can make zero or more plates of each desired size. A marble plate is wasted if it is not of any of the desired sizes after all cuts are completed. Phidias wonders how to cut the initial slab so that as little of it as possible will be wasted.

As an example, assume that in the figure below the width of the original slab is 21 and the height of the original slab is 11, and the desired plate sizes are 10 \times 4, 6 \times 2, 7 \times 5, and 15 \times 10. The minimum possible area wasted is 10, and the figure shows one sequence of cuts with total waste area of size 10.

Your task is to write a program that, given the size of the original slab and the desired plate sizes, calculates the minimum total area of the original slab that must be wasted.

Input Specification

The first line of input contains two integers: first W (1 \le W \le 600), the width of the original slab, and then H (1 \le H \le 600), the height of the original slab. The second line contains one integer N (0 < N \le 200): the number of desired plate sizes. The following N lines contain the desired plate sizes. Each of these lines contains two integers: first the width W_i (1 \le W_i \le W) and then the height H_i (1 \le H_i \le H) of that desired plate size.

Output Specification

The output should contain one line with a single integer: the minimum total area of the original slab that must be wasted.

Sample Input

21 11
4
10 4
6 2
7 5
15 10

Sample Output

10

Note: in 50\% of the inputs, W \le 20, H \le 20, and N \le 5.


Comments

There are no comments at the moment.