Tap here to switch tabs
Problem
Submissions

Problem: Maximum Candies You Can Get from Boxes

Try to solve the Maximum Candies You Can Get from Boxes problem.

Statement

You are given nn boxes labeled from 00 to n−1n - 1. You are provided with the following arrays:

  • status: An integer array of length nn, where status[i] is 11 if the ithi^{ {th}} box is open, and 00 if it is closed.

  • candies: An integer array of length nn, where candies[i] represents the number of candies inside the ithi^{ {th}} box.

  • keys: A 22D integer array, where keys[i] contains the labels of boxes for which keys can be found inside the ithi^{ {th}} box.

  • containedBoxes: A 22D integer array, where containedBoxes[i] contains the labels of other boxes located inside the ithi^{ {th}} box.

  • initialBoxes: An integer array containing the labels of the boxes you start with.

You can open a box if you possess it and it is either already open or you have obtained its corresponding key. When you open a box, you collect all the candies inside it, obtain all the keys contained within it, and discover all the new boxes inside it.

Return the maximum number of candies you can collect by opening the available boxes according to these rules.

Constraints:

  • n == status.length == candies.length == keys.length == containedBoxes.length

  • 1≤n≤10001 \leq n \leq 1000

  • status[i] is either 00 or 11.

  • 1≤1 \leq candies[i] ≤1000\leq 1000

  • 0≤0 \leq keys[i].length ≤n\leq n

  • 0≤0 \leq keys[i][j] <n< n

  • All values of keys[i] are unique.

  • 0≤0 \leq containedBoxes[i].length ≤n\leq n

  • 0≤0 \leq containedBoxes[i][j] <n< n

  • All values of containedBoxes[i] are unique.

  • Each box is contained in at most one box.

  • 0≤0 \leq initialBoxes.length ≤n\leq n

  • 0≤0 \leq initialBoxes[i] <n< n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Candies You Can Get from Boxes

Try to solve the Maximum Candies You Can Get from Boxes problem.

Statement

You are given nn boxes labeled from 00 to n−1n - 1. You are provided with the following arrays:

  • status: An integer array of length nn, where status[i] is 11 if the ithi^{ {th}} box is open, and 00 if it is closed.

  • candies: An integer array of length nn, where candies[i] represents the number of candies inside the ithi^{ {th}} box.

  • keys: A 22D integer array, where keys[i] contains the labels of boxes for which keys can be found inside the ithi^{ {th}} box.

  • containedBoxes: A 22D integer array, where containedBoxes[i] contains the labels of other boxes located inside the ithi^{ {th}} box.

  • initialBoxes: An integer array containing the labels of the boxes you start with.

You can open a box if you possess it and it is either already open or you have obtained its corresponding key. When you open a box, you collect all the candies inside it, obtain all the keys contained within it, and discover all the new boxes inside it.

Return the maximum number of candies you can collect by opening the available boxes according to these rules.

Constraints:

  • n == status.length == candies.length == keys.length == containedBoxes.length

  • 1≤n≤10001 \leq n \leq 1000

  • status[i] is either 00 or 11.

  • 1≤1 \leq candies[i] ≤1000\leq 1000

  • 0≤0 \leq keys[i].length ≤n\leq n

  • 0≤0 \leq keys[i][j] <n< n

  • All values of keys[i] are unique.

  • 0≤0 \leq containedBoxes[i].length ≤n\leq n

  • 0≤0 \leq containedBoxes[i][j] <n< n

  • All values of containedBoxes[i] are unique.

  • Each box is contained in at most one box.

  • 0≤0 \leq initialBoxes.length ≤n\leq n

  • 0≤0 \leq initialBoxes[i] <n< n