Maximum Median(二分)

You are given an array aa of nn integers, where nn is odd. You can make the following operation with it:

  • Choose one of the elements of the array (for example aiai) and increase it by 11 (that is, replace it with ai+1ai+1).

You want to make the median of the array the largest possible using at most kk operations.

The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array [1,5,2,3,5][1,5,2,3,5] is 33.

Input

The first line contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, nn is odd, 1≤k≤1091≤k≤109) — the number of elements in the array and the largest number of operations you can make.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

Output

Print a single integer — the maximum possible median after the operations.

Examples

input

3 2
1 3 5

output

5

input

5 5
1 2 1 1 1

output

3

input

7 7
4 1 2 4 3 4 4

output

5

Note

In the first example, you can increase the second element twice. Than array will be [1,5,5][1,5,5] and it's median is 55.

In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is 33.

In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be [5,1,2,5,3,5,5][5,1,2,5,3,5,5] and the median will be 55.

 

题意:给一个数组,一次操作可以将数组中的的任意数加1,问k次操作后,数组的中位数最大为多少。

思路:一般输出就一个数,且为最大值或者最小值的,就用二分枚举答案,再判断枚举出的答案是否符合题意。

 

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 2e5+100;
ll a[maxn];
ll ans,n,k;

int check( ll mid )
{
    ll last = 0;
    for ( int i=n/2+1; i<=n; i++ ) {
        if ( a[i]<=mid ) {
            last += (mid-a[i]);
        }
    }

    if ( last<=k ) {
        return 1;
    }
    else {
        return 0;
    }
}

int main()
{
    int i;
    cin >> n >> k;
    for ( i=1; i<=n; i++ ) {
        cin >> a[i];
    }
    sort(a+1,a+n+1);
    ll l = 1;
    ll r = 3e9;
    while ( l<=r ) { 
        ll mid = (l+r)/2;  // 枚举答案,中位数为mid是否符合题意
        if ( check(mid)==1 ) {
            l = mid+1;
            ans = mid;
        }
        else {
            r = mid-1;
        }
    }
    cout << ans << endl;

    return 0;
}

 

上一篇:TypeScript 报错


下一篇:typescript中Object,object,{}类型之间的区别