H - Buying Products (SDUT 2018 Autumn Individual Contest - F)

滴答滴答--题目链接 

Salim (a.k.a Slango) and his wife fight all the time. When Salim came home at night, his needy wife Afaf (a.k.a Afoufe) was waiting for him at the door.

"You don't buy any products for this house! You are a lousy husband! Well guess what?? You are not coming home before buying me exactly Kproducts, or you are sleeping on the streets!!". Said his wife.

"I'm going to buy you these products, but when you open that door you are divorced by 3 you needy wife!!". Thought Salim to him self.

Anyway, having no other option, Salim went down the stairs and headed to the nearby mall. the mall has exactly N shops. Salim knows that each of these shops has exactly 3 products (it's sales time and shops are almost empty).

However, Salim knows that buying all the 3 products of some shop will cause the owner of the shop to ask him for a huge amount of money, because he will say that the last piece is very rare. Thus, he made a rule, he will buy at most 2 products from every shop.

Salim is really tired and he just wants to go home and sleep a while in order to wake up in the morning and divorce his needy wife. Also, Salim doesn't really care about the quality of the products, he just wants to buy them with the lowest possible price. Can you help him calculate the minimum amount of money he needs to spend in order to buy his wife exactly K products, while buying at most 2 products from any given shop?

Input

The first line contains an integer T, the number of test cases.

The first line of each test case contains 2 space separated integer Nand K (1 ≤ N ≤ 1000)(1 ≤ K ≤ 2N).

N lines follows. The ith line contains exactly 3 space separated integers denoting the prices of the 3 products at the ith shop. (1 ≤ Pij ≤ 106)

Output

For each test case print a single line, containing a single integer, denoting the minimum price needed to buy exactly K products from the given shops while fulfilling the above given condition.

Example

Input

1
3 4
1 10 300
4 5 6
1 100 1

Output

7
#include 
#include
#include
#include
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        int b[2010];
        int ll=0;
        int x1,y1;
        for(int i=1; i<=n; i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(x>=y&&x>=z)x1=y,y1=z;
            else if(y>=x&&y>=z)x1=x,y1=z;
            else if(z>=x&&z>=y)x1=x,y1=y;
            b[ll++]=x1;
            b[ll++]=y1;
        }
        int ans=0;
        sort(b,b+ll);
        int o=0;
        for(int i=0; i

 

你可能感兴趣的:(VJ)