Difference Row(347A)

You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.

More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequencea. The value of such an arrangement is (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).

Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.

Input

The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a1a2...an (|ai| ≤ 1000).

Output

Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.

Sample Input

Input
5
100 -100 50 0 -50
Output
100 -50 0 50 -100 

Hint

In the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.

Sequence x1, x2, ... , xp is lexicographically smaller than sequence y1, y2, ... , yp if there exists an integer r(0 ≤ r < p) such that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.


第一个元素最大,最后一个元素最小,中间的元素递增排列。

#include <stdio.h> 
#include <string.h>  
#include <math.h>  
#include <stdlib.h>  
#include <ctype.h>  
int comp(const void *a,const void *b)
{
	return *(int*)b-*(int*)a;
}
int main()  
{   <span style="white-space:pre">	</span>int i,j,n,t;
	int s[100];
	
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d",&s[i]);
		}
		qsort(s,n,sizeof(int),comp);
		
		for(i=1;i<n-2;i++)
		{
			for(j=i+1;j<n-1;j++)
			{
				if(s[i]>s[j])
				{
					t=s[i];
					s[i]=s[j];
					s[j]=t;
				}
			}
		}
		for(i=0;i<n;i++)
		{
			if(i==0)
			printf("%d",s[i]);
			else
			printf(" %d",s[i]);	
		}		
	}
 
    return 0;  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值