A. Ehab Fails to Be Thanos
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You're given an array aa of length 2n2n. Is it possible to reorder it in such way so that the sum of the first nn elements isn't equal to the sum of the last nn elements?
Input
The first line contains an integer nn (1≤n≤10001≤n≤1000), where 2n2n is the number of elements in the array aa.
The second line contains 2n2n space-separated integers a1a1, a2a2, ……, a2na2n (1≤ai≤1061≤ai≤106) — the elements of the array aa.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n2nspace-separated integers. They must form a reordering of aa. You are allowed to not change the order.
Examples
input
Copy
3
1 2 2 1 3 1
output
Copy
2 1 3 1 1 2
input
Copy
1
1 1
output
Copy
-1
Note
In the first example, the first nn elements have sum 2+1+3=62+1+3=6 while the last nn elements have sum 1+1+2=41+1+2=4. The sums aren't equal.
In the second example, there's no solution.
题意:给了2N个数,重新排列,是否能够让前n个的和不等于后面n个数的和,可以就输出排序的数,没有就输出-1.
题解:直接排序 判断前n个和是否等于后n个,相当于直接比较第一个和最后一个数是否相等,因为只要有一个数不相等,排序后前后不相等,相等只能是所有数相等。
c++:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a[2010];
cin>>n;
for(int i=0; i<2*n; i++)
scanf("%d",&a[i]);
sort(a,a+2*n);
if(a[0]==a[2*n-1])
puts("-1");
else for(int i=0; i<n*2; i++)
printf("%d\n",a[i]);
return 0;
}
python:
n=int(input())
a=sorted(list(map(int,input().split())))
if a[0]==a[-1]:print("-1")
else:print(*a)