use v5.16; # my $L = 100; # my $N = 10; # my $W = "14 85 77 26 50 45 66 79 10 3"; my $L = <>; my $N = <>; my $W = <>; chomp( $L, $N, $W ); my @W = sort ( split / /, $W ); while ( my ( $i, $e ) = each @W ) { $L = $L - $e; if ( $L < 0 ) { say $i; last; } }