You are given a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1, represented by a 2D array edges, where edges[i] = [u_i, v_i] indicates a directed edge from node u_i to v_i.
Each node has an associated score given in an array score, where score[i] represents the score of node i.
You must process the nodes in a valid topological order. Each node is assigned a 1-based position in the processing order. The profit is calculated by summing up the product of each node's score and its position in the ordering.
Return the maximum possible profit achievable with an optimal topological order.
A topological order of a DAG is a linear ordering of its nodes such that for every directed edge u → v, node u comes before v in the ordering.
Input & Output
Constraints
- 1 ≤ score.length ≤ 105
- 0 ≤ edges.length ≤ min(score.length × (score.length - 1) / 2, 105)
- edges[i].length == 2
- 0 ≤ edges[i][0], edges[i][1] ≤ score.length - 1
- edges[i][0] ≠ edges[i][1]
- There are no duplicate edges
- The graph is a valid DAG
- 1 ≤ score[i] ≤ 108