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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			ll maxh = getmax(0, atar);

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

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

	return 0;
}


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			ll maxh = getmax(0, atar);

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

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

	return 0;
}


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			ll maxh = getmax(0, atar);

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

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

	return 0;
}


#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//

#define ti tuple<int,int,int,int>

const int MAX = 100020;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

struct ST {
    ll id = 0;

    int size;
    vl operations;
    vector<pair<ll,ll>> mins;

    pair<ll,ll> merg(pair<ll,ll> a, pair<ll,ll> b) {
        if(a.ff > b.ff) {
            return a;
        }
        return b;
    }

    void init(int n) {
        size = 1;
        while(size < n) size *= 2;
        mins.assign(2*size, pair<ll,ll>(0, 0));
        operations.assign(2*size, 0);
    }

    void add(int l, int r, int v, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return;
        if(l <= lx && rx <= r) {
            if(rx-lx == 1)
                mins[x].ss = lx;

            operations[x] += v;
            mins[x].ff += v;
            return;
        }

        int m = (rx + lx)/2;
        add(l, r, v, 2*x +1 , lx, m);
        add(l, r, v, 2*x +2 , m, rx);

        mins[x] = merg(mins[2*x +1], mins[2*x +2]);
        mins[x].ff += operations[x];
    }

    void add(int l, int r, int v) {
        add(l, r+1, v, 0, 0, size);
    }

    pair<ll,ll> min(int l, int r, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return mp(0, -1);
        if(l <= lx && rx <= r) {
            return mins[x];
        }

        int m = (lx + rx)/2;

        auto s1 = min(l, r, 2*x +1, lx, m);
        auto s2 = min(l, r, 2*x +2, m, rx);
        auto res = merg(s1, s2);
        res.ff += operations[x];

        return res;
    }

    pair<ll,ll> min(int l, int r) {
        return min(l, r+1, 0, 0, size);
    }
};

bool cmp2(const ti a, const ti b) {
    if(get<1>(a) == get<1>(b)) {
        return get<0>(a) > get<0>(b);
    }
    return get<1>(a) > get<1>(b);
}
bool cmp(const ti a, const ti b) {
    if(get<0>(a) == get<0>(b)) {
        return get<1>(a) < get<1>(b);
    }
    return get<0>(a) > get<0>(b);
}

ll solve(vector<ti>& v, int l, int r, int val) {
    int res = r+1;
    while(l <= r) {
        // cout << "wtf = " << l << " " << r << endl;
        int mid = (l+r)/2;

        if(val > get<0>(v[mid])) {
            res = mid;
            r = mid-1;
        }
        else {
            l = mid+1;
        }
    }

    return res;
}

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

    vector<ti> v, v2;
    forn(i, n) {
        int a, b, h;
        cin >> a >> b >> h;

        v.pb(mt(a, b, h, i));
    }

    sort(all(v), cmp);

    forn(i, n) {
        v[i] = mt(get<0>(v[i]), get<1>(v[i]), get<2>(v[i]), i);
    }
    v2 = v;
    sort(all(v2), cmp2);

    ST st = ST();
    st.init(n+1);

    ll res = 0;
    for(int i = 0; i < n; i++) {
        int a, b, h, j;
        tie(a, b, h, j) = v2[i];

        int idx = solve(v, 0, n-1, b);

        ll val = st.min(idx, n).ff;
        val = max(0LL, val);
        st.add(j, j, val+h);
        // cout << "wtf = " << i << endl;
//
        // cout << a << " " << b << " " << h << " " << j << " " << idx << " " << val+h << endl;

    }

    forn(i, n) {
        res = st.min(0, n).ff;
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//

#define ti tuple<int,int,int,int>

const int MAX = 100020;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

struct FT {
    vi bit;  // indexado em 1
    int n;

    FT(int n) {
        this->n = n+1;
        bit.assign(n+1, 0);
    }

    int sum(int idx) {
        int ret = 0;
        for (; idx > 0; idx -= idx & -idx)
            ret += bit[idx];
        return ret;
    }

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

    void add(int idx, int delta) {
        for (; idx < n; idx += idx & -idx)
            bit[idx] += delta;
    }
};

bool cmp2(const ti a, const ti b) {
    if(get<1>(a) == get<1>(b)) {
        return get<0>(a) < get<0>(b);
    }
    return get<1>(a) < get<1>(b);
}
bool cmp(const ti a, const ti b) {
    if(get<0>(a) == get<0>(b)) {
        return get<1>(a) < get<1>(b);
    }
    return get<0>(a) > get<0>(b);
}

ll solve(vector<ti>& v, int l, int r, int val) {
    int res = r+1;
    while(l <= r) {
        // cout << "wtf = " << l << " " << r << endl;
        int mid = (l+r)/2;

        if(val > get<0>(v[mid])) {
            res = mid;
            r = mid-1;
        }
        else {
            l = mid+1;
        }
    }

    return res;
}

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

    FT ft = FT(n+1);
    vector<ti> v, v2;
    forn(i, n) {
        int a, b, h;
        cin >> a >> b >> h;

        v.pb(mt(a, b, h, i));
    }

    sort(all(v), cmp);

    forn(i, n) {
        v[i] = mt(get<0>(v[i]), get<1>(v[i]), get<2>(v[i]), i);
    }
    v2 = v;
    sort(all(v2), cmp2);

    ll res = 0;
    for(int i = n-1; i >= 0; i--) {
        int a, b, h, j;
        tie(a, b, h, j) = v2[i];

        int idx = solve(v, i, n-1, b);

        res = max(res, 1LL*ft.sum(idx+1, n+1)+h);

        ft.add(j+1, h);
        // cout << "wtf = " << i << endl;

    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//

#define ti tuple<int,int,int,int>

const int MAX = 100020;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

struct ST {
    ll id = 0;

    int size;
    vl operations;
    vector<pair<ll,ll>> mins;

    pair<ll,ll> merg(pair<ll,ll> a, pair<ll,ll> b) {
        if(a.ff > b.ff) {
            return a;
        }
        return b;
    }

    void init(int n) {
        size = 1;
        while(size < n) size *= 2;
        mins.assign(2*size, pair<ll,ll>(0, 0));
        operations.assign(2*size, 0);
    }

    void add(int l, int r, int v, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return;
        if(l <= lx && rx <= r) {
            if(rx-lx == 1)
                mins[x].ss = lx;

            operations[x] += v;
            mins[x].ff += v;
            return;
        }

        int m = (rx + lx)/2;
        add(l, r, v, 2*x +1 , lx, m);
        add(l, r, v, 2*x +2 , m, rx);

        mins[x] = merg(mins[2*x +1], mins[2*x +2]);
        mins[x].ff += operations[x];
    }

    void add(int l, int r, int v) {
        add(l, r+1, v, 0, 0, size);
    }

    pair<ll,ll> min(int l, int r, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return mp(0, -1);
        if(l <= lx && rx <= r) {
            return mins[x];
        }

        int m = (lx + rx)/2;

        auto s1 = min(l, r, 2*x +1, lx, m);
        auto s2 = min(l, r, 2*x +2, m, rx);
        auto res = merg(s1, s2);
        res.ff += operations[x];

        return res;
    }

    pair<ll,ll> min(int l, int r) {
        return min(l, r+1, 0, 0, size);
    }
};

bool cmp2(const ti a, const ti b) {
    if(get<1>(a) == get<1>(b)) {
        return get<0>(a) < get<0>(b);
    }
    return get<1>(a) > get<1>(b);
}
bool cmp(const ti a, const ti b) {
    if(get<0>(a) == get<0>(b)) {
        return get<1>(a) > get<1>(b);
    }
    return get<0>(a) > get<0>(b);
}

ll solve(vector<ti>& v, int l, int r, int val) {
    int res = r+1;
    while(l <= r) {
        // cout << "wtf = " << l << " " << r << endl;
        int mid = (l+r)/2;

        if(val > get<0>(v[mid])) {
            res = mid;
            r = mid-1;
        }
        else {
            l = mid+1;
        }
    }

    return res;
}

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

    vector<ti> v, v2;
    forn(i, n) {
        int a, b, h;
        cin >> a >> b >> h;

        v.pb(mt(a, b, h, i));
    }

    sort(all(v), cmp);

    forn(i, n) {
        v[i] = mt(get<0>(v[i]), get<1>(v[i]), get<2>(v[i]), i);
    }
    v2 = v;
    sort(all(v2), cmp2);

    ST st = ST();
    st.init(n+1);

    ll res = 0;
    for(int i = 0; i < n; i++) {
        int a, b, h, j;
        tie(a, b, h, j) = v2[i];

        int idx = solve(v, 0, n-1, b);

        ll val = st.min(idx, n).ff;
        val = max(0LL, val);
        st.add(j, j, val+h);
        // cout << "wtf = " << i << endl;
//
        // cout << a << " " << b << " " << h << " " << j << " " << idx << " " << val+h << endl;

    }

    forn(i, n) {
        res = st.min(0, n).ff;
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//

#define ti tuple<int,int,int,int>

const int MAX = 100020;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

struct ST {
    ll id = 0;

    int size;
    vl operations;
    vector<pair<ll,ll>> mins;

    pair<ll,ll> merg(pair<ll,ll> a, pair<ll,ll> b) {
        if(a.ff > b.ff) {
            return a;
        }
        return b;
    }

    void init(int n) {
        size = 1;
        while(size < n) size *= 2;
        mins.assign(2*size, pair<ll,ll>(0, 0));
        operations.assign(2*size, 0);
    }

    void add(int l, int r, int v, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return;
        if(l <= lx && rx <= r) {
            if(rx-lx == 1)
                mins[x].ss = lx;

            operations[x] += v;
            mins[x].ff += v;
            return;
        }

        int m = (rx + lx)/2;
        add(l, r, v, 2*x +1 , lx, m);
        add(l, r, v, 2*x +2 , m, rx);

        mins[x] = merg(mins[2*x +1], mins[2*x +2]);
        mins[x].ff += operations[x];
    }

    void add(int l, int r, int v) {
        add(l, r+1, v, 0, 0, size);
    }

    pair<ll,ll> min(int l, int r, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return mp(0, -1);
        if(l <= lx && rx <= r) {
            return mins[x];
        }

        int m = (lx + rx)/2;

        auto s1 = min(l, r, 2*x +1, lx, m);
        auto s2 = min(l, r, 2*x +2, m, rx);
        auto res = merg(s1, s2);
        res.ff += operations[x];

        return res;
    }

    pair<ll,ll> min(int l, int r) {
        return min(l, r+1, 0, 0, size);
    }
};

bool cmp2(const ti a, const ti b) {
    if(get<1>(a) == get<1>(b)) {
        return get<0>(a) < get<0>(b);
    }
    return get<1>(a) > get<1>(b);
}
bool cmp(const ti a, const ti b) {
    if(get<0>(a) == get<0>(b)) {
        return get<1>(a) > get<1>(b);
    }
    return get<0>(a) > get<0>(b);
}

ll solve(vector<ti>& v, int l, int r, int val) {
    int res = r+1;
    while(l <= r) {
        // cout << "wtf = " << l << " " << r << endl;
        int mid = (l+r)/2;

        if(val > get<0>(v[mid])) {
            res = mid;
            r = mid-1;
        }
        else {
            l = mid+1;
        }
    }

    return res;
}

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

    vector<ti> v, v2;
    forn(i, n) {
        int a, b, h;
        cin >> a >> b >> h;

        v.pb(mt(a, b, h, i));
    }

    sort(all(v), cmp);

    forn(i, n) {
        v[i] = mt(get<0>(v[i]), get<1>(v[i]), get<2>(v[i]), i);
    }
    v2 = v;
    sort(all(v2), cmp2);

    ST st = ST();
    st.init(n+1);

    ll res = 0;
    for(int i = 0; i < n; i++) {
        int a, b, h, j;
        tie(a, b, h, j) = v2[i];

        int idx = solve(v, 0, n-1, b);

        ll val = st.min(idx, n).ff;
        val = max(0LL, val);
        st.add(j, j, val+h);
        // cout << "wtf = " << i << endl;

        cout << a << " " << b << " " << h << " " << j << " " << idx << " " << val+h << endl;

    }

    forn(i, n) {
        res = st.min(0, n).ff;
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//

#define ti tuple<int,int,int,int>

const int MAX = 100020;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

struct ST {
    ll id = 0;

    int size;
    vl operations;
    vector<pair<ll,ll>> mins;

    pair<ll,ll> merg(pair<ll,ll> a, pair<ll,ll> b) {
        if(a.ff > b.ff) {
            return a;
        }
        return b;
    }

    void init(int n) {
        size = 1;
        while(size < n) size *= 2;
        mins.assign(2*size, pair<ll,ll>(0, 0));
        operations.assign(2*size, 0);
    }

    void add(int l, int r, int v, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return;
        if(l <= lx && rx <= r) {
            if(rx-lx == 1)
                mins[x].ss = lx;

            operations[x] += v;
            mins[x].ff += v;
            return;
        }

        int m = (rx + lx)/2;
        add(l, r, v, 2*x +1 , lx, m);
        add(l, r, v, 2*x +2 , m, rx);

        mins[x] = merg(mins[2*x +1], mins[2*x +2]);
        mins[x].ff += operations[x];
    }

    void add(int l, int r, int v) {
        add(l, r+1, v, 0, 0, size);
    }

    pair<ll,ll> min(int l, int r, int x, int lx, int rx) {
        if(r <= lx or l >= rx) return mp(0, -1);
        if(l <= lx && rx <= r) {
            return mins[x];
        }

        int m = (lx + rx)/2;

        auto s1 = min(l, r, 2*x +1, lx, m);
        auto s2 = min(l, r, 2*x +2, m, rx);
        auto res = merg(s1, s2);
        res.ff += operations[x];

        return res;
    }

    pair<ll,ll> min(int l, int r) {
        return min(l, r+1, 0, 0, size);
    }
};

bool cmp2(const ti a, const ti b) {
    if(get<1>(a) == get<1>(b)) {
        return get<0>(a) > get<0>(b);
    }
    return get<1>(a) > get<1>(b);
}
bool cmp(const ti a, const ti b) {
    if(get<0>(a) == get<0>(b)) {
        return get<1>(a) < get<1>(b);
    }
    return get<0>(a) > get<0>(b);
}

ll solve(vector<ti>& v, int l, int r, int val) {
    int res = r+1;
    while(l <= r) {
        // cout << "wtf = " << l << " " << r << endl;
        int mid = (l+r)/2;

        if(val > get<0>(v[mid])) {
            res = mid;
            r = mid-1;
        }
        else {
            l = mid+1;
        }
    }

    return res;
}

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

    vector<ti> v, v2;
    forn(i, n) {
        int a, b, h;
        cin >> a >> b >> h;

        v.pb(mt(a, b, h, i));
    }

    sort(all(v), cmp);

    forn(i, n) {
        v[i] = mt(get<0>(v[i]), get<1>(v[i]), get<2>(v[i]), i);
    }
    v2 = v;
    sort(all(v2), cmp2);

    ST st = ST();
    st.init(n+1);

    ll res = 0;
    for(int i = 0; i < n; i++) {
        int a, b, h, j;
        tie(a, b, h, j) = v2[i];

        int idx = solve(v, 0, n-1, b);

        ll val = st.min(idx, n).ff;
        st.add(j, j, val+h);
        // cout << "wtf = " << i << endl;

        // cout << a << " " << b << " " << h << endl;

    }

    forn(i, n) {
        res = max(res, st.min(i, i).ff);
    }

    cout << res << endl;

    return 0;
}


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

#define pb push_back
#define st first
#define nd second
#define int long long

typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int N = 1e5+5;

int n , r , a[N] ,  bit[N];
pii b[N];
piii A[N];
vector<piii> vec;

void add(int p , int val){for(;p<N;p+=p&(-p)) bit[p] = max(bit[p],val);}

int query(int p){
  int resp = 0;
  for(;p>0;p-=p&(-p)) resp = max(resp,bit[p]);
  return resp;
}

bool comp(pii a , pii b){
  if(a.st > b.st) return true;
  return false;
}

main(){

  cin >> n;
  for(int i = 1 ; i <= n ; i++) cin >> A[i].nd.st >> A[i].st >> A[i].nd.nd;
  sort(A+1,A+n+1);

  for(int i = 1; i <= n ; i++){
    int sm = 0 , mn = A[i].nd.st;
    while(i < n && A[i].st == A[i+1].st) sm += A[i].nd.nd , mn = min(mn,A[i].nd.st) , i++;
    sm += A[i].nd.nd , mn = min(mn,A[i].nd.st) , vec.pb({A[i].st,{mn,sm}});
  }
  n = vec.size();

  for(int i = 0 ; i < n ; i++)
    A[i+1] = {vec[i].nd.st,{vec[i].st,vec[i].nd.nd}} , a[i+1] = A[i+1].st;
  sort(A+1,A+n+1) , sort(a+1,a+n+1);

  for(int i = 1 ; i <= n ; i++){
//    cout << A[i].st << " " << A[i].nd.st << " " << A[i].nd.nd << "\n";
    b[i] = {A[i].nd.st,i};
  }
  sort(b+1,b+n+1,comp);

  for(int i = 1 ; i <= n ; i++){
    int pos = lower_bound(a+1,a+n+1,b[i].st) - a - 1;
    add(b[i].nd,query(pos)+A[b[i].nd].nd.nd);
  }

  for(int i = 1 ; i <= n ; i++) r = max(r,bit[i]);
  cout << r << "\n";

}


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

#define pb push_back
#define st first
#define nd second
#define int long long

typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int N = 1e5+5;

int n , r , dp[N] , bit[N];
piii A[N];
vector<int> vec;

void add(int p , int val){for(;p<N;p+=p&(-p)) bit[p] += val;}

int query(int p){
  int resp = 0;
  for(;p>0;p-=p&(-p)) resp = max(resp,bit[p]);
  return resp;
}

bool comp(piii a , piii b){
  if(a.st < b.st) return true;
  else if(a.st == b.st && a.nd.st < b.nd.st) return true;
  return false;
}

main(){

  cin >> n;
  for(int i = 1 ; i <= n ; i++) cin >> A[i].nd.st >> A[i].st >> A[i].nd.nd;
  sort(A+1,A+n+1,comp);
  for(int i = 1 ; i <= n ; i++) vec.pb(A[i].st);

  for(int i = n ; i > 0 ; i--){
    int pos = upper_bound(vec.begin(),vec.end(),A[i].nd.st) - vec.begin() + 1;
//    cout << pos << " " << query(i) << " " << A[i].nd.nd << "\n";
    dp[i] = query(i) + A[i].nd.nd , add(pos,A[i].nd.nd);
  }

  for(int i = 1 ; i <= n ; i++) r = max(r,dp[i]);
  cout << r << "\n";

}


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

#define pb push_back
#define st first
#define nd second
#define int long long

typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int N = 1e5+5;

int n , r , dp[N] , bit[N];
piii B[N];
vector<int> vec;
vector<piii> A;

void add(int p , int val){for(;p<N;p+=p&(-p)) bit[p] += val;}

int query(int p){
  int resp = 0;
  for(;p>0;p-=p&(-p)) resp += bit[p];
  return resp;
}

bool comp(piii a , piii b){
  if(a.st > b.st) return true;
  else if(a.st == b.st && a.nd.st > b.nd.st) return true;
  return false;
}

main(){

  cin >> n;
  for(int i = 1 ; i <= n ; i++) cin >> B[i].nd.st >> B[i].st >> B[i].nd.nd;
  sort(B+1,B+n+1,comp);

  for(int i = 1 ; i <= n ; i++){
    int sm = 0 , mn = B[i].nd.st;
    while(i < n && B[i].st == B[i+1].st) mn = min(B[i].nd.st,mn) , sm += B[i].nd.nd , i++;
    mn = min(B[i].nd.st,mn) , sm += B[i].nd.nd , A.pb({B[i].st,{mn,sm}}) , vec.pb(B[i].st);
  }
  sort(vec.begin(),vec.end());

  for(int i = 0 ; i < A.size() ; i++){
//    cout << A[i].nd.st << " " << A[i].st << " " << A[i].nd.nd << "\n";
    int pos = vec.size() - (upper_bound(vec.begin(),vec.end(),A[i].nd.st) - vec.begin());
//    cout << i << ": " << pos << "\n";
    add(i+1,A[i].nd.nd) , add(pos+1,-A[i].nd.nd);
  }

  for(int i = 1 ; i <= n ; i++){r = max(r,query(i));}
  cout << r << "\n";

}


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

#define pb push_back
#define st first
#define nd second
#define int long long

typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int N = 1e5+5;

int n , dp[N] , bit[N];
piii A[N];
vector<int> vec;

void add(int p , int val){for(;p<N;p+=p&(-p)) bit[p] += val;}

int query(int p){
  int resp = 0;
  for(;p>0;p-=p&(-p)) resp = max(resp,bit[p]);
  return resp;
}

bool comp(piii a , piii b){
  if(a.st > b.st) return true;
  else if(a.st == b.st && a.nd.st < b.nd.st) return true;
  return false;
}

main(){

  cin >> n;
  for(int i = 1 ; i <= n ; i++) cin >> A[i].nd.st >> A[i].st >> A[i].nd.nd;
  sort(A+1,A+n+1,comp);
  for(int i = 1 ; i <= n ; i++) vec.pb(A[i].st);

  for(int i = 1 ; i <= n ; i++){
    int pos = lower_bound(vec.begin(),vec.end(),A[i].nd.st) - vec.begin() + 1;
    dp[i] = query(i) + A[i].nd.nd , add(pos,A[i].nd.nd);
  }

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

}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e6 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in[NN] , ou[NN] , H[NN] ;
map <int , pair <int , int> , greater <int>> a ;
ll mn [NN] , mx [NN] , Test , Ans , mxn[NN] ;
int Pos ;
void build (int pos , int st , int en) {
    if (st == en) {
        mx [pos] = H[st] ;
        mn [pos] = in[st] ;
        mxn[pos] = in[st] ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    build(lef , st , (st + en) / 2) ;
    build(rig , (st + en) / 2 + 1  , en) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
    mn[pos] = min(mn[lef] , mn[rig]) ;
    mxn[pos] = max(mn[lef] , mn[rig]) ;
}

void solve (int pos , int st , int en) {
    if (Pos <= st) return ;
    if (mx [pos] < Test || mn [pos] >= ou[Pos]) return ;
    if (en < Pos && mxn[pos] < ou[Pos]) {
        Test = max(Test , mx[pos]) ;
        return ;
    }
    if (st == en) {
        Test = max(mx [pos] , Test) ;
        return ;
    }
    solve(2 * pos  , st , (st + en) / 2) ;
    solve(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
void update (int pos , int st , int en) {
    if (st > Pos || en < Pos) return ;
    if (st == en) {
        mx [pos] += Test ;
        Ans = max(Ans , mx[pos]) ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    update(lef , st , (st + en) / 2 ) ;
    update(rig , (st + en) / 2 + 1  , en ) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
}
int main()
{
    int inn , oun , Hn ,nn ;
    cin >> nn ;
    forq (i , 1 , nn) {
        scanf("%d%d%d" , &inn , &oun , &Hn) ;
        auto it = a.find(oun) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , inn) , Hn + it IS .S} ;
        }
        else a[oun] = {inn , Hn} ;
    }
    int n = 0 ;
    for (auto it : a) {
        n ++ ;
        ou[n] = it.F ;
        in[n] = it.S.F ;
        H[n] = it.S .S ;
        Ans = max(Ans , H[n]) ;
    }
    build(1 , 1 , n) ;
    forq (i , 2 , n) {
        Test = 0 ; Pos = i ;
        solve(1 , 1 , n ) ;
        update(1 , 1 , n) ;
    }
    cout << Ans ;
    return 0;
}
// maximum hight with minimum inner smaller than the outer


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e6 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in[NN] , ou[NN] , H[NN] ;
map <int , pair <int , int> , greater <int>> a ;
ll mn [NN] , mx [NN] , Test , Ans ;
int Pos ;
void build (int pos , int st , int en) {
    if (st == en) {
        mx [pos] = H[st] ;
        mn [pos] = in[st] ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    build(lef , st , (st + en) / 2) ;
    build(rig , (st + en) / 2 + 1  , en) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
    mn[pos] = min(mn[lef] , mn[rig]) ;
}

void solve (int pos , int st , int en) {
    if (Pos <= st) return ;
    if (mx [pos] < Test || mn [pos] >= ou[Pos]) return ;
    if (st == en) {
        Test = max(mx [pos] , Test) ;
        return ;
    }
    solve(2 * pos  , st , (st + en) / 2) ;
    solve(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
void update (int pos , int st , int en) {
    if (st > Pos || en < Pos) return ;
    if (st == en) {
        mx [pos] += Test ;
        Ans = max(Ans , mx[pos]) ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    update(lef , st , (st + en) / 2 ) ;
    update(rig , (st + en) / 2 + 1  , en ) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
}
int main()
{
    int inn , oun , Hn ,nn ;
    cin >> nn ;
    forq (i , 1 , nn) {
        scanf("%d%d%d" , &inn , &oun , &Hn) ;
        auto it = a.find(oun) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , inn) , Hn + it IS .S} ;
        }
        else a[oun] = {inn , Hn} ;
    }
    int n = 0 ;
    for (auto it : a) {
        n ++ ;
        ou[n] = it.F ;
        in[n] = it.S.F ;
        H[n] = it.S .S ;
        Ans = max(Ans , H[n]) ;
    }
    build(1 , 1 , n) ;
    forq (i , 2 , n) {
        Test = 0 ; Pos = i ;
        solve(1 , 1 , n ) ;
        update(1 , 1 , n) ;
    }
    cout << Ans ;
    return 0;
}
// maximum hight with minimum inner smaller than the outer


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e6 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in[NN] , ou[NN] , H[NN] ;
map <ll , pair <ll , ll> , greater <ll>> a ;
ll mn [NN] , mx [NN] ;
void build (int pos , int st , int en) {
    if (st == en) {
        mx [pos] = H[st] ;
        mn [pos] = in[st] ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    build(lef , st , (st + en) / 2) ;
    build(rig , (st + en) / 2 + 1  , en) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
    mn[pos] = min(mn[lef] , mn[rig]) ;
}
ll Test ;
int Pos ;
void solve (int pos , int st , int en) {
    if (Pos <= st) return ;
    if (mx [pos] < Test || mn [pos] >= ou[Pos]) return ;
    if (st == en) {
        Test = max(mx [pos] , Test) ;
        return ;
    }
    solve(2 * pos  , st , (st + en) / 2) ;
    solve(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
void update (int pos , int st , int en) {
    if (st > Pos || en < Pos) return ;
    if (st == en) {
        mx [pos] += Test ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    update(lef , st , (st + en) / 2 ) ;
    update(rig , (st + en) / 2 + 1  , en ) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
}
void sho (int pos , int st , int en) {
    cout << st << " -> "<< en << " " << mx[pos] << " " << mn[pos] << endl ;
    if (st == en) return ;
    sho(2 * pos , st , (st + en) / 2) ;
    sho(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
int main()
{
    ll inn , oun , Hn ,nn ;
    cin >> nn ;
    forq (i , 1 , nn) {
        scanf("%I64d%I64d%I64d" , &inn , &oun , &Hn) ;
        auto it = a.find(oun) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , inn) , Hn + it IS .S} ;
        }
        else a[oun] = {inn , Hn} ;
    }
    int n = 0 ;
    for (auto it : a) {
        n ++ ;
        ou[n] = it.F ;
        in[n] = it.S.F ;
        H[n] = it.S .S ;
    }
    build(1 , 1 , n) ;
    forq (i , 2 , n) {
        Test = 0 ; Pos = i ;
        solve(1 , 1 , n ) ;
        update(1 , 1 , n) ;
    }
    ll Ans = 0 ;
    forq (i , 1 , NN - 1) Ans = max(Ans , mx[i]) ;
    cout << Ans ;
    return 0;
}
// maximum hight with minimum inner smaller than the outer


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e5 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in , ou , H ;
map <ll , pair <ll , ll> , greater <ll>> a ;
map <ll , ll , greater <ll> > inner ;
int main()
{
    cin >> n ;
    forq (i , 1 , n) {
        scanf("%I64d%I64d%I64d" , &in , &ou , &H) ;
        auto it = a.find(ou) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , in) , H + it IS .S} ;
        }
        else a[ou] = {in , H} ;
    }
    for (auto it : a) {
        //cout << it.F << " " << it.S.F << " " << it.S.S << "a\n" ;
        ll res = 0 ; // it.S.S ;
        for (auto it2 : inner) {
            if (it2.S < it.F) {
                res = max( res , it2.F ) ;  break ;
            }
        }
        inner[res + it.S.S] = it.S.F ;
    }
    cout << inner.begin() IF ;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e6 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in[NN] , ou[NN] , H[NN] ;
map <int , pair <int , int> , greater <int>> a ;
ll mn [NN] , mx [NN] , Test , Ans ;
int Pos ;
void build (int pos , int st , int en) {
    if (st == en) {
        mx [pos] = H[st] ;
        mn [pos] = in[st] ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    build(lef , st , (st + en) / 2) ;
    build(rig , (st + en) / 2 + 1  , en) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
    mn[pos] = min(mn[lef] , mn[rig]) ;
}

void solve (int pos , int st , int en) {
    if (Pos <= st) return ;
    if (mx [pos] < Test || mn [pos] >= ou[Pos]) return ;
    if (en < Pos) {
        Test = max(Test , mx[pos]) ;
        return ;
    }
    if (st == en) {
        Test = max(mx [pos] , Test) ;
        return ;
    }
    solve(2 * pos  , st , (st + en) / 2) ;
    solve(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
void update (int pos , int st , int en) {
    if (st > Pos || en < Pos) return ;
    if (st == en) {
        mx [pos] += Test ;
        Ans = max(Ans , mx[pos]) ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    update(lef , st , (st + en) / 2 ) ;
    update(rig , (st + en) / 2 + 1  , en ) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
}
int main()
{
    int inn , oun , Hn ,nn ;
    cin >> nn ;
    forq (i , 1 , nn) {
        scanf("%d%d%d" , &inn , &oun , &Hn) ;
        auto it = a.find(oun) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , inn) , Hn + it IS .S} ;
        }
        else a[oun] = {inn , Hn} ;
    }
    int n = 0 ;
    for (auto it : a) {
        n ++ ;
        ou[n] = it.F ;
        in[n] = it.S.F ;
        H[n] = it.S .S ;
        Ans = max(Ans , H[n]) ;
    }
    build(1 , 1 , n) ;
    forq (i , 2 , n) {
        Test = 0 ; Pos = i ;
        solve(1 , 1 , n ) ;
        update(1 , 1 , n) ;
    }
    cout << Ans ;
    return 0;
}
// maximum hight with minimum inner smaller than the outer


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e6 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in[NN] , ou[NN] , H[NN] ;
map <int , pair <int , int> , greater <int>> a ;
ll mn [NN] , mx [NN] , Test , Ans , mxn[NN] ;
int Pos ;
void build (int pos , int st , int en) {
    if (st == en) {
        mx [pos] = H[st] ;
        mn [pos] = in[st] ;
        mxn[pos] = in[st] ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    build(lef , st , (st + en) / 2) ;
    build(rig , (st + en) / 2 + 1  , en) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
    mn[pos] = min(mn[lef] , mn[rig]) ;
    mxn[pos] = max(mxn[lef] , mxn[rig]) ;
}

void solve (int pos , int st , int en) {
    if (Pos <= st) return ;
    if (mx [pos] < Test || mn [pos] >= ou[Pos]) return ;
    if (en < Pos && mxn[pos] < ou[Pos]) {
        Test = max(Test , mx[pos]) ;
        return ;
    }
    if (st == en) {
        Test = max(mx [pos] , Test) ;
        return ;
    }
    solve(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
    solve(2 * pos  , st , (st + en) / 2) ;
}
void update (int pos , int st , int en) {
    if (st > Pos || en < Pos) return ;
    if (st == en) {
        mx [pos] += Test ;
        Ans = max(Ans , mx[pos]) ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    update(lef , st , (st + en) / 2 ) ;
    update(rig , (st + en) / 2 + 1  , en ) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
}
int main()
{
    int inn , oun , Hn ,nn ;
    cin >> nn ;
    forq (i , 1 , nn) {
        scanf("%d%d%d" , &inn , &oun , &Hn) ;
        auto it = a.find(oun) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , inn) , Hn + it IS .S} ;
        }
        else a[oun] = {inn , Hn} ;
    }
    int n = 0 ;
    for (auto it : a) {
        n ++ ;
        ou[n] = it.F ;
        in[n] = it.S.F ;
        H[n] = it.S .S ;
        Ans = max(Ans , H[n]) ;
    }
    build(1 , 1 , n) ;
    forq (i , 2 , n) {
        Test = 0 ; Pos = i ;
        solve(1 , 1 , n ) ;
        update(1 , 1 , n) ;
    }
    cout << Ans ;
    return 0;
}
// maximum hight with minimum inner smaller than the outer


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define read(x) scanf( "%lld" , &x )
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define carr( n , array ) forr(i , n) read(array[i])
#define coutarr(n , array ) cout << "\n" ; forr (i , n) printf("%lld " , array[i]) ; cout << "\n" ;
#define F first
#define S second
#define IF ->first
#define IS ->second
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define sizee(vec) (ll)vec.size()

ll R = 7 + 1e9 , NUMTESTCASE ;
const int NN = 10 + 1e5 ;
const double pi = acos(-1.0) ;
int di [4] = {1 , 0 , -1 , 0} , dj [4] = {0 , 1 , 0 , -1} , DI [8] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1} , DJ [8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1} ;
using namespace std;
ll n , memo[NN] , in[NN] , ou[NN] , H[NN] ;
map <ll , pair <ll , ll> , greater <ll>> a ;
ll mn [NN] , mx [NN] ;
void build (int pos , int st , int en) {
    if (st == en) {
        mx [pos] = H[st] ;
        mn [pos] = in[st] ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    build(lef , st , (st + en) / 2) ;
    build(rig , (st + en) / 2 + 1  , en) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
    mn[pos] = min(mn[lef] , mn[rig]) ;
}
ll Test ;
int Pos ;
void solve (int pos , int st , int en) {
    if (Pos <= st) return ;
    if (mx [pos] < Test || mn [pos] >= ou[Pos]) return ;
    if (st == en) {
        Test = max(mx [pos] , Test) ;
        return ;
    }
    solve(2 * pos  , st , (st + en) / 2) ;
    solve(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
void update (int pos , int st , int en) {
    if (st > Pos || en < Pos) return ;
    if (st == en) {
        mx [pos] += Test ;
        return ;
    }
    int lef = 2 * pos , rig = 2 * pos + 1 ;
    update(lef , st , (st + en) / 2 ) ;
    update(rig , (st + en) / 2 + 1  , en ) ;
    mx[pos] = max(mx[lef] , mx[rig]) ;
}
void sho (int pos , int st , int en) {
    cout << st << " -> "<< en << " " << mx[pos] << " " << mn[pos] << endl ;
    if (st == en) return ;
    sho(2 * pos , st , (st + en) / 2) ;
    sho(2 * pos + 1 , (st + en) / 2 + 1 , en) ;
}
int main()
{
    ll inn , oun , Hn ,nn ;
    cin >> nn ;
    forq (i , 1 , nn) {
        scanf("%I64d%I64d%I64d" , &inn , &oun , &Hn) ;
        auto it = a.find(oun) ;
        if (it != a.end()) {
            it IS = {min(it IS.F , inn) , Hn + it IS .S} ;
        }
        else a[oun] = {inn , Hn} ;
    }
    int n = 0 ;
    for (auto it : a) {
        n ++ ;
        ou[n] = it.F ;
        in[n] = it.S.F ;
        H[n] = it.S .S ;
    }
    build(1 , 1 , n) ;
    forq (i , 2 , n) {
        Test = 0 ; Pos = i ;
        solve(1 , 1 , n ) ;
        update(1 , 1 , n) ;
    }
    ll Ans = 0 ;
    forq (i , 1 , NN - 1) Ans = max(Ans , mx[i]) ;
    cout << Ans ;
    return 0;
}
// maximum hight with minimum inner smaller than the outer


#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

inline void inp(ll n, vector<ll> &v)
{
    ll temp;
    for(ll i=0;i<n;i++)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

inline void outp(ll n, vector<ll> &v)
{
    for(ll i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
}

vector<ll> bitmask(ll number, ll k, ll limit)
{
    vector<ll> bm(limit, 0);

    while(number!=0)
    {
        ll index = floor(log10(number)/log10(k));
        bm[index]+=1;
        number -= pow(k, index);
    }

    return bm;
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;

    while(t--)
    {
        ll n, i, j, temp, flag=0, flag2, count=0, x, y, a, b, m, ans=0, k;
        string st, st2, st3;
        cin>>n>>k;

        ld limit = ceil(16.0 / log10(k));
        vector<ll> v(limit, 0), bm[n], answer(limit, 0);

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

            bm[i] = bitmask(temp, k, limit);

            //outp(limit, bm[i]);
            //cout<<endl;

        }
        flag=0;
        for(i=0;i<limit;i++)
        {
            ans =0; flag=0;
            for(j=0;j<n;j++)
            {
                if(bm[j][i]==1)
                {
                    flag++;
                }
                else if(bm[j][i]>1)
                {
                    flag=-1;
                    cout<<"NO"<<endl;
                    break;
                }

                if(flag>1)
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
            if(flag>1 || flag==-1)
            {
                break;
            }
            answer[i] = ans;
        }

        if(flag==1 || flag==0)
            cout<<"YES"<<endl;

     }
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

inline void inp(ll n, vector<ll> &v)
{
    ll temp;
    for(ll i=0;i<n;i++)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

inline void outp(ll n, vector<ll> &v)
{
    for(ll i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
}

vector<ll> bitmask(ll number, ll k, ll limit)
{
    vector<ll> bm(limit, 0);

    while(number!=0)
    {
        ll index = floor(log10(number)/log10(k));
        bm[index]=1;
        number -= pow(k, index);
    }

    return bm;
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;

    while(t--)
    {
        ll n, i, j, temp, flag=0, flag2, count=0, x, y, a, b, m, ans=0, k;
        string st, st2, st3;
        cin>>n>>k;

        ld limit = ceil(16.0 / log10(k));
        vector<ll> v(limit, 0), bm[n], answer(limit, 0);

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

            bm[i] = bitmask(temp, k, limit);

            //outp(limit, bm[i]);
            //cout<<endl;

        }
        flag=0;
        for(i=0;i<limit;i++)
        {
            ans =0; flag=0;
            for(j=0;j<n;j++)
            {
                if(bm[j][i]==1)
                {
                    flag++;
                }

                if(flag>1)
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
            if(flag>1)
            {
                break;
            }
            answer[i] = ans;
        }

        if(flag<=1)
            cout<<"YES"<<endl;

     }
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

inline void inp(ll n, vector<ll> &v)
{
    ll temp;
    for(ll i=0;i<n;i++)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

inline void outp(ll n, vector<ll> &v)
{
    for(ll i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
}

vector<ll> bitmask(ll number, ll k, ll limit)
{
    vector<ll> bm(limit, 0);

    while(number!=0)
    {
        ll index = floor(log10(number)/log10(k));
        bm[index]+=1;
        number -= pow(k, index);
    }

    return bm;
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;

    while(t--)
    {
        ll n, i, j, temp, flag=0, flag2, count=0, x, y, a, b, m, ans=0, k;
        string st, st2, st3;
        cin>>n>>k;

        ld limit = ceil(16.0 / log10(k))+1;
        vector<ll> v(limit, 0), bm[n], answer(limit, 0);

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

            bm[i] = bitmask(temp, k, limit);
            //outp(limit, bm[i]);
            //cout<<endl;

        }
        flag=0;
        for(i=0;i<limit;i++)
        {
            ans =0; flag=0;
            for(j=0;j<n;j++)
            {

                flag+=bm[j][i];

                if(flag>1)
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
            if(flag>1)
            {
                break;
            }
            answer[i] = ans;
        }

        if(flag<=1)
            cout<<"YES"<<endl;

     }
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

ll power(ll x, unsigned ll y, ll p)
{
    ll res = 1;     // Initialize result

    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

inline void inp(ll n, vector<ll> &v)
{
    ll temp;
    for(ll i=0;i<n;i++)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

inline void outp(ll n, vector<ll> &v)
{
    for(ll i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
}

vector<ll> bitmask(ll number, ll k, ll limit)
{
    vector<ll> bm(limit, 0);

    while(number!=0)
    {
        ll index = floor(log10(number)/log10(k));
        bm[index]+=1;
        number -= power(k, index, 1e16 +1);
    }

    return bm;
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;

    while(t--)
    {
        ll n, i, j, temp, flag=0, flag2, count=0, x, y, a, b, m, ans=0, k;
        string st, st2, st3;
        cin>>n>>k;

        ld limit = ceil(16.0 / log10(k))+1;
        vector<ll> v(limit, 0), bm[n], answer(limit, 0);

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

            bm[i] = bitmask(temp, k, limit);
            //outp(limit, bm[i]);
            //cout<<endl;

        }
        flag=0;
        for(i=0;i<limit;i++)
        {
            ans =0; flag=0;
            for(j=0;j<n;j++)
            {

                flag+=bm[j][i];

                if(flag>1)
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
            if(flag>1)
            {
                break;
            }
            answer[i] = ans;
        }

        if(flag<=1)
            cout<<"YES"<<endl;

     }
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

ll power(ll x, int y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
    {
        if(y > 0)
            return x * temp * temp;
        else
            return (temp * temp) / x;
    }
}

inline void inp(ll n, vector<ll> &v)
{
    ll temp;
    for(ll i=0;i<n;i++)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

inline void outp(ll n, vector<ll> &v)
{
    for(ll i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
}

vector<ll> bitmask(ll number, ll k, ll limit)
{
    vector<ll> bm(limit, 0);

    while(number!=0)
    {
        ll index = floor(log10(number)/log10(k));
        bm[index]+=1;
        number -= power(k, index);
    }

    return bm;
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;

    while(t--)
    {
        ll n, i, j, temp, flag=0, flag2, count=0, x, y, a, b, m, ans=0, k;
        string st, st2, st3;
        cin>>n>>k;

        ld limit = ceil(16.0 / log10(k))+1;
        vector<ll> v(limit, 0), bm[n], answer(limit, 0);

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

            bm[i] = bitmask(temp, k, limit);
            //outp(limit, bm[i]);
            //cout<<endl;

        }
        flag=0;
        for(i=0;i<limit;i++)
        {
            ans =0; flag=0;
            for(j=0;j<n;j++)
            {

                flag+=bm[j][i];

                if(flag>1)
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
            if(flag>1)
            {
                break;
            }
            answer[i] = ans;
        }

        if(flag<=1)
            cout<<"YES"<<endl;

     }
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

ll power(ll x, unsigned ll y, ll p)
{
    ll res = 1;     // Initialize result

    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

inline void inp(ll n, vector<ll> &v)
{
    ll temp;
    for(ll i=0;i<n;i++)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

inline void outp(ll n, vector<ll> &v)
{
    for(ll i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
}

vector<ll> bitmask(ll number, ll k, ll limit)
{
    vector<ll> bm(limit, 0);

    while(number!=0)
    {
        ll index = floor(log10(number)/log10(k));
        bm[index]+=1;
        number -= power(k, index);
    }

    return bm;
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;

    while(t--)
    {
        ll n, i, j, temp, flag=0, flag2, count=0, x, y, a, b, m, ans=0, k;
        string st, st2, st3;
        cin>>n>>k;

        ld limit = ceil(16.0 / log10(k))+1;
        vector<ll> v(limit, 0), bm[n], answer(limit, 0);

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

            bm[i] = bitmask(temp, k, limit);
            //outp(limit, bm[i]);
            //cout<<endl;

        }
        flag=0;
        for(i=0;i<limit;i++)
        {
            ans =0; flag=0;
            for(j=0;j<n;j++)
            {

                flag+=bm[j][i];

                if(flag>1)
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
            if(flag>1)
            {
                break;
            }
            answer[i] = ans;
        }

        if(flag<=1)
            cout<<"YES"<<endl;

     }
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[32];
        for(int i=0;i<32;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if(tmp[x]==1&&a%k==1)
                    {f=false;
                    break;}
                    if(tmp[x]==-1)
                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[50];
        for(int i=0;i<50;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if((tmp[x]!=-1)&&(a%k==1))
                    {f=false;
                    break;}
                    if(tmp[x]==-1)
                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[32];
        for(int i=0;i<32;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {

            ll tmp[32];
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if(tmp[x]==1&&a%k==1)
                    {f=false;
                    break;}

                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

        vector<ll>tmp(60,0);
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            ll x=0;
            while(a>0)
            {
                tmp[x]+=a%k;
                a=a/k;
                x++;
            }
        }
        if(*max_element(tmp.begin(),tmp.end())<2)
        cout<<"YES\n";
        else
        cout<<"NO\n";

    }
    return 0;
}#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

        vector<ll>tmp(60,0);
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            ll x=0;
            while(a>0)
            {
                tmp[x]+=a%k;
                a=a/k;
                x++;
            }
        }
        if(*max_element(tmp.begin(),tmp.end())<2)
        cout<<"YES\n";
        else
        cout<<"NO\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[32];
        for(int i=0;i<32;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if(tmp[x]==1&&a%k==1)
                    {f=false;
                    break;}

                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

        vector<ll>tmp(60,0);
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            ll x=0;
            while(a>0)
            {
                tmp[x]+=a%k;
                a=a/k;
                x++;
            }
        }
        if(*max_element(tmp.begin(),tmp.end())<2)
        cout<<"YES\n";
        else
        cout<<"NO\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[32];
        for(int i=0;i<32;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if(tmp[x]!=-1&&a%k==1)
                    {f=false;
                    break;}
                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[50];
        for(int i=0;i<50;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if((tmp[x]!=-1)&&(a%k==1))
                    {f=false;
                    break;}
                    if(tmp[x]!=-1)
                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }
                if(!f)
                break;

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000000000000001

ll root(ll x,vector<ll>&v)
{
    while(v[x]!=x)
    {
        v[x]=v[v[x]];
        x=v[x];
    }
    return x;
}

int main()
{
     fast;
    ll t;
    // t=1;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n);
        for(auto &x:v)cin>>x;
        sort(all(v));

bool f=true;
        int tmp[50];
        for(int i=0;i<50;i++)
        tmp[i]=-1;
        for(int i=0;i<n;i++)
        {
            ll a=v[i];
            int x=0;
            while(a>0)
            {
                if(a%k==0||a%k==1)
                {
                    if((tmp[x]==1)&&(a%k==1))
                    {f=false;
                    break;}
                    if(tmp[x]==0)
                    tmp[x]=(int)(a%k);
                    a=a/k;
                    x++;
                }
                else
                {
                    f=false;
                    break;
                }
                if(!f)
                break;

            }
            if(!f)
            {cout<<"NO\n";break;}
        }
        if(f)
        cout<<"YES\n";

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n,k;
        cin >> n>>k;
        int a[n];
        int ans = 0,f=0;
        map<int, int> mp;
for (int i = 0; i < n;i++)
cin>>a[i];
        for (int i = 0; i < n;i++){

            if(a[i]==0){
                continue;
            }
            ans = 1;
            int g = 0;
            while(ans*k<=a[i]){
                ans = ans * k;
                g++;
            }

            if(mp[g]){
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0;j--){
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if(ans+u<=a[i]){
                    if(mp[j]){
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                    ans=ans+u;
                }
            }
            if(ans!=a[i])
            f=1;
            if(f){
                break;
            }
        }
        if(!f){
            cout << "YES" << endl;
        }
        else{

            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++)
        {

            if (a[i] == 0)
            {
                continue;
            }
            ans = 1;
            int g = 0;
            while (ans * k <= a[i])
            {
                ans = ans * k;
                g++;
            }
            //cout << g << " ";
            if (mp[g])
            {
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0; j--)
            {
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if (ans + u <= a[i])
                {
                    if (mp[j])
                    {
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                }
            }
            if(ans!=a[i])
                f = 1;
        }
        if (!f)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        unordered_map<int, int> mp;

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] != 0)
            {

                ans = 1;
                int g = 0;
                while (ans*k < a[i])
                {
                    ans = ans * k;
                    g++;
                }
                 if(ans*k==a[i]){
                    g++;
                    ans*=k;
                 }
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {

                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                        ans = ans + u;
                    }
                }
                if (f)
                {
                    break;
                }
            }
        }
        if (!f)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        int mp[1000] = {0};

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] != 0)
            {

                ans = 1;
                int g = 0;
                while (ans < a[i])
                {
                    ans = ans * k;
                    g++;
                }
                if(ans>a[i]){
                    ans /= k;
                    g--;
                }
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {

                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                        ans = ans + u;
                    }
                }
                if(ans!=a[i]){
                    f = 1;
                }
                if (f)
                {
                    break;
                }
            }
        }
        if (!f)
        {
            cout << "YES" << "\n";
        }
        else
        {
            cout << "NO" << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] != 0)
            {

                ans = 1;
                int g = 0;
                while (ans < a[i])
                {
                    ans = ans * k;
                    g++;
                }
                if(ans>a[i]){
                    ans /= k;
                    g--;
                }
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {

                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                        ans = ans + u;
                    }
                }
                if(ans!=a[i]){
                    f = 1;
                }
                if (f)
                {
                    break;
                }
            }
        }
        if (!f)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++)
        {

            if (a[i] == 0)
            {
                continue;
            }
            ans = 1;
            int g = 0;
            while (ans * k <= a[i])
            {
                ans = ans * k;
                g++;
            }
            //cout << g << " ";
            if (mp[g])
            {
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0; j--)
            {
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if (ans + u <= a[i])
                {
                    if (mp[j])
                    {
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                    ans = ans + u;
                }
            }
            if(ans!=a[i])
                f = 1;
        }
        if (!f)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n,k;
        cin >> n>>k;
        int a[n];
        int ans = 0,f=0;
        map<int, int> mp;
for (int i = 0; i < n;i++)
cin>>a[i];
        for (int i = 0; i < n;i++){

            if(a[i]==0){
                continue;
            }
            ans = 1;
            int g = 0;
            while(ans*k<=a[i]){
                ans = ans * k;
                g++;
            }

            if(mp[g]){
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0;j--){
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if(ans+u<=a[i]){
                    if(mp[j]){
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                    ans=ans+u;
                }
            }
            if(ans!=a[i])
            f=1;
            if(f ){
                break;
            }
        }
        if(!f){
            cout << "YES" << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {

        int n,k;
        cin >> n>>k;
        int a[n];
        int ans = 0,f=0;
        map<int, int> mp;
for (int i = 0; i < n;i++)
cin>>a[i];
        for (int i = 0; i < n;i++){

            if(a[i]==0){
                continue;
            }
            ans = 1;
            int g = 0;
            while(ans*k<=a[i]){
                ans = ans * k;
                g++;
            }

            if(mp[g]){
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0;j--){
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if(ans+u<=a[i]){
                    if(mp[j]){
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                    ans=ans+u;
                }
            }
            if(f){
                break;
            }
        }
        if(!f){
            cout << "YES" << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n,k;
        cin >> n>>k;
        int a[n];
        int ans = 0,f=0;
        map<int, int> mp;
for (int i = 0; i < n;i++)
cin>>a[i];
        for (int i = 0; i < n;i++){

            if(a[i]==0){
                continue;
            }
            ans = 1;
            int g = 0;
            while(ans*k<=a[i]){
                ans = ans * k;
                g++;
            }

            if(mp[g]){
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0;j--){
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if(ans+u<=a[i]){
                    if(mp[j]){
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                    ans=ans+u;
                }
            }
            if(f){
                break;
            }
        }
        if(!f){
            cout << "YES" << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define endl "\n"
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        int mp[1000] = {0};
         for(int i=0;i<n;i++){
             cin >> a[i];
         }
        for (int i = 0; i < n; i++)
        {

            if (a[i] != 0)
            {

                ans = 1;
                int g = 0;
                while (ans < a[i])
                {
                    ans = ans * k;
                    g++;
                }
                if(ans>a[i]){
                    ans /= k;
                    g--;
                }
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {

                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                        ans = ans + u;
                    }
                }
                if(ans!=a[i]){
                    f = 1;
                }
                if (f)
                {
                    break;
                }
            }
        }
        if (!f)
        {
            cout << "YES" << "\n";
        }
        else
        {
            cout << "NO" << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;

        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] != 0)
            {

                ans = 1;
                int g = 0;
                while (ans < a[i])
                {
                    ans = ans * k;
                    g++;
                }
                if(ans>a[i]){
                    ans /= k;
                    g--;
                }
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {

                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                        ans = ans + u;
                    }
                }
                if(ans!=a[i]){
                    f = 1;
                }
                if (f)
                {
                    break;
                }
            }
        }
        if (!f)
        {
            cout << "YES" << "\n";
        }
        else
        {
            cout << "NO" << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n,k;
        cin >> n>>k;
        int a[n];
        int ans = 0,f=0;
        map<int, int> mp;
for (int i = 0; i < n;i++)
cin>>a[i];
        for (int i = 0; i < n;i++){

            if(a[i]==0){
                continue;
            }
            ans = 1;
            int g = 0;
            while(ans*k<=a[i]){
                ans = ans * k;
                g++;
            }

            if(mp[g]){
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0;j--){
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if(ans+u<=a[i]){
                    if(mp[j]){
                 c       f = 1;
                        break;
                    }
                    mp[j] = 1;
                    ans=ans+u;
                }
            }
            if(ans!=a[i])
            f=1;
            if(f){
                break;
            }
        }
        if(!f){
            cout << "YES" << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++)
        {

            if (a[i] == 0)
            {
                continue;
            }
            ans = 1;
            int g = 0;
            while (ans * k <= a[i])
            {
                ans = ans * k;
                g++;
            }
            //cout << g << " ";
            if (mp[g])
            {
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0; j--)
            {
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if (ans + u <= a[i])
                {
                    if (mp[j])
                    {
                        f = 1;
                        bre#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++)
        {

            if (a[i] == 0)
            {
                continue;
            }
            ans = 1;
            int g = 0;
            while (ans * k <= a[i])
            {
                ans = ans * k;
                g++;
            }
            //cout << g << " ";
            if (mp[g])
            {
                f = 1;
                break;
            }
            mp[g] = 1;
            for (int j = g - 1; j >= 0; j--)
            {
                //cout << j << " ";
                int u;
                u = pow(k, j);
                if (ans + u <= a[i])
                {
                    if (mp[j])
                    {
                        f = 1;
                        break;
                    }
                    mp[j] = 1;
                }
            }
            if(ans!=a[i])
                f = 1;
        }
        if (!f)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define endl "\n"
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc = 1;
    cin >> tc;
    while (tc--)a
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        int mp[1000] = {0};
         for(int i=0;i<n;i++){
             cin >> a[i];
         }
        for (int i = 0; i < n; i++)
        {

            if (a[i] != 0)
            {

                ans = 1;
                int g = 0;
                while (ans < a[i])
                {
                    ans = ans * k;
                    g++;
                }
                if(ans>a[i]){
                    ans /= k;
                    g--;
                }
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {

                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                        ans = ans + u;
                    }
                }
                if(ans!=a[i]){
                    f = 1;
                }
                if (f)
                {
                    break;
                }
            }
        }
        if (!f)
        {
            cout << "YES" << "\n";
        }
        else
        {
            cout << "NO" << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
int32_t main()
{
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        int ans = 0, f = 0;
        map<int, int> mp;
        for (int i = 0; i < n;i++){
            cin > a[i];
        }
            for (int i = 0; i < n; i++)
            {

                if (a[i] == 0)
                {
                    continue;
                }
                ans = 1;
                int g = 0;
                while (ans * k <= a[i])
                {
                    ans = ans * k;
                    g++;
                }
                //cout << g << " ";
                if (mp[g])
                {
                    f = 1;
                    break;
                }
                mp[g] = 1;
                for (int j = g - 1; j >= 0; j--)
                {
                    //cout << j << " ";
                    int u;
                    u = pow(k, j);
                    if (ans + u <= a[i])
                    {
                        if (mp[j])
                        {
                            f = 1;
                            break;
                        }
                        mp[j] = 1;
                    }
                }
            }
        if (!f)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}