SRM 598 - Dynamic programming.