leetle

Daily challenge for 2026-10-25

663. Cascade Failure Cost

A distributed network contains n nodes labeled from 0 to n - 1. You are given an array costs of length n where costs[i] represents the downtime cost if node i fails.

You are also given a list of directed edges edges, where each edge [u, v] indicates that a failure in node u propagates directly to node v.

A failure starts at node initial. Furthermore, a list of node labels protected specifies nodes that are fortified against failure: if a failure propagates to a protected node, the protected node does not fail, does not incur any cost, and stops the failure from propagating any further through its outgoing edges.

Calculate the total downtime cost across all uniquely affected nodes. If the initial node itself is protected, no nodes fail and the total cost is 0.

Constraints:
There are 1–500 nodes and at most 2,000 directed edges. Costs has exactly n non-negative integer entries at most 1,000,000. Every edge endpoint, protected node and initial node is a valid index. Cycles, self-loops and repeated protected entries are allowed.

Example:
Input: n = 4, edges = [[0, 1], [1, 2], [2, 3]], costs = [10, 20, 30, 40], initial = 0, protected = [2]
Output: 30

Run this challenge interactively in Python or JavaScript when the app loads.