Given integers $g, h, k, n$ how to find the smallest integer with $k\ 1$-bits that is the sum of two integers with $g, h\ 1$-bits respectively. Moreover, all three numbers (summands and sum) can have at most $n$ bits, i.e. the result is the n-bit number with $k\ 1$'s and $(k - n)$ zeros, for instance $\underbrace{0...0}_\text{n -k}\underbrace{1...1}_\text{k}$. These $1$-bits don't have to be consecutive. Of course, such a number may not exist at all. You can think of this output integer (if it exists) as a permutation of a set of $n - k$ zeroes and $k$ ones.
I have a strong feeling that this is a dynamic programming problem but I can't get what is a subproblem here. I actually have no clue about DP relation for this problem. Could anyone give me some hints?
UPDATE. I've came up with the following plan:
Let $dp(g, h, k, n, carry)$ be such a sum, where $carry$ is an optional bit for carrying. Then $dp(g, h, k, n + 1, 0)$ = $min\{dp(g,h,k, n,0),\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ :(0, 0) \\2^n + dp(g,h,k - 1, n,1)\\ 2^n + dp(g - 1, h, k-1, n, 0), \ \ :(1, 0) \\ 2^n + dp(g, h - 1, k - 1, n, 0), \ \ :(0, 1) \}$
$dp(g, h, k, n + 1, 1)$ = $min\{dp(g - 1, h, k, n, 1),\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ :(1, 0) \\ dp(g, h - 1, k , n, 1),\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ :(0, 1) \\ dp(g-1,h-1,k, n,0)\ \ \ \ \ \ \ \ \ \ \ \ \ \ :(1, 1)\\ 2^n + dp(g - 1, h - 1, k - 1, n, 1)\}$
I realized that this is too complex for me to handle on my own. I don't know what are the initial values (or base case of recursion in top-down approach) of this DP-table should be. Provided $g + h = k \leq n$ it is obvious that $dp(g, h, k, n, ?) = 2^k - 1$. As the question mark indicates, I don't understand what to do with the optional carrying bit in this case. I do not really understand how I should indicate there is no such sum, etc. Also, how to fill this DP-table? Should I use top-down approach? I need further help to proceed.