Given an **N-Ary** tree containing **N **nodes and an array **weight [ ]** that denotes the **weight** of the nodes which can be **positive** or **negative**, the task for every node is to print the **maximum sum** possible by a sequence of nodes including the current node.

**Examples:**

Input:N = 7 weight[] = [-8, 9, 7, -4, 5, -10, -6] N-Ary tree: -8 / \ 9 7 / \ / -4 5 -10 / -6Output:13 14 13 10 14 3 4Explanations:Node -8: [-8 + 9 + 7 + 5] = 13 Node 9: [9 + 5] = 14 Node 3: [7 + (-8) + 9 + 5] = 13 Node 4: [-4 + 9 + 5] = 10 Node: [5 + 9] = 14 Node 6: [-10 + 7 + (-8) + 9 + 5] = 3 Node 7: [-6 + (-4) + 9 + 5] = 4Input:N = 6 weight[] = [2, -7, -5, 8, 4, -10] N-Ary tree: 2 / \ -7 -5 / \ \ 8 4 -10Output:7 7 2 8 7 -8

**Approach:**This problem can be solved using Dp on Trees technique by applying two DFS.

- Apply the first DFS to store the maximum sum possible for every node by including them in a sequence with their respective
**successors**. Store the maximum possible sum in**dp1[].**array. - Maximum possible value for each node in the first DFS can be obtained by:

dp1[node] += maximum(0, dp1[child1], dp1[child2], …)

- Perform the second
**Dfs**to update the maximum sum for each node in**dp1[]**by including them in a sequence with their**ancestors**also. The maximum values stored in**dp2[]**for every node are the required answers. - Maximum possible value for each node in the second DFS can be obtained by:

**dp2[node] = dp1[node] + maximum(0, maxSumAncestors)**

Best answer can be obtained by including or excluding the maximum sum possible for its ancestors

where**maxSumAncestors = dp2[parent] – maximum(0, dp1[node])**, i.e. including or excluding contribution of the maximum sum of the current node stored in dp1[] - Iterative Segment Tree (Range Maximum Query with Node Update)
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Cartesian tree from inorder traversal | Segment Tree
- Build a segment tree for N-ary rooted tree
- Maximum of all subarrays of size K using Segment Tree
- Segment Tree | Set 1 (Sum of given range)
- Euler Tour | Subtree Sum using Segment Tree
- Two Dimensional Segment Tree | Sub-Matrix Sum
- Range Sum and Update in Array : Segment Tree using Stack
- Queries for the count of even digit sum elements in the given range using Segment Tree.
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Find root of the tree where children id sum for every node is given
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Sum of the distances from every node to all other nodes is maximum
- Segment Tree | Set 2 (Range Minimum Query)
- Segment Tree | Set 3 (XOR of given range)
- Levelwise Alternating GCD and LCM of nodes in Segment Tree
- Iterative Segment Tree (Range Minimum Query)

Refer to the pictorial explanation for better understanding:

Below is the implementation of the above approach:

## C++

`// C++ program to calculate the maximum ` `// sum possible for every node by including ` `// it in a segment of the N-Ary Tree ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Stores the maximum ` `// sum possible for every node ` `// by including them in a segment ` `// with their successors ` `int` `dp1[100005]; ` ` ` `// Stores the maximum ` `// sum possible for every node ` `// by including them in a segment ` `// with their ancestors ` `int` `dp2[100005]; ` ` ` `// Store the maximum sum ` `// for every node by ` `// including it in a ` `// segment with its successors ` `void` `dfs1(` `int` `u, ` `int` `par, ` ` ` `vector<` `int` `> g[], ` ` ` `int` `weight[]) ` `{ ` ` ` ` ` `dp1[u] = weight[u]; ` ` ` `for` `(` `auto` `c: g[u]) { ` ` ` `if` `(c != par) { ` ` ` `dfs1(c, u, g, weight); ` ` ` `dp1[u] += max(0, dp1); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Update the maximum sums ` `// for each node by including ` `// them in a sequence with ` `// their ancestors ` `void` `dfs2(` `int` `u, ` `int` `par, ` ` ` `vector<` `int` `> g[], ` ` ` `int` `weight[]) ` `{ ` ` ` `// Condition to check, ` ` ` `// if current node is not root ` ` ` `if` `(par != 0) { ` ` ` `int` `maxSumAncestors = dp2[par] ` ` ` `- max(0, dp1[u]); ` ` ` `dp2[u] = dp1[u] + max(0, ` ` ` `maxSumAncestors); ` ` ` `} ` ` ` `for` `(` `auto` `c: g[u]) { ` ` ` `if` `(c != par) { ` ` ` `dfs2(c, u, g, weight); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Add edges ` `void` `addEdge(` `int` `u, ` `int` `v, vector<` `int` `> g[]) ` `{ ` ` ` `g[u].push_back(v); ` ` ` `g[v].push_back(u); ` `} ` ` ` `// Function to find the maximum ` `// answer for each node ` `void` `maxSumSegments(vector<` `int` `> g[], ` ` ` `int` `weight[], ` ` ` `int` `n) ` `{ ` ` ` ` ` `// Compute the maximum sums ` ` ` `// with successors ` ` ` `dfs1(1, 0, g, weight); ` ` ` ` ` `// Store the computed maximums ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `dp2[i] = dp1[i]; ` ` ` `} ` ` ` ` ` `// Update the maximum sums ` ` ` `// by including their ` ` ` `// ancestors ` ` ` `dfs2(1, 0, g, weight); ` `} ` ` ` `// Print the desired result ` `void` `printAns(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `cout << dp2[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` ` ` `// Number of nodes ` ` ` `int` `n = 6; ` ` ` `int` `u, v; ` ` ` ` ` `// graph ` ` ` `vector<` `int` `> g[100005]; ` ` ` ` ` `// Add edges ` ` ` `addEdge(1, 2, g); ` ` ` `addEdge(1, 3, g); ` ` ` `addEdge(2, 4, g); ` ` ` `addEdge(2, 5, g); ` ` ` `addEdge(3, 6, g); ` ` ` `addEdge(4, 7, g); ` ` ` ` ` `// Weight of each node ` ` ` `int` `weight[n + 1]; ` ` ` `weight[1] = -8; ` ` ` `weight[2] = 9; ` ` ` `weight[3] = 7; ` ` ` `weight[4] = -4; ` ` ` `weight[5] = 5; ` ` ` `weight[6] = -10; ` ` ` `weight[7] = -6; ` ` ` ` ` `// Compute the max sum ` ` ` `// of segments for each ` ` ` `// node ` ` ` `maxSumSegments(g, weight, n); ` ` ` ` ` `// Print the answer ` ` ` `// for every node ` ` ` `printAns(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

13 14 13 10 14 3

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.