Polynomial Calculator
Time limit2sMemory limit128 MB
Find the minimum number of key presses to build a given monic polynomial using a calculator that applies operations sequentially without memory.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Math
- Solved
- No attempts yet
Problem
There is a special calculator for evaluating polynomials. It has 14 keys: the digits 0 through 9, plus +, ×, x, and =.
For example, to compute x^3 + x + 11, one possible key sequence is x, ×, x, ×, x, +, x, +, 1, 1, =. To compute x^3 + 2x^2 + 11, one possible key sequence is x, +, 2, ×, x, ×, x, +, 1, 1, =.
A normal calculator would interpret x, +, 2, ×, x, ×, x, +, 1, 1, = as x + 2x^2 + 11. This calculator has no separate memory, so each operation is applied immediately to the value currently stored in the calculator and the next value entered. With that sequence, the stored value becomes x, x + 2, x^2 + 2x, x^3 + 2x^2, and finally x^3 + 2x^2 + 11.
To keep the problem simple, the leading coefficient is always 1. Negative coefficients do not appear.
Given a polynomial, find the minimum number of key presses needed to compute it with this calculator.
Input
The first line contains the degree N of the polynomial. (1 ≤ N ≤ 10,000)
The second line contains the coefficients of the polynomial in order from the highest-degree term to the constant term. The leading coefficient is always 1, every coefficient is nonnegative, and no coefficient exceeds 1,000,000,000.
Output
Print the minimum number of key presses required.