Official

E - Greedy Ant Editorial by evima


The result of the game does not change if we change Snuke’s actions in the game to the following:

  • Before the start of the game, Snuke prepares a mat and some coins. Initially, no coin is on the mat.
  • In Snuke’s turn, he does the following:
    1. Put one coin on the mat. (Let \(k\) be the number of coins on the mat after this.)
    2. Choose an integer \(n\) between \(0\) and \(k\).
    3. Remove \(n\) coins from the mat and take \(n\) candies of his choice.

If Snuke takes the candy Ant ignored in her last turn, it corresponds to taking that candy in this turn in the original game. On the other hand, if he takes one of the other candies, in the original game, it corresponds to taking that candy in this turn, or some previous turn when the candy Snuke took is not yet determined.

In the end, we should maximize the total tastiness of the candies Snuke takes in this new game. With a DP where the state consists of the pair of candies to the left and right of Ant and the number of coins on the mat, we can do it in \(O(N^3)\) time, which is fast enough for \(N \leq 400\).

posted:
last update: