// Problem: C2. Potions (Hard Version)
// Contest: Codeforces - Codeforces Round #723 (Div. 2)
// URL: https://codeforces.com/contest/1526/problem/C2
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define int long long
#define float long double
#define pr pair<int,int>
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cout << vars << " = ";
    string delim = "";
    (..., (cout << delim << values, delim = ", "));
    cout<<endl;
}

const int mod=1e9+7;
const int N=2e5+5;
const int inf=1e18;
void solve(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
	cin>>a[i];
	priority_queue<int,vector<int> , greater<int> > pq;
	int sum=0,c=0,ans=0;
	for(int i=0;i<n;i++){
		if(sum+a[i]>=0){
			c++;
			sum+=a[i];
			if(a[i]<0)
			pq.push(a[i]);
		}
		else{
			if(pq.size()>0&&pq.top()<a[i]){
				sum-=pq.top();
				pq.pop();
				sum+=a[i];
				if(a[i]<0)
				pq.push(a[i]);
			}
		}
		// deb(i,c,sum);
		
		// cout<<"i="<<i<<" c="<<c<<endl;
		ans=max(ans,c);
	}
	cout<<ans<<endl;
}
int32_t main(){
	IOS
    int t=1;
    //cin>>t;
    while(t--)
        solve();
}