read -dx n m c for c in $c { a[c*10+i++]= } for c in ${!a[@]} {((r+=(m-=c/10)>=0)) } echo $r