{"problem":{"name":"C. Urn with Balls","description":{"content":"Megabrain has usual problems with the occupiers: he is captured again and is forced to solve logical puzzles about urns with balls. In the urn staying in front of Megabrain there are a red balls, b gr","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF10134C"},"statements":[{"statement_type":"Markdown","content":"Megabrain has usual problems with the occupiers: he is captured again and is forced to solve logical puzzles about urns with balls. In the urn staying in front of Megabrain there are a red balls, b green balls and also c balls, colors of which are unknown to Megabrain. The occupiers demand an answer to a question: what is the maximal number of balls one can take out of this urn, so that there would be no more than n red and no more than m green balls among them for sure?\n\nThe first line contains three integers a, b and c separated by a space (0 ≤ a, b, c ≤ 109) — the number of red balls in the urn, the number of green balls in the urn, and the number of balls of unknown color in the urn, correspondingly.\n\nThe second line contains two integers n and m separated by a space (0 ≤ n, m ≤ 109) — the maximal number of red balls allowed to be taken out of the urn and the maximal number of green balls allowed to be taken out of the urn, correspondingly.\n\nOutput a single integer — the maximal number of balls that can be taken out of the urn so that no restrictions are violated.\n\n## Input\n\nThe first line contains three integers a, b and c separated by a space (0 ≤ a, b, c ≤ 109) — the number of red balls in the urn, the number of green balls in the urn, and the number of balls of unknown color in the urn, correspondingly.The second line contains two integers n and m separated by a space (0 ≤ n, m ≤ 109) — the maximal number of red balls allowed to be taken out of the urn and the maximal number of green balls allowed to be taken out of the urn, correspondingly.\n\n## Output\n\nOutput a single integer — the maximal number of balls that can be taken out of the urn so that no restrictions are violated.\n\n[samples]","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ a, b, c \\in \\mathbb{Z}_{\\geq 0} $ denote the number of red, green, and unknown-colored balls in the urn, respectively.  \nLet $ n, m \\in \\mathbb{Z}_{\\geq 0} $ denote the maximum allowed red and green balls in the extracted subset, respectively.\n\n**Constraints**  \nThe unknown-colored balls may be any combination of red and green; the worst-case assignment (to violate constraints) must be avoided.\n\n**Objective**  \nFind the maximum integer $ x $ such that, regardless of the colors of the $ c $ unknown balls, it is possible to extract $ x $ balls with at most $ n $ red and at most $ m $ green balls.\n\nThis is equivalent to:  \n$$\nx = \\min(a + c, n + c) + \\min(b + c, m + c) - c\n$$  \nbut constrained by the fact that the unknown balls cannot be double-counted.\n\nBetter formalization:  \n\nThe worst-case scenario is that all unknown balls are red or all are green. To guarantee the constraints, we must assume the unknown balls could add to *either* color beyond $ a $ or $ b $.  \n\nTo ensure **no more than $ n $ red** and **no more than $ m $ green** in the extracted set, we must have:  \n- Extracted red ≤ $ \\min(a + c, n) $  \n- Extracted green ≤ $ \\min(b + c, m) $  \n\nBut since the unknown balls are shared between the two, the total extracted balls $ x $ must satisfy:  \n$$\nx \\leq \\min(a + c, n) + \\min(b + c, m) - \\max(0, \\min(a + c, n) + \\min(b + c, m) - (a + b + c))\n$$  \n\nSimpler and correct approach:  \n\nWe can extract at most $ n $ red balls (including up to $ c $ from unknown), and at most $ m $ green balls (including up to $ c $ from unknown). Since the $ c $ unknown balls can be assigned to either color, the total number of balls we can safely extract is bounded by:  \n\n$$\nx = \\min(a + b + c, \\; n + m + c - \\max(0, n - a) - \\max(0, m - b))\n$$  \n\nWait — better known solution from similar problems:  \n\nThe maximal number of balls we can extract such that we are **guaranteed** not to exceed $ n $ red and $ m $ green is:  \n\n$$\nx = \\min(a + b + c, \\; n + m + \\min(c, \\max(0, n - a) + \\max(0, m - b)))\n$$  \n\nNo — standard solution:  \n\nWe must ensure that even if all unknown balls are red, we don’t exceed $ n $ red, and same for green.  \n\nActually, the correct and known solution is:  \n\nWe can take at most $ n $ red balls (so we cannot take more than $ n $ red, even if unknowns are red). Similarly, at most $ m $ green.  \n\nBut the unknown balls can be assigned to either color. To guarantee safety, the total number of balls we can extract is:  \n\n$$\nx = \\min(a + b + c, \\; n + m + c - \\max(0, a - n) - \\max(0, b - m))\n$$  \n\nStill messy.  \n\n**Correct and clean formulation:**  \n\nTo guarantee that the number of red balls in the extracted set is ≤ $ n $, we must not extract more than $ n $ red balls. Since there are $ a $ known red balls, we can extract at most $ n $ red balls total — meaning we can extract at most $ \\min(a, n) $ known red balls, and up to $ \\min(c, n - \\min(a, n)) = \\min(c, \\max(0, n - a)) $ unknown balls as red.  \n\nSimilarly for green: extract at most $ \\min(b, m) $ known green, and $ \\min(c, \\max(0, m - b)) $ unknown as green.  \n\nBut the unknown balls are a single pool — they can't be both red and green. So the total number of unknown balls we can use is limited by $ c $, and we must allocate them to red and green without exceeding the deficits $ \\max(0, n - a) $ and $ \\max(0, m - b) $.  \n\nThe maximum number of unknown balls we can use is:  \n$$\n\\min(c, \\max(0, n - a) + \\max(0, m - b))\n$$  \n\nThen total extractable balls:  \n$$\nx = \\min(a, n) + \\min(b, m) + \\min(c, \\max(0, n - a) + \\max(0, m - b))\n$$  \n\nBut this is bounded by total balls: $ a + b + c $, so:  \n\n$$\nx = \\min(a + b + c, \\; \\min(a, n) + \\min(b, m) + \\min(c, \\max(0, n - a) + \\max(0, m - b)))\n$$  \n\nThis is correct.  \n\nBut note: $ \\min(a, n) + \\min(b, m) + \\min(c, \\max(0, n - a) + \\max(0, m - b)) $ is always ≤ $ a + b + c $, so we can drop the outer min.  \n\nWait:  \nLet’s test with $ a=10, b=10, c=10, n=5, m=5 $:  \nThen $ \\min(a,n)=5 $, $ \\min(b,m)=5 $, $ \\max(0,n-a)=0 $, $ \\max(0,m-b)=0 $, so $ \\min(c,0)=0 $, total = 10.  \nBut total balls = 30, and we can only take 10 balls (5 red, 5 green), so 10 is correct.  \n\nAnother: $ a=3, b=4, c=5, n=6, m=5 $:  \n$ \\min(a,n)=3 $, $ \\min(b,m)=4 $, $ \\max(0,n-a)=3 $, $ \\max(0,m-b)=1 $, sum=4, $ \\min(c,4)=4 $, total=3+4+4=11.  \nTotal balls=12, so 11 is safe.  \n\nCan we take 12? Only if all 5 unknowns are green → then green=4+5=9 > m=5 → violates.  \nSo 11 is max.  \n\nThus, the formula holds.  \n\nBut note: $ \\max(0, n - a) + \\max(0, m - b) $ is the maximum number of unknown balls we can assign to red and green to meet the caps.  \n\nSo final:  \n\n**Objective**  \nCompute:  \n$$\nx = \\min(a, n) + \\min(b, m) + \\min\\left(c, \\max(0, n - a) + \\max(0, m - b)\\right)\n$$","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10134C","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}