#include<bits/stdc++.h>
using namespace std;

const int MAXN = 5e3+5;
const int INF = 1e9+7;

#define ll long long
#define mp make_pair
#define pb push_back

int a[MAXN],dp[MAXN][MAXN],par[MAXN][MAXN];
// dp[i][j] = Javab baraye [0...i] ke akharin baze  hadeaksar be j khatm she

int n;

void Print()
{
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }
}

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

    //Print();

    for(int i=0;i<=n;++i){
        int sums=0;
        for(int j=i;j<=n;++j){
            sums += a[j];
            par[i][j] = sums;
        }
    }

    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            dp[j][i] = INF;
        }
    }

    for(int i=1;i<=n;++i){
        for(int j=i;j>=1;--j){
           /// cout << "Enter ==>  i==> " << i << " j==> " << j << endl;
            dp[j][i] = min(dp[j][i],dp[j+1][i]);
         //   cout << "After One ==> dp[j][i] ==> " << dp[j][i] << endl;
            int l=0,r=j;
            while(r-l>1){
                int mid = l + (r-l)/2;
                if(par[mid][j-1] <= par[j][i]) r = mid;
                else l = mid;
            }
            if(a[j-1] > par[j][i]) dp[j][i] = INF;
            else dp[j][i] = min(dp[j][i] , dp[r][j-1] + (i-j));
           // cout << "Sum [j...i] ==> " << par[j][i] << " r ==>" << r << endl;
            //cout << "dp[j][i] ==> " << dp[j][i] << endl;
        }
    }

   // Print();

    cout << dp[1][n];
}


#include<bits/stdc++.h>
using namespace std;

const int MAXN = 5e3+5;
const int INF = 1e9+7;

#define ll long long
#define mp make_pair
#define pb push_back

int a[MAXN],dp[MAXN][MAXN],mx[MAXN][MAXN],sum[MAXN];

// dp[i][j] = Mximum javab agar [1..i] dashte bashim va groh [j..i] niz bashad

int n;

int get(int l,int r){
    return sum[r] - sum[l-1];
}

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

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

    for(int i=1;i<=n+5;++i){
        for(int j=1;j<=n+5;++j){
            dp[i][j] = INF;
            mx[i][j] = INF;
        }
    }

    for(int i=1;i<=n;++i){
        for(int j=i;j>=1;--j){
            int l=0,r=j;
            while(r-l>1){
                int mid = l + (r-l)/2;
                if(get(mid,j-1) <= get(j,i)) r=mid;
                else l=mid;
            }
            if(r==j && j!=1) dp[j][i] = INF;
         //       cout << i << " " << j << endl;

            else dp[j][i] = (i-j) + mx[j-1][r];
        }
        //mx[i][i] = INF;
        for(int j=i;j>=1;--j){
            mx[i][j] = min(mx[i][j+1],dp[j][i]);
        }
    }

    int res=INF;
    for(int i=1;i<=n;++i){
        res = min(res,dp[i][n]);
    }
    cout << res;
}


#include <bits/stdc++.h>
#include <bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
#include <random>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define REP(i,a,b) for(int i=int(a);i<=int(b);i++)
#define MOD 1000000007
#define MOD2 1000000009
#define INF 2000000000
#define DESPACITO 1000000000000000
#define E 998244353
#define pb push_back
#define mp make_pair
#define double long double
#define pdd pair<double,double>
#define ll long long

using namespace std;
//using namespace __gnu_pbds;

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int n;
int arr[5001];
int dp[5002][5002];
int pre[5001];

int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    cin >> n;
    REP(i,1,n)
    {
        cin >> arr[i];
        pre[i] = pre[i-1] + arr[i];
    }
    deque<int>q;
    REP(i,1,n+1)
        REP(j,0,n+1)
            dp[i][j] = MOD;
    REP(j,0,n+1)
        dp[n+1][j] = 0;
    for(int i = n; i>=1; i--)
    {
        REP(j,i+1,n+1)
        {
            while((!q.empty()) && (((j-i-1)+dp[j][i]) <= ((q.back()) - i - 1) + dp[q.back()][i]))
                q.pop_back();
            q.push_back(j);
        }
        for(int j = i-1;j >= 0; j--)
        {
            int val = pre[i-1] - pre[j-1];
            while((!q.empty()) && (pre[q.front() - 1] - pre[i-1] < val))
                q.pop_front();
            if(q.empty())
                continue;
            dp[i][j] = min(dp[i][j],((q.front())-i-1) + dp[q.front()][i]);
        }
        q.clear();
    }
    cout << dp[1][0];
}


#include <bits/stdc++.h>
#include <bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
#include <random>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define REP(i,a,b) for(int i=int(a);i<=int(b);i++)
#define MOD 1000000007
#define MOD2 1000000009
#define INF 2000000000
#define DESPACITO 1000000000000000
#define E 998244353
#define pb push_back
#define mp make_pair
#define double long double
#define pdd pair<double,double>
#define ll long long

using namespace std;
//using namespace __gnu_pbds;

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int n;
int arr[5001];

int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    cin >> n;
    REP(i,1,n)
        cin >> arr[i];
    int sum = 0;
    int minim = n;
    REP(i,1,n)
    {
        sum+=arr[i];
        int ct = i-1;
        int prev = i;
        int curr = 0;
        REP(j,i+1,n)
        {
            curr+=arr[j];
            if(curr>=sum)
            {
                sum = curr;
                curr = 0;
                continue;
            }
            ct++;
        }
        if(curr < sum && curr >0)
            ct++;
        minim=min(minim,ct);
    }
    cout <<  minim;
}


#include <bits/stdc++.h>
#include <bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
#include <random>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define REP(i,a,b) for(int i=int(a);i<=int(b);i++)
#define MOD 1000000007
#define MOD2 1000000009
#define INF 2000000000
#define DESPACITO 1000000000000000
#define E 998244353
#define pb push_back
#define mp make_pair
#define double long double
#define pdd pair<double,double>
#define ll long long

using namespace std;
//using namespace __gnu_pbds;

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int n;
int arr[5001];

int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    cin >> n;
    REP(i,1,n)
        cin >> arr[i];
    int sum = 0;
    int minim = n;
    REP(i,1,n)
    {
        sum+=arr[i];
        int temp = sum;
        int ct = i-1;
        int curr = 0;
        REP(j,i+1,n)
        {
            curr+=arr[j];
            if(curr>=temp)
            {
                temp = curr;
                curr = 0;
                continue;
            }
            ct++;
        }
        minim=min(minim,ct);
    }
    cout <<  minim;
}


//xtwakioi! xtwddYnoi(双重含义)!
#include <bits/stdc++.h>
#define ri register
#define int long long
#define E (n+1)
#define mk make_pair
using namespace std; const int N=5010;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int n,h[N],dp[N][N],qz[N];
signed main()
{
    n=read();
    for(ri int i=1;i<=n;i++) h[i]=read(), qz[i]=qz[i-1]+h[i];

    memset(dp,0x3f,sizeof(dp));

    dp[1][1]=0;
    dp[1][0]=dp[0][0]=0;
    for(ri int i=2;i<=n;i++)
    {
        int L=i;
        for(ri int j=i;j;j--)
        {
            if(!L) dp[j][i]=min(dp[j][i],dp[1][j-1]+i-j);
            while( L && qz[i]-qz[j-1]>=qz[j-1]-qz[L-1])
            {
                dp[j][i]=min(dp[j][i],dp[L][j-1]+i-j);
                L--;
            }
        }
    }

    int qwq=1e9;
    for(ri int i=0;i<n;i++) qwq=min(qwq,dp[i+1][n]);
    printf("%lld\n",qwq);
    return 0;
}


//xtwakioi! xtwddYnoi(双重含义)!
#include <bits/stdc++.h>
#define ri register
#define int long long
#define E (n+1)
#define mk make_pair
using namespace std; const int N=5010;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int n,h[N],dp[N][N],qz[N];
signed main()
{
    n=read();
    for(ri int i=1;i<=n;i++) h[i]=read(), qz[i]=qz[i-1]+h[i];

    memset(dp,0x3f,sizeof(dp));

    dp[1][1]=0;
    dp[1][0]=dp[0][0]=0;
    for(ri int i=2;i<=n;i++)
    {
        int L=i;
        for(ri int j=i;j;j--)
        {
            if(!L) dp[j][i]=min(dp[j][i],dp[1][j-1]+i-j);
            while( L && qz[i]-qz[j-1]>=qz[j-1]-qz[L-1])
            {
                dp[j][i]=min(dp[j][i],dp[L][j-1]+i-j);
                L--;
            }
        }
    }

    int qwq=1e9;
    for(ri int i=0;i<n;i++) qwq=min(qwq,dp[1][i]+dp[i+1][n]);
    printf("%lld\n",qwq);
    return 0;
}


//xtwakioi! xtwddYnoi(双重含义)!
#include <bits/stdc++.h>
#define ri register
//#define int long long
#define E (n+1)
#define mk make_pair
using namespace std; const int N=5010;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int n,h[N],dp[N][N],qz[N];
signed main()
{
    n=read();
    for(ri int i=1;i<=n;i++) h[i]=read(), qz[i]=qz[i-1]+h[i];

    memset(dp,0x3f,sizeof(dp));

    dp[1][1]=0;
    dp[1][0]=dp[0][0]=0;
    for(ri int i=2;i<=n;i++)
    {
        int L=i;
        for(ri int j=i;j;j--)
        {
            if(!L) dp[j][i]=min(dp[j][i],dp[1][j-1]+i-j);
            while( L && qz[i]-qz[j-1]>=qz[j-1]-qz[L-1])
            {
                dp[j][i]=min(dp[j][i],dp[L][j-1]+i-j);
                L--;
            }
        }
    }

    int qwq=1e9;
    for(ri int i=0;i<n;i++) qwq=min(qwq,dp[1][i]+dp[i+1][n]);
    printf("%d\n",qwq);
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
long long n,a[500005],S[500005];
pair < long long , int > dp[500005];
main () {
	cin>>n;

	for (int i=1; i<=n; i++) {
		cin>>a[i];
		S[i]=S[i-1]+a[i];
		dp[i].F=1e9;
	}

	dp[0].S=1;

	for (int i=1; i<=n; i++) {
		if (dp[i].F>=dp[i-1].F+a[i]) dp[i].F=dp[i-1].F+a[i],dp[i].S=dp[i-1].S;
		int l=i+1,r=n,mid,idx=-1;

		while (l<=r) {
			mid=(l+r)/2;
			if (S[mid]-S[i]>=dp[i].F) { idx=mid; r=mid-1; }
				else l=mid+1;
		}

		if (idx==-1) continue;
		if (dp[idx].F>=S[idx]-S[i]) dp[idx].F=S[idx]-S[i],dp[idx].S=dp[i].S+1;
	}

	cout<<n-dp[n].S<<endl;
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
long long n,a[500005],S[500005];
pair < long long , int > dp[500005];
main () {
	cin>>n;

	for (int i=1; i<=n; i++) {
		cin>>a[i];
		S[i]=S[i-1]+a[i];
		dp[i].F=1e9;
	}

	dp[0].S=1;

	for (int i=1; i<=n; i++) {
		if (dp[i].F==1e9) dp[i].F=dp[i-1].F+a[i],dp[i].S=dp[i-1].S;
		int l=i+1,r=n,mid,idx=-1;

		while (l<=r) {
			mid=(l+r)/2;
			if (S[mid]-S[i]>=dp[i].F) { idx=mid; r=mid-1; }
				else l=mid+1;
		}

		if (idx==-1) continue;
		dp[idx].F=S[idx]-S[i];
		dp[idx].S=dp[i].S+1;
	}

	cout<<n-dp[n].S<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define maxn 2000005 /*rem*/
#define mod 1000000007
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
ll ksm(ll a, ll b) {
   if (!b) return 1;
   ll ns = ksm(a, b >> 1);
   ns = ns * ns % mod;
   if (b & 1) ns = ns * a % mod;
   return ns;
}
int n;
ll h[maxn];
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> h[i];

	}
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (h[i] < h[i - 1]) {
			h[i + 1] += h[i];
			h[i] = h[i - 1];
			cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define maxn 2000005 /*rem*/
#define mod 1000000007
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
ll ksm(ll a, ll b) {
   if (!b) return 1;
   ll ns = ksm(a, b >> 1);
   ns = ns * ns % mod;
   if (b & 1) ns = ns * a % mod;
   return ns;
}
int n;
ll h[maxn];
ll dp[maxn];
int res[maxn];
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> h[i];

	}
	ll s = 0;
	for (int i = 1; i <= n; i++) {
		if (i == 1) dp[i] = h[i], res[i] = 0;
		else {
			s = 0;
			for (int j = i; j >= 1; j--) {
				s += h[j];
				if (s >= dp[j - 1]) {
					dp[i] = s;
					res[i] = res[j - 1] + (i - j);
		//			cout << "??? " << i << ' ' << j << endl;
					break;

				}
			}
		}
	//	cout << i << ' ' << dp[i] << ' ' << res[i] << endl;
	}
	cout << res[n] << endl;
	return 0;
}


#include<bits/stdc++.h>
#define sc1(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scl1(a) scanf("%lld",&a)
#define scl2(a,b) scanf("%lld%lld",&a,&b)
#define scl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define pr1(a) printf("%d ",a)
#define pr2(a,b) printf("%d %d ",a,b)
#define pr3(a,b,c) printf("%d %d %d ",a,b,c)
#define mii map<int,int>
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define mii map<int,int>
#define ll long long
#define ii pair<int,int>
#define loop(i,a,b) for(int i=(a);i<(b);i++)
#define rloop(i,a,b) for(int i=(a);i>(b);i--)
#define tr(it,c) for(decltype((c).begin()) it=(c).begin();it!=(c).end();it++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define ft first
#define sd second
#define mod 998244353
#define mod2 1000000007
#define SYNC ios_base::sync_with_stdio(false);cin.tie(NULL)

using namespace std;
int gcd(int a,int b)
{
	if(a<b)
	swap(a,b);
	if(b==0)
	return a;
	while((a=a%b)!=0)
	{
		swap(a,b);
	}
	return b;
}
ll modpow(ll x,int y)
{
	ll res=1;
	while(y>0)
	{
		if(y&1)
		res=res*x%mod;
		y=y>>1;
		x=x*x%mod;
	}
	return res;
}
ll ncr(int n,int r)
{
	ll f1=1,f2=1;
	loop(i,n-r+1,n+1)
	f1=f1*i%mod;
	loop(i,1,r+1)
	f2=f2*i%mod;
	return f1*modpow(f2,mod-2)%mod;
}
int *sieve(int n)
{
	int *lpf=new int[n+1];
	for(int i=1;i<=n;i++)
	lpf[i]=i;
	int rt=(int)floor(sqrt(n))+1;
	loop(i,2,rt)
	{
		if(lpf[i]!=i)
		continue;
		for(int j=i*i;j<=n;j+=i)
		{
			if(lpf[j]==j)
			lpf[j]=i;
		}
	}
	return lpf;
}

void solve()
{
	int n,sum;
	cin>>n;
	int a[n];
	ii dp[n+1];
	loop(i,0,n)
	cin>>a[i];
	dp[0].ft=0;dp[0].sd=0;
	loop(i,0,n)
	{
		sum=0;
		rloop(j,i,-1)
		{
			sum+=a[j];
			if(sum>=dp[j].sd)
			{
				dp[i+1].ft=i-j+dp[j].ft;
				dp[i+1].sd=sum;
				break;
			}
		}
	}
	cout<<dp[n].ft;
}

int main()
{
	SYNC;
	//freopen("commuting.in","r",stdin);
	//freopen("commuting.out","w",stdout);
	int t=1;
	//cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
typedef long long ll;
#define MAX 5015
using namespace std;
#define INF 2147483647
#define inf 1<<30
#define MOD 1000000007
#define EPS 1e-9
#define G 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
bool debug = false;
int n,m, k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk;

int h[MAX], ss[MAX];
int d[MAX], H[MAX];
int main() {
    scanf("%d", &n);
    REP(i,n) {
        scanf("%d", h+i+1);
    }
    partial_sum(h, h+n+1, ss);
    for(int i=1;i<=n;i++) {
        for(int j=i-1;j>=0;j--) {
            if (ss[i]-ss[j] >= H[j]) {
                H[i]=ss[i]-ss[j];
                d[i] = d[j]+1;
                break;
            } else {
                d[i]=d[j];
            }
        }
    }
    printf("%d\n", n-d[n]);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
typedef long long ll;
#define MAX 5015
using namespace std;
#define INF 2147483647
#define inf 1<<30
#define MOD 1000000007
#define EPS 1e-9
#define G 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
bool debug = false;
int n,m, k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk;

int h[MAX];
int main() {
    scanf("%d", &n);
    REP(i,n) {
        scanf("%d", h+i);
    }
    ll ans = 0;
    int t = 0;
    for(int cur=h[0],i=1;i<n;i++) {
        int x = t + h[i];
        if (x>=cur) {
            cur = x; t=0;
        } else {
            t=x; ans++;
        }
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 5003
#define INF 1000000000
using namespace std;
int n;
int arr[MAXN];
vector <int> sums[MAXN];
int preffix_max[MAXN][MAXN];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
    sums[0].push_back(0);
    preffix_max[0][1] = 0;
    for (int i = 1; i <= n; i++)
    {
        int sum = 0;
        preffix_max[i][0] = -INF;
        for (int j = i; j >= 1; j--)
        {
            sum += arr[j];
            auto it = upper_bound(sums[j-1].begin(), sums[j-1].end(), sum);
            if (it == sums[j-1].begin())
                preffix_max[i][i-j+1] = max(preffix_max[i][i-j], -INF);
            else
                {
                    it--;
                    preffix_max[i][i-j+1] = max(preffix_max[i][i-j], 1 + preffix_max[j-1][it-sums[j-1].begin()+1]);
                }
            sums[i].push_back(sum);
        }
    }
    cout << n - preffix_max[n][n] << endl;
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 5003
#define INF 1000000000
using namespace std;
int n;
int arr[MAXN];
struct Node
{
    int val;
    int left, right;
    Node *left_child, *right_child;
    Node() {}
    Node(int _val, int _left, int _right, Node *_left_child, Node *_right_child)
    {
        val = _val;
        left = _left;
        right = _right;
        left_child = _left_child;
        right_child = _right_child;
    }
    void merge()
    {
        val = -INF;
        if (left_child != nullptr)
            val = max(val, left_child->val);
        if (right_child != nullptr)
            val = max(val, right_child->val);
    }
    void update(int pos, int new_val)
    {
        if (left == right)
        {
            val = new_val;
            return;
        }
        int mid = (left + right) >> 1;
        if (pos <= mid)
        {
            if (left_child == nullptr)
                left_child = new Node(0, left, mid, nullptr, nullptr);
            left_child->update(pos, new_val);
            merge();
        }
        else
        {
            if (right_child == nullptr)
            right_child = new Node(0, mid + 1, right, nullptr, nullptr);
            right_child->update(pos, new_val);
            merge();
        }
    }
    int get(int l, int r)
    {
        if (left > r || right < l)
            return -INF;
        if (left >= l && right <= r)
                return val;
        int mid = (left + right) >> 1;
        int ans = -INF;
        if (left_child != nullptr)
            ans = max(ans, left_child->get(l, r));
        if (right_child != nullptr)
            ans = max(ans, right_child->get(l, r));
        return ans;
    }
};
Node *roots[MAXN];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
    for (int i = 0; i <= n; i++)
        roots[i] = new Node(-INF, 0, INF, nullptr, nullptr);
    roots[0]->update(0, 0);
    for (int i = 1; i <= n; i++)
    {
        int sum = 0;
        for (int j = i; j >= 1; j--)
        {
            sum += arr[j];
            roots[i]->update(sum, 1 + roots[j-1]->get(0, sum));
        }
    }
    cout << n - roots[n]->get(0, INF) << endl;
}


// In the name of God

#include <bits/stdc++.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> h(n);
	for(int i = 0; i < n; i++) {
		cin >> h[i];
	}
	vector< vector<pair<int,int>> > answers(n);
	for(int i = 0; i < n; i++) {
		int sum = 0;
		for(int j = i; j >= 0; j--) {
			sum += h[j];
			if(j == 0) {
				answers[i].push_back(make_pair(sum, i-j));
				break;
			}
			if(answers[j-1].size() == 0) continue;
			int go;
			if(answers[j-1][(int)answers[j-1].size() - 1].first < sum) go = (int)answers[j-1].size() - 1;
			else {
				go = upper_bound(answers[j-1].begin(), answers[j-1].end(), make_pair(sum, (int)1e9)) - answers[j-1].begin();
				if(go == 0) continue;
				go--;
			}
			answers[i].push_back(make_pair(sum, answers[j-1][go].second + i - j));
		}
		sort(answers[i].begin(), answers[i].end());

		int mn = answers[i][0].second;
		for(int j = 1; j < answers[i].size(); j++) {
			mn = min(mn, answers[i][j].second);
			answers[i][j].second = mn;
		}
	}
	int mn = 1e9;
	for(auto x : answers[n-1]) mn=min(mn, x.second);
	cout << mn;
	return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>

using namespace std;

typedef long long li;
typedef  long double ld;
const li MAX = 5e3 + 20;

li inf = (li)1e15;
li mod = (li)1e9 + 7;
li n, dp[MAX][MAX], uk[MAX], a[MAX], pref[MAX];
void solve() {
    cin >> n;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) dp[i][j] = inf;
    }
    dp[0][0] = 0;

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

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            li ind = i - j;
            li val = pref[i] - pref[i - j];
            while (uk[ind] <= ind && pref[ind] - pref[ind - uk[ind]] <= val) uk[ind]++;
            uk[ind]--;
            dp[i][j] = min(dp[i][j], dp[ind][uk[ind]] + j - 1);
        }
        for (int j = 1; j <= i; j++) {
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        }
    }

    cout << dp[n][n];
}

int main() {
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    ios::sync_with_stdio(0);
    li q;
    q = 1;
    while (q--) solve();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int main()
{
    fastio
    int n;
    cin >> n;
    vector<int> vals(n+1);
    for(int i=1;i<=n;i++) cin >> vals[i];
    vector<int> dp(n+1), last(n+1); // last => minimum height at i for last building
    for(int i=1;i<=n;i++) {
        int j=i, sum = vals[i];
        while(j && sum < last[j-1]) sum += vals[--j];
        dp[i] = dp[j-1] + 1;
        last[i] = sum;
//        debug(dp[i], last[i]);
    }
    cout << n-dp[n];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    vector<ii> pairs;
    for(int i = 0; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            pairs.push_back({i, j});
    sort(pairs.begin(), pairs.end(), [&](ii a, ii b){
        int d1 = pre[a.second] - pre[a.first], d2 = pre[b.second] - pre[b.first];
        if(d1 != d2)
            return d1 < d2;
        return a.second > b.second;
    });
    for(auto p : pairs)
        dp[p.second] = max(dp[p.second], dp[p.first] + 1);
    cout << n - *max_element(dp, dp + n + 1) << '\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    vector<ii> pairs;
    for(int i = 0; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            pairs.push_back({i, j});
    sort(pairs.begin(), pairs.end(), [&](ii a, ii b){
        int d1 = pre[a.second] - pre[a.first], d2 = pre[b.second] - pre[b.first];
        if(d1 != d2)
            return d1 < d2;
        return a.second > b.second;
    });
    for(auto p : pairs)
        dp[p.second] = max(dp[p.second], dp[p.first] + 1);
    cout << n - dp[n] << '\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    for(int i = 0; i < MAXN; i++)
        fill(dp[i], dp[i] + MAXN, 1000000000);
    dp[0][0] = 0;
    for(int i = 0; i < n; i++) {
        for(int k = 1; k <= n; k++)
            dp[i + 1][k] = min(dp[i + 1][k], dp[i][k] + h[i + 1]);
        for(int k = (i > 0), p = i + 1; k < n; k++) {
            while(p <= n && dp[i][k] > pre[p] - pre[i])
                p++;
            if(p > n)
                break;
            dp[p][k + 1] = min(dp[p][k + 1], pre[p] - pre[i]);
        }
    }
    int a = 0;
    while(dp[n][a + 1] < 1000000000)
        a++;
    cout << n - a << '\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    for(int i = 0; i < MAXN; i++)
        fill(dp[i], dp[i] + MAXN, 1000000000);
    dp[0][0] = 0;
    for(int i = 0; i < n; i++) {
        for(int k = n - 1; k >= 0; k--)
            dp[i][k] = min(dp[i][k], dp[i][k + 1]);
        for(int k = 1; k <= n; k++)
            dp[i + 1][k] = min(dp[i + 1][k], dp[i][k] + h[i + 1]);
        for(int k = (i > 0), p = i + 1; k < n; k++) {
            while(p <= n && dp[i][k] > pre[p] - pre[i])
                p++;
            if(p > n)
                break;
            dp[p][k + 1] = min(dp[p][k + 1], pre[p] - pre[i]);
        }
    }
    int a = 0;
    while(dp[n][a + 1] < 1000000000)
        a++;
    cout << n - a << '\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    vector<ii> pairs;
    for(int i = 0; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            pairs.push_back({i, j});
    sort(pairs.begin(), pairs.end(), [&](ii a, ii b){
        int d1 = pre[a.second] - pre[a.first], d2 = pre[b.second] - pre[b.first];
        if(d1 != d2)
            return d1 < d2;
        return a.second < b.second;
    });
    fill(dp, dp + MAXN, -100000);
    dp[0] = 0;
    for(auto p : pairs)
        dp[p.second] = max(dp[p.second], dp[p.first] + 1);
    cout << n - dp[n] << '\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    vector<ii> pairs;
    for(int i = 1; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            pairs.push_back({i, j});
    sort(pairs.begin(), pairs.end(), [&](ii a, ii b){
        int d1 = a.second - a.first, d2 = b.second - b.first;
        if(d1 != d2)
            return d1 < d2;
        return a.second > b.second;
    });
    for(auto p : pairs)
        dp[p.second] = max(dp[p.second], dp[p.first] + 1);
    cout << n - *max_element(dp, dp + n + 1) << '\n';
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5005;
int h[MAXN], pre[MAXN], dp[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[i];
        pre[i] = pre[i - 1] + h[i];
    }
    vector<ii> pairs;
    for(int i = 0; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            pairs.push_back({i, j});
    sort(pairs.begin(), pairs.end(), [&](ii a, ii b){
        int d1 = pre[a.second] - pre[a.first], d2 = pre[b.second] - pre[b.first];
        if(d1 != d2)
            return d1 < d2;
        return a.second < b.second;
    });
    for(auto p : pairs)
        dp[p.second] = max(dp[p.second], dp[p.first] + 1);
    cout << n - dp[n] << '\n';
}


#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <utility>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <assert.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define endl '\n'
#define mp make_pair
#define pb push_back
#define MOD 1000000007LL
#define MOD2 1000000009LL
#define INFD 1000000000LL
#define INFL 1000000000000000000LL
#define F first
#define S second
#define eps 1e-9
#define PI 3.14159265358979
#define gcd __gcd
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define ss(x) scanf("%s", x)
#define sc(x) scanf(" %c", &x)
#define pd(x) printf("%d\n", (x))
#define plld(x) printf("%lld\n", (x))
#define loop(it, begin, end) for(ll it = begin; it <= end; it++)
#define gc getchar
#define pll pair<ll, ll>

int main() {
	// freopen("in", "r", stdin);
 	// freopen("out", "w", stdout);
 	ll n; cin >> n;
 	if(n == 1) {
 		cout << 0 << endl;
 		return 0;
 	}
 	ll h0; cin >> h0;
 	// cout << h0 << " ";
 	ll h1 = 0;
 	ll towers = 1, in = 1;
 	while(in < n) {
 		while(h1 < h0 && in < n) {
 			ll h; cin >> h; in++;
 			h1 += h;
 		}
		if(h1 >= h0) {
			h0 = h1; h1 = 0; towers++;
			// cout << h0 << " ";
		} else {
			// cout << "(" << h1 << ")" << " ";
			h0 += h1; h1 = 0;
		}
 	}
 	// cout << endl;
 	cout << towers << endl;
}


#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <utility>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <assert.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define endl '\n'
#define mp make_pair
#define pb push_back
#define MOD 1000000007LL
#define MOD2 1000000009LL
#define INFD 1000000000LL
#define INFL 1000000000000000000LL
#define F first
#define S second
#define eps 1e-9
#define PI 3.14159265358979
#define gcd __gcd
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define ss(x) scanf("%s", x)
#define sc(x) scanf(" %c", &x)
#define pd(x) printf("%d\n", (x))
#define plld(x) printf("%lld\n", (x))
#define loop(it, begin, end) for(ll it = begin; it <= end; it++)
#define gc getchar
#define pll pair<ll, ll>

int main() {
	// freopen("in", "r", stdin);
 	// freopen("out", "w", stdout);
 	ll n; cin >> n;
 	if(n == 1) {
 		cout << 0 << endl;
 		return 0;
 	}
 	ll h0; cin >> h0;
 	// cout << h0 << " ";
 	ll h1 = 0;
 	ll towers = 1, in = 1;
 	while(in < n) {
 		while(h1 < h0 && in < n) {
 			ll h; cin >> h; in++;
 			h1 += h;
 		}
		if(h1 >= h0) {
			h0 = h1; h1 = 0; towers++;
			// cout << h0 << " ";
		} else {
			// cout << "(" << h1 << ")" << " ";
			h0 += h1; h1 = 0;
		}
 	}
 	// cout << endl;
 	cout << n - towers << endl;
}


#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <utility>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <assert.h>
using namespace std;

#define ll int
#define ull unsigned long long
#define endl '\n'
#define mp make_pair
#define pb push_backy
#define MOD 1000000007LL
#define MOD2 1000000009LL
#define INFD 1000000000LL
#define INFL 1000000000000000000LL
#define F first
#define S second
#define eps 1e-9
#define PI 3.14159265358979
#define gcd __gcd
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define ss(x) scanf("%s", x)
#define sc(x) scanf(" %c", &x)
#define pd(x) printf("%d\n", (x))
#define plld(x) printf("%lld\n", (x))
#define loop(it, begin, end) for(ll it = begin; it <= end; it++)
#define gc getchar
#define pll pair<ll, ll>

const ll N = 5e3;
ll n;
ll S[N + 1], dp[N + 1][N + 1];

ll sum(ll l, ll r) {
	assert(1 <= l && l <= r && r <= n);
	return S[r] - S[l - 1];
}

int main() {
	// freopen("in", "r", stdin);
	// freopen("out", "w", stdout);
 	cin >> n;
 	S[0] = 0;
 	loop(i, 1, n) {
 		cin >> S[i];
 		S[i] += S[i - 1];
 	}
 	loop(i, 1, n) {
 		dp[1][i] = 1;
 		ll k = 1;
 		loop(j, 2, i) {
 			while((k <= j - 1) && (sum(k, j - 1) > sum(j, i))) {
 				k++;
 			}
 			if(k >= j) {
 				dp[j][i] = 0;
 			} else {
 				dp[j][i] = ((dp[k][j - 1] == 0) ? 0 : (1 + dp[k][j - 1]));
 			}
 		}
 		for(ll j = i - 1; j >= 1; j--) {
 			dp[j][i] = max(dp[j + 1][i], dp[j][i]);
 		}
 	}
 	cout << n - dp[1][n] << endl;
}


#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <utility>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <assert.h>
using namespace std;

#define ll int
#define ull unsigned long long
#define endl '\n'
#define mp make_pair
#define pb push_back
#define MOD 1000000007LL
#define MOD2 1000000009LL
#define INFD 1000000000LL
#define INFL 1000000000000000000LL
#define F first
#define S second
#define eps 1e-9
#define PI 3.14159265358979
#define gcd __gcd
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define ss(x) scanf("%s", x)
#define sc(x) scanf(" %c", &x)
#define pd(x) printf("%d\n", (x))
#define plld(x) printf("%lld\n", (x))
#define loop(it, begin, end) for(ll it = begin; it <= end; it++)
#define gc getchar
#define pll pair<ll, ll>

const ll N = 5e3;
ll n;
ll S[N + 1], K[N + 1][N + 1], dp[N + 1][N + 1], T[N + 1][N + 1];

ll sum(ll l, ll r) {
	assert(1 <= l && l <= r && r <= n);
	return S[r] - S[l - 1];
}

int main() {
	// freopen("in", "r", stdin);
 	// freopen("out", "w", stdout);
 	cin >> n;
 	S[0] = 0;
 	loop(i, 1, n) {
 		cin >> S[i];
 		S[i] += S[i - 1];
 	}
 	loop(i, 1, n) {
 		ll k = 1;
 		loop(j, 2, i) {
 			while((k <= j - 1) && (sum(k, j - 1) > sum(j, i))) {
 				k++;
 			}
 			K[i][j] = k;
 			// if(K[i][j] == j) {
 			// 	cout << "X\t";
 			// } else {
 			// 	cout << K[i][j] << "\t";
 			// }
 		}
 		// cout << endl;
 	}
 	// cout << endl;
 	loop(i, 1, n) {
 		dp[i][1] = 1;
 		// cout << dp[i][1] << "\t";
 		loop(j, 2, i) {
 			ll k0 = K[i][j];
 			if(k0 >= j) {
 				dp[i][j] = 0;
 			} else {
 				dp[i][j] = ((T[j - 1][k0] == 0) ? 0 : (1 + T[j - 1][k0]));
 			}
 			// cout << dp[i][j] << "\t";
 		}
 		// cout << endl;
 		T[i][i] = dp[i][i];
 		for(ll j = i - 1; j >= 1; j--) {
 			T[i][j] = max(T[i][j + 1], dp[i][j]);
 		}
 	}
 	// cout << endl;
 	ll ans = 0;
 	loop(j, 1, n) {
 		ans = max(ans, dp[n][j]);
 	}
 	cout << n - ans << endl;
}


#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <utility>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <assert.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define endl '\n'
#define mp make_pair
#define pb push_back
#define MOD 1000000007LL
#define MOD2 1000000009LL
#define INFD 1000000000LL
#define INFL 1000000000000000000LL
#define F first
#define S second
#define eps 1e-9
#define PI 3.14159265358979
#define gcd __gcd
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define ss(x) scanf("%s", x)
#define sc(x) scanf(" %c", &x)
#define pd(x) printf("%d\n", (x))
#define plld(x) printf("%lld\n", (x))
#define loop(it, begin, end) for(ll it = begin; it <= end; it++)
#define gc getchar
#define pll pair<ll, ll>

const ll N = 5e3;
ll n;
ll S[N + 1], K[N + 1][N + 1], dp[N + 1][N + 1], T[N + 1][N + 1];

ll sum(ll l, ll r) {
	assert(1 <= l && l <= r && r <= n);
	return S[r] - S[l - 1];
}

int main() {
	// freopen("in", "r", stdin);
 	// freopen("out", "w", stdout);
 	cin >> n;
 	S[0] = 0;
 	loop(i, 1, n) {
 		cin >> S[i];
 		S[i] += S[i - 1];
 	}
 	loop(i, 1, n) {
 		ll k = 1;
 		loop(j, 2, i) {
 			while((k <= j - 1) && (sum(k, j - 1) > sum(j, i))) {
 				k++;
 			}
 			K[i][j] = k;
 			// if(K[i][j] == j) {
 			// 	cout << "X\t";
 			// } else {
 			// 	cout << K[i][j] << "\t";
 			// }
 		}
 		// cout << endl;
 	}
 	// cout << endl;
 	loop(i, 1, n) {
 		dp[i][1] = 1;
 		// cout << dp[i][1] << "\t";
 		loop(j, 2, i) {
 			ll k0 = K[i][j];
 			if(k0 >= j) {
 				dp[i][j] = 0;
 			} else {
 				dp[i][j] = ((T[j - 1][k0] == 0) ? 0 : (1 + T[j - 1][k0]));
 			}
 			// cout << dp[i][j] << "\t";
 		}
 		// cout << endl;
 		T[i][i] = dp[i][i];
 		for(ll j = i - 1; j >= 1; j--) {
 			T[i][j] = max(T[i][j + 1], dp[i][j]);
 		}
 	}
 	// cout << endl;
 	ll ans = 0;
 	loop(j, 1, n) {
 		ans = max(ans, dp[n][j]);
 	}
 	cout << n - ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define MP make_pair
#define F first
#define S second
#define sz(x) (ll)x.size()
#define G(a,b) get<a>(b)

const ll N=5005;
ll arr[N];

void solve()
{
  ll n;
  cin>>n;
  f(i,1,n+1) cin>>arr[i];
  arr[n+1]=1e10;
  ll ans=0,prev=arr[1];
  f(i,2,n+1)
  {
	  ll temp=arr[i],j=i;
	  while(temp<prev)
	  {
		  j++;
		  ans++;
		  temp+=arr[j];
	  }
	  i=j;
	  prev=temp;
  }

  cout<<ans<<endl;
}

int32_t main()
{
    ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll TestCase=1;
    //cin>>TestCase;

    while(TestCase--)
    {
      solve();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(ll i=(b-1);i>=a;i--)
#define MP make_pair
#define F first
#define S second
#define sz(x) (ll)x.size()
#define G(a,b) get<a>(b)
#define infl LONG_LONG_MAX

const ll N=5005;
ll h[N],dp[N][N],pre[N],mn[N];

void solve()
{
  ll n;
  cin>>n;
  f(i,1,n+1) cin>>h[i];

  f(i,1,n+1) pre[i]=pre[i-1]+h[i];

  fi(j,1,n+1)
  {
	  fi(i,1,j+1)
	  {
		  if(j==n)
		  {
			  dp[i][j]=n-i;
			  continue;
		  }

		  if(pre[j]-pre[i-1]>pre[n]-pre[j])
		  {
			  dp[i][j]=infl;
			  continue;
		  }

		  ll idx=lower_bound(pre+j+1,pre+n+1,2*pre[j]-pre[i-1])-pre;
		  if(mn[idx]!=infl) dp[i][j]=j-i+mn[idx];
		  else dp[i][j]=infl;
	  }

	  ll temp=infl;
	  fi(w,j,n+1)
	  {
		  if(dp[j][w]!=infl) temp=min(temp,dp[j][w]);
		  mn[w]=temp;
	  }
  }

  ll ans=infl;
  f(j,1,n+1) ans=min(ans,dp[1][j]);
  cout<<ans<<endl;
}

int32_t main()
{
    ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll TestCase=1;
    //cin>>TestCase;

    while(TestCase--)
    {
      solve();
    }

    return 0;
}


#include<bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ba back
#define ppb pop_back
#define eb emplace_back

#define sz(x) (int((x.size())))
#define all(x) (x).begin(),(x).end()

#define db(x)        cerr << #x << " = " << x << endl
#define db2(x, y)   {cerr << #x << " = " << x << "\t";  db(y);}
#define db3(x, y, z){cerr << #x << " = " << x << "\t";  db2(y,z);}

#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%I64d", &a)
#define sll(a, b) scanf("%I64d%I64d", &a, &b)
#define slll(a, b, c) scanf("%I64d%I64d%I64d", &a, &b, &c)

#define bye exit(0)
#define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL)

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

const ll MOD = 1e9 + 7;

ll add(ll x, ll y, ll CMOD=MOD){return (x+y+CMOD+CMOD)%CMOD;}
ll mult(ll x, ll y, ll CMOD=MOD){return add(x, 0)*add(y, 0) % CMOD;}

ll fast_expo(ll x, ll y, ll CMOD=MOD){
    if(x==0) return 0;
    if(y==0) return 1;
    ll ans = fast_expo(x, y/2, CMOD);
    ans = mult(ans, ans, CMOD);
    if(y&1) ans = mult(ans, x, CMOD);
    return ans;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll gen(ll x, ll y){
    if(x==y) return x;
    return x + rng()%(y-x+1);
}

const int TAM = 1e5 + 100;
const ll INF = LLONG_MAX/4;

int a[TAM];
int dp[TAM];
int sum[TAM];
int n;

int main(){
    si(n);
    for(int i=1; i<=n; i++) si(a[i]);

    for(int i=1; i<=n; i++){
        int acum = a[i];
        for(int j=i-1; j>=0; j--){
            if( acum >= sum[j] ){
                dp[i] = dp[j]+1;
                sum[i] = acum;
                break;
            }
            acum += a[j];
        }
    }
    //for(int i=1; i<=n; i++) db2(dp[i], sum[i]);
    cout << n-dp[n] << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
#define MAXN 1000007
#define edl '\n'
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define all(a) a.begin(), a.end()
using namespace std;

vector < unordered_map <int, int> > dp(5001);

int minimalist(int idx, vector <int> &A, int atleast)
{
	int n = A.size();
	if(idx >= n)return 0;
	//cout<<idx<<" idx "<<edl;
	if(dp[idx].find(atleast) != dp[idx].end())return dp[idx][atleast];
	int way1 = MAXN, way2 = MAXN;
	int sum = A[idx], i = idx + 1;
	//cout<<A[idx]<<" 1 "<<edl;
	int cnt = 0;
	while(i < n)
	{
		if(sum >= atleast) way1 = min(way1, cnt + minimalist(i, A, sum));
		sum += A[i];
		i++;
		cnt++;
	}
	//cout<<sum<<" sum 3 "<<edl;
	int way3 = n-idx;
	if(sum >= atleast)way2 = cnt;
	dp[idx][atleast] = min(way1, min(way2, way3));
	//cout<<dp[idx][atleast]<<" dj "<<edl;
	return dp[idx][atleast];
}

void solve()
{
	int n;
	cin>>n;
	vector <int> A(n);
	for(int i = 0; i < n; i++)
	{
		cin>>A[i];
	}
	int res = minimalist(0, A, 0);
	cout<<res<<edl;
	return;
}

int main()
{
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
#define MAXN 1000007
#define edl '\n'
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define all(a) a.begin(), a.end()
using namespace std;

//vector < unordered_map <int, int> > dp(5001);
vector < vector <int> > dp(5001, vector <int> (100001,-1));
int minimalist(int idx, vector <int> &A, int atleast)
{
	int n = A.size();
	if(idx >= n)return 0;
	//cout<<idx<<" idx "<<edl;
	if(dp[idx][atleast] != -1)return dp[idx][atleast];
	int way1 = MAXN, way2 = MAXN;
	int sum = A[idx], i = idx + 1;
	//cout<<A[idx]<<" 1 "<<edl;
	int cnt = 0;
	while(i < n)
	{
		if(sum >= atleast) way1 = min(way1, cnt + minimalist(i, A, sum));
		sum += A[i];
		i++;
		cnt++;
	}
	//cout<<sum<<" sum 3 "<<edl;
	int way3 = n-idx;
	if(sum >= atleast)way2 = cnt;
	dp[idx][atleast] = min(way1, min(way2, way3));
	//cout<<dp[idx][atleast]<<" dj "<<edl;
	return dp[idx][atleast];
}

void solve()
{
	int n;
	cin>>n;
	vector <int> A(n);
	for(int i = 0; i < n; i++)
	{
		cin>>A[i];
	}
	int res = minimalist(0, A, 0);
	cout<<res<<edl;
	return;
}

int main()
{
	solve();
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#define int long long int
using namespace std;
//OMG I GOT THE PROOF:
/*Lets say that for aj, merges upto ai and at are valid. then if i<t, (which means
sum[t]<sum[i]). It is now enough to prove that number of moves in t is less than i.
Which is constand+d vs constand+d-1. PROVED*/
//MY SOLUTION IS EVEN BETTER THAN THE EDITORIALS HAHAHAHAHAHA
//AUTHOR: LEGENDARY GRANDMASTER mshiladityam
int solve()
{
	int n; cin>>n;
	vector<int> v(n);
	vector<int> pref(n, 0);
	vector<pair<int, int>> dp(n);
	for (int j=0; j<n; j++)
	{
		cin>>v[j];
		if (j==0)
		{
			pref[j]=v[j];
		}
		else
		{
			pref[j]=v[j]+pref[j-1];
		}
	}

	dp[0]={0, v[0]};
	for (int j=1; j<n; j++)
	{
		int s=pref[j];
		dp[j]={j, s};
		for (int i=0; i<j; i++)
		{
			s-=v[i];
			if (dp[i].second<=s)
			{
				if (dp[i].first+j-i-1<dp[j].first)
				{
					dp[j].first=dp[i].first+j-i-1;
					dp[j].second=s;
				}
				else if (dp[i].first+j-i-1==dp[j].first)
				{
					dp[j].second=s;
				}
			}
		}
	}
	cout<<dp[n-1].first;
	return 0;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
	int t;
	//cin >> t;
	t=1;
	while (t--)
	{
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
#define MAXN 1000007
#define edl '\n'
#define IOFAST ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define all(a) a.begin(), a.end()
using namespace std;

vector < unordered_map <int, int> > dp(5001);

int minimalist(int idx, vector <int> &A, int atleast)
{
	int n = A.size();
	if(idx >= n)return 0;
	//cout<<idx<<" idx "<<edl;
	if(dp[idx].find(atleast) != dp[idx].end())return dp[idx][atleast];
	int way1 = MAXN, way2 = MAXN;
	int sum = A[idx], i = idx + 1;
	//cout<<A[idx]<<" 1 "<<edl;
	int cnt = 0;
	while(i < n && sum < atleast)
	{
		sum += A[i];
		i++;
		cnt++;
	}
	//cout<<sum<<" sum 2 "<<edl;
	if(sum >= atleast) way1 = cnt + minimalist(i, A, sum);
	while(i < n)
	{
		sum += A[i];
		i++;
		cnt++;
	}
	//cout<<sum<<" sum 3 "<<edl;
	int way3 = n-idx;
	if(sum >= atleast)way2 = cnt;
	dp[idx][atleast] = min(way1, min(way2, way3));
	//cout<<dp[idx][atleast]<<" dj "<<edl;
	return dp[idx][atleast];
}

void solve()
{
	int n;
	cin>>n;
	vector <int> A(n);
	for(int i = 0; i < n; i++)
	{
		cin>>A[i];
	}
	int res = minimalist(0, A, 0);
	cout<<res<<edl;
	return;
}

int main()
{
	solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=5005;

int n;
int a[N], pref[N], cache[N][N];

int dp(int idx, int prev)
{
	if(idx==n+1)
		return 0;
	int &ans=cache[idx][prev];
	if(ans!=-1)
		return ans;
	ans=1 + dp(idx+1, prev+1);
	int prevsum=pref[idx-1] - pref[idx-prev-1];
	auto it=lower_bound(pref+idx, pref+n+2, pref[idx-1] + prevsum) - pref;
	if(it!=n+1)
		ans=min(ans, it-idx + dp(it+1, it-idx+1));
	return ans;
}

int32_t main()
{
	IOS;
	memset(cache, -1, sizeof(cache));
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		pref[i]=pref[i-1] + a[i];
	}
	pref[n+1]=1e18;
	int ans=dp(1, 0);
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=5005;

int n, ans=0;
int a[N];
vector<int> v;

int32_t main()
{
	IOS;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	v.push_back(a[1]);
	int cur=0, len=0;
	for(int i=2;i<=n;i++)
	{
		cur+=a[i];
		len++;
		if(cur>=v.back())
		{
			ans+=len-1;
			v.push_back(cur);
			len=0;
			cur=0;
		}
	}
	if(cur>0)
		ans+=len;
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

// primes - 31 53 (1-3)67 1(0-1)87 125(6-8)87 3256(0-9)7
// primes - 569(6-8)09 1299709 2750159 154858(57-63-67) 998244353

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((int)(x).size())
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

using   lli     =   long long;
using   ulli    =   unsigned long long int;
using   pll     =   pair<lli, lli>;
using   ttt     =   pair<lli, pll>;
using   vttt    =   vector<ttt>;
using   vll     =   vector<pll>;
using   vl      =   vector<lli>;
using   vi      =   vector<int>;
using   vvi     =   vector<vector<int>>;
using   cd      =   complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1); cout<<endl;
}
template<typename Arg1>
void __t(const char *name, Arg1 &&arg1)
{ cout<<name<<" : { "; for (auto p : arg1) __p(p); cout<<"}"<<endl; }
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	cout.write(names,i)<<" : ";
	__p(arg1);  cout<<"| ";
	__f(names+i+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
#define cotra(...) { cout<<"Line:"<<__LINE__<<" | "; __t(#__VA_ARGS__, __VA_ARGS__); }
#else
#define trace(...) {}
#define cotra(...) {}
#define endl '\n'
#endif

#define MAXN 5002

vvi dp(MAXN,vi(MAXN,mod));

int tejas_919(int kkkk)
{
    lli n, m, k, q, u, v, temp=0, ans=0;
    cin>>n;
    int arr[n+5];
    fol(i,1,n+1) cin>>arr[i];
    dp[0][0]=0;
    fol(i,0,n)
    {
        deque<pair<int,int>> de;
        fol(j,0,MAXN)
        {
            if(dp[i][j]==mod) continue;
            while(!de.empty()&&de.back().ff>=dp[i][j]) de.ppb;
            de.pb({dp[i][j],j});
        }
        int sum=accumulate(arr+i+1,arr+n+1,0);
        loop(k,n,i+1)
        {
            while(!de.empty()&&de.back().ff>sum) de.ppb;
            if(de.empty()) break;
            dp[k][de.back().ss+1]=min(dp[k][de.back().ss+1],sum);
            sum-=arr[k];
        }
        fol(i,1,n+1) cotra(dp[i]) cout<<endl;
    }
    loop(j,MAXN-1,0) if(dp[n][j]!=mod) return cout<<n-j<<endl, 0;
    return 0;
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
}


#include <iostream>
#include <vector>

#define int int64_t

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> dp(n + 1, 1e16);
    vector<int> olddp(n + 1, 1e16);
    dp[0] = 1;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) cin >> vs[i];

    vector<int> vr(n);

    for (int i = 0; i < n; i++)
    {
        swap(dp, olddp);
        int l = 0;
        int ssum = 0;
        dp.assign(n + 1, 1e16);

        for (int j = 1; j <= n; j++)
        {
            ssum += vs[j - 1];
            while (ssum - vs[l] >= olddp[l + 1] && l < j)
            {
                ssum -= vs[l];
                l++;
            }
            if (ssum >= olddp[l]) dp[j] = ssum;
        }
        vr[i] = dp[n];
        /*
        for (auto i : dp) cout << i << " ";
        cout << "\n";*/
    }
    int res = n;
    while (vr[n - res] < 1e15) res--;
    cout << res << "\n";
}


#include <iostream>
#include <vector>

#define int int64_t

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> dp(n + 1, 1e16);
    vector<int> olddp(n + 1, 1e16);
    dp[0] = 1;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) cin >> vs[i];

    vector<int> vr(n);

    for (int i = 0; i < n; i++)
    {
        swap(dp, olddp);
        int l = 0;
        int ssum = 0;
        dp.assign(n + 1, 1e16);

        for (int j = 1; j <= n; j++)
        {
            ssum += vs[j - 1];
            while (ssum - vs[l] >= olddp[l + 1] && l < j)
            {
                ssum -= vs[l];
                l++;
            }
            if (ssum >= olddp[l]) dp[j] = ssum;
        }
        vr[i] = dp[n];
        /*
        for (auto i : dp) cout << i << " ";
        cout << "\n";*/
    }
    int res = n;
    while (vr[n - res] < 1e15 && res > 0) res--;
    cout << res << "\n";
}


#include <iostream>
#include <vector>

#define int int64_t

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> dp(n + 1, 1e16);
    vector<int> olddp(n + 1, 1e16);
    dp[0] = 1;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) cin >> vs[i];

    vector<int> vr(n);

    for (int i = 0; i < n; i++)
    {
        swap(dp, olddp);
        int l = 0;
        int ssum = 0;
        dp.assign(n + 1, 1e16);

        for (int j = 1; j <= n; j++)
        {
            if (i != 0 && olddp[j - 1] > 1e15)
            {
                l++;
                continue;
            }
            ssum += vs[j - 1];
            while (ssum - vs[l] >= olddp[l + 1] && l < j)
            {
                ssum -= vs[l];
                l++;
            }
            if (ssum >= olddp[l]) dp[j] = ssum;
        }
        vr[i] = dp[n];
        /*
        for (auto i : dp) cout << i << " ";
        cout << "\n"; */
    }
    int res = n;
    while (vr[n - res] < 1e15 && res > 0) res--;
    cout << res << "\n";
}


#include <iostream>
#include <vector>

#define int int64_t

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> dp(n + 1, 1e16);
    vector<int> olddp(n + 1, 1e16);
    dp[0] = 1;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) cin >> vs[i];

    vector<int> vr(n);

    for (int i = 0; i < n; i++)
    {
        swap(dp, olddp);
        int l = 0;
        int ssum = 0;
        dp.assign(n + 1, 1e16);

        for (int j = 1; j <= n; j++)
        {
            ssum += vs[j - 1];
            while (ssum - vs[l] >= olddp[l + 1] && l < j)
            {
                ssum -= vs[l];
                l++;
            }
            if (ssum >= olddp[l]) dp[j] = ssum;
        }
        vr[i] = dp[n];
        /*
        for (auto i : dp) cout << i << " ";
        cout << "\n";*/
    }
    int res = n;
    while (vr[n - res] < 1e15 && res >= 0) res--;
    cout << res << "\n";
}


#include <iostream>
#include <vector>
#include <algorithm>

#define int int64_t

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> vs(n);
    for (int i = 0; i < n; i++) cin >> vs[i];

    vector<int> dp(n + 1);
    vector<int> minv(n + 1);

    for (int i = 1; i <= n; i++)
    {
        int ssum = vs[i - 1];
        int l = i - 1;

        for (; l > 0 && minv[l] > ssum; l--) ssum += vs[l - 1];

        dp[i] = dp[l] + i - l - 1;
        minv[i] = ssum;
    }
    cout << dp[n] << "\n";
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5007;

int n, a[MAXN], prefix[MAXN];
array<int, 2> dp[MAXN];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n;

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

	for(int i = 1; i <= n; ++i){
		for(int j = 0; j < i; ++j){
			int groups = dp[j][0] + 1, sum = prefix[i] - prefix[j];
			if(sum >= dp[j][1]){
				if(groups > dp[i][0]){
					dp[i] = {groups, sum};
				}
				else if(dp[i][0] == groups  && sum < dp[i][1]){
					dp[i][1] = sum;
				}
			}
		}
	}

	cout << n - dp[n][0] << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5000;
const int INF = 1e9;

int N;
int H[MAXN + 10], sum[MAXN + 10];
int dp[MAXN + 10][MAXN + 10];

int main() {
   scanf("%d", &N);
   for(int i = 1; i <= N; i++) {
      scanf("%d", &H[i]);
   }
   partial_sum(H+1, H+N+1, sum+1);
   for(int i = N; i >= 1; i--) {
      int l = N+1;
      int mn = INF;
      for(int j = i-1; j >= 1; j--) {
         while(l > 0 && sum[l-1] >= 2 * sum[i-1] - sum[j-1]) {
            l--;
            mn = min(mn, l + dp[l+1][i]);
         }
         dp[i][j] = min(INF, -i + mn);
      }
   }
   int res = N-1;
   for(int r = 1; r < N; r++) {
      res = min(res, r-1 + dp[r+1][1]);
   }
   printf("%d\n", res);
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5000;
const int INF = 1e9;

int N;
int H[MAXN + 10], sum[MAXN + 10];
int dp[MAXN + 10][MAXN + 10];

int main() {
   scanf("%d", &N);
   for(int i = 1; i <= N; i++) {
      scanf("%d", &H[i]);
   }
   partial_sum(H+1, H+N+1, sum+1);
   for(int i = N; i >= 1; i--) {
      int l = N+1;
      int mn = INF;
      for(int j = 1; j < i; j++) {
         while(l > i && sum[l-1] >= 2 * sum[i-1] - sum[j-1]) {
            l--;
            mn = min(mn, l + dp[l+1][i]);
         }
         dp[i][j] = min(INF, -i + mn);
      }
   }
   int res = N-1;
   for(int r = 1; r < N; r++) {
      res = min(res, r-1 + dp[r+1][1]);
   }
   printf("%d\n", res);
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5000;
const int INF = 1e9;

int N;
int H[MAXN + 10], sum[MAXN + 10];
int dp[MAXN + 10][MAXN + 10];

int main() {
   scanf("%d", &N);
   for(int i = 1; i <= N; i++) {
      scanf("%d", &H[i]);
   }
   partial_sum(H+1, H+N+1, sum+1);
   for(int i = N; i >= 1; i--) {
      int l = N+1;
      int mn = INF;
      for(int j = i-1; j >= 1; j--) {
         while(l > 0 && sum[l-1] >= 2 * sum[i-1] - sum[j-1]) {
            l--;
            mn = min(mn, l + dp[l+1][i]);
         }
         dp[i][j] = min(INF, -i + mn);
      }
   }
   int res = N-1;
   for(int r = 1; r < N; r++) {
      res = min(res, r-1 + dp[r+1][r]);
   }
   printf("%d\n", res);
}


#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
#define Get(x,y) scanf("%I64d%I64d",&x,&y);
#define get(x) scanf("%I64d",&x);
#define f(i,a,b) for(ll i=a;i<b;i++)
#define f_(i,a,b) for(ll i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define maxm(a,b) a=max(a,b);
#define minm(a,b) a=min(a,b);
const int N=5009;
ll dp[N][N],a[N],n,l,r,mid,p,mx;
ll binary_s(ll b,ll x){
	l=0;r=N;
	if(dp[b][0]>x)
		return -1;
	while(l+1<r){
		mid=(l+r)/2;
		if(dp[b][mid]<=x)
			l=mid;
		else
			r=mid;
	}
	return l;
}
int main(){
	cin>>n;
	f(i,1,n+1){cin>>a[i];a[i]+=a[i-1];}
	f(i,0,N){f(j,0,N) dp[i][j]=1e9;}
	dp[0][0]=0;
	f(i,1,n+1){
		f(j,0,i){
			p=binary_s(j,a[i]-a[j]);
			if(p>=0)
				minm(dp[i][p+1],a[i]-a[j]);
		}
		f_(j,i,0){
			minm(dp[i][j],dp[i][j+1]);
			if(i==n && dp[i][j]<1e9)
				maxm(mx,j);
		}
	}
	cout<<n-mx;
}


#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
#define f(i,a,b) for(ll i=a;i<b;i++)
#define f_(i,a,b) for(ll i=a;i>=b;i--)
#define ll long long
#define pb push_back
ll n,p[5009],salam=1e9;
int solve(int l,int mn){
	int x=-1,ans=0;
	f(i,l,n+1){
		if(p[i]-p[l-1]>=mn){
			x=i;
			break;}}
	if(x==-1) return n-l+1;
	return (x-l)+solve(x+1,p[x]-p[l-1]);
}
int main(){
	cin>>n;
	f(i,1,n+1){cin>>p[i];p[i]+=p[i-1];}
	f(i,1,n+1) salam=min(salam,solve(i+1,p[i])+i-1);
	cout<<salam;
}


#include <bits/stdc++.h>
using namespace std;

int n, a, b[5003], s[5003], p[5003];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> a;
		b[i] = b[i - 1] + a;
	}
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= i; ++j) {
			if(b[i] - b[j - 1] >= s[j - 1]) {
				s[i] = b[i] - b[j - 1];
				p[i] = p[j - 1] + i - j;
			}
		}
	}
	cout << p[n] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, h, ph, nh, fp;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> h;
        if(nh) ++fp;
        nh += h;
        if(nh >= ph) {
            ph = nh;
            nh = 0;
        }
    }
    if(nh < ph) ++fp;
    cout << fp << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, h, ph, nh, fp;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> h;
        if(nh) ++fp;
        nh += h;
        if(nh >= ph) {
            ph = nh;
            nh = 0;
        }
    }
    if(nh < ph && nh) ++fp;
    cout << fp << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

long long a[5002],h[5002];
int n;
long long sum;
int f[5002];
int main()
{
   cin >> n;
   for(int i=1;i<=n;i++)
      cin >> a[i];
   for(int i=1;i<=n;i++){
      sum = 0;
      for(int j=i;j>=0;j--){
         sum += a[j];
         if (sum>=h[j-1]){
            f[i]=f[j-1]+(i-j);
            h[i]=sum;
            break;
         }
      }
   }
   cout << min(f[n],n-1) << endl;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAXN  100001
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less<int>, rb_tree_tag,
 tree_order_statistics_node_update>
 ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x ;//% p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
int pre[5009]={0},dp[5009],height[5009];
void solve()
{
    int n;
    cin>>n;
    int a[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        pre[i]+=pre[i-1]+a[i];
        height[i]=1e18;
        dp[i]=1e18;
    }
    for(int i=1;i<=n;i++)
    {
        int j=i;
        for(j;j>=1;j--)
        {
            if(height[j-1]<=pre[i]-pre[j-1])
            {
                if(dp[j-1]+i-j<dp[i])
                {
                    dp[i]=dp[j-1]+i-j;
                    height[i]=pre[i]-pre[j-1];
                }
                else if(dp[j-1]+i-j==dp[i])
                {
                    height[i]=min(pre[i]-pre[j-1],height[i]);
                }
            }
        }
    }
    cout<<dp[n]<<endl;
}
signed main()
{
    fast
    int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll maxn = 5e3+10;
const ll ZERO = 0;
const ll SADAT = 7.84e-17;
const ll INF = 1e10;
const ll mod = 1e9+7;

#define endl '\n'
#define dokme(x) cout << x ;  return(0);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_init freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);

int n , a[maxn] , sum[maxn] , dp[maxn] , sml[maxn];

int main(){
	cin >> n;
	for (int i = 0 ; i< n ; i ++){

		cin >> a[i];
	}
	sum[0] = 0;
	for (int i = 1 ; i <= n; i ++){
		sum[i] = sum[i-1] + a[i-1];
	}
	dp[0] = 0 ;
	sml[0] = 0;
	for (int i = 1 ; i <= n; i ++){
		dp[i] = 1;
		sml[i] = sum[i];
		for (int j = 0 ; j  < i ; j ++){
			if (sum[i] - sum[j] >= sml[j] and dp[i] <= dp[j] + 1){
				dp[i] = dp[j] + 1;
				sml[i] = sum[i] - sum[j];

			}

		}
	}

	cout << n - dp[n];

	return(0);
}

//When we hit our lowest point, we are open to the greatest change.
//If I try, I fail. If I don't try, I'm NEVER GOING TO GET IT.
//Sometimes, life is like this dark tunnel. You can't always see the light at the end of the tunnel. But if you just keep moving... you will come to a better place.
//It is important to draw wisdom from diffrent places. If you take it from only one place it becomes rigid and stale.


/*
 * Author - Abhas Jain
 * Code is inside the solve function
 */

#include <bits/stdc++.h>

#define F first
#define S second
#define all(c) c.begin(), c.end()
#define endl '\n'
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;
mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ld PI = 3.141592653589793;
const ll INF = 9223372036854775807ll;
const ll mod = 1e9 + 7;
const int N = 5e3 + 1;

int dp[N][N];

void solve()
{
	int n;
	cin >> n;
	int big = 1e9;
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			dp[i][j] = big;
		}
	}
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	dp[0][0] = 0;
	for (int i = 0; i <= n; ++i) {
		int ptr = 0;
		int sum = 0;
		for (int j = n - 1; j >= 0; --j) {
			dp[i][j] = min(dp[i][j], dp[i][j + 1]);
		}
		for (int j = i + 1; j <= n; ++j) {
			sum += a[j];
			while (dp[i][ptr] <= sum) {
				ptr++;
			}
			dp[j][ptr] = min(dp[j][ptr], sum);
		}
	}
	for (int i = n; i >= 0; --i) {
		if (dp[n][i] != big) {
			cout << (n - i) << endl;
			break;
		}
	}
}

int main( )
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    solve();
    return 0;
}


/*
 * Author - Abhas Jain
 * Code is inside the solve function
 */

#include <bits/stdc++.h>

#define F first
#define S second
#define all(c) c.begin(), c.end()
#define endl '\n'
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;
mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ld PI = 3.141592653589793;
const ll INF = 9223372036854775807ll;
const ll mod = 1e9 + 7;
const int N = 5e3 + 1;

int dp[N][N];

void solve()
{
	int n;
	cin >> n;
	int big = 1e9;
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			dp[i][j] = big;
		}
	}
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	dp[0][0] = 0;
	for (int i = 0; i <= n; ++i) {
		int ptr = 0;
		int sum = 0;
		for (int j = n - 2; j >= 0; --j) {
			dp[i][j] = min(dp[i][j], dp[i][j + 1]);
		}
		for (int j = i + 1; j <= n; ++j) {
			sum += a[j];
			while (ptr < n && dp[i][ptr] <= sum) {
				ptr++;
			}
			dp[j][ptr] = min(dp[j][ptr], sum);
		}
	}
	for (int i = n; i >= 0; --i) {
		if (dp[n][i] != big) {
			cout << (n - i) << endl;
			break;
		}
	}
}

int main( )
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    solve();
    return 0;
}


/*
 * Author - Abhas Jain
 * Code is inside the solve function
 */

#include <bits/stdc++.h>

#define F first
#define S second
#define all(c) c.begin(), c.end()
#define endl '\n'
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;
mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ld PI = 3.141592653589793;
const ll INF = 9223372036854775807ll;
const ll mod = 1e9 + 7;
const int N = 5e3 + 1;

int dp[N][N];

void solve()
{
	int n;
	cin >> n;
	int big = 1e9;
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			dp[i][j] = big;
		}
	}
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	dp[0][0] = 0;
	for (int i = 0; i <= n; ++i) {
		int ptr = 0;
		int sum = 0;
		for (int j = n - 1; j >= 0; --j) {
			dp[i][j] = min(dp[i][j], dp[i][j + 1]);
		}
		for (int j = i + 1; j <= n; ++j) {
			sum += a[j];
			while (ptr < n && dp[i][ptr] <= sum) {
				ptr++;
			}
			dp[j][ptr] = min(dp[j][ptr], sum);
		}
	}
	for (int i = n; i >= 0; --i) {
		if (dp[n][i] != big) {
			cout << (n - i) << endl;
			break;
		}
	}
}

int main( )
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    solve();
    return 0;
}


/*
 * Author - Abhas Jain
 * Code is inside the solve function
 */

#include <bits/stdc++.h>

#define F first
#define S second
#define all(c) c.begin(), c.end()
#define endl '\n'
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;
mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ld PI = 3.141592653589793;
const ll INF = 9223372036854775807ll;
const ll mod = 1e9 + 7;
const int N = 5e3 + 1;

int dp[N][N];

void solve()
{
	int n;
	cin >> n;
	int big = 1e9;
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			dp[i][j] = big;
		}
	}
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	dp[0][0] = 0;
	for (int i = 0; i <= n; ++i) {
		int ptr = 0;
		int sum = 0;
		for (int j = n - 1; j >= 0; --j) {
			dp[i][j] = min(dp[i][j], dp[i][j + 1]);
		}
		for (int j = i + 1; j <= n; ++j) {
			sum += a[j];
			while (dp[i][ptr] <= sum) {
				ptr++;
			}
			if (ptr) dp[j][ptr] = min(dp[j][ptr], sum);
		}
	}
	for (int i = n; i >= 0; --i) {
		if (dp[n][i] != big) {
			cout << (n - i) << endl;
			break;
		}
	}
}

int main( )
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp male_pair
#define ll long long
#define INF 1999999999
using namespace std;
ll n,x[100006],sm,lst,ans;
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    cin>>x[i];
  }
  x[n+1]=INF;
  lst=x[1];
  for(int i=2;i<=n;i++)
  {
    int j=i;
    sm=x[i];
    while(sm<lst)
    {
      j++;
      sm+=x[j];
      ans++;
    }
    lst=sm;
    i=j;
  }
  cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp male_pair
#define ll long long
#define INF 1999999999
using namespace std;
ll n,x[100006],sm,lst,ans;
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    cin>>x[i];
  }
  x[n+1]=INF;
  lst=x[1];
  for(int i=2;i<=n;i++)
  {
    int j=i;
    sm=x[i];
    while(sm<lst)
    {
      j++;
      sm+=x[i];
      ans++;
    }
    lst=sm;
    i=j;
  }
  cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define INF 1999999999ll
#define pi pair<ll,ll>

using namespace std;
ll n,x[100006],sm,lst,ans,sum[100006];
pi dp[100006],k;
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    cin>>x[i];
    sum[i]=sum[i-1]+x[i];
  }
  dp[1].f=x[1];
  for(int i=2;i<=n;i++)
  {
    k=mp(INF,INF);
    for(int j=1;j<=i;j++)
    {
      if(sum[i]-sum[j-1]>=dp[j-1].f)
      {
        k=min(k,mp(sum[i]-sum[j-1],dp[j-1].s+i-j));
      }
    }
    dp[i]=k;
  }
  cout<<dp[n].s<<endl;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}

const int MAXN = 5e3 + 10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 8e18;

ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % MOD, b / 2)) % MOD : pw(a * a % MOD, b / 2))); }

int v[MAXN];

ll dp[MAXN][MAXN], ps[MAXN];

int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    //scanf("%d %d %f %lld %s %c", &x);
    //printf("%d %d %f %lld %s %c", x);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> v[i];
        ps[i + 1] = ps[i] + v[i];
    }
    for(int i = 0 ;i <= n + 10; i++){
        for(int j = 0; j <= n + 10; j++){
            dp[i][j] = 1e9;
        }
    }
    dp[0][0] = 0;
    dp[0][1] = 0;
    dp[1][1] = 0;
    dp[1][0] = 0;
    //return cout << ps[2] - ps[1], 0;
    for(int i = 2; i <= n; i++){
        int x = i - 1;
        for(int j = i; j >= 1; j--){
            x = min(x, j);
            while(x != 0 && ps[i] - ps[j - 1] >= ps[j - 1] - ps[x - 1]){
                dp[i][j] = min(dp[i][j], dp[j - 1][x] + (i - j));
                x--;
            }
            dp[i][j] = min(dp[i][j], dp[i][j + 1]);
        }
    }
    /*
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= n; j++){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }
    */
    ll ans = 1e18;
    for(int i = 0 ;i <= n ;i++){
        //cout << dp[n][i] << " ";
        ans = min(ans, dp[n][i]);
    }
    cout << ans;

    return 0;
}


from sys import stdin,stdout

def solve():
    n,m,x,y = map(int, input().split())
    y=min(y,x<<1)
    ans=0
    for _ in range(n):
        s=input()
        i=0
        if m==1:
            if s[0]=='.':
                ans+=x
        else:
            while i<m-1:
                if s[i]=='.':
                    i+=1
                    if s[i]=='.':
                        ans+=y
                    else:
                        ans+=x
                i+=1
            if s[-1]=='.':
                if m&1 or s[-2]=='*':
                    ans+=x
    return ans

for _ in range(int(stdin.readline())):
    stdout.write(str(solve()) + '\n')


from sys import stdin,stdout

def solve():
    n,m,x,y = map(int, input().split())
    y=min(y,x<<1)
    ans=0
    for _ in range(n):
        s=input()+'*'
        i=0
        if m==1:
            if s[0]=='.':
                ans+=x
        else:
            while i<m:
                if s[i]=='.':
                    i+=1
                    if s[i]=='.':
                        ans+=y
                    else:
                        ans+=x
                i+=1
    return ans

for _ in range(int(stdin.readline())):
    stdout.write(str(solve()) + '\n')


import  math
t=int(input())
for u in range(t):
    n,m,x,y=map(int,input().split())
    p=0
    flag=0
    for q in range(n):
        s=input()
        c=0

        for i in range(len(s)):
            if (s[i] == '.'):
                c = c + 1
                flag=0
            elif(s[i]=='*'):

                flag=1
                cost = 0
                if (c == 1):
                    cost = x
                elif (2 * x <= y):
                    cost = x
                elif (c % 2 == 0 and y <= x):
                    cost = y
                elif (c % 2 == 0 and y > x):
                    cost = x
                elif (c % 2 != 0 and y <= x):
                    cost = y
                elif (c % 2 != 0 and y > x):
                    cost = x

                if (c == 1):
                    p = p + x
                elif (c % 2 == 0 and cost == y):
                    p = p + (c // 2 * cost)
                elif (c % 2 != 0 and cost == y):
                    p = p + (c // 2 * cost + x)
                elif (c % 2 != 0 and cost == x):
                    p = p + (c * cost + x)
                elif (c % 2 == 0 and cost == x):
                    p = p + (c * cost)
                c=0
        if(flag==0):
            cost =0
            if (c == 1):
                cost = x
            elif (2 * x <= y):
                cost = x
            elif (c % 2 == 0 and y <= x):
                cost = y
            elif (c % 2 == 0 and y > x):
                cost = x
            elif (c % 2 != 0 and y <= x):
                cost = y
            elif (c % 2 != 0 and y > x):
                cost = x

            if (c == 1):
                p = p + x
            elif (c % 2 == 0 and cost == y):
                p = p + (c // 2 * cost)
            elif (c % 2 != 0 and cost == y):
                p = p + (c // 2 * cost + x)
            elif (c % 2 != 0 and cost == x):
                p = p + (c * cost + x)
            elif (c % 2 == 0 and cost == x):
                p = p + (c * cost)

    print(p)


t=int(input())
for _ in range(t):
    n,m,x,y=map(int,input().split())
    s=0
    c=0
    for _ in range(n):
        l=input()
        c1=l.count('.')
        c2=l.count('..')
        s+=c2*y + (c1-2*c2)*x
        c+=c1
    print(min(s,c*x))


import  math
t=int(input())
for u in range(t):
    n,m,x,y=map(int,input().split())
    p=0
    for q in range(n):
        s=input()
        c=0

        for i in range(len(s)):
            if (s[i] == '.'):
                c = c + 1
        cost = 0
        if (c == 1):
            cost = x
        elif (2 * x <= y):
            cost = x
        elif (c % 2 == 0 and y <= x):
            cost = y
        elif (c % 2 == 0 and y > x):
            cost = x
        elif (c % 2 != 0 and y <= x):
            cost = y
        elif (c % 2 != 0 and y > x):
            cost = x

        if(c==1):
            p=p+x
        elif(c % 2 == 0 and cost == y):
            p=p+(c // 2 * cost)
        elif (c % 2 != 0 and cost == y):
            p=p+(c // 2 * cost + x)
        elif (c % 2 != 0 and cost == x):
            p=p+(c * cost + x)
        elif (c % 2 == 0 and cost == x):
            p=p+(c * cost)

    print(p)


for i in range(n):
		j=0
		s=-1
		while(j<=m):
			while(A[i][j]=='.'):
				j+=1
			else:
				z=j-s-1
				if(z>0 and 2*x>y):
					ans+=((z//2)*y)
					ans+=((z%2)*x)
				elif(z>0 and 2*x<=y):
					ans+=(z*x)
				s=j
				j+=1
	print(ans)


for _ in range(int(input())):
    n,m,x,y=map(int,input().split())
    l=[]
    for i in range(n):
        lst=input()
        lst+='*'
        l.append(lst)
    cost=0
    for i in range(n):
        k=0
        s=-1
        while(k<=m):
            while(l[i][k]=='.'):
                k+=1
            z=k-s-1
            if(z>0 and 2*x>y):
                cost+=((z//2)*y)
                cost+=((z%2)*x)
            elif(z>0 and 2*x<=y):
                cost+=z*x
            s=k
            k+=1
    print(cost)


for _ in range(int(input())):
    n,m,x,y=map(int,input().split())
    l=[]
    for i in range(n):
        lst=input()
        lst+='*'
        l.append(lst)
    cost=0
    for i in range(n):
        k=0
        s=-1
        while(k<=m):
            while(l[i][k]=='.'):
                k+=1
            z=k-s-1
            if(z>0 and 2*x*y):
                cost+=((z//2)*y)
                cost+=((z%2)*x)
            elif(z>0 and 2*x<=y):
                cost+=z*x
            s=k
            k+=1
    print(cost)


for I in range(int(input())):
    n,m,x,y = map(int,input().split())

    result=0
    for i in range(n):
        s = input()

        if 2*x<=y:
            for i in range(m):
                if s[i]=='.':
                    result+=x

        else:
            for i in range(m-1):
                if s[i]=='.' and s[i+1]=='.':
                    result+=y
                elif s[i]=='.':
                    result+=x
            if len(s)>1:
                if s[-1]=='.' and s[-2]=='.':
                    pass
                elif s[-1]=='.':
                    result+=x
            else:
                if s[0]=='.':
                    result+=x

    print(result)


for I in range(int(input())):
    n,m,x,y = map(int,input().split())

    result=0
    for i in range(n):
        s = input()

        if 2*x<=y:
            for i in range(m):
                if s[i]=='.':
                    result+=x

        else:
            i=0
            while(i<m-1):
                if s[i]=='.' and s[i+1]=='.':
                    result+=y
                    i+=1
                elif s[i]=='.':
                    result+=x
                i+=1

            if len(s)>1:
                if s[-1]=='.' and s[-2]=='.':
                    pass
                elif s[-1]=='.':
                    result+=x
            else:
                if s[0]=='.':
                    result+=x

    print(result)


for I in range(int(input())):
    n,m,x,y = map(int,input().split())

    result=0
    for i in range(n):
        s = input()

        if 2*x<=y:
            for i in range(m):
                if s[i]=='.':
                    result+=x

        else:
            flag=''
            i=0
            while(i<m-1):
                if s[i]=='.' and s[i+1]=='.':
                    result+=y
                    if i+1==m-1:
                        flag='done'
                    i+=1
                elif s[i]=='.':
                    result+=x
                    if i==m-1:
                        flag='done'
                i+=1

            if len(s)>1:
                if flag=='':
                    if s[-1]=='.':
                        result+=x
            else:
                if s[0]=='.':
                    result+=x

    print(result)


for _ in range(int(input())):
    n,m,x,y=map(int , input().split())
    a = [None]*n

    for i in range(n):
        a[i] = input()

    if m == 1:
        print(a.count('.')*x)
    elif(2*x<y):
        c = 0
        for i in range(n):
            c = c+a[i].count('.')
        print(c*x)
    else:
        c = 0
        for i in range(n):
            c  = c + (a[i].count('..')*y) +((a[i].count('.') - 2*a[i].count('..'))*x)

        print(c)


for _ in range(int(input())):
    n,m,x,y=map(int , input().split())
    a = [None]*n

    for i in range(n):
        a[i] = input()

    if m == 1:
        print(a.count('.')*x)
    elif(2*x<y):
        c = 0
        for i in range(n):
            c = c+a[i].count('.')
        print(c*x)
    else:
        c = 0
        for i in range(n):
            if a[i].count('.')%2 == 0:
                c = c + (a[i].count('.')//2)*y

            elif a[i].count('.') == 1:
                c = c+x

            else:
                c = c+((a[i].count('.')-1)//2)*y + x

        print(c)


from sys import stdin

for _ in range(int(stdin.readline())):
    n, m, x, y = list(map(int, stdin.readline().split()))
    two_tile = y if y < 2 * x else 2 * x
    res = 0
    for _ in range(n):
        s = input() + '*'
        con = 0
        for i in s:
            if i == '.':
                con += 1
            else:
                a, b = divmod(con, 2)
                res += two_tile * a + x * b
                con = 0
    print(res)


from sys import stdin

for _ in range(int(stdin.readline())):
    n, m, x, y = list(map(int, stdin.readline().split()))
    two_tile = y if y < 2 * x else 2 * x
    res = 0
    for _ in range(n):
        s = input()
        for i in range(0, m, 2):
            if s[i:i+2] == '..':
                res += two_tile
            elif '.' in s[i:i+2]:
                res += x
    print(res)


# for t in range(int(input())):
#     n, x, m = [int(i) for i in input().split()]
#     l, r = x, x
#     # print(l, r)
#     for i in range(m):
#         cl, cr = [int(i) for i in input().split()]
#         # print(cl, r, l, cr)
#         if cl <= r and l <= cr:
#             l, r = min(l, cl), max(r, cr)
#         # print(l, r)
#     print(r - l + 1)

# 12 58
# for t in range(int(input())):
#     n = int(input())
#     a = [int(i) for i in input().split()]
#     b = [int(i) for i in input().split()]
#     sorted = True
#     i = 1
#     while i < n and sorted:
#         if a[i] >= a[i - 1]:
#             i += 1
#         else:
#             sorted = False
#     print('Yes' if 0 < sum(b) < n or sorted else 'No')

# 13 20
# for t in range(int(input())):
#     n = int(input())
#     p = [int(i) for i in input().split()]
#     ans = [str(p[0])]
#     i = 1
#     while i + 1 < n:
#         if p[i - 1] < p[i] > p[i + 1] or p[i - 1] > p[i] < p[i + 1]:
#             ans.append(str(p[i]))
#         i += 1
#     ans.append(str(p[-1]))
#     print(len(ans))
#     print(' '.join(ans))

# for t in range(int(input())):
#     s = input()
#     ones, zeros, ans = 0, 0, float('inf')
#     for ch in s:
#         if ch == '1':
#             ones += 1
#     zeros = len(s) - ones
#     enc = 0  # count ecountered ones
#     for i in range(len(s)):
#         enc += s[i] == '1'
#         flip0, flip1 = enc + zeros - (i + 1 - enc), i + 1 - enc + ones - enc
#         ans = min(ans, flip0, flip1)
#     print(ans)

# for t in range(int(input())):
#     ans = -1
#     n = int(input())
#     s = {int(i) for i in input().split()}
#     i = 1
#     while i < 1024:
#         a = {x ^ i for x in s}
#         if a == s:
#             ans = i
#             break
#         i += 1
#     print(ans)

for t in range(int(input())):
    m, n, x, y = [int(i) for i in input().split()]
    grid = [input() for i in range(m)]
    y = min(y, 2 * x)
    ans = 0
    for r in grid:
        i = 0
        while i < n:
            while i < n and r[i] == '*':
                i += 1
            j = i
            while j + 1 < n and r[j + 1] == '.':
                j += 1
            ans += (j - i + 1) // 2 * y + (j - 1 + 1) % 2 * x
            i = j + 1
    print(ans)


# for t in range(int(input())):
#     n, x, m = [int(i) for i in input().split()]
#     l, r = x, x
#     # print(l, r)
#     for i in range(m):
#         cl, cr = [int(i) for i in input().split()]
#         # print(cl, r, l, cr)
#         if cl <= r and l <= cr:
#             l, r = min(l, cl), max(r, cr)
#         # print(l, r)
#     print(r - l + 1)

# 12 58
# for t in range(int(input())):
#     n = int(input())
#     a = [int(i) for i in input().split()]
#     b = [int(i) for i in input().split()]
#     sorted = True
#     i = 1
#     while i < n and sorted:
#         if a[i] >= a[i - 1]:
#             i += 1
#         else:
#             sorted = False
#     print('Yes' if 0 < sum(b) < n or sorted else 'No')

# 13 20
# for t in range(int(input())):
#     n = int(input())
#     p = [int(i) for i in input().split()]
#     ans = [str(p[0])]
#     i = 1
#     while i + 1 < n:
#         if p[i - 1] < p[i] > p[i + 1] or p[i - 1] > p[i] < p[i + 1]:
#             ans.append(str(p[i]))
#         i += 1
#     ans.append(str(p[-1]))
#     print(len(ans))
#     print(' '.join(ans))

# for t in range(int(input())):
#     s = input()
#     ones, zeros, ans = 0, 0, float('inf')
#     for ch in s:
#         if ch == '1':
#             ones += 1
#     zeros = len(s) - ones
#     enc = 0  # count ecountered ones
#     for i in range(len(s)):
#         enc += s[i] == '1'
#         flip0, flip1 = enc + zeros - (i + 1 - enc), i + 1 - enc + ones - enc
#         ans = min(ans, flip0, flip1)
#     print(ans)

# for t in range(int(input())):
#     ans = -1
#     n = int(input())
#     s = {int(i) for i in input().split()}
#     i = 1
#     while i < 1024:
#         a = {x ^ i for x in s}
#         if a == s:
#             ans = i
#             break
#         i += 1
#     print(ans)

for t in range(int(input())):
    m, n, x, y = [int(i) for i in input().split()]
    grid = [input() for i in range(m)]
    if 2 * x < y:
        ans = 0
        for r in grid:
            ans += r.count('.')
        print(ans * x)
    else:
        dp = [[0] * (n + 1) for i in range(m + 1)]
        ans = 0
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if grid[i - 1][j - 1] == '.':
                    if dp[i][j - 1] == x:
                        dp[i][j - 1], dp[i][j] = 0, y
                        ans = ans + y - x
                    else:
                        dp[i][j] = x
                        ans += x

        print(ans)


# for t in range(int(input())):
#     n, x, m = [int(i) for i in input().split()]
#     l, r = x, x
#     # print(l, r)
#     for i in range(m):
#         cl, cr = [int(i) for i in input().split()]
#         # print(cl, r, l, cr)
#         if cl <= r and l <= cr:
#             l, r = min(l, cl), max(r, cr)
#         # print(l, r)
#     print(r - l + 1)

# 12 58
# for t in range(int(input())):
#     n = int(input())
#     a = [int(i) for i in input().split()]
#     b = [int(i) for i in input().split()]
#     sorted = True
#     i = 1
#     while i < n and sorted:
#         if a[i] >= a[i - 1]:
#             i += 1
#         else:
#             sorted = False
#     print('Yes' if 0 < sum(b) < n or sorted else 'No')

# 13 20
# for t in range(int(input())):
#     n = int(input())
#     p = [int(i) for i in input().split()]
#     ans = [str(p[0])]
#     i = 1
#     while i + 1 < n:
#         if p[i - 1] < p[i] > p[i + 1] or p[i - 1] > p[i] < p[i + 1]:
#             ans.append(str(p[i]))
#         i += 1
#     ans.append(str(p[-1]))
#     print(len(ans))
#     print(' '.join(ans))

# for t in range(int(input())):
#     s = input()
#     ones, zeros, ans = 0, 0, float('inf')
#     for ch in s:
#         if ch == '1':
#             ones += 1
#     zeros = len(s) - ones
#     enc = 0  # count ecountered ones
#     for i in range(len(s)):
#         enc += s[i] == '1'
#         flip0, flip1 = enc + zeros - (i + 1 - enc), i + 1 - enc + ones - enc
#         ans = min(ans, flip0, flip1)
#     print(ans)

# for t in range(int(input())):
#     ans = -1
#     n = int(input())
#     s = {int(i) for i in input().split()}
#     i = 1
#     while i < 1024:
#         a = {x ^ i for x in s}
#         if a == s:
#             ans = i
#             break
#         i += 1
#     print(ans)

for t in range(int(input())):
    m, n, x, y = [int(i) for i in input().split()]
    grid = [input() for i in range(m)]
    if 2 * x <= y:
        ans = 0
        for r in grid:
            ans += r.count('.')
        print(ans * x)
    else:
        ans = 0
        for i in range(1, m + 1):
            dp = [0] * (n + 1)
            for j in range(1, n + 1):
                if grid[i - 1][j - 1] == '.':
                    if dp[j - 1] == x:
                        dp[j - 1], dp[j] = 0, y
                        ans = ans + y - x
                    else:
                        dp[j] = x
                        ans += x

        print(ans)


# for t in range(int(input())):
#     n, x, m = [int(i) for i in input().split()]
#     l, r = x, x
#     # print(l, r)
#     for i in range(m):
#         cl, cr = [int(i) for i in input().split()]
#         # print(cl, r, l, cr)
#         if cl <= r and l <= cr:
#             l, r = min(l, cl), max(r, cr)
#         # print(l, r)
#     print(r - l + 1)

# 12 58
# for t in range(int(input())):
#     n = int(input())
#     a = [int(i) for i in input().split()]
#     b = [int(i) for i in input().split()]
#     sorted = True
#     i = 1
#     while i < n and sorted:
#         if a[i] >= a[i - 1]:
#             i += 1
#         else:
#             sorted = False
#     print('Yes' if 0 < sum(b) < n or sorted else 'No')

# 13 20
# for t in range(int(input())):
#     n = int(input())
#     p = [int(i) for i in input().split()]
#     ans = [str(p[0])]
#     i = 1
#     while i + 1 < n:
#         if p[i - 1] < p[i] > p[i + 1] or p[i - 1] > p[i] < p[i + 1]:
#             ans.append(str(p[i]))
#         i += 1
#     ans.append(str(p[-1]))
#     print(len(ans))
#     print(' '.join(ans))

# for t in range(int(input())):
#     s = input()
#     ones, zeros, ans = 0, 0, float('inf')
#     for ch in s:
#         if ch == '1':
#             ones += 1
#     zeros = len(s) - ones
#     enc = 0  # count ecountered ones
#     for i in range(len(s)):
#         enc += s[i] == '1'
#         flip0, flip1 = enc + zeros - (i + 1 - enc), i + 1 - enc + ones - enc
#         ans = min(ans, flip0, flip1)
#     print(ans)

# for t in range(int(input())):
#     ans = -1
#     n = int(input())
#     s = {int(i) for i in input().split()}
#     i = 1
#     while i < 1024:
#         a = {x ^ i for x in s}
#         if a == s:
#             ans = i
#             break
#         i += 1
#     print(ans)

for t in range(int(input())):
    m, n, x, y = [int(i) for i in input().split()]
    grid = [input() for i in range(m)]
    y = min(y, 2 * x)
    ans = 0
    for r in grid:
        i = 0
        while i < n:
            if r[i] == '*':
                i += 1
                continue
            j = i
            while j + 1 < n and r[j + 1] == '.':
                j += 1

            ans += (j - i + 1) // 2 * y + (j - i + 1) % 2 * x
            i = j + 1

    print(ans)


# for t in range(int(input())):
#     n, x, m = [int(i) for i in input().split()]
#     l, r = x, x
#     # print(l, r)
#     for i in range(m):
#         cl, cr = [int(i) for i in input().split()]
#         # print(cl, r, l, cr)
#         if cl <= r and l <= cr:
#             l, r = min(l, cl), max(r, cr)
#         # print(l, r)
#     print(r - l + 1)

# 12 58
# for t in range(int(input())):
#     n = int(input())
#     a = [int(i) for i in input().split()]
#     b = [int(i) for i in input().split()]
#     sorted = True
#     i = 1
#     while i < n and sorted:
#         if a[i] >= a[i - 1]:
#             i += 1
#         else:
#             sorted = False
#     print('Yes' if 0 < sum(b) < n or sorted else 'No')

# 13 20
# for t in range(int(input())):
#     n = int(input())
#     p = [int(i) for i in input().split()]
#     ans = [str(p[0])]
#     i = 1
#     while i + 1 < n:
#         if p[i - 1] < p[i] > p[i + 1] or p[i - 1] > p[i] < p[i + 1]:
#             ans.append(str(p[i]))
#         i += 1
#     ans.append(str(p[-1]))
#     print(len(ans))
#     print(' '.join(ans))

# for t in range(int(input())):
#     s = input()
#     ones, zeros, ans = 0, 0, float('inf')
#     for ch in s:
#         if ch == '1':
#             ones += 1
#     zeros = len(s) - ones
#     enc = 0  # count ecountered ones
#     for i in range(len(s)):
#         enc += s[i] == '1'
#         flip0, flip1 = enc + zeros - (i + 1 - enc), i + 1 - enc + ones - enc
#         ans = min(ans, flip0, flip1)
#     print(ans)

# for t in range(int(input())):
#     ans = -1
#     n = int(input())
#     s = {int(i) for i in input().split()}
#     i = 1
#     while i < 1024:
#         a = {x ^ i for x in s}
#         if a == s:
#             ans = i
#             break
#         i += 1
#     print(ans)

for t in range(int(input())):
    m, n, x, y = [int(i) for i in input().split()]
    grid = [input() for i in range(m)]
    if 2 * x <= y:
        ans = 0
        for r in grid:
            ans += r.count('.')
        print(ans * x)
    else:
        dp = [[0] * (n + 1) for i in range(m + 1)]
        ans = 0
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if grid[i - 1][j - 1] == '.':
                    # if dp[i - 1][j] == x:
                    #     dp[i - 1][j], dp[i][j] = 0, y
                    #     ans += y - x
                    if dp[i][j - 1] == x:
                        dp[i][j - 1], dp[i][j] = 0, y
                        ans += y - x
                    else:
                        dp[i][j] = x
                        ans += x
            # for r in dp:
            #     print(r)
        print(ans)