#2836

Maximize Value of Function in a Ball Passing Game

international master · 1985 · lc hard +32 · 30.5% accepted · 318 likes · top 8%

Description

An integer array receiver of length n and an integer k are given. There are n players in a ball-passing game.

Choose a starting player i. Player i passes the ball to receiver[i], who passes to receiver[receiver[i]], and so on for k passes. The score is the sum of all player indices who touched the ball, including repetitions: i + receiver[i] + receiver[receiver[i]] + ... + receiver^(k)[i].

Return the maximum possible score.

Notes:

- receiver may contain duplicates.

- receiver[i] may equal i.

Code

1
2
3