sum optimization for logical vectors and comparison