Construct Tree

1

1 votes
Algorithms, Depth First Search, C++, Graphs
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Problem statement

Given a set of N nodes, where each node has an integer A[i] associated with it. 

Construct a tree using N nodes such that the value of the given function Z is maximized.

Z=Stretch(u, v), for all uv such that u<v and  uv are leaf nodes in the tree. 

Stretch(u,v) is defined as: (Sum of values of nodes present on simple path between node  u and v) * (Length of the simple path)

Please note:

  • A node x is said to be a leaf node if it is connected to exactly one other node in the tree, exception is a tree with 1 node, where the only node present in the leaf node.
  • Length of a simple path is equal to number of nodes on simple path - 1.

Input

  • First line contains an integer denoting number of nodes in the tree.
  • Next line contains N space separated integers denoting the value of each node i.e. A[i]

Output

  • Print N - 1 lines where each line contains
    • Two space separated integers a b denoting an edge between node a and b in the tree. 

Constraints

2N10001A[i]104

Verdit and Scoring

  • is the score for each test case.
  • If the output is not a tree or contains any invalid entry, then you will receive a Wrong Answer verdict.
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

 For the given test case:

  • Z=Stretch(1, 3)
  • Z=(4+1+9)×2=28
Editor Image

?