modified maximal sum to include indices