What weights can be measured on a balance with weights of $1,3,9,27$ grams? Generalize the problem!

Translate the above text into English, please retain the original text's line breaks and format, and output the translation result directly.
In the base-3 number system, every integer less than 81 can be written in the following form:

$$
n=a \cdot 27+b \cdot 9+c \cdot 3+d
$$

where \(a, b, c, d\) can take the values 0, 1, and 2. If, however, \(a, b, c, d\) can only take the values 0, +1, and -1, then the largest number that can be represented is 40, because \(27+9+3+1=40\). We claim, however, that every integer less than 41 can be represented this way. Specifically, for any such number, there is a form (I) where \(a=0\) or 1, and \(b, c, d\) can be 0, 1, or 2. If any of \(d, c, b\) is 2, and the first such is \(d\), then we write \(d=3-1\) and thus

$$
n=27a + 9b + 3(c+1) - 1
$$

If \(c+1=2\), then we write \(3(c+1)=9-3\):

$$
n=27a + 9(b+1) - 3 - 1
$$

If \(c+1=3\), then

$$
n=27a + 9(b+1) - 1, \text{ etc. }
$$

Finally, \(n\) takes the form (I) where \(a, b, c, d\) can take the values 0, 1, -1. \(a\) cannot be 2, because then the smallest value would be \(2 \cdot 27 - 9 - 3 - 1 = 41\), whereas we are concerned with numbers less than 41. Accordingly, with weights of 1, 3, 9, 27 grams, any object with an integer weight of up to 40 grams can be measured by placing the weights on either the left or right pan.

Generalization: If we have weights of \(1, 3, 9, \ldots, 3^n\) grams, then any object with an integer weight up to \(1+3+9+\ldots+3^n=\frac{3^{n+1}-1}{3-1}\) grams can be measured with them. Finally, if we have two of each weight, any object with an integer weight up to \(3^{n+1}\) grams can be measured by placing the weights in the same pan.