ZOJ 3908 Number Game

Number Game

Time Limit: 2 Seconds       Memory Limit: 65536 KB

The bored Bob is playing a number game. In the beginning, there are n numbers. For each turn, Bob will take out two numbers from the remaining numbers, and get the product of them. There is a condition that the sum of two numbers must be not larger than k.

Now, Bob is curious to know what the maximum sum of products he can get, if he plays at most m turns. Can you tell him?

Input

The first line of input contains a positive integer T, the number of test cases. For each test case, the first line is three integers nm(0≤ nm ≤100000) and k(0≤k ≤20000). In the second line, there are n numbers ai(0≤ ai ≤10000, 1≤ i ≤n).

Output

For each test case, output the maximum sum of products Bob can get.

Sample Input
2
4 2 7
1 3 2 4
3 2 3
2 3 1
Sample Output
14
2


/*
思路:对于一个数,每次肯定取满足条件的最大的数和他乘,然后取前m   大


*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8


using namespace std;

#define INF 0x3f3f3f3f
#define N 100005

multiset<int>s;
multiset<int>::iterator it;

int n,m,k;
int a[N];

int cmp(int a,int b)
{
    return a>b;
}

void solve()
{
    int i,j;
    long long ans=0;
    int cnt=0;

    while(s.size()>=2)
    {
         it=s.end();
         --it;
         int x=*it;
         s.erase(it);
         it=s.upper_bound(k-x);
         if(it==s.begin()) continue;
         --it;
         a[cnt++]=x*(*it);
         s.erase(it);
    }
    sort(a,a+cnt,cmp);
    for(i=0;i<m&&i<cnt;i++)
        ans=ans+a[i];
    printf("%lld\n",ans);
}

int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        s.clear();
        int x;
        while(n--)
        {
            scanf("%d",&x);
            s.insert(x);
        }
        solve();
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值