There are currently nn hot topics numbered from 00 to n−1n−1 at your local bridge club and 2n2n players numbered from 00 to 2n−12n−1. Each player holds a different set of views on those nn topics, more specifically, the ii-th player holds a positive view on the jj-th topic if i & 2j>0i & 2j>0, and a negative view otherwise. Here && denotes the bitwise AND operation.

Bridge Club solution codeforces

There are currently nn hot topics numbered from 00 to n1n−1 at your local bridge club and 2n2n players numbered from 00 to 2n12n−1. Each player holds a different set of views on those nn topics, more specifically, the ii-th player holds a positive view on the jj-th topic if i & 2j>0i & 2j>0, and a negative view otherwise. Here && denotes the bitwise AND operation.

You are going to organize a bridge tournament capable of accommodating at most kk pairs of players (bridge is played in teams of two people). You can select teams arbitrarily, but there is one catch: two players cannot be in the same pair if they disagree on 22 or more of those nn topics, as they would argue too much during the play.

You know that the ii-th player will pay you aiai dollars if they play in this tournament. Compute the maximum amount of money that you can earn if you pair the players in your club optimally.


The first line contains two integers nnkk (1n201≤n≤201k2001≤k≤200) — the number of hot topics and the number of pairs of players that your tournament can accommodate.

The second line contains 2n2n integers a0,a1,,a2n1a0,a1,…,a2n−1 (0ai1060≤ai≤106) — the amounts of money that the players will pay to play in the tournament.


Print one integer: the maximum amount of money that you can earn if you pair the players in your club optimally under the above conditions.

Examples Bridge Club solution codeforces


3 1
8 3 5 7 1 10 3 2

output Bridge Club solution codeforces



2 3
7 4 5 7




3 2
1 9 1 5 7 8 1 1



In the first example, the best we can do is to pair together the 00-th player and the 22-nd player resulting in earnings of 8+5=138+5=13 dollars. Although pairing the 00-th player with the 55-th player would give us 8+10=188+10=18 dollars, we cannot do this because those two players disagree on 22 of the 33 hot topics.

In the second example, we can pair the 00-th player with the 11-st player and pair the 22-nd player with the 33-rd player resulting in earnings of 7+4+5+7=237+4+5+7=23 dollars.

Leave a Comment

Your email address will not be published. Required fields are marked *