Picture

Charlotte went on holidays to Machu Picchu and took a picture that wants
to frame to hang it on the wall. Naturally, she wants a frame big enough
to contains her picture, but also wants that it is not bigger than
necessary. Specifically, she wants to minimize the area of a frame. The
picture as well as the frame are rectangles which dimensions are
described by two natural numbers. Write an algorithm that finds, given a
sequence of frames, the area of the smallest frame in which fits the
picture.

For instance, if the picture measures 7 × 11 and there are three frames
with dimensions 9 × 12, 6 × 15, and 13 × 8, Charlotte would choose the
last frame. The second one is too small, and the other frames the first
one is the biggest (9 * 12 = 108, compared with 13 * 8 = 104).

Input

Each case of the input starts with two natural numbers X ≤ 1000 and
Y ≤ 1000 describing the dimensions of the picture. Then, a number
N ≤ 1000 of frames in the shop follows, and N lines with two natural
numbers A ≤ 1000 and B ≤ 1000 in each one, describing the dimensions of
each frame. The input may contain various cases, separated between them
by a line in white; your program must detect when the cases finish.

Output

For each case, your program must print the area of the smallest frame in
which the picture fits. If it does not fit in any frame, it must print
−1.

Author: Anders Jonsson

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T10:18:02.809Z

© Jutge.org, 2006–2026.
https://jutge.org
