-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild_aquarium.cpp
More file actions
48 lines (46 loc) · 961 Bytes
/
Build_aquarium.cpp
File metadata and controls
48 lines (46 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pop pop_back
int main()
{
ll t;
cin>>t;
while(t--){
ll n,x;
cin>>n>>x;
ll arr[n];
for(ll i=0;i<n;i++){
cin>>arr[i];
}if(n==1){
cout<<arr[0]+x<<"\n";
}else{
sort(arr,arr+n);
ll hi=max(x+arr[n-1],arr[n-1]),lo=1,ans=0,mid=0;
while(hi-lo>1){
mid=(hi+lo)/2;
ll h=0;
for(ll i=0;i<n;i++){
if(arr[i]<=mid){
h+=abs(arr[i]-mid);
}
}
if(h>x){
hi=mid;
}else if(h<x){
ans=mid;
lo=mid;
}else{
ans=mid;
break;
}
}
if(ans>0)
cout<<ans<<"\n";
else
cout<<1<<"\n";
}
}
return 0;
}