A Hajós-like theorem for weighted coloring