#include using namespace std; using ll = long long; using pii = pair; #define all(x) x.begin(), x.end() struct Phone { int h, w, t; bool operator<(const Phone &rhs) const { if (h == rhs.h) { return w > rhs.w; } return h < rhs.h; } }; struct BIT { vector data; BIT(int n): data(n + 1) {} ll getmax(int a) { ll ret = 0; for(int i = a; i > 0; i -= i & -i) { ret = max(ret, data[i]); } return ret; } void update(int a, ll w) { for(int i = a; i < data.size(); i += i & -i) { data[i] = max(data[i], w); } } }; ll LIS(vector &A) { vector dp; for(int i = 0; i < A.size(); i++) { int k = lower_bound(all(dp), A[i]) - dp.begin(); if (k == dp.size()) dp.push_back(A[i]); else dp[k] = A[i]; } return dp.size(); } ll LIS_dp(vector &A) { vector dp(A.size()); for (int i = 0; i < A.size(); i++) { ll ma = 0; for (int j = 0; j < i; j++) { if (A[j] >= A[i]) continue; ma = max(ma, dp[j]); } dp[i] = ma + 1; } return *max_element(all(dp)); } // BIT解(想定解, O(N log N)) ll solve1(int N, vector &P) { vector W(N); for (int i = 0; i < N; i++) { W[i] = P[i].w; } sort(all(W)); W.erase(unique(W.begin(), W.end()), W.end()); BIT bit(N + 2); for(int i = 0; i < N; i++) { int k = lower_bound(all(W), P[i].w) - W.begin() - 1; k += 2; // for BIT ll mi = bit.getmax(k); bit.update(k + 1, mi + P[i].t); } return bit.getmax(N + 2); } // Naive DP解(O(N^2)) ll solve2(int N, vector &P) { vector dp(N, 0); for(int i = 0; i < N; i++) { ll ma = 0; for(int j = 0; j < i; j++) { if (P[j].w >= P[i].w) continue; ma = max(ma, dp[j]); } dp[i] = ma + P[i].t; } return *max_element(all(dp)); } // 二分探索 LIS解(O(N log N), T_i = 1) ll solve3(int N, vector &P) { vector W(N); for (int i = 0; i < N; i++) { W[i] = P[i].w; } return LIS(W); } // LIS DP解(O(N^2), T_i = 1) ll solve4(int N, vector &P) { vector W(N); for (int i = 0; i < N; i++) { W[i] = P[i].w; } return LIS_dp(W); } // 全探索解(O(2^N N)) ll solve5(int N, vector &P) { ll ans = 0; for(int i = 0; i < 1 << N; i++) { vector W; ll sum = 0; for(int j = 0; j < N; j++) { if (i >> j & 1) { W.push_back(P[j].w); sum += P[j].t; } } bool flag = true; for(int j = 0; j + 1 < W.size(); j++) { if (W[j] >= W[j + 1]) { flag = false; break; } } if (!flag) continue; ans = max(ans, sum); } return ans; } // 前処理 いらないPhoneの削除と、順番の並び替えを行う void preprocess(vector &P) { int N = P.size(); map M; for(int i = 0; i < N; i++) { M[{P[i].h, P[i].w}] = max(M[{P[i].h, P[i].w}], P[i].t); } vector newP; for(auto &&x : M) { newP.push_back({x.first.first, x.first.second, x.second}); } P = newP; sort(all(P)); } void solve() { int N; cin >> N; if (N < 1 || N > 200000) exit(1); vector P; for(int i = 0; i < N; i++) { int h, w, t; cin >> h >> w >> t; if (h < 1 || h > 1000000000) exit(1); if (w < 1 || w > 1000000000) exit(1); if (t < 1 || t > 1000000000) exit(1); P.push_back({h, w, t}); } preprocess(P); N = P.size(); constexpr int num_solver = 5; bool solve3_f = true; for (int i = 0; i < N; i++) { if (P[i].t != 1) solve3_f = false; } bool solve4_f = solve3_f && (N <= 2000); bool flags[num_solver] = {true, N <= 2000, solve3_f, solve4_f, N <= 16}; ll (*solvers[num_solver])(int, vector &) = {solve1, solve2, solve3, solve4, solve5}; vector ans; for (int i = 0; i < num_solver; i++) { if (flags[i]) ans.push_back(solvers[i](N, P)); } set S; for (int i = 0; i < ans.size(); i++) { S.insert(ans[i]); } if (S.size() > 1) { cout << "Error!" << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } } else { cout << ans[0] << endl; } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }