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

const int MOD=1e9+9;
const int MAXN=1e5+5;
typedef long long ll;

char qwq[MAXN];
int s[MAXN],n,a,b,k;
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1)(ans*=base)%=MOD;
		(base*=base)%=MOD;
		b>>=1;
	}
	return ans;
}

int main(){
	scanf("%d%d%d%d",&n,&a,&b,&k);
	ll bi=qpow(b,k)*qpow(qpow(a,k),MOD-2)%MOD;
	scanf("%s",qwq);
	for(int i=1;i<=k;i++){
		s[i]=qwq[i-1]=='+'?1:-1;
	}
	if(bi==1){
		ll base=qpow(a,n),ans=0,tmp=0;
		for(int i=1;i<=k;i++)if(s[i])tmp++;else tmp--;
		ans=tmp*base;
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		ans=ans*lun;ans%=MOD;ans+=MOD;ans%=MOD;
		for(int i=qidian;i<=n;i++){
			ans+=s[++cnt]*base;
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		ans+=MOD;ans%=MOD;
		cout<<ans<<endl;
		return 0;
	}
	else {
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		cnt=n-qidian+1;
		ll ans=0;
		for(int i=0;i<k;i++){
			ans+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		//ll sum=ans*(1-qpow())
		ll sum=ans*(1-qpow(bi,lun))%MOD*qpow(1-bi,MOD-2)%MOD;
		sum+=MOD;sum%=MOD;
		for(int i=1;i<=cnt;i++){
			sum+=s[i+1]*qpow(a,n-qidian)*qpow(b,qidian);
			sum%=MOD;sum+=MOD;sum%=MOD;
			qidian++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
/*
*/

// (b/a)^k +


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

const int MOD=1e9+9;
const int MAXN=1e5+5;
typedef long long ll;

char qwq[MAXN];
int s[MAXN],n,a,b,k;
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1)(ans*=base)%=MOD;
		(base*=base)%=MOD;
		b>>=1;
	}
	return ans;
}

int main(){
	scanf("%d%d%d%d",&n,&a,&b,&k);
	ll bi=qpow(b,k)*qpow(qpow(a,k),MOD-2)%MOD;
	scanf("%s",qwq);
	for(int i=1;i<=k;i++){
		s[i]=qwq[i-1]=='+'?1:-1;
	}
	if(bi==1){
		ll base=qpow(a,n),ans=0;
		for(int i=1;i<=k;i++){
			ans+=s[i]*base;ans+=MOD;ans%=MOD;
		}
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		ans=ans*lun;ans+=MOD;ans%=MOD;
		for(int i=qidian;i<=n;i++){
			ans+=s[++cnt]*base;
			ans+=MOD;ans%=MOD;
		}
		ans+=MOD;ans%=MOD;
		cout<<ans<<endl;
		return 0;
	}
	else {
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		cnt=n-qidian+1;
		ll ans=0;
		for(int i=0;i<k;i++){
			ans+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		//ll sum=ans*(1-qpow())
		ll sum=ans*(1-qpow(bi,lun))%MOD*qpow(1-bi,MOD-2)%MOD;
		sum+=MOD;sum%=MOD;
		for(int i=1;i<=cnt;i++){
			sum+=s[i+1]*qpow(a,n-qidian)*qpow(b,qidian);
			sum%=MOD;sum+=MOD;sum%=MOD;
			qidian++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
/*
*/

// (b/a)^k +


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

const int MOD=1e9+9;
const int MAXN=1e5+5;
typedef long long ll;

char qwq[MAXN];
int s[MAXN],n,a,b,k;
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1)(ans*=base)%=MOD;
		(base*=base)%=MOD;
		b>>=1;
	}
	return ans;
}

int main(){
	scanf("%d%d%d%d",&n,&a,&b,&k);
	ll bi=qpow(b,k)*qpow(qpow(a,k),MOD-2)%MOD;
	scanf("%s",qwq);
	for(int i=1;i<=k;i++){
		s[i]=qwq[i-1]=='+'?1:-1;
	}
	if(bi==1){
		ll base=qpow(a,n),ans=0,tmp=0;
		for(int i=1;i<=k;i++)if(s[i]==1)tmp++;else tmp--;
		ans=tmp*base;
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		ans=ans*lun;ans%=MOD;ans+=MOD;ans%=MOD;
		for(int i=qidian;i<=n;i++){
			ans+=s[++cnt]*base;
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		ans+=MOD;ans%=MOD;
		cout<<ans<<endl;
		return 0;
	}
	else {
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		cnt=n-qidian+1;
		ll ans=0;
		for(int i=0;i<k;i++){
			ans+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		//ll sum=ans*(1-qpow())
		ll sum=ans*(1-qpow(bi,lun))%MOD*qpow(1-bi,MOD-2)%MOD;
		sum+=MOD;sum%=MOD;
		for(int i=1;i<=cnt;i++){
			sum+=s[i+1]*qpow(a,n-qidian)*qpow(b,qidian);
			sum%=MOD;sum+=MOD;sum%=MOD;
			qidian++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
/*
*/

// (b/a)^k +


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

const int MOD=1e9+9;
const int MAXN=1e5+5;
typedef long long ll;

char qwq[MAXN];
int s[MAXN],n,a,b,k;
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1)(ans*=base)%=MOD;
		(base*=base)%=MOD;
		b>>=1;
	}
	return ans;
}

int main(){
	scanf("%d%d%d%d",&n,&a,&b,&k);
	scanf("%s",qwq);
	for(int i=1;i<=k;i++){
		s[i]=qwq[i-1]=='+'?1:-1;
	}
	if((a==b)||((a==-b)&&(k%2==0))){
		ll base=qpow(a,n),ans=0;
		for(int i=1;i<=k;i++){
			ans+=s[i]*base;ans+=MOD;ans%=MOD;
		}
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		ans=ans*lun;ans+=MOD;ans%=MOD;
		for(int i=qidian;i<=n;i++){
			ans+=s[++cnt]*base;
			ans+=MOD;ans%=MOD;
		}
		ans+=MOD;ans%=MOD;
		cout<<ans<<endl;
		return 0;
	}
	else {
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		cnt=n-qidian+1;
		ll ans=0;
		for(int i=0;i<k;i++){
			ans+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		//ll sum=ans*(1-qpow())
		ll bi=qpow(b,k)*qpow(qpow(a,k),MOD-2)%MOD;
		ll sum=ans*(1-qpow(bi,lun))%MOD*qpow(1-bi,MOD-2)%MOD;
		sum+=MOD;sum%=MOD;
		for(int i=1;i<=cnt;i++){
			sum+=s[i+1]*qpow(a,n-qidian)*qpow(b,qidian);
			sum%=MOD;sum+=MOD;sum%=MOD;
			qidian++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
/*
*/

// (b/a)^k +


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

const int MOD=1e9+9;
const int MAXN=1e5+5;
typedef long long ll;

char qwq[MAXN];
int s[MAXN],n,a,b,k;
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1)(ans*=base)%=MOD;
		(base*=base)%=MOD;
		b>>=1;
	}
	return ans;
}

int main(){
	scanf("%d%d%d%d",&n,&a,&b,&k);
	ll bi=qpow(b,k)*qpow(qpow(a,k),MOD-2)%MOD;
	scanf("%s",qwq);
	for(int i=1;i<=k;i++){
		s[i]=qwq[i-1]=='+'?1:-1;
	}
	if(b==1){
		ll base=qpow(a,n),ans=0;
		for(int i=1;i<=k;i++){
			ans+=s[i]*base;ans+=MOD;ans%=MOD;
		}
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		ans=ans*lun;ans+=MOD;ans%=MOD;
		for(int i=qidian;i<=n;i++){
			ans+=s[++cnt]*base;
			ans+=MOD;ans%=MOD;
		}
		ans+=MOD;ans%=MOD;
		cout<<ans<<endl;
		return 0;
	}
	else {
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		cnt=n-qidian+1;
		ll ans=0;
		for(int i=0;i<k;i++){
			ans+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		//ll sum=ans*(1-qpow())
		ll sum=ans*(1-qpow(bi,lun))%MOD*qpow(1-bi,MOD-2)%MOD;
		sum+=MOD;sum%=MOD;
		for(int i=1;i<=cnt;i++){
			sum+=s[i+1]*qpow(a,n-qidian)*qpow(b,qidian);
			sum%=MOD;sum+=MOD;sum%=MOD;
			qidian++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
/*
*/

// (b/a)^k +


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

const int MOD=1e9+9;
const int MAXN=1e5+5;
typedef long long ll;

char qwq[MAXN];
int s[MAXN],n,a,b,k;
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1)(ans*=base)%=MOD;
		(base*=base)%=MOD;
		b>>=1;
	}
	return ans;
}

int main(){
	scanf("%d%d%d%d",&n,&a,&b,&k);
	ll bi=qpow(b,k)*qpow(qpow(a,k),MOD-2)%MOD;
	scanf("%s",qwq);
	for(int i=1;i<=k;i++){
		s[i]=qwq[i-1]=='+'?1:-1;
	}
	if(bi==1){
		ll base=0,ans=0,tmp=0;
		for(int i=0;i<k;i++){
			base+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			base%=MOD;base+=MOD;base%=MOD;
		}
		ans=base;
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		ans=ans*lun;ans%=MOD;ans+=MOD;ans%=MOD;
		for(int i=qidian;i<=n;i++){
			ans+=s[++cnt]*base;
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		ans+=MOD;ans%=MOD;
		cout<<ans<<endl;
		return 0;
	}
	else {
		int lun=(n+1)/k,qidian=lun*k,cnt=0;
		cnt=n-qidian+1;
		ll ans=0;
		for(int i=0;i<k;i++){
			ans+=s[i+1]*qpow(a,n-i)*qpow(b,i);
			ans%=MOD;ans+=MOD;ans%=MOD;
		}
		//ll sum=ans*(1-qpow())
		ll sum=ans*(1-qpow(bi,lun))%MOD*qpow(1-bi,MOD-2)%MOD;
		sum+=MOD;sum%=MOD;
		for(int i=1;i<=cnt;i++){
			sum+=s[i+1]*qpow(a,n-qidian)*qpow(b,qidian);
			sum%=MOD;sum+=MOD;sum%=MOD;
			qidian++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
/*
621922027 16665164 173157 4676
*/

// (b/a)^k +


#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#define ll long long
using namespace std;
const int mod = 1e9+9;
const int maxn = 1e5+7;

char s[maxn];
ll fac;
ll quickpow(ll x,ll n){
    ll ans = 1;
    while(n){
        if(n & 1)
            ans = (ans * x) % mod;
        x = (x * x) % mod;
        n>>=1;
    }
    return ans;
}

ll cal(int st,int ed){
    ll ans = 0;
    ll x = quickpow(fac,st);
    for(int i=st;i<=ed;i++){
        ans = ( (ans + s[i] * x ) % mod + mod ) % mod;
        x = x * fac % mod;
    }
    return ans;
}
int main(){

    ll n,a,b,k;
    scanf("%lld %lld %lld %lld",&n,&a,&b,&k);
    scanf("%s",s);
    for(int i=0;s[i];i++){
        if(s[i] == '+')
            s[i]=1;
        else
            s[i]=-1;
    }
    fac = b * quickpow(a,mod-2) % mod;
    if( n<k ){
        printf("%lld\n",cal(0,n) * quickpow(a,n) % mod);
        return 0;
    }
    ll pre = cal(0,k-1);
    // printf("pre = %lld\n",pre * quickpow(a,n) % mod);
    ll period = n/k,geom,base;
    // printf("period=%lld\n",period);
    base = quickpow(fac,k);
    if(base==1)
        geom = period;
    else
        geom = ( quickpow(base,period) + mod -1) % mod * quickpow( (base+mod-1) % mod , mod-2 ) % mod;
    // printf("%lld\n",geom);
    pre = (pre * geom) % mod;
    ll ans = ( pre + quickpow(fac,n/k*k) * cal(0,n-n/k*k) % mod ) % mod;
    ans = ans * quickpow(a,n) % mod;
    printf("%lld\n",ans);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P 1000000009
int Pow(int x,int y)
{
	int s=1;
	for(;y;y>>=1,x=(ll)x*x%P)if(y&1)s=(ll)s*x%P;
	return s;
}
int n,a,b,x,ans,m,i,j;
char c[100005];
int main()
{
	scanf("%d%d%d%d%s",&n,&a,&b,&m,c);
	x=(ll)b*Pow(a,P-2)%P;
	for(i=0,j=1;i<m;i++,j=(ll)j*x%P)if(c[i]=='+')ans=(ans+j)%P;
	else ans=(ans-j+P)%P;
	ans=(ll)ans*Pow(a,n)%P;
	if(j==1)ans=(ll)ans*(n+1)/m%P;
	else ans=(ll)ans*(Pow(x,n+1)-1)%P*Pow(j-1,P-2)%P;
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P 1000000009
int Pow(int x,int y)
{
	int s=1;
	for(;y;y>>=1,x=(ll)x*x%P)if(y&1)s=(ll)s*x%P;
	return s;
}
int n,a,b,x,ans,m,i,j;
char c[100005];
int main()
{
	scanf("%d%d%d%d%s",&n,&a,&b,&m,c);
	x=(ll)b*Pow(a,P-2)%P;
	for(i=0,j=1;i<m;i++,j=(ll)j*x%P)if(c[i]=='+')ans=(ans+j)%P;
	else ans=(ans-j+P)%P;
	ans=(ll)ans*Pow(a,n)%P;
	cout<<j<<endl;
	cout
	if(j==1)ans=(ll)ans*(n+1)/m%P;
	else ans=(ll)ans*(Pow(x,n+1)-1)%P*Pow(j-1,P-2)%P;
	cout<<ans<<endl;
	return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z);
	for (ll i=0; i<k; i++) {
		s=(s + (z[i]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=ksm(ksm(a,mo-2)*b%mo,k);
	ll ans=(ksm(ab,h)*s%mo-s)%mo * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z);
	for (ll i=0; i<k; i++) {
		s=(s + (z[i]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=ksm(a,mo-2)*b%mo;
	ll ans=(ksm(ab,h)*s%mo-s)%mo * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	x%=mo;
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z);
	for (ll i=0; i<k; i++) {
		s=(s + (z[i]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=ksm(ksm(a,mo-2)*b%mo,k);
	ll ans=s * (ksm(ab,h)%mo-1)%mo * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	x%=mo;
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z);
	for (ll i=0; i<k; i++) {
		s=(s + (z[i]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=ksm(ksm(a,mo-2)*b%mo,k);
	ll ans=0;
	if (ab==1) {
		ans = s * h;
	} else
	ans=s * (ksm(ab,h)%mo-1)%mo * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z+1);
	for (int i=0; i<k; i++) {
		s=(s + (z[i+1]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=a*b%mo;
	ll ans=(ksm(ab,h)*s%mo-s) * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z);
	for (ll i=0; i<k; i++) {
		s=(s + (z[i]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=ksm(ksm(a,mo-2)*b%mo,h);
	ll ans=(ksm(ab,h)*s%mo-s)%mo * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int mo=1e9+9,N=1e5+10;
ll n,a,b,k,ab;
ll s;
char z[N];
ll ksm(ll x,ll y) {
	ll ret=1;
	for (; y; y>>=1) {
		if (y&1) ret=ret*x%mo;
		x=x*x%mo;
	}
	return ret;
}
int main() {
	cin>>n>>a>>b>>k; scanf("%s",z);
	for (ll i=0; i<k; i++) {
		s=(s + (z[i]=='+'?1:-1) * ksm(a,n-i) * ksm(b,i)%mo)%mo;
	}
	ll h=(n+1)/k;
	ab=a*b%mo;
	ll ans=(ksm(ab,h)*s%mo-s)%mo * ksm(ab-1,mo-2) % mo;
	cout<<(ans%mo+mo)%mo<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000009
#define sz(a) ((int)(a).size())

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define eps 1e-10
typedef pair<int, int> II;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI 2 * acos(0)

#define maxn 400006

ll mu(ll x, ll k){
	if(k == 0) return 1;
	if(k & 1) return mu(x, k - 1) * x % mod;
	ll T = mu(x, k >> 1);
	return T * T % mod;
}

string s;
ll n, a, b, k;

int main(){
	srand(1412);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
    cin >> n >> a >> b >> k;
    cin >> s;
    ll res = 0;

    ll mul = mu(a, mod - 2) * b % mod;
    ll Mul = mu(mul, k);
    ll run = mu(a, n);

    for(int i = 0; i < k && i <= n; i++){
    	ll A = (n - i) / k;
    	ll tich = 1;
    	if(s[i] == '-') tich = -1;
//    	cout << run << " " << A << endl;

    	if(Mul == 0){
    		res = (res + tich * run) % mod;
    	} else if(Mul == 1){
    		res = (res + run * (A + 1) % mod * tich) % mod;
    	} else{
    		res = (res + tich * run * (mu(Mul, A + 1) - 1) % mod * mu(Mul - 1, mod - 2) % mod) % mod;
    	}
    	res = ((res % mod) + mod) % mod;
    	run = run * mul % mod;
    }
    cout << res << endl;

//    cout << res << endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);
		sum=sum+(aa*bb%mod)*ans;
		sum=sum%mod;

	}
	if(sum<=a[k-1])
	printf("1\n");
	else
	printf("%lld\n",sum/a[k-1]);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);

		sum=sum+(aa*bb%mod)%mod;
		sum%=mod;
	}
	printf("%lld\n",sum*ans/a[k-1]%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);
		sum=sum+(aa*bb%mod)*ans/a[k-1]%mod;
		sum%=mod;
	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);

		sum=sum+(aa*bb%mod)%mod;
		sum%=mod;
	}
	printf("%lld\n",sum*ans*Quick_pow(a[k-1],mod-2)%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);
		sum=sum+(aa*bb%mod)*ans;
		if(sum<=a[k-1])
		{
			printf("1\n");
			return 0;
		}
		sum/=a[k-1];
		sum%=mod;
	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll q=Quick_pow(B,k)*Quick_pow(Quick_pow(A,k),mod-2)%mod;
	ll Q=Quick_pow(q,m);
	ll sum=0;
	sum=Quick_pow(q-1,mod-2)*(Q-1)%mod;
	printf("%lld\n",sum*ans%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=n/k;
	ll sum=0;
	for(ll i=0;i<=m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);
		sum=sum+(aa*bb%mod)*ans/a[k-1]%mod;
		sum%=mod;
	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);
		if((aa*bb%mod)*ans<=a[k-1])
		{
			printf("1\n");
			return 0;
		}
		sum=sum+(aa*bb%mod)*ans/a[k-1]%mod;
		sum%=mod;
	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);

		sum=sum+(aa*bb%mod)%mod;
		sum%=mod;
	}
	printf("%lld\n",sum*ans*%mod*Quick_pow(a[k-1],mod-2)%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;

	for (int i = 0; i < k; ++i) {
        ll v = c[i] == '+' ? 1 : -1;
        ans = (ans + v * Quick_pow(A, n - i)*Quick_pow(B, i)%mod+ mod) % mod;

    }
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll q=B*Quick_pow(A,mod-2)%mod;
	q=Quick_pow(q,k)%mod;
	ll Q=Quick_pow(q,m);
	ll sum=0;
	if(q!=1)
	sum=Quick_pow(q-1,mod-2)*(Q-1)%mod;
	else
	sum=m;
	printf("%lld\n",sum*ans%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);

		sum=sum+(aa*bb%mod)*ans/a[k-1]%mod;
		sum%=mod;
	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=n/k;
	ll sum=0;
	for(ll i=0;i<=m;i++)
	{
		sum=sum+(Quick_pow(A,n-i*k)*Quick_pow(B,i*k)%mod)*ans/a[k-1]%mod;
		sum%=mod;
	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll aa,bb;
		aa=Quick_pow(A,n-i*k);
		bb=Quick_pow(B,i*k);
		sum=sum+(aa*bb%mod)*ans;
		sum=sum/a[k-1]%mod;

	}
	printf("%lld\n",sum);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll q=B*Quick_pow(A,mod-2)%mod;
	q=Quick_pow(q,k)%mod;
	ll Q=Quick_pow(q,m);
	ll sum=0;
	if(q!=1)
	sum=Quick_pow(q-1,mod-2)*(Q-1)%mod;
	else
	sum=m;
	printf("%lld\n",sum*ans%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+9;
const int maxn=1e5+5;
ll a[maxn],b[maxn];
char c[maxn];
ll Quick_pow(ll x,ll n){
    ll res = 1;
    while(n){
        if(n & 1)   res = res*x % mod;
        x = x*x % mod;
        n/=2;
    }
    return res;
}
void init(ll k,ll A,ll B)
{
	a[0]=1;
	b[0]=1;
	for(int i=1;i<=k;i++)
	{
		a[i]=a[i-1]*A%mod;
		b[i]=b[i-1]*B%mod;
	}

}
int main()
{
	ll n,A,B,k;
	scanf("%lld%lld%lld%lld",&n,&A,&B,&k);
	scanf("%s",c);
	ll ans=0;
	init(k,A,B);
	for(int i=0;i<k;i++)
	{
		if(c[i]=='+')
		ans=(ans+a[k-i-1]*b[i]%mod)%mod;
		else
		{
			ans=ans-a[k-i-1]*b[i]%mod;
			if(ans<0)
			ans=ans+mod;
			ans%=mod;
		}
	}
//	printf("%lld\n",ans);
	ll m=(n+1)/k;
	ll q=B*Quick_pow(A,mod-2)%mod;
	q=Quick_pow(q,k)%mod;
	ll Q=Quick_pow(q,m);
	ll sum=0;
	if(Q!=1)
	sum=Quick_pow(q-1,mod-2)*(Q-1)%mod;
	else
	sum=m;
	printf("%lld\n",sum*ans%mod);
	return 0;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int MAX=1e5+10;
const LL mod=1e9+9;
LL n,a,b,k;
char str[MAX];
LL ksm(LL x,LL tim)
{
    if(tim==0)return 1;
    LL tmp=ksm(x,tim/2);
    if(tim&1)return tmp*tmp%mod*x%mod;
    else return tmp*tmp%mod;
}
int main()
{
    cin>>n>>a>>b>>k;
    cin>>str;
    LL tmp=ksm(a,n),s=0;
    LL inva=ksm(a,mod-2);
    for(int i=0;i<k;i++)
    {
        if(str[i]=='+')s+=tmp;
        else s-=tmp;
        s=(s+mod)%mod;
        tmp=tmp*inva%mod*b%mod;
    }
    LL t=inva*b%mod;
    LL m=ksm(t,k);
    LL ans=0;
    int cnt=(n+1)/k;
    tmp=1;
    for(int i=0;i<cnt;i++)
    {
        ans+=tmp*s;
        ans=(ans+mod)%mod;
        tmp=tmp*m%mod;
    }
    tmp=ksm(a,cnt*k-1)*ksm(b,n-cnt*k+1)%mod;
    for(int i=cnt*k;i<=n;i++)
    {
        if(str[i]=='+')ans+=tmp;
        else ans-=tmp;
        ans=(ans+mod)%mod;
        tmp=tmp*inva%mod*b%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int MAX=1e5+10;
const LL mod=1e9+9;
LL n,a,b,k;
char str[MAX];
LL ksm(LL x,LL tim)
{
    if(tim==0)return 1;
    LL tmp=ksm(x,tim/2);
    if(tim&1)return tmp*tmp%mod*x%mod;
    else return tmp*tmp%mod;
}
int main()
{
    cin>>n>>a>>b>>k;
    cin>>str;
    int sq=sqrt(n);
    int cnt=(sq+k)/k;
    for(int i=0;i<k*cnt;i++)str[i]=str[i%(int)k];
    k=k*cnt;
    //cout<<k<<endl;
    LL tmp=ksm(a,n),s=0;
    LL inva=ksm(a,mod-2);
    for(int i=0;i<k;i++)
    {
        if(str[i]=='+')s+=tmp;
        else s-=tmp;
        s=(s+mod)%mod;
        tmp=tmp*inva%mod*b%mod;
    }
    LL t=inva*b%mod;
    LL m=ksm(t,k);
    LL ans=0;
    cnt=(n+1)/k;
    tmp=1;
    for(int i=0;i<cnt;i++)
    {
        ans+=tmp*s;
        ans=(ans+mod)%mod;
        tmp=tmp*m%mod;
    }
    tmp=ksm(a,cnt*k-1)*ksm(b,n-cnt*k+1)%mod;
    for(int i=cnt*k;i<=n;i++)
    {
        if(str[i%k]=='+')ans+=tmp;
        else ans-=tmp;
        ans=(ans+mod)%mod;
        tmp=tmp*inva%mod*b%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int MAX=1e5+10;
const LL mod=1e9+9;
LL n,a,b,k;
char str[MAX];
LL ksm(LL x,LL tim)
{
    if(tim==0)return 1;
    LL tmp=ksm(x,tim/2);
    if(tim&1)return tmp*tmp%mod*x%mod;
    else return tmp*tmp%mod;
}
int main()
{
    cin>>n>>a>>b>>k;
    cin>>str;
    int sq=sqrt(n);
    int cnt=(sq+k)/k;
    for(int i=0;i<k*cnt;i++)str[i]=str[i%(int)k];
    k=k*cnt;
    //cout<<k<<endl;
    LL tmp=ksm(a,n),s=0;
    LL inva=ksm(a,mod-2);
    for(int i=0;i<k;i++)
    {
        if(str[i]=='+')s+=tmp;
        else s-=tmp;
        s=(s+mod)%mod;
        tmp=tmp*inva%mod*b%mod;
    }
    LL t=inva*b%mod;
    LL m=ksm(t,k);
    LL ans=0;
    cnt=(n+1)/k;
    tmp=1;
    //cout<<s<<" "<<m<<endl;
    for(int i=0;i<cnt;i++)
    {
        ans+=tmp*s;
        ans=(ans+mod)%mod;
        tmp=tmp*m%mod;
    }
    //cout<<ans<<endl;
    tmp=ksm(a,n-cnt*k)*ksm(b,cnt*k)%mod;
    for(int i=cnt*k;i<=n;i++)
    {
        if(str[i%k]=='+')ans+=tmp;
        else ans-=tmp;
        ans=(ans+mod)%mod;
        tmp=tmp*inva%mod*b%mod;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
using namespace std;

// Create a namespace to avoid naming conflicts.
namespace neal {

const int MOD = 1000000009;

long long mod_pow(long long a, long long b) {
    long long p = 1;

    for (a %= MOD; b > 0; b >>= 1) {
        if (b & 1)
            p = p * a % MOD;

        a = a * a % MOD;
    }

    return p;
}

long long mod_inverse(long long a) {
    return mod_pow(a, MOD - 2);
}

long long N, A, B, K;
string sequence;

long long get_index(int i) {
    return (sequence[i % K] == '+' ? 1 : MOD - 1) * mod_pow(A, N - i) % MOD * mod_pow(B, i) % MOD;
}

void solve_problem() {
    cin >> N >> A >> B >> K;
    cin >> sequence;
    K = min(K, N + 1);

    long long sum_K = 0;

    for (int i = 0; i < K; i++)
        sum_K = (sum_K + get_index(i)) % MOD;

    long long repeats = (N + 1) / K;
    long long multiplier = mod_pow(mod_inverse(A), K) * mod_pow(B, K) % MOD;

    long long sum_N = sum_K * (1 - mod_pow(multiplier, repeats) + MOD) % MOD * mod_inverse(1 - multiplier + MOD) % MOD;

    if (multiplier == 1) {
        sum_N = sum_K * repeats % MOD;
    }

    for (int i = N + 1 - (N + 1) % K; i <= N; i++) {
        sum_N = (sum_N + get_index(i)) % MOD;
    }

    cout << sum_N << '\n';
}

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    neal::solve_problem();
    return 0;
}


#include <iostream>
using namespace std;

// Create a namespace to avoid naming conflicts.
namespace neal {

const int MOD = 1000000009;

long long mod_pow(long long a, long long b) {
    a %= MOD;
    long long p = 1;

    while (b > 0) {
        if (b & 1)
            p = p * a % MOD;

        a = a * a % MOD;
        b >>= 1;
    }

    return p;
}

long long mod_inverse(long long a) {
    return mod_pow(a, MOD - 2);
}

long long N, A, B, K;
string sequence;

long long get_index(int i) {
    return (sequence[i % K] == '+' ? 1 : MOD - 1) * mod_pow(A, N - i) % MOD * mod_pow(B, i) % MOD;
}

void solve_problem() {
    cin >> N >> A >> B >> K;
    cin >> sequence;
    K = min(K, N + 1);

    long long sum_K = 0;

    for (int i = 0; i < K; i++)
        sum_K = (sum_K + get_index(i)) % MOD;

    long long repeats = (N + 1) / K;
    long long multiplier = mod_pow(mod_inverse(A), K) * mod_pow(B, K) % MOD;

    long long sum_N = sum_K * (1 - mod_pow(multiplier, repeats) + MOD) % MOD * mod_inverse(1 - multiplier + MOD) % MOD;

    for (int i = N + 1 - (N + 1) % K; i <= N; i++) {
        sum_N = (sum_N + get_index(i)) % MOD;
    }

    cout << sum_N << '\n';
}

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    neal::solve_problem();
    return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <cmath>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DEBUG2 0
#define cerr if (DEBUG2) cerr
#define pb push_back

using namespace std;

bool compare(vector<long long> a, vector<long long> b){
    if(a[1] != b[1]){
        return a[1] > b[1];
    }else{
        return a[0] > b[0];
    }
}

int main(){
    long n;

    cin >> n;
    vector<vector<long long> > rings;
    for(int i=0; i<n; i++){
        vector<long long> v;
        for(int j=0; j<3; j++){
            long long ip;
            cin >> ip;
            v.pb(ip);
        }
        rings.pb(v);
    }
    sort(rings.begin(), rings.end(), compare);

    vector<int> disk_below;
    disk_below.push_back(0);

    long long height = rings[0][2], max_height = rings[0][2];

    for(int i=0; i<rings.size(); i++){
        for(int j=0; j<3; j++){
            cerr << rings[i][j] << " ";
        }
        cerr << endl;
    }
    for(int i=1; i<rings.size(); i++){

        for(long j=disk_below.size()-1; j>=0; j--){
            if(rings[i][1] <= rings[disk_below[j]][0]){
                height -= rings[disk_below[j]][2];
                disk_below.pop_back();
            }else{
                break;
            }
        }
        disk_below.push_back(i);
        height += rings[i][2];

        if(height > max_height){
            max_height = height;
        }
        cerr << "Put " << i << " " << ", Height = " << height << endl;

    }
    cout << max_height;

    return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <cmath>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define DEBUG2 0
#define cerr if (DEBUG2) cerr
#define pb push_back

using namespace std;

bool compare(vector<long long> a, vector<long long> b){
    if(a[1] != b[1]){
        return a[1] > b[1];
    }else{
        return a[0] > b[0];
    }
}

int main(){
    long n;
    cin >> n;
    vector<vector<long long> > rings;
    for(int i=0; i<n; i++){
        vector<long long> v;
        for(int j=0; j<3; j++){
            long long ip;
            cin >> ip;
            v.pb(ip);
        }
        rings.pb(v);
    }
    sort(rings.begin(), rings.end(), compare);
    long long height = rings[0][2], max_height = rings[0][2];

    for(int i=1; i<rings.size(); i++){
        height += rings[i][2];
        for(int j=i-1; j>=0; j--){
            if(rings[i][1] <= rings[j][0]){
                height -= rings[j][2];
                cerr << "Height falls " << rings[j][2] << endl;
            }else{
                break;
            }
        }
        if(height > max_height){
            max_height = height;
        }
    }
    cout << max_height;
    return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <cmath>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define DEBUG2 0
#define cerr if (DEBUG2) cerr
#define pb push_back

using namespace std;

bool compare(vector<long long> a, vector<long long> b){
    if(a[1] != b[1]){
        return a[1] > b[1];
    }else{
        return a[0] > b[0];
    }
}

int main(){
    long n;
    cin >> n;
    vector<vector<long long> > rings;
    for(int i=0; i<n; i++){
        vector<long long> v;
        for(int j=0; j<3; j++){
            long long ip;
            cin >> ip;
            v.pb(ip);
        }
        rings.pb(v);
    }
    sort(rings.begin(), rings.end(), compare);
    long long height = rings[0][2], max_height = 0;

    for(int i=1; i<rings.size(); i++){
        height += rings[i][2];
        for(int j=i-1; j>=0; j--){
            if(rings[i][1] <= rings[j][0]){
                height -= rings[j][2];
                cerr << "Height falls " << rings[j][2] << endl;
            }else{
                break;
            }
        }
        if(height > max_height){
            max_height = height;
        }
    }
    cout << max_height;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], b[N], h[N], num[N];
long long seg[N << 2], ans, val;
void compress() {
	vector<int> v;
	for (int i = 0; i < n; i++)
		v.push_back(a[i]), v.push_back(b[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 0; i < n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin(), b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
}
long long get(int l, int r, int id = 1, int s = 0, int e = n) {
	if (l <= s && e <= r)
		return seg[id];
	if (e <= l || r <= s)
		return 0;
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	return max(get(l, r, lc, s, mid), get(l, r, rc, mid, e));
}
void add(int p, int id = 1, int s = 0, int e = n) {
	if (e - s == 1) {
		seg[id] = max(seg[id], val);
		return;
	}
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	if (p < mid)
		add(p, lc, s, mid);
	else
		add(p, rc, mid, e);
	seg[id] = max(seg[lc], seg[rc]);
	return;
}
int main() {
	ios_base::sync_with_stdio (false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i] >> b[i] >> h[i];
	compress();
	iota(num, num + n, 0);
	sort(num, num + n, [] (int x, int y) { return b[x] > b[y]; });
	for (int i = 0; i < n; i++) {
		long long x = get(0, b[num[i]]);
		ans = max(ans, h[num[i]] + x);
		val = h[num[i]] + x;
		add(a[num[i]]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, MAXN = 2e5 + 10;
int n, a[N], b[N], h[N], num[N];
long long seg[MAXN << 2], ans, val;
void compress() {
	vector<int> v;
	for (int i = 0; i < n; i++)
		v.push_back(a[i]), v.push_back(b[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 0; i < n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin(), b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
}
long long get(int l, int r, int id = 1, int s = 0, int e = MAXN) {
	if (l <= s && e <= r)
		return seg[id];
	if (e <= l || r <= s)
		return 0;
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	long long rtr = max(get(l, r, lc, s, mid), get(l, r, rc, mid, e));
//	cout << "l = " << l << " r = " << r << " s = " << s << " e = " << e << " : " << rtr << endl;
	return rtr;
}
void add(int p, int id = 1, int s = 0, int e = MAXN) {
	if (e - s == 1) {
		seg[id] = max(seg[id], val);
		return;
	}
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	if (p < mid)
		add(p, lc, s, mid);
	else
		add(p, rc, mid, e);
	seg[id] = max(seg[lc], seg[rc]);
	return;
}
int main() {
	ios_base::sync_with_stdio (false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i] >> b[i] >> h[i];
	compress();
	iota(num, num + n, 0);
	sort(num, num + n, [] (int x, int y) { return (b[x] > b[y] || (b[x] == b[y] &&  a[x] > a[y])); });
	for (int i = 0; i < n; i++) {
//		cout << "num[i] = " << num[i] << " : " << b[num[i]] << " " << a[num[i]] << " " << h[num[i]] << " : \n";
		long long x = get(0, b[num[i]]);
//		cout << "x = " << x << " :: " << x + h[num[i]] << endl;
		ans = max(ans, h[num[i]] + x);
		val = h[num[i]] + x;
		add(a[num[i]]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], b[N], h[N], num[N];
long long seg[N << 2], ans, val;
void compress() {
	vector<int> v;
	for (int i = 0; i < n; i++)
		v.push_back(a[i]), v.push_back(b[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 0; i < n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin(), b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
}
long long get(int l, int r, int id = 1, int s = 0, int e = n) {
	if (l <= s && e <= r)
		return seg[id];
	if (e <= l || r <= s)
		return 0;
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	return max(get(l, r, lc, s, mid), get(l, r, rc, mid, e));
}
void add(int p, int id = 1, int s = 0, int e = n) {
	if (e - s == 1) {
		seg[id] = max(seg[id], val);
		return;
	}
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	if (p < mid)
		add(p, lc, s, mid);
	else
		add(p, rc, mid, e);
	seg[id] = max(seg[lc], seg[rc]);
	return;
}
int main() {
	ios_base::sync_with_stdio (false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i] >> b[i] >> h[i];
//	compress();
	iota(num, num + n, 0);
	sort(num, num + n, [] (int x, int y) { return (b[x] > b[y] || (b[x] == b[y] &&  a[x] > a[y])); });
	for (int i = 0; i < n; i++) {
		long long x = get(0, b[num[i]]);
		ans = max(ans, h[num[i]] + x);
		val = h[num[i]] + x;
		add(a[num[i]]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], b[N], h[N], num[N];
long long seg[N << 2], ans, val;
void compress() {
	vector<int> v;
	for (int i = 0; i < n; i++)
		v.push_back(a[i]), v.push_back(b[i]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 0; i < n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin(), b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
}
long long get(int l, int r, int id = 1, int s = 0, int e = n) {
	if (l <= s && e <= r)
		return seg[id];
	if (e <= l || r <= s)
		return 0;
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	return max(get(l, r, lc, s, mid), get(l, r, rc, mid, e));
}
void add(int p, int id = 1, int s = 0, int e = n) {
	if (e - s == 1) {
		seg[id] = max(seg[id], val);
		return;
	}
	int mid = (s + e) >> 1, lc = id * 2, rc = lc + 1;
	if (p < mid)
		add(p, lc, s, mid);
	else
		add(p, rc, mid, e);
	seg[id] = max(seg[lc], seg[rc]);
	return;
}
int main() {
	ios_base::sync_with_stdio (false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i] >> b[i] >> h[i];
	compress();
	iota(num, num + n, 0);
	sort(num, num + n, [] (int x, int y) { return (b[x] > b[y] || (b[x] == b[y] &&  a[x] > a[y])); });
	for (int i = 0; i < n; i++) {
		long long x = get(0, b[num[i]]);
		ans = max(ans, h[num[i]] + x);
		val = h[num[i]] + x;
		add(a[num[i]]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100000 + 5;

struct Tower
{
    int a;
    int b;
    ll h;
};

int n;
Tower v[N];

bool cmp (Tower x, Tower y)
{
    return x.b > y.b;
}

int vf = 0;
Tower a[N];

ll dp[N];

struct info
{
    int i;
    int a;
    ll val;
};

bool operator < (info a, info b)
{
    if (a.val == b.val)
    {
        return a.i < b.i;
    }
    else
    {
        return a.val > b.val;
    }
}

int main()
{
    ios_base::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].a >> v[i].b >> v[i].h;
    }
    sort (v + 1, v + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        int j = i;
        while (i + 1 <= n && v[i + 1].b == v[j].b)
        {
            i++;
        }
        int miA = (1 << 30);
        ll h = 0LL;
        for (int k = j; k <= i; k++)
        {
            miA = min (miA, v[k].a);
            h += v[k].h;
        }
        a[++vf] = {miA, v[j].b, h};
    }
    n = vf;
    for (int i = 1; i <= n; i++)
    {
        v[i] = a[i];
    }
    set<info>s;
    for (int i = 1; i <= n; i++)
    {
        dp[i] = v[i].h;
        while (!s.empty() && s.begin()->a >= v[i].b)
        {
            s.erase (s.begin());
        }
        if (!s.empty() && s.begin()->a < v[i].b)
        {
            dp[i] = max (dp[i], s.begin()->val + v[i].h);
        }
        s.insert ({i, v[i].a, dp[i]});
    }
    ll ans = 0LL;
    for (int i = 1; i <= n; i++)
    {
        ans = max (ans, dp[i]);
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100000 + 5;

struct Tower
{
    int a;
    int b;
    ll h;
};

int n;
Tower v[N];

bool cmp (Tower x, Tower y)
{
    return x.b > y.b;
}

int vf = 0;
Tower a[N];

ll dp[N];

int main()
{
    // ios_base::sync_with_stdio (0);
    // cin.tie (0);
    // cout.tie (0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].a >> v[i].b >> v[i].h;
    }
    sort (v + 1, v + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        int j = i;
        while (i + 1 <= n && v[i + 1].b == v[j].b)
        {
            i++;
        }
        int miA = (1 << 30);
        ll h = 0LL;
        for (int k = j; k <= i; k++)
        {
            miA = min (miA, v[k].a);
            h += v[k].h;
        }
        a[++vf] = {miA, v[j].b, h};
    }
    n = vf;
    for (int i = 1; i <= n; i++)
    {
        v[i] = a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        dp[i]=v[i].h;
        for (int j = 1; j < i; j++)
        {
            if (v[i].b > v[j].a)
            {
                dp[i] = max (dp[i], dp[j] + v[i].h);
            }
        }
    }
    ll ans = 0LL;
    for (int i = 1; i <= n; i++)
    {
        ans = max (ans, dp[i]);
    }
    cout << ans << "\n";
    return 0;
    for (int i = 1; i <= n; i++)
    {
        cout << "\t" << v[i].a << " " << v[i].b << " " << v[i].h << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100000 + 5;

struct Tower
{
    int a;
    int b;
    ll h;
};

int n;
Tower v[N];

bool cmp (Tower x, Tower y)
{
    return x.b > y.b;
}

int vf = 0;
Tower a[N];

ll dp[N];

int main()
{
    // ios_base::sync_with_stdio (0);
    // cin.tie (0);
    // cout.tie (0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].a >> v[i].b >> v[i].h;
    }
    sort (v + 1, v + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        int j = i;
        while (i + 1 <= n && v[i + 1].b == v[j].b)
        {
            i++;
        }
        int miA = (1 << 30);
        ll h = 0LL;
        for (int k = j; k <= i; k++)
        {
            miA = min (miA, v[k].a);
            h += v[k].h;
        }
        a[++vf] = {miA, v[j].b, h};
    }
    n = vf;
    for (int i = 1; i <= n; i++)
    {
        v[i] = a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        dp[i]=v[i].h;
        for (int j = 1; j < i; j++)
        {
            if (v[i].b > v[j].a)
            {
                dp[i] = max (dp[i], dp[j] + v[i].h);
            }
        }
    }
    cout << dp[n] << "\n";
    return 0;
    for (int i = 1; i <= n; i++)
    {
        cout << "\t" << v[i].a << " " << v[i].b << " " << v[i].h << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100000 + 5;

struct Tower
{
    int a;
    int b;
    ll h;
};

int n;
Tower v[N];

bool cmp (Tower x, Tower y)
{
    return x.b > y.b;
}

int vf = 0;
Tower a[N];

ll dp[N];

struct info
{
    int i;
    int a;
    ll val;
};

bool operator < (info a,info b)
{
    if(a.val==b.val)
    {
        return a.i<b.i;
    }
    else
    {
        return a.val>b.val;
    }
}

int main()
{
    ios_base::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].a >> v[i].b >> v[i].h;
    }
    sort (v + 1, v + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        int j = i;
        while (i + 1 <= n && v[i + 1].b == v[j].b)
        {
            i++;
        }
        int miA = (1 << 30);
        ll h = 0LL;
        for (int k = j; k <= i; k++)
        {
            miA = min (miA, v[k].a);
            h += v[k].h;
        }
        a[++vf] = {miA, v[j].b, h};
    }
    n = vf;
    for (int i = 1; i <= n; i++)
    {
        v[i] = a[i];
    }
    set<info>s;
    for (int i = 1; i <= n; i++)
    {
        dp[i]=v[i].h;
        while(!s.empty() && s.begin()->a>=v[i].b)
        {
            s.erase(s.begin());
        }
        if(!s.empty() && s.begin()->a<v[i].b)
        {
            dp[i]=max(dp[i],s.begin()->val+v[i].h);
        }
        s.insert({i,v[i].a,dp[i]});
    }
    ll ans = 0LL;
    for (int i = 1; i <= n; i++)
    {
        ans = max (ans, dp[i]);
    }
    cout << ans << "\n";
    return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
// #define multitest_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef pair<pii, ll> ppi;
typedef pair<pii, pii> ppp;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define print4(x, y, z, t) cout << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << '\n'
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << '\n'
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << '\n'; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << '\n'
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << '\n'; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << '\n'
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << '\n'; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << '\n'; }
#define printav(x, n) fori (iaa123477, n) { printv(x[iaa123477]); }
#define printia(x, n) fori (ja212345, n) { cout << ja212345 << " : " << (x)[ja212345] << '\n'; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debug2(x, y) cout << #x << ' ' << #y << " -> "; print2(x, y)
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> "; print3(x, y, z)
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> "; print4(x, y, z, t)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#define debugav(x, n) cout << #x << " ->\n"; printav(x, n)
#define debugia(x, n) cout << #x << " ->\n"; printia(x, n)
ll clk_ar[10];
#define clk_s(i) clk_ar[i] = clock();
#define clk_e(i) cout << "Clock " << i << " : " << (double) (clock() - clk_ar[i]) / CLOCKS_PER_SEC << " sec\n";
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, t)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#define debugav(x, n, m)
#define debugia(x, n)
#define clk_s(i)
#define clk_e(i)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n) - 1; i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 200010;

map<ll, ll> dp;
ll compress[maxn * 2];
ppi ring[maxn];

void update(ll i, ll x) {
    ++i;
    while (i < maxn) {
        smax(dp[i], x);
        i += i & (-i);
    }
}

ll get(ll i) {
    ++i;
    ll mx = 0;
    while (i > 0) {
        smax(mx, dp[i]);
        i -= i & (-i);
    }
    return mx;
}

void MAIN() {

    inp(n);
    fori (i, n) {
        inp3(a, b, h);
        compress[i * 2] = a;
        compress[i * 2 + 1] = b;
        ring[i] = {{b, a}, h};
    }
    sort(ring, ring + n, greater<ppi>());
    fori (i, n) {
        ll b = ring[i].ff.ff;
        ll a = ring[i].ff.ss;
        ll h = ring[i].ss;
        ll v = get(b - 1) + h;
        update(a, v);
    }
    print(get(maxn - 1));

}

// -----------------------------------------------------------------------

ll TESTCASEN = 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
3
10 11 2
7 11 2
2 4 2

3
2 5 1
3 6 2
4 7 3

4
1 2 1
1 3 3
4 6 2
5 7 1

    )";
    #ifdef multitest_enable
    cin >> TESTCASEN;
    #endif
    fori (test, TESTCASEN) {
        MAIN();
    }
    return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
// #define multitest_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef pair<pii, ll> ppi;
typedef pair<pii, pii> ppp;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define print4(x, y, z, t) cout << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << '\n'
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << '\n'
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << '\n'; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << '\n'
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << '\n'; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << '\n'
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << '\n'; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << '\n'; }
#define printav(x, n) fori (iaa123477, n) { printv(x[iaa123477]); }
#define printia(x, n) fori (ja212345, n) { cout << ja212345 << " : " << (x)[ja212345] << '\n'; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debug2(x, y) cout << #x << ' ' << #y << " -> "; print2(x, y)
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> "; print3(x, y, z)
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> "; print4(x, y, z, t)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#define debugav(x, n) cout << #x << " ->\n"; printav(x, n)
#define debugia(x, n) cout << #x << " ->\n"; printia(x, n)
ll clk_ar[10];
#define clk_s(i) clk_ar[i] = clock();
#define clk_e(i) cout << "Clock " << i << " : " << (double) (clock() - clk_ar[i]) / CLOCKS_PER_SEC << " sec\n";
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, t)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#define debugav(x, n, m)
#define debugia(x, n)
#define clk_s(i)
#define clk_e(i)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n) - 1; i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 200010;

ll dp[maxn];
ll compress[maxn * 2];
ppi ring[maxn];

void update(ll i, ll x) {
    ++i;
    while (i < maxn) {
        smax(dp[i], x);
        i += i & (-i);
    }
}

ll get(ll i) {
    ++i;
    ll mx = 0;
    while (i > 0) {
        smax(mx, dp[i]);
        i -= i & (-i);
    }
    return mx;
}

void MAIN() {

    inp(n);
    fori (i, n) {
        inp3(a, b, h);
        compress[i * 2] = a;
        compress[i * 2 + 1] = b;
        ring[i] = {{b, a}, h};
    }
    sort(compress, compress + (n * 2));
    ll cs = unique(compress, compress + (n * 2)) - compress;
    fori (i, n) {
        ring[i].ff.ff = lower_bound(compress, compress + cs, ring[i].ff.ff) - compress + 1;
        ring[i].ff.ss = lower_bound(compress, compress + cs, ring[i].ff.ss) - compress + 1;
    }
    sort(ring, ring + n, greater<ppi>());
    fori (i, n) {
        ll b = ring[i].ff.ff;
        ll a = ring[i].ff.ss;
        ll h = ring[i].ss;
        ll v = get(b - 1) + h;
        update(a, v);
    }
    print(get(maxn - 1));

}

// -----------------------------------------------------------------------

ll TESTCASEN = 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
3
10 11 2
7 11 2
2 4 2

3
2 5 1
3 6 2
4 7 3

4
1 2 1
1 3 3
4 6 2
5 7 1

    )";
    #ifdef multitest_enable
    cin >> TESTCASEN;
    #endif
    fori (test, TESTCASEN) {
        MAIN();
    }
    return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
// #define multitest_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef pair<pii, ll> ppi;
typedef pair<pii, pii> ppp;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define print4(x, y, z, t) cout << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << '\n'
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << '\n'
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << '\n'; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << '\n'
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << '\n'; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << '\n'
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << '\n'; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << '\n'; }
#define printav(x, n) fori (iaa123477, n) { printv(x[iaa123477]); }
#define printia(x, n) fori (ja212345, n) { cout << ja212345 << " : " << (x)[ja212345] << '\n'; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debug2(x, y) cout << #x << ' ' << #y << " -> "; print2(x, y)
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> "; print3(x, y, z)
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> "; print4(x, y, z, t)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#define debugav(x, n) cout << #x << " ->\n"; printav(x, n)
#define debugia(x, n) cout << #x << " ->\n"; printia(x, n)
ll clk_ar[10];
#define clk_s(i) clk_ar[i] = clock();
#define clk_e(i) cout << "Clock " << i << " : " << (double) (clock() - clk_ar[i]) / CLOCKS_PER_SEC << " sec\n";
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, t)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#define debugav(x, n, m)
#define debugia(x, n)
#define clk_s(i)
#define clk_e(i)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n) - 1; i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 100010;

ll dp[maxn];
ll compress[maxn * 2];
ppi ring[maxn];

void update(ll i, ll x) {
    ++i;
    while (i < maxn) {
        smax(dp[i], x);
        i += i & (-i);
    }
}

ll get(ll i) {
    ++i;
    ll mx = 0;
    while (i > 0) {
        smax(mx, dp[i]);
        i -= i & (-i);
    }
    return mx;
}

void MAIN() {

    inp(n);
    fori (i, n) {
        inp3(a, b, h);
        compress[i * 2] = a;
        compress[i * 2 + 1] = b;
        ring[i] = {{b, a}, h};
    }
    sort(compress, compress + (n * 2));
    ll cs = unique(compress, compress + (n * 2)) - compress;
    fori (i, n) {
        ring[i].ff.ff = lower_bound(compress, compress + cs, ring[i].ff.ff) - compress + 1;
        ring[i].ff.ss = lower_bound(compress, compress + cs, ring[i].ff.ss) - compress + 1;
    }
    sort(ring, ring + n, greater<ppi>());
    fori (i, n) {
        // cout << ring[i].ff.ff << ' ' << ring[i].ff.ss << ' ' << ring[i].ss << '\n';
        ll b = ring[i].ff.ff;
        ll a = ring[i].ff.ss;
        ll h = ring[i].ss;
        ll v = get(b - 1) + h;
        update(a, v);

        // fori (i, 10) {
        //     cout << get(i) << endl;
        // }
    }

    print(get(maxn - 1));

}

// -----------------------------------------------------------------------

ll TESTCASEN = 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
3
10 11 2
7 11 2
2 4 2

3
2 5 1
3 6 2
4 7 3

4
1 2 1
1 3 3
4 6 2
5 7 1

    )";
    #ifdef multitest_enable
    cin >> TESTCASEN;
    #endif
    fori (test, TESTCASEN) {
        MAIN();
    }
    return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
// #define multitest_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef pair<pii, ll> ppi;
typedef pair<pii, pii> ppp;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define print4(x, y, z, t) cout << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << '\n'
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << '\n'
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << '\n'; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << '\n'
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << '\n'; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << '\n'
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << '\n'; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << '\n'; }
#define printav(x, n) fori (iaa123477, n) { printv(x[iaa123477]); }
#define printia(x, n) fori (ja212345, n) { cout << ja212345 << " : " << (x)[ja212345] << '\n'; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debug2(x, y) cout << #x << ' ' << #y << " -> "; print2(x, y)
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> "; print3(x, y, z)
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> "; print4(x, y, z, t)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#define debugav(x, n) cout << #x << " ->\n"; printav(x, n)
#define debugia(x, n) cout << #x << " ->\n"; printia(x, n)
ll clk_ar[10];
#define clk_s(i) clk_ar[i] = clock();
#define clk_e(i) cout << "Clock " << i << " : " << (double) (clock() - clk_ar[i]) / CLOCKS_PER_SEC << " sec\n";
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, t)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#define debugav(x, n, m)
#define debugia(x, n)
#define clk_s(i)
#define clk_e(i)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n) - 1; i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 300010;

ll dp[maxn];
ll compress[maxn * 2];
ppi ring[maxn];

void update(ll i, ll x) {
    ++i;
    while (i < maxn) {
        smax(dp[i], x);
        i += i & (-i);
    }
}

ll get(ll i) {
    ++i;
    ll mx = 0;
    while (i > 0) {
        smax(mx, dp[i]);
        i -= i & (-i);
    }
    return mx;
}

void MAIN() {

    inp(n);
    fori (i, n) {
        inp3(a, b, h);
        compress[i * 2] = a;
        compress[i * 2 + 1] = b;
        ring[i] = {{b, a}, h};
    }
    sort(compress, compress + (n * 2));
    ll cs = unique(compress, compress + (n * 2)) - compress;
    fori (i, n) {
        ring[i].ff.ff = lower_bound(compress, compress + cs, ring[i].ff.ff) - compress + 1;
        ring[i].ff.ss = lower_bound(compress, compress + cs, ring[i].ff.ss) - compress + 1;
    }
    sort(ring, ring + n, greater<ppi>());
    fori (i, n) {
        // cout << ring[i].ff.ff << ' ' << ring[i].ff.ss << ' ' << ring[i].ss << '\n';
        ll b = ring[i].ff.ff;
        ll a = ring[i].ff.ss;
        ll h = ring[i].ss;
        ll v = get(b - 1) + h;
        update(a, v);

        // fori (i, 10) {
        //     cout << get(i) << endl;
        // }
    }

    print(get(maxn - 1));

}

// -----------------------------------------------------------------------

ll TESTCASEN = 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
3
10 11 2
7 11 2
2 4 2

3
2 5 1
3 6 2
4 7 3

4
1 2 1
1 3 3
4 6 2
5 7 1

    )";
    #ifdef multitest_enable
    cin >> TESTCASEN;
    #endif
    fori (test, TESTCASEN) {
        MAIN();
    }
    return 0;
}


#include <bits/stdc++.h>
#define mid (l+r)/2
#define le node+node
#define ri node+node+1
using namespace std;
const int inf = 1e5+9;
int n,cnt,ans;
map<int,int> mp;
int tree[inf<<3],dp[inf];
pair<int,pair<int,int> > ring[inf];

void update(int node,int l,int r,int idx,int val){

    if(l == r)
        return void(tree[node] = max(tree[node],val));
    if(idx<=mid)
        update(le,l,mid,idx,val);
    else
        update(ri,mid+1,r,idx,val);
    tree[node] = max( tree[le] , tree[ri] );
}

int query(int node,int l,int r,int x,int y){

    if(l>r || r<x || l>y || x>y)
        return 0;

    if(l>=x && r<=y)
        return tree[node];
    return max( query(le,l,mid,x,y) , query(ri,mid+1,r,x,y) );
}

int main(){

    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b,h;
        scanf("%d%d%d",&a,&b,&h);
        ring[i] = make_pair( h , make_pair( a,b ) );
        mp[a],mp[b];
    }

    sort(ring+1,ring+1+n);
    for(auto &o:mp)
        o.second = ++cnt;

    for(int i=1;i<=n;i++)
        ring[i].second.first = mp[ ring[i].second.first ],
        ring[i].second.second = mp[ ring[i].second.second ];

    for(int i=1;i<=n;i++){
        dp[i] = query( 1,1,cnt,1,ring[i].second.second-1 ) + ring[i].first;
        update(1,1,cnt,ring[i].second.first,dp[i]);
        ans = max(ans,dp[i]);
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
#define mid (l+r)/2
#define le node+node
#define ri node+node+1
#define ll long long
using namespace std;
const ll inf = 1e5+9;
ll n,cnt,ans;
map<ll,ll> mp;
ll tree[inf<<3],dp[inf];
pair<ll,pair<ll,ll> > ring[inf];

void update(ll node,ll l,ll r,ll idx,ll val){

    if(l == r)
        return void(tree[node] = max(tree[node],val));
    if(idx<=mid)
        update(le,l,mid,idx,val);
    else
        update(ri,mid+1,r,idx,val);
    tree[node] = max( tree[le] , tree[ri] );
}

ll query(ll node,ll l,ll r,ll x,ll y){

    if(l>r || r<x || l>y || x>y)
        return 0;

    if(l>=x && r<=y)
        return tree[node];
    return max( query(le,l,mid,x,y) , query(ri,mid+1,r,x,y) );
}

bool cmp(pair<ll,pair<ll,ll> > x , pair<ll,pair<ll,ll> > y){//sort by b then by a
        if(x.second.second == y.second.second)
            return x.second.first > y.second.first;
        return x.second.second > y.second.second;
}

int main(){

    scanf("%I64d",&n);
    for(ll i=1;i<=n;i++){
        ll a,b,h;
        scanf("%I64d%I64d%I64d",&a,&b,&h);
        ring[i] = make_pair( h , make_pair( a,b ) );
        mp[a],mp[b];
    }

    sort(ring+1,ring+1+n,cmp);
    for(auto &o:mp)
        o.second = ++cnt;

    for(ll i=1;i<=n;i++)
        ring[i].second.first = mp[ ring[i].second.first ],
        ring[i].second.second = mp[ ring[i].second.second ];

    for(ll i=1;i<=n;i++){
        //cout<<ring[i].first<<" "<<ring[i].second.first<<" "<<ring[i].second.second<<" ";
        dp[i] = query( 1,1,cnt,1,ring[i].second.second-1 ) + ring[i].first;
        update(1,1,cnt,ring[i].second.first,dp[i]);
        ans = max(ans,dp[i]);
        //cout<<dp[i]<<"\n";
    }
    printf("%I64d\n",ans);
}


#include <bits/stdc++.h>
#define mid (l+r)/2
#define le node+node
#define ri node+node+1
using namespace std;
const int inf = 1e5+9;
int n,cnt,ans;
map<int,int> mp;
int tree[inf<<3],dp[inf];
pair<int,pair<int,int> > ring[inf];

void update(int node,int l,int r,int idx,int val){

    if(l == r)
        return void(tree[node] = max(tree[node],val));
    if(idx<=mid)
        update(le,l,mid,idx,val);
    else
        update(ri,mid+1,r,idx,val);
    tree[node] = max( tree[le] , tree[ri] );
}

int query(int node,int l,int r,int x,int y){

    if(l>r || r<x || l>y || x>y)
        return 0;

    if(l>=x && r<=y)
        return tree[node];
    return max( query(le,l,mid,x,y) , query(ri,mid+1,r,x,y) );
}

bool cmp(pair<int,pair<int,int> > x , pair<int,pair<int,int> > y){//sort by b then by a
        if(x.second.second == y.second.second)
            return x.second.first > y.second.first;
        return x.second.second > y.second.second;
}

int main(){

    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b,h;
        scanf("%d%d%d",&a,&b,&h);
        ring[i] = make_pair( h , make_pair( a,b ) );
        mp[a],mp[b];
    }

    sort(ring+1,ring+1+n,cmp);
    for(auto &o:mp)
        o.second = ++cnt;

    for(int i=1;i<=n;i++)
        ring[i].second.first = mp[ ring[i].second.first ],
        ring[i].second.second = mp[ ring[i].second.second ];

    for(int i=1;i<=n;i++){
        //cout<<ring[i].first<<" "<<ring[i].second.first<<" "<<ring[i].second.second<<" ";
        dp[i] = query( 1,1,cnt,1,ring[i].second.second-1 ) + ring[i].first;
        update(1,1,cnt,ring[i].second.first,dp[i]);
        ans = max(ans,dp[i]);
        //cout<<dp[i]<<"\n";
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
#define mid (l+r)/2
#define le node+node
#define ri node+node+1
using namespace std;
const int inf = 1e5+9;
int n,cnt,ans;
map<int,int> mp;
int tree[inf<<3],dp[inf];
pair<int,pair<int,int> > ring[inf];

void update(int node,int l,int r,int idx,int val){

    if(l == r)
        return void(tree[node] = max(tree[node],val));
    if(idx<=mid)
        update(le,l,mid,idx,val);
    else
        update(ri,mid+1,r,idx,val);
    tree[node] = max( tree[le] , tree[ri] );
}

int query(int node,int l,int r,int x,int y){

    if(l>r || r<x || l>y || x>y)
        return 0;

    if(l>=x && r<=y)
        return tree[node];
    return max( query(le,l,mid,x,y) , query(ri,mid+1,r,x,y) );
}

bool cmp(pair<int,pair<int,int> > x , pair<int,pair<int,int> > y){

    return x.second.second<y.second.second;
}

int main(){

    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b,h;
        scanf("%d%d%d",&a,&b,&h);
        ring[i] = make_pair( h , make_pair( a,b ) );
        mp[a],mp[b];
    }

    sort(ring+1,ring+1+n,cmp);
    for(auto &o:mp)
        o.second = ++cnt;

    for(int i=1;i<=n;i++)
        ring[i].second.first = mp[ ring[i].second.first ],
        ring[i].second.second = mp[ ring[i].second.second ];

    for(int i=1;i<=n;i++){
        dp[i] = query( 1,1,cnt,1,ring[i].second.second-1 ) + ring[i].first;
        update(1,1,cnt,ring[i].second.first,dp[i]);
        ans = max(ans,dp[i]);
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
#define mid (l+r)/2
#define le node+node
#define ri node+node+1
using namespace std;
const int inf = 1e5+9;
int n,cnt,ans;
map<int,int> mp;
int tree[inf<<3],dp[inf];
pair<int,pair<int,int> > ring[inf];

void update(int node,int l,int r,int idx,int val){

    if(l == r)
        return void(tree[node] = max(tree[node],val));
    if(idx<=mid)
        update(le,l,mid,idx,val);
    else
        update(ri,mid+1,r,idx,val);
    tree[node] = max( tree[le] , tree[ri] );
}

int query(int node,int l,int r,int x,int y){

    if(l>r || r<x || l>y || x>y)
        return 0;

    if(l>=x && r<=y)
        return tree[node];
    return max( query(le,l,mid,x,y) , query(ri,mid+1,r,x,y) );
}

int main(){

    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b,h;
        scanf("%d%d%d",&a,&b,&h);
        ring[i] = make_pair( h , make_pair( a,b ) );
        mp[a],mp[b];
    }

    sort(ring+1,ring+1+n);
    for(auto &o:mp)
        o.second = ++cnt;

    for(int i=1;i<=n;i++)
        ring[i].second.first = mp[ ring[i].second.first ],
        ring[i].second.second = mp[ ring[i].second.second ];

    for(int i=1;i<=n;i++){
        dp[i] = query( 1,1,cnt,1,ring[i].second.second ) + ring[i].first;
        update(1,1,cnt,ring[i].second.first,dp[i]);
        ans = max(ans,dp[i]);
    }
    printf("%d\n",ans);
}


#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[200500];

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>

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];
int tree[400500];

int 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, int 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++)
	{
		int 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 + 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>

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;
	vector<pair<int, pair<short, int>>> v;
	for (auto it = m.begin(); it != m.end(); it++)
	{
		v[i] = make_pair(it->first, make_pair(1, i));
		v[i] = make_pair(it->second.first, make_pair(0, i));
		i++;
	}
	sort(v.begin(), v.end());
	i = 0;
	for (auto it = m.begin(); it != m.end(); it++)
	{
		arr[i].h = it->second.second;
		i++;
	}
	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>
#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[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]);
	}
}

bool comp(circle a, circle b)
{
	return 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;/*
		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;
		}*/
	}
	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++)
	//{
	//	cout << arr[i].r << " " << arr[i].R << endl;
	//}
	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;
};

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[i].R);
		update(1, 0, 2*n, arr[i].r,  pref_max + 1ll* arr[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;
};

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[i].R - 1);
		update(1, 0, 2*n, arr[i].r,  pref_max + 1ll* arr[i].h);
	}
	cout << sum(1, 0, 2*n,0, 2*n) << endl;
}