As a complement to the other answers, let me elaborate the idea of using base-3 representation in details. Consider any positive integer $m$ that can be represented in base 3 with $n$ digits as follows:
$$
m=\sum_{i=0}^{n-1}a_i\cdot 3^{i}, \quad\quad a_i\in\{0,1,2\}
$$
Our goal is to represent it using $a_i\in\{-1,0,1\}$. This can be done as follows
$$
m^*=m-\sum
_{i=0}^{n-1}3^i=\sum_{i=0}^{n-1}(a_i-1)\cdot 3^{i}=\sum_{i=0}^{n-1}c_i\cdot 3^{i}, \quad\quad c_i\in\{-1,0,1\}
$$
Note
$$
\sum
_{i=0}^{n-1}3^i=\frac{1-3^n}{1-3}=\frac{3^n-1}{2}
$$
Since
$$
m\in\{k\in\mathbb{Z}:k\in[0,3^n-1]\}
$$
we have
$$
m^*\in\left\{k\in\mathbb{Z}:k\in\left[-\frac{3^n-1}{2},\frac{3^n-1}{2}\right]\right\}
$$
In other words, any integer between $-\frac{3^n-1}{2}$ and $\frac{3^n-1}{2}$ can be represented in base 3 with $n$ digits where the digits can be $-1$, $0$, or $1$.
Then, given $m^*$, we can find the smallest $n$ such that it can be represented as above. If $m^*>0$, then, we need
$$
m^*\leq\frac{3^n-1}{2}\Rightarrow n\geq\log_3(2m^*+1)
$$
So the smallest $n$ is $\lceil \log_3(2m^*+1) \rceil$.
Back to the problem, to "weigh" any integer, we can first identify the smallest $n$ such that the integer is in the range. Since $m^*=40$, we have $n=\lceil \log_3(2\times 40+1) \rceil=4$. To obtain the combination of weights and their positions, we can represent the target integer using base 3 with $\{-1,0,1\}$. Then, for each digit (weight), if it is $-1$, then put it on the same side as the target integer; if it is 0, then do not use it; if it is 1, then put it on the other side of the scale.
Extension. The above framework works only if each weight can be used only once (no extra ones). Suppose that the number of each weight is 2. Then, the question becomes how to represent an integer in base 5 where each digit can be $-2$, $-1$, $0$, $1$, and $2$. In this case, to map $\{0,1,2,3,4\}$ to $\{-2,-1,0,1,2\}$, we subtract $\sum_{i=0}^{n-1}2\cdot 5^{i}$. Then, the range for $m^*$ becomes
$$
-\frac{5^n-1}{2}\leq m^* \leq\frac{5^n-1}{2}
$$
The same idea can be generalized in other similar problems.