sub smart { my $t=sum @_; if ($t%2) { my $i=($t-1)/2; ($i -= $_[$_])<0 and return $_ for 0..$#_; } else { my $i=$t/2-1; my ($found1, $found2); for (0..$#_) { ($found1=$_), last if ($i -= $_[$_])<0; } my $j=$t/2; for (0..$#_) { ($found2=$_), last if ($j -= $_[$_])<0; } return ($found1+$found2)/2; } }