You are given nn numbers a1,a2,…,ana1,a2,…,an. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?
给定了n个数,a1...an。是否存在一种排列方式使得每个数比与他相邻的两个数的和小?
For example, for the array [1,4,5,6,7,8][1,4,5,6,7,8], the arrangement on the left is valid, while arrangement on the right is not, as 5≥4+15≥4+1 and 8>1+68>1+6.
比如说,对于这个数列,左侧排列是可以的,右侧排列是不行的
Input
The first line contains a single integer nn (3≤n≤1053≤n≤105) — the number of numbers.
第一行包含一个整数n——数字的个数
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
第二行包含n个整数——这些数字。给定的数字并不是绝对不同的(也就是说相同的数字也是可以的)
Output
If there is no solution, output "NO" in the first line.
如果没有答案,直接输出NO
If there is a solution, output "YES" in the first line. In the second line output nn numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.
如果有一个解决方案,在第一行输出YES。第二行输出任何一个可行的方案。
Examples
input
Copy
3
2 4 3
output
Copy
YES
4 2 3
input
Copy
5
1 2 3 4 4
output
Copy
YES
4 4 2 1 3
input
Copy
3
13 8 5
output
Copy
NO
input
Copy
4
1 10 100 1000
output
Copy
NO
Note
One of the possible arrangements is shown in the first example:
4<2+34<2+3;
2<4+32<4+3;
3<4+23<4+2.
One of the possible arrangements is shown in the second example.
No matter how we arrange 13,8,513,8,5 in a circle in the third example, 1313 will have 88 and 55 as neighbors, but 13≥8+513≥8+5.
There is no solution in the fourth example.
这个题的核心也是一个IDEA:对于“一般的排序”(逆序或顺序),这个序列早已满足了所谓“两边之和大于中间”的需求。比如说有序的序列1 2 3 4 5,2比1+3小,3比2+4小。这也就是说直接对原序列进行排列就可以得到一个解决方案,但是,这还得满足一个条件。
比如对于序列1 2 3 4 5 6,将他连成一个圈就是1 2 3 4 5 6 1,而6=1+5,并不满足条件。换句话说,你得让两端的连接点和它两端的元素满足题设条件。我们可以假设这个连接点为任何元素。假设它是b(n)元素,并且有显然的b(n-1)+b(n-2)>b(n),那么b(n)就可以当做一个连接点,把b(n-1)放在左侧,把b(n-2)放在右侧,由于数列有序,所以b(n-3)+b(n-1)>b(n-2)成立,以此类推到最后一个元素。b(n)b(n-2) 1,由于b(n)显然大于b(n-2),所以这一小节依然成立。所以全体数列符合题意。
所以思路如下:
//#include
#include
#include
#include
#include