We are given a problem: There are N vertices and M edges, directed? Actually edges are given as pairs (u, v). Also given is an array A of size N (values for each vertex). There is a root? Not exactly. It says: "You are given a rooted tree with N nodes, but... Actually reading the problem statement from typical contest: Probably it's a directed tree? However the sample: N=6, M=6 edges: (1,2), (1,4), (2,6), (5,4), (6,5), (3,2). That is a graph, but seems like it might be a rooted tree? Let's check: N=6, edges 6, tree should have N-1 edges. So it's not a tree, it's a graph. Possibly it's a directed graph? The problem statement missing. Let's reconstruct from sample.
Given N nodes, each with value A_i. M edges. You have to find a set S of nodes such that: S is "good" meaning? Possibly S is a set where for every node u in S, all its ancestors (or children?) are also in S? Or maybe it's a set that is closed under reachability? Since edges are directed, maybe it's a directed graph, and a good set is one that if you take a node, you must also take all nodes reachable from it? Or the other way? Actually typical problem: "Find a subset of vertices that is closed under outgoing edges (i.e., if a node is in S then all its outgoing neighbors are also in S) and maximize sum of values" is like finding a closure in a directed graph. That is classic maximum weight closure problem, solvable via min-cut.
Check sample: N=6, M=6 edges, values: [3,1,9,4,3,6] (index 1..6). Edges: 1->2, 1->4, 2->6, 5->4, 6->5, 3->2. Compute max weight closure. Let's compute potentials: Positive values: all positive except maybe 1? Actually 3,1,9,4,3,6 all positive. So all positive, trivial closure would be all nodes if there's no constraint? But closure must satisfy if you include a node, you must include all its outgoing neighbors. So if we include node 1, we must include 2 and 4. Include 2, must include 6. Include 6, must include 5. Include 5, must include 4 (already). Include 3, must include 2, so then 6 and 5 and 4. So if we include any node with outgoing edges, we may be forced to include many. But since all values are positive, the maximum closure would be all nodes if the whole set is closed? Check if all nodes set is closed: For each node, its outgoing neighbors are in set. Yes, if we include all nodes, condition satisfied. So closure = all nodes, sum = 3+1+9+4+3+6 = 26. But sample output is 17. So it's not that.
Perhaps it's the other way: If you include a node, you must include all its incoming neighbors? That seems less typical. Or maybe it's an undirected graph and a "good set" is an independent set? But 17 sum: could be nodes with values: 9+4+3+? = 16? Actually 9+4+3 =16, plus 1=17. So maybe nodes {3,4,5,2?} 3 is 9, 4 is 4, 5 is 3, sum=16, need 1 more maybe node1 value3? That would be 19. Not 17. Or {3,4,6} =9+4+6=19. {1,4,5,6} =3+4+3+6=16. {1,2,4,5} =3+1+4+3=11. Hmm.
Alternatively maybe it's a rooted tree and we need a "good" set that is a rooted connected subgraph? But sample M=N, so it's a tree plus one extra edge? Actually N=6, M=6 edges, if it's a tree it would have 5 edges. So it's not a tree. Could be that the graph is actually a directed graph representing a "functional graph"? Or maybe it's a graph and the problem is to find a set S such that for every edge (u,v), if u is in S then v is NOT in S? That would be an independent set. Let's test: Find maximum weight independent set in a directed graph? Usually independent set is for undirected graph. But if we interpret edges as undirected (since they are just pairs), we could find maximum weight independent set in a general graph, which is NP-hard. But constraints? Possibly N up to 2e5? So not NP-hard. So likely it's something solvable with min-cut.
Given sample output 17, perhaps the solution is to compute something like maximum weight closure after converting negative values? But all values are positive. So perhaps the condition is the opposite: You can choose a set, but for each edge (u,v) if u is chosen then v must NOT be chosen? That would be like bipartite? Not clear.
Let's read the input format: first line N M, then N lines each with A_i, then M lines each with u v. Possibly the edges are directed from u to v? Or maybe it's an undirected tree? The sample N=6 M=6 is ambiguous.
Let's check the sample edges: 1-2, 1-4, 2-6, 5-4, 6-5, 3-2. If we draw as undirected, the graph is: 1 connected to 2,4; 2 to 1,6,3; 3 to 2; 4 to 1,5; 5 to 4,6; 6 to 2,5. That's a cycle? 1-2-6-5-4-1 is a cycle of length 5? Actually 1-2-6-5-4-1 is a cycle of length 5? Wait edges: 1-2, 2-6, 6-5, 5-4, 4-1. That's a cycle of 5 nodes (1-2-6-5-4-1) yes it's a cycle. Plus 3 attached to 2. So it's a cycle with a pendant 3. So it's an undirected graph with a cycle. The maximum weight independent set on this graph? Let's compute values: 1:3, 2:1, 3:9, 4:4, 5:3, 6:6. We want independent set (no two adjacent). Compute manually: The graph is a 5-cycle (1-2-6-5-4-1) and node 3 attached to 2. For the 5-cycle, maximum weight independent set can be computed. We'll try: Choose nodes with high values: 3 is 9 but adjacent only to 2. So if we pick 3, we cannot pick 2. Then in the cycle, we consider remaining nodes 1,4,5,6,2 excluded? Actually 2 excluded. So the cycle becomes: 1-? edges: 1-4, 1-2 (but 2 excluded, so edge 1-2 not relevant), 2-6 (2 excluded, so 6 is not adjacent via 2, but 6 is still adjacent to 5 and 2? Actually 6 adjacent to 2 and 5. So edges in the cycle after removing 2: 1-4, 4-5, 5-6, and also 1-? missing 1-2, 2-6. So essentially it's a path 1-4-5-6? Actually edges: 1-4, 4-5, 5-6. Also is there edge 1-? no. So it's a path of 4 nodes: 1-4-5-6. Maximum independent set on path: choose max sum by DP. Values: 1:3, 4:4, 5:3, 6:6. Best independent set: can't pick adjacent. For path, you can compute: choose 4 and 6 sum=10, or 1 and 5 and? Actually 1 and 5 are not adjacent (1 adjacent to 4 only, 5 adjacent to 4 and6). 1 and 5 sum=6, 1 and 6 sum=9, 4 and 6 sum=10, 3? Actually 1,5,? Could also choose 1 and 6? Are 1 and 6 adjacent? No direct edge. So 1 and 6 sum=9. So best is 4+6=10. So total with 3 is 9+10=19. That's 19, not 17.
If we do not pick 3, we can pick from whole cycle. For a 5-cycle, max independent set: we can pick either 2 nodes? Actually cycle of odd length, max independent set size = floor(5/2)=2, but weight matters. Compute all sets of non-adjacent vertices: possible picks: {1,3? no 3 not in cycle}, in cycle nodes: 1,2,4,5,6. Find independent sets: (1,3? 3 is separate). For the cycle, independent sets: {1,5} values 3+3=6; {1,6}? 1-6 not adjacent? No edge 1-6, so {1,6} is allowed? Check edges: 1 adjacent to 2 and 4. 6 adjacent to 2 and 5. So 1 and 6 are not adjacent, yes. So {1,6}=3+6=9; {2,4}=1+4=5; {2,5}=1+3=4; {2,? 2 and? 2 adjacent to 1,6,3; so cannot pick 1 or 6 or 3. So {2,4} allowed, {2,5} allowed? 2-5 not adjacent, yes; {4,6}=4+6=10 but are 4 and 6 adjacent? 4 adjacent to 1 and 5. 6 adjacent to 2 and 5. So 4 and 6 are not directly connected (unless there is edge 4-6, none). So {4,6}=10 allowed. Also {5,1?} 5 and 1? 5 adjacent to 4 and 6; 1 adjacent to 2 and 4; so 5 and 1 not adjacent => 3+3=6. Also could pick three nodes? In a 5-cycle, maximum independent set size is 2 because odd. So max weight among these: {4,6}=10. Also {1,6}=9, {2,4}=5, {2,5}=4. So best is 10. Then add node 3 maybe? But if we include 3, we can't include 2. But if we already have set {4,6} from cycle, does it conflict with 3? 3 is only adjacent to 2, so no conflict. So we could also include 3 with {4,6}? That gives 9+4+6=19 as computed earlier. So independent set maximum weight seems 19, not 17. So not independent set.
Maybe it's "vertex cover"? Minimum weight vertex cover? But maximum weight vertex cover? Not typical.
Maybe it's "dominating set"? Unlikely.
Given that typical problem "Maximum weight closure in a directed graph" yields 26 for all positive, but sample is 17. So maybe the values can be negative? But they are all positive. Wait maybe the closure condition is: if you choose a node, you must also choose all nodes that can reach it? That is "predecessor closure". That is also a closure (if a node is in S, all its ancestors must be in S). That would be a closure in the reverse graph. But same issue: all positive -> all nodes closure works because if we choose a node, we must include its ancestors. If we take all nodes, condition holds because all ancestors of any node are in the set (since all nodes are in set). So sum = 26. So still not 17.
Maybe the set must be such that for every edge (u,v), exactly one of u or v is in the set? That's a cut? Actually that would be a vertex cut? Not typical.
Alternatively, maybe it's about selecting a set of vertices such that the induced subgraph is acyclic? Or something else.
Given that it's from a contest, perhaps it's "Maximum weight independent set in a tree" but M=N suggests it's a tree plus an extra edge? Actually N=6, M=6 edges; a tree would have 5 edges, so it's a tree with one extra edge, making a single cycle. But then the problem might be about selecting a set with no two adjacent and also some condition about the cycle? Could be maximum weight independent set on a cactus? But then sample answer 17, we found 19 as independent set. Could we get 17? Let's recompute values: maybe I mis-index. Input:
6 6
3
1
9
4
3
6
edges:
1 2
1 4
2 6
5 4
6 5
3 2
So values: node1=3, node2=1, node3=9, node4=4, node5=3, node6=6.
Our independent set computed 19 by taking nodes {3,4,6}? Wait {3,4,6} sum=9+4+6=19 but check adjacency: 4 and 6 are not adjacent? Edge list: 4 connected to 1 and 5, not 6. So {3,4,6} is independent? 3 adjacent to 2 only, ok. 4 adjacent to 1 and 5, ok. 6 adjacent to 2 and 5, ok. So set {3,4,6} is independent, sum=19. But sample output is 17, so maybe there is an extra constraint: maybe it's a directed graph and you need to pick a set that is "good" meaning if you pick a node, you must also pick all nodes reachable from it, and you want to minimize sum? No.
Another possibility: The problem could be "Find a subset S such that for every edge (u,v), either u is in S or v is in S" (a vertex cover) and we want to minimize sum? That would be minimum weight vertex cover. In a bipartite graph, it can be solved by max flow. But our graph is not bipartite? Let's check if it's bipartite: Graph has cycle of odd length (5-cycle is odd). So it's not bipartite. But min weight vertex cover in general graph is NP-hard. So unlikely.
Given typical contest problems, max weight closure is common and yields a solution via min cut. The sample has all positive values, but answer less than total, so maybe the closure condition includes also that you must exclude some nodes? Wait closure: choose a set S such that if a node is in S, then all its successors are in S. For all positive, best is all nodes. So why answer 17? Possibly the problem is to find a set that is "good" meaning that it is closed under incoming edges? i.e., if you pick a node, you must pick all its predecessors. That again would be all nodes if all positive. So not.
Maybe it's the opposite: "good" set means that for every edge (u,v), if u is in S then v is NOT in S. That's like a cut. But then maximizing sum of chosen nodes subject to that: choose nodes such that no edge goes from a chosen node to another chosen node. That is like selecting a set with no outgoing edges to other selected nodes. But that is essentially a set that is an independent set in the directed graph ignoring direction? Actually direction matters: condition only prohibits edges from chosen to another chosen? It says: if u in S then v not in S. So if both endpoints chosen, and there is an edge from u to v, it violates because then u in S implies v not in S. But if there is edge from v to u, that would be separate: v in S implies u not in S. So effectively, you cannot have an edge where the tail is in S and the head is in S. So it's like a "digraph closure" where you cannot have both endpoints of any directed edge in S. That is like a bipartition? But still NP-hard generally? Possibly it's reducible to maximum weight independent set in the underlying undirected graph? Because if you cannot have both endpoints of any directed edge in S, but if there is an edge in both directions (u->v and v->u) then that prohibits both being in S same as undirected edge. If only one direction, it still prohibits both being in S because if u in S, v must not be, regardless of v->u? But if v in S, does it force anything about u? Only edges with tail at v matter. So if we have only u->v, then S can contain v but not u? Because condition: if v in S, then we check edges from v to others; there is none from v to u, so it's allowed. But if u in S, then v not allowed. So this condition is not symmetric. So it's a kind of "orientation" constraint. Maximum weight feasible set might be solvable via min-cut? Possibly yes, because it's like we have constraints "if u in S then v not in S" which is of the form "u => not v", which can be represented in a flow network? Actually such constraints can be modeled as 2-SAT? But maximum weight satisfying assignment for Horn clauses? Not sure.
But can we get 17 from this? Let's test this interpretation: We need to pick set S maximizing sum, with constraint: for each directed edge (u,v), if u ∈ S then v ∉ S. Let's compute feasible sets and see if max sum is 17.
Values: 1:3, 2:1, 3:9, 4:4, 5:3, 6:6.
Edges: 1->2, 1->4, 2->6, 5->4, 6->5, 3->2.
Constraints:
- If 1 in S, then 2∉S and 4∉S.
- If 2 in S, then 6∉S.
- If 5 in S, then 4∉S.
- If 6 in S, then 5∉S.
- If 3 in S, then 2∉S.
We want max sum.
Let's try to compute by brute reasoning. Because all values positive, we want to include as many high-value nodes as possible while respecting constraints.
High values: 3:9, 6:6, 4:4, 1:3,5:3,2:1.
Constraint relationships: 3->2 means if include 3, cannot include 2. 2->6 means if include 2, cannot include 6. So 2 and 6 conflict indirectly? Actually if we include 2, we cannot include 6. If we include 6, does it force anything about 2? Only edges from 6: 6->5, so if 6 in S, then 5 not in S. No direct effect on 2. So we could include both 6 and 2? Only constraint from 2->6 says if 2 in S then 6 not in S. But if 6 in S, 2 can be in S only if we don't have edge from 6 to 2, but we have edge from 2 to 6, so condition applies: if 2 in S then 6 not in S, so if both in S, violation. So cannot have both 2 and 6.
Now 1: if 1 in S, then 2 and 4 not in S. So 1 excludes 2 and 4.
5->4: if 5 in S, then 4 not in S.
6->5: if 6 in S, then 5 not in S.
Also 5 and 6 have mutual constraint? 6->5 says if 6 in S then 5 not in S. But there is no edge from 5 to 6. So we could have 5 in S and 6 not? That's fine. Or both not, or only 6, but not both. So 6 and 5 cannot both be in S.
Goal: maximize sum.
We can consider including high value 3 (9). If we include 3, then 2 cannot be in S (since 3->2). 2 is low value (1) so fine. We can still include 6,4,5,1? Must check constraints.
With 3 in S, 2 out. Other constraints: we need to decide on 1,4,5,6.
Check constraints among these:
- If 1 in S, then 2 out (already out) and 4 out. So 1 forces 4 out.
- If 5 in S, then 4 out.
- If 6 in S, then 5 out.
- Also 2->6 doesn't matter because 2 out.
- 1->4, 5->4, 6->5.
We want to maximize sum from {1,4,5,6} with constraints: (1 -> not4), (5 -> not4), (6 -> not5). And also note that 4 and 5 are not mutually exclusive by themselves, only if 5 in then 4 out; but 4 does not force anything about 5. So we can have 4 and 6? Check: 4 and 6 have no direct constraint, but 6 forces 5 out, so if we take both 4 and 6, ok. Can we take 5 and 6? No because 6->5 prohibits both. Can we take 1 and 5? Yes, but note 1 forces 4 out, but 5 forces 4 out anyway, no conflict. However 1 and 5 together: 1 forces 4 out, okay. 1 and 6? Yes, but 1 forces 4 out, 6 forces 5 out. Fine. 1 and 4? Not allowed because 1->4 prohibits 4.
So we need to choose subset of {1,4,5,6} with constraints. Their values: 1:3, 4:4, 5:3, 6:6.
We want to maximize sum. Let's enumerate possibilities:
-
Include 6: then 5 cannot be included. So remaining {1,4} can be both? But if include 1, cannot include 4. So with 6, we can either include 1 (3) -> sum=6+3=9, or include 4 (4) -> sum=6+4=10, or include both 1 and 4? Not allowed. Or just 6 alone=6. So best with 6 is 6+4=10 (choose 6 and 4). Also could add 1? No, 1 and 4 conflict. So 10.
-
Exclude 6: then we can consider {1,4,5}. Constraints: 1-> not4, 5-> not4. So 4 cannot be with 1 or 5. So possibilities: include 4 alone: sum=4; include 1 and 5 together: sum=3+3=6 (since both don't conflict with each other, and they both force 4 out); include 1 alone=3; 5 alone=3; include 1,5,? none; So best without 6 is max(4,6)=6 (choose 1 and 5). Also could do 4+? no. So max from {1,4,5,6} is 10 (6+4). So total with 3: 9+10=19.
So S = {3,4,6} sum 9+4+6=19. That's same as independent set earlier. But sample output is 17. So not matching.
Maybe the constraint is symmetric: if u in S then v not in S, AND if v in S then u not in S (i.e., no edge between S vertices). That would be independent set (undirected). We computed max independent set is 19. Not 17.
Maybe the graph is directed and the condition is: For every edge (u,v), either u is in S or v is in S (vertex cover). Then we want to minimize sum? But then min weight vertex cover? Let's compute min weight vertex cover. Values all positive, need to cover all edges. Edges: (1,2), (1,4), (2,6), (5,4), (6,5), (3,2). Minimum weight set such that each edge has at least one endpoint. This is a vertex cover problem. Since graph is small, compute min weight vertex cover. Could be 17? Let's see. Total sum 26. Max independent set weight = total - min vertex cover weight (by complement, for any graph, the complement of a vertex cover is an independent set). Actually for any graph, a set is independent iff its complement is a vertex cover. So min weight vertex cover = total weight - max weight independent set. So max independent set we computed as 19? Let's verify if we missed any better independent set. Compute all independent sets systematically for this graph:
Nodes: 1,2,3,4,5,6. Adjacency list (undirected):
1: 2,4
2: 1,6,3
3: 2
4: 1,5
5: 4,6
6: 2,5
We want max weight independent set. Let's brute force by hand. Try to include 3 (9). Then cannot include 2. Remaining graph after removing 2 and 3? Actually we only exclude neighbors of chosen nodes: 3 excludes 2. So remaining nodes {1,4,5,6} with edges: 1-4, 4-5, 5-6, 1? 1-? only 4, 4-1,4-5,5-6,6? also 6? 6 adjacent to? 6 adjacent to 5 only? Actually 6 originally adjacent to 2 and 5. Since 2 excluded, only 5 remains. So it's a path 1-4-5-6. Max weight independent set on path: compute DP. Values: 1:3,4:4,5:3,6:6. Possibilities: choose 1 and 5? 1 and5 are not adjacent (1 adjacent to 4; 5 adjacent to 4 and6) so allowed. Sum=6. 1 and6? 1 and6 not adjacent? 1 adjacent to 4;6 adjacent to5; no edge, so allowed sum=9. 4 and6? not adjacent, sum=10. 4 alone=4, 6 alone=6, 5 alone=3, 1 alone=3, 4+? can't with 5 or1. So best is 4+6=10. So total 19.
Now consider not including 3. Then we can include 2 maybe. Let's compute max independent set without 3 (or with 3 not included). Actually we should consider cases to see if we can get more than 19. If we exclude 3, we can include 2? But 2 is low weight, maybe better exclude 2 to include others. Let's systematically compute maximum independent set weight for whole graph. Since graph is small, we can brute force all subsets. But let's do smarter: The pendant node 3 with high weight suggests we should include it. Could we include 3 and also include 2? No, conflict. So we need to choose either 3 or 2. Since 3 is much larger, we choose 3 and exclude 2. So we are at 19.
What about including 6 and 4 and 3 gave 19. Could there be a set with 3,1,5,6? Check: 3 in, 2 out. Then 1,5,6 in: check edges: 1-4? but 4 not in. 1-2? 2 out, ok. 5-4? 4 out. 5-6? edge 5-6, both in -> conflict because they are adjacent. So not allowed. 3,1,5? that's 9+3+3=15, less. 3,1,6? 9+3+6=18, but check adjacency: 1-? 1 adjacent to 2 and 4, both out, ok. 6 adjacent to 2 and 5, but 5 out, so 6 ok. So set {3,1,6} sum=18, but is it independent? Need to ensure no edge between 1 and6: there is no edge, so allowed. So {3,1,6} sum=18, which is less than 19? Actually 18 < 19. {3,4,6}=19. {3,4,5}? 4-5 edge conflict. {3,4,1}? 1-4 edge conflict. {3,5,6}? 5-6 conflict. {3,4,2}? 3-2 conflict. {3,6,4} we have. {3,1,4} conflict. {3,1,5}? sum=15. So best seems 19.
But maybe there is an independent set of weight 20? {3,1,4,? can't}. {2,4,6}? sum=1+4+6=11. {2,4,5}? 4-5 conflict. {2,5,1}? 2-1 conflict. {2,5,6}? 2-6 conflict. {1,4,6}? 1-4 conflict. {1,5,6}? 5-6 conflict. {4,6,1} conflict. {3,6,1,?} we have 18. {3,4,6,?} can't add any other because 3 blocks 2, 4 blocks 1 and 5, 6 blocks 5 and 2. So 19.
Thus max independent set weight = 19. So min vertex cover weight = total - 19 = 7. That is not 17.
Wait total sum is 3+1+9+4+3+6 = 26. 26-19=7. So min vertex cover weight would be 7. Not 17.
Maybe the problem is to find maximum weight "closed" set but with a twist: maybe we are allowed to choose a set of vertices to "remove" and the answer is total sum of remaining? Actually sample output 17 is less than 26. So maybe we are required to select a set of vertices to delete, such that the remaining graph has no directed cycles? Or something like "maximum weight feedback vertex set"? That would be to remove vertices to break all cycles, minimize weight removed? Then remaining weight would be total - removed. If we want maximum remaining weight, it's equivalent to minimum weight feedback vertex set. For the sample, if we remove vertices with total weight 9 (i.e., 26-17=9), then feedback vertex set weight 9. Could that be minimal? Let's test: Graph (directed) might have cycles. Directed cycles in the graph: 1->2->6->5->4->? Actually 4->? there is edge 1->4, so we have 1->4? Actually edges: 1->2,1->4,2->6,5->4,6->5,3->2. So cycles: 1->4? Wait 4 has no outgoing edges? Actually edges are directed from u to v. So we have 1->2, 2->6, 6->5, 5->4, and 1->4 is also an edge but not part of a cycle unless there is edge from 4 to something to get back to 1. There is no edge from 4 to 1; it's directed 1->4, not both ways. So for directed cycles, we need a directed cycle. Let's list directed edges: 1->2, 1->4, 2->6, 5->4, 6->5, 3->2. So possible directed cycles: 2->6->5->? There is no edge from 5 back to 2. 6->5->4->? No outgoing from 4. 1->2->6->5->4->? no. So no directed cycle? Actually check: 1->4, but no edge out of 4. So no directed cycles. So feedback vertex set trivial.
Maybe it's undirected cycle, then feedback vertex set (to make it a forest) would be to break cycles. Graph has one cycle: 1-2-6-5-4-1 (5-cycle). Minimum weight vertex cover on the cycle? But feedback vertex set in undirected graph is a set of vertices whose removal makes the graph acyclic (forest). Minimum weight feedback vertex set is NP-hard in general but polynomial for cactus? But N up to maybe 2e5? Could be solved with DP on tree plus one extra edge? But the sample has one cycle, and answer 17 would correspond to removing vertices total weight 9 to break cycle. Let's check: To break the 5-cycle, we need to remove at least? In a cycle, you need to remove at least one vertex to break the cycle. Minimum weight vertex on cycle: weights: 1:3,2:1,4:4,5:3,6:6. Minimum weight is 1 (vertex 2). But also there is node 3 attached to 2, which is a tree. Removing vertex 2 also breaks the connection to 3? Actually if we remove 2, the graph becomes: nodes 1,3,4,5,6 with edges: 1-4, 4-5, 5-6, and 3 isolated? Actually 3 was connected to 2 only, so after removing 2, 3 becomes isolated, okay. The remaining graph has edges: 1-4,4-5,5-6. That's a path (acyclic). So removing vertex 2 (weight 1) makes the graph a forest. Then remaining weight = 26-1=25, not 17. So not that.
If we need to remove vertices to make it a bipartite? Not likely.
Maybe the problem is about selecting a set of vertices that form a "chain" or "path"? Hmm.
Let's think about the sample output 17. Which set of nodes sum to 17? 9+4+3+1 = 17 (3,4,5,2? 9+4+3+1=17). That set is {2,3,4,5}. Check adjacency? In undirected: 2 connected to 3,1,6; 3 connected to 2; 4 connected to 1,5; 5 connected to 4,6; So {2,3,4,5} includes edge 2-3 (adjacent) and 4-5 (adjacent), so not independent. In directed constraints earlier (if u in S then v not in S) we can test {2,3,4,5}: Check each directed edge:
- 1->2: 1 not in S, ok.
- 1->4: 1 not in S, ok.
- 2->6: 2 in S => 6 must not be in S. 6 is not in S, ok.
- 5->4: 5 in S => 4 must not be in S. But 4 is in S! Violation. So not allowed.
- 6->5: 6 not in S, ok.
- 3->2: 3 in S => 2 must not be in S. But 2 is in S, violation.
So fails.
Set {3,4,5,1}? sum=9+4+3+3=19.
Set {3,4,6,?} sum=19.
Set {3,5,6,?} sum=9+3+6=18.
Set {3,4,2}? sum=9+4+1=14.
Set {3,5,2}? 9+3+1=13.
Set {6,4,2}? 6+4+1=11.
Set {6,4,1}? 6+4+3=13.
Set {6,5,1}? 6+3+3=12.
Set {6,5,4}? conflict 5-4? edge 5->4 forces 4 out if 5 in? Actually 5->4: if 5 in S then 4 not in S. So 5 and 4 cannot both be in. So not allowed.
Set {6,4,3}? 6+4+9=19.
Set {6,5,3}? 6+3+9=18 but check 5->4? no constraint on 3; 6->5 prohibits 5 if 6 in? Actually 6->5: if 6 in S then 5 not in S. So 6 and 5 cannot both be in. So {6,5,3} invalid.
Set {1,4,6,?} 1-4 conflict? 1->4 prohibits 4 if 1 in. So invalid.
Set {1,5,6}? 1 in, 5 in, 6 in: check 1->? 1->2,4 but those not in S; 1->4 forces 4 out, but 4 not in; 5->4 forces 4 out (ok); 6->5 forces 5 out, but 5 is in S, violation. So invalid.
Set {1,5,4}? 1->4 prohibits 4, violation.
Set {1,2,6}? 1->2 prohibits 2, violation.
Set {2,4,6}? 2->6 prohibits 6, violation.
Set {2,4,5}? 2->6 (ok), 5->4 prohibits 4, violation.
Set {2,5,6}? 2->6 prohibits 6, violation.
Set {3,1,5}? 3->2 ok, 1->4 ok, 5->4 ok, no edge from 5 to something? Also 6 not in, so maybe ok? Check constraints: 3 in S => 2 not in S, ok. 1 in S => 2 and 4 not in S, ok. 5 in S => 4 not in S, ok. No edge from 5 to something else? 5->4 only. So {3,1,5} sum=9+3+3=15, allowed? Also need to check edges from nodes not in S to S? No, only condition is if u in S then v not in S. So if u not in S, no constraint. So {3,1,5} seems valid. But sum 15 <17.
Set {3,1,4}? 1->4 prohibits 4, violation.
Set {3,2,4}? 3->2 prohibits 2, violation.
Set {3,2,5}? 3->2 prohibits 2, violation.
Set {3,2,6}? 3->2 prohibits 2, violation.
Set {3,4,5}? 5->4 prohibits 4, violation.
Set {3,4,6}? 3->2 ok, 4 no outgoing? Actually 4 has no outgoing edges. 6->5 prohibits 5, but 5 not in S, ok. So {3,4,6} valid, sum=19.
Set {3,5,6}? 6->5 prohibits 5, violation.
Set {3,4,1}? violation.
Set {3,4,2}? violation.
Set {3,6,1}? check: 1->4 ok (4 not in), 1->2 ok, 6->5 ok, 3->2 ok. So {3,6,1} valid sum=9+6+3=18.
Set {3,6,4} =19.
Set {3,6,5} invalid.
Set {3,1,6} =18.
Set {3,1,5} =15.
Set {3,4,6} =19.
So max seems 19. So why answer 17?
Maybe the constraint is reversed: If u in S, then v must be in S as well (closure). Then all positive would give total 26. Not 17.
Maybe it's: Choose a subset S such that for every edge (u,v), u in S implies v in S, and also if v in S then u in S? That would be that S must be a union of connected components? Actually that would be: for each edge, both endpoints are either both in S or both out. That's a "cut" but also closure? That would be like a connected condition? But that condition means that if an edge has one endpoint in S, the other must also be in S. That means S is a union of connected components? Actually if you have an edge between u and v, then you cannot have exactly one of them in S. So either both in or both out. That means S must be a (possibly empty) union of connected components of the graph (where each component is considered as a whole). So the maximum weight set would be the sum of positive-weight components. Here the graph is connected (one component) so either take all nodes (sum 26) or none (0). So answer would be 26. Not 17.
Maybe it's: Choose S such that for every edge (u,v), either u in S or v in S (vertex cover). Then we want maximum weight vertex cover? That would be total weight (since covering all edges, you could take all nodes). But we want maximum weight? Usually vertex cover minimization. Not 17.
Maybe it's: Choose S such that S is a "feedback vertex set" (remove to make acyclic) and we want to minimize weight removed; answer is weight of remaining = total - removed. In sample, total 26, answer 17 so removed weight 9. Could minimal feedback vertex set weight be 9? Let's test: For the directed graph, we need to break all directed cycles. As we saw, there are no directed cycles? Let's re-examine: Directed edges: 1->2,1->4,2->6,5->4,6->5,3->2. Are there any directed cycles? Starting from 1: 1->2->6->5->? 5->4, from 4 no outgoing. 1->4, stop. 2->6->5->4 stop. 3->2->6->5->4 stop. 6->5->4 stop. 5->4 stop. So no directed cycle. So feedback vertex set weight could be 0; then remaining weight 26. So not 17.
Maybe it's undirected graph and we want maximum weight induced forest? Or maximum weight subset that induces a forest (i.e., acyclic). That is maximum weight induced subgraph without cycles. That is the complement of minimum feedback vertex set. So we want to select a subset of vertices such that the induced subgraph is a forest, and maximize sum of weights. That is known as Maximum Weight Induced Forest (or Maximum Weight Feedback Vertex Set complement). That is NP-hard in general but maybe special graph? However, the graph in sample is a cycle with a pendant. The maximum weight induced forest: we need to avoid cycles. The induced subgraph must be acyclic. So we cannot include all vertices of a cycle. In the 5-cycle (1-2-6-5-4-1), we must omit at least one vertex from the cycle to break it. So we can include at most 4 vertices from the cycle. Additionally, node 3 is attached to 2, so including 3 is fine as long as we don't create a cycle through 2? Actually including 3 and the cycle nodes may still be acyclic if we break the cycle sufficiently. So we want to choose a subset of vertices maximizing total weight such that the induced subgraph has no cycles.
Compute: total vertices 6. To avoid cycles, we must delete at least 1 vertex from the cycle. Which vertex deletion yields maximum sum of remaining? Delete the smallest weight vertex from the cycle: vertices in cycle: 1,2,4,5,6. Node 2 weight 1 is smallest. Delete node 2. Then we can include all other vertices: 1,3,4,5,6. Check induced subgraph: vertices {1,3,4,5,6}. Edges among them: 1-4, 4-5, 5-6, and 3 is isolated. There is no cycle (since after removing 2, the cycle becomes a path 1-4-5-6). So total weight = 3+9+4+3+6 = 25. That's much larger than 17. If we delete node 1 (weight 3), then include {2,3,4,5,6}. Induced edges: 2-6, 4-5, 5-6, and 2-? 2-6, also 2-3? 2-3 edge? Yes 2-3 edge exists (since edge 3-2). So subgraph: edges 2-6, 2-3, 4-5, 5-6. That graph contains a cycle? Let's see: 2-6-5-? 5-4? 4-? not connected to 2. So possible cycles? 2-6-5-? no edge 5-2. So no cycle? Check 2-6-5-4? 4 not connected to 2. So it's acyclic. So weight = 1+9+4+3+6 = 23. Delete node 4 (weight 4): include {1,2,3,5,6}. Edges: 1-2, 2-6, 2-3, 5-6, 1-? also 1-4? no 4 removed. Edges: 1-2, 2-6, 2-3, 5-6, and also maybe 1? also edge 1-? no. This graph: 1-2-6-5, and 2-3. Is there a cycle? 1-2-6-5-? 5 to? only 6. 5 not connected to 1 or 2 besides 6. So no cycle. Weight = 3+1+9+3+6=22. Delete node 5 (weight 3): include {1,2,3,4,6}. Edges: 1-2,1-4,2-6,2-3,4? 4-? only 1, and 5 removed, so no 4-5. Also 6-? 6-2, and maybe 6-5 removed. So edges: 1-2,1-4,2-6,2-3. That's a tree? Actually 1-2-6, 1-4, 2-3. It's acyclic. Weight = 3+1+9+4+6=23. Delete node 6 (weight 6): include {1,2,3,4,5}. Edges: 1-2,1-4,2-? 2-? 2-3, 2-6? removed, 4-5, 5-? 5-6 removed. So edges: 1-2,1-4,2-3,4-5. This is acyclic. Weight = 3+1+9+4+3=20. So best induced forest includes all but node 2, weight 25. So answer would be 25 if maximum weight induced forest, not 17.
Maybe it's maximum weight subset that induces a tree (connected and acyclic)? That would be more restrictive. The maximum weight tree in the graph? The whole graph is not a tree (has a cycle). So we need to select a subset that forms a tree. Could maximum weight tree be 17? Let's see: total weight 26, we need to drop some vertices to break cycle and also ensure connectivity? Possibly we need a connected induced subgraph that is a tree. That would be a spanning tree of the induced subgraph? Actually induced tree means the subgraph induced by the chosen vertices is a tree (connected and acyclic). So we need to pick a set of vertices such that the induced subgraph is a tree. For the given graph, what's the maximum weight induced tree? We can try to include many vertices but must avoid cycles and ensure connectivity (maybe connectivity is required?). If we require connectivity, then we may need to include vertices that connect the tree. For example, if we drop node 2, we have vertices {1,3,4,5,6} with induced edges: 1-4,4-5,5-6, and 3 isolated. So it's not connected (3 isolated). So that's not a tree (it's forest with two components). To have a tree, the induced subgraph must be connected. So we might need to include 2 to connect 3 to the rest, but then we have a cycle? If we include 2, we might need to drop another vertex to break the cycle. Let's find a set that induces a connected acyclic subgraph (i.e., a tree). The graph itself has a cycle, so we need to remove at least one vertex from the cycle to break it, and also ensure that the induced subgraph is connected. Also note node 3 is attached to 2, so to include 3 we must include 2 to keep connectivity (if we want the tree to be connected). But if we include 2, we are including a vertex from the cycle. Then we must break the cycle by removing at least one other vertex from the cycle. Additionally, we might also need to ensure the induced subgraph is connected. Let's enumerate possible trees.
We want to maximize sum. The vertices weights: 3:9, 6:6, 4:4, 1:3,5:3,2:1. So we want to include high-weight nodes: 3,6,4,1,5, but avoid cycle. The cycle is 1-2-6-5-4-1. To break the cycle, we can remove one edge by not including a vertex or not including an edge? But induced subgraph includes all edges among chosen vertices. So to avoid cycle, we must omit at least one vertex from the cycle. But if we omit a vertex, we must ensure connectivity of the induced subgraph. Since node 3 is only connected to 2, if we omit 2, then 3 becomes isolated unless we also omit 3 or connect via some other? Actually 3 only connects to 2, so if we include 3, we must include 2 to have any edge incident to 3. So to have a connected induced subgraph that includes 3, 2 must be present. So we likely need to include 2. Then to break the cycle, we must omit at least one of the other cycle vertices (1,4,5,6). Which one to omit to maximize sum? Omit the one with smallest weight among {1,4,5,6} is 1 (weight 3) or 5 (weight 3) or 4 (4) or 6 (6). Smallest are 1 and 5 (weight 3). So we could omit vertex 1 (weight 3) and include {2,3,4,5,6}. Check induced subgraph on {2,3,4,5,6}: edges: 2-3,2-6,4-5,5-6, and also 1-? missing. Also 2-? also 2-1? no 1 omitted. Edges: 2-3,2-6,5-6,4-5. Is this connected? Let's see: component 1: 2 connected to 3 and 6; 6 connected to 5; 5 connected to 4. So all vertices are connected: 2-6-5-4 and 2-3. Yes it's connected (since 2 connects to 6, which connects to 5, which connects to 4, and 3 connects to 2). Check for cycles: Does this graph contain a cycle? 2-6-5-4 is a path, 2-6-5-4, 2-3 is leaf. There is no cycle because the original cycle 1-2-6-5-4-1 is broken by removing 1. So the induced subgraph is a tree? Let's verify edges: We have edges: (2,3), (2,6), (6,5), (5,4). That's a tree (4 nodes, 4 edges? Actually vertices: 2,3,6,5,4 = 5 vertices, edges: 4 edges. So it's a tree (acyclic). So this set {2,3,4,5,6} yields sum = 1+9+4+3+6 = 23. That's > 17.
If we omit 5 instead of 1, include {1,2,3,4,6}. Edges: 1-2,1-4,2-3,2-6,4? also 4-5? no 5 omitted, 6-? 6-5? no. So edges: 1-2,1-4,2-3,2-6. That's connected? 1-2-6 and 1-4, also 2-3. It's connected: all nodes reachable from 1: 1 connects to 2 and 4; 2 connects to 3 and 6. So yes. Check cycles: any cycle? 1-2-6? 6 not connected to 4 or 1 directly. So no cycle. So {1,2,3,4,6} sum = 3+1+9+4+6 = 23 as well? Actually 3+1=4, +9=13, +4=17, +6=23. Yes 23.
If we omit 4 (weight 4), include {1,2,3,5,6}. Edges: 1-2,2-3,2-6,5-6,1? also 1-4? no. Edges: 1-2,2-3,2-6,6-5. Is it connected? 1 connected to 2; 2 to 3 and 6; 6 to 5. So yes, connected. Check cycles: 1-2-6-5? 5 not connected to 1, so no cycle. sum = 3+1+9+3+6 = 22.
If we omit 6 (weight 6), include {1,2,3,4,5}. Edges: 1-2,1-4,2-3,4-5. Connectivity: 1 connected to 2 and 4; 2 to 3; 4 to 5. So there are two components? Actually 1-2-3 is a component, and 1-4-5 is another? Wait 1 connects to both 2 and 4, so all are connected through 1: 1-2-3, and 1-4-5, so all nodes are in one component: 1 connects to 2 and 4, so 3 via 2, 5 via 4. So it's connected. Check cycles: any? 1-2-? back to 1? No. So it's a tree? Edges: 1-2,1-4,2-3,4-5. That's 4 edges for 5 vertices, so it's a tree. Sum = 3+1+9+4+3 = 20.
If we omit 2 (weight 1), we cannot include 3 or else 3 isolated. So we might omit 3 as well to keep connectivity? But if we omit 2, we could still include 3? No, 3 would be isolated because its only neighbor is 2. So if we want connected tree, we must either include 2 or exclude 3. To maximize sum, we might exclude 3 (weight 9) but that seems bad. So better to include 2.
Thus the maximum weight induced tree appears to be 23. So not 17.
Maybe it's maximum weight "branching" or "arborescence" in a directed graph? For directed graph, a branching is a directed spanning tree with a root. But we need to choose a subset of vertices? Hmm.
Given the difficulty, let's search memory: There is a known problem: "Good Subset" on a rooted tree? The sample input and output might be from Codeforces problem "Maximum Weight Closure in a Directed Graph" but with negative values? But here all values are positive.
Wait maybe the problem is: "You are given a directed graph with N vertices and M edges. Each vertex has a value (could be positive or negative). You need to select a subset of vertices such that if you select a vertex, you must also select all vertices that can reach it (i.e., all ancestors). This is the "minimum cut" problem for maximum weight closure when we reverse edges? Actually typical closure: if you select a vertex, you must select all its successors. The reversal would be ancestors. But either way, sum of all positive would be max if all positive. So not.
Maybe the values can be negative? In sample, all are positive, so closure would be all nodes. But output is 17, so maybe the set is not arbitrary; maybe we are required to choose exactly K vertices? Or maybe we need to select a set of vertices that is "good" meaning that for every edge (u,v), if u is in S then v is also in S? That's closure. Then we also may have a root? Not.
Another idea: The graph might be a functional graph (each node has exactly one outgoing edge) but M can be any. However sample M = N, each node may have outdegree? Let's see outdegrees: 1 has 2 outgoing? Actually from edges: 1->2,1->4 so outdegree 2. 2->6 outdegree 1. 3->2 outdegree 1. 4 has none? 5->4 outdegree 1. 6->5 outdegree 1. So not functional.
Maybe it's a bipartite matching problem? But then output 17?
Perhaps the problem is to find maximum weight "independent set" in a bipartite graph, but the graph is not bipartite (odd cycle). However sometimes you can solve maximum weight independent set on a bipartite graph using min-cut. But here graph is not bipartite. However if we convert to a directed graph with some orientation, maybe it becomes bipartite? Unclear.
Maybe the graph is actually a tree? But M = N, so it's a tree plus one extra edge, maybe it's a "tree with one extra edge" i.e., a cactus. There is a known problem: "Maximum weight subset of nodes such that no two are adjacent or no two are connected by an edge" but that's independent set which we already considered.
Perhaps the problem is "Maximum weight subset such that for every edge, at most one of its endpoints is selected" which is independent set. But that gave 19.
Maybe it's "Maximum weight subset such that for every edge, at least one of its endpoints is selected" which is vertex cover, and we want maximum weight? That would be all nodes (26). Not.
Maybe it's "Maximum weight subset such that the induced subgraph is a clique"? That would be small.
Let's compute sum 17: 9+4+3+1 = 17. That's nodes 3,4,5,2. But we saw conflict with directed constraints: 5->4 prohibits 4 if 5 in S, and 3->2 prohibits 2 if 3 in S. So that set violates both. But maybe the condition is opposite: if u in S, then v must be in S (closure). For set {2,3,4,5}, check edges: 1->2 (1 not in S, ok), 1->4 (1 not in S, ok), 2->6 (2 in S => 6 must be in S, but 6 is not, violation). So fails closure.
Maybe the condition is: for each edge (u,v), u and v cannot both be NOT in S? That is vertex cover? Actually vertex cover requires at least one endpoint in S. So if we want S to be a vertex cover, the complement is independent set. Max weight vertex cover would be total - min weight independent set? Actually max weight vertex cover is complement of min weight independent set. Since all positive, max weight vertex cover is all nodes (26). Min weight vertex cover is 7. So not 17.
But 17 could be the weight of some specific set that is "good" according to some other definition.
Let's parse the problem statement that might be missing: Could be from Codeforces Round #... Problem "Good Subset" or "Maximum Weight Closure in a Graph" but with a twist: You are given a directed graph, each vertex has a value, you can select a subset S, and your score is sum of values in S minus sum of values of edges leaving S? Or something like that. There is a classic problem: "Given a directed graph, each vertex has a weight, find a subset of vertices that maximizes the sum of weights of selected vertices minus the sum of weights of edges going from selected to unselected." That is the maximum weight closure problem reduced to min-cut as well. Actually the closure problem can be seen as: we want to choose a set S such that if we choose u, we must choose all its successors. That's one interpretation. But there is also the "maximum weight independent set" in bipartite graphs via min-cut. Or the "maximum weight anticlique" etc.
Maybe the score is sum of values of selected vertices minus sum of values of edges that go from selected to unselected? That is a cut. For each edge (u,v) with some capacity? Not given.
Alternatively, maybe the problem is: "You are given a rooted tree with N nodes (node 1 is root). Each node i has a value A_i. You need to choose a subset of nodes such that for every chosen node, all its ancestors are also chosen." That is a "rooted subtree" or "prefix" set. Then the maximum sum would be something like: you can choose any set that is "upward closed", i.e., if node is in S then its parent is in S. This is like you can only choose a node if you choose its parent. That is like selecting a rooted connected subgraph containing the root. In a tree, the maximum weight such set given all positive would be the whole tree. But if there are negative values, you might exclude subtrees. Here all positive, so answer would be total. Not 17.
But maybe the tree is not rooted at 1? Could be any root? The sample edges could represent a tree? But M=N=6, which is one more than N-1=5. So it's not a tree. Could be a rooted tree with an extra edge? Hmm.
Maybe the graph is actually undirected and we are to find a maximum weight subset such that the induced subgraph is a "forest" with no cycles, and we also require the subset to be connected? That would be a maximum weight tree. We computed max weight tree as 23. Not 17.
What about maximum weight subset that induces a bipartite subgraph? That seems unlikely.
Maybe the problem is about "Maximum weight subset with no two nodes at distance exactly 2"? Or something.
Given the sample output 17, let's try to see if there is a set summing to 17 that satisfies some plausible condition.
Possible sums: 26 total, 25, 24, 23, 22, 21, 20, 19, 18, 17, etc. 17 is less than 19, so maybe the optimal is 17, meaning our independent set gave 19, but maybe there is an additional restriction: maybe the set must be "good" meaning it is closed under both outgoing and incoming edges? That is, if you pick a node, you must pick all nodes reachable from it and also all nodes that can reach it? That would force the set to be a union of strongly connected components. Then the maximum weight set would be the sum of positive SCCs. In this graph, let's compute SCCs. Directed graph: edges as given. Find SCCs: 1->2,2->6,6->5,5->4, no edge from 4 to anything else, 1->4, 3->2. There is no path back from 2 to 1? 2->6->5->4, from 4 no outgoing to 1. So 1 is not reachable from 2. 6->5->4, no path to 1 or 2? 5->4, 4 no outgoing. So 6 cannot reach 2? 6->5->4, stop. So there is no cycle. So each node is its own SCC. Then if we must include all nodes that can reach a chosen node, that would force including ancestors. For example, if we choose node 6, we must also include all nodes that can reach 6: those are 1 (1->2->6), 2 (2->6), 3 (3->2->6), maybe also? 1,2,3. So choosing 6 forces inclusion of 1,2,3. And also must include successors? Not sure. If condition is both ways, then choosing any node forces inclusion of entire reachable set both ways, which might be large. Let's see if we can find a set that is closed under both predecessor and successor. That would mean S must be a union of SCCs that is "downward closed" and "upward closed"? Actually if we require that if u in S then all nodes reachable from u (descendants) are in S, and also all nodes that can reach u (ancestors) are in S, then S must be a union of SCCs that is both an "ideal" and a "filter". In a DAG, that would mean S must be either empty or the whole set if the graph is weakly connected? Let's test: Suppose we choose node 6. Then we must include all ancestors: 1,2,3 (since they can reach 6). Also all descendants: from 6, we have 5,4 (since 6->5->4). So we must include 1,2,3,6,5,4 = all nodes. So S = all. So any non-empty set might force all nodes due to connectivity? Check node 4: ancestors: all nodes that can reach 4: 1 (1->4), 5 (5->4), 6 (6->5->4), 2 (2->6...), 3 (3->2...). So all nodes again? 2 can reach 4 via 2->6->5->4, yes. So any node that is in a "sink" but reachable from many would force all. But node 2: ancestors: 1,3 (1->2,3->2). Also maybe? 6? can 6 reach 2? no. 5? no. So ancestors of 2 are {1,3}. Descendants of 2: {6,5,4}. So if we choose 2, we must include {1,3} and {2,6,5,4} = all nodes except maybe? That's all nodes: 1,2,3,4,5,6. Yes all. Node 1: ancestors: none? because no one points to 1?