<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>

<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 5, limit = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3, limit = 3
<strong>Output:</strong> 10
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 50</code></li>
	<li><code>1 &lt;= limit &lt;= 50</code></li>
</ul>

class Solution:
    def distributeCandies(self, n: int, limit: int) -> int:
        
        if 3 * limit < n:
            print(f'too many candies for 3 piles of {limit}')
            return 0

        minA = max(0, n - (2 * limit))
        maxA = min(n, limit)

        print(f'A: {minA}..{maxA}')
        answer = 0
        for childA in range(minA, maxA + 1):
            leftForBC = n - childA
            print(f'{childA=} {leftForBC=}')
            minB = leftForBC - limit    # again, B can't leave more than C can take
            # again, no negative candies
            if minB < 0:
                # lower the maximum by however much below zero the minimum was
                maxB = limit + minB
                minB = 0
            else:
                maxB = limit
            for childB in range(minB, maxB + 1):
                childC = leftForBC - childB
                print(f'  {childB=} {childC=}')
                if childC > limit:
                    print(f'    ERROR: {childC=} > {limit=}')
                    continue
                if childC < 0:
                    print(f'    less than zero')
                    continue
                answer += 1
            # no need to loop through each answer: grab the entire B/C group at once
            # BC = maxB - minB + 1
            # print(f'  child B/C {minB}..{maxB} ({BC})')
            # answer += BC

        return answer

120