#1513. [POI2006]Tet-Tetris 3D

[POI2006]Tet-Tetris 3D

Statement

The authors of the game "Tetris" have decided to make a new, three-dimensional version, in which cuboids would fall down on a rectangular platform. The blocks fall down separately in a certain order, just like in the two-dimensional game. A block falls down until it reaches an obstacle: the platform or another block, that has already stopped - then it stops and remains in this exact position till the game is over.

However, the authors wanted to change the spirit of the game, turning it from a simple arcade-game into a play far more puzzling. Knowing the order of the falling blocks and their flight path the player's task is to tell the height of the highest point of the arrangement after all blocks have fallen down (and stopped). All the blocks are falling down vertically and do not rotate while falling. For convenience we'll introduce a cartesian coordinate system on the platform, with the center in one of the platform's corners and the axes parallel to the platform's edges.

Write a programme that automates verification of the player's answer.

TaskWrite a programme that:

reads the descriptions of subsequent falling blocks from the standard input,determines the height of the highest point of the arrangement of blocks after all have fallen down and stopped,writes the result to the standard output.

Input Format

In the first line of the input there are three integers DD, SS and NN (1N20 0001\le N\le 20\ 000, 1D,S1 0001\le D,S\le 1\ 000), separated by single spaces and denoting respectively: the length and the depth of the platform and the number of blocks that are going to fall down on it. In the following NN lines the descriptions of subsequent blocks are given, one in each line.

Each description of a block consists of five integers: dd,ss,ww,xx and yy (1d1\le d, 0x0\le x, d+xDd+x\le D, 1s1\le s, 0y0\le y, s+ySs+y\le S, 1w100 0001\le w\le 100\ 000), representing a block of length dd depth ss and height ww. This very block will be be falling down on the platform with its d×sd\times s face as the bottom, where the length and depth of the block are parallel to those of the platform. The coordinates of the vertices of the projection of the block on the platform are: (x,y)(x,y), (x+d,y)(x+d,y), (x,y+s)(x,y+s) and (x+d,y+s)(x+d,y+s).

Output Format

The first and only line of the standard output should contain exactly one integer, the height of the highest point of the arrangement of blocks after all have fallen down and stopped.

7 5 4
4 3 2 0 0
3 3 1 3 0
7 1 2 0 3
2 3 3 2 2
6

Constraints

1N2×1041 \leq N \leq 2 \times 10^41D,S1031 \leq D,S \leq 10^3di,si1d_i,s_i \geq 11wi1051 \leq w_i \leq 10^50xi,di+xiD0 \leq x_i,d_i+x_i \leq D0yi,si+yiS0 \leq y_i,s_i+y_i \leq S