The only suitable way is a pml or better pml2-macro.
Reason: exporting the data to a file and run externally a small programme for calculating the totals isn't an advantage. The main issue is drawing the two tables with dynamic frames. I assume the number of lines is not fixed and can vary with the bracket design.
You will have to invest some time and research work for a suitable solution.