## # 1298. Maximum Candies You Can Get from Boxes

Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where:

status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed.
candies[i]: an integer representing the number of candies in box[i].
keys[i]: an array contains the indices of the boxes you can open with the key in box[i].
containedBoxes[i]: an array contains the indices of the boxes found in box[i].
You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.

Return the maximum number of candies you can get following the rules above.

Example 1:

``````Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],,[]], containedBoxes = [[1,2],,[],[]], initialBoxes = 
Output: 16
Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2.
In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed.
Total number of candies collected = 7 + 4 + 5 = 16 candy.
``````

Example 2:

``````Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = 
Output: 6
Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6.
``````

Example 3:

``````Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = 
Output: 1
``````

Example 4:

``````Input: status = , candies = , keys = [[]], containedBoxes = [[]], initialBoxes = []
Output: 0
``````

Example 5:

``````Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0]
Output: 7
``````

### # Solution

Approach 1: just do it.

### # Code (Python)

Approach 1:

``````    def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int:
# can get candy if box is available (contained, or initially given) AND open (initially or has key)
boxes_available = set([]) # available but can't open
boxes_ready = [] # available and can open
extra_keys = set([])

for box in initialBoxes:
if status[box] == 1:
else:

total_candies = 0

candy, new_keys, new_boxes = candies[box], keys[box], containedBoxes[box]
total_candies += candy

for key in new_keys:
for new_box in new_boxes:
if status[new_box] == 1:
else:

new_ready_boxes = boxes_available & extra_keys
for box in new_ready_boxes: