#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iterator>

using namespace std;

struct circle
{
	int r;
	int R;
	int h;

	friend bool operator < (circle& c1, circle& c2)
	{
		return c1.R > c2.R;
	}
	friend bool operator > (circle& c1, circle& c2)
	{
		return c1.R < c2.R;
	}
};

circle arr[100500];
long long tree[800500];

long long sum(int v, int tl, int tr,int l, int r)
{
	if (l > r)
		return 0;
	if (tl == l && tr == r)
		return tree[v];
	else
	{
		int tm = (tl + tr) / 2;
		return max(sum(2 * v, tl, tm, l, min(tm, r)), sum(2 * v + 1, tm + 1, tr, max(tm+1, l), r));
	}
}

void update(int v, int tl, int tr, int pos, long long new_val)
{
	if (tl == tr)
		tree[v] = new_val;
	else
	{
		int tm = (tl + tr) / 2;
		if (pos > tm)
			update(v * 2 + 1, tm + 1, tr, pos, new_val);
		else
			update(2 * v, tl, tm, pos, new_val);
		tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
	}
}

bool comp(circle a, circle b)
{
	return a.R == b.R ? a.r > b.r : a.R > b.R;
}

int main()
{
	int n;
	cin >> n;
	for (size_t i = 0; i < n; i++)
	{
		int r, R, h;
		cin >> arr[i].r >> arr[i].R >> arr[i].h;
	}
	sort(arr, arr + n, comp);
	vector<pair<int, pair<short, int>>> v;
	for (int i = 0; i < n; i++)
	{
		v.push_back(make_pair(arr[i].R, make_pair(1, i)));
		v.push_back(make_pair(arr[i].r, make_pair(0, i)));
	}
	sort(v.begin(), v.end());
	int j = 0;
	for (size_t i = 0; i < v.size()-1; i++)
	{
		if (v[i].first != v[i + 1].first)
		{
			j++;
			v[i].first = j-1;
		}
		else
			v[i].first = j;
	}
	v[v.size() - 1].first = j;
	for (size_t i = 0; i < v.size(); i++)
	{
		if (v[i].second.first == 1)
		{
			arr[v[i].second.second].R = v[i].first;
		}
		else
			arr[v[i].second.second].r = v[i].first;
	}
	for (size_t i = 0; i < n; i++)
	{
		long long pref_max = sum(1, 0, 2*n, 0, arr[n-1-i].R);
		update(1, 0, 2*n, arr[n-1-i].r,  pref_max + 1ll* arr[n-1-i].h);
	}
	cout << sum(1, 0, 2*n,0, 2*n) << endl;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>

using namespace std;

struct circle
{
	int r;
	int R;
	int h;

	friend bool operator < (circle& c1, circle& c2)
	{
		return c1.R > c2.R;
	}
	friend bool operator > (circle& c1, circle& c2)
	{
		return c1.R < c2.R;
	}
};

circle arr[200000];
long long tree[400500];

long long sum(int v, int tl, int tr,int l, int r)
{
	if (l > r)
		return 0;
	if (tl == l && tr == r)
		return tree[v];
	else
	{
		int tm = (tl + tr) / 2;
		return max(sum(2 * v, tl, tm, l, min(tm, r)), sum(2 * v + 1, tm + 1, tr, max(tm+1, l), r));
	}
}

void update(int v, int tl, int tr, int pos, long long new_val)
{
	if (tl == tr)
		tree[v] = new_val;
	else
	{
		int tm = (tl + tr) / 2;
		if (pos > tm)
			update(v * 2 + 1, tm + 1, tr, pos, new_val);
		else
			update(2 * v, tl, tm, pos, new_val);
		tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
	}
}

int main()
{
	int n;
	cin >> n;
	map<int, pair<int, int>> m;
	for (size_t i = 0; i < n; i++)
	{
		int r, R, h;
		cin >> r >> R >> h;
		if (m.find(R) == m.end())
			m.insert(make_pair(R, make_pair(r, h)));
		else
		{
			auto it = m.find(R);
			it->second.first = min(it->second.first, r);
			it->second.second += h;
		}
	}
	n = m.size();
	int* r = new int[2*n];
	//int  i = 0;
	//for (auto it = m.begin(); it != m.end(); it++)
	//{
	//	r[i] = it->second.first;
	//	i++;
	//	r[i] = it->first;
	//	i++;
	//}
	//sort(r, r + n);
	//map<int, int> s;
	//int ind = 1;
	//s.insert(make_pair(r[0], 0));
	//for (i = 1; i < n; i++)
	//{
	//	if (r[i] == r[i - 1])
	//		continue;
	//	s.insert(make_pair(r[i], ind));
	//	ind++;
	//}
	//for (auto it = m.begin(); it != m.end(); it++)
	//{
	//	it->second.first = s.find(it->second.first)->second;
	//}
	//int i = n - 1;
	int i = 0;
	map<int, pair<int, int>> s;
	for (auto it = m.begin(); it != m.end(); it++)
	{
		s.insert(make_pair(it->first, make_pair(1,i)));
		s.insert(make_pair(it->second.first, make_pair(0,i)));
		i++;
	}
	i = 0;
	for (auto it = m.begin(); it != m.end(); it++)
	{
		arr[i].h = it->second.second;
		i++;
	}
	int stn = 0;
	for (auto it = s.begin(); it != s.end(); it++)
	{
		if (it->second.first == 0)
			arr[it->second.second].r = stn;
		else
			arr[it->second.second].R = stn;
		stn++;
	}
	/*cout << endl;
	for (size_t i = 0; i < n; i++)
	{
		cout << arr[i].r << arr[i].R;
	}*/
	for (size_t i = 0; i < n; i++)
	{
		long long pref_max = sum(1, 0, 2*n, 0, arr[n-1-i].R);
		update(1, 0, 2*n, arr[n-1-i].r,  pref_max + 1ll* arr[n-1-i].h);
	}
	cout << sum(1, 0, 2*n,0, 2*n) << endl;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iterator>

using namespace std;

struct circle
{
	int r;
	int R;
	int h;

	friend bool operator < (circle& c1, circle& c2)
	{
		return c1.R > c2.R;
	}
	friend bool operator > (circle& c1, circle& c2)
	{
		return c1.R < c2.R;
	}
};

circle arr[100500];
long long tree[800500];

long long sum(int v, int tl, int tr,int l, int r)
{
	if (l > r)
		return 0;
	if (tl == l && tr == r)
		return tree[v];
	else
	{
		int tm = (tl + tr) / 2;
		return max(sum(2 * v, tl, tm, l, min(tm, r)), sum(2 * v + 1, tm + 1, tr, max(tm+1, l), r));
	}
}

void update(int v, int tl, int tr, int pos, long long new_val)
{
	if (tl == tr)
		tree[v] = new_val;
	else
	{
		int tm = (tl + tr) / 2;
		if (pos > tm)
			update(v * 2 + 1, tm + 1, tr, pos, new_val);
		else
			update(2 * v, tl, tm, pos, new_val);
		tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
	}
}

bool comp(circle a, circle b)
{
	return a.R == b.R ? a.r < b.r : a.R < b.R;
}

int main()
{
	int n;
	cin >> n;
	for (size_t i = 0; i < n; i++)
	{
		int r, R, h;
		cin >> arr[i].r >> arr[i].R >> arr[i].h;
	}
	sort(arr, arr + n, comp);
	vector<pair<int, pair<short, int>>> v;
	for (int i = 0; i < n; i++)
	{
		v.push_back(make_pair(arr[i].R, make_pair(1, i)));
		v.push_back(make_pair(arr[i].r, make_pair(0, i)));
	}
	sort(v.begin(), v.end());
	int j = 0;
	for (size_t i = 0; i < v.size()-1; i++)
	{
		if (v[i].first != v[i + 1].first)
		{
			j++;
			v[i].first = j-1;
		}
		else
			v[i].first = j;
	}
	v[v.size() - 1].first = j;
	for (size_t i = 0; i < v.size(); i++)
	{
		if (v[i].second.first == 1)
		{
			arr[v[i].second.second].R = v[i].first;
		}
		else
			arr[v[i].second.second].r = v[i].first;
	}
	for (size_t i = 0; i < n; i++)
	{
		long long pref_max = sum(1, 0, 2*n, 0, arr[n-1-i].R);
		update(1, 0, 2*n, arr[n-1-i].r,  pref_max + 1ll* arr[n-1-i].h);
	}
	cout << sum(1, 0, 2*n,0, 2*n) << endl;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(l.begin(),l.end());
	sort(q.begin(),q.end());
	map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	frev(i,n-1,-1)
	{
		int b=binarySearchCount(q,n,l[i].first.first);
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;

	}
	ll ans=0;
	f(i,0,n)
	{
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
}
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      unordered_map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(l.begin(),l.end());
	sort(q.begin(),q.end());
	unordered_map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	ll ans=0;
	int b=0;
	int r[N];
	f(i,0,n)
	{
		while((b<n)&&(l[i].first.first>q[b]))
		{
			b+=1;
		}
		r[i]=b;
	}
	frev(i,n-1,-1)
	{
		int b=r[i];
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
}
int main()
{
	fast;
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      unordered_map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(l.begin(),l.end());
	sort(q.begin(),q.end());
	unordered_map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	ll ans=0;
	int b=0;
	frev(i,n-1,-1)
	{
		while((b<n)&&(l[i].first.first>q[b]))
		{
			b+=1;
		}
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
}
int main()
{
	fast;
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=1005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(q.begin(),q.end());
	map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	frev(i,n-1,-1)
	{
		int b=binarySearchCount(q,n,l[i].first.first);
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;

	}
	ll ans=0;
	f(i,0,n)
	{
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
}
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      unordered_map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(l.begin(),l.end());
	sort(q.begin(),q.end());
	unordered_map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	ll ans=0;
	frev(i,n-1,-1)
	{
		int b=binarySearchCount(q,n,l[i].first.first);
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
}
int main()
{
	fast;
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(l.begin(),l.end());
	sort(q.begin(),q.end());
	map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	ll ans=0;
	frev(i,n-1,-1)
	{
		int b=binarySearchCount(q,n,l[i].first.first);
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;
		ans=max(ans,dp[i]);

	}
	cout<<ans<<endl;
}
int main()
{
	fast;
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(l.begin(),l.end());
	sort(q.begin(),q.end());
	map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	frev(i,n-1,-1)
	{
		int b=binarySearchCount(q,n,l[i].first.first);
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;

	}
	ll ans=0;
	f(i,0,n)
	{
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
}
int main()
{
	fast;
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <iostream>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long llu;
typedef long double lld;
typedef pair<int,int> pi;
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)
#define ll        long long int
#define mod       (ll) 1000000007
#define pb        push_back
#define f(i,s,n)    for (int i=s;i<n;i++)
#define frev(i,s,n) for(int i=s;i>n;i--)
#define vii       vector<int>
#define seti      set<int>
#define mapi      map<int,int>
#define mapv      map<int,vii>
#define vll       vector<ll>
#define vpp       vector<pp>
#define vss       vector<string>
#define lcm(a,b)    (a*b)/__gcd(a,b)
#define all(a)      a.begin(),a.end()
#define all(a)      a.begin(),a.end()
#define fo(i,a)       for(auto &i:a)
#define bs(v,x)     binary_search(all(v),x)
#define fast ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);srand(time(NULL))
#define sortrev(a) sort(a.begin(), a.end(), greater<int>());
#define vii2d       vector<vector<int>>
#define ms          multiset<int>
#define pairi       pair<int,int>
#define useti   unordered_set<int>
#define mp          make_pair
#define gcd(a,b)    __gcd(a,b)
const int N=100005;
int n;
vector<pair<pairi,int>> l;
vii q;
ll e[N];
ll dp[N];
ll tree[2*N];
ll max(ll a, ll b)
{
	if(a>b)
		return a;
	return b;
}
int binarySearchCount(vii arr, int n, int key)
{
    int left = 0;
    int right = n - 1;

    int count = 0;

    while (left <= right) {
        int mid = (right + left) / 2;
        if (arr[mid] <key) {
            count = mid + 1;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }

    return count;
}
void build(ll arr[])
{
    for (int i=0; i<n; i++)
        tree[n+i] = arr[i];
    for (int i = n - 1; i > 0; --i)
        tree[i] = max(tree[i<<1] , tree[i<<1 | 1]);
}
void updateTreeNode(int p, ll value)
{
    tree[p+n] = value;
    p = p+n;
    for (int i=p; i > 1; i >>= 1)
        tree[i>>1] = max(tree[i] , tree[i^1]);
}
ll query(int l, int r)
{
    ll res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1)
    {
        if (l&1)
            res = max(res,tree[l++]);

        if (r&1)
            res = max(res,tree[--r]);
    }
    return res;
}
void solve()
{
	cin>>n;
	f(i,0,n)
	{
		int a,b,c;
		cin>>a>>b>>c;
		l.pb(mp(mp(b,a),c));
		q.pb(a);
	}
	sort(q.begin(),q.end());
	map<int,vii>d;
	mapi t;
	f(i,0,n)
	{
		d[q[i]].pb(i);
		t[q[i]]=0;
		e[i]=0;
		dp[i]=0;
	}
	build(e);
	frev(i,n-1,-1)
	{
		int b=binarySearchCount(q,n,l[i].first.first);
		if(b!=0)
			dp[i]=query(0,b);
		dp[i]+=(ll)l[i].second;
		int ind=d[l[i].first.second][t[l[i].first.second]];
		e[ind]=dp[i];
		updateTreeNode(ind,dp[i]);
		t[l[i].first.second]+=1;

	}
	ll ans=0;
	f(i,0,n)
	{
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
}
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    while(t--)
      solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
map<ll, ll> sg;
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        int s = get(1, 1, 1e9, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 1e9, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
map<ll, ll> sg;
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        long long s = get(1, 1, 1e9, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 1e9, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
unordered_map<ll, ll> sg;
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector < pair < int, pair < int, int > > > g;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
        g.push_back({p[i].second.first, {i, 0}});
        g.push_back({p[i].first, {i, 1}});
    }
    sort(g.begin(), g.end());
    for(int i = 0, cnt = 0; i < g.size(); ++i) {
        if(i == 0 || g[i].first != g[i - 1].first) {
            cnt++;
        }
        if(g[i].second.second == 0) {
            p[g[i].second.first].second.first = cnt;
        } else {
            p[g[i].second.first].first = cnt;
        }
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        long long s = get(1, 1, 1e9, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 1e9, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
map<ll, ll> sg;
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        long long s = get(1, 1, 1e9, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 1e9, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
ll sg[8 * N];
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector < pair < int, pair < int, int > > > g;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
        g.push_back({p[i].second.first, {i, 0}});
        g.push_back({p[i].first, {i, 1}});
    }
    sort(g.begin(), g.end());
    for(int i = 0, cnt = 0; i < g.size(); ++i) {
        if(i == 0 || g[i].first != g[i - 1].first) {
            cnt++;
        }
        if(g[i].second.second == 0) {
            p[g[i].second.first].second.first = cnt;
        } else {
            p[g[i].second.first].first = cnt;
        }
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        long long s = get(1, 1, 2 * n, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 2 * n, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
unordered_map<ll, ll> sg;
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        long long s = get(1, 1, 1e9, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 1e9, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
ll n, ans;
ll dp[N];
pair<int, pair<int, int> > p[N];
ll sg[4 * N];
void upd(int u, int s, int e, int i, ll x){
	 if(i < s || i > e)return;
	 if(s == e) sg[u] = max(sg[u], x);
	 else {
	 	upd(u * 2, s, (s + e) / 2, i, x);
		upd(u * 2 + 1, (s + e) / 2 + 1, e, i, x);
		sg[u] = max(sg[u * 2], sg[u*2+1]);
     }
}
ll get(int u, int s, int e, int i, int j){
	if(j < s || i > e)return 0;
	if(i <= s && j >= e)return sg[u];
	return max(get(u * 2, s, (s + e) / 2, i, j), get(u * 2 + 1, (s + e) / 2 + 1, e, i, j));
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector < pair < int, pair < int, int > > > g;
    for(int i = 1; i <= n; i++) {
        cin >> p[i].second.first >> p[i].first >> p[i].second.second;
        g.push_back({p[i].second.first, {i, 0}});
        g.push_back({p[i].first, {i, 1}});
    }
    sort(g.begin(), g.end());
    for(int i = 0, cnt = 0; i < g.size(); ++i) {
        if(i == 0 || g[i].first != g[i - 1].first) {
            cnt++;
        }
        if(g[i].second.second == 0) {
            p[g[i].second.first].second.first = cnt;
        } else {
            p[g[i].second.first].first = cnt;
        }
    }
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) {
        long long s = get(1, 1, 2 * n, p[i].second.first + 1, p[i].first);
        dp[i] = s + p[i].second.second;
        upd(1, 1, 2 * n, p[i].first, dp[i]);
    }
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}


/**
 *	@file: C.cpp
 *	@author: Huy Le
 *	@created: 08-12-2020 11:34:01
**/

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

//***---***---***---***---***---***---BEGIN USELESS CODE---***---***---***---***---***---***//

#define ll long long
#define ld long double
const char newl = '\n';
#define sz(x) (int)(x).size()

template<class T> void read(vector<T>& vt);
template<class T>
void read(T& args) { cin >> args; }
template<class A, class... B >
void read(A& argc, B&... argv) {
	read(argc);
	read(argv...);
}
template<class T>
void read(vector<T>& vt) {
	for(auto &a : vt)
		read(a);
}

string to_string(bool bo) { return (bo ? "true" : "false"); }
string to_string(char c) { return string(1, c); }
string to_string(const char* str) { return string(str);}
string to_string(string str) { return str; }
template<class T>
string to_string(T vt) {
	bool ft = 1;
	string res;
	for(auto &a : vt) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += to_string(a);
	}
	return res;
}
string to_string(vector<bool> vt) {
	string res;
	bool ft = 1;
	for(int i = 0; i < (int)vt.size(); ++i) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += char('0' + vt[i]);
	}
	return res;
}

template<class T>
void write(T args) { cout << to_string(args); }
template<class A, class... B>
void write(const A argc, const B... argv) {
	write(argc);
	write(argv...);
}

void print() { write(newl); }
template<class A, class... B>
void print(const A argc, const B... argv) {
	write(argc);
	if(sizeof...(argv))
		write(' ');
	print(argv...);
}

void DEBUGLOCAL() { cerr << ")" << newl; }
template<class A, class... B>
void DEBUGLOCAL(A argc, B... argv) {
	cerr << to_string(argc);
	if(sizeof...(argv))
		cerr << ", ";
	DEBUGLOCAL(argv...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") [" << __FUNCTION__ << "] -> (" << #__VA_ARGS__ << "): (",DEBUGLOCAL(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

//***---***---***---***---***---***---END USELESS CODE---***---***---***---***---***---***//

int ts;
int n;
const int mxN=1e5+5;
vector<ll> inner(mxN);
vector<pair<pair<ll,ll>,ll>> ring(mxN);
ll bit[mxN];

void update(int ind, ll val) {
	for(++ind; ind <= n+1; ind += ind&-ind)
		bit[ind] += val;
}

ll query(int ind) {
	ll sum = 0;
	for(++ind; ind >0; ind -= ind&-ind)
		sum += bit[ind];
	return sum;
}

void solve() {
	read(n);
	memset(bit, 0, sizeof(bit));
	for(int i=0; i < n; ++i) {
		read(ring[i].first.first, ring[i].first.second, ring[i].second);
		inner[i]=ring[i].first.first;
	}
	sort(inner.begin(), inner.begin() + n);
	sort(ring.begin(), ring.begin() + n, [](pair<pair<ll,ll>,ll> p1, pair<pair<ll,ll>,ll> p2)->bool{
				if(p1.first.second == p2.first.second)
					return p1.first.first > p2.first.first;
				else
					return p1.first.second > p2.first.second;
			});
	ll ans = 0;
	for(int i=0 ; i < n; ++i) {
		int pos = lower_bound(inner.begin(), inner.begin() + n, ring[i].first.second) - inner.begin();
		ll cur = ring[i].second + query(pos);
		dbg(cur, ans);
		ans = max(ans, cur);
		pos = upper_bound(inner.begin(), inner.begin() + n, ring[i].first.first) - inner.begin();
		dbg(pos);
		update(pos, ring[i].second);
	}
	print(ans);
}

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

	//read(ts);
	//while(ts--)
		solve();
}


/**
 *	@file: B.cpp
 *	@author: Huy Le
 *	@created: 08-12-2020 11:34:01
**/

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

//***---***---***---***---***---***---BEGIN USELESS CODE---***---***---***---***---***---***//

#define ll long long
#define ld long double
const char newl = '\n';
#define sz(x) (int)(x).size()

template<class T> void read(vector<T>& vt);
template<class T>
void read(T& args) { cin >> args; }
template<class A, class... B >
void read(A& argc, B&... argv) {
	read(argc);
	read(argv...);
}
template<class T>
void read(vector<T>& vt) {
	for(auto &a : vt)
		read(a);
}

string to_string(bool bo) { return (bo ? "true" : "false"); }
string to_string(char c) { return string(1, c); }
string to_string(const char* str) { return string(str);}
string to_string(string str) { return str; }
template<class T>
string to_string(T vt) {
	bool ft = 1;
	string res;
	for(auto &a : vt) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += to_string(a);
	}
	return res;
}
string to_string(vector<bool> vt) {
	string res;
	bool ft = 1;
	for(int i = 0; i < (int)vt.size(); ++i) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += char('0' + vt[i]);
	}
	return res;
}

template<class T>
void write(T args) { cout << to_string(args); }
template<class A, class... B>
void write(const A argc, const B... argv) {
	write(argc);
	write(argv...);
}

void print() { write(newl); }
template<class A, class... B>
void print(const A argc, const B... argv) {
	write(argc);
	if(sizeof...(argv))
		write(' ');
	print(argv...);
}

void DEBUGLOCAL() { cerr << ")" << newl; }
template<class A, class... B>
void DEBUGLOCAL(A argc, B... argv) {
	cerr << to_string(argc);
	if(sizeof...(argv))
		cerr << ", ";
	DEBUGLOCAL(argv...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") [" << __FUNCTION__ << "] -> (" << #__VA_ARGS__ << "): (",DEBUGLOCAL(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

//***---***---***---***---***---***---END USELESS CODE---***---***---***---***---***---***//

int ts;
int n;

const int mxN = 2e5+5;
vector<pair<ll, int>> cor;
vector<ll> hei;
ll bit[mxN];

void add(int ind, ll val) {
	for(++ind; ind <= 2*n; ind+=ind&-ind)
		bit[ind]+=val;
}

void update(int i1, int i2, ll val) {
	add(i1, val);
	add(i2+1, -val);
}

ll query(int ind) {
	ll sum=0;
	for(; ind > 0; ind-=ind&-ind)
		sum+=bit[ind];
	return sum;
}

void solve() {
	read(n);
	cor.clear();
	memset(bit, 0, sizeof(bit));
	hei = vector<ll>(n);
	for(int i=0; i<n; ++i) {
		ll a,b,h;
		read(a,b,h);
		hei[i] = h;
		cor.push_back({a, i});
		cor.push_back({b, i});
	}
	n++;
	sort(cor.begin(), cor.end(), [](pair<ll, int> p1, pair<ll, int> p2)->bool {
			return p1.first < p2.first;
			});
	int cur = 0;
	for(int i=0; i < sz(cor); ++i) {
		if(i > 0 && cor[i].first != cor[i-1].first)
			++cur;
		cor[i].first = cur;
	}
	sort(cor.begin(), cor.end(), [](pair<ll, int> p1, pair<ll, int> p2)->bool {
			if(p1.second == p2.second)
				return p1.first < p2.first;
			else
				return p1.second < p2.second;
			});
	for(int i=0; i < sz(cor); i+=2) {
		update(cor[i].first, cor[i+1].first, hei[cor[i].second]);
	}
	int bitmask = 1<<17;
	ll ans = 0;
	while(bitmask > 0) {
		ans = max(ans, query(bitmask));
		bitmask >>= 1;
	}
	print(ans);
}

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

	//read(ts);
	//while(ts--)
		solve();
}


/**
 *	@file: B.cpp
 *	@author: Huy Le
 *	@created: 08-12-2020 11:34:01
**/

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

//***---***---***---***---***---***---BEGIN USELESS CODE---***---***---***---***---***---***//

#define ll long long
#define ld long double
const char newl = '\n';
#define sz(x) (int)(x).size()

template<class T> void read(vector<T>& vt);
template<class T>
void read(T& args) { cin >> args; }
template<class A, class... B >
void read(A& argc, B&... argv) {
	read(argc);
	read(argv...);
}
template<class T>
void read(vector<T>& vt) {
	for(auto &a : vt)
		read(a);
}

string to_string(bool bo) { return (bo ? "true" : "false"); }
string to_string(char c) { return string(1, c); }
string to_string(const char* str) { return string(str);}
string to_string(string str) { return str; }
template<class T>
string to_string(T vt) {
	bool ft = 1;
	string res;
	for(auto &a : vt) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += to_string(a);
	}
	return res;
}
string to_string(vector<bool> vt) {
	string res;
	bool ft = 1;
	for(int i = 0; i < (int)vt.size(); ++i) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += char('0' + vt[i]);
	}
	return res;
}

template<class T>
void write(T args) { cout << to_string(args); }
template<class A, class... B>
void write(const A argc, const B... argv) {
	write(argc);
	write(argv...);
}

void print() { write(newl); }
template<class A, class... B>
void print(const A argc, const B... argv) {
	write(argc);
	if(sizeof...(argv))
		write(' ');
	print(argv...);
}

void DEBUGLOCAL() { cerr << ")" << newl; }
template<class A, class... B>
void DEBUGLOCAL(A argc, B... argv) {
	cerr << to_string(argc);
	if(sizeof...(argv))
		cerr << ", ";
	DEBUGLOCAL(argv...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") [" << __FUNCTION__ << "] -> (" << #__VA_ARGS__ << "): (",DEBUGLOCAL(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

//***---***---***---***---***---***---END USELESS CODE---***---***---***---***---***---***//

int ts;
int n;

const int mxN = 2e5+5;
vector<pair<ll, int>> cor;
vector<ll> hei;
ll bit[mxN];

void add(int ind, ll val) {
	for(++ind; ind <= 2*n; ind+=ind&-ind)
		bit[ind]+=val;
}

void update(int i1, int i2, ll val) {
	add(i1, val);
	add(i2+1, -val);
}

ll query(int ind) {
	ll sum=0;
	for(++ind; ind > 0; ind-=ind&-ind)
		sum+=bit[ind];
	return sum;
}

void solve() {
	read(n);
	cor.clear();
	memset(bit, 0, sizeof(bit));
	hei = vector<ll>(n);
	for(int i=0; i<n; ++i) {
		ll a,b,h;
		read(a,b,h);
		hei[i] = h;
		cor.push_back({a, i});
		cor.push_back({b, i});
	}
	n++;
	sort(cor.begin(), cor.end(), [](pair<ll, int> p1, pair<ll, int> p2)->bool {
			return p1.first < p2.first;
			});
	int cur = 0;
	for(int i=0; i < sz(cor); ++i) {
		if(i > 0 && cor[i].first != cor[i-1].first)
			++cur;
		cor[i].first = cur;
	}
	sort(cor.begin(), cor.end(), [](pair<ll, int> p1, pair<ll, int> p2)->bool {
			if(p1.second == p2.second)
				return p1.first < p2.first;
			else
				return p1.second < p2.second;
			});
	for(int i=0; i < sz(cor); i+=2) {
		update(cor[i].first, cor[i+1].first, hei[cor[i].second]);
	}
	int bitmask = 1<<17;
	ll ans = 0;
	while(bitmask > 0) {
		ans = max(ans, query(bitmask));
		bitmask >>= 1;
	}
	print(ans);
}

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

	//read(ts);
	//while(ts--)
		solve();
}


/**
 *	@file: C.cpp
 *	@author: Huy Le
 *	@created: 08-12-2020 11:34:01
**/

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

//***---***---***---***---***---***---BEGIN USELESS CODE---***---***---***---***---***---***//

#define ll long long
#define ld long double
const char newl = '\n';
#define sz(x) (int)(x).size()

template<class T> void read(vector<T>& vt);
template<class T>
void read(T& args) { cin >> args; }
template<class A, class... B >
void read(A& argc, B&... argv) {
	read(argc);
	read(argv...);
}
template<class T>
void read(vector<T>& vt) {
	for(auto &a : vt)
		read(a);
}

string to_string(bool bo) { return (bo ? "true" : "false"); }
string to_string(char c) { return string(1, c); }
string to_string(const char* str) { return string(str);}
string to_string(string str) { return str; }
template<class T>
string to_string(T vt) {
	bool ft = 1;
	string res;
	for(auto &a : vt) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += to_string(a);
	}
	return res;
}
string to_string(vector<bool> vt) {
	string res;
	bool ft = 1;
	for(int i = 0; i < (int)vt.size(); ++i) {
		if(!ft)
			res += ' ';
		ft = 0;
		res += char('0' + vt[i]);
	}
	return res;
}

template<class T>
void write(T args) { cout << to_string(args); }
template<class A, class... B>
void write(const A argc, const B... argv) {
	write(argc);
	write(argv...);
}

void print() { write(newl); }
template<class A, class... B>
void print(const A argc, const B... argv) {
	write(argc);
	if(sizeof...(argv))
		write(' ');
	print(argv...);
}

void DEBUGLOCAL() { cerr << ")" << newl; }
template<class A, class... B>
void DEBUGLOCAL(A argc, B... argv) {
	cerr << to_string(argc);
	if(sizeof...(argv))
		cerr << ", ";
	DEBUGLOCAL(argv...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") [" << __FUNCTION__ << "] -> (" << #__VA_ARGS__ << "): (",DEBUGLOCAL(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

//***---***---***---***---***---***---END USELESS CODE---***---***---***---***---***---***//

int ts;
int n;
const int mxN=1e5+5;
vector<ll> inner(mxN);
vector<pair<pair<ll,ll>,ll>> ring(mxN);
ll bit[mxN];

void update(int ind, ll val) {
	for(++ind; ind <= n+1; ind += ind&-ind)
		bit[ind] = max(bit[ind], val);
}

ll query(int ind) {
	ll sum = 0;
	for(; ind >0; ind -= ind&-ind)
		sum = max(sum, bit[ind]);
	return sum;
}

void solve() {
	read(n);
	memset(bit, 0, sizeof(bit));
	for(int i=0; i < n; ++i) {
		read(ring[i].first.first, ring[i].first.second, ring[i].second);
		inner[i]=ring[i].first.first;
	}
	sort(inner.begin(), inner.begin() + n);
	sort(ring.begin(), ring.begin() + n, [](pair<pair<ll,ll>,ll> p1, pair<pair<ll,ll>,ll> p2)->bool{
				if(p1.first.second == p2.first.second)
					return p1.first.first > p2.first.first;
				else
					return p1.first.second > p2.first.second;
			});
	ll ans = 0;
	for(int i=0 ; i < n; ++i) {
		int pos = lower_bound(inner.begin(), inner.begin() + n, ring[i].first.second) - inner.begin();
		ll cur = ring[i].second + query(pos);
		ans = max(ans, cur);
		pos = lower_bound(inner.begin(), inner.begin() + n, ring[i].first.first) - inner.begin();
		update(pos, cur);
	}
	print(ans);
}

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

	//read(ts);
	//while(ts--)
		solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z } }

vector<pair<int, pii>> vec;

mapii mp;
mapii rev;

const int N = 1e5 + 5;
ll tree[4 * N];

void update(int node, int st, int en, int shemal, int yemeen, ll val)
{
	if (st > en || st > yemeen || en < shemal)
		return;

	if (shemal <= st && en <= yemeen)
	{
		tree[node] += val;
		return;
	}

	int mid = (st + en) / 2;

	update(2 * node, st, mid, shemal, yemeen, val);
	update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
	tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll query(int node, int st, int en, int shemal, int yemeen)
{
	if (st > en || st > yemeen || en < shemal)
		return 0;

	if (shemal <= st && en <= yemeen)
		return tree[node];

	int mid = (st + en) / 2;

	return max(query(2 * node, st, mid, shemal, yemeen),
			query(2 * node + 1, mid + 1, en, shemal, yemeen));
}

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

	int n;
	cin >> n;
	vec.resize(n);
	set<int> st;
	int id = 0;
	rep(i, 0, n)
	{
		cin >> vec[i].second.first >> vec[i].first >> vec[i].second.second;
		st.insert(vec[i].first);
		st.insert(vec[i].second.first);
	}

	for (auto i : st)
	{
		rev[id] = i;
		mp[i] = id++;
	}

	sort(all(vec));

	ll mx = 0;

	rep(i, 0, n)
	{
		int idx = vec[i].first;
		ll val = vec[i].second.second;
		int a = vec[i].second.first, b = vec[i].first;
		ll res = val + query(1, 0, id - 1, mp[a], mp[b]);
//		cout << mp[a] << " " << mp[b] << " " << res << endl;
		mx = max(mx, res);
		update(1, 0, id - 1, mp[idx], mp[idx], res);
	}
	cout << mx;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z } }

vector<pair<int, pii>> vec;

mapii mp;

const int N = 3e5 + 5;
ll tree[4 * N];

void update(int node, int st, int en, int shemal, int yemeen, ll val)
{
	if (st > en || st > yemeen || en < shemal)
		return;

	if (shemal <= st && en <= yemeen)
	{
		tree[node] = max(tree[node], val);
		return;
	}

	int mid = (st + en) / 2;

	update(2 * node, st, mid, shemal, yemeen, val);
	update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
	tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll query(int node, int st, int en, int shemal, int yemeen)
{
	if (st > en || st > yemeen || en < shemal)
		return 0;

	if (shemal <= st && en <= yemeen)
		return tree[node];

	int mid = (st + en) / 2;

	return max(query(2 * node, st, mid, shemal, yemeen),
			query(2 * node + 1, mid + 1, en, shemal, yemeen));
}

bool cmp(pair<int, pii> a, pair<int, pii> b)
{
	if (a.first != b.first)
		return a.first > b.first;
	if (a.second.first != b.second.first)
		return a.second.first > b.second.first;

	return a.second.second < b.second.second;
}

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

	int n;
	cin >> n;
	vec.resize(n);
	set<int> st;
	int id = 1;
	rep(i, 0, n)
	{
		cin >> vec[i].second.first >> vec[i].first >> vec[i].second.second;
		st.insert(vec[i].first);
		st.insert(vec[i].second.first);
	}

	for (auto i : st)
		mp[i] = id++;

	sort(all(vec), cmp);

	ll mx = 0;

	rep(i, 0, n)
	{
		ll val = vec[i].second.second;
		int a = vec[i].second.first, b = vec[i].first;
//		cout << a << " " << b << endl;
		ll res = val + query(1, 0, id - 1, 0, mp[b]-1);

		mx = max(mx, res);
		update(1, 0, id - 1, mp[a], mp[a], res);
	}
	cout << mx;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z } }

vector<pair<int, pii>> vec;

mapii mp;
mapii rev;

const int N = 1e5 + 5;
ll tree[4 * N];

void update(int node, int st, int en, int shemal, int yemeen, ll val)
{
	if (st > en || st > yemeen || en < shemal)
		return;

	if (shemal <= st && en <= yemeen)
	{
		tree[node] = max(tree[node], val);
		return;
	}

	int mid = (st + en) / 2;

	update(2 * node, st, mid, shemal, yemeen, val);
	update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
	tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll query(int node, int st, int en, int shemal, int yemeen)
{
	if (st > en || st > yemeen || en < shemal)
		return 0;

	if (shemal <= st && en <= yemeen)
		return tree[node];

	int mid = (st + en) / 2;

	return max(query(2 * node, st, mid, shemal, yemeen),
			query(2 * node + 1, mid + 1, en, shemal, yemeen));
}

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

	int n;
	cin >> n;
	vec.resize(n);
	set<int> st;
	int id = 0;
	rep(i, 0, n)
	{
		cin >> vec[i].second.first >> vec[i].first >> vec[i].second.second;
		st.insert(vec[i].first);
		st.insert(vec[i].second.first);
	}

	for (auto i : st)
	{
		rev[id] = i;
		mp[i] = id++;
	}

	sort(all(vec));

	ll mx = 0;

	rep(i, 0, n)
	{
		int idx = vec[i].first;
		ll val = vec[i].second.second;
		int a = vec[i].second.first, b = vec[i].first;

		ll res = val + query(1, 0, id - 1, mp[a], mp[b]);
//		cout << val << " " << query(1, 0, id - 1, mp[a], mp[b]) << endl;
//		cout << mp[a] << " " << mp[b] << " " << res << endl;
		mx = max(mx, res);
		update(1, 0, id - 1, mp[idx], mp[idx], res);
	}
	cout << mx;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z } }

vector<pair<int, pii>> vec;

mapii mp;

const int N = 3e5 + 5;
ll tree[4 * N];

void update(int node, int st, int en, int shemal, int yemeen, ll val)
{
	if (st > en || st > yemeen || en < shemal)
		return;

	if (shemal <= st && en <= yemeen)
	{
		tree[node] = max(tree[node], val);
		return;
	}

	int mid = (st + en) / 2;

	update(2 * node, st, mid, shemal, yemeen, val);
	update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
	tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll query(int node, int st, int en, int shemal, int yemeen)
{
	if (st > en || st > yemeen || en < shemal)
		return 0;

	if (shemal <= st && en <= yemeen)
		return tree[node];

	int mid = (st + en) / 2;

	return max(query(2 * node, st, mid, shemal, yemeen),
			query(2 * node + 1, mid + 1, en, shemal, yemeen));
}

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

	int n;
	cin >> n;
	vec.resize(n);
	set<int> st;
	int id = 0;
	rep(i, 0, n)
	{
		cin >> vec[i].second.first >> vec[i].first >> vec[i].second.second;
		st.insert(vec[i].first);
		st.insert(vec[i].second.first);
	}

	for (auto i : st)
		mp[i] = id++;

	sort(all(vec));

	ll mx = 0;

	rep(i, 0, n)
	{
		int idx = vec[i].first;
		ll val = vec[i].second.second;
		int a = vec[i].second.first, b = vec[i].first;

		ll res = val + query(1, 0, id - 1, mp[a], mp[b] - 1);

		mx = max(mx, res);
		update(1, 0, id - 1, mp[idx], mp[idx], res);
	}
	cout << mx;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z } }

vector<pair<int, pii>> vec;

mapii mp;

const int N = 3e5 + 5;
ll tree[4 * N];

void update(int node, int st, int en, int shemal, int yemeen, ll val)
{
	if (st > en || st > yemeen || en < shemal)
		return;

	if (shemal <= st && en <= yemeen)
	{
		tree[node] = max(tree[node], val);
		return;
	}

	int mid = (st + en) / 2;

	update(2 * node, st, mid, shemal, yemeen, val);
	update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
	tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll query(int node, int st, int en, int shemal, int yemeen)
{
	if (st > en || st > yemeen || en < shemal)
		return 0;

	if (shemal <= st && en <= yemeen)
		return tree[node];

	int mid = (st + en) / 2;

	return max(query(2 * node, st, mid, shemal, yemeen),
			query(2 * node + 1, mid + 1, en, shemal, yemeen));
}

bool cmp(pair<int, pii> a, pair<int, pii> b)
{
	if (a.second.first != b.second.first)
		return a.second.first > b.second.first;

	if (a.first != b.first)
		return a.first > b.first;
	return a.second.second < b.second.second;
}

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

	int n;
	cin >> n;
	vec.resize(n);
	set<int> st;
	int id = 0;
	rep(i, 0, n)
	{
		cin >> vec[i].second.first >> vec[i].first >> vec[i].second.second;
		st.insert(vec[i].first);
		st.insert(vec[i].second.first);
	}

	for (auto i : st)
		mp[i] = id++;

	sort(all(vec), cmp);

	ll mx = 0;

	rep(i, 0, n)
	{
		ll val = vec[i].second.second;
		int a = vec[i].second.first, b = vec[i].first;

		ll res = val + query(1, 0, id - 1, 0, mp[b]-1);

		mx = max(mx, res);
		update(1, 0, id - 1, mp[a], mp[a], res);
	}
	cout << mx;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z } }

vector<pair<int, pii>> vec;

mapii mp;
mapii rev;

const int N = 3e5 + 5;
ll tree[4 * N];

void update(int node, int st, int en, int shemal, int yemeen, ll val)
{
	if (st > en || st > yemeen || en < shemal)
		return;

	if (shemal <= st && en <= yemeen)
	{
		tree[node] = max(tree[node], val);
		return;
	}

	int mid = (st + en) / 2;

	update(2 * node, st, mid, shemal, yemeen, val);
	update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
	tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll query(int node, int st, int en, int shemal, int yemeen)
{
	if (st > en || st > yemeen || en < shemal)
		return 0;

	if (shemal <= st && en <= yemeen)
		return tree[node];

	int mid = (st + en) / 2;

	return max(query(2 * node, st, mid, shemal, yemeen),
			query(2 * node + 1, mid + 1, en, shemal, yemeen));
}

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

	int n;
	cin >> n;
	vec.resize(n);
	set<int> st;
	int id = 0;
	rep(i, 0, n)
	{
		cin >> vec[i].second.first >> vec[i].first >> vec[i].second.second;
		st.insert(vec[i].first);
		st.insert(vec[i].second.first);
	}

	for (auto i : st)
	{
		rev[id] = i;
		mp[i] = id++;
	}

	sort(all(vec));

	ll mx = 0;

	rep(i, 0, n)
	{
		int idx = vec[i].first;
		ll val = vec[i].second.second;
		int a = vec[i].second.first, b = vec[i].first;

		ll res = val + query(1, 0, id - 1, mp[a], mp[b]);
//		cout << val << " " << query(1, 0, id - 1, mp[a], mp[b]) << endl;
//		cout << mp[a] << " " << mp[b] << " " << res << endl;
		mx = max(mx, res);
		update(1, 0, id - 1, mp[idx], mp[idx], res);
	}
	cout << mx;

	return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue <int> pq;
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

const ll mod = 1000000007;
ll po(ll x, ll n ){
	ll ans=1;
     while(n>0){
     	if(n&1) ans=(ans*x)%mod;
     	x=(x*x)%mod;
     	n/=2;
     }
     return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int T=1;
	//cin >> T;
	while(T--){
		ll n,a,b,h;
		cin>>n;
		vector<pair<ii,ll> >v;
		rep(i,n){
			cin>>a>>b>>h;
			v.pb(mp(mp(b,a),h));
		}
		sort(all(v));
		ll dp[n+1]={0};
		map<ll,ll>s;
		s.insert(mp(0,n));
		s.insert(mp(-1*v[n-1].ff.ss,n-1));
		dp[n]=0;
		dp[n-1]=v[n-1].ss;
		ll mx=v[n-1].ff.ss, c=n-1;
		// tr(s,h) cout<<h->ff<<" "<<h->ss<<el;
		// cout<<"||"<<el;
		rev(i,n-2){
			//cout<<v[i].ff.
			if(v[i].ff.ff>mx) dp[i]=dp[c]+v[i].ss;
			else{
				auto it=s.lower_bound(-1*v[i].ff.ff+1);
				dp[i]=dp[it->ss]+v[i].ss;
			}
			if(mx>=v[i].ff.ss) mx=v[i].ff.ss, c=i;
			s.insert(mp(-1*v[i].ff.ss,i));
			// tr(s,h) cout<<h->ff<<" "<<h->ss<<el;
			// cout<<"||"<<el;

		}
		mx=0;
		rep(i,n+1){
			if(dp[i]>mx) mx=dp[i];
			//cout<<dp[i]<<" ";
		}
		//cout<<el;
		cout<<mx<<el;
	}

	return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it++)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue <int> pq;
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

const ll mod = 1000000007;
ll po(ll x, ll n ){
	ll ans=1;
     while(n>0){
     	if(n&1) ans=(ans*x)%mod;
     	x=(x*x)%mod;
     	n/=2;
     }
     return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int T=1;
	//cin >> T;
	while(T--){
		ll n,a,b,c,h;
		cin>>n;
		vector<pair<ii,ll> >v;
		rep(i,n){
			cin>>a>>b>>h;
			v.pb(mp(mp(b,a),h));
		}
		sort(all(v));
		map<ll,ll>m;
		ll dp[n+1]={0};
		dp[n]=0;
		dp[n-1]=v[n-1].ss;
		m.insert(mp(0,n));
		m.insert(mp(-1*v[n-1].ff.ss, n-1));
		// tr(m,h) cout<<h->ff<<" ";
		// cout<<el;
		auto it =m.begin();
		rev(i,n-2){
			auto it1=m.lower_bound(-1*v[i].ff.ff+1);
			//cout<<it1->ff<<el;
			c=it1->ss;
			dp[i]=dp[c]+v[i].ss;
			tr(m,h){
				if(h->ff>(-1)*v[i].ff.ss){
					it=h;
					break;
				}
			}
			//cout<<it->ff<<"{{"<<el;
			if(it!=m.begin()) m.erase(m.begin(),it);
			m.insert(mp(-1*v[i].ff.ss, i));
			// tr(m,h) cout<<h->ff<<" ";
			// cout<<el;
		}
		ll mx=0;
		rep(i,n+1){
			if(dp[i]>mx) mx=dp[i];
			//cout<<dp[i]<<" ";
		}
		//cout<<el;
		cout<<mx<<el;
	}

	return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it++)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue <int> pq;
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

const ll mod = 1000000007;
ll po(ll x, ll n ){
	ll ans=1;
     while(n>0){
     	if(n&1) ans=(ans*x)%mod;
     	x=(x*x)%mod;
     	n/=2;
     }
     return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int T=1;
	//cin >> T;
	while(T--){
		ll n,a,b,c,h;
		cin>>n;
		vector<pair<ii,ll> >v;
		rep(i,n){
			cin>>a>>b>>h;
			v.pb(mp(mp(b,a),h));
		}
		sort(all(v));
		map<ll,ll>m;
		ll dp[n+1]={0};
		dp[n]=0;
		dp[n-1]=v[n-1].ss;
		m.insert(mp(0,n));
		m.insert(mp(v[n-1].ff.ss, n-1));
		rev(i,n-2){
			auto it=m.lower_bound(v[i].ff.ff);
			it--;
			c=it->ss;
			dp[i]=dp[c]+v[i].ss;
			rtr(m,h){
				if(h->ff<v[i].ff.ss){
					break;
				}
				else{
					m.erase(--h.base());
				}
			}
			m.insert(mp(v[i].ff.ss, i));
		}
		ll mx=0;
		rep(i,n+1){
			if(dp[i]>mx) mx=dp[i];
			//cout<<dp[i]<<" ";
		}
		cout<<mx<<el;
	}

	return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it++)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef priority_queue <int> pq;
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

const ll mod = 1000000007;
ll po(ll x, ll n ){
	ll ans=1;
     while(n>0){
     	if(n&1) ans=(ans*x)%mod;
     	x=(x*x)%mod;
     	n/=2;
     }
     return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int T=1;
	//cin >> T;
	while(T--){
		ll n,a,b,c,h;
		cin>>n;
		vector<pair<ii,ll> >v;
		rep(i,n){
			cin>>a>>b>>h;
			v.pb(mp(mp(b,a),h));
		}
		sort(all(v));
		map<ll,ll>m;
		ll dp[n+1]={0};
		dp[n]=0;
		dp[n-1]=v[n-1].ss;
		m.insert(mp(0,n));
		m.insert(mp(v[n-1].ff.ss, n-1));
		// tr(m,h) cout<<h->ff<<" ";
		// cout<<el;
		rev(i,n-2){
			auto it=m.lower_bound(v[i].ff.ff);
			it--;
			c=it->ss;
			dp[i]=dp[c]+v[i].ss;
			rtr(m,h){
				//cout<<"||"<<h->ff<<el;
				if(h->ff<v[i].ff.ss){
					break;
				}
				else{
					auto it=h.base();
					it--;
					//cout<<it->ff<<"||"<<el;
					m.erase(it);
					--h;
					// tr(m,h1) cout<<h1->ff<<" ";
					// cout<<el;
				}
			}
			m.insert(mp(v[i].ff.ss, i));
			// tr(m,h) cout<<h->ff<<" ";
			// cout<<el;
		}
		ll mx=0;
		rep(i,n+1){
			if(dp[i]>mx) mx=dp[i];
			//cout<<dp[i]<<" ";
		}
		//cout<<el;
		cout<<mx<<el;
	}

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;
ifstream fin(".in");
ofstream fout(".out");

vector < pair < pair <ll int,ll int>,ll int > > v;
set < pair<ll int,ll int> > myset;
set < pair<ll int,ll int> > :: iterator it, it2;

ll int n, a, b, c;

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

        cin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.lower_bound(mp(-v[i].x.x, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   cout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;

vector < pair < pair <int, int>, int > > v;
set < pair<int, int> > myset;
set < pair<int, int> > :: iterator it, it2;

int n, a, b, c;

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<40);
    for(int i = 0; i < n; i++)
    {
        it = myset.lower_bound(mp(-v[i].x.x, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   cout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;

vector < pair < pair <int, int>, int > > v;
set < pair<int, int> > myset;
set < pair<int, int> > :: iterator it, it2;

int n, a, b, c;

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.upper_bound(mp(-v[i].x.x+1, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   cout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;
ifstream fin(".in");
ofstream fout(".out");

vector < pair < pair <ll int,ll int>,ll int > > v;
set < pair<ll int,ll int> > myset;
set < pair<ll int,ll int> > :: iterator it, it2;

ll int n, a, b, c;

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
    {

        fin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.upper_bound(mp(-v[i].x.x-1, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   fout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;

vector < pair < pair <int, int>, int > > v;
set < pair<int, int> > myset;
set < pair<int, int> > :: iterator it, it2;

long long int n, a, b, c;

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.lower_bound(mp(-v[i].x.x, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   cout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;
ifstream fin(".in");
ofstream fout(".out");

vector < pair < pair <ll int,ll int>,ll int > > v;
set < pair<ll int,ll int> > myset;
set < pair<ll int,ll int> > :: iterator it, it2;

ll int n, a, b, c;

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
    {

        fin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.lower_bound(mp(-v[i].x.x, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   fout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;

vector < pair < pair <ll int,ll int>,ll int > > v;
set < pair<ll int,ll int> > myset;
set < pair<ll int,ll int> > :: iterator it, it2;

ll int n, a, b, c;

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

        cin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.upper_bound(mp(-v[i].x.x-1, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   cout << myset.rbegin()->second;

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
#define ll long long

using namespace std;

vector < pair < pair <int, int>, int > > v;
set < pair<int, int> > myset;
set < pair<int, int> > :: iterator it, it2;

int n, a, b, c;

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        v.push_back(mp(mp(-b, -a), c));

    }

    sort(v.begin(), v.end());
    myset.insert(mp(0, 0));

    ll int big = (1ll<<60);
    for(int i = 0; i < n; i++)
    {
        it = myset.upper_bound(mp(-v[i].x.x-1, big));
        it--;
        a = it->second + v[i].y;

        it = myset.insert(mp(-v[i].x.y, a)).x;
        --it;
        if(it->second >= a)
        {
            it++;
            myset.erase(it);
            continue;

        }
        it++;
        it++;

        while(it!=myset.end() && it->second <= a)
        {
            it2 = it;
            it++;
            myset.erase(it2);
        }

    }

   cout << myset.rbegin()->second;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , res , n;
bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
main(){
      stack < int > s;
      cin >> n;
      for(int i = 1; i <= n; i ++) cin >> a[i].in >> a[i].out >> a[i].he;
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            while(!s.empty() && a[s.top()].in >= a[i].out) s.pop();
            if(!s.empty())
                  dp[i] = dp[s.top()];
            dp[i] += a[i].he;
            s.push(i);
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , res , n;
bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
int main(){
      stack < int > s;
      cin >> n;
      for(int i = 1; i <= n; i ++) cin >> a[i].in >> a[i].out >> a[i].he;
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            while(!s.empty() && a[s.top()].in >= a[i].out) s.pop();
            if(!s.empty())
                  dp[i] = dp[s.top()];
            dp[i] += a[i].he;
            s.push(i);
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10 , INF = 1e9;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , n , res;
map < int , int > S;
bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
bool ok(int x , int y){
      return (a[x].out > a[y].in && a[x].out <= a[y].out);
}
void upd(int l , int r , int x , int v , int p){
      if(l > x || r < x) return;
      if(l == r){
            S[p] = max(v , S[p]);
            return;
      }
      int m = (l + r) >> 1;
      upd(l , m , x , v , p << 1);
      upd(m + 1 , r , x , v , p << 1 | 1);
      S[p] = max(S[p << 1] , S[p << 1 | 1]);
}
int query(int l , int r , int L , int R , int p){
      if(l > R || L > r) return 0;
      int m = (l + r) >> 1;
      return (L <= l && R >= r) ? S[p] : max(query(l , m , L , R , p << 1) , query(m + 1 , r , L , R , p << 1 | 1));
}
main(){
      ios::sync_with_stdio(0); cin.tie(0);
      cin >> n;
      for(int i = 1; i <= n; i ++){
            cin >> a[i].in >> a[i].out >> a[i].he;
      }
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            dp[i] = a[i].he;
            dp[i] = max(dp[i] , query(1 , INF , 1 , a[i].out - 1 , 1) + a[i].he);
            upd(1 , INF , a[i].in , dp[i] , 1);
      }
      for(int i = 1; i <= n; i ++){
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10 , INF = 1e9;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , n , res;
map < int , int > S;
inline bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
inline void upd(int l , int r , int x , int v , int p){
      if(l > x || r < x) return;
      if(l == r){
            S[p] = max(v , S[p]);
            return;
      }
      int m = (l + r) >> 1;
      upd(l , m , x , v , p << 1);
      upd(m + 1 , r , x , v , p << 1 | 1);
      S[p] = max(S[p << 1] , S[p << 1 | 1]);
}
inline int query(int l , int r , int L , int R , int p){
      if(l > R || L > r) return 0;
      int m = (l + r) >> 1;
      return (L <= l && R >= r) ? S[p] : max(query(l , m , L , R , p << 1) , query(m + 1 , r , L , R , p << 1 | 1));
}
main(){
      cin >> n;
      for(int i = 1; i <= n; i ++){
            scanf("%d%d%d" , &a[i].in , &a[i].out , &a[i].he);
      }
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            dp[i] = a[i].he;
            dp[i] = max(dp[i] , query(1 , INF , 1 , a[i].out - 1 , 1) + a[i].he);
            upd(1 , INF , a[i].in , dp[i] , 1);
      }
      for(int i = 1; i <= n; i ++){
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10 , INF = 1e9;
struct str{
      int in , out , he;
};
str a[N];
long long dp[N] , n , res;
map < int , long long > S;
bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
bool ok(int x , int y){
      return (a[x].out > a[y].in && a[x].out <= a[y].out);
}
void upd(int l , int r , int x , int v , int p){
      if(l > x || r < x) return;
      if(l == r){
            S[p] = max(1ll*v , S[p]);
            return;
      }
      int m = (l + r) >> 1;
      upd(l , m , x , v , p << 1);
      upd(m + 1 , r , x , v , p << 1 | 1);
      S[p] = max(S[p << 1] , S[p << 1 | 1]);
}
long long query(int l , int r , int L , int R , int p){
      if(l > R || L > r) return 0;
      int m = (l + r) >> 1;
      return (L <= l && R >= r) ? S[p] : max(query(l , m , L , R , p << 1) , query(m + 1 , r , L , R , p << 1 | 1));
}
int main(){
      ios::sync_with_stdio(0); cin.tie(0);
      cin >> n;
      for(int i = 1; i <= n; i ++){
            cin >> a[i].in >> a[i].out >> a[i].he;
      }
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            dp[i] = a[i].he;
            dp[i] = max(dp[i] , query(1 , INF , 1 , a[i].out - 1 , 1) + 1ll*a[i].he);
            upd(1 , INF , a[i].in , dp[i] , 1);
      }
      for(int i = 1; i <= n; i ++){
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , n , res;
map < int , int > S;
bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
bool ok(int x , int y){
      return (a[x].out > a[y].in && a[x].out <= a[y].out);
}
void upd(int l , int r , int x , int v , int p){
      if(l > x || r < x) return;
      if(l == r){
            S[p] = max(v , S[p]);
            return;
      }
      int m = (l + r) >> 1;
      upd(l , m , x , v , p << 1);
      upd(m + 1 , r , x , v , p << 1 | 1);
      S[p] = max(S[p << 1] , S[p << 1 | 1]);
}
int query(int l , int r , int L , int R , int p){
      if(l > R || L > r) return 0;
      int m = (l + r) >> 1;
      return (L <= l && R >= r) ? S[p] : max(query(l , m , L , R , p << 1) , query(m + 1 , r , L , R , p << 1 | 1));
}
int main(){
      ios::sync_with_stdio(0); cin.tie(0);
      cin >> n;
      for(int i = 1; i <= n; i ++){
            cin >> a[i].in >> a[i].out >> a[i].he;
      }
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            dp[i] = a[i].he;
            dp[i] = max(dp[i] , query(1 , n , 1 , a[i].out - 1 , 1) + a[i].he);
            upd(1 , n , a[i].in , dp[i] , 1);
      }
      for(int i = 1; i <= n; i ++){
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10 , INF = 2e5 + 100;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , n , res , S[4 * INF];
inline bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
inline void upd(int l , int r , int x , int v , int p){
      if(l > x || r < x) return;
      if(l == r){
            S[p] = max(v , S[p]);
            return;
      }
      int m = (l + r) >> 1;
      upd(l , m , x , v , p << 1);
      upd(m + 1 , r , x , v , p << 1 | 1);
      S[p] = max(S[p << 1] , S[p << 1 | 1]);
}
inline int query(int l , int r , int L , int R , int p){
      if(l > R || L > r) return 0;
      int m = (l + r) >> 1;
      return (L <= l && R >= r) ? S[p] : max(query(l , m , L , R , p << 1) , query(m + 1 , r , L , R , p << 1 | 1));
}
main(){
      cin >> n;
      for(int i = 1; i <= n; i ++){
            scanf("%d%d%d" , &a[i].in , &a[i].out , &a[i].he);
      }
      vector < pair < int , pair < int , int > > > b;
      sort(a + 1 , a + n + 1);
      int cnt = 0;
      for(int i = 1; i <= n; i ++) {
            b.push_back({a[i].in , {i , 1}});
            b.push_back({a[i].out , {i , 2}});
      }
      sort(b.begin() , b.end());
      for(int i = 0; i < b.size(); i ++){
            if(b[i - 1].first != b[i].first || !i){
                  cnt ++;
            }
            if(b[i].second.second == 1)a[b[i].second.first].in = cnt;
            else a[b[i].second.first].out = cnt;
      }
      for(int i = 1; i <= n; i ++){
            dp[i] = a[i].he;
            dp[i] = max(dp[i] , query(1 , INF , 1 , a[i].out - 1 , 1) + a[i].he);
            upd(1 , INF , a[i].in , dp[i] , 1);
      }
      for(int i = 1; i <= n; i ++){
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10 , INF = 1e9;
struct str{
      int in , out , he;
};
str a[N];
int dp[N] , n , res;
map < int , int > S;
bool operator < (str a , str b){
      if(a.out == b.out){
            if(a.in == b.in){
                  return a.he > b.he;
            }
            return a.in > b.in;
      }
      return a.out > b.out;
}
bool ok(int x , int y){
      return (a[x].out > a[y].in && a[x].out <= a[y].out);
}
void upd(int l , int r , int x , int v , int p){
      if(l > x || r < x) return;
      if(l == r){
            S[p] = max(v , S[p]);
            return;
      }
      int m = (l + r) >> 1;
      upd(l , m , x , v , p << 1);
      upd(m + 1 , r , x , v , p << 1 | 1);
      S[p] = max(S[p << 1] , S[p << 1 | 1]);
}
int query(int l , int r , int L , int R , int p){
      if(l > R || L > r) return 0;
      int m = (l + r) >> 1;
      return (L <= l && R >= r) ? S[p] : max(query(l , m , L , R , p << 1) , query(m + 1 , r , L , R , p << 1 | 1));
}
int main(){
      ios::sync_with_stdio(0); cin.tie(0);
      cin >> n;
      for(int i = 1; i <= n; i ++){
            cin >> a[i].in >> a[i].out >> a[i].he;
      }
      sort(a + 1 , a + n + 1);
      for(int i = 1; i <= n; i ++){
            dp[i] = a[i].he;
            dp[i] = max(dp[i] , query(1 , INF , 1 , a[i].out - 1 , 1) + a[i].he);
            upd(1 , INF , a[i].in , dp[i] , 1);
      }
      for(int i = 1; i <= n; i ++){
            res = max(res , dp[i]);
      }
      cout << res << "\n";
}


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

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define fi first
#define se second

unordered_map<int, int> aind;
// tree on interval 0..n-1
// before usage set MAXX, n and call init()
// inc increments values on interval [aw,bw] by x
// getmin returns minimum on interval [aw,bw]
// MAXX should be 2 * (next power of two after n) + 10
// n should be a power of 2

#define MAXX 524298
#define INF 1000 * 1000 * 1000

ll incr[MAXX], maxx[MAXX]; int n;
void init() { FOR(i, MAXX) incr[i] = maxx[i] = 0; }
int aw, bw, x; // temp
ll arrayResult[MAXX];

void emptyNode(int v) {
	incr[v] = 0;
}

void merge(int v, int val) {
	incr[v] = val;
}

void applyAll2(int v, int a, int b) {
	if (b - a == 0) {
		arrayResult[a] = incr[v];
		return;
	}
	else {
		int mid = (a + b) / 2;
		merge(2 * v, incr[v]);
		merge(2 * v + 1, incr[v]);
		emptyNode(v);

		applyAll2(2 * v, a, mid);
		applyAll2(2 * v + 1, mid + 1, b);
		maxx[v] = max(maxx[2 * v], maxx[2 * v + 1]);
	}
}

void applyAll() {
	applyAll2(1, 0, n - 1);
}

void inc2(int v, int a, int b) {
	if (aw > b || bw < a) return;
	if (a >= aw && b <= bw) {
		merge(v, x);
		maxx[v] = x;
		if (b - a == 0) arrayResult[a] = incr[v];
	}
	else {
		int mid = (a + b) / 2;
		merge(2 * v, incr[v]);
		merge(2 * v + 1, incr[v]);
		emptyNode(v);

		inc2(2 * v, a, mid);
		inc2(2 * v + 1, mid + 1, b);
		maxx[v] = max(maxx[2 * v], maxx[2 * v + 1]);
	}
}

void inc(int _aw, int _bw, int _x) {
	aw = _aw; bw = _bw; x = _x;
	inc2(1, 0, n - 1);
}

ll getmax(int v, int a, int b) {
	if (aw > b || bw < a) return -1 * ((ll)INF) * INF;
	if (a >= aw && b <= bw) {
		return maxx[v];
	}
	else {
		int mid = (a + b) / 2;

		ll left = getmax(2 * v, a, mid);
		ll right = getmax(2 * v + 1, mid + 1, b);
		return incr[v] + max(left, right);
	}
}

ll getmax(int _aw, int _bw) {
	aw = _aw; bw = _bw;
	return getmax(1, 0, n - 1);
}

int main() {
	n = 131072;
	init();

	int N;
	scanf("%d", &N);

	vector<ll> A;
	vector<pair<ll, pair<ll, ll>>> B;

	for (int i = 0; i < N; i++) {
		int a, b, h;
		scanf("%d %d %d", &a, &b, &h);

		A.push_back(a);
		B.push_back(make_pair((ll)b, make_pair((ll)h, (ll)a)));
	}
	sort(A.begin(), A.end());
	for (int i = 0; i < N; i++) {
		if (aind.count(A[i]) > 0) continue;
		aind[A[i]] = i;
	}

	sort(B.begin(), B.end());
	reverse(B.begin(), B.end());

	for (int i = 0; i < N; i++) {
		int atar = lower_bound(A.begin(), A.end(), B[i].first) - A.begin();
		if (atar == 0) {
			int atemp = B[i].second.second;
			inc(aind[atemp], aind[atemp], max(arrayResult[atemp], B[i].second.first));
		} else {
			atar--;

			ll maxh = getmax(0, atar);

			int atemp = B[i].second.second;
			inc(aind[atemp], aind[atemp], max(arrayResult[atemp], maxh + B[i].second.first));
		}
	}

	ll ans = *max_element(arrayResult, arrayResult + N);
	printf("%lld", ans);

	return 0;
}