If you want the total amount on the header, you are complicating things. It is easier on a trailer. For a header, you need to store your records in a table, so that you can write the header with the amount. Or, if there will be too many, you need to write the header "out of sequence" and sort the file afterwards.
Can you get the spec changed, or are you stuck with it?