MAQ Software Interview Question

dp based