Here's a volatile solution (assuming you don't have more than 10k rows):
The array formula in G2 is
=SUM(INDEX(B2:$B$10000,AGGREGATE(15,6,(1/ISNUMBER(B2:$B$10000))*(ROW(B2:$B$10000)-(ROW(B2)-1)),ROW(INDIRECT("1:"&D2)))))
(you could possibly reduce volatility by hard-coding the array constant - instead of using INDIRECT() - but I'm not very familiar with all of LibreOffice's volatile functions)