Codeforces Round 898 (Div. 4) 1873E - Building an Aquarium Solution
Problem Link: https://codeforces.com/problemset/problem/1873/E
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n , x ; cin >> n >> x ;
- int a[n+1] ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- }
- int low = 1 , high = 2e9 ;
- int ans = 0 , mid ;
- while(low <= high){
- mid = ( low + high ) / 2 ;
- int sum = 0 ;
- for ( int i = 0 ; i < n ; i++){
- if ( a[i] < mid ) sum += (mid - a[i]) ;
- if(sum > x) break ;
- }
- if( sum <= x){
- ans = mid ;
- low = mid + 1 ;
- }
- else {
- high = mid - 1 ;
- }
- }
- cout << ans << '\n';
- }
- return 0 ;
- }
No comments