#include #include #include #include #include struct Customer{ int i, j, w; Customer(int I_, int J_, int W_): i(I_), j(J_), w(W_){} bool operator < (const Customer & c) const { if(j != c.j){return j < c.j;} if(i != c.i){return i < c.i;} return w < c.w; } }; int cache[400][400][2]; int get(const std::vector & customers, int begin, int end, int used = -1){ //std::cout << begin << ' ' << end << ' ' << used << std::endl; if(begin <= 0 || end <= 0 || begin > end){ return 0; } if(cache[begin][end][used == -1? 0: 1] >= 0){ return cache[begin][end][used == -1? 0: 1]; } int k = get(customers, begin, end - 1, used); if(begin == end){ int u = used; if(u >= 0 && begin == customers[u].i){u = -1;} k = std::max(get(customers, begin - 1, end - 1, u), k); } for(int i = 0; i < customers.size(); i++){ if(i == used){continue;} if(customers[i].j == end){ if(used < 0){ //assert(begin == end); k = std::max(get(customers, customers[i].i, customers[i].j, i) + customers[i].w, k); }else if(customers[i].i > begin){ k = std::max(get(customers, begin, customers[i].i - 1, used) + customers[i].w, k); }else if(customers[i].i == begin){ k = std::max(get(customers, begin - 1, begin - 1) + customers[i].w, k); }else{ k = std::max(get(customers, customers[i].i, begin - 1, i) + customers[i].w, k); } } } cache[begin][end][used == -1? 0: 1] = k; return k; } int main(){ while(true){ int N; std::cin >> N; if(N == 0){return 0;} std::vector customers_[400][400]; std::vector customers; for(int i = 0; i < N; i++){ int I, J, W; std::cin >> I >> J >> W; customers_[I][J].push_back(Customer(I, J, W)); } for(int j = 1; j <= 365; j++){ for(int i = 1; i <= 365; i++){ std::sort(customers_[i][j].begin(), customers_[i][j].end()); for(int s = 0; s < std::min(2, (int)customers_[i][j].size()); s++){ customers.push_back(customers_[i][j][customers_[i][j].size() - s - 1]); } } } memset(cache, -1, sizeof(cache)); //std::cout << "customers.size() = " << customers.size() << std::endl; get(customers, 366, 366); std::cout << std::max(cache[365][365][0], cache[365][365][1]) << std::endl; //std::cout << get(customers, 1, 1) << std::endl; //std::cout << get(customers, 2, 2) << std::endl; //std::cout << get(customers, 3, 3) << std::endl; //std::cout << get(customers, 20, 20) << std::endl; } }