Find most valuable subset of a node weighed tree Suppose you got a directed acyclic graph of positvely and negatively weighed nodes $T\hspace{-0.02 in}$.

Now I have to find the the subset ​ $M\subseteq T$ ​ such that the sum of

the weights of the nodes in $M$ is maximised, but if ​ $v \in M$ ​ then all

children of $v$ as well as their children, and so on have to be in $M$ too.

For now my algorithm is as follows:


calculate the depth of each node (how many nodes you have to
traverse at maximum to reach one which doesn't have any children)
for all nodes $v$ of depth $n$ do the following:


add up the weight of $v$ and the weights of all children and 
grandchildren,
... (you get the idea) of $v$
if the last step added up to a positive value then mark $v$ and
all children of $v$ as in $M$ and don't consider them any further
if the last step marked any node with the current depth then repeat
for this depth (since the sums changed) otherwise increment n.



This works fine for some DAGs: ​ ​ ​ (Note: ​ the edges are directed downwards)


As expected all nodes except the top node are in $M$

But for othes the algorithm falls short:

 

The algorithm outputs that ​ $M = \emptyset$ , ​ whereas ​ $M=T$ ​ should be the case.

Is there an algorithm which solves a similar problem?

If no, how can I correct my solution? ​ Any help is highly appreciated.

PS: ​ ​ ​ I apollogize for the use of MS Paint. ​ Sorry for that.
I forget the name but it's a classic minimum-cut problem. You can always consider min-cut approach when you want to choose an optimal subset. 

(Edit: It's called "project selection problem".)

Think about cutting nodes into two partitions $S$, $T$ and $S$ is what we choose. Ideally we want $S$ to contain all the positive nodes and avoid any negative node. However, it's not always possible. If there exists an edge $(u,v)$, $u\in S\to v\in S$. In the min-cut model, it means we can't cut this edge, so we set $capacity(u,v)=\inf$. Next consider what we should pay if we can't assign them ideally.

Create a source $s$ and a target $t$. For each node $u$, if $w_u$ is positive, add an edge $(s,u)$ and set $capacity(s,u)=w_u$. It means we lose $w_u$ if $u\notin S$. If $w_u$ is negative, add $(u,t)$ and set $capacity(u,t)=-w_u$. It means we lose $-w_u$ if $u \in S$. 

The answer is $\sum_{w_u&gt;0} w_u-min cut(s,t)$, and $M$ is the source partition.