Under construction

Still being built. Things may change

Gibonni

Time limit1sMemory limit1024 MB

Summary
Given up to six tiles with a notch in the top row, find the largest rectangle area that a subset of them can tile without gaps, with rotations allowed.
Level

Medium6 of 10

Topics
Backtracking, Brute force, Matrix
Solved
No attempts yet

Problem

At a spectacular concert in Zagreb's so-called fake Arena, Gibonni had his fan Marin fascinated by the shapes shown on the screens behind the musician.

Inspired by those shapes, Marin decided to end his programming career and become a potter. He wants to be a potter who uses only tiles that resemble the shapes from Gibonni's concert.

He already has nn such tiles. The ii-th tile is described by four natural numbers tit_i, wiw_i, hih_i, pip_i as follows:

  • The tile is made of unit squares. wiw_i is the number of columns and hih_i is the number of rows.
  • tit_i is either 1 or 2 and gives the tile type. In a tile of type ti=1t_i = 1, the top row is missing the square in column pip_i. In a tile of type ti=2t_i = 2, the top row is missing every square except the one in column pip_i. All other rows are complete.
1 3 4 32 3 4 3

Tiles can be rotated left and right, so each tile has at most four different orientations.

Illustration of all possible orientations for the tile described by 1 3 4 3

Marin wants to arrange his tiles into a rectangle of the largest possible area. He does not have to use all the tiles, but there must be no gaps between them. Find the area of that rectangle.

Input

The first line contains a natural number nn (1≤n≤61 ≤ n ≤ 6), the number of tiles.

Each of the next nn lines contains four natural numbers tit_i, wiw_i, hih_i, pip_i (1≤ti≤21 ≤ t_i ≤ 2, 1≤wi≤41 ≤ w_i ≤ 4, 2≤hi≤42 ≤ h_i ≤ 4, 2≤wi⋅hi≤122 ≤ w_i \cdot h_i ≤ 12, 1≤pi≤wi1 ≤ p_i ≤ w_i), describing the ii-th tile.

Output

Print one line with the requested area.

Hint

Explanation of the first test case: The illustration shows the tiles in input order and one way to form a rectangle of area 35. One tile is not used.

Explanation of the second test case:

Explanation of the third test case: The tiles from the statement cannot be arranged into a rectangle without gaps.

Examples3

  1. Example 1

    Input
    5
    2 3 4 3
    1 3 4 1
    1 2 4 1
    1 2 4 1
    1 3 4 3
    
    Expected output
    35
    
  2. Example 2

    Input
    3
    2 4 2 3
    2 3 2 2
    2 3 3 1
    
    Expected output
    16
    
  3. Example 3

    Input
    2
    1 3 4 3
    2 3 4 3
    
    Expected output
    0