weighted algorithm