Using some of @noob2 notation, if:
$x_i$: the initial dollar amount of asset i
$t_i$: the transacted dollar amount of asset i
$\theta$: the fee fraction (0.2%).
$w_i$: the desired post balancing weights
Then you have the minimisation problem:
$$ \min_{t}{f(t)} = \sum_i \left ( \frac{x_i + t_i -\theta|t_i|}{\sum_j x_j +t_j -\theta|t_j|} - w_i\right )^2 $$
The minimum is zero (target weights achieved for each asset) and at this point we must have $\frac{\partial f}{\partial t_i} = 0$.
------- Contains errors: left for demonstration
$$ \frac{\partial f}{\partial t_i} = 2\frac{1-\theta sign(t_i)}{\sum_j x_j + t_j -\theta|t_j|} \left (1 - \frac{x_i + t_i -\theta |t_i|}{\sum_j x_j + t_j -\theta|t_j|} \right )$$
So multiplying through at with optimality conditions yields:
$$ x_i + t_i - \theta|t_i| = \sum_j x_j + t_j -\theta|t_j| \quad \forall i$$
Or in linear algebra notation, where $\mathbf{1}$ is a matrix of ones:
$$ \mathbf{(I - 1)}(\mathbf{t} - \theta |\mathbf{t}|) = (\mathbf{1-I}) \mathbf{x} $$
--------EDIT: Looking again, refreshed, the derivative above is wrong. The real derivative is more complicated. I leave this answer here if someone can complete but it might simply be too messy algebra to solve for a closed form optimisation solution in matrix notation
For
$X_i = x_i + t_i - \theta | t_i |$ (vector)
$S = \sum_j x_j + t_j -\theta | t_j | $ (scalar)
Then:
$$ f(t) = \sum_i \left ( \frac{X_i}{S} - w_i \right )^2 $$
$$ \frac{\partial f}{\partial t_i} = 2 \left ( \frac{X_i}{S} - w_i \right ) \left( \frac{1 - \theta sign(t_i)}{S} \right ) + \sum_j 2 \left( \frac{X_j}{S} - w_j \right ) \left( \frac{-X_j (1-sign(t_i))}{S^2}\right )$$
When you set optimality condition and multiply through:
$$\left ( \frac{X_i}{S} - w_i \right ) + \sum_j \left( \frac{X_j}{S} - w_j \right ) \left( \frac{-X_j}{S}\right ) = 0$$
$$ \implies S X_i - S^2 w_i + \sum_j \left( X_j X_j - S w_j \right ) = 0 \quad \forall i$$
This looks suspiciously quadratic form-y and probably not going to yield a nice formula.
One other thing that I didn't include that might have improved matters is the constraint: $\sum_j t_j = 0$, ie. whatever you sell you buy in another asset. Would change the optimality conditions.