awk '$0 == $0+0{
summ += $0
next}
{
if(summ)
format="%06d\n%s\n"
else
format="%s%s\n"
printf format, summ, $0
summ=""}
END {
if(summ)
printf "%06d\n", summ}' foo.txt