An important meeting is to be held and there are exactly people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting.
Each person has limited sociability. The sociability of the -th person is a non-negative integer . This means that after exactly talks this person leaves the meeting (and does not talk to anyone else anymore). If , the -th person leaves the meeting immediately after it starts.
A meeting is considered most productive if the maximum possible number of talks took place during it.
You are given an array of sociability , determine which people should talk to each other so that the total number of talks is as large as possible.
The first line contains an integer () — the number of test cases.
The next lines contain descriptions of the test cases.
The first line of each test case description contains an integer () —the number of people in the meeting. The second line consists of space-separated integers () — the sociability parameters of all people.
It is guaranteed that the sum of over all test cases does not exceed . It is also guaranteed that the sum of all (over all test cases and all ) does not exceed .
Print answers to all test cases.
On the first line of each answer print the number — the maximum number of talks possible in a meeting.
On each of the next lines print two integers and ( and ) — the numbers of people who will have another talk.
If there are several possible answers, you may print any of them.
8 2 2 3 3 1 2 3 4 1 2 3 4 3 0 0 2 2 6 2 3 0 0 2 5 8 2 0 1 1 5 0 1 0 0 6
2 1 2 1 2 3 1 3 2 3 2 3 5 1 3 2 4 2 4 3 4 3 4 0 2 1 2 1 2 0 4 1 2 1 5 1 4 1 2 1 5 2
The solution to the above Problem is
We will make a max priority queue, then we will take the top two elements and they will talk, we will then decrease the value of them by one and then again push it back to the priority queue.
We will do this till there are two people left.
All the answer will be stored in vector.
Thank you for reading.
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.