Plus and Minus

5

8 votes
Algorithms, , Binary Search
Problem

Initially, you have an array of length 2, which is a=[a1,a2]. Here a1a20.


In each step, you must choose the two largest integers ai and aj in the array, where aiaj, and insert two integers ai+aj and aiaj to the array (in any positions).

Thus, you can construct an array of any even length 4.

Now you are given an array of even length N.

Please determine whether you can form this array by the steps. If so, then find out which is the initial array.

Constraints

  • 1T1000
  • 4N107
  • 0ai109
  •  The sum of N across all test cases does not exceed 107.

Input Format

  • The first line contains a single integer T —- the number of test cases.
  • For each testcase:
    • The first line contains one odd integer N —- the length of the arrays.
    • The second line contains N positive integers a1,a2,...,aN.
  • The sum of N over all test cases does not exceed 107.

Output Format

  • For each testcase, output "YES" if you can construct the given array by the steps. Otherwise, output "NO".
  • If you can construct such an array, then print two integers a1,a2. Note that a1a2.
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

In the first case:

  • Assume that the initial array is a=[2,1].
  • In the first step, we add 2+1=3 and 21=1 to the array, and so the array becomes [3,2,1,1].
  • In the second step, we add 3+2=5 and 32=1 to the array, and so the array becomes [5,3,2,1,1,1].
  • In the third step, we add 5+3=8 and 53=2 to the array, and so the array becomes [8,5,3,2,2,1,1,1].

In the second case:

  • If you can construct the given array, there exists the initial array a=[a1,a2] with a1a2.
  • Since there is no removal in any step, a1 and a2 are in the given array.
  • However, by considering all possible arrays of length 2, you can find there is no way to construct this array. 
Editor Image

?