题目传送门P1109学生分组
#include<bits/stdc++.h>
using namespace std;
long long n,ans;
long long a[55];
long long q,w;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(long long i=1;i<=n;i++){
cin>>a[i];
}
cin>>q>>w;
sort(a+1,a+1+n);
if(a[1]>w){
cout<<-1;
return 0;
}
int l=1,r=n;
while(l<r){
if(a[r]>w){
int m=a[r]-w;
while(m!=0){
if(a[l]+m<=w){
ans+=m;
l++;
m=0;
}else{
ans+=w-a[l];
a[l]=w;
}
}
}
r--;
}
cout<<ans;
return 0;
}