//
//AtCoder Beginner Contest 182
//D
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <functional>
#include <cassert>
#include <bitset>
#include <numeric>

using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }    //最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }            //最小公倍数

int main()
{
    int n;
    cin >> n;

    vector<int> a(n);
    REP(i, n) cin >> a[i];

    ll ans=0;
    ll sum=0;
    ll mvMax=0;
    ll now=0;

    REP(i, n){
        sum+=a[i];
        mvMax = max(mvMax,sum);
        ans = max(ans, now+mvMax);
        now = now+sum;
    }
    cout << ans << endl;
    return 0;
}

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
struct Node
{
	int l,r;
	bool operator<(const Node& a )const
	{
		return l<a.l;
	}
}line[N];
int a[N],m;
long long tr[N];
int lowbit(int x)
{
	return x&(-x);
}
void add(int x,int d)
{
	for(;x<=m+m;x+=lowbit(x))
    {
		tr[x]+=d;
	}
}
long long query(int x)
{
	long long res=0;
	for(;x;x-=lowbit(x))
    {
		res+=tr[x];
	}
	return res;
}
int find(int x)
{
	return lower_bound(a+1,a+1+m,x)-a;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
    {
            int t,l,r;
            cin>>t>>l>>r;
            l<<=1;
            r<<=1;
            if(t==2)
            {
                r--;
            }
		else if(t==3)
        {
            l++;
        }else if(t==4)
		{
			l++;r--;
		}
		line[i]={l,r};
		a[++m]=l;
		a[++m]=r;
	}
	sort(a+1,a+1+m);
	m=unique(a+1,a+1+m)-(a+1);//去除相邻重复数字
	sort(line+1,line+1+n);
	int pre=0;
	long long res=0;
	for(int i=1;i<=n;i++)
	{
		int l=line[i].l;
		int r=line[i].r;
		l=find(l);
		r=find(r);
		int t=query(m)-query(l-1);
		res+=max(t,0);
		//pre=max(r,pre);
		add(r,1);
	}
	cout<<res;
	return 0;
}
