Make it Increasing

Codeforces Round #783 (Div. 2)   C题

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.

思路:我们通过观察可以得知,数组b存在0时可以使操作数最少,减少一次操作,也能将其他数的值减少。这时我们只要暴力寻找0点位置,然后再由0点向外扩展即可,这样就能满足单调行了,让离0点的变化次数最小,此时的时间复杂度是n^2,对于5000的数据足够了。

结论:暴力枚举0点,由0点向外扩展(向两端扩展)。

完整代码:

#include <bits/stdc++.h>

using namespace std;

#define int long long
const int mod=1e9+7;

const int N=5010;
int a[N];

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

    int ans=1e18;
    for(int i=1;i<=n;i++)
    {
        int res=0;
        int f=0,x=0;
        for(int j=i-1;j>=1;j--)
        {
            x=f/a[j]+1;
            res+=x;
            f=x*a[j];
        }

        x=0,f=0;
        for(int j=i+1;j<=n;j++)
        {
            x=f/a[j]+1;
            res+=x;
            f=x*a[j];
        }

        ans=min(ans,res);
    }
    cout<<ans<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值