My algo to compute the weigh with terms