A. Make it Increasing-Codeforces Round #783 (Div. 1)

Problem - 1667A - Codeforces

A. Make it Increasing

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn positive integers, and an array bb, with length nn. Initially bi=0bi=0 for each 1≤i≤n1≤i≤n.

In one move you can choose an integer ii (1≤i≤n1≤i≤n), and add aiai to bibi or subtract aiai from bibi. What is the minimum number of moves needed to make bb increasing (that is, every element is strictly greater than every element before it)?

Input

The first line contains a single integer nn (2≤n≤50002≤n≤5000).

The second line contains nn integers, a1a1, a2a2, ..., anan (1≤ai≤1091≤ai≤109) — the elements of the array aa.

Output

Print a single integer, the minimum number of moves to make bb increasing.

Examples

input

Copy

5
1 2 3 4 5

output

Copy

4

input

Copy

7
1 2 1 2 1 2 1

output

Copy

10

input

Copy

8
1 8 2 7 3 6 4 5

output

Copy

16

Note

Example 11: you can subtract a1a1 from b1b1, and add a3a3, a4a4, and a5a5 to b3b3, b4b4, and b5b5 respectively. The final array will be [−1−1, 00, 33, 44, 55] after 44 moves.

Example 22: you can reach [−3−3, −2−2, −1−1, 00, 11, 22, 33] in 1010 moves.

观察样例易知,必定要有一个是0的位置,所以又根据数据范围5000推测n^2算法,故可以枚举0位置左右n扩展,求出最小值即可

# include
# include
# include
# include
# include
# define mod 1000000007
using namespace std;
typedef long long int ll;

ll a[10000];

int main ()
{


    int n;

    cin>>n;

    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
    }

    ll ans=0x7f7ff7f7f7f;


    for(int i=1;i<=n;i++)
    {

        ll temp=0;

        ll pre=0;

        for(int j=i-1;j>=1;j--)
        {
            temp+=(abs(pre)/(abs(a[j])))+1;

            pre=((abs(pre)/(abs(a[j])))+1)*abs(a[j]);

        }

        pre=0;

        for(int j=i+1;j<=n;j++)
        {
            temp+=(abs(pre)/(abs(a[j])))+1;

            pre=((abs(pre)/(abs(a[j])))+1)*abs(a[j]);

        }

        ans=min(ans,temp);

    }

    cout<

你可能感兴趣的:(codeforces,codeforces,算法)