当前位置:   article > 正文

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

make it increasing codeforces

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扩展,求出最小值即可

  1. # include<iostream>
  2. # include<vector>
  3. # include<cstring>
  4. # include<map>
  5. # include<algorithm>
  6. # define mod 1000000007
  7. using namespace std;
  8. typedef long long int ll;
  9. ll a[10000];
  10. int main ()
  11. {
  12. int n;
  13. cin>>n;
  14. for(int i=1; i<=n; i++)
  15. {
  16. cin>>a[i];
  17. }
  18. ll ans=0x7f7ff7f7f7f;
  19. for(int i=1;i<=n;i++)
  20. {
  21. ll temp=0;
  22. ll pre=0;
  23. for(int j=i-1;j>=1;j--)
  24. {
  25. temp+=(abs(pre)/(abs(a[j])))+1;
  26. pre=((abs(pre)/(abs(a[j])))+1)*abs(a[j]);
  27. }
  28. pre=0;
  29. for(int j=i+1;j<=n;j++)
  30. {
  31. temp+=(abs(pre)/(abs(a[j])))+1;
  32. pre=((abs(pre)/(abs(a[j])))+1)*abs(a[j]);
  33. }
  34. ans=min(ans,temp);
  35. }
  36. cout<<ans;
  37. return 0;
  38. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/71492
推荐阅读
相关标签
  

闽ICP备14008679号