A quantum algorithm to count weighted ground states of classical spin Hamiltonians