We are given three integers r, g, b representing numbers of red, green, and blue balloons. Need to maximize t, the number of tables (each table gets exactly three balloons) such that for each table the three balloons are not all the same color. So each table must have either three balloons all of different colors (one of each) OR two balloons of one color and one of another (i.e., two same, one different). We cannot have all three same.
We want maximum number of tables we can form given constraints.
This is essentially: Given three piles of items, each table uses 3 items, with the condition that not all three are from the same pile. Equivalent to: We must avoid using three from a single color on one table, but any combination with at least two colors is allowed.
Goal: maximize total tables t, subject to total balloons used = 3t, and for each color, used count ≤ available count (r, g, b). Also we must ensure that it's possible to assign the tables such that no table gets all three from same color. But that is essentially a combinatorial feasibility: given counts of balloons used from each color (say r_used, g_used, b_used, with r_used ≤ r, etc., and r_used+g_used+b_used = 3t), we need to check if we can partition into t triples each avoiding all same. Is there any extra condition? Let's think.
Given we want maximum t, we are essentially looking for the largest integer t such that we can select r_used, g_used, b_used satisfying constraints and existence of a partition. But we can decide allocation as we like, we don't have to use all balloons. So we need to find the maximum t such that there exists a multiset of triples with the given total counts. This is similar to a classic problem: maximum number of triples (with no triple all same) from given color counts. Usually the answer is min( floor((r+g+b)/3), r+g+b - max(r,g,b) )? Wait, not exactly.
Let's derive.
We need to form t triples, each triple uses three balloons, and the only forbidden pattern is triple of same color. So as long as we don't exceed balloon counts, we can always arrange as long as we avoid three of same color. However, if one color is extremely abundant, it might force some tables to have three of that color if we try to use too many tables. But we can always avoid by mixing colors. However, there might be constraints because we have only limited numbers of other colors to break the homogeneity.
Consider we have r,g,b. Let total = r+g+b. Obviously t ≤ floor(total/3). Also, since each table can use at most 2 balloons of a given color (since if it used 3 of same, it's forbidden). Actually a table could have 2 of one color and 1 of another, or 1 of each. So each table can have at most 2 balloons of any particular color. Therefore, if we have t tables, the maximum number of balloons of a given color that can be used across all tables is at most 2t. Because each table can at most contribute 2 of that color. So we must have r ≤ 2t? Wait, we need to use balloons from each color at most what we have, but we could also leave some unused. However, if we want to achieve t tables, we need to assign balloons to tables, and the number of balloons used from a particular color cannot exceed both its availability and also at most 2t (since each table can take at most 2 of that color). But the condition is necessary: If we try to form t tables, we might need to use up some balloons, but we can leave some unused. So we don't need to use all balloons of a color. However, to achieve t tables, we need to assign exactly 3t balloons. So we need to pick some counts x ≤ r, y ≤ g, z ≤ b such that x+y+z = 3t, and also each table's composition feasible. The condition that each table can have at most 2 of any color gives a per-table constraint. Summing over tables, the total number of used balloons from each color is at most 2t. So x ≤ 2t, y ≤ 2t, z ≤ 2t. Is that sufficient? Also, we need to be able to partition x, y, z into t triples each not all same. This is a known necessary and sufficient condition: Given counts a, b, c (nonnegative integers summing to 3t), there exists a decomposition into t triples with no triple all same iff max(a,b,c) ≤ 2t (or equivalently, each ≤ 2t) and sum = 3t. Actually, is that always sufficient? Let's check.
We want to arrange t triples, each triple is of type: (3 distinct), (2 of one + 1 of another), but not (3 same). Equivalent to we want to fill t tables with balloons such that no table gets three same. This is similar to scheduling problem. We can think of it as we have to assign balloons to tables, each table capacity 3, with constraint that color i cannot be assigned to a table more than 2. If we have totals a, b, c, can we assign to t tables each capacity 3 with per-table color capacity 2? This is a flow or bipartite matching. Since tables are identical, we can think of a greedy.
Consider we have a, b, c ≤ 2t. We need to partition into t groups such that each group size 3 and no group has three same. The necessary conditions: a+b+c=3t, and each ≤ 2t. Are they sufficient? Let's try some examples:
Suppose t=2, a=4, b=2, c=0. Then sum=6, max=4, 2t=4, so condition holds. Can we form 2 tables without all same? We have 4 of color1, 2 of color2. Possible triples: need two triples. One approach: we could try to avoid triple of color1 alone. Since we have only two tables, we can give each table at most 2 of color1. With 4 color1 total, we need to allocate 2 per table exactly. Then the remaining spots: each table needs 1 more balloon. We have 2 color2, so we can assign one color2 to each table. That gives tables: (2,1,0) each, which is valid. So works.
Another: t=3, a=5, b=4, c=0. sum=9, max=5, 2t=6, condition holds. Can we? We need 3 triples with only colors a and b. Each triple cannot be all a (but could be all b? Since we have 4 b, but all b would be 3 b in one table; we could have that? Actually the rule: three balloons attached to some table shouldn't have the same color. So all b is also forbidden. So no triple all same, regardless of color. So we cannot have a triple of all b either. So each table can have at most 2 of any color. With totals a=5, b=4, we need to distribute such that each table at most 2 of a and at most 2 of b. Is it possible? Let's try. Since total tables=3, each table capacity 3. Let xi = number of a in table i, yi = number of b. xi+yi=3, 0≤ xi,yi ≤2. We need sum xi=5, sum yi=4. Since each yi ≤2, maximum total y is 3*2=6, we are under. But need to satisfy both sums. Check possibilities: Sum xi=5, so average ~1.67, but need integer per table. Let's try assign: Table1: (2,1), Table2: (2,1), Table3: (1,2) gives xi=5, yi=4? Actually (2,1) gives y=1? No: (2 a, 1 b) => a=2,b=1. That table's b count is 1. So three tables: two tables (2,1) gives a=4,b=2; one table (1,2) gives a=1,b=2 => totals a=5,b=4. Works. So yes.
What about a=6, b=3, c=0, t=3? sum=9, max=6, 2t=6, condition holds. Can we? Need xi+yi=3, xi≤2, yi≤2. Sum xi=6, sum yi=3. Since each xi ≤2, max total xi = 6, so all tables must have xi=2 (since sum xi=6 exactly, to get 6 over 3 tables, each must be 2). Then yi = 1 each, sum yi=3, works. So (2,1) each table. Valid.
Now a=6, b=0, c=0, t=3? sum=6? Actually sum must be 3t=9, but a=6, we need 9 total, so not sum 9; but we need x+y+z=9 exactly. So if we only have a=6, we cannot make 9 balloons used; we would have to use other colors but they are 0. So need to adjust. So condition includes sum=3t. So we need to pick some used counts ≤ actual counts. But for feasibility of allocation given used counts, condition seems to be: max(x,y,z) ≤ 2t and x+y+z = 3t. I think that's sufficient.
But is it always? Consider t=4, suppose we want to use x=6, y=6, z=0? sum=12, max=6, 2t=8, condition holds. Need to partition into 4 triples each with no triple all same, only colors x and y. Each triple cannot have 3 of same color. So each triple can have at most 2 of each color. With totals 6 each, we need to assign to 4 triples. Is it possible? Let's try. Let each triple have a count of x (say a_i) and y_i = 3 - a_i, with a_i ∈ {0,1,2} (but 0 means 3 y? That would be 3 of y, which is forbidden; a_i=0 implies y_i=3, not allowed. Similarly a_i=3 not allowed. So a_i must be 1 or 2. Because if a_i=0 => 3 y; if a_i=3 => 3 x; both invalid. So each triple must have exactly either 1 x and 2 y, or 2 x and 1 y. So we need a_i ∈ {1,2} for all i. Sum of a_i = total x = 6. Over 4 tables, sum a_i=6, each a_i ∈ {1,2}. That's possible: three tables with a_i=2 and one table with a_i=1? That gives sum=2+2+2+1=7, too high. Or two tables with 2 and two tables with 1 gives sum=2+2+1+1=6. So works: two tables (2x,1y) and two tables (1x,2y). So feasible.
What about t=5, x=9, y=6, z=0? sum=15, max=9, 2t=10, condition holds. Need to partition into 5 triples with a_i ∈ {1,2}? Actually if z=0, only x and y, each triple cannot be all x or all y. So a_i ∈ {1,2} for each i. Sum a_i = x =9. Over 5 tables, each a_i ∈ {1,2}. Minimum sum is 5 (all 1), maximum sum is 10 (all 2). Sum=9 is possible: four tables with 2 and one with 1 => sum=9. Then y_i = 3 - a_i, sum y_i = 15 - 9 =6. That matches. So works.
Consider t=3, x=4, y=4, z=1? sum=9, max=4, 2t=6, condition holds. Feasible? We have three colors. Might be possible. Let's test: We need to allocate 9 balloons to 3 tables with no triple all same. It seems plausible.
Consider a tricky case: t=2, x=2, y=2, z=2? sum=6, max=2, 2t=4, condition holds. That's actually 1 of each per table? Could be (1,1,1) each table. Works.
Thus seems condition is sufficient.
But we also must consider that we are not forced to use exactly these used counts; we can choose any x ≤ r, y ≤ g, z ≤ b such that the conditions hold and we want to maximize t. However, the problem is to find maximum t such that there exist nonnegative integers x, y, z with x ≤ r, y ≤ g, z ≤ b, x+y+z = 3t, and max(x,y,z) ≤ 2t. Since we can always choose to not use some balloons, the constraints are: we need to see if it's possible to pick such x,y,z. But maybe we can always use as many as possible? The maximum t would be achieved by using as many balloons as possible, but sometimes using all may violate max ≤ 2t. So we need to check if we can adjust by leaving some balloons unused.
So the problem reduces to: find the largest integer t such that there exist x,y,z with 0 ≤ x ≤ r, 0 ≤ y ≤ g, 0 ≤ z ≤ b, x+y+z = 3t, and x ≤ 2t, y ≤ 2t, z ≤ 2t.
We can try to combine these constraints to derive a simpler expression.
Let’s denote the actual counts r,g,b. We need to find max t integer satisfying:
-
3t ≤ r+g+b (since total used cannot exceed total available, but we could use less than total, but if 3t > total, impossible because we cannot have enough balloons overall, even if we use all. So t ≤ floor((r+g+b)/3).
-
For each color, we must be able to assign at most 2t from that color. Since we can choose to not use some, we just need that for each color, we have at least something? Actually the condition is: we need to find x ≤ r, y ≤ g, z ≤ b such that x ≤ 2t, y ≤ 2t, z ≤ 2t. But we could choose x less than or equal to min(r, 2t). So as long as we can pick x,y,z meeting sum 3t, we need that the total capacity of usable balloons from each color (i.e., min(r,2t) for that color) sum to at least 3t. Because we can take at most min(r,2t) from each color due to both availability and per-color max usage. Conversely, if sum_i min(r_i, 2t) >= 3t, then we can select x,y,z such that each ≤ min(r_i,2t) and sum exactly 3t (by some selection algorithm, e.g., we can greedily assign as much as possible up to min(r_i,2t) until we reach 3t). Since we have integer constraints, it's possible if sum of caps >= 3t. Also we need that we don't exceed the individual caps; but if sum caps >= 3t, we can pick numbers less than or equal to caps to sum to 3t (since they are nonnegative integers). This is always possible because we can take from the colors sequentially. For example, let cap1 = min(r,2t), cap2 = min(g,2t), cap3 = min(b,2t). If total_cap >= 3t, we can set x = min(cap1, 3t), then remaining = 3t - x, set y = min(cap2, remaining), then z = remaining - y (which will be ≤ cap3 automatically because total_cap >= 3t ensures z ≤ cap3? Not necessarily if we choose x and y poorly; but we can always arrange to take numbers such that each ≤ cap. Since caps are integers, we can always find a feasible triple because it's like we have three bins with capacities caps, and we need to fill total 3t. Feasibility condition for existence of nonnegative integers x ≤ cap1, y ≤ cap2, z ≤ cap3 with x+y+z = 3t is exactly that sum caps >= 3t and also each cap is not too large? Actually, there is no lower bound (except 0). So the only condition is sum caps >= 3t, because we can always set x = min(cap1, 3t), then if we need more, adjust. But is it possible that even if sum caps >= 3t, we cannot achieve exactly 3t because of integrality? Since caps are integers, we can achieve any integer between 0 and sum caps, because we can increment by 1 using one unit from a cap. More formally, the set of achievable sums from numbers each between 0 and cap_i inclusive is all integers from 0 to sum caps (since we can take any number from 0 to cap1, and then any from cap2, etc.; gaps do not occur because you can vary one while keeping others at max/min). Actually it's known that given two bins, the set of achievable sums is all integers from 0 to cap1+cap2 (since you can take k from first where k ≤ cap1, then remainder from second; but if remainder > cap2, you adjust). With three bins, also full range. The condition for a target T to be achievable with x_i ∈ [0, cap_i] is that 0 ≤ T ≤ sum caps and also T ≤ sum caps? That's it. Because you can start by taking x1 = min(cap1, T), then T1 = T - x1, then x2 = min(cap2, T1), then x3 = T - x1 - x2. Since x3 = T1 - x2, and we need to ensure x3 ≤ cap3. But if after taking x1 = min(cap1, T) and x2 = min(cap2, T1), it might happen that x3 > cap3 if caps1 and caps2 are small? However, we can choose different x1, x2 to avoid that. The condition for existence is exactly that T ≤ sum caps and also T ≥ max(0, T - (cap2+cap3)? Not necessary. Actually the necessary and sufficient condition for existence of nonnegative integers x_i ≤ cap_i with sum = T is that sum caps ≥ T and also T ≤ sum caps obviously, but there is also the condition that T ≥ (sum caps - max_cap?) Wait, we need to ensure that we are not forced to exceed any cap. This is equivalent to checking that for each i, we cannot have T > sum caps - cap_i, because if T > sum caps - cap_i, then even if we set the other two to their maximums, we would need more than cap_i from i. So necessary: T ≤ cap_i + (sum caps - cap_i) actually always. Let's derive: For any i, since x_i = T - sum_{j≠i} x_j, and sum_{j≠i} x_j ≤ sum_{j≠i} cap_j, we have x_i ≥ T - sum_{j≠i} cap_j. To have x_i ≥ 0 is automatically satisfied if T ≥ something? Actually we need x_i ≤ cap_i, so we need T - sum_{j≠i} cap_j ≤ cap_i, which is always true because T ≤ sum caps. Wait, we also need x_i ≥ 0: that requires T ≥ sum_{j≠i} x_j, but we can choose x_j as low as 0, so that's not restrictive. The key is that we need to ensure that we can pick x_i such that 0 ≤ x_i ≤ cap_i. The condition for existence is that T ≤ sum caps and also T ≥ 0? That's not enough. For example, caps: cap1=0, cap2=0, cap3=1. sum caps=1. Can we achieve T=1? Yes: x3=1 works. What about caps: cap1=1, cap2=1, cap3=0, sum caps=2. Can we achieve T=2? Yes: x1=1, x2=1. What about caps: cap1=2, cap2=0, cap3=0, sum caps=2. Can achieve T=2 via x1=2. So seems always possible? Consider caps: cap1=5, cap2=0, cap3=0, T=3: can do x1=3 ≤5, so yes. So any T between 0 and sum caps inclusive is achievable. Because you can simply allocate T to the first bin as long as T ≤ cap1; if not, allocate cap1 to first, then remainder to second if ≤ cap2, else allocate cap2, remainder to third. The only potential failure is if after allocating caps1 and caps2, the remainder exceeds cap3. But if sum caps ≥ T, that can't happen because after allocating full caps1 and caps2, remainder = T - cap1 - cap2. If that remainder > cap3, then T > cap1+cap2+cap3 = sum caps, contradiction. So the greedy algorithm works: allocate as much as possible to the first, then to second, then remainder to third. Since sum caps ≥ T, remainder ≤ cap3. So indeed any T with 0 ≤ T ≤ sum caps is achievable. So condition sum caps ≥ 3t is sufficient for existence of x,y,z meeting caps.
Thus necessary and sufficient condition for existence of a valid assignment of balloons to t tables (i.e., existence of used counts x,y,z meeting constraints) is:
- 3t ≤ r+g+b (since we need total balloons to at least 3t, but we could also leave some unused; however we cannot use more than total. So if 3t ≤ total, it's okay because we can use exactly 3t from total. But is it possible that 3t ≤ total but sum caps < 3t? Because caps = min(r,2t) + min(g,2t) + min(b,2t). If sum caps < 3t, then even though total balloons may be enough, we cannot select 3t balloons without violating the per-color max 2t constraint. So condition is sum caps ≥ 3t.
But note that 3t ≤ total is automatically implied by sum caps ≥ 3t? Not necessarily, because caps are min(r,2t) etc. Sum caps could be less than total if some colors exceed 2t. For example, r=100, g=0, b=0, total=100. For t=10, caps = min(100,20)+0+0 =20, sum caps=20 < 30 = 3t. So sum caps < 3t, even though total=100 ≥30. So sum caps condition is stronger. However, we also need that we can pick 3t balloons from the available ones, which requires total ≥ 3t. But sum caps ≥ 3t already ensures total ≥ 3t? Not necessarily: caps are min(r,2t) ≤ r, etc., so sum caps ≤ total. So sum caps ≥ 3t implies total ≥ sum caps ≥ 3t, so total ≥ 3t automatically. So the only condition needed is sum_{c∈{r,g,b}} min(c, 2t) ≥ 3t.
Therefore, maximum t is the largest integer such that sum(min(r,2t), min(g,2t), min(b,2t)) ≥ 3t.
Now we need to compute this efficiently given r,g,b up to 2e9.
We can attempt to find t via binary search. t can be at most floor((r+g+b)/3). Also note that t cannot exceed r+g+b, but also note that t is at most something like (r+g+b) but that's fine.
Upper bound for binary search: hi = floor((r+g+b)/3). Or perhaps a bit larger? Actually t cannot exceed total/3, but also cannot exceed something else? Let's check extreme: r=2e9, g=0, b=0, total=2e9, t max? To have tables, we need three balloons per table, no all same. With only red, we cannot make any table because any triple would be all red. So t=0. But floor(total/3) = 666,666,666, which is far larger. So we need to use condition sum min(r,2t) etc. So binary search up to floor(total/3) is safe, because condition will fail for large t. But we might also consider that t cannot exceed (r+g+b) obviously. So hi = floor((r+g+b)/3) is a valid upper bound; the answer t satisfies t ≤ floor(total/3). So we can binary search in [0, floor(total/3)] inclusive.
But careful: In the example r=2e9, g=0,b=0, total/3 huge, but the condition fails for any t>0 because sum min = min(2e9,2t) which is 2t for t less than 1e9? Actually if t>0, sum min = 2t (since g and b are 0), then 2t ≥ 3t? That would require 2t >= 3t => t <=0. So fails for t>=1. So answer 0. Binary search works.
Thus we can binary search t. Complexity O(log(total)).
Alternatively, there might be a direct formula.
Let's analyze condition:
Let r,g,b sorted maybe. Let m1 ≤ m2 ≤ m3 be sorted values.
Condition: min(r,2t) + min(g,2t) + min(b,2t) ≥ 3t.
If the largest color is > 2t, then min of that is 2t, and the others may be ≤ 2t. So condition becomes: if all three ≤ 2t, then sum = r+g+b ≥ 3t. So condition reduces to r+g+b ≥ 3t. But if r+g+b ≥ 3t and all are ≤ 2t, that's automatically satisfied? Not exactly; if all ≤ 2t, the condition is exactly total ≥ 3t. So in that region, t ≤ floor(total/3). But also we need that each ≤ 2t, which is equivalent to t ≥ ceil(max/2). So for t >= ceil(max/2), condition becomes total >= 3t.
For t less than ceil(max/2), the largest color is capped at 2t, so condition is (capped sum) >= 3t.
Thus the maximum t is the maximum integer satisfying either t >= ceil(max/2) and total >= 3t, or t < ceil(max/2) and capped sum >= 3t.
But we can combine: The maximum t is min( floor(total/3), something else?). Let's think.
Consider the classic problem: Given piles of balls, you want to form triples with no three same. I recall a known solution: t = min( (r+g+b)/3, (r+g+b - max(r,g,b) )? Actually there is a known problem: "Balloons" or "Decoration" maybe. Let's test with examples.
Example1: r=5,g=4,b=3 => total=12, total/3=4, max=5, total-max=7? (r+g+b - max) =7, half? Not sure. min(total/3, total-max) would be min(4,7)=4. Output 4. Works.
Example2: 1 1 1 => total=3, total/3=1, total-max=2, min(1,2)=1. Works.
Example3: 2 3 3 => total=8, total/3=2.666 floor=2, total-max=5? max=3, total-max=5, min(2,5)=2. Works.
Now test r=5,g=0,b=0: total=5, total/3=1, total-max=0, min=0 => answer 0, correct.
r=100,g=0,b=0 => total=100, floor/3=33, total-max=0, min=0 => 0.
r=4,g=4,b=0: total=8, floor/3=2, total-max=4, min(2,4)=2. Can we make 2 tables? With 4 red, 4 green, 0 blue. Each table can't be all same. Two tables require 6 balloons. We have 8 total. Can we form two tables? For each table, we need to avoid 3 red or 3 green. Since we have only two tables, we could allocate each table: 2 red + 1 green and 2 green + 1 red, using 3 red and 3 green? Actually 2 red+1 green uses 2 red,1 green; the other table 1 red+2 green uses 1 red,2 green; total red=3, green=3, leaving 1 red and 1 green unused. So yes 2 tables possible. So answer 2 matches.
r=5,g=5,b=0: total=10, floor/3=3, total-max=5, min(3,5)=3. Can we do 3 tables? We need 9 balloons. With 5 red,5 green. Need to form 3 triples with no all same. Let's try: Table1: 2r+1g, Table2: 2r+1g (that would use 4r,2g) leaves 1r,3g. Table3: need 1r+2g? That uses 1r,2g total r=5,g=5? Actually with our plan: first two tables each 2r+1g uses 4r,2g. Remaining 1r,3g. Table3 could be 1r+2g, uses 1r,2g, leaving 1g unused. So yes 3 tables. So answer 3.
r=5,g=5,b=1: total=11, floor/3=3, total-max=6 (max=5? Actually max=5, total-max=6), min(3,6)=3. Could we do 3 tables? Need 9 balloons. With 5,5,1. Let's attempt: Use blue balloon maybe. We could do tables: (2r,1g), (2g,1r), (1r,1g,1b) – totals: r:2+1+1=4, g:1+2+1=4, b:1. That's 4,4,1 using 9 balloons. So yes 3. So formula holds.
r=6,g=6,b=0: total=12, floor/3=4, total-max=6, min(4,6)=4. 4 tables? Need 12 balloons. Only red and green. Can we do 4 tables with no triple all same? Each table must be either 2r+1g or 1r+2g, with at most 2 of each per table. We need total red=6, green=6. Let's try assign a_i (red count per table) ∈ {1,2}, sum a_i =6 over 4 tables. Each a_i must be 1 or 2. The sum of four numbers each 1 or 2 can range from 4 to 8. To get 6, we need two 2s and two 1s: a = [2,2,1,1] sum=6. Then green counts = 3 - a_i = [1,1,2,2] sum=6. Works. So 4 tables. So formula holds.
Now r=2,g=2,b=2: total=6, floor/3=2, total-max=4, min(2,4)=2, works.
Now consider case where one color is huge, but others moderate: r=10,g=5,b=5. total=20, floor/3=6, total-max=10, min(6,10)=6. Can we do 6 tables? Need 18 balloons. We have 10,5,5. Each table at most 2 of any color, so red max usable 2*6=12, we have 10, so red ok. Others 5 ≤ 12. Condition sum(min) for t=6: min(10,12)=10, min(5,12)=5 each, sum=20 >=18. So condition holds. Should be feasible. Let's try construct: We need to use 18 balloons. We can use all 10 red, all 5 green, all 5 blue? That sums to 20, but we need only 18, so we leave 2 maybe. But we must use exactly 18. Since sum caps =20 ≥18, we can choose to not use 2 balloons. Let's try see if we can allocate 6 tables. Since red is abundant, we need to ensure no table gets 3 red. So each table at most 2 red. To use 10 red over 6 tables, we need distribution: sum red =10, each table ≤2. That's possible: 4 tables with 2 red, 2 tables with 1 red (for example). Then remaining slots per table filled with green/blue. Need total green+blue = 8 (since total balloons 18, red 10, so 8 others). We have 10 green+blue available, we can use 8. So possible. So 6 seems okay.
Now consider r=9,g=9,b=0: total=18, floor/3=6, total-max=9, min(6,9)=6. Already done similar.
Now test a tricky boundary: r=1,g=1,b=100. total=102, floor/3=34, total-max=2, min(34,2)=2. So answer 2. Let's test manually: With 100 blue, 1 red, 1 green, we can make at most 2 tables? Because each table needs 3 balloons, cannot be all same. So the limiting factor is the number of non-blue balloons? Since we have only 1 red and 1 green, we can at most make tables that avoid all-blue. To avoid all-blue, each table must have at least one non-blue. Since only 2 non-blue total, we can have at most 2 tables (each using at least one non-blue). After using both non-blue, we could fill remaining spots with blue, but careful: each table can have at most 2 blue? Actually if a table has two blue and one non-blue, that's okay. So with 2 tables: Table1: 1 red + 2 blue? but we have only 1 red, so that uses red; Table2: 1 green + 2 blue. That uses both non-blue and 4 blue, total balloons 6. So 2 tables is feasible. Could we make 3 tables? To have 3 tables, we would need at least 3 non-blue balloons (since each table must contain at least one non-blue to avoid all-blue). But we have only 2, so impossible. So answer 2 matches min(total/3=34, total-max=2) =2.
Thus the formula t = min( floor((r+g+b)/3), (r+g+b) - max(r,g,b) ) seems to produce correct answers for these tests. However, is it always correct? Let's verify with more cases.
We need to ensure that the condition sum min(c,2t) >= 3t is equivalent to t <= floor((r+g+b)/3) and t <= (r+g+b) - max(r,g,b)? Actually if we take t = min( floor(S/3), S - M ), where S = r+g+b, M = max(r,g,b). Is that always the maximal t? Let's test.
Suppose r=5,g=4,b=3: S=12, floor(S/3)=4, M=5, S-M=7, min=4 => t=4.
Suppose r=5,g=5,b=5: S=15, floor=5, S-M=10, min=5 => t=5, obviously possible (all distinct each table? Actually we can make 5 tables each with one of each color? That uses 5 each color, total 15, works. So t=5.)
Suppose r=0,g=0,b=5: S=5, floor=1, S-M=0, min=0 => t=0. Indeed impossible because need 3 balloons, all blue would be invalid. So t=0.
Suppose r=2,g=2,b=3: S=7, floor=2, M=3, S-M=4, min=2 => t=2. Check if 2 tables possible? Need 6 balloons. We have 2,2,3. Could we make 2 tables without all same? Let's try: one possible distribution: Table1: 1r+1g+1b; Table2: 1r+1g+1b? That would use 2r,2g,2b, leaving 1 blue unused. Works. So t=2.
Suppose r=4,g=2,b=2: S=8, floor=2, M=4, S-M=4, min=2 => t=2. Check: need 6 balloons. Use 4r,1g,1b? But that would give tables? Let's try to allocate: we have 4r,2g,2b. Need 2 tables. Each table cannot be all r. So we need to ensure each table has at most 2r. With total r=4, we could give each table 2r. Then remaining spots: each table needs 1 more balloon; we have 2g and 2b, so we can give one table g and one table b, for example. That yields tables: (2r,1g) and (2r,1b). That uses 4r,1g,1b, leaving 1g,1b unused. So t=2 works. Could t=3? floor=2, so not considered.
Suppose r=6,g=6,b=1: S=13, floor=4, M=6, S-M=7, min=4 => t=4. Check: need 12 balloons. We have 6,6,1. Can we make 4 tables without all same? We need to use at most 2r per table, total r=6, so need exactly 2r per table? Actually if we have 4 tables, max r per table 2, to use all 6 red, we need distribution: maybe 2,2,2,0? But 0 would mean that table has 0 red, but then it must have 3 other balloons? It could have 3 green? That would be all green, which is forbidden. So each table must have at least one non-red? Actually not necessarily: If a table has 0 red, it could have 2 green and 1 blue, or 1 green and 2 blue, etc. But it cannot have 3 green because that's all same (green). Since we have only one blue, we have to be careful. Let's attempt to construct: We have 6 red, 6 green, 1 blue. We need 4 tables, total balloons used =12. Since we have only 1 blue, we rely on red and green. Each table must avoid all-red and all-green. So for each table, if it has 0 red, then it must have at most 2 green and at least 1 non-green (i.e., blue or red) but if 0 red, only green and blue left; but we only have one blue, so at most one table can have blue. So other tables without blue must have at least 1 red to avoid all-green, and also at most 2 red to avoid all-red. So they will have mix of red and green. So let's try to design: We need to use 12 balloons; we have 13 total, so we will leave 1 unused. Could leave one green unused maybe.
We need to assign per table (r_i, g_i, b_i) where r_i+g_i+b_i=3, 0 ≤ each ≤2 except cannot have 3 of same color. So allowed patterns: (2,1,0), (2,0,1), (1,2,0), (0,2,1), (1,0,2), (0,1,2), (1,1,1). Also (0,0,3) etc not allowed. So we have at most 2 of any color per table.
We have b=1, so at most one table can have a blue balloon (since each table can have at most 2, but we have only 1). So exactly one table uses the blue, or we may not use it? We can choose to not use it. But perhaps using it helps. Let's try to use the blue: that table can be e.g., (2r,0g,1b) or (1r,1g,1b) or (0r,2g,1b) etc.
We need total red= some number ≤6, green ≤6.
Goal is to see if we can achieve 4 tables.
We can attempt to set three tables without blue: they must have mix of red and green, each with at most 2 of each. And the table with blue can also have red and green.
Let's denote we will use x red, y green, and the 1 blue. The totals used: red used = R_used, green used = G_used, blue used =1. And R_used+G_used+1=12 => R_used+G_used=11. Since we have max 6 each, the only possibility is R_used=6, G_used=5 (or 5 and 6). Because 6+5=11. So we must use all 6 red and 5 green (leaving 1 green unused). That seems plausible.
Now we need to partition these into 4 tables, one includes the blue. Let the blue table have composition (r_b, g_b, 1). Then remaining tables (3 tables) use red_total = 6 - r_b, green_total = 5 - g_b. And these three tables have no blue.
We need to see if we can partition the remaining red and green into three tables each with r_i+g_i=3, r_i,g_i ∈ {0,1,2} but not (0,3) or (3,0). So for each of these three tables, we need r_i between 1 and 2 inclusive? Actually if r_i=0, then g_i=3 which is invalid (all green). If g_i=0, then r_i=3 invalid. So for tables without blue, both r_i and g_i must be at least 1? Not necessarily; could be (2,1) or (1,2) only. Because if (2,0,1) has blue; without blue, the only allowed patterns with two colors are (2,1,0) and (1,2,0). So yes, each non-blue table must have exactly 2 of one color and 1 of the other. So each such table uses either (2r,1g) or (1r,2g). Therefore, for the three non-blue tables, the sum of reds is either 2+2+2=6, or 2+2+1=5, etc. Actually each table contributes either 2 or 1 red. So total red from non-blue tables is an integer between 31=3 and 32=6, but also sum of reds plus sum of greens = 9 (since 3 tables *3 =9). And each table's red+green=3. So if a table has r red, then g = 3 - r. So total red = R_non, total green = 9 - R_non. R_non can be 3,4,5,6. Correspondingly green = 6,5,4,3.
Now we also have the blue table: it can be (2,0,1) => r_b=2,g_b=0; or (1,1,1) => r_b=1,g_b=1; or (0,2,1) => r_b=0,g_b=2; or (2,? Actually (2,0,1) and (0,2,1) are valid; (2,0,1) uses 2 red, 0 green; (0,2,1) uses 0 red, 2 green; (1,1,1) uses 1 each. Also (1,0,2) but blue is 2? Not allowed since only 1 blue total, but a table could have 2 blue if we had more, but we have only 1, so not possible. Also (0,1,2) not possible. So blue table options: r_b ∈ {0,1,2}, g_b ∈ {0,1,2} with the condition r_b + g_b = 2? Actually if we have 1 blue balloon, the table has 3 balloons: so r_b + g_b + 1 = 3 => r_b+g_b =2. So r_b,g_b are nonnegative integers summing to 2. But also must avoid triple all same, which is already avoided because blue is only 1, so no problem. However, need to ensure that the table does not have 3 of red or 3 of green, but with r_b=3 impossible because sum is 2. So all pairs (r_b,g_b) with r_b+g_b=2 are allowed: (2,0), (1,1), (0,2). Good.
Now total red used = r_b + R_non =6, total green used = g_b + (9 - R_non) =5 (since we need total green used 5). So we have:
r_b + R_non = 6
g_b + (9 - R_non) = 5 => g_b = 5 - (9 - R_non) = R_non - 4.
Also g_b must be between 0 and 2 inclusive, and r_b = 2 - g_b (since r_b+g_b=2). Also R_non must be between 3 and 6.
From the second equation: g_b = R_non - 4. So possible R_non values: 4 => g_b=0, acceptable; 5 => g_b=1, acceptable; 6 => g_b=2, acceptable; 3 => g_b=-1 invalid. So R_non can be 4,5,6. Then r_b = 2 - g_b.
Case R_non=4 => g_b=0 => r_b=2. Then check first equation: r_b + R_non =2+4=6, works. So we have a solution: blue table: (2r,0g,1b); non-blue tables: total red =4, total green =5? Wait from R_non=4, total green from non-blue tables = 9 - R_non =5. Then overall green used = g_b + 5 =0+5=5, matches. So we need to partition 4 red and 5 green into three tables each of type (2r,1g) or (1r,2g). That means we need to find three numbers r_i ∈ {1,2} summing to 4, with each table's green = 3 - r_i summing to 5. Let's check possible: sum of r_i =4 over three tables; possibilities: (2,1,1) sum=4. That corresponds to green: (1,2,2) sum=5. So yes. So tables: one (2r,1g) and two (1r,2g). This works. So t=4 is feasible. So formula holds.
Now test r=8,g=8,b=1: S=17, floor=5, M=8, S-M=9, min=5 => t=5. Can we? Need 15 balloons. Have 8,8,1. We need to use 15 balloons: maybe all red and green plus the blue? Let's see if possible. Max red per table 2, so with 5 tables max red usable =10, we have 8, okay. Similarly green. Use blue in one table. Then we need to use total red+green=14. Since we have 16 total red+green, we leave 2 unused. Need to partition red+green into 5 tables, but one table includes blue, so 4 tables without blue and one with blue. Let's check feasibility: Let blue table have (r_b,g_b,1) with r_b+g_b=2. Then remaining 4 tables use red = 8 - r_b, green = 8 - g_b. These 4 tables must be of type (2,1) or (1,2) each, as before. Their total balloons = 12, red+green sum = (8 - r_b)+(8 - g_b)=16 - (r_b+g_b)=14. So total red+green in 4 tables is 14, which matches 12? Wait 4 tables each have 3 balloons =12 total. But the sum of red+green over those 4 tables is exactly the number of red and green balls used, which must be 12 because each table uses 3 balloons and none have blue (since only one blue used in the other table). So we need (8 - r_b)+(8 - g_b) = 12 => 16 - (r_b+g_b)=12 => r_b+g_b=4. But we have r_b+g_b=2 from the blue table's composition (since it has 1 blue and 2 others). Contradiction. So we cannot use all 4 tables without blue; we must have some blue in other tables? But we have only one blue. Alternatively, we might not use the blue? If we don't use blue, then we have 5 tables without any blue. Then total red+green needed =15. But we have 16 available. Need to use 15 from red+green. Each table must be (2,1) or (1,2). So for 5 tables, let R_total = sum r_i, G_total = sum g_i, with each r_i ∈ {1,2}, g_i=3 - r_i. Then R_total + G_total =15, and G_total = 15 - R_total. But also each g_i = 3 - r_i, so G_total = 5*3 - R_total =15 - R_total, consistent. So any R_total between 5 and 10 is possible? Actually r_i ∈ {1,2} so sum R_total can be any integer from 5 to 10? Minimum all 1's: 5, maximum all 2's: 10. So we need to select R_total such that R_total ≤ 8 (since we have only 8 red) and G_total = 15 - R_total ≤ 8 (since only 8 green). So we need R_total between 7 and 8? Because if R_total=7, then G_total=8, that's ≤8; if R_total=8, G_total=7 ≤8. So R_total could be 7 or 8. But can we achieve R_total=7 with 5 numbers each 1 or 2? Yes, e.g., 2,2,1,1,1 sum=7. That uses red 7, green 8. That's within limits. So we could use 7 red and 8 green, total 15, leaving 1 red unused. That yields 5 tables with no blue. So t=5 is feasible without using blue. So answer 5 works.
Thus formula still holds.
Now test r=0,g=0,b=2: S=2, floor=0, S-M=0, min=0 -> t=0. Indeed can't make a table because need 3 balloons.
Now test r=2,g=2,b=2: we did.
Now test r=3,g=3,b=3: S=9, floor=3, S-M=6, min=3 -> t=3.
Now test r=1e9, g=1e9, b=1e9 -> t = min( (3e9)/3 = 1e9, 3e9 - 1e9 = 2e9 ) = 1e9. So t = 1e9, which is correct? We have total 3e9 balloons, we can make 1e9 tables each with one of each color. So works.
Now test r=2e9, g=2e9, b=0: S=4e9, floor=1.333e9? Actually 4e9/3 = 1.333...e9 floor = 1333333333? But 4e9 is 4,000,000,000? Actually 2e9 is 2,000,000,000, so sum = 4,000,000,000, floor(4e9/3) = 1,333,333,333 (since 3*1,333,333,333 = 3,999,999,999). And S-M = 4e9 - 2e9 = 2e9. min = 1,333,333,333. So t = 1,333,333,333. Is that achievable? We need to check condition: t = 1333333333. Then 2t = 2,666,666,666 which is > r and g (2e9). So condition sum min = min(r,2t)+min(g,2t)+min(b,2t) = 2e9+2e9+0 = 4e9, and 3t = 3,999,999,999. So 4e9 >= 3,999,999,999, condition holds. So t feasible. Can we construct? With only red and green, each table must be (2,1) or (1,2). We need to use 3t = 3,999,999,999 balloons. But total available is 4e9, so we leave 1 balloon unused. We need to allocate red and green such that total used red+green = 3,999,999,999, and each table uses either 2 red+1 green or 1 red+2 green. Over t tables, let x be number of tables with 2 red, 1 green; then t-x tables with 1 red,2 green. Then red used = 2x + (t-x) = x + t; green used = x + 2(t-x) = 2t - x. Total used = (x+t)+(2t-x) = 3t. So any x from 0 to t yields a valid distribution as long as red used ≤ 2e9 and green used ≤ 2e9. So we need x+t ≤ 2e9 and 2t - x ≤ 2e9. For t=1,333,333,333, x+t ≤ 2e9 => x ≤ 2e9 - t = 666,666,667. And 2t - x ≤ 2e9 => x ≥ 2t - 2e9 = 2,666,666,666 - 2e9 = 666,666,666. So x must satisfy 666,666,666 ≤ x ≤ 666,666,667. Since x is integer, there are two possible x values. So feasible. So t works.
Thus the formula seems plausible.
But is it always exactly min( floor(S/3), S - M )? Let's test some edge cases where maybe the condition sum min(c,2t) >= 3t yields a larger t than that min? Could it be that t can be larger than S - M? For t = S - M, is condition always satisfied? Let's check: Suppose t = S - M. Then we need to check if sum min(c,2t) >= 3t. Write S = r+g+b, M = max. Without loss assume r ≥ g ≥ b? Actually let's denote M = max. Consider two cases: If M is large, t = S - M may be relatively small. Let's test r=10, g=1, b=1 => S=12, M=10, S-M=2, floor(S/3)=4, min=2. So formula gives t=2. Is t=2 feasible? We have 10 red,1 green,1 blue. Need 6 balloons. Could we make 2 tables? Each table must avoid all red. So each table can have at most 2 red. To use 2 tables, max red usable = 4, we have 10 red but we can leave some unused. We need to select 6 balloons such that no table all red. We could use 2 red + 1 green for one table, 2 red + 1 blue for second table. That uses 4 red,1 green,1 blue. So t=2 works. Could we do t=3? That would need 9 balloons. To avoid all-red, each table can have at most 2 red, so maximum red we could use across 3 tables is 6. To get to 9 balloons, we would need at least 3 non-red balloons (since total red used ≤6, need at least 3 other colors). But we only have 2 non-red balloons (green+blue=2). So impossible. So t=2 is max. So formula correct.
Now consider r=5, g=4, b=4? Actually let's test r=5,g=5,b=2: S=12, floor=4, M=5, S-M=7, min=4. So t=4. Feasible? Need 12 balloons. Have 5,5,2. Check condition: sum min for t=4: min(5,8)=5, min(5,8)=5, min(2,8)=2 sum=12 >=12, ok. So feasible. Try constructing: Need to use all balloons? Possibly 4 tables. Let's attempt: Use all balloons. We need to allocate 4 tables. Since blue is only 2, we need to distribute. Let's try to find composition. Let x be number of tables with blue. Since blue total=2, and each table can have at most 2 blue, but we have only 2 blue, so maybe 2 tables have 1 blue each? Could also have one table with 2 blue and others 0, but if a table has 2 blue, then the third balloon must not be blue to avoid all-blue, so it's okay. But then the table with 2 blue would use 2 blue, leaving none for others. Both options possible.
We need to see if 4 tables possible: Let's try to use all balloons. Suppose we use all 5 red,5 green,2 blue. Need to partition into 4 triples.
We can approach with linear equations: Let a = number of tables of type (r,g,b) i.e., 1 each; b = number with (2r,1g); c = number with (2r,1b); d = (1r,2g); e = (1r,2b); f = (2g,1b); g = (1g,2b); and also types with two of one and one of another, but we must account all. Also types with 2 of a color and 1 of another. But maybe we can solve simpler: Since we have only 2 blue, the tables that include blue can only have at most 2 blue total. So we have at most 2 tables with blue (if each uses 1) or 1 table with 2 blue. The rest tables are only red+green. Let's try with 2 tables having blue: each uses 1 blue. Then those two tables each have 2 other balloons (from red and green). The remaining two tables have only red+green. For the blue tables, each is (r_i, g_i, 1) with r_i+g_i=2, and each r_i,g_i ∈ {0,1,2} but sum 2. Possibilities: (2,0), (1,1), (0,2). For the non-blue tables, each is either (2r,1g) or (1r,2g). Let total red used = R, green used = G, blue used =2. Need R+G+2=12 => R+G=10. We have red total 5, green total 5, so R ≤5, G ≤5, and R+G=10 => R=5,G=5. So we must use all red and green. So R=5,G=5.
Now let the blue tables have (r1,g1,1) and (r2,g2,1) with r1+g1=2, r2+g2=2. Then red used in blue tables = r1+r2, green used = g1+g2. The remaining red and green for non-blue tables: R_rem =5 - (r1+r2), G_rem =5 - (g1+g2). Those must be allocated to two tables of type (2r,1g) or (1r,2g). For two tables, the red sum can be either: if both are (2,1): red=4; if one (2,1) and one (1,2): red=3; if both (1,2): red=2. Similarly green sum = 6 - red (since two tables total balloons 6, red+green=6). So we need R_rem to be one of {2,3,4} and G_rem = 6 - R_rem.
Now compute r1+r2 + g1+g2 = (r1+g1)+(r2+g2) = 2+2=4. So sum of red and green used in blue tables =4. Therefore R_rem + G_rem = (5+5) - 4 = 6, which matches total needed for two tables (6). So that's consistent. Now we need to find integers r1,r2,g1,g2 with r1+g1=2, r2+g2=2 such that R_rem = 5 - (r1+r2) is in {2,3,4} and G_rem = 5 - (g1+g2) = 5 - (2 - r1 + 2 - r2) = 5 - (4 - (r1+r2)) = 1 + (r1+r2). Actually compute: g1 = 2 - r1, g2 = 2 - r2, so g1+g2 = 4 - (r1+r2). Then G_rem = 5 - (4 - (r1+r2)) = 1 + (r1+r2). And R_rem = 5 - (r1+r2). So we have R_rem + G_rem = 6 automatically. For the two non-blue tables, we need R_rem to be 2,3, or 4. So 5 - (r1+r2) ∈ {2,3,4} => (r1+r2) ∈ {1,2,3}. But r1 and r2 are integers between 0 and 2, so r1+r2 can be 0,1,2,3,4. So 1,2,3 are possible. Also we need G_rem = 1 + (r1+r2) to be ≤5 (always). Also we need that the partition into two tables with those totals is possible: for two tables, it is possible if R_rem is between 2 and 4 inclusive, and also the individual tables require each to have at least 1 red and 1 green? Actually each non-blue table must be either (2,1) or (1,2). So for two tables, the set of possible (R_rem) values are exactly {2,3,4} (since if both (1,2) => R_rem=2; one each => R_rem=3; both (2,1) => R_rem=4). So condition satisfied. So we need r1+r2 ∈ {1,2,3}. That's possible: e.g., r1=1,r2=1 gives r1+r2=2 => R_rem=3, G_rem=3. Then blue tables: (1,1,1) each? Actually if r1=1,g1=1, r2=1,g2=1, then each blue table is (1,1,1) which uses 1 blue each, okay. Then remaining red=3, green=3 for two tables: can be one (2,1) and one (1,2) giving red=3, green=3. So works. Thus t=4 feasible.
Thus formula holds.
Now consider r=100, g=50, b=50: S=200, floor=66, M=100, S-M=100, min=66 => t=66. Check sum min: for t=66, 2t=132 > all, so min each = actual, sum=200 >= 198, works. So feasible.
Now test r=100, g=50, b=0: S=150, floor=50, M=100, S-M=50, min=50 => t=50. Can we make 50 tables? Need 150 balloons, but we have total 150 exactly. Only red and green. We need to use all balloons? Possibly. Each table must be either (2r,1g) or (1r,2g). Let x tables of type (2r,1g), then red used = 2x + (50-x) = x+50, green used = x + 2(50-x) = 100 - x. We have red total 100, green total 50. So we need x+50 = 100 => x=50, and 100 - x = 50 => x=50, consistent. So all tables must be (2r,1g). That uses 2 red and 1 green per table, 50 tables => 100 red, 50 green. That's valid because each table has 2 red,1 green, not all same. So t=50 works. Good.
Now test r=100, g=51, b=0: S=151, floor=50, M=100, S-M=51, min=50 => t=50. Need 150 balloons. Use all red? We have 100 red, 51 green. We need to use 150 balloons, so we will leave 1 green unused. For 50 tables, we need to assign red and green such that each table not all same. Let x tables (2r,1g), 50-x tables (1r,2g). Then red used = 2x + (50-x) = x+50, green used = x + 2(50-x) = 100 - x. We have red max 100, green max 51. So we need x+50 ≤ 100 => x≤50; 100-x ≤ 51 => x≥49. So x ∈ {49,50}. If x=50, red=100, green=50, but we have 51 green, we could leave 1 green unused. That's fine. If x=49, red=99, green=51, leave 1 red unused. Both possible. So t=50 works. Could t=51? floor(S/3)=50, so not considered. So max t=50.
Now test a case where S - M is less than floor(S/3) but maybe t could be higher than S - M? The formula says t = min( floor(S/3), S-M ). If S-M is the limiting factor, t = S-M. Could there be a scenario where we can achieve more than S-M? Let's test with r=5,g=5,b=4: S=14, floor=4, M=5, S-M=9, min=4 -> t=4. So not limiting.
Consider r=6,g=6,b=1: S=13, floor=4, M=6, S-M=7, min=4 -> t=4. So S-M not limiting.
Consider r=6,g=6,b=2: S=14, floor=4, M=6, S-M=8, min=4.
Now consider r=2,g=2,b=2: S=6, floor=2, M=2, S-M=4, min=2.
Now consider r=0,g=1,b=2: S=3, floor=1, M=2, S-M=1, min=1. Can we make t=1? Yes, use (0,1,2) but that's 2 blue and 1 green? That's 2 blue and 1 green: allowed? That's 2 blue, 1 green -> not all same. So t=1 works. Could we make t=1? Yes.
Now consider r=0,g=1,b=3: S=4, floor=1, M=3, S-M=1, min=1 -> t=1. Can we make 1 table? Yes, e.g., (0,1,2) uses 2 blue and 1 green, leaving 1 blue unused. So t=1. Could we make t=2? floor=1, so no.
Now consider r=1,g=1,b=3: S=5, floor=1, M=3, S-M=2, min=1 -> t=1. Could t=2? floor=1, so no.
All seem consistent.
But I want to test a case where S-M is very small, but maybe we can exceed it by using some tables with 2 of the dominant color and 1 of another, and we have enough of the dominant color? Actually S-M is total of the two smaller colors. If you have huge dominant color, the limiting factor is the sum of the other two because each table needs at least one balloon from the non-dominant colors to avoid all-dominant? Not exactly: It is possible to have a table with no non-dominant color? That would be all dominant, which is forbidden. So indeed each table must contain at least one balloon from the non-dominant colors combined? Actually if we have two colors, say dominant red and other green, then each table must contain at least one green to avoid all-red. So the number of tables is limited by the total number of green balloons (since each table needs at least one green). But if we have also blue, then a table could avoid red by using green and blue, but still it must have at least one non-red. So overall, each table must contain at least one balloon from the set of colors that are not the dominant? Actually if one color is extremely dominant, you could still have tables that use only the other two colors, but those tables also need to avoid being all same among those two. For example, if we have many red, and some green and blue, each table could have either: at least one non-red. So the total number of tables cannot exceed the total number of non-red balloons? Not exactly: A table could use 2 red and 1 green, that uses 1 non-red. So each table consumes at least one non-red balloon. So total tables ≤ total non-red balloons (since each table needs at least one non-red). Similarly, if two colors are limited, the maximum tables is bounded by the sum of the two smaller colors? Let's derive: Let M = max(r,g,b). Then the total number of balloons from the other two colors is S - M. Each table must contain at least one balloon from these two colors because if a table had only balloons from the max color, it would be all same, forbidden. So each table consumes at least one balloon from the set of colors that are not the maximum. Therefore, t ≤ S - M. Also obviously t ≤ floor(S/3). So t ≤ min(floor(S/3), S - M). This provides an upper bound.
Now the question: is this upper bound always achievable? That is, can we always achieve t = min(floor(S/3), S - M)? If yes, then answer is that min. We have tested many examples, and it seems achievable. But we should be thorough. This is a known problem: Codeforces problem "Balloons" maybe? Actually I recall a problem "A. Balloons" or "B. Balloons" where the answer is min(total/3, sum of two smallest) maybe. There's a known problem: "A. Decorating The Table" or something? Possibly from Codeforces Round? But let's reason.
We need to prove that for any nonnegative integers r,g,b, the maximum t = min( floor((r+g+b)/3), (r+g+b) - max(r,g,b) ). Or equivalently, t = min( floor(S/3), S - M ). I've also seen alternative: t = min( floor(S/3), S - M ). That seems plausible.
But we must ensure that when S - M is the limiting factor, we can achieve t = S - M, even if S - M may be greater than floor(S/3)? Actually S - M can be greater than floor(S/3) only if the max is less than 2/3 of total? Let's check: S - M > S/3 implies M < 2S/3. For example, r=5,g=4,b=3: S=12, M=5, S-M=7 > 4 (floor(S/3)=4). So min is floor(S/3). So the bound floor(S/3) can be smaller. In cases where S-M is smaller, it becomes the limiting factor.
So the answer is the smaller of the two.
Now we should confirm that when S-M is smaller than floor(S/3), we can indeed achieve t = S-M. That is, when the sum of the two smaller colors is the bottleneck, we can achieve exactly that many tables.
Consider r=100, g=1, b=1: S=102, floor(S/3)=34, S-M=2, min=2. Claim t=2. Is it achievable? Yes, we argued earlier. Could we possibly do t=3? That would need at least 3 non-red balloons, but we have only 2, so impossible. So max is 2.
Now consider r=100, g=2, b=1: S=103, floor=34, S-M=3? M=100, S-M=3, min=3. Can we do t=3? Need 9 balloons. We have 100 red,2 green,1 blue. We need each table to have at least one non-red. Total non-red = 3, so at most 3 tables. So t ≤ 3. Is t=3 feasible? Need to allocate 9 balloons, with at least one non-red per table, and not all same per table. Let's try: We'll use all non-red (2 green,1 blue) and 6 red (since total used = 9, non-red=3, so red used=6). Each table must have at most 2 red (to avoid all-red). With 3 tables, to use 6 red, we need exactly 2 red per table. So each table gets 2 red and 1 other. So we need to distribute the 3 non-red balloons: one green, one green, one blue perhaps. So tables: (2r,1g), (2r,1g), (2r,1b). All valid. So t=3 works. So max t=3.
Now consider r=100, g=2, b=2: S=104, floor=34, S-M=4, min=4. Can we do t=4? Need 12 balloons, non-red =4, so red used = 8. With 4 tables, each table must have at least one non-red, and at most 2 red. To use 8 red across 4 tables, average 2 red per table, so each table must have exactly 2 red and 1 non-red. That uses 4 non-red total, exactly what we have. So we can assign: two tables with green, two with blue? But we have 2 green and 2 blue, so we can assign each non-red appropriately: (2r,1g) for two tables, (2r,1b) for two tables. Works. So t=4.
Now r=100, g=3, b=1: S=104, floor=34, S-M=4? Actually M=100, S-M=4, min=4. Non-red=4. t=4 need 12 balloons, red used=8, each table 2 red, 1 non-red. But we have non-red total=4, but composition: 3 green,1 blue. That's fine: we can assign tables: three tables (2r,1g) and one table (2r,1b). Works. So t=4.
Now r=100, g=3, b=0: S=103, floor=34, S-M=3, min=3. t=3 possible? Need 9 balloons, red used=6, non-red (green)=3, each table must have at least one green (since no other non-red), and each table can have at most 2 red. So with 3 tables, to use 6 red, each table gets 2 red and 1 green. That uses exactly 3 green. So t=3 works. So good.
Now consider scenario where the two smaller colors are not both enough to provide one per table if we need exactly one per table? But we have flexibility: a table could use 2 non-red of different colors? Actually if we have two non-red colors, a table could use 2 non-red and 1 red, or 1 non-red and 2 red, or even 3 non-red but not all same (which would require at least two colors among non-red). In the bound t ≤ S - M, each table consumes at least one non-M balloon, so that's a hard upper bound. So if we aim for t = S - M, we would need to use all non-M balloons (since there are exactly S-M of them), and each table uses exactly one non-M balloon, and the remaining 2t - (S-M) balloons must come from M, but careful: total balloons used = 3t. Non-M used = S-M (all of them) if we want to achieve t = S - M? Actually t = S - M, then total balloons used = 3(S-M) = 3S - 3M. But total available balloons = S. To use all balloons? Not necessarily; we may not need to use all. But if t = S - M, we need to check if it's possible to use exactly 3(S-M) balloons, which might exceed total S? Actually 3(S-M) could be greater than S. Let's compute: 3(S-M) vs S: 3(S-M) ≤ S? That implies 3S - 3M ≤ S → 2S ≤ 3M → M ≥ (2/3)S. So if M is at least 2/3 of total, then 3(S-M) ≤ S, meaning we could potentially use all non-M balloons and some M balloons to fill up to 3t. If M < 2/3 S, then 3(S-M) > S, which would require using more balloons than we have, impossible. But in that case floor(S/3) is smaller than S-M? Because if M < 2/3 S, then S-M > S/3? Let's check: M < 2S/3 ⇒ S-M > S/3. So min would be floor(S/3), not S-M. So t = S-M is only considered when S-M ≤ floor(S/3), which implies 3(S-M) ≤ S (since floor(S/3) ≤ S/3, so S-M ≤ S/3 ⇒ 3(S-M) ≤ S). So in the cases where S-M is the min, we have 3(S-M) ≤ S. So it's feasible in terms of total balloons. Also we need to check that we can allocate the M color appropriately: we need to use total balloons = 3t = 3(S-M) ≤ S, so we need to select 3(S-M) balloons from total S, using all non-M balloons (since there are S-M of them) and 3(S-M) - (S-M) = 2(S-M) balloons from M. That requires that M ≥ 2(S-M) (since we need to take that many from M). So condition: M ≥ 2(S-M). But is that always true when S-M ≤ floor(S/3)? Let's analyze.
We have S = r+g+b, M = max. Suppose S-M ≤ floor(S/3). Since floor(S/3) ≤ S/3, we have S-M ≤ S/3 ⇒ multiply 3: 3(S-M) ≤ S ⇒ 2(S-M) ≤ S - (S-M) = M. Yes, exactly: 3(S-M) ≤ S ⇒ 3S - 3M ≤ S ⇒ 2S ≤ 3M ⇒ M ≥ 2/3 S. Then 2(S-M) = 2S - 2M. Since M ≥ 2/3 S, we have 2S - 2M ≤ 2S - 2*(2S/3) = 2S - 4S/3 = 2S/3. Compare with M: M is at least 2S/3, so it's possible that M might be less than 2(S-M)? Let's solve M ≥ 2(S-M) ⇒ M ≥ 2S - 2M ⇒ 3M ≥ 2S ⇒ M ≥ 2/3 S. Which is exactly the same condition. So indeed, if S-M ≤ floor(S/3), then M ≥ 2/3 S, and thus M ≥ 2(S-M). So we have enough M balloons to provide the needed 2(S-M) balloons. Also, we need to ensure that we can arrange tables such that each table uses exactly one non-M balloon? Not necessarily exactly one; but we can aim for each table using exactly one non-M and two M. That would satisfy: each table has 2 of the dominant color and 1 of a non-dominant, which avoids all same. This uses exactly one non-M per table. So with t = S-M tables, we would need S-M non-M balloons (which we have exactly that many if we use all non-M). And we would need 2(S-M) M balloons. Since M ≥ 2(S-M), we can take that many from M. So such an allocation is feasible provided we can allocate the non-M balloons appropriately among the two non-M colors. Since non-M consists of two colors (maybe only one if one of the others is zero). But we must assign each non-M balloon to a table, and each table gets exactly one non-M. That's possible as long as we can assign them arbitrarily; we don't need to worry about mixing because each table only gets one non-M. The non-M balloon could be either from the smaller colors; if both colors are present, we just assign each non-M balloon to a table. There's no restriction that a table must have at most 2 of a non-M? Actually a table will have exactly one non-M, so that's fine. Also, we need to avoid a table having three of the same non-M? Not possible because only one non-M. So the construction works: For t = S-M, we can use all non-M balloons (since there are exactly t of them) and use 2t balloons from M. This yields t tables each with (2 M, 1 non-M). This is valid as long as we have at least 2t M balloons. Since M ≥ 2t (t = S-M, condition M ≥ 2(S-M) equivalent to M ≥ 2t), holds when S-M ≤ floor(S/3). So indeed t = S-M is achievable.
Now what if the non-M set includes two colors, but one of them might be zero? That's fine; we just assign the existing non-M balloons. If there is only one non-M color, say only g>0, b=0, then non-M = g. t = g (since S-M = g). Then we need t tables each with 2 M and 1 g. That uses 2t M, need M ≥ 2g. Condition M ≥ 2g? Since M = r, and S = r+g, S-M = g, and condition for t = g to be ≤ floor(S/3) is g ≤ floor((r+g)/3) -> 3g ≤ r+g -> 2g ≤ r -> r ≥ 2g. So indeed r ≥ 2g, so M ≥ 2g = 2t. So feasible.
Thus the upper bound is achievable.
Now consider the other case where floor(S/3) is the min. Then t = floor(S/3). Can we always achieve t = floor(S/3)? Need to verify that there exists a feasible assignment for t = floor(S/3). This might require more careful argument. In this case, floor(S/3) ≤ S-M, so S-M ≥ t. That means we have at least t non-M balloons (since S-M is total of the other two). Actually S-M ≥ t implies total non-M ≥ t. So we have at least t balloons from the two smaller colors combined. Also, we need to check that we can pick 3t balloons total without violating per-color max constraints. The condition sum min(c,2t) ≥ 3t should hold. For t = floor(S/3), is it always true? Not necessarily; we need to ensure that the condition holds for that t. But since t ≤ floor(S/3) ≤ S/3, we have 3t ≤ S. However, sum min(c,2t) could be less than 3t? Possibly if one color is extremely dominant? But recall that when t ≤ floor(S/3) and also t ≤ S-M (since floor(S/3) ≤ S-M in this case? Actually if floor(S/3) is the min, then floor(S/3) ≤ S-M. So t ≤ S-M. So we have t ≤ total non-M. So non-M ≥ t. But is that enough to guarantee sum min(c,2t) ≥ 3t? Let's test: Suppose r=10, g=1, b=1: S=12, floor(S/3)=4, S-M=2, here floor(S/3) > S-M, so min is S-M=2. So not this case. So the case floor(S/3) is min occurs when floor(S/3) ≤ S-M, i.e., S-M ≥ floor(S/3). That typically happens when the colors are relatively balanced, i.e., no single color is too large. In such balanced cases, the condition sum min(c,2t) ≥ 3t likely holds because each color is at most 2t? Not necessarily; if colors are balanced, each is ≤? For t = floor(S/3), 2t could be less than some color? Let's see: Suppose r=5,g=5,b=5, S=15, t=5, 2t=10, each color=5 ≤10, so sum min =15 ≥15. OK.
Suppose r=8,g=8,b=1, S=17, floor(S/3)=5, S-M=9? Actually M=8, S-M=9, so floor(S/3)=5 ≤9, so min is floor=5. Check sum min: 2t=10, so min(r,10)=8, min(g,10)=8, min(b,10)=1 sum=17 ≥15, ok.
Suppose r=9,g=6,b=0, S=15, floor(S/3)=5, S-M=6? Actually M=9, S-M=6, floor=5 ≤6, min=5. Check sum min: 2t=10, min(9,10)=9, min(6,10)=6, min(0,10)=0 sum=15 ≥15, ok.
Suppose r=9,g=5,b=1, S=15, floor=5, S-M=6? M=9, S-M=6, min=5. 2t=10, min(9,10)=9, min(5,10)=5, min(1,10)=1 sum=15 ≥15.
Suppose r=10,g=3,b=2, S=15, floor=5, S-M=5? Actually M=10, S-M=5, floor=5 ≤5, so min=5. Check sum min: 2t=10, min(10,10)=10, min(3,10)=3, min(2,10)=2 sum=15 ≥15, ok.
Suppose r=10,g=2,b=2, S=14, floor=4, S-M=4? M=10, S-M=4, floor=4 ≤4, min=4. 2t=8, min(10,8)=8, min(2,8)=2, min(2,8)=2 sum=12 >=12? 3t=12, sum min=8+2+2=12, equality. So works.
Suppose r=10,g=1,b=1, S=12, floor=4, S-M=2, floor=4 >2, so min is not floor. So for floor to be the min, we need S-M ≥ floor(S/3). That implies M ≤ S - floor(S/3). Since floor(S/3) ≈ S/3, this says M ≤ 2S/3. So colors are relatively balanced (M not too large). In that region, each color could still exceed 2t? Let's check if any color can be >2t when t=floor(S/3). Since t ≈ S/3, 2t ≈ 2S/3. If M > 2t, that would mean M > 2S/3, but then S-M < S/3 ≤ t? Actually S-M < S/3, and since t ≈ S/3, S-M could be less than t, contradicting the assumption that floor(S/3) ≤ S-M? Let's be precise:
Assume t = floor(S/3). For floor(S/3) to be ≤ S-M, we need floor(S/3) ≤ S-M. If M > 2t, then 2t < M. But t = floor(S/3) ≤ S/3, so 2t ≤ 2S/3. So M > 2t implies M > 2S/3? Not exactly, but M could be between 2t and something. However, we can bound: Since t ≤ S/3, 2t ≤ 2S/3. So if M > 2t, it could still be ≤ 2S/3? If M > 2t, it's possible that M ≤ 2S/3 if 2t < M ≤ 2S/3. But is that possible given t = floor(S/3)? Example: S=10, floor(S/3)=3, 2t=6, 2S/3≈6.66. So M could be, say, 6.5? But M is integer. Let's test S=10, t=3. For floor=3 to be ≤ S-M, we need 3 ≤ 10-M → M ≤ 7. So M could be 6 or 7. If M=7, then M > 2t? 2t=6, so M=7 >6. So M > 2t is possible while still floor(S/3) ≤ S-M? Check: S-M = 3, so floor(S/3)=3, equality holds. So t=3, M=7, S=10, non-M=3. Here M=7, 2t=6, so M > 2t. Is sum min(c,2t) ≥ 3t? Let's compute: r=7, g and b? Actually we need three numbers summing to 10 with max 7. Let's set r=7,g=2,b=1? That sums to 10. Check sum min: min(7,6)=6, min(2,6)=2, min(1,6)=1 sum=9, 3t=9. So equality works! Because we capped the dominant color at 2t=6, and the others at their actual values. So sum = 6+2+1=9 = 3t. So condition holds. So even if M > 2t, condition can still hold as long as the capped sum is at least 3t. In this example, M=7 >2t=6, but capped sum equals 3t. So floor(S/3) can be achievable even if M > 2t, provided the other colors are enough to make up the deficit. However, note that in this example S-M = 3, which equals t. So t = S-M, and floor(S/3) = t as well? Actually floor(10/3)=3, S-M=3. So it's exactly the boundary where both are equal. So this is the case where t = S-M = floor(S/3). So it's fine.
Now, could there be a case where floor(S/3) is strictly less than S-M, yet sum min(c,2t) < 3t for t = floor(S/3)? That would mean that even though we have enough total balloons, the per-color cap restricts us, making t smaller than floor(S/3). But would that contradict the earlier reasoning that the maximum t is min(floor(S/3), S-M)? Because if such a case exists, then the maximum t might be less than floor(S/3). Let's test to see if such a case exists.
We need t = floor(S/3) such that sum min(c,2t) < 3t, yet t ≤ S-M (since floor(S/3) ≤ S-M in this scenario). Is it possible? Let's search.
Condition sum min(c_i,2t) < 3t.
Let’s denote the sorted colors: a ≤ b ≤ c. So c = M. We have S = a+b+c, t = floor(S/3). Also we assume t ≤ S-M = a+b (since S-M = a+b). So t ≤ a+b.
Now sum min = min(a,2t) + min(b,2t) + min(c,2t). Since a ≤ b ≤ c. For t, note that 2t might be less than c possibly. Since t ≤ a+b, and a+b could be less than c? Actually if c is large, then a+b might be small, but we have t ≤ a+b, so t is not large. In that case 2t ≤ 2(a+b). Could 2t be less than c? Possibly if c is huge. But also note that min(c,2t) = 2t if 2t < c, else c.
We want sum min < 3t.
Case 1: 2t ≤ a (so even a ≥ 2t). Then all min are 2t, so sum min = 3*2t = 6t, which is ≥ 3t, so condition holds. So need 2t > a for possible failure.
Case 2: a < 2t ≤ b. Then min(a,2t)=a, min(b,2t)=2t? Actually if 2t ≤ b, then min(b,2t)=2t? Wait b ≥ 2t, so min(b,2t) = 2t. And min(c,2t) = 2t if 2t ≤ c else c. But since c ≥ b ≥ 2t, then min(c,2t)=2t. So sum = a + 2t + 2t = a + 4t. We need a+4t < 3t => a < -t, impossible. So condition holds.
Case 3: b < 2t ≤ c. Then min(a,2t)=a, min(b,2t)=b, min(c,2t)=2t (since 2t ≤ c? Actually 2t ≤ c, so min=2t). Sum = a + b + 2t. Condition fails if a+b+2t < 3t => a+b < t. So we need a+b < t.
Case 4: c < 2t. Then all min are actuals: a+b+c = S. Condition fails if S < 3t, but t = floor(S/3) so S ≥ 3t, so S >= 3t. Actually if S >= 3t, then sum = S >= 3t, condition holds. Could be equality if S = 3t, else >. So only possible failure scenario is case 3: when 2t lies between b and c (i.e., b < 2t ≤ c) and a+b < t. And we also have t ≤ a+b (since t ≤ S-M = a+b). So case 3 requires a+b < t and t ≤ a+b simultaneously, which is impossible. Because a+b < t and t ≤ a+b cannot both hold unless a+b = t and the inequalities are strict? Let's examine: We need a+b < t and t ≤ a+b? The latter is t ≤ a+b. For both to be true, we would need a+b < t ≤ a+b, which implies a+b < t and t ≤ a+b => t ≤ a+b < t, contradiction. So it's impossible.
But careful: In case 3, condition for failure is a+b+2t < 3t → a+b < t. But we also have t ≤ a+b? That came from the assumption that floor(S/3) ≤ S-M? Actually we are in the scenario where floor(S/3) is the min, meaning t = floor(S/3) ≤ S-M = a+b. So t ≤ a+b. So if a+b < t, then t ≤ a+b < t, impossible. Therefore, failure cannot occur. So indeed, if t = floor(S/3) and t ≤ a+b, then sum min ≥ 3t always. Let's verify with the inequalities: We have t ≤ a+b. In case 3, we have b < 2t ≤ c. Also a+b ≥ t. Then a+b+2t ≥ t+2t = 3t, so sum min ≥ 3t. Actually we need to be careful: In case 3, sum min = a + b + 2t. Since a+b ≥ t, then a+b+2t ≥ 3t. So condition holds. Good.
Thus, when t = floor(S/3) and t ≤ S-M, the condition sum min ≥ 3t holds. So t is achievable. Therefore, the maximum t is indeed min(floor(S/3), S-M).
But wait: Could there be a scenario where the maximum t is larger than min(floor(S/3), S-M)? No, because we have two upper bounds: total balloons gives t ≤ floor(S/3), and each table needs at least one non-M balloon gives t ≤ S-M. So t cannot exceed the min. And we've shown that t = min is achievable. So the answer is exactly that.
Thus we can compute answer as min( (r+g+b) / 3 (floor), (r+g+b) - max(r,g,b) ). However, careful: The bound t ≤ S-M is integer, S-M is integer. So answer = min( floor(S/3), S - M ).
But we need to ensure that the answer is integer, and we take the minimum of these two numbers, which are both integers.
Edge Cases:
-
If any of r,g,b are zero or all zero: works.
-
If S - M may be larger than floor(S/3), then answer = floor(S/3). Is floor(S/3) always achievable? We argued yes, as long as it is ≤ S-M, which holds in that case. So yes.
Thus answer = min( (r+g+b) // 3, (r+g+b) - max(r,g,b) ).
But double-check with example: Input: 5 4 3 => S=12, floor=4, S-M=12-5=7, min=4 -> output 4.
Input: 1 1 1 => S=3, floor=1, S-M=3-1=2, min=1 -> output 1.
Input: 2 3 3 => S=8, floor=2, S-M=8-3=5, min=2 -> output 2.
Now test some random cases mentally.
Case: r=0, g=0, b=5: S=5, floor=1, S-M=5-5=0, min=0 -> output 0. Correct.
Case: r=2, g=0, b=0: S=2, floor=0, S-M=2-2=0, min=0 -> output 0.
Case: r=2, g=1, b=0: S=3, floor=1, S-M=3-2=1, min=1 -> output 1. Can we make 1 table? Need three balloons, cannot be all same. We have 2 red,1 green: table can be (2r,1g) which is valid. So yes.
Case: r=2, g=2, b=0: S=4, floor=1, S-M=4-2=2, min=1 -> output 1. But can we make 1 table? Yes, (2r,1g) or (1r,2g) uses 3 balloons, leaving 1. Could we make 2 tables? Need 6 balloons but only have 4, so impossible. So correct.
Case: r=3, g=3, b=0: S=6, floor=2, S-M=6-3=3, min=2 -> output 2. Works.
Case: r=4, g=2, b=0: S=6, floor=2, S-M=6-4=2, min=2 -> output 2. Is 2 tables possible? Use 4 red,2 green? Need 6 balloons. Each table must avoid all-red. With 2 tables, we can give each table 2 red and 1 green? But we have only 2 green total, so that would use 2 green. That uses 4 red, 2 green. That's exactly (2r,1g) each. Works. So t=2.
Case: r=4, g=1, b=0: S=5, floor=1, S-M=5-4=1, min=1 -> output 1. 2 tables would need at least 2 non-red, but only 1. So yes.
Case: r=4, g=4, b=1: S=9, floor=3, S-M=9-4=5, min=3 -> output 3. Need to check: Can we make 3 tables? We have 4,4,1. Use all? 3 tables need 9 balloons, total 9, so must use all. Need to avoid all-same. Can we partition? Possibly: (2r,1g), (2g,1r), (1r,1g,1b). That uses r:2+1+1=4, g:1+2+1=4, b:1. Works. So t=3.
Case: r=4, g=4, b=2: S=10, floor=3, S-M=10-4=6, min=3 -> output 3. Should be feasible.
Case: r=1000000000, g=1000000000, b=1000000000 -> answer = min( 3e9/3=1e9, 3e9-1e9=2e9) = 1e9.
Case: r=2000000000, g=2000000000, b=0 -> S=4e9, floor=1333333333, S-M=4e9-2e9=2e9, min=1333333333 -> output that.
Case: r=2000000000, g=1, b=0 -> S=2000000001, floor=666666667 (since 2e9+1 = 2000000001, /3 = 666666667 exactly? 2000000001/3 = 666666667, because 3*666,666,667 = 2,000,000,001). S-M = 2000000001 - 2000000000 = 1, min=1 -> output 1. Indeed, with 2e9 red and 1 green, max tables = 1.
All good.
Thus the solution is straightforward: read r,g,b, compute S = r+g+b, M = max(r,g,b), answer = min(S // 3, S - M).
But wait: There is also possibility that answer could be something else? Let's test a borderline: r=0, g=0, b=0 => S=0, floor=0, S-M=0-0=0, min=0.
Now test r=1, g=2, b=3: S=6, floor=2, M=3, S-M=3, min=2 -> output 2. Feasible? Need 6 balloons, use all. Can we partition? E.g., (1,1,1) twice? That uses 2 each of r,g,b? Actually (1,1,1) each uses 1 of each, two tables would use r=2,g=2,b=2, but we have r=1,g=2,b=3, so not enough red. So maybe different: Table1: (1,2,0?) cannot have 2 green and 1 red? (1r,2g,0b) is 1+2=3, allowed? Yes (1r,2g) is allowed. Table2: (0,0,3) not allowed. So maybe (1r,2g) and (0,1,2) etc. Let's try: Use Table1: (1r,2g,0b) uses r1,g2. Table2: (0r,0g,3b) invalid. So need to avoid all blue. So maybe Table1: (1r,1g,1b) uses 1 each, remaining (0,1,2): that's (0r,1g,2b) which is allowed? That table has 2 blue and 1 green, allowed. So totals: r used=1, g used=1+1=2, b used=1+2=3. Works. So t=2.
Now test r=0,g=2,b=4: S=6, floor=2, M=4, S-M=2, min=2 -> output 2. Need to use 6 balloons: all. Can we partition? Try: Table1: (0,2,1) uses g2,b1; Table2: (0,0,3) invalid; need to use all 4 blue. Let's try: Table1: (0,1,2) uses g1,b2; Table2: (0,1,2) uses g1,b2, total g2,b4. Works. So t=2.
Now test r=0,g=3,b=3: S=6, floor=2, M=3, S-M=3, min=2 -> output 2. Use all: maybe (0,1,2) and (0,2,1) etc. Works.
Now test r=2,g=2,b=3: S=7, floor=2, M=3, S-M=4, min=2 -> output 2.
Now test r=1,g=100,b=100: S=201, floor=67, M=100, S-M=101, min=67 -> output 67. Feasible? Need 201 balloons? Actually 367=201, total is 201, so must use all. We have r=1,g=100,b=100. Need to arrange 67 tables without all same. Since red only 1, each table must have at least one non-red, but that's fine. More critical: We need to avoid all-green or all-blue tables. Since we have many green and blue, but we can mix. We need to check if we can partition 100 green, 100 blue, 1 red into 67 tables each with not all same. This is similar to the case where two colors are abundant and one is scarce. Since total non-red =200, which is more than t=67, we have many non-red. But also we need to ensure no table is all-green or all-blue. That means each table must have at most 2 of any color. So each table can have at most 2 green and at most 2 blue. So maximum green usage across 67 tables is 267=134, we have 100, so okay. Similarly blue. So we need to use all 201 balloons: green 100, blue 100, red 1. That sums to 201. Let's see if we can find a distribution. Let x be number of tables with red balloon. Since only 1 red, x can be 0 or 1. But if we don't use red, then all tables are only green and blue, with no red. Then each table must avoid all-green and all-blue. So each table must have at least one of each color? Actually if a table has only green and blue, to avoid all-green, it cannot have 3 green; similarly cannot have 3 blue. So possible patterns: (2g,1b) or (1g,2b) or (1g,1g? no) So each table must have both colors? If a table has 2g,1b, that's fine; if 1g,2b, fine; but what about 2g,1b etc. So each table must have at least one green and at least one blue? If a table had 3 green, not allowed; 2 green and 1 blue is allowed, it has at least one blue; if 1 green and 2 blue, it has at least one green. So yes, each table must have at least one of each color if only two colors are used, because if it had only one color, it would be all that color, which is forbidden. So if no red, each of the 67 tables must have at least one green and at least one blue. That would require at least 67 green and 67 blue. We have 100 each, so that's okay. However, we also need the total counts to match: Let a be number of tables with (2g,1b), then b = 67 - a tables with (1g,2b). Then green used = 2a + (67 - a) = a + 67; blue used = a + 2(67 - a) = 134 - a. We need a+67 = 100 => a=33, and 134 - a = 101? Actually 134 - 33 = 101, but we have blue 100. Not match. If we set a=34, green=34+67=101, blue=134-34=100. That uses green 101, but we have green 100. So not match. If we set a=33, green=100, blue=101. So we cannot exactly match both to 100,100 with 67 tables if we use no red. So we need to involve the red balloon. With one red, we can have one table that includes red, and the other 66 tables without red. The table with red could be e.g., (1r,1g,1b) or (1r,2g,0b) or (1r,0g,2b). Let's consider. Let's denote t=67. Let there be one table with red. The remaining 66 tables are only green/blue. For those 66 tables, each must have at least one green and one blue (as argued). So for 66 tables, let a be number of (2g,1b) among them, and b = 66 - a be (1g,2b). Then green used in these tables = 2a + (66 - a) = a + 66; blue used = a + 2(66 - a) = 132 - a. The red table uses some green and blue as well depending on its composition. Let the red table have composition (1r, g_r, b_r) with g_r + b_r = 2. So g_r can be 0,1,2. Then total green = (a+66) + g_r = 100, total blue = (132 - a) + b_r = 100. Also b_r = 2 - g_r. So we have:
a + 66 + g_r = 100 → a = 34 - g_r.
132 - a + (2 - g_r) = 100 → 132 - a + 2 - g_r = 100 → 134 - a - g_r = 100 → a + g_r = 34.
Now substitute a from first: (34 - g_r) + g_r = 34 → 34 = 34, identity. So for any g_r in {0,1,2}, we can set a = 34 - g_r, and then check that a is between 0 and 66, and b = 66 - a is nonnegative. For g_r=0 → a=34, b=32; for g_r=1 → a=33, b=33; for g_r=2 → a=32, b=34. All valid. So there is a solution. So t=67 is feasible. So answer 67 works.
Thus formula holds.
Given the reasoning, the solution is simple.
But wait: Could there be a scenario where S - M is less than floor(S/3) but still t = S - M is not achievable because of some other constraint? We argued that with t = S-M, we can use all non-M balloons and exactly 2t from M, provided M ≥ 2t. But t = S-M, so M ≥ 2(S-M) is equivalent to M ≥ 2/3 S. Since t = S-M is the min, we have S-M ≤ floor(S/3) ≤ S/3, so S-M ≤ S/3 ⇒ 3(S-M) ≤ S ⇒ 2(S-M) ≤ S - (S-M) = M, so indeed M ≥ 2(S-M) = 2t. So condition holds. Also need to ensure that we can assign non-M balloons: we have exactly t non-M balloons. They may be of two colors. In the construction, each table gets exactly one non-M balloon. That is possible regardless of distribution: just assign each non-M balloon to a distinct table. Since tables are identical, we can do that. There's no additional restriction like a table cannot have two different non-M colors? It can have exactly one non-M balloon, which could be from either of the two colors. That's fine. So construction works.
Therefore answer = min( (r+g+b) // 3, (r+g+b) - max(r,g,b) ).
That is the solution.
But we need to be absolutely sure about integer overflow? r,g,b up to 2e9, sum up to 6e9, which fits in 64-bit (less than 2^63). So we can use 64-bit (long long in C++). Python handles big ints.
Now we must implement a program that reads three integers and outputs that min.
Edge Cases: If sum // 3 may be huge, but fine.
Test with given examples.
Thus the solution is trivial.
But wait: There's a nuance: The bound t ≤ S - M is derived from the need that each table must have at least one balloon from the colors other than the max. However, is it possible that the max color is not unique? If two colors tie for max, say r = g = 10, b = 5. Then M = 10, S-M = 15? Actually S=25, S-M=15. But the bound using max: t ≤ S - max = 15. But could we have t = 15? That would require 45 balloons, but total is only 25, so floor(S/3)=8 is smaller. So the bound is not active. So it's fine.
If all three equal, M = value, S-M = 2M, which is > floor(S/3) = floor(3M/3)=M. So min = M. So answer = M, which is correct: with M each, we can make M tables of (1,1,1). Works.
Now consider case r=0,g=0,b=5: M=5, S-M=0, min=0. Good.
Now consider r=0,g=5,b=5: S=10, floor=3, M=5, S-M=5, min=3. So answer 3. Check: Can we make 3 tables? Use all 10? Need 9 balloons. We can do (2g,1b), (1g,2b), (2g,1b) etc. Let's try: Use 3 tables: total balloons 9, leave 1. We have 5g,5b. Need to use 9, so use 4g and 5b or 5g and 4b, etc. Can we avoid all-same? Yes. So 3 is fine.
Now consider r=0,g=6,b=6: S=12, floor=4, M=6, S-M=6, min=4. So 4 tables. Feasible? Need 12 balloons. Use all. Each table must have both colors (since only two colors). So each table must have at least 1g and 1b. With 4 tables, minimum 4 each. We have 6 each, so possible. Let's find numbers: Let a tables (2g,1b), b tables (1g,2b), a+b=4. Then green used = 2a + b = a + (a+b) = a+4; blue used = a + 2b = a + 2(4-a) = 8 - a. Set equal to 6 each: a+4=6 => a=2; then blue = 8-2=6, matches. So works.
Now consider r=1,g=6,b=6: S=13, floor=4, M=6, S-M=7, min=4. So 4 tables, using 12 balloons. Should be feasible. Possibly similar.
Thus all good.
Therefore, the implementation is straightforward.
But we should also consider a scenario where the bound S - M might be less than floor(S/3) but not integer? S-M is integer. So min is integer.
Now we need to write the program. It will be simple: read three ints, compute sum, compute max, compute ans = min(sum // 3, sum - max). Print ans.
I'll also consider if there is any trick: If sum - max might be negative? max can be greater than sum? No, max ≤ sum, so sum - max ≥ 0.
Thus solution done.
But wait, I recall a Codeforces problem "A. Balloons" where the answer is something else? There is a known problem "A. Balloons" about dividing into two sets? Not this. There is also "B. Balloons" but I'm not sure. However, I'm pretty confident that this solution is correct for this problem statement.
Let's verify with some random small values via brute force to be extra sure. Since we cannot run code now, we can manually enumerate small numbers.
We can test all combinations with r,g,b from 0 to maybe 5 and compare the formula to brute force enumeration of maximum t. Let's try some.
We need to check if there is any case where the formula fails.
We can do mental brute for small numbers.
Define f(r,g,b) = min( floor((r+g+b)/3), (r+g+b) - max(r,g,b) ). We'll compute actual maximum t via reasoning.
We'll test all triples with sum up to maybe 5 or 6.
Case (0,0,0): formula 0, actual 0.
(0,0,1): sum=1, floor=0, S-M=1-1=0, min=0. actual 0.
(0,0,2): sum=2, floor=0, S-M=0, min=0. actual 0.
(0,0,3): sum=3, floor=1, S-M=0, min=0. actual 0 because can't have all same. So correct.
(0,1,1): sum=2, floor=0, S-M=2-1=1, min=0? Wait floor=0, S-M=1, min=0, so answer 0. Actual: we have 2 balloons, need 3 for a table, so 0. correct.
(0,1,2): sum=3, floor=1, S-M=3-2=1, min=1? Actually floor=1, S-M=1, min=1 -> answer 1. Can we make 1 table? Yes, use (0,1,2) allowed. So correct.
(0,1,3): sum=4, floor=1, S-M=4-3=1, min=1 -> answer 1. Can we? Use (0,1,2) leaves 1 blue unused; works. So correct.
(0,2,2): sum=4, floor=1, S-M=4-2=2, min=1 -> answer 1. Can we make 1 table? Yes, (0,2,1) uses 2 green,1 blue? That's 2 green, 1 blue, allowed. So 1. Could we make 2 tables? Need 6 balloons, only 4. So max 1. correct.
(0,2,3): sum=5, floor=1, S-M=5-3=2, min=1 -> answer 1. Can we do 1? yes. Could we do 2? need 6 >5, no.
(0,3,3): sum=6, floor=2, S-M=6-3=3, min=2 -> answer 2. We already reasoned works.
(0,4,1): sum=5, floor=1, S-M=5-4=1, min=1 -> answer 1. Could we do 1? yes (e.g., (0,2,1)? Actually we have 4 green,1 blue; table can be (0,2,1) uses 2 green,1 blue, leaving 2 green unused. So 1 table. Could we do 2? need 6 balloons, only 5. So correct.
(0,4,2): sum=6, floor=2, S-M=6-4=2, min=2 -> answer 2. Need to check if 2 tables possible with 4 green,2 blue. Use all 6 balloons: 2 tables. Each table must have at least one of each color (since only two colors). So each table must have at least 1 green and 1 blue. With 2 tables, that requires at least 2 green and 2 blue. We have 4 green,2 blue, so blue is exactly 2. So each table must have exactly 1 blue? Could have one table with 2 blue and 1 green, but that would use 2 blue, okay. The other table then would have 0 blue? That would be all green (3 green) not allowed. So both tables must have at least 1 blue. Since total blue=2, each gets exactly 1 blue. Then each table gets 2 green (since 3-1=2). So tables: (2g,1b) each, uses 4 green,2 blue. Works. So 2 tables.
(0,4,3): sum=7, floor=2, S-M=7-4=3, min=2 -> answer 2. Need 6 balloons, can use 4 green,2 blue perhaps? But we have 3 blue, so we can leave 1 blue unused. Construction similar: 2 tables, each need at least one blue? Actually if we have 2 tables and only two colors, each table must have both colors to avoid all-same. So each table must have at least 1 blue and at least 1 green. With 2 tables, total blue needed at least 2, we have 3, okay. So we can allocate 1 blue to each table, and then 2 green to each, using 4 green,2 blue. Leaves 1 blue. So works.
(0,5,1): sum=6, floor=2, S-M=6-5=1, min=1 -> answer 1. Actually floor=2, S-M=1, min=1. So answer 1. Check if 2 tables possible? Need 6 balloons, we have 6 total. Could we form 2 tables with 5 green,1 blue? Each table must avoid all-green, so each table must have at least one non-green (i.e., blue). But only one blue, so at most one table can have a blue, the other table would have only green, which would be all-green (3 green) not allowed. Could we have a table with 2 blue? only 1 blue. So indeed only 1 table possible. So answer 1, correct.
(0,5,2): sum=7, floor=2, S-M=7-5=2, min=2 -> answer 2. Need 6 balloons. We have 5 green,2 blue. Check feasibility: 2 tables, need each to avoid all-green. So each table must have at least one blue? If a table has no blue, it would be all green (since only green available for that table), which would be invalid if it has 3 green. But could a table have 2 green and 1 blue? That's okay. If a table has 1 green and 2 blue? That's okay but uses 2 blue. We have only 2 blue, so if one table uses 2 blue, the other table would have 0 blue and would need to be all green (3 green) which is invalid. So each table must have exactly 1 blue. Then each table gets 2 green. That uses 4 green and 2 blue. We have 5 green, so one green leftover. So 2 tables feasible. So answer 2.
(0,5,3): sum=8, floor=2, S-M=8-5=3, min=2 -> answer 2. Feasible? 2 tables, need at least 2 blue (since each table needs at least one blue). We have 3, so okay. Use 2 tables: allocate each 1 blue, 2 green uses 4 green,2 blue; leaves 1 green,1 blue. So works.
(0,5,4): sum=9, floor=3, S-M=9-5=4, min=3 -> answer 3. Need 9 balloons, total 9, so use all. Only green and blue. With 3 tables, each must have at least 1 green and 1 blue (since only two colors). So need at least 3 green and 3 blue. We have 5 and 4, so okay. Need to find integer solution: let a tables (2g,1b), b tables (1g,2b), a+b=3. Green = 2a+b = a+3, Blue = a+2b = a+2(3-a)=6 - a. Set equal to 5 and 4? Need a+3=5 => a=2, then blue=6-2=4, matches. So works.
(0,5,5): sum=10, floor=3, S-M=10-5=5, min=3 -> answer 3. Use 9 balloons? Actually 3 tables need 9, we have 10, leave 1. Feasible? We can try a=2 as before? For 3 tables: a=2 gives green=5, blue=4, using 9 balloons (since totals 5+4=9). That leaves 1 blue. So works.
(0,6,1): sum=7, floor=2, S-M=7-6=1, min=1 -> answer 1. Could we make 2 tables? 2 tables require 6 balloons, we have 7 total. But need at least one blue per table (since only green and blue). Only one blue, so impossible. So answer 1, correct.
(0,6,2): sum=8, floor=2, S-M=8-6=2, min=2 -> answer 2. 2 tables need at least 2 blue, we have 2, so possible: each table 1 blue, 2 green uses 4 green,2 blue, leaving 2 green. Works.
(0,6,3): sum=9, floor=3, S-M=9-6=3, min=3 -> answer 3. 3 tables need at least 3 blue, we have 3, so possible: each table 1 blue, 2 green uses 6 green,3 blue, total 9, but we have 6 green,3 blue exactly. That works: each table (2g,1b). So answer 3.
(0,6,4): sum=10, floor=3, S-M=10-6=4, min=3 -> answer 3. Need 9 balloons, we have 10. With 3 tables, need at least 3 blue, we have 4. Feasible: e.g., a=2 tables (2g,1b), 1 table (1g,2b) gives green: 22+1=5, blue: 2+2=4? Actually compute: a=2,b=1: green = 22+1=5, blue = 2+2*1=4? Wait formula: green = 2a + b = 4+1=5, blue = a + 2b = 2+2=4. So uses 5 green,4 blue =9 balloons, leaving 1 green. Works.
(0,6,5): sum=11, floor=3, S-M=11-6=5, min=3 -> answer 3. Works similarly.
(0,6,6): sum=12, floor=4, S-M=12-6=6, min=4 -> answer 4. Need 12 balloons, all. For 4 tables with only green/blue, each must have at least 1 green and 1 blue, so need at least 4 each, we have 6 each. Find a: a+b=4, green = 2a+b = a+4, blue = a+2b = a+2(4-a)=8 - a. Set equal to 6 each: a+4=6 => a=2, then blue=8-2=6, matches. So works.
Now test with three colors present.
(1,1,1): we did.
(1,1,2): sum=4, floor=1, M=2, S-M=2, min=1 -> answer 1. Can we make 1 table? Yes, e.g., (1,1,1) uses 1,1,1? But we have only 2 blue, so we can do (1,1,1) using 1 blue, leaving 1 blue. That's valid. Could we make 2 tables? Need 6 balloons, only 4. So max 1.
(1,1,3): sum=5, floor=1, M=3, S-M=2, min=1 -> answer 1. Can we make 2? need 6 >5. So ok.
(1,1,4): sum=6, floor=2, M=4, S-M=2, min=2? floor=2, S-M=2, min=2 -> answer 2. Check: We have 1 red,1 green,4 blue. Need to form 2 tables (6 balloons). Use all? total 6, must use all. Is it possible? We need to avoid all-same. Let's try: Table1: (1r,1g,1b) uses 1 each. Table2: need 3 balloons left: 0r,0g,3b -> all blue, invalid. So maybe different: Table1: (1r,0g,2b) uses r1,b2; Table2: (0r,1g,2b) uses g1,b2; totals r1,g1,b4. That's valid. So 2 tables possible. So answer 2.
(1,2,2): sum=5, floor=1, M=2, S-M=3, min=1 -> answer 1. Could we make 2 tables? need 6 >5, so no.
(1,2,3): sum=6, floor=2, M=3, S-M=3, min=2 -> answer 2. Feasible? We have 1,2,3. Need to use 6 balloons. Try: Table1: (1,1,1) uses 1,1,1; remaining (0,1,2) -> (0,1,2) valid. So 2 tables. Works.
(1,2,4): sum=7, floor=2, M=4, S-M=3, min=2 -> answer 2. 2 tables need 6 balloons. We can leave 1. Try to use: Table1: (1,1,1) uses 1,1,1; remaining (0,1,3) but 3 blue all same invalid. So maybe different: Table1: (1,2,0) uses 1,2,0? That's (1r,2g,0b) allowed? Yes, 1r+2g, allowed. Then remaining (0,0,4) -> cannot use 3 blue. So need to use blue in both tables. Try: Table1: (1,1,1) uses 1r,1g,1b. Remaining (0,1,3). Cannot form a table without all blue. Could use different: Table1: (1,0,2) uses 1r,2b; Table2: (0,2,1) uses 2g,1b? That would be (0,2,1) uses 0r,2g,1b; totals r1,g2,b3. That's 1+2+3=6, works. So 2 tables.
(1,3,3): sum=7, floor=2, M=3, S-M=4, min=2 -> answer 2. Feasible? Need 6 balloons. Can do: Table1: (1,1,1); Table2: (0,2,1)? That would use r1,g3,b2? Actually (1,1,1) uses 1,1,1; (0,2,1) uses 0,2,1 -> totals 1,3,2. That's r1,g3,b2, sum=6, works.
(2,2,2): sum=6, floor=2, M=2, S-M=4, min=2 -> answer 2. Works.
(2,2,3): sum=7, floor=2, M=3, S-M=4, min=2 -> answer 2. Feasible? Need 6 balloons. Can do: Table1: (1,1,1); Table2: (1,1,1)? But that would use 2,2,2 and leave 0,0,1. Not using all? Actually we need only 6, so can leave 1. So (1,1,1) each uses 2,2,2? That's 2 red? Actually if we use two tables both (1,1,1), that uses 2 red,2 green,2 blue. That's exactly (2,2,2). So we can use that, leaving 1 blue. So works.
(2,3,3): sum=8, floor=2, M=3, S-M=5, min=2 -> answer 2. Feasible? Use 6 balloons: can do (1,1,1) twice, uses 2,2,2 leaving 0,1,1. Or other combos. So works.
(2,3,4): sum=9, floor=3, M=4, S-M=5, min=3 -> answer 3. Need 9 balloons, all. Let's see if possible: We need to partition 2,3,4 into 3 tables no all same. Try: Table1: (1,1,1); Table2: (1,1,1); Table3: (0,1,2)? That would give red:2, green:1+1+1=3, blue:1+1+2=4. Actually compute: two (1,1,1) use r2,g2,b2. Third (0,1,2) uses r0,g1,b2 -> totals r2,g3,b4. Works. So 3 tables.
(2,4,4): sum=10, floor=3, M=4, S-M=6, min=3 -> answer 3. Need 9 balloons. Use 2,3,4? Actually we have 2,4,4. Try: two (1,1,1) use 2,2,2; third (0,2,1) uses 0,2,1 -> totals r2,g4,b3, but we have b4, so maybe (0,1,2) gives b2? Let's find proper: We need to use exactly 9 balloons; we can leave one. Let's try: Use tables: (1,1,1), (1,1,1), (0,2,1). That uses r2,g4,b3. We have b4, so we can leave 1 blue. So works.
(3,3,3): answer 3.
(3,3,4): sum=10, floor=3, M=4, S-M=6, min=3 -> answer 3. Feasible? Use 9 balloons. Try: three tables of (1,1,1) would use 3,3,3, leaving 0,0,1. That's valid. So works.
(3,4,5): sum=12, floor=4, M=5, S-M=7, min=4 -> answer 4. Use 12? Need 12 balloons, all. Need to partition 3,4,5 into 4 tables no all same. Possible? Likely yes.
(4,4,4): answer 4.
(4,4,5): sum=13, floor=4, M=5, S-M=8, min=4 -> answer 4.
(4,5,5): sum=14, floor=4, M=5, S-M=9, min=4.
(5,5,5): sum=15, floor=5, M=5, S-M=10, min=5.
All seems consistent.
Now test edge: (0,0,0) fine.
Thus the formula stands.
Therefore the solution code is trivial.
But before finalizing, let's check if there is any trick with large numbers: need to use 64-bit. In Python, automatically big int.
Implementation steps:
- Read line, split, map to ints.
- Compute total = r + g + b.
- Compute max_color = max(r, g, b).
- Compute ans = min(total // 3, total - max_color).
- Print ans.
That's it.
Now, let's consider if we should consider negative numbers? Input says 0 ≤ r,g,b.
Thus we are done.
Now confirm with sample:
Input: 5 4 3 -> total=12, max=5, total-max=7, total//3=4, min=4 -> output 4. matches.
Input: 1 1 1 -> total=3, max=1, total-max=2, total//3=1, min=1.
Input: 2 3 3 -> total=8, max=3, total-max=5, total//3=2, min=2.
All good.
Thus answer.
I will now produce the final answer: Python code. ```python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
r, g, b = map(int, data)
total = r + g + b
max_color = max(r, g, b)
ans = min(total // 3, total - max_color)
print(ans)
if name == "main":
main()