Pergunta de entrevista da empresa Mastercard

DP based problem(medium-hard) in first round.