abs($S-$B) && $B>=$S && $B>$A){ $ans += $B; }elseif(abs($S-$A) <= abs($S-$B)){ $ans += abs($S-$A) + $S; }elseif(abs($S-$A) > abs($S-$B) && $S>$B){ $ans += abs($S-$B) + $S; }elseif(abs($S-$A) > abs($S-$B) && $B>=$S){ if($A - $S*2 +1 > 0){ $ans += $A + $B -1; }else{ $ans += $A *2 + $B -$S*2; } } echo $ans;