I count your attempt at 40 chars:
#1234567890123456789012345678901234567890
perl -wle "$a=<>;($x=<>)>0?$_+=$x:0 while$a--;print
My attempt is 23 chars:
#123456789012345678901234
perl -wle "print 0+map{1..<>}2..<>"
Likely there are ways to improve on that, for example the space between print and 0 likely can be eliminated. If you use Perl 5.10 or higher, you can replace print with say, shaving off another two characters.
Update: Relaxing the condition of respecting the number of items in the first line doesn't shorten the code for me:
#123456789012345678901234
perl -wle "<>;print 0+map{1..<>}<>"