Dynamic Programming21 sections · 915 units
Open in Course

Codeforces 455A Boredom - Final Answer

Reading the answer

4.4. Determine the final answer

You process all values from 22 up to MM using the formula dp[i]=max⁡(dp[i−1], dp[i−2]+i⋅cnt[i]),for i=2,3,…,M.dp[i] = \max\bigl(dp[i - 1],\ dp[i - 2] + i \cdot \text{cnt}[i]\bigr), \quad \text{for } i = 2, 3, \dots, M.

The final answer to the problem is simply dp[M].dp[M].

This solution runs in O(M)O(M) time and uses O(M)O(M) space, where MM is the maximum value in the input. The structure is exactly the same as in the previous problems: define the state, find the transition, set the base cases, and read the final answer from the appropriate dpdp state.