$l=<>;<>;$a+=0<=($l-=$_)for sort{$a<=>$b}split$",<>;print$a