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++ (judge verdict: Accepted)

#include<queue>
#include<cstdio>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class T>
struct interval{
	T a,b,w;
	interval():a(0),b(0){}
	interval(const T &a,const T &b):a(a<b?a:b),b(a<b?b:a){}
	bool operator<(const interval &I)const{ return b<I.b || b==I.b && a<I.a; }
};

const int V_MAX=2003;
const int CAPA_INF=1<<29;
const int COST_INF=1<<29;

template<class T,class U>
struct edge{
	int v,rev;
	T capa;
	U cost;
	T flow;
};

template<class T,class U>
void add_directed_edge(vector< edge<T,U> > *G,int u,int v,T capa,U cost){
	G[u].push_back((edge<T,U>){v,G[v].size()  ,capa, cost,0});
	G[v].push_back((edge<T,U>){u,G[u].size()-1,   0,-cost,0});
}

template<class T,class U>
pair<T,U> augment(int n,vector< edge<T,U> > *G,int s,int t,U *pot,bool fir){
	static int pre[V_MAX];
	static U d[V_MAX];
	rep(u,n) d[u]=(u==s?0:COST_INF);

	bool ok;
	if(fir){
		// Bellman-Ford
		while(1){
			bool end=true;
			rep(u,n) rep(i,G[u].size()) {
				edge<T,U> &e=G[u][i];
				if(e.capa-e.flow>0 && d[u]<COST_INF && d[u]+e.cost<d[e.v]){
					d[e.v]=d[u]+e.cost;
					pre[e.v]=e.rev;
					end=false;
				}
			}
			if(end) break;
		}
		ok=(d[t]<COST_INF);
	}
	else{
		// Dijkstra
		ok=false;
		priority_queue< pair<U,int> > Q; Q.push(make_pair(0,s));
		while(!Q.empty()){
			int u=Q.top().second;
			U cost=-Q.top().first; Q.pop();

			if(cost<d[u]) continue;
			if(u==t) ok=true;

			rep(i,G[u].size()){
				edge<T,U> &e=G[u][i];
				U cost2=cost+e.cost+pot[u]-pot[e.v];
				if(e.capa-e.flow>0 && cost2<d[e.v]){
					d[e.v]=cost2;
					pre[e.v]=e.rev;
					Q.push(make_pair(-cost2,e.v));
				}
			}
		}
	}

	if(!ok) return make_pair(0,0);

	T water=CAPA_INF;
	for(int v=t;v!=s;){
		edge<T,U> &e1=G[v][pre[v]];
		edge<T,U> &e2=G[e1.v][e1.rev];
		water=min(water,e2.capa-e2.flow);
		v=e1.v;
	}

	U cost=0;
	for(int v=t;v!=s;){
		edge<T,U> &e1=G[v][pre[v]];
		edge<T,U> &e2=G[e1.v][e1.rev];
		e1.flow-=water;
		e2.flow+=water;
		cost+=water*e2.cost;
		v=e1.v;
	}

	rep(u,n) pot[u]+=d[u];

	return make_pair(water,cost);
}

template<class T,class U>
pair<T,U> primal_dual(int n,vector< edge<T,U> > *G,int s,int t){
	static U pot[V_MAX];
	rep(u,n) pot[u]=0;

	T ans1=0;
	U ans2=0;
	for(bool fir=true;;fir=false){
		pair<T,U> tmp=augment(n,G,s,t,pot,fir);
		if(tmp.first==0) break;
		ans1+=tmp.first;
		ans2+=tmp.second;
	}
	return make_pair(ans1,ans2);
}

int main(){
	for(int n;scanf("%d",&n),n;){
		interval<int> I[1000];
		rep(i,n) scanf("%d%d%d",&I[i].a,&I[i].b,&I[i].w);

		sort(I,I+n);

		int s=0,t=2*n+1;
		vector< edge<int,int> > G[V_MAX];
		rep(u,n) for(int v=u+1;v<n;v++) if(I[u].b<I[v].a) {
			add_directed_edge(G,1+2*u+1,1+2*v,1,-I[v].w);
		}
		rep(u,n){
			add_directed_edge(G,s,1+2*u,1,-I[u].w);
			add_directed_edge(G,1+2*u,1+2*u+1,1,0);
			add_directed_edge(G,1+2*u+1,t,1,0);
		}
		int tt=t+1;
		add_directed_edge(G,t,tt,2,0);

		printf("%d\n",-primal_dual(2*n+3,G,s,tt).second);
	}

	return 0;
}