#P5917. Pack Rectangles

    ID: 19143 Type: Default 1000ms 256MiB

Pack Rectangles

Pack Rectangles

Four rectangles are given, each with sides parallel to the coordinate axes. You are allowed to rotate each rectangle (i.e. swap its width and height). Your task is to pack these four rectangles into a larger enclosing rectangle without any overlaps. The enclosing rectangle must also have its sides parallel to the coordinate axes. Among all such placements, we are interested only in those with the minimal possible area. There may be several different enclosing rectangles (with different dimensions) that achieve this minimal area; you must output all of them.

Let the dimensions of a rectangle be given by (w) and (h). For example, one possible configuration is to place all four rectangles in a row, yielding an enclosing rectangle of width (w_1+w_2+w_3+w_4) and height (\max(h_1, h_2, h_3, h_4)) (in (\LaTeX) that is: (\left(\sum_{i=1}^4 w_i,; \max_{1\le i\le4}{h_i}\right))). Other configurations (such as vertical stacking, forming two rows, or an L–shaped layout) must be considered.

Input consists of four lines, each containing two positive integers representing the width and height of a rectangle. Output the minimal possible area on the first line, followed by each possible enclosing rectangle's dimensions (with the smaller dimension first) on subsequent lines, sorted in increasing order of the first number.

inputFormat

The input consists of 4 lines. Each line contains two positive integers (w) and (h) (separated by a space) representing the width and height of a rectangle.

outputFormat

On the first line, output the minimal area achievable. Then for each enclosing rectangle that has this minimal area, output one line containing two integers: the smaller dimension and the larger dimension (separated by a space). The output rectangles must be sorted in increasing order of the first number.

sample

1 1
1 1
1 1
1 1
4

1 4 2 2

</p>