We need to parse problem: We have tetrominos counts. We want to join K tetrominos to form a rectangle 2 tall and 2K wide. So the rectangle is 2 rows, 2K columns. Each tetromino covers 4 squares. Rules: Each tetromino can be rotated (but not reflected). They must fit exactly in the rectangle, covering each square exactly once, no overhang.
We need to maximize K given limited pieces.
We need to consider which tetrominos can be placed in a 2-row high rectangle? Since rectangle height is 2, any tetromino placed must fit within that height. Tetrominos are shapes of 4 squares. They can be rotated to fit a height of 2. Let's examine each type under rotation.
List all tetromino shapes (with rotations) that can fit within a bounding box of height <=2. Since the rectangle is exactly 2 tall, any tetromino placed must not exceed row 1 or 2. So we need placements where the shape's vertical extent is exactly within rows 1 and 2. Many tetrominos can be placed horizontally or vertically.
But note: The rectangle is a contiguous 2x(2K) region. Tetrominos are placed to exactly cover all squares without overlap, like a tiling of a 2x(2K) board with tetrominos? But tetrominos cover 4 squares each, and the board total area is 4K squares. So it's a tiling of a 2x(2K) board using tetrominos from given set (with allowed rotations). So we need to find maximum K such that we can tile a 2-by-2K rectangle using the given numbers of tetromino shapes, respecting counts.
This is a combinatorial tiling problem. Classic: Tiling a 2xn board with tetrominos? Actually 2x(2K) is 2 rows, even number of columns. The area is multiple of 4, which matches tetrominos. Could we tile it with I, O, T, J, L, S, Z? But note: S and Z tetrominos are the ones that can form interlocking shapes. However, 2xn board is narrow (height 2). Let's examine which tetrominos can be placed in a 2-high board without exceeding height.
-
I-tetromino: can be placed as a vertical 4x1, but that would be height 4 >2, not allowed. But we can rotate to horizontal: 1x4, which fits in height 1, so can be placed within a 2-high board if we place it covering one row? Actually height of horizontal I is 1, so it can be placed covering either row 1 or row 2, but we need to cover both rows? Not necessarily each piece individually can be placed in either row? But the whole tiling must cover all squares exactly. So we can place a horizontal I piece covering 4 squares in a single row. That would leave some squares in the other row uncovered. That's okay because other pieces can cover them. However, the I piece placed horizontally occupies 4 contiguous columns, but only one row. That's allowed as long as it doesn't go outside the rectangle (it stays within the 2 rows, since row 1 or 2 is within). So I horizontal is allowed.
-
O-tetromino: 2x2 square. That fits perfectly in a 2-high board, covering both rows for two columns.
-
T-tetromino: shape: three in a row and one below the middle. Rotations: We need to see if it can be placed in 2-high. The T shape: original orientation: row1: three squares, row2: one square under the middle. That's height 2, width 3. So fits. Rotate 90°: shape becomes vertical: row1: one square (top of T), row2: two squares? Actually let's define coordinates: T tetromino shapes: There are 4 orientations. In all orientations, the bounding box is either 2x3 or 3x2. The 3x2 orientation: height 3, width 2, which exceeds height 2? Height 3 >2, so cannot fit in 2-high board. So only the orientations with bounding box height 2 are allowed. So T can be placed in two orientations: the standard and upside-down? Actually T standard (pointing down) uses rows: row1 has squares at columns 1,2,3; row2 has square at column 2. That's height 2. Upside-down T (pointing up) would have row1: square at column 2; row2: squares at columns 1,2,3. Also height 2. So both are allowed. Also T rotated 90° left or right give bounding box 3x2, height 3, not allowed. So only two orientations for T: point down and point up. So we can use T in those orientations.
-
J tetromino: shape like L but with a hook. The J shape: 3 squares in a row with one attached below the leftmost. So bounding box 2x3. Rotations: There are four orientations. The ones with bounding box 2x3: orientation where the long part is horizontal and the extra square is below left or below right? Actually J: original orientation: row1: squares at columns 1,2,3; row2: square at column 1. That's 2x3. Another orientation: rotate 90°: shape becomes vertical: row1: squares at columns 1,2? Let's list properly. J tetromino is like L but reflected. Actually standard tetromino shapes: J is mirror of L. L: three in a row with one below the rightmost. J: three in a row with one below the leftmost. So both have a 2x3 orientation (with the extra square in row2 at left or right). Also orientations where the shape is 3x2: e.g., L pointing up: row1: two squares? Let's see: L rotated 90° clockwise: shape: column1: squares at rows 1,2,3; column2: square at row3? Actually that would be height 3, width 2, so bounding box 3x2, height 3 >2, not allowed. So only the orientations that are 2 rows tall are allowed: those where the long bar is horizontal. For J, that's two orientations: one with the extra square below left, and one with the extra square above left? Wait if we rotate J 180°, we get a shape: row1: square at column 1? Actually let's systematically consider: J shape (0,0),(1,0),(2,0),(0,1) (coordinates: x from 0 to 2, y=0 for the bar; extra at (0,1)). Rotations:
-
0°: rows: y=0: columns 0,1,2; y=1: column0. height 2 (y from 0 to1). width 3.
-
90° rotation clockwise: around center? (x,y) -> (y, -x) maybe. Result shape: (0,0)->(0,0); (1,0)->(0,-1); (2,0)->(0,-2); (0,1)->(1,-0)?? Better to think: after 90° rotation, shape becomes: three vertical and one attached to bottom right? Actually typical J after 90° CW: shape: column0: squares at rows 0,1,2; column1: square at row2. That's width 2, height 3 (rows 0 to 2). So height 3 not allowed.
-
180°: shape: (0,0)->( -0, -0)? Actually 180° rotation: (x,y)->(-x,-y). Then shift to positive coordinates. The shape becomes: originally (0,0),(1,0),(2,0),(0,1). After invert signs: (0,0),(-1,0),(-2,0),(0,-1). To bring to nonnegative, we add offsets. Typically the shape: row0: (0,0) maybe? Actually the shape would be: three in a row but now at top row? Let's just realize that 180° rotated J is effectively same as 0° but reflected? Actually J rotated 180° gives shape: row0: square at column 0? Let's do coordinates: place so that bounding box is minimal. Starting shape coordinates: (0,0),(1,0),(2,0),(0,1). After 180°, we get (0,0),(-1,0),(-2,0),(0,-1). Add (2,1) to make nonnegative: (2,1),(1,1),(0,1),(2,0). So row1 (y=1): columns 0,1,2? Actually coordinates: (0,1),(1,1),(2,1) from ( -2+2=0? Wait recalc: coordinates after transformation: (0,0) -> (0,0) -> add offset? I'm mixing. Simpler: The J tetromino is chiral; its rotations produce shapes that are 2x3 or 3x2. The two orientations that are 2x3 are the ones where the long bar is horizontal. For J, that includes the original orientation (extra below left) and the 180° orientation (extra above right? Actually after 180°, the shape would be: three squares in a row, with the extra square above the rightmost? Let's check: Starting from J shape: horizontal bar of three at bottom, extra at left bottom? Actually my initial: J has extra below leftmost. But if we rotate 180°, we get a shape that is the mirror? It becomes like L? Actually 180° rotation of J yields a shape with horizontal bar of three on top and extra below right? That would be L? Let's not get confused. But the key: the J shape has two orientations that are 2 rows tall: one with the bar on top and the extra at bottom left? Or bottom right? We'll figure out later. Anyway, there are exactly two orientations that fit within 2 rows: those where the long bar is horizontal. For both J and L, similar. For S and Z, also there are orientations: S shape: two squares top left and middle? Actually S tetromino: shape like a zigzag: two squares horizontally, then two squares offset to the right below. In its flat orientation, it's 2x3? Let's examine: S standard orientation: rows: top row: squares at columns 1,2; bottom row: squares at columns 0,1. That's width 3, height 2. So fits. Rotated 90°, it becomes vertical: height 4? Actually S rotated 90° becomes shape: column1: squares at rows 0,1; column0: squares at rows 1,2? Actually that would be 3x2? Let's compute: S coordinates: (1,0),(2,0),(0,1),(1,1). Bounding box: x from 0 to2, y from 0 to1 -> 2x3. Rotating 90° yields (0,1),(0,2),( -1,0?) Actually easier: Standard S orientation fits within 2 rows. The rotated orientation (vertical) would have bounding box 3x2 (height 3, width 2). So not allowed. Similarly Z has a flat orientation of width 3 height 2. So for S and Z, only flat orientation fits.
Thus, the allowed placements for each piece are those with bounding box height exactly 2. For I, that's only the horizontal orientation (1 row) but can be placed either in top or bottom row, effectively it occupies only one row. So I can be placed covering 4 contiguous cells in a single row.
Now, we need to tile a 2x(2K) board with these shapes. Since the board is only 2 rows high, the tiling is essentially a tiling of a 2-row board with domino-like shapes of width up to 3? Actually the shapes are polyominoes covering 4 squares.
We can consider that the board is a 2-row grid. Each tetromino placement can be represented as a set of cells. The tiling must cover all cells. This is reminiscent of tiling a 2xn board with trominoes? But here pieces are tetrominoes of 4 squares, but area matches.
One approach: Because board height is only 2, the tiling constraints can be simplified by considering columns. Since pieces can span multiple columns, we need to cover columns 1 to 2K. Let's denote columns indexed 1..2K.
We need to partition the board into pieces. Each piece covers 4 cells. Some pieces cover cells in both rows across a width of 2 (O, and maybe others) or width 3 (T, J, L, S, Z) or width 4 (I horizontal). Some pieces (I horizontal) cover 4 cells in a single row, leaving gaps in the other row that must be covered by other pieces. This suggests we need to pair I pieces with other pieces to fill both rows.
This is a combinatorial optimization problem with potentially large counts (up to 1e9). We need to compute maximum K that can be achieved. Since counts are huge, the solution likely involves a formula based on counts, maybe a linear programming or greedy.
Observation: The board is 2x(2K) = area 4K. Each piece covers 4 squares. So the total number of pieces used is exactly K. So we need to select K pieces from the inventory to tile the board.
Given the board shape, the tiling must satisfy certain parity or pattern constraints.
Let's attempt to characterize tilings of a 2xn board with these tetrominos when n is even? Actually 2x(2K) is a specific width, even. We could think of tiling by columns: each column pair? Maybe we can tile using combinations of "blocks" of width 2 or 3 that cover both rows fully.
Alternatively, we could consider that each tetromino either covers both rows (like O, T, J, L, S, Z) or covers only one row (I horizontal). However, note that T, J, L, S, Z in allowed orientations cover both rows but not necessarily fully covering the columns they span. They have holes? For example, T covers three squares in one row and one square in the other row, leaving two squares in that column? Actually in a 2-row board, each column has two cells: top and bottom. When we place a T in allowed orientation (pointing down), it covers top row cells at columns c, c+1, c+2 (three cells) and bottom row cell at column c+1 (one cell). So in columns c and c+2, only top row is covered; column c+1 both rows covered. So there are two uncovered cells: bottom row at column c and c+2. Those must be covered by other pieces. So T introduces "holes" that must be filled.
Similarly, J (original orientation) covers top row columns c, c+1, c+2 and bottom row column c. So top row three cells covered, bottom row one cell at column c covered. So holes: bottom row columns c+1 and c+2. L (mirror) covers top row columns c, c+1, c+2 and bottom row column c+2. Holes: bottom row columns c and c+1. S and Z: S covers top row columns c+1,c+2, bottom row columns c,c+1. That leaves top row column c and bottom row column c+2 uncovered? Actually S: coordinates: (c+1,top), (c+2,top), (c,bottom), (c+1,bottom). So uncovered: top row column c, bottom row column c+2. Z: (c,top),(c+1,top),(c+1,bottom),(c+2,bottom). Uncovered: top row column c+2, bottom row column c. O covers both rows columns c,c+1 fully: covers all 4 cells. I horizontal placed on top row covers top row columns c..c+3; bottom row none in those columns. If placed on bottom row, covers bottom row columns c..c+3; top row none.
Thus, the tiling problem can be seen as covering a 2xN board (N=2K) with these "blocks" that may leave some cells uncovered that must be covered by other blocks overlapping? Actually they cannot overlap; they are placed with no overlap. So each block occupies certain cells. The holes are just cells not covered by that block, but they will be covered by other blocks placed adjacent or overlapping? Since blocks cannot overlap, they must be placed in disjoint sets of cells. So the uncovered cells after placing a block are covered by other blocks placed elsewhere, but those blocks must fit into those gaps, possibly spanning across columns.
So it's a classic tiling by polyominoes. Because the board is only 2 high, we might be able to reduce to a 1D state representing which cells in the current column are covered as we sweep left to right. This is like a DP with state representing the pattern of coverage in the two rows for the next column. But with huge counts, maybe there's a simpler combinatorial structure.
Given the piece counts are huge, the maximum K may be limited by total number of pieces and also by constraints on parity of I pieces etc. Since K = number of pieces used, we want to maximize K subject to tiling possibility and piece counts. But note that the total number of pieces available is at most sum a_i. So K <= sum a_i. However, not all pieces may be usable because of tiling constraints.
Given large numbers, the solution probably involves using as many pieces as possible with some optimal combination. Perhaps we can always tile a sufficiently long 2xN board with a mix of these pieces as long as certain counts ratios are satisfied. Or maybe the only problematic pieces are those that create imbalance between rows.
Let's try to think in terms of row coverage totals. The total number of cells in top row = 2K, bottom row also = 2K. Each piece contributes some number of cells to top row and bottom row. Let's denote for each piece type, the number of cells it covers in the top row when placed (but orientation might change which row is top). However, since we can rotate pieces (but not reflect), the assignment of which row is top matters. For I horizontal, we can place it on top row or bottom row. For O, it covers both rows equally. For T, J, L, S, Z, in allowed orientations, the shape is asymmetric: there is a top row and bottom row. But we can also rotate 180°, which swaps top and bottom? Actually for T, the two allowed orientations are point down and point up. In point down, top row has 3 cells, bottom row has 1. In point up, top row has 1, bottom row has 3. So we can choose which row gets 3 and which gets 1. Similarly for J: allowed orientations: one where the extra square is below left (so top row has 3, bottom row has 1) and one where the extra square is above left? Let's enumerate carefully:
Define coordinates: rows: 1 (top), 2 (bottom). We can place a tetromino anywhere such that all squares are within rows 1 and 2.
For each type, list the possible placements (including rotations) that keep the piece within rows 1 and 2. Since rows are only two, the piece cannot extend vertically beyond these rows. So any orientation that results in height 3 is not allowed. So allowed orientations are those where the piece's bounding box height is <=2. Since piece covers 4 squares, the minimum height is 1 (for I horizontal) and maximum height is 2 for many. But note that a piece that has height 2 might also be placed such that it occupies both rows, but we could also shift it vertically? Actually the board has exactly rows 1 and 2; we cannot shift the piece to occupy rows 0 and 1 because that would be outside. So the piece must be placed entirely within rows 1 and 2. That means if the piece's height is 2, it must occupy both rows exactly. If the piece's height is 1 (I horizontal), it can be placed either on row 1 or row 2. So for pieces with height 2, they always cover both rows but not necessarily fully.
Thus, for height-2 pieces (O, T, J, L, S, Z), they always cover exactly 4 cells across both rows, but distribution between rows can vary depending on orientation. For O, it covers 2 cells in each row. For T, J, L, S, Z, the distribution can be either (3 top, 1 bottom) or (1 top, 3 bottom) depending on flipping (180° rotation). However, for S and Z, does flipping change distribution? S: standard orientation: top row has 2 cells (at columns 2 and 3 if placed at columns 1-3? Actually we need to check: S shape when placed with bounding box 2x3, there are two possibilities: S shape and its 180° rotation. Are both allowed? Let's compute S coordinates: standard S: squares at (1,0),(2,0),(0,1),(1,1) if we use origin at top-left? But we can rotate 180°: S rotated 180° yields the same shape? Actually S is symmetric under 180° rotation: S rotated 180° becomes S again (just shifted). Check: S shape: squares (0,1),(1,1),(1,0),(2,0) after 180° becomes (0,-1),(-1,-1),(-1,0),(-2,0) -> after translation, we get (2,1),(1,1),(1,0),(0,0) which is essentially the same shape but mirrored? Actually S and Z are mirrors of each other, but S rotated 180° gives the same shape? I recall that S tetromino is chiral but has rotation symmetry of 180°. Actually typical tetrominoes: S and Z are each other's mirror; S rotated 180° yields S (it's symmetric). Let's verify: The S tetromino in standard orientation looks like:
[ ][ ]
[ ][ ]
If you rotate 180°, you get the same shape but shifted. Actually imagine the shape: blocks at (1,0),(2,0),(0,1),(1,1). Rotate 180° about its center (maybe at (1,0.5)?) yields (1,1),(0,1),(2,0),(1,0) which is the same set? Actually (1,1) and (0,1) and (2,0),(1,0) - that's exactly the same set? Wait after 180° rotation, the shape becomes (1,1),(0,1),(2,0),(1,0). That's the same as the original if we reorder? Original set: (1,0),(2,0),(0,1),(1,1). Yes, it's the same set: both contain (0,1),(1,1),(1,0),(2,0). So indeed S is invariant under 180° rotation. So S has only two distinct orientations? Actually S has 2 orientations: flat (2x3) and vertical (3x2). The flat orientation is symmetric under 180°, so flipping upside down yields the same shape (just shifted). However, if we place S on a 2-row board, the flat orientation's distribution between rows: it covers two cells in top row and two cells in bottom row? Let's compute: With coordinates: top row y=0, bottom row y=1. S shape: squares at (c+1, top), (c+2, top), (c, bottom), (c+1, bottom). So top row: 2 squares; bottom row: 2 squares. That is symmetric: 2 top, 2 bottom. So S always covers 2 top, 2 bottom regardless of orientation? But what about the other flat orientation? Since 180° rotation gives same shape, there is no orientation that yields 3 top,1 bottom. So S always covers exactly 2 cells in each row. Similarly, Z also has 2 and 2? Z shape: (c, top),(c+1, top),(c+1, bottom),(c+2, bottom). So also 2 top, 2 bottom. So S and Z are balanced.
Now J and L: Are they symmetric under 180°? J and L are mirrors; each is not symmetric under 180°? Actually L rotated 180° yields an L shape that is rotated, but is it the same as original? Standard L: squares (0,0),(1,0),(2,0),(2,1). Rotate 180°: becomes (0,1),(-1,1),(-2,1),(-2,0) -> after shift: (2,0),(1,0),(0,0),(0,1). That set is (0,1),(0,0),(1,0),(2,0). That's actually a J shape? Let's see: original L: long bar at top with extra at bottom right. After 180°, we get long bar at bottom with extra at top left. That is still an L? Actually shape: squares at (0,1) (top row leftmost), (0,0) (bottom row leftmost), (1,0), (2,0). That is not an L shape? Visualizing: bottom row: columns 0,1,2 all filled? Actually bottom row: columns 0,1,2? Wait squares: (0,0),(1,0),(2,0) -> three in a row on bottom row; and (0,1) is top row column 0. That's a J shape (extra at top left above the leftmost of the bar). So L rotated 180° becomes J? No, L and J are mirror images, but rotation of L yields a shape that is like a backwards L? Actually in tetrominoes, L and J are distinct: L has the extra attached to the end; J has extra attached to the other end. Under 180° rotation, L becomes L again? Let's check properly. L tetromino: coordinates (0,0), (1,0), (2,0), (2,1). Rotate 180° about its center (maybe (1,0.5)): (0,0)->(2,1), (1,0)->(1,1), (2,0)->(0,1), (2,1)->(0,0). So resulting set: (0,0),(0,1),(1,1),(2,1). That is a shape: top row: (0,1),(1,1),(2,1) three in a row; bottom row: (0,0). That's a J shape (extra at bottom left). Wait J shape is (0,0),(1,0),(2,0),(0,1) or (0,0),(1,0),(2,0),(2,1)? Actually J shape: In my earlier, I defined J as three in a row with one below leftmost: (0,0),(1,0),(2,0),(0,1). That matches (0,0) bottom left? Actually (0,0) could be bottom row, (0,1) top row? Let's fix orientation: Let's use grid with rows increasing downward (row 1 top, row 2 bottom). But here I'm using y=0 top, y=1 bottom. For L: typically L shape is three in a row on top and one below rightmost: (0,0),(1,0),(2,0),(2,1). For J: three in a row on top and one below leftmost: (0,0),(1,0),(2,0),(0,1). So indeed L rotated 180° yields (0,1),(1,1),(2,1),(0,0) -> three on bottom row (y=1) and one on top row at column 0. That's J shape? Actually J shape could also be three on bottom and one on top left? But typical J shape orientation: three on bottom and one on top left is also J? Let's see: J shapes: there are four orientations: 0°: three on top, one below left; 90°: three on left, one above right; 180°: three on bottom, one above right; 270°: three on right, one below left. So three on bottom and one on top left is actually J orientation? 180° J is three on bottom, one above right? Actually let's not get lost. The key: L and J are different pieces, and they are not the same under rotation. But in the set of pieces we have, we have both J and L counts separately. So for L tetromino, allowed orientations within 2 rows: The orientation where the long bar is horizontal and the extra is below right: that's 3 top, 1 bottom? Actually if the long bar is on top row and extra below right, then top row gets 3, bottom row gets 1. The orientation where the long bar is horizontal and extra is above left? Can we rotate L 180° to get an orientation that fits within 2 rows? Starting from L shape that is 2x3, rotate 180° yields a shape that is also 2x3? Check L shape: bounding box 2x3. Rotating 180° yields shape that also has bounding box 2x3. So that orientation is allowed. That orientation would have three on bottom row and one on top row at the rightmost? Let's derive: L original: (c,top),(c+1,top),(c+2,top),(c+2,bottom). After 180° rotation and appropriate translation, we get: (c, bottom),(c+1,bottom),(c+2,bottom),(c, top). So it becomes three on bottom, one on top at leftmost. That's a different distribution: bottom gets 3, top gets 1. So L can be placed with either 3 top/1 bottom (original) or 1 top/3 bottom (rotated 180°). So L has both distributions.
Similarly J: original: (c,top),(c+1,top),(c+2,top),(c,bottom) -> 3 top,1 bottom. Rotated 180°: (c, bottom),(c+1,bottom),(c+2,bottom),(c+2,top) -> 1 top,3 bottom? Actually check: J original: extra below left. After 180°: three on bottom, extra above right? That would be (c, bottom? Let's compute: J shape: (c,top),(c+1,top),(c+2,top),(c,bottom). Rotate 180°: (c,top) -> (c+2,bottom), (c+1,top)->(c+1,bottom), (c+2,top)->(c,bottom), (c,bottom)->(c+2,top). After shifting to start at c, we get: top: column c+2; bottom: columns c, c+1, c+2. So top row has 1 (col c+2), bottom row has 3 (c, c+1, c+2). So yes, J can also be placed with either distribution.
T: original (point down): top row 3 (c,c+1,c+2), bottom row 1 (c+1) -> 3 top,1 bottom. Rotated 180° (point up): top row 1 (c+1), bottom row 3 (c,c+1,c+2) -> 1 top,3 bottom.
O: always 2 top,2 bottom. S and Z: always 2 top,2 bottom.
I horizontal: can be placed on top row only (4 top,0 bottom) or bottom row only (0 top,4 bottom). So I can give distribution (4,0) or (0,4).
Thus, each piece type can contribute a certain vector (top_cells, bottom_cells) modulo possible choices.
Now, the total number of top cells used must equal total bottom cells = 2K. So if we sum the top cells from all pieces, we must get exactly 2K, and bottom cells sum to 2K.
Let x_i be number of pieces of type i used (i in {I, O, T, J, L, S, Z}). For I pieces, we decide how many placed on top (I_top) and bottom (I_bottom) with I_top+I_bottom = x_I. For T, J, L, we decide how many placed in orientation giving 3 top (call T_3top) and 1 top (T_1top) etc. For O, S, Z, each gives (2,2) fixed.
So total top cells = 4I_top + 0I_bottom + 2x_O + (3T_3top + 1T_1top) + (3J_3top + 1J_1top) + (3L_3top + 1L_1top) + 2x_S + 2*x_Z = 2K.
Similarly bottom cells = 0I_top + 4I_bottom + 2x_O + (1T_3top + 3T_1top) + (1J_3top + 3J_1top) + (1L_3top + 3L_1top) + 2x_S + 2*x_Z = 2K.
We also have x_I = I_top + I_bottom, etc.
The sum of top and bottom cells both equal 2K, and total cells = 4K = 4 * (x_I + x_O + x_T + x_J + x_L + x_S + x_Z) because each piece contributes 4 cells. Indeed total cells = sum over pieces of 4 = 4 * K, so K = total pieces used.
But we also have that the total top cells + bottom cells = 4K, which is automatically satisfied if each piece contributes 4 total.
Now, subtracting the two equations: top - bottom = 0. So we get a balance condition:
(4I_top - 4I_bottom) + ( (3T_3top+1T_1top) - (1T_3top+3T_1top) ) + ... = 0.
Simplify: For I: 4*(I_top - I_bottom). For T: (3T_3top + 1T_1top) - (1T_3top+3T_1top) = 2T_3top - 2T_1top = 2*(T_3top - T_1top). Similarly for J: 2*(J_3top - J_1top). For L: 2*(L_3top - L_1top). For O, S, Z: (2-2)=0.
Thus, balance condition: 4*(I_top - I_bottom) + 2*(ΔT + ΔJ + ΔL) = 0, where ΔT = T_3top - T_1top etc.
Divide by 2: 2*(I_top - I_bottom) + (ΔT + ΔJ + ΔL) = 0. So (ΔT+ΔJ+ΔL) = -2*(I_top - I_bottom).
Thus, the net imbalance from T,J,L must compensate twice the imbalance from I placements.
Now, note that for each T piece, the difference Δ_i (top minus bottom) can be either +2 (if placed 3 top/1 bottom) or -2 (if placed 1 top/3 bottom). So each T contributes either +2 or -2 to the sum ΔT+ΔJ+ΔL. Similarly J and L. So sum S = Σ over T,J,L of ±2. So S is an even integer, multiple of 2.
Then we have S = -2*(I_top - I_bottom) => I_top - I_bottom = -S/2. Since S is even, I_top - I_bottom is an integer.
Given I_top + I_bottom = x_I, we can compute possible differences. I_top - I_bottom can range from -x_I to x_I with same parity as x_I (since sum and difference have same parity: I_top = (x_I + d)/2, I_bottom = (x_I - d)/2, so d must have same parity as x_I). And d = I_top - I_bottom.
Also S = Σ ±2, so S/2 is an integer between - (x_T+x_J+x_L) and +(x_T+x_J+x_L) with same parity as sum? Actually each ±1 after dividing by 2? Let's define for each T, we choose orientation variable t_i = +1 for 3top, -1 for 1top. Then contribution to S/2 is t_i. So sum over T,J,L of t_i = - (I_top - I_bottom). So we need to be able to assign signs to T,J,L pieces such that their total equals -d.
Thus, a necessary condition for a tiling is that |d| ≤ x_T + x_J + x_L and d has same parity as (x_T + x_J + x_L?) Actually sum of t_i can be any integer between -N and N with same parity as N? Since each t_i is ±1, sum of N variables can achieve any integer with parity same as N (i.e., sum ≡ N mod 2). Because sum mod 2 = N mod 2 if all are ±1? Actually each t_i ≡ 1 mod 2? Wait ±1 mod 2 = 1 mod 2? In mod 2, -1 ≡ 1 mod 2 because -1 mod 2 = 1. So each t_i ≡ 1 (mod 2). So sum of N such numbers ≡ N (mod 2). So possible sums S' = Σ t_i have parity = N mod 2. And range is -N ≤ S' ≤ N with steps of 2. So indeed S' can be any integer with same parity as N and |S'| ≤ N.
Thus we require that -d = S' must satisfy that |d| ≤ N and d ≡ N (mod 2)? Actually S' = -d, so d must satisfy |d| ≤ N and d ≡ N (mod 2) (since S' ≡ N mod 2). But note d = I_top - I_bottom, and I_top+I_bottom = x_I. d and x_I have same parity. So we have parity constraints linking x_I and N = x_T+x_J+x_L. Not sure if this is also sufficient along with other constraints? Probably not enough because there are also placement constraints about how pieces fit together geometrically, not just row balance.
We need to find maximum K such that we can select K pieces from available counts to tile a 2x(2K) board. This is like a packing problem with specific shapes. But maybe due to the board being only 2 rows high, the tiling can be broken into patterns of width 2 or 3 that are independent. We might be able to characterize all possible tilings as combinations of small "blocks" that cover a certain number of columns, and then we need to count how many of each block can be formed from pieces.
Perhaps we can think of covering the board by "domino-like" patterns of width 2? But tetrominos often cover 3 columns. We need to cover an even number of columns 2K. The O piece covers 2 columns. I horizontal covers 4 columns. Others cover 3 columns but with specific hole patterns. The holes must be filled by other pieces that interlock. Since the board is only 2 rows, the tiling essentially corresponds to a word over some tiles that fit together.
We could attempt to model the tiling as a 1D "state machine" where we process columns from left to right, maintaining which cells in the current column are already covered by a piece that started earlier. At each step, we place a piece that starts at the current leftmost uncovered column. This is typical for tiling with polyominoes. Since the board is only 2 rows, the state can be represented by a bitmask of height 2 for the column we are at, indicating whether the top or bottom cell is already filled by a piece that extends from left. Since all pieces have width at most 4, we can define placements that start at a given column and cover certain pattern.
Alternatively, we could think in terms of covering the board with "blocks" that are closed: i.e., sets of pieces that together perfectly cover a contiguous set of columns with no dangling cells. Because the board is simply connected, we can break the tiling into independent "modules" that tile a segment of columns completely, and these modules can be concatenated. Since the board has width 2K, we can try to decompose the tiling into segments that use certain combinations of pieces.
I recall a known problem: AtCoder ABC (maybe ABC274? Actually this is from AtCoder "Tetromino Tiling"? I've seen a problem about tiling a 2xN board with tetrominos, with given counts, maximize N. I think it's from AtCoder ABC 139 F? Not sure. But I've encountered a problem: "Tetromino Tiling" from AtCoder Regular Contest 098? There is a problem "Tetromino Tiling" where you have counts of tetrominos and you want to maximize the number of cells in a rectangle? Actually I recall a problem: "AtCoder ABC 211 D"? Hmm.
Let's search memory: There's a known AtCoder problem "Tetromino Tiling" (maybe from ABC 126 F?). I think I've seen a solution that uses the fact that I, O, J, L, S, Z, T tetrominos can tile a 2xN board with some constraints and the maximum K is determined by formula: K = a_O + min( something )... Actually recall something about using I pieces to fill gaps. Let's derive.
Let's attempt to find a constructive tiling using combinations.
Observation: The O piece (2x2) covers a 2x2 block perfectly. That can be used as a building block of width 2.
The I horizontal piece covers 4 consecutive cells in one row, leaving the other row empty for those 4 columns. To fill the other row, we need pieces that can fill that stripe. Possibly using J, L, T, S, Z, O, I vertical? But I vertical not allowed. So if we place an I on top row, we have a 4-column stretch where bottom row is completely empty. To fill that bottom row, we must place pieces that cover those bottom cells. However, the bottom row alone is a 1x4 segment. But we have pieces that can cover bottom row cells combined with top row cells from adjacent columns? However, the top row cells are already occupied by the I piece. So we cannot place any piece that would occupy a top row cell in those columns because they are already covered. So we must fill the bottom row cells using pieces that only occupy bottom row cells? The only piece that can cover a single row is the I piece itself (horizontal). But we already used an I on top. Could we use another I on bottom? Yes, we could place an I on bottom covering the same 4 columns? That would overlap with the top I? No, overlapping not allowed. But we could place an I on bottom covering exactly the same columns as the top I? That would fill bottom row cells, but then total cells would be 8, not 4. Actually the top I covers top row 4 cells; bottom I covers bottom row 4 cells; together they would cover a 2x4 rectangle fully, using 2 I pieces. That's a valid tiling: two I pieces stacked vertically? But they are both horizontal placed on separate rows. That uses 2 I pieces and covers a 2x4 area. So a 2x4 block can be tiled by two I pieces (one on top, one on bottom). That's a width 4 segment. Also, we could use one O piece and something else? Not needed.
But more generally, pieces like T, J, L, S, Z interlock with each other to fill holes.
Let's try to find "atomic" tilings of small widths that use certain pieces.
We need to cover a 2xN board. Since pieces have width 3 or 4 or 2, we can combine them.
A classic known fact: In tiling a 2xN board with trominoes? Not that.
Maybe we can think of covering the board column by column. At any point, we have a "frontier" that might be jagged. Because pieces can extend to the right. Since we only have two rows, the frontier can be described by which cells in the current column are already filled by pieces that started earlier. Actually, when we place a piece, it covers cells in some columns. As we scan left to right, when we reach a column, some cells may already be filled by pieces that started earlier (to the left). The unfilled cells must be covered by a piece that starts at or before this column.
Given the shapes, the possible patterns of covering a segment from a clean start (no pending cells) to a clean end (no pending cells) define building blocks. These building blocks would have a certain width and consume certain pieces.
We can try to enumerate all possible ways to tile a contiguous segment of columns such that at the start and end, both rows are "even" (i.e., no half-filled cells). The segment must be covered exactly by pieces that are wholly within the segment. Then the whole board can be tiled by concatenating such segments.
The minimal widths for such closed segments: width 2: O piece (covers 2x2). Could there be a segment of width 1? No piece fits width 1 except vertical I (height 4) which is not allowed. So width 1 impossible. Width 2: only O fits exactly because O is 2x2. Could we combine two I pieces? That's width 4, not 2. Could we combine a T and something to get width 2? T width is 3, so no. So only O gives width 2 closed block.
Width 3: Could we tile a 2x3 rectangle with tetrominos? The area is 6, not multiple of 4, so impossible. So width must be multiple of 2? Actually total area of segment must be multiple of 4 (since each piece 4 cells). So width (number of columns) times 2 (rows) = 2*width must be multiple of 4 => width must be even. So any closed segment must have even width. So widths possible: 2,4,6,...
Thus O (width 2) is a building block. Width 4: many possibilities. For example: two I pieces (one top, one bottom) covering 2x4. Also combination of other pieces could tile 2x4. For instance, a T and something? Let's explore. 2x4 area = 8 cells. Using pieces of area 4, we need exactly 2 pieces. So we need pairs of pieces that together exactly tile a 2x4 rectangle. Which pairs work? We need to place two tetrominos that together cover 2 rows by 4 columns without overlap and exactly. Let's enumerate possible pairs:
- I (top) + I (bottom): works as described.
- O + O: two O's would cover 2x4? Actually each O covers 2x2, so two O's side by side would cover 2x4 if placed adjacent horizontally: first O covers columns 1-2, second O covers columns 3-4. That's valid. So O+O works.
- O + something else? O covers 2x2, leaving 2x2 area to cover with another piece? But a single tetromino covers 4 cells, and 2x2 area is exactly O. So O+O only.
- T + ? T covers 3 columns, leaving 1 column width? But total width 4, T covers 3 columns, so the remaining area would be 2x1 (2 cells) plus maybe some cells overlapping? Not possible because remaining area would be 2 cells (area 2) but we need one more tetromino of area 4, so not possible. So any two tetrominos must together exactly cover 2x4. Since each has area 4, total area 8. The union of their bounding boxes might overlap. They must fit in 2x4 without gaps. Could we have a T and an I? T width 3, I horizontal width 4. But if we place T in some orientation, it occupies 3 columns, I occupies 4 columns, total columns might be 4 if they overlap? But they can't overlap. So we need to arrange them within 4 columns. Suppose we place I on top row covering columns 1-4. Then bottom row columns 1-4 are empty. T placed in some orientation that covers some of bottom row cells but must not overlap top I. T width 3, could be placed such that its cells occupy bottom row and also some top row? But top row is already occupied by I, so T cannot occupy any top cell. However T always occupies at least one top cell? Actually T orientation: if placed point up, top row has 1 cell, bottom row 3 cells; if point down, top row 3, bottom row 1. So T always occupies at least one cell in each row? Actually point up: top row 1 cell, bottom row 3 cells. So if we placed I on top row covering all top cells, T cannot be placed because it would need a top cell. So T cannot be placed with I on top covering same columns. Could we place I on bottom row and T on top? Similar issue. So T + I not possible in 2x4? What about T and S? S width 3, covers both rows. Could T and S together cover 2x4? Let's attempt. Both have width 3, so total width might be up to 6 if placed non-overlapping. To fit in 4 columns, they must overlap in at least 2 columns. But they can't overlap. So impossible. Similarly, any two pieces both of width 3 cannot fit in width 4 without overlap because their combined minimal width if placed side by side is at least 3+? Actually if placed with some offset, they could interlock? For example, one piece might cover columns 1-3, the other covers columns 2-4? That would overlap in columns 2-3, which is not allowed. So they must be placed without overlapping cells, so they cannot overlap in columns unless they are in different rows? But they occupy both rows, so overlapping columns would cause potential overlap if they share rows. However, it's possible that one piece occupies only top row in some columns and the other occupies only bottom row in those columns, thus no overlap. For example, a piece that covers top row in a column and another piece covers bottom row in the same column: they could coexist. So overlapping columns is allowed as long as they don't occupy the same cell. So two pieces could be placed in the same set of columns if they occupy different rows. For instance, an I on top covering columns 1-4, and an I on bottom covering columns 1-4 would be overlapping in columns but not in rows, covering the whole 2x4. That's the I+I case. Similarly, a piece that covers both rows in some columns could coexist with a piece that covers only one row in those columns, as long as they don't conflict. So we need to consider column overlap.
Thus, building blocks could be more complex than just partitioning into independent column blocks. However, we can still think in terms of covering the board with a sequence of placements that might interleave in columns.
But perhaps there is known result: The maximum tiling of a 2xn board with tetrominos (I,O,T,J,L,S,Z) given counts can be computed by greedy: Use as many O pieces as possible (each uses 2 columns), use I pieces in pairs (one top, one bottom) to cover 4 columns (each pair uses 2 I pieces), and then use combinations of J, L, T, S, Z to fill remaining area, perhaps with some I singles used to balance.
However, note S and Z pieces are balanced (2,2) and have width 3. They also create a specific pattern. Let's examine how S and Z can be used. S shape (flat) covers columns c, c+1, c+2 with pattern: top row: c+1,c+2; bottom row: c,c+1. So it leaves top row column c empty, bottom row column c+2 empty. Those empty cells must be filled by other pieces. Similarly Z leaves top c empty? Actually Z: top row: c,c+1; bottom row: c+1,c+2. So holes: top c+2? Wait Z: top (c,top), (c+1,top); bottom (c+1,bottom),(c+2,bottom). So uncovered: top column c+2, bottom column c. So both S and Z create a "staggered" hole pattern.
T: holes bottom columns c and c+2 (if point down) or top columns c and c+2 (if point up). J: holes bottom columns c+1,c+2 (if extra below left) or top columns c+1,c+2? Actually J original (3 top,1 bottom): holes bottom c+1,c+2. J rotated (1 top,3 bottom): holes top c+1,c+2? Wait earlier: J rotated gives top 1 at column c+2, bottom 3 columns c,c+1,c+2; holes top columns c,c+1. So J holes: either bottom two rightmost columns or top two leftmost? Let's be systematic:
Let's define piece placements by their leftmost column index and orientation. We'll define the shape as a set of relative coordinates (dx, row) where row 0=top, row 1=bottom.
List all allowed orientations (height<=2):
I (horizontal): row 0 only: dx=0,1,2,3. Or row 1 only: dx=0,1,2,3. So I can cover a single row across 4 columns.
O: rows 0 and 1, dx=0,1 for both rows. So covers 2x2 block.
T (point down): rows: top: dx=0,1,2; bottom: dx=1. So holes: bottom at dx=0 and 2.
T (point up): top: dx=1; bottom: dx=0,1,2. Holes: top at dx=0 and 2.
J (original): top: dx=0,1,2; bottom: dx=0. Holes: bottom at dx=1,2.
J (rotated 180): top: dx=2; bottom: dx=0,1,2. Holes: top at dx=0,1.
L (original): top: dx=0,1,2; bottom: dx=