Gym 101064 D Black Hills golden jewels

In Rapid City are located the main producers of the Black Hills gold jewelry, a very popular product among tourists. The main characteristics of these jewels are the designs of leaves and grape clusters that are made with gold alloys in yellow, green and pink tones.

Planning a trip to Rapid City in 2017, Nay Suames decides that he wants to buy these famous jewels, but he doesn't have much money to spend. Searching for the lowest prices, he found a jewelry store that will have a sale in May, the same month he will travel to that city. Lucky guy!

The jewelry store intends to sell the jewels that were produced with small defects, in general, only perceivable by experts. The jewels are separated in N categories of defects. Even if the defect is imperceptible, the jewelry store can't sell the jewel at full price, so it gives a discount proportional to the defect.

Knowing the high demand for these jewels, the jewelry store created a system aiming to be fair and, at the same time, to prevent everybody from buying only the cheapest jewels. The clients have to make a queue and will be served one at a time. When their time comes, the client must choose two jewels from different categories. Furthermore, they can't choose the same combination of categories that was previously chosen by another client.

Nay asks for your help to find the minimum amount of money he needs, being the K-th client in the queue, so that it is guaranteed that he can buy two different jewels.

Input

The first line contains two integers, N and K, representing the number of categories and the position of Nay in the queue, respectively. The next line contains N integers a1, a2, ..., aN (0 ≤ ai ≤ 109), corresponding to the prices of each category.

Output

Print a single integer, the minimum amount of money to guarantee that Nay can buy the jewels.

Examples

Input

4 3
2 4 5 2

Output

6

Input

6 9
1 2 3 4 5 6

Output

7

 

题目bb了这么多,实际上就是找第k小的数对和。

数值太大了,暴力模拟百分百不行。只能用二分。

说是二分,实际上是二分套二分。一个二分来二分答案,下界是最小和上界是最大和;对于每个二分的mid,还需要进行一个二分,来判断它在数对和中的位置(即找出有多少个数对和比mid小)。如果有超过k个数对和,说明mid太大了,要把上限缩小;如果小于k个,说明mid太小了,要把下限放大,就是这样。

利用upper_bound可以简化第二个二分。

AC代码:

/*#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
using namespace std;
int a[100010];
vector<int>sum;
int main()
{
    int n;
    long long k;
    while(cin>>n>>k)
    {
        sum.clear();
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        int t=0;
        for(int i=0;i<n-1;i++)
            for(int j=i+1;j<n;j++)
                sum.push_back(a[i]+a[j]);

        sort(sum.begin(),sum.end());
        /*for(int i=0;i<n*(n-1)/2;i++)
            cout<<sum[i]<<' ';

        cout<<sum[k-1]<<endl;
    }
    return 0;
}*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
using namespace std;

typedef long long LL;
const int maxn=100010;

LL a[maxn];
LL n,k;

int slove(int x)
{
    LL cnt=0,end;
    for(int i=1;i<=n;i++)
        if(a[i]>x)
        {
            end=i;
            break;
        }

    for(LL i=1;i<end;i++)
    {
        LL num=upper_bound(a+1+i,a+n+1,x-a[i])-(a+1+i);

        cnt+=num;
        if(cnt>=k)
            return 1;
        if(num==0)
            break;
    }

    return 0;
}

int main()
{
    while(cin>>n>>k)
    {
        for(LL i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        sort(a+1,a+1+n);

        LL l=a[1]+a[2],r=a[n]+a[n-1],mid;

        while(l<=r)
        {
            mid=(l+r)>>1;
            int t=slove(mid);

            if(t)
                r=mid-1;
            else
                l=mid+1;
        }
        cout<<l<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值