Wrote logic to generate cumulative energy matrix