Bridge Club
Contest: Codeforces Round 743 (Div. 1)
Difficulty rating: 2800
Tags: flows, graph matchings, graphs, greedy

There are currently $$$n$$$ hot topics numbered from $$$0$$$ to $$$n-1$$$ at your local bridge club and $$$2^n$$$ players numbered from $$$0$$$ to $$$2^n-1$$$. Each player holds a different set of views on those $$$n$$$ topics, more specifically, the $$$i$$$-th player holds a positive view on the $$$j$$$-th topic if $$$i\ \&\ 2^j > 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 $$$k$$$ pairs of players (bridge is played in teams of two people). You can select teams arbitrarily while each player is in at most one team, but there is one catch: two players cannot be in the same pair if they disagree on $$$2$$$ or more of those $$$n$$$ topics, as they would argue too much during the play.

You know that the $$$i$$$-th player will pay you $$$a_i$$$ 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.

Input format:
The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 20$$$, $$$1 \le k \le 200$$$) — the number of hot topics and the number of pairs of players that your tournament can accommodate.

The second line contains $$$2^n$$$ integers $$$a_0, a_1, \dots, a_{2^n-1}$$$ ($$$0 \le a_i \le 10^6$$$) — the amounts of money that the players will pay to play in the tournament.

Output format:
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.

Constraints:
Time limit: 2.5 s
Memory limit: 512.0 MB

Note:
In the first example, the best we can do is to pair together the $$$0$$$-th player and the $$$2$$$-nd player resulting in earnings of $$$8 + 5 = 13$$$ dollars. Although pairing the $$$0$$$-th player with the $$$5$$$-th player would give us $$$8 + 10 = 18$$$ dollars, we cannot do this because those two players disagree on $$$2$$$ of the $$$3$$$ hot topics.

In the second example, we can pair the $$$0$$$-th player with the $$$1$$$-st player and pair the $$$2$$$-nd player with the $$$3$$$-rd player resulting in earnings of $$$7 + 4 + 5 + 7 = 23$$$ dollars.

Example input:
3 1
8 3 5 7 1 10 3 2

Example output:
13

Example input:
2 3
7 4 5 7

Example output:
23

Editorial:
Let's make a graph in which the vertices are the players and there is an edge of weight $$$a_i + a_j$$$ between the $$$i$$$-th and the $$$j$$$-th player if they can play together. We can notice that the problem can then be solved by finding a matching of size at most $$$k$$$ with the biggest sum of weights in this graph.
To solve this problem efficiently we can make the following observations:
1. The graph is bipartite.
This stands from the fact that if two players disagree at exactly $$$1$$$ topic then the numbers of positive views that they hold have different parities.
2. We can limit ourselves to considering only the $$$(2n-1)(k-1) + 1$$$ edges with the biggest weights.
We can prove this with a proof by contradiction. Firstly, we can notice that if we use a particular edge in the matching then we prohibit ourselves from using at most $$$2(n-1)$$$ other edges with each of the matched vertices being incident to exactly $$$n-1$$$ of those edges, because each vertex has degree $$$n$$$ in this graph. Now we can see that if we were to use an edge that's not one of those $$$(2n-1)(k-1)+1$$$ best ones, then we can just replace it with one of those best ones, because we know that at least one of them will not be prohibited.
Combining those two observations we are left with a bipartite graph with $$$O(nk)$$$ edges and $$$O(nk)$$$ vertices in which we can find a matching with maximum cost and size at most $$$k$$$ with for example one of the standard min cost max flow algorithms (we can look for a matching of size $$$k$$$ because all edges have non-negative weights).
The only problem that's left for us to solve is efficiently selecting those best edges. Because there are $$$O(n2^n)$$$ edges in total we cannot use a standard sorting algorithm as that would run in $$$O(n^2 2^n)$$$. Instead we can for example use the quick select algorithm which solves this problem in $$$O(n2^n)$$$.
Our final time complexity is then $$$O(n2^n + nk^2\log(kn))$$$.