new cost algorithm for beams