We need to understand the problem: There are n students using pieces made of two blocks (i.e., each block is of length? Actually "pieces made of two blocks" means each piece is comprised of two blocks? Or each piece is a block that is two units? The phrasing: "Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks." So each piece is like a segment of length 2 or 3? They stack them. So each student's tower height is some multiple of 2 or 3, depending on which type they use. They want to assign distinct positive integer multiples: n students using 2's will have heights that are positive multiples of 2 (i.e., 2,4,6,8,...). m students using 3's will have heights that are positive multiples of 3 (i.e., 3,6,9,12,...). But the towers must all contain distinct numbers of blocks overall, regardless of type. So we need to choose n distinct multiples of 2, and m distinct multiples of 3, such that all n+m chosen numbers are distinct (i.e., the sets are disjoint) and we want to minimize the maximum number among them (i.e., the tallest tower's height). Equivalent: We need to assign to each student a positive integer height that is a multiple of its piece length (2 or 3), with no two students sharing the same height. The objective is to minimize the maximum height.
So we want to fill the smallest possible numbers (starting from the smallest multiples) but avoiding conflicts between multiples of 2 and multiples of 3. Essentially we need to select a set S of size n+m, partitioned into two subsets: S2 of size n consisting of multiples of 2, and S3 of size m consisting of multiples of 3. The maximum element of S is to be minimized.
This is like: We have natural numbers. We need to pick n numbers that are multiples of 2, and m numbers that are multiples of 3, all distinct, minimizing the maximum chosen.
We can think of a process: The numbers that are multiples of 2 or 3 are candidates. We want to pick n numbers from the multiples of 2, m from multiples of 3, such that the overall picked set is as low as possible. That is akin to scheduling: we need to assign the smallest possible numbers to students, but some numbers are both multiples of 2 and 3 (i.e., multiples of 6). Those numbers can be claimed by either type, but only one student can use that number. So there is contention for numbers that are multiples of both.
We want to minimize the max; it is like we need to find the smallest integer H such that there exist at least n multiples of 2 up to H, at least m multiples of 3 up to H, and the total number of distinct multiples (i.e., the union count) is at least n+m. Actually we need exactly n and m, but we can pick subsets; if up to H there are enough numbers for each type, we might still have conflicts that reduce the available distinct slots? More precisely, for a given H, the set of numbers ≤ H that are multiples of 2 is A = {2,4,..., up to H}. Its size is floor(H/2). Similarly multiples of 3: B, size floor(H/3). Their union is U, size = floor(H/2) + floor(H/3) - floor(H/6) (since common multiples of 2 and 3 are multiples of 6). We need to pick n numbers from A and m from B, all distinct. This is possible iff there exist numbers a from A and b from B with a+b? Actually we need to see if we can select n from A and m from B such that no overlap. Equivalent to: there exist subsets of sizes n and m of A and B with empty intersection. A necessary and sufficient condition: floor(H/2) >= n, floor(H/3) >= m, and floor(H/2) + floor(H/3) - min( something )? Actually we need to check that we can assign distinct numbers to the two groups despite the overlap. This is a bipartite selection problem.
We can think of we have to assign each of the n two-type students a distinct multiple of 2, and each of the m three-type a distinct multiple of 3. They cannot share numbers. So we need to choose a total of n+m numbers from the union, but with the constraint that exactly n of them are from A (multiples of 2) and m from B (multiples of 3). That's feasible iff there exists a subset S ⊆ U with |S∩A| = n and |S∩B| = m. Since A and B overlap on multiples of 6, we need to ensure that we can cover the required counts while resolving overlaps.
Let x = |A∩B| = floor(H/6). For the selection, we could assign some numbers that are in both to either group, but each can only be used by one student. So we need to allocate some of these overlap numbers to the two groups. Let k be the number of overlap numbers assigned to the two-group (i.e., used as a multiple of 2) and l the number assigned to the three-group. Since each overlap number can be used by only one group, we have k + l ≤ x. But also, the total numbers taken from A is: numbers from A-only (multiples of 2 but not 3) plus k (overlap assigned to 2) = n. Similarly, from B-only + l = m. So we need: let a_only = |A \ B| = floor(H/2) - x, b_only = |B \ A| = floor(H/3) - x. Then we require:
a_only + k = n (1)
b_only + l = m (2)
with k, l ≥ 0 integers and k + l ≤ x.
So we need existence of nonnegative integers k,l satisfying:
k = n - a_only
l = m - b_only
But k and l must also satisfy: max(0, n - a_only) and max(0, m - b_only) are the minimum numbers of overlaps needed to satisfy each group. Actually from (1), k = n - a_only, but k must be ≥ 0 because we cannot have negative from only, meaning n ≤ a_only? Actually if n > a_only, we need to use some overlaps for 2. If n ≤ a_only, we can set k=0 (we could still use some overlaps but not needed). Similarly for l. However, we can also choose to not use all only numbers; we might leave some only numbers unused if we want to use overlaps for one group? But we must meet exactly n and m. Usually we would take as many only numbers as possible because they don't cause conflict. So the minimal usage of overlaps is: needed2 = max(0, n - a_only), needed3 = max(0, m - b_only). Then we need needed2 + needed3 ≤ x. That's the condition for feasibility. However, we might also be able to not use all only numbers? Suppose a_only is larger than n, we could still use some overlaps for 2, but that would be wasteful and might increase needed overlaps for 3? But we can always choose to ignore extra only numbers and use only numbers for the exact count, so the minimal required overlaps is exactly needed2 + needed3. So condition is: max(0, n - a_only) + max(0, m - b_only) ≤ x.
But is that also sufficient? Yes, because we can assign needed2 overlaps to 2, needed3 overlaps to 3, and the rest overlaps (if any) can be left unused. And we assign all only numbers needed: for group 2 we take all a_only numbers (or just enough to fill n, but since needed2 is defined as n - a_only when a_only < n, we need to take all a_only and then needed2 overlaps; if a_only >= n, we can take any n of the a_only numbers, leaving some unused, and take 0 overlaps for 2. Similarly for 3. So the condition is necessary and sufficient.
Thus for a given H, feasibility is:
Let A = floor(H/2), B = floor(H/3), C = floor(H/6).
a_only = A - C,
b_only = B - C.
Then needed2 = max(0, n - a_only)
needed3 = max(0, m - b_only)
Condition: needed2 + needed3 ≤ C.
We want the smallest H such that condition holds.
Alternatively, we can formulate that we need to have:
A >= n (obviously, because we need at least n multiples of 2 up to H) and B >= m. But those are not sufficient due to overlaps.
Indeed, from condition: needed2 + needed3 ≤ C is equivalent to:
max(0, n - (A - C)) + max(0, m - (B - C)) ≤ C.
Simplify: n - (A - C) is >0 only if n > A - C => A < n + C? Actually n - (A - C) = n + C - A. So needed2 = max(0, n + C - A). Similarly needed3 = max(0, m + C - B). So condition: max(0, n + C - A) + max(0, m + C - B) ≤ C.
We can also write: A + C >= n + ? Not exactly.
Let's explore properties.
We can binary search on H. H can be up to some large number. Since n,m up to 1e6, H could be up to maybe 2e6? Actually worst case: n=1e6, m=1e6. Need to assign 2e6 distinct numbers, each either multiple of 2 or 3. The numbers themselves could be larger than 2e6 because of conflicts. For instance, if we try to take many multiples of 2 and 3, the union of multiples up to H grows roughly H/2+H/3-H/6 = 2H/3. Actually that sum = (3H+2H- H)/6? Let's compute: floor(H/2)+floor(H/3)-floor(H/6) ≈ (1/2+1/3-1/6)H = (2/3)H. So to have at least n+m distinct numbers in the union, we need roughly H ≥ 1.5*(n+m). For n+m=2e6, H ≈ 3e6. But also need enough for each type individually. Might be a bit higher due to discrete and distribution. So H max maybe around 3e6 or a bit more. However, we can compute exact bound: worst-case when all numbers are taken from multiples of 2 and 3, the union count is about 2H/3. To have 2e6, H >= 3e6. Additionally, each type individually need at least n and m, so we need floor(H/2) >= 1e6 => H>=2e6; floor(H/3)>=1e6 => H>=3e6. So the limiting factor may be m=1e6 requires H>=3e6 (since 31e6? Actually floor(H/3) >= 1e6 => H >= 3e6 (if H=3e6, floor=1e6 exactly). So H around 3e6 might be enough. But there might be conflicts: overlapping multiples of 6 reduce the union count, so we might need H larger to accommodate both. However, union count grows 2/3 H, so to get 2e6 union size, we need H >= 3e6 (since 2/33e6=2e6). At H=3e6 exactly, union count = floor(3e6/2)+floor(3e6/3)-floor(3e6/6)=1.5e6+1e6-500k=2e6 exactly (since 3e6/2=1.5e6 integer, /3=1e6, /6=500k). So union count = 2e6 exactly. However we need to also meet the individual counts: A=1.5e6 >= n=1e6, B=1e6 >= m=1e6. And overlaps C=500k. Let's check condition: a_only = A-C=1.5e6-500k=1e6, b_only = B-C=1e6-500k=500k. Wait B=1e6, C=500k => b_only = 500k. But m=1e6, so needed3 = m - b_only = 1e6-500k = 500k. needed2 = n - a_only = 1e6-1e6=0. Then needed2+needed3 = 500k <= C=500k => satisfied. So H=3e6 works. Could we go lower? Possibly. So maximum H might be up to maybe 3e6. But we need to be safe; we can binary search up to some upper bound, say 4e6 or 5e6. Or we could derive formula.
Alternative approach: We can compute the minimal H directly via reasoning about the numbers that are taken. But binary search is straightforward: Since n,m up to 1e6, H up to maybe 4e6, binary search from 0 to something like 4e6 or 10e6 is fine.
But maybe we can find closed form? Let's attempt.
We need to minimize max height. This is similar to problem "Two types of numbers (multiples of 2 and 3) pick distinct numbers to meet counts" often solved by binary search.
We'll binary search on H. For each H, compute A, B, C. Compute a_only = A - C, b_only = B - C. Compute need2 = max(0, n - a_only), need3 = max(0, m - b_only). If need2 + need3 ≤ C, then H is feasible (we can assign). Then we find minimal H.
Edge cases: n=0 or m=0. If m=0, then we just need n distinct multiples of 2. The minimal max height is the n-th multiple of 2, which is 2n. Similarly if n=0, answer = 3m. But wait, if only one type, there is no conflict, so the smallest possible max is the n-th smallest multiple: 2n for 2's, 3m for 3's. Check sample: Input 5 0 => output 10, which is 2*5 = 10. Yes.
If both present, we need to handle overlaps.
Binary search bounds: low = 1? Actually towers must be non-zero, so min height at least min(2,3) = 2 maybe. But we can start low=0. high = something like 2*(n+m) maybe? Because worst-case if we used all multiples of 2, we'd get max 2*(n+m). But with 3's, might be less. But to be safe, we can compute high = 2*(n+m) maybe? Let's test: n=m=1e6 => n+m=2e6 => 2*(n+m)=4e6. That's above 3e6 needed, so safe. But is it always enough? Consider n=0, m=1e6 => answer = 3e6, which is ≤ 4e6. So high=4e6 works for worst-case? Actually n=0,m=1e6 -> answer = 3e6. Yes. n=1e6,m=0 -> answer = 2e6. So 4e6 is fine. But consider n=1e6,m=1e6 => answer=3e6 <4e6. So high=4e6 is safe. But what about n=1,m=1? Let's compute: n=1,m=1. Small numbers: multiples of 2:2,4,6,...; multiples of 3:3,6,9,... Want distinct heights: easiest: 2 and 3 => max=3. Could also 2 and 6? Max=6 but min is 3. So answer should be 3? But check condition: n=1,m=1. Need one multiple of 2 and one multiple of 3 distinct. The smallest possible max is max(2,3)=3. However, 2 and 3 are both available. So answer 3. Using binary search: For H=3: A=floor(3/2)=1 (multiples:2), B=floor(3/3)=1 (3), C=floor(3/6)=0. a_only=1-0=1, b_only=1-0=1. need2 = max(0,1-1)=0, need3 = max(0,1-1)=0 => sum 0 ≤ C=0 => feasible. So H=3 works. H=2: A=1, B=floor(2/3)=0, fails because B>=m? Actually need at least m=1 multiple of 3, B=0 <1, not feasible. So minimal H=3. That's fine.
Now high=4e6 is okay for up to 1e6 each, but just to be safe we might take high = 2*(n+m) + something, but note for n=0,m=1e6, 2*(n+m)=2e6 which is less than 3e6, not enough. So high must be at least 3e6. Better high = 3*(n+m) maybe? That would be up to 6e6, safe. Or we can compute high = max(2n, 3m) * 2? Actually we need to ensure condition can be met. The worst-case where conflicts force high might be when n and m are both large and near equal. Possibly H = 3*(n+m) might be overkill but still small enough for 2e6 => 6e6 which is fine.
Given n,m up to 1e6, 6e6 is small; binary search over 0..6e6 (~23 steps) is trivial. We can even set high=2e7 to be safe but no need.
But we must be careful: The condition we derived is necessary and sufficient for existence of assignment, but only if we have enough numbers overall? Actually we also need A >= n and B >= m, but these are implied by the condition? Let's see: If need2 = max(0, n - a_only) then need2 >0 implies n > a_only, but a_only = A - C, so n > A - C => n + C > A. For condition we need need2 + need3 ≤ C. So need2 ≤ C. So n + C - A ≤ C => n ≤ A. So A >= n. Similarly need3 ≤ C implies m ≤ B. So condition automatically enforces A>=n and B>=m. So we can just use the condition.
Thus binary search works.
Now we can implement: While low < high, mid = (low+high)//2, check if feasible(mid). If feasible, high = mid; else low = mid+1. Output low.
Now we must ensure the answer is an integer, and that towers are made of pieces, so heights are multiples of 2 or 3. The answer we compute is the minimal H such that an assignment exists. That H may not be a multiple of 2 or 3; but the condition uses floor divisions. If H is feasible, then there exists assignment with all heights ≤ H. But the tallest tower height will be the maximum of the assigned numbers. Since we want the minimum possible height of the tallest tower, it will be some integer which is at least the max of assigned numbers. In our feasibility test, we ask: can we assign all heights ≤ H? So the minimal H for which it's possible is the answer. Since assignments can only use numbers that are multiples of 2 or 3, the answer might be not a multiple of 2 or 3? Actually the tallest tower's height will be one of the assigned numbers, which is a multiple of 2 or 3. So answer is a multiple of 2 or 3. But our binary search may return some H that is not a multiple, but the minimal H that satisfies condition will be exactly the minimal possible max height? Let's think: Suppose the minimal possible max height is T, which is a multiple of either 2 or 3. For any H < T, the condition fails; for H >= T, condition holds. Since T is an integer multiple, the minimal H that is feasible is exactly T. So binary search yields T. Because if T is feasible, any H >= T is also feasible (since we can use the same assignment, all heights <= T <= H). So monotonic. So answer = min H such that condition holds = T.
Thus binary search is correct.
We just need to implement check(mid) efficiently.
Now let's test on samples:
Sample1: n=1, m=3.
We guess answer 9.
Check H=9: A=floor(9/2)=4 (2,4,6,8); B=floor(9/3)=3 (3,6,9); C=floor(9/6)=1 (6). a_only=A-C=4-1=3 (2,4,8); b_only=B-C=3-1=2 (3,9). need2 = max(0, 1-3)=0; need3 = max(0, 3-2)=1; need2+need3=1 ≤ C=1 => feasible. H=8: A=4 (2,4,6,8); B=floor(8/3)=2 (3,6); C=floor(8/6)=1 (6). a_only=4-1=3 (2,4,8? Actually 8 is not multiple of 6, yes); b_only=2-1=1 (3). need2= max(0,1-3)=0; need3= max(0,3-1)=2; need2+need3=2 > C=1 => not feasible. So answer=9. Good.
Sample2: n=3, m=2 => answer 8.
Check H=8: A=4, B=2, C=1. a_only=3, b_only=1. need2 = max(0,3-3)=0, need3 = max(0,2-1)=1 => sum=1 ≤1 => feasible. H=7: A=floor(7/2)=3 (2,4,6); B=floor(7/3)=2 (3,6); C=floor(7/6)=1 (6). a_only=3-1=2 (2,4); b_only=2-1=1 (3). need2=max(0,3-2)=1, need3=max(0,2-1)=1 => sum=2 > C=1 => not feasible. So answer=8. Good.
Sample3: n=5,m=0 => answer 10. Our binary search will check H=10? But condition with m=0: a_only = A - C, need2 = max(0, n - a_only). need3=0. Condition reduces to: max(0, n - (A-C)) ≤ C => n ≤ A. Since A = floor(H/2) must be >= n. For H=10, floor(10/2)=5 => A=5 >=5, and check: a_only = 5 - floor(10/6)=5-1=4? Wait C = floor(10/6)=1. So a_only=4, need2 = max(0,5-4)=1, need2+0=1 ≤ C=1 => feasible. H=9: floor(9/2)=4 => A=4 <5 => not feasible (need2 = max(0,5- (4 - floor(9/6)=4-1=3) =2? Actually let's compute: A=4, C=1, a_only=3, need2=5-3=2, C=1, fails). So answer=10. Works.
Thus binary search approach works.
But we can potentially compute directly without binary search: The condition involves floor functions, but binary search is simple.
Now constraints: n,m up to 1e6, binary search range up to maybe 6e6, which is trivial.
Edge Cases: n=0 or m=0 already handled by condition? Let's test n=0,m=5: For H=15, A=7, B=5, C=2. a_only=5, b_only=3? Actually B=5, C=2 => b_only=3. need2=0, need3 = max(0,5-3)=2, need2+need3=2 ≤ C=2 => feasible. But can we have H smaller? For H=14: A=7, B=4, C=2? floor(14/3)=4, floor(14/6)=2. So b_only=4-2=2, need3 = 5-2=3, need2+need3=3 > C=2 => not feasible. So answer = 15? But we expect answer = 3*5 = 15. Yes.
If n=0, the condition becomes: need3 = max(0, m - (B-C)) ≤ C. That is m ≤ B. Since B = floor(H/3), the minimal H with floor(H/3) >= m is H = 3m (if m>0). But also need to satisfy need3 ≤ C, which may impose additional? Let's check: For n=0, need3 = max(0, m - (B-C)). Since B = (B-C)+C, so m - (B-C) = m + C - B. The condition need3 ≤ C is equivalent to max(0, m + C - B) ≤ C => if m ≤ B then m + C - B ≤ C? Actually if m ≤ B, then m + C - B ≤ C, but could be negative, so need3 = max(0, m+C-B). The condition need3 ≤ C may hold even if m > B? If m > B, then need3 = m + C - B > C? Since m > B => m - B > 0 => m + C - B > C, then need3 > C, fails. So condition essentially requires m ≤ B. Because if m ≤ B, then m + C - B ≤ C, but could be negative, so need3 = 0 ≤ C. So condition simplifies to m ≤ B. Similarly for n=0, A condition irrelevant. So answer = smallest H with floor(H/3) >= m => H = 3m if m>0 (since 3*m gives floor(3m/3)=m exactly). So good.
Thus binary search works for all.
Now we need to be careful with the upper bound. For n=m=1e6, we saw H=3e6 works. Could there be a case where H needs to be larger than 3e6? Suppose n=1e6, m=1e6-1? Let's test extreme: n=1e6, m=0 -> H=2e6. n=0,m=1e6 -> H=3e6. So worst-case is when m is large because 3's require larger numbers for a given count. But also overlaps may cause need for more numbers. For n and m both near 1e6, we needed H=3e6. But is there a scenario where H > 3e6? Let's try n=1e6, m=1e6, we got H=3e6 exactly. Could it be that H must be >3e6? Check n=1e6, m=1e6 at H=3e6-1? Let's test: H=2999999? Actually 3e6 = 3000000. At H=2999999, floor(H/2)=1499999 (since 21499999=2999998, so floor=1499999), floor(H/3)=999999 (since 3999999=2999997, floor=999999), floor(H/6)=499999 (6*499999=2999994, floor=499999). Then a_only = A-C = 1499999-499999=1000000, b_only = B-C = 999999-499999=500000. n=1e6, so need2 = max(0, n - a_only)=0, need3 = max(0, m - b_only)=1e6 - 500000 = 500000. C=499999. need2+need3=500000 > 499999 => fails. So H=3e6 needed. So H=3e6 works, not less. So max bound for n,m=1e6 is 3e6. But could n=1e6, m=1e6-1 also require 3e6? Maybe H=2999999 also fails similarly. So maximum answer might be 3e6. However, what about n=1e6, m=1e6+? but m max is 1e6, so fine.
But consider n=1, m=1000000. Then need H=3e6? Actually m=1e6 requires at least 3e6 to have enough multiples of 3, and n=1 can be placed at 2, which is fine. At H=3e6, floor(H/3)=1e6, floor(H/2)=1.5e6, C=500k, a_only=1.5e6-500k=1e6, b_only=1e6-500k=500k, need2 = max(0,1-1e6)=0, need3=1e6-500k=500k, C=500k => sum=500k <=500k, feasible. So H=3e6 works. Could H be less? Try H=2999999, floor(H/3)=999999 < 1e6, fails because B < m. So min is 3e6. So max answer 3e6.
Now consider n=1000000, m=1. Then H must be at least 2e6 for n? Actually n=1e6 needs at least 2e6 to have 1e6 multiples of 2, but also m=1 needs a multiple of 3. At H=2e6: floor(2e6/2)=1e6, floor(2e6/3)=666666, floor(2e6/6)=333333. a_only = 1e6-333333 = 666667? Actually 1,000,000 - 333,333 = 666,667. b_only = 666,666 - 333,333 = 333,333. need2 = max(0, 1e6 - 666,667) = 333,333? Actually 1e6 - 666,667 = 333,333. need3 = max(0, 1 - 333,333)=0. sum = 333,333. C = 333,333. So sum <= C, exactly equal? 333,333 <= 333,333 yes. So H=2,000,000 feasible? Wait check: H=2,000,000, A=1,000,000, B=666,666, C=333,333. a_only = 666,667 (since 1,000,000 - 333,333 = 666,667). But is that correct? Let's compute precisely: 2,000,000 / 2 = 1,000,000 exactly, so A=1,000,000. /3 = 666,666.666..., floor = 666,666. /6 = 333,333.333..., floor = 333,333. So a_only = A - C = 1,000,000 - 333,333 = 666,667. b_only = B - C = 666,666 - 333,333 = 333,333. Then need2 = max(0, n - a_only) = 1,000,000 - 666,667 = 333,333. need3 = max(0, 1 - 333,333) = 0. sum = 333,333. C = 333,333. So condition holds. So H=2,000,000 works. Could we have H=1,999,999? Then A=floor(1,999,999/2)=999,999 (since 2999,999=1,999,998), B=floor(1,999,999/3)=666,666 (3666,666=1,999,998), C=floor(1,999,999/6)=333,333 (6333,333=1,999,998). a_only=999,999-333,333=666,666. need2=1,000,000-666,666=333,334. b_only=666,666-333,333=333,333. need3= max(0,1-333,333)=0. sum=333,334. C=333,333 => fails. So answer = 2,000,000 exactly? But is there assignment possible with max height 2,000,000? The tallest tower would be maybe 2,000,000? But note that 2,000,000 is a multiple of 2, and there are 1e6 multiples of 2 up to 2e6 inclusive: 2,4,...,2e6. Among those, those that are multiples of 6 (i.e., up to 2e6) are 333,333 of them (since 6333,333=1,999,998, plus maybe 2,000,004? Actually 6*333,334=2,000,004 >2e6, so count 333,333). So there are 1e6 - 333,333 = 666,667 multiples of 2 not divisible by 3. That's a_only. But we need 1e6 multiples of 2. So we need to take all 666,667 non-overlap multiples, plus 333,333 from the overlaps (multiples of 6). That fills the n=1e6. For m=1, we need one multiple of 3. We can take a multiple of 3 that is not already used. Among multiples of 3, there are B=666,666 total, of which 333,333 are multiples of 6 (overlap) and 333,333 are multiples of 3 only. The overlaps are all used by the 2-group? We used 333,333 overlaps for the 2-group. That uses all overlaps? Actually total overlaps C=333,333. If we use all of them for the 2-group, then we have no overlaps left for the 3-group. But we need one multiple of 3. There are 333,333 multiples of 3 only, we can take one of those, e.g., 3. So that works. So the max height might be 2,000,000? The tallest tower among the chosen: we used all multiples of 2 up to 2e6, so the tallest is 2e6. For the 3-group, we take some small multiple like 3. So tallest = 2e6. That's indeed max height. Could we lower the max height? At H=1,999,999 we could not assign because we lack one multiple of 2 (since only 999,999 multiples of 2 total). So the minimum possible tallest height is 2,000,000. So answer = 2e6. That's > 3e6? Actually 2e6 < 3e6. So maximum answer among extremes might be max(2e6,3e6) = 3e6. So overall answer is at most 3e6.
But consider n=1e6, m=1e6 we got 3e6. So upper bound 3e6. So we can set high = 3e6 + maybe some safety margin like 10. However, we should be careful: n and m can be up to 1e6, but maybe combination where n is close to 0 and m large gives 3e6, or n large m small gives 2e6. So max is at most 3e6. But is there any scenario where answer > 3e6? Suppose n=1e6, m=1e6-1? Let's test H=2999999? Actually we saw that for m=1e6-1, B floor(H/3) needed to be at least 999,999. At H=3e6-3? Actually we need to ensure condition. We can compute worst-case: maybe when n and m are both large, the overlap usage may force H to exceed 3e6. Let's attempt to find theoretical max.
We want to minimize H such that:
A = floor(H/2) >= n
B = floor(H/3) >= m
And also n - (A - C) + m - (B - C) ≤ C, where C = floor(H/6).
Rewrite inequality: (n - (A - C)) + (m - (B - C)) ≤ C => n + m - (A + B) + 2C ≤ C => n + m ≤ A + B - C.
But A+B-C = floor(H/2) + floor(H/3) - floor(H/6). That's exactly the count of numbers ≤ H that are multiples of 2 or 3 (i.e., the union size). So the condition is simply: the total number of distinct multiples of 2 or 3 up to H must be at least n + m. And also individually A ≥ n, B ≥ m. But is that enough? Let's see: n + m ≤ U, where U = A+B-C. That is a necessary condition: we need at least n+m distinct numbers available. But is it sufficient? We also need to be able to pick exactly n from A and m from B. Does n+m ≤ U guarantee that we can choose n from A and m from B? Not always? Let's test: Suppose H=5, A=floor(5/2)=2 (2,4), B=floor(5/3)=1 (3), C=floor(5/6)=0, U=3. n=2, m=1 => n+m=3 ≤ U=3, and A=2 ≥2, B=1≥1. Can we assign? We need two multiples of 2 and one multiple of 3 distinct. The numbers are {2,4} from 2, {3} from 3. They are all distinct, so yes. Another test: H=6, A=3 (2,4,6), B=2 (3,6), C=1 (6), U=4. n=2, m=2 => n+m=4 ≤ U=4. A=3≥2, B=2≥2. Can we assign? We need two multiples of 2 and two multiples of 3. Options: for 2's we can take {2,4} (not 6), for 3's we can take {3,6} (6 is overlapping but we can assign 6 to 3's and avoid conflict). That works. n=3, m=1 => n+m=4 ≤ U=4, A=3≥3, B=2≥1. Need three multiples of 2 and one multiple of 3. The 2's must be three distinct numbers from {2,4,6}. The 3's one from {3,6}. If we assign 6 to a 2, then 3 can be 3. That's fine. If we assign 6 to 3, then we need three 2's but only {2,4} left, only two. So we cannot assign 6 to 3 if we need three 2's. But we can assign 6 to 2 and 3 to 3, that works. So it's possible. n=1, m=3: n+m=4 ≤ U=4, A=3≥1, B=2≥3? B=2 <3, fails individual condition. So B≥m is required.
Now is there a scenario where A≥n, B≥m, U≥n+m, but still impossible? Let's try to construct counterexample. The condition we derived (need2+need3 ≤ C) is equivalent to: n - (A-C) ≤ C - (m - (B-C))? Actually we derived necessary and sufficient. Let's derive relation between U≥n+m and the previous condition.
We have need2 = max(0, n - (A-C)), need3 = max(0, m - (B-C)). Condition need2+need3 ≤ C. Let's express U = A+B-C = (A-C) + (B-C) + C. So n+m ≤ U means n+m ≤ (A-C)+(B-C)+C => (n - (A-C)) + (m - (B-C)) ≤ C. But note that n - (A-C) could be negative, similarly for m - (B-C). However, the inequality n+m ≤ U is equivalent to (n - (A-C)) + (m - (B-C)) ≤ C, but without the max. However, the condition we need is need2+need3 ≤ C, where need2 = max(0, n - (A-C)), need3 = max(0, m - (B-C)). Since the left side with max is at least as large as the sum without max (since if n - (A-C) is negative, max replaces with 0 but the sum without max would be smaller). So n+m ≤ U is a weaker condition: It's possible that n+m ≤ U but need2+need3 > C. For example, consider H=8, we had A=4, B=2, C=1, U=5. n=3, m=2 => n+m=5 ≤5, A=4≥3, B=2≥2. But we found earlier that it's not feasible? Actually earlier we had n=3,m=2, H=8 was feasible? Let's check: For H=8, A=4, B=2, C=1, U=5. n=3,m=2 => n+m=5 ≤5, A≥3, B≥2. Our earlier test for H=8 (sample 2) said feasible with towers: 2,4,8 for twos, 3,6 for threes? Wait 6 ≤8, 6 is multiple of 6, 8 is multiple of 2. But we had to check assignment: Actually earlier sample2 said answer=8, so H=8 is feasible. So that is feasible. Let's find a potential counterexample.
We need n - (A-C) > 0 and m - (B-C) > 0 such that their sum > C, but n+m ≤ U still holds? Let's see: Since U = (A-C)+(B-C)+C, we have n+m ≤ (A-C)+(B-C)+C. Rearranged: (n - (A-C)) + (m - (B-C)) ≤ C. If both n - (A-C) and m - (B-C) are positive, then need2+need3 = exactly that sum. So condition equals n+m ≤ U. If one of them is negative, then need2+need3 = max(0, positive part) which is less than the sum (since negative part is ignored). So n+m ≤ U is actually stronger than need2+need3 ≤ C? Let's examine:
Let x = n - (A-C), y = m - (B-C). Then need2 = max(0,x), need3 = max(0,y). Condition need2+need3 ≤ C. The sum without max is x+y. Since x+y ≤ C is required by n+m ≤ U (because x+y = n+m - [(A-C)+(B-C)] = (n+m) - (U - C) = n+m - U + C, so x+y ≤ C <=> n+m ≤ U). So n+m ≤ U is equivalent to x+y ≤ C. Now note that need2+need3 = max(0,x)+max(0,y) ≤ x+y if both x and y are non-negative? Actually if both are non-negative, need2+need3 = x+y. If one is negative and the other positive, say x<0, y>0, then need2+need3 = y, while x+y ≤ C implies y ≤ C - x, but since x is negative, -x positive, so y could be > C? Let's test: Suppose x = -2, y = C+1. Then x+y = C-1 ≤ C, so n+m ≤ U holds. But need2+need3 = y = C+1 > C, so condition fails. However, is such a scenario possible given constraints A, B, C? Since x = n - (A-C). If x < 0, that means n < A-C, i.e., n ≤ a_only - 1. So we have more than enough only-2 numbers. y = m - (B-C) > 0 means m > b_only, insufficient only-3 numbers. So we would need to use some overlaps for 3's. But we also have many only-2 numbers that we don't need to use all. Overlaps count C. Could we have y > C? Since y = m - (B-C). B-C = b_only. So y > C means m > b_only + C = B. But B is floor(H/3). So m > B. However, condition B ≥ m is required? In our scenario, if m > B, then B < m, so that would violate the individual requirement that we need at least m multiples of 3. But our feasibility condition also includes B ≥ m? Actually from need3 = max(0, m - b_only) > 0, but if m > B then m - b_only > B - b_only = C, so need3 > C. But is that possible while A≥n and n+m ≤ U? Let's test: Suppose H such that B < m. Then n+m ≤ U? U = A+B-C, but if B < m, then U might still be >= n+m? Possibly, but we cannot satisfy m students because there aren't enough multiples of 3 at all. So individual condition B ≥ m is necessary. Similarly A ≥ n. So we must have A ≥ n and B ≥ m. Under those, we have x = n - (A-C) and y = m - (B-C). If x < 0, that means n ≤ a_only (since A-C = a_only). Since A ≥ n, it's possible x negative? Yes, if n is less than a_only. In that case, n ≤ a_only, so n - a_only ≤ 0. Then y = m - b_only. Since B ≥ m, we have m ≤ B, so y = m - b_only ≤ B - b_only = C. So y ≤ C. Thus need2+need3 = max(0,x)+max(0,y) = 0 + y (since x negative) = y ≤ C. So condition holds. Similarly, if y negative, then need2+need3 = max(0,x) ≤ C because x ≤ C (since A ≥ n implies x = n - (A-C) ≤? Actually if y negative, then m ≤ b_only. Since B ≥ m, that's fine. x = n - a_only. Since A ≥ n, we have n ≤ A, but a_only = A-C, so n - a_only ≤ C? Not necessarily. If n is large, x could be positive up to C? Because n can be as high as A, then n - a_only = A - (A-C) = C. So x ≤ C. So need2+need3 = x ≤ C. So condition holds. So indeed, under the individual constraints A ≥ n and B ≥ m, we have x ≤ C and y ≤ C. And also if both are positive, their sum x+y ≤ C from n+m ≤ U? Actually if both positive, then x = n - a_only > 0 and y = m - b_only > 0. Then n+m = a_only + b_only + (x+y) ≤ a_only + b_only + C = U? That's equivalent to x+y ≤ C. But is it automatically true given A≥n,B≥m? Not necessarily. We could have A ≥ n and B ≥ m but x+y > C. For example, earlier H=8, n=3,m=2: A=4,B=2,C=1. a_only=A-C=3, b_only=B-C=1. n=3 => x=3-3=0, m=2 => y=2-1=1, sum=1 ≤ C=1. That's fine. To get x+y > C, we would need both n > a_only and m > b_only such that (n - a_only)+(m - b_only) > C. But since a_only = A-C, b_only = B-C, we have n > A-C and m > B-C, so n ≥ A-C+1, m ≥ B-C+1. Then n+m ≥ (A-C)+(B-C)+2 = A+B-2C+2. And U = A+B-C. So n+m ≤ U would require A+B-2C+2 ≤ A+B-C => -2C+2 ≤ -C => 2 ≤ C? Actually simplify: n+m ≤ U => (A-C+1)+(B-C+1) ≤ A+B-C => A+B-2C+2 ≤ A+B-C => -2C+2 ≤ -C => 2 ≤ C. So if C ≥ 2, it's possible that n+m ≤ U still holds? Let's plug numbers: Suppose A=10, B=10, C=2 (so H such that floor(H/2)=10, floor(H/3)=10, floor(H/6)=2). Then a_only=8, b_only=8. Take n=9, m=9. Then x=9-8=1, y=1, sum=2 ≤ C=2. So ok. Take n=9, m=10? But m=10 > B? B=10 so m=10 is allowed. Then b_only=8, y=2, x=1, sum=3 > C=2. But does this satisfy A≥n? n=9 ≤10, B≥m? m=10 ≤10 ok. Check n+m=19, U = A+B-C=10+10-2=18. So n+m=19 > 18, so U condition fails. So automatically, if both x>0 and y>0, then n+m > U? Let's derive: x>0 => n > A-C, y>0 => m > B-C. Then n+m > (A-C)+(B-C) = A+B-2C. So n+m ≥ A+B-2C+2 (since integers). For n+m ≤ U = A+B-C, we need A+B-2C+2 ≤ A+B-C => 2 ≤ C. So if C ≥ 2, it's possible that n+m ≤ U while both x,y positive? Let's test with C=2: n+m could be A+B-2C+2 = A+B-2. That is ≤ A+B-C = A+B-2? Actually A+B-2C+2 = A+B-4+2 = A+B-2. And U = A+B-2. So n+m can be exactly U. So if n+m = U, then x+y = C? Because n+m = (A-C)+(B-C)+C => x+y = C. So if both positive, x+y = C. So condition holds. If n+m < U, then x+y < C. So indeed, under A≥n and B≥m, the condition need2+need3 ≤ C is equivalent to n+m ≤ U? Let's test scenario where one of x or y negative and the other positive but sum x+y could be less than or equal to C, but need2+need3 = max(0,x)+max(0,y) = positive part only. If x negative, then need2+need3 = y. Since y = m - b_only. And we have B ≥ m, so y ≤ B - b_only = C. So y ≤ C. So condition holds. So need2+need3 ≤ C holds automatically if A ≥ n, B ≥ m, and n+m ≤ U? Let's check scenario: x negative, y positive, but y could be > C? As argued, if B ≥ m then m ≤ B, but m could be as high as B. Then y = m - b_only ≤ B - b_only = C. So y ≤ C. So indeed if B ≥ m, then y ≤ C. Similarly if A ≥ n, then x ≤ C. So if both A≥n and B≥m, then individually x ≤ C and y ≤ C. And if x negative, need2+need3 = y ≤ C; if y negative, need2+need3 = x ≤ C; if both positive, need2+need3 = x+y. But does A≥n and B≥m guarantee x+y ≤ C? Not necessarily; we need also n+m ≤ U? Let's test with numbers: Suppose