Added functions for calculating the min energy. DP contains error somewhere...