# Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions

Given an array arr[] of (N – 1) integers and each value arr[i](1-based indexing) is the score of the nodes having degree i. The task is to determine the maximum score of any tree of N nodes that can be constructed.

Examples:

Input: arr[] = {1, 3, 0}
Output: 8
Explanation:
One possible way to construct tree is:
1
/   \
2     3
\
4
Node 1 have degree 2. Therefore, its score is 3.
Node 2 have degree 1. Therefore, its score is 1.
Node 3 have degree 2. Therefore, its score is 3.
Node 4 have degree 1. Therefore, its score is 1.
Therefore, the total score = 3 + 1 + 3 + 1 = 8.

Input: arr[] = {0, 1}
Output: 1
Explanation:
One possible way to construct tree is:
1
/   \
2     3
Node 1 have degree 2. Therefore, its score is 1.
Node 2 have degree 1. Therefore, its score is 0.
Node 3 have degree 1. Therefore, its score is 0.
Therefore, total score = 1 + 0 + 0 = 1.

Naive Approach: The simplest approach is to generate all possible combinations of constructing a tree having N nodes and find the total score for each of them. Then, print the maximum of all the scores obtained.

Time Complexity: (N!) where N is the number of nodes in the tree.
Auxiliary Space: O(N)

Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming by creating a dp[][] table where dp[i][j] represents the maximum score using i nodes having the sum of degrees of the nodes as j. Follow the below steps to solve the problem:

• Initialize an array dp[N + 1][2*(N – 1) + 1] where N is the number of nodes and (2*(N – 1)) is the maximum sum of degrees.
• Initialize dp with 0.
• Iterate two nested loops, one over the range [1, N], and another for till the possible maximum score  2*(N – 1) from 1 and for each score s in the range [1, N] traverse the given array of scores arr[] and updating dp[i][s] as:

dp[i][s] = max(dp[i][s], scores[j-1] dp[i-1][s-j])
where dp[i][s] represents the maximum score of tree having i nodes and sum of degrees as s.

• For a tree with N vertices and (N – 1) edges, the sum of all degrees should be 2 * (N – 1). Therefore, print the value of dp[N][2*(N – 1)] as the maximum score for a tree with N nodes.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the maximum score` `// for one possible tree having N nodes` `// N - 1 Edges` `int` `maxScore(vector<``int``>& arr)` `{` `    ``int` `N = arr.size();`   `    ``// Number of nodes` `    ``N++;`   `    ``// Initialize dp[][]` `    ``vector >` `        ``dp(N + 1, vector<``int``>(2 * N,` `                              ``-100000));`   `    ``// Score with 0 vertices is 0` `    ``dp = 0;`   `    ``// Traverse the nodes from 1 to N` `    ``for` `(``int` `i = 1; i <= N; i++) {`   `        ``// Find maximum scores for` `        ``// each sum` `        ``for` `(``int` `s = 1;` `             ``s <= 2 * (N - 1); s++) {`   `            ``// Iterate over degree of` `            ``// new node` `            ``for` `(``int` `j = 1; j <= N - 1` `                            ``and j <= s;` `                 ``j++) {`   `                ``// Update the current` `                ``// state` `                ``dp[i][s]` `                    ``= max(dp[i][s],` `                          ``arr[j - 1]` `                              ``+ dp[i - 1][s - j]);` `            ``}` `        ``}` `    ``}`   `    ``// Return maximum score for N node` `    ``// tree having 2(N - 1) sum of degree` `    ``return` `dp[N][2 * (N - 1)];` `}`   `// Driver Code` `int` `main()` `{` `    ``// Given array of scores` `    ``vector<``int``> arr = { 1, 3, 0 };`   `    ``// Function Call` `    ``cout << maxScore(arr);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to find the maximum score` `// for one possible tree having N nodes` `// N - 1 Edges` `static` `int` `maxScore(``int``[] arr)` `{` `    ``int` `N = arr.length;`   `    ``// Number of nodes` `    ``N++;`   `    ``// Initialize dp[][]` `    ``int` `[][] dp = ``new` `int``[N + ``1``][``2` `* (N - ``1``) + ``1``];` `    `  `    ``// Score with 0 vertices is 0` `    ``dp[``0``][``0``] = ``0``;`   `    ``// Traverse the nodes from 1 to N` `    ``for``(``int` `i = ``1``; i <= N; i++)` `    ``{` `        `  `        ``// Find maximum scores for` `        ``// each sum` `        ``for``(``int` `s = ``1``; s <= ``2` `* (N - ``1``); s++)` `        ``{` `            `  `            ``// Iterate over degree of` `            ``// new node` `            ``for``(``int` `j = ``1``; j <= N - ``1` `&& j <= s; j++)` `            ``{` `                `  `                ``// Update the current` `                ``// state` `                ``dp[i][s] = Math.max(dp[i][s],` `                                   ``arr[j - ``1``] + ` `                                    ``dp[i - ``1``][s - j]);` `            ``}` `        ``}` `    ``}`   `    ``// Return maximum score for N node` `    ``// tree having 2(N - 1) sum of degree` `    ``return` `dp[N][``2` `* (N - ``1``)] - ``1``;` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    `  `    ``// Given array of scores` `    ``int` `[] arr = { ``1``, ``3``, ``0` `};`   `    ``// Function Call` `    ``System.out.print(maxScore(arr));` `}` `}`   `// This code is contributed by Amit Katiyar`

## Python3

 `# Python3 program for the above approach`   `# Function to find the maximum score` `# for one possible tree having N nodes` `# N - 1 Edges` `def` `maxScore(arr):` `    `  `    ``N ``=` `len``(arr)`   `    ``# Number of nodes` `    ``N ``+``=` `1`   `    ``# Initialize dp[][]` `    ``dp ``=` `[[``-``100000` `for` `i ``in` `range``(``2` `*` `N)] ` `                   ``for` `i ``in` `range``(N ``+` `1``)]`   `    ``# Score with 0 vertices is 0` `    ``dp[``0``][``0``] ``=` `0`   `    ``# Traverse the nodes from 1 to N` `    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        `  `        ``# Find maximum scores for` `        ``# each sum` `        ``for` `s ``in` `range``(``1``, ``2` `*` `(N ``-` `1``) ``+` `1``):` `            `  `            ``# Iterate over degree of` `            ``# new node` `            ``j ``=` `1` `            ``while` `j <``=` `N ``-` `1` `and` `j <``=` `s:` `                `  `                ``# Update the current` `                ``# state` `                ``dp[i][s] ``=` `max``(dp[i][s], arr[j ``-` `1``] ``+` `                               ``dp[i ``-` `1``][s ``-` `j])` `                ``j ``+``=` `1` `                `  `    ``# Return maximum score for N node` `    ``# tree having 2(N - 1) sum of degree` `    ``return` `dp[N][``2` `*` `(N ``-` `1``)]`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``# Given array of scores` `    ``arr ``=` `[ ``1``, ``3``, ``0` `]`   `    ``# Function Call` `    ``print``(maxScore(arr))`   `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the ` `// above approach` `using` `System;` `class` `GFG{`   `// Function to find the ` `// maximum score for one ` `// possible tree having N ` `// nodes N - 1 Edges` `static` `int` `maxScore(``int``[] arr)` `{` `  ``int` `N = arr.Length;`   `  ``// Number of nodes` `  ``N++;`   `  ``// Initialize [,]dp` `  ``int` `[,] dp = ``new` `int``[N + 1,` `                       ``2 * (N - ` `                       ``1) + 1];`   `  ``// Score with 0 vertices ` `  ``// is 0` `  ``dp[0, 0] = 0;`   `  ``// Traverse the nodes from ` `  ``// 1 to N` `  ``for``(``int` `i = 1; i <= N; i++)` `  ``{` `    ``// Find maximum scores for` `    ``// each sum` `    ``for``(``int` `s = 1; ` `            ``s <= 2 * (N - 1); s++)` `    ``{` `      ``// Iterate over degree of` `      ``// new node` `      ``for``(``int` `j = 1; ` `              ``j <= N - 1 && j <= s; j++)` `      ``{` `        ``// Update the current` `        ``// state` `        ``dp[i, s] = Math.Max(dp[i, s],` `                            ``arr[j - 1] + ` `                            ``dp[i - 1,` `                               ``s - j]);` `      ``}` `    ``}` `  ``}`   `  ``// Return maximum score for` `  ``// N node tree having 2(N - 1) ` `  ``// sum of degree` `  ``return` `dp[N, 2 * (N - ` `            ``1)] - 1;` `}`   `// Driver Code` `public` `static` `void` `Main(String[] args)` `{    ` `  ``// Given array of scores` `  ``int` `[] arr = {1, 3, 0};`   `  ``// Function Call` `  ``Console.Write(maxScore(arr));` `}` `}`   `// This code is contributed by Princi Singh`

Output

`8`

Time Complexity: O(N3)
Auxiliary Space: O(N2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.