How to maximize rewards with exactly k tasks | MathWorks Interview Question