TODO: Optimize using dynamic programming
TODO: Optimize using dynamic programming