Problem:
Problem G: Concert Hall Scheduling

You are appointed director of a famous concert hall, to save it from bankruptcy. The hall is very
popular, and receives many requests to use its two fine rooms, but unfortunately the previous
director was not very efficient, and it has been losing money for many years. The two rooms are
of the same size and arrangement. Therefore, each applicant wishing to hold a concert asks for
a room without specifying which. Each room can be used for only one concert per day.

In order to make more money, you have decided to abandon the previous fixed price policy, and
rather let applicants specify the price they are ready to pay. Each application shall specify a
period [i, j] and an asking price w, where i and j are respectively the first and last days of the
period (1 ≤ i ≤ j ≤ 365), and w is a positive integer in yen, indicating the amount the applicant
is willing to pay to use a room for the whole period.

You have received applications for the next year, and you should now choose the applications
you will accept. Each application should be either accepted for its whole period or completely
rejected. Each concert should use the same room during the whole applied period.

Considering the dire economic situation of the concert hall, artistic quality is to be ignored,
and you should just try to maximize the total income for the whole year by accepting the most
profitable applications.

Input

The input has multiple data sets, each starting with a line consisting of a single integer n, the
number of applications in the data set. Then, it is followed by n lines, each of which represents
one application with a period [i, j] and an asking price w yen in the following format.

i j w

A line containing a single zero indicates the end of the input.

The maximum number of applications in a data set is one thousand, and the maximum asking
price is one million yen.

Output

For each data set, print a single line containing an integer, the maximum total income in yen
for the data set.

Sample Input

4
1 2 10
2 3 10
3 3 10
1 3 10
6
1 20 1000
3 25 10000
5 15 5000
22 300 5500
10 295 9000
7 7 6000
8
32 251 2261
123 281 1339
211 235 5641
162 217 7273
22 139 7851
194 198 9190
119 274 878
122 173 8640
0

Output for the Sample Input

30
25500
38595

Solution in C++14 (judge verdict: Accepted)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll n;
ll l[1001],r[1001],w[1001];
vector<ll> rs[400];
vector<ll> wss[400];
vector<ll> ids[400];
ll dp[401][1001];

int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    rep(i,400){
      rs[i].clear();
      wss[i].clear();
      ids[i].clear();
    }
    repl(i,1,n+1){
      cin>>l[i]>>r[i]>>w[i];
      l[i]--; r[i]--;
      r[i]++;
      rs[l[i]].push_back(r[i]);
      wss[l[i]].push_back(w[i]);
      ids[l[i]].push_back(i);
    }
    rep(i,401)rep(j,1001)dp[i][j]=-INF;
    dp[0][0]=0;
    rep(i,400)rep(j,1001){
      if(dp[i][j]<0)continue;
      if(j==0){
        maxch(dp[i+1][j],dp[i][j]);
        rep(k,rs[i].size()){
          maxch(dp[i][ids[i][k]],dp[i][j]+wss[i][k]);
        }
      }else{
        if(r[j]==i+1)maxch(dp[i+1][0],dp[i][j]);
        else maxch(dp[i+1][j],dp[i][j]);
        rep(k,rs[i].size()){
          if(ids[i][k]==j)continue;
          ll mn=min(r[j],rs[i][k]);
          ll mx=max(r[j],rs[i][k]);
          if(mn==mx)maxch(dp[mn][0],dp[i][j]+wss[i][k]);
          else{
            maxch(dp[mn][(r[j]==mx?j:ids[i][k])],dp[i][j]+wss[i][k]);
          }
        }
      }
    }
    ll res=-INF;
    rep(i,401)rep(j,1001)maxch(res,dp[i][j]);
    cout<<res<<endl;
  }
  return 0;
}