C. Urn with Balls

Codeforces
IDCF10134C
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
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? The 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. Output a single integer — the maximal number of balls that can be taken out of the urn so that no restrictions are violated. ## Input The 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. ## Output Output a single integer — the maximal number of balls that can be taken out of the urn so that no restrictions are violated. [samples]
**Definitions** Let $ a, b, c \in \mathbb{Z}_{\geq 0} $ denote the number of red, green, and unknown-colored balls in the urn, respectively. Let $ n, m \in \mathbb{Z}_{\geq 0} $ denote the maximum allowed red and green balls in the extracted subset, respectively. **Constraints** The unknown-colored balls may be any combination of red and green; the worst-case assignment (to violate constraints) must be avoided. **Objective** Find 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. This is equivalent to: $$ x = \min(a + c, n + c) + \min(b + c, m + c) - c $$ but constrained by the fact that the unknown balls cannot be double-counted. Better formalization: The 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 $. To ensure **no more than $ n $ red** and **no more than $ m $ green** in the extracted set, we must have: - Extracted red ≤ $ \min(a + c, n) $ - Extracted green ≤ $ \min(b + c, m) $ But since the unknown balls are shared between the two, the total extracted balls $ x $ must satisfy: $$ x \leq \min(a + c, n) + \min(b + c, m) - \max(0, \min(a + c, n) + \min(b + c, m) - (a + b + c)) $$ Simpler and correct approach: We 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: $$ x = \min(a + b + c, \; n + m + c - \max(0, n - a) - \max(0, m - b)) $$ Wait — better known solution from similar problems: The maximal number of balls we can extract such that we are **guaranteed** not to exceed $ n $ red and $ m $ green is: $$ x = \min(a + b + c, \; n + m + \min(c, \max(0, n - a) + \max(0, m - b))) $$ No — standard solution: We must ensure that even if all unknown balls are red, we don’t exceed $ n $ red, and same for green. Actually, the correct and known solution is: We 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. But the unknown balls can be assigned to either color. To guarantee safety, the total number of balls we can extract is: $$ x = \min(a + b + c, \; n + m + c - \max(0, a - n) - \max(0, b - m)) $$ Still messy. **Correct and clean formulation:** To 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. Similarly for green: extract at most $ \min(b, m) $ known green, and $ \min(c, \max(0, m - b)) $ unknown as green. But 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) $. The maximum number of unknown balls we can use is: $$ \min(c, \max(0, n - a) + \max(0, m - b)) $$ Then total extractable balls: $$ x = \min(a, n) + \min(b, m) + \min(c, \max(0, n - a) + \max(0, m - b)) $$ But this is bounded by total balls: $ a + b + c $, so: $$ x = \min(a + b + c, \; \min(a, n) + \min(b, m) + \min(c, \max(0, n - a) + \max(0, m - b))) $$ This is correct. But 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. Wait: Let’s test with $ a=10, b=10, c=10, n=5, m=5 $: Then $ \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. But total balls = 30, and we can only take 10 balls (5 red, 5 green), so 10 is correct. Another: $ a=3, b=4, c=5, n=6, m=5 $: $ \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. Total balls=12, so 11 is safe. Can we take 12? Only if all 5 unknowns are green → then green=4+5=9 > m=5 → violates. So 11 is max. Thus, the formula holds. But 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. So final: **Objective** Compute: $$ x = \min(a, n) + \min(b, m) + \min\left(c, \max(0, n - a) + \max(0, m - b)\right) $$
API Response (JSON)
{
  "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 gr...",
      "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...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments