Skip to content

Practice Maximum Profit from Valid Topological Order in DAG

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3530
LeetCode Problem

Maximum Profit from Valid Topological Order in DAG

Problem Statement

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] = [ui, vi] indicates a directed edge from node ui to vi. 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.

Source: Maximum Profit from Valid Topological Order in DAG on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 2, edges = [[0,1]], score = [2,3]
Output
8

# Example 2

Input
n = 3, edges = [[0,1],[0,2]], score = [1,6,3]
Output
25

Constraints

1 <= n == score.length <= 22
1 <= score[i] <= 10^5
0 <= edges.length <= n * (n - 1) / 2
edges[i] == [ui, vi] denotes a directed edge from ui to vi.
0 <= ui, vi < n
ui != vi
The input graph is guaranteed to be a DAG.
There are no duplicate edges.

Related LeetCode Problems

Practice more problems similar to Maximum Profit from Valid Topological Order in DAG.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.