Rabbit loves flowers very much and she plants n pots of flowers in her house. But she never prunes them because she is lazy. So the flowers have different heights and look ugly. One day, Kayaking decides to prune the flowers to make them look beautiful. To make them have equal heights, smart Kayaking has a wonderful idea. Initially, the i-th pot of flower’s height is a[i]. Each time, Kayaking will select n-1 pots of flowers and prune them to make their height subtract 1 with his 49m knife. Exactly, the height of the flowers is at least 1. Now, Kayaking wants to know if it is possible to prune the flowers to make them have equal height. If possible, what is the minimum times he prunes the flowers. Could you tell him?
Input
The input starts with a line contains exactly one positive number T which is the number of test case.
Each test case starts with a line contains a number n indicates the number of flowers. Then there is a line contains n numbers indicates a[i].
Output
For each test case, print a single line containing one integer—the minimum times Kayaking prunes the flowers, or -1 if it is impossible.
Sample Input
2
5
1 2 2 2 2
5
1 2 2 2 3
Sample Output
1
-1
Hint
T<=10,n<=105,ai<=109
题意 有n堆草每次只能对n-1堆操作每次只能减1问最少操作几次能把这些草剪到相同高度如果不能输出-1
emm这个题好难想关键是真么把他转化为另一种方式——倒着看把它看成每一株草与最高的草的差的和。假设要把最高的变成最低的,那么中间的肯定多减了比最低的还要低,所以要剪回来所以要把多减的次数加到操作次数中。
5 8 9 10
5 7 8 9
5 6 7 8
5 5 6 7
5 4 5 6
5 3 4 5
4 3 3 4
3 3 2 3
2 2 2 2
5+2+1=8
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
#include <stdio.h>
#include<map>
using namespace std;
int a[1000099],b[10000],c[20000];
int main()
{
int n,m,k,u,y,x;
cin>>x;
while(x--)
{
cin>>n;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
long long s=0;
sort(a,a+n);
for(int i=0;i<n-1;i++)
{
s+=a[n-1]-a[i];
}
if(s>=a[n-1])
cout<<-1<<endl;
else
cout<<s<<endl;
}
}