My logic:

If n is a power of 2, the answer is -1.
Now, all odd numbers in the range (1, n] can be connected to 1 to attain the least non-zero cost (if n is odd, n & 1 is 1).
So I maintain a variable ans and add the number of odd number to it which is \text{ceil}\Big(\dfrac{n}{2}\Big) - 1.
Consider the even numbers in (4, 8). The minimum cost can be attained if we take their bitwise AND with 4. I came up with a formula which returns the sum of the AND values of all even numbers in the range (2^k, 2^{k+1}) which is given by \text{floor}\Bigg(\dfrac{2^{k+1} - 2^k - 1}{2}\Bigg)\cdot2^k.
\forall 2^k, k \in \mathbb{Z}^+, the minimum cost can be attained if 2^k is connected to 2^k+1. The cost is simply 2^k.
The largest multiple of 2 less than or equal to n is given by 2^{\text{floor}(\log_2{n})}.
So I run a loop for all integers in [0, \text{floor}(\log_2{n})).
In each iteration, I add 2^{i+1} and the sum of the AND values of those even numbers to the answer.
The only task left is to add the AND of even numbers in (2^{\text{floor}(\log_2{n})}, n] which is simply \text{floor}\Bigg(\dfrac{n-2^{\text{floor}(\log_2{n})}}{2}\Bigg) if the number is even. Or else it is \text{floor}\Bigg(\dfrac{(n-1)-2^{\text{floor}(\log_2{n-1})}}{2}\Bigg).
I got WA for this approach. Please help me find out what I’ve done wrong.

In range (4,8) , the even no. 6 could be connected to 2 that is minimum cost .

(6\&2) = 2 whereas (6 \& 4)=4