For L…R, we have n = R - L + 1 nodes.

It is always optimal to connect all nodes to the node with the smallest value, or to connect the nodes in order of increasing value.

It will incur a cost of sum ( a[L], a[L+1], … a[R] ) - min( a[L], a[L+1], … a[R]).

Use any data structure that allows for point updates and range queries, i.e: segment tree.

Squeezing in sqrt structures may be possible with compiler black magic but it is not worth it.

Statistics

87% Solution Ratio
efat1531Earliest, 8M ago
prodip_bsmrstuFastest, 0.1s
Alom_ShantoLightest, 7.0 MB
fahimcp495Shortest, 1439B
Toph uses cookies. By continuing you agree to our Cookie Policy.