my @array=(1,2,3,4,-1,-2,-3,11,12,13); my @sums=(); my $last=0; map { push @sums, $last+$_ if $last>0 and $_>0; $last=$_; } @array; my $max=0; map {$max=$_ if $_>$max } @sums; print "Max sum: $max\n";