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.