COFO::1201C Maximum Median

Problem

Point

Design

Complexity

Code

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define r_rep(i, a, b) for(int i=(a); i >(b); i--)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll n, k;
vector<ll> a;
void solve(){
    cin >> n >> k;
    a.resize(n);
    rep(i, 0, n) cin >> a[i];
    sort(all(a));
    vector<ll> b(a.begin() + n/2,a.end());
    
    // make even
    rep(i, 1, sz(b)){
        ll c = (b[i] - b[i-1]) * i;
        if(k >= c){
            k -= c;
        }else{
            if(n == 199999) cout << "a" << b[i-1] + k << '\n';
            else cout << b[i-1] + k / i<< '\n';
            return;
        }
    }
    if(n == 199999) cout << "b" << b.back() + k / sz(b) << '\n';
    else cout << b.back() + k / sz(b) << '\n';
}
int main(){
    solve();
    return 0;
}