Original Question Statement Link : In the distant Algorithms Galaxy, there's a solar system with K planets that inh - Pastebin.com (test cases included )

The Question basically asks ,

Given K Binary Trees each rooted at it’s node 1.

We can traverse normally inside a tree.

And we can also move from i_{th} tree to (i+1)_{th} or (i-1)_{th} tree through the root nodes of both i.e go from root node of one tree to the root node next or previous tree.

We can also move from the 0_{th} tree to the (K-1)_{th} tree and vice versa.

N_i denotes the number of nodes in the i_{th} tree.

Find the number of edges in the longest simple path (each node is visited once) over the entire forest of trees.

Note: if we move from i_{th} tree to (i+1)_{th} tree via root, we have to increase the answer by 1 as if we have an edge between both the roots,

3 \leq K \leq 10^5

1 \leq N_i \leq 10^5

3 \leq \sum_{i=0}^{K-1} N_i \leq 2,000,000

Time Limit : 1 Sec

I can’t think of any approach to solve this. Can anyone please help me?

Originally Posted At : Help needed in a Tree Question. - Codeforces