use strict; use warnings; my $num = ; my @w = split(' ', ); my $half = $num % 2 == 0 ? $num/2-1 : $#w/2; my @ary_a = sort {$a <=> $b} @w[0..$half]; my @ary_b = sort {$a <=> $b} @w[$half+1..$#w]; my @sum; my %store; for(;;){ @sum = (0, 0); $sum[0] = eval join('+', @ary_a); $sum[1] = eval join('+', @ary_b); if(defined($store{$sum[0]}) and defined($store{$sum[1]})){ print 'impossible'; last; }else{ $store{$sum[0]} = 1; $store{$sum[1]} = 1; } if($sum[0] > $sum[1]){ push @ary_b, shift @ary_a; @ary_b = sort {$a <=> $b} @ary_b; }elsif($sum[0] < $sum[1]){ push @ary_a, shift @ary_b; @ary_a = sort {$a <=> $b} @ary_a; }else{ print 'possible'; last; } }