Problem:
Problem F:
TV Watching
You are addicted to watching TV, and you watch so many TV programs every day. You have been in
trouble recently: the airtimes of your favorite TV programs overlap.
Fortunately, you have both a TV and a video recorder at your home. You can therefore watch a program
on air while another program (on a different channel) is recorded to a video at the same time. However,
it is not easy to decide which programs should be watched on air or recorded to a video. As you are a
talented computer programmer, you have decided to write a program to find the way of watching TV
programs which gives you the greatest possible satisfaction.
Your program (for a computer) will be given TV listing of a day along with your
score
for each TV
program. Each score represents how much you will be satisfied if you watch the corresponding TV
program on air or with a video. Your program should compute the maximum possible sum of the scores
of the TV programs that you can watch.
Input
The input consists of several scenarios.
The first line of each scenario contains an integer
N
(1 ≤
N
≤ 1000) that represents the number of
programs. Each of the following
N
lines contains program information in the format below:
T
T
b
T
e
R
1
R
2
T
is the title of the program and is composed by up to 32 alphabetical letters.
T
b
and
T
e
specify the
start time and the end time of broadcasting, respectively.
R
1
and
R
2
indicate the score when you watch
the program on air and when you have the program recorded, respectively.
All times given in the input have the form of “hh:mm” and range from 00:00 to 23:59. You may assume
that no program is broadcast across the twelve midnight.
The end of the input is indicated by a line containing only a single zero. This is not part of scenarios.
Output
For each scenario, output the maximum possible score in a line.
Sample Input
4
OmoikkiriTV 12:00 13:00 5 1
WaratteIitomo 12:00 13:00 10 2
WatarusekennhaOnibakari 20:00 21:00 10 3
SuzumiyaharuhiNoYuuutsu 23:00 23:30 100 40
5
a 0:00 1:00 100 100
b 0:00 1:00 101 101
c 0:00 1:00 102 102
d 0:00 1:00 103 103
e 0:00 1:00 104 104
0
Output for the Sample Input
121
207

Submitted solutions:

code_snippet: // AOJ #2063 TV Watching
// 2025.3.20

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

struct Prog { int s, e, r1, r2; };

int toMin(const string &t) {
    int hh = stoi(t.substr(0, t.find(':')));
    int mm = stoi(t.substr(t.find(':')+1));
    return hh * 60 + mm;
}

int n;
vector<Prog> p;

vector<vector<int>> dp;

int recDP(int i, int j) {
    int &res = dp[i][j];
    if(res != -1) return res;
    int base = max(i, j) + 1;
    res = 0;
    int lo = base, hi = n+1;
    while(lo < hi) {
        int mid = (lo+hi)/2;
        if(p[mid].s < p[i].e) lo = mid+1;
        else hi = mid;
    }
    int startTV = lo;
    for (int k = max(startTV, base); k <= n; k++) {
        if(p[k].s >= p[i].e) res = max(res, p[k].r1 + recDP(k, j));
    }
    lo = base, hi = n+1;
    while(lo < hi) {
        int mid = (lo+hi)/2;
        if(p[mid].s < p[j].e) lo = mid+1;
        else hi = mid;
    }
    int startR = lo;
    for (int k = max(startR, base); k <= n; k++) {
        if(p[k].s >= p[j].e) res = max(res, p[k].r2 + recDP(i, k));
    }
    return res;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    while(true) {
        cin >> n;
        if(n==0) break;

        p.assign(n+1, Prog());
        p[0] = {0, 0, 0, 0};
        string t, tb, te;
        int r1, r2;
        for (int i = 1; i <= n; i++) {
            cin >> t >> tb >> te >> r1 >> r2;
            p[i].s = toMin(tb);
            p[i].e = toMin(te);
            p[i].r1 = r1;
            p[i].r2 = r2;
        }
        sort(p.begin()+1, p.end(), [](const Prog &a, const Prog &b) {
            return a.s < b.s || (a.s == b.s && a.e < b.e);
        });
        dp.assign(n+1, vector<int>(n+1, -1));
        cout << recDP(0, 0) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2570
memory_kb: 6836
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 1440

struct Info{
	Info(int arg_end_time,int arg_live_value,int arg_record_value){
		end_time = arg_end_time;
		live_value = arg_live_value;
		record_value = arg_record_value;
	}
	int end_time,live_value,record_value;
};

int N;
int dp[NUM][NUM];
vector<Info> G[NUM];

int get_time(char buf[6]){
	int hour = 10*(buf[0]-'0')+buf[1]-'0';
	int minute = 10*(buf[3]-'0')+buf[4]-'0';
	return 60*hour+minute;
}

int recursive(int live_time,int record_time){

	if(dp[live_time][record_time] != -1){
		return dp[live_time][record_time];
	}

	int ret = 0;

	if(live_time < record_time){

		ret = max(ret,recursive(live_time+1,record_time));

		for(int i = 0; i < G[live_time].size(); i++){

			ret = max(ret,recursive(G[live_time][i].end_time,record_time)+G[live_time][i].live_value);
		}

		return dp[live_time][record_time] = ret;

	}else if(live_time > record_time){

		ret = max(ret,recursive(live_time,record_time+1));

		for(int i = 0; i < G[record_time].size(); i++){
			ret = max(ret,recursive(live_time,G[record_time][i].end_time)+G[record_time][i].record_value);
		}
		return dp[live_time][record_time] = ret;

	}else{ //live_time == record_time

		if(live_time == NUM-1){

			return 0;
		}

		ret = max(ret,recursive(live_time+1,record_time+1));

		for(int i = 0; i < G[live_time].size(); i++){
			ret = max(ret,recursive(G[live_time][i].end_time,record_time+1)+G[live_time][i].live_value);
		}

		for(int i = 0; i < G[record_time].size(); i++){
			ret = max(ret,recursive(live_time+1,G[record_time][i].end_time)+G[record_time][i].record_value);
		}

		if(G[live_time].size() > 1){
			for(int i = 0; i < G[live_time].size(); i++){
				for(int k = 0; k < G[live_time].size(); k++){
					if(k == i)continue;

					ret = max(ret,recursive(G[live_time][i].end_time,G[live_time][k].end_time)+
							G[live_time][i].live_value+G[live_time][k].record_value);

				}
			}
		}
		return dp[live_time][record_time] = ret;
	}
}

void func(){

	for(int i = 0; i < NUM; i++){
		G[i].clear();
	}

	int live_value,record_value,start_time,end_time;
	char name[33],start_buf[6],end_buf[6];

	for(int loop = 0; loop < N; loop++){

		scanf("%s %s %s %d %d",name,start_buf,end_buf,&live_value,&record_value);
		start_time = get_time(start_buf);
		end_time = get_time(end_buf);

		G[start_time].push_back(Info(end_time,live_value,record_value));
	}

	for(int i = 0; i < NUM; i++){
		for(int k = 0; k < NUM; k++){
			dp[i][k] = -1;
		}
	}

	printf("%d\n",recursive(0,0));
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 11496
final_rank: 13

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

int N;
int dp[1011][1011];

void update( int &a, int b ){
  a = max( a, b );
}

int main(){
  while( ~scanf("%d",&N) && N ) {
    vector<int> S1(N),S2(N);
    vector<int> Ts[60*25], Te[60*25];
    char buf[256];
    for(int i=0;i<N;i++){
      int tb1,tb2,te1,te2;
      scanf("%s %d:%d %d:%d %d %d",buf,&tb1,&tb2,&te1,&te2,&S1[i],&S2[i]);
      int ts = tb1 * 60 + tb2, te = te1 * 60 + te2;
      Ts[ts].emplace_back( i );
      Te[te].emplace_back( i );
    }
    vector<int> G(N+1);
    vector<int> nx(N+1);
    int cur = N;
    for(int i=60*24;i>-1;i--){
      for( int id : Ts[i] ) {
        G[--cur] = id;
      }
      for( int id : Te[i] ) {
        nx[id] = cur;
      }
    }

   

    fill( dp[0], dp[N], 0 );
    for(int i=0;i<=N;i++){
      for(int j=0;j<=N;j++){
        int a = G[i], b = G[j];
        if( i == j ) {
          if( i == N ) continue;
          update( dp[i+1][j+1], dp[i][j] );
          update( dp[nx[a]][j+1], dp[i][j] + S1[a] );
          update( dp[i+1][nx[b]], dp[i][j] + S2[b] );
        } else if( i < j ) {
          update( dp[i+1][j], dp[i][j] );
          update( dp[nx[a]][j], dp[i][j] + S1[a] );
        } else {
          update( dp[i][j+1], dp[i][j] );
          update( dp[i][nx[b]], dp[i][j] + S2[b] );
        }
      }
    }
    printf("%d\n",dp[N][N]);
  }  
}
accuracy: 1
time_ms: 20
memory_kb: 7108
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<algorithm>
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

using namespace std;

struct Data{
  int start,end,onAir,recorded;
  bool operator<(const Data &data) const{
    if(end != data.end) return end < data.end;
    if(start != data.start) return start < data.start;
    return (onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
int N,dp[2000][2000],h,m;
vector<int> buf;


int toTime(string s){
  rep(i,(int)s.size()) if(s[i] == ':') s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(void){
  while(cin>>N,N){
    vector<Data> vec(N);
    rep(i,N){
      string a,b,c;
      cin>>a>>b>>c>>vec[i].onAir>>vec[i].recorded;
      vec[i].start = toTime(b); vec[i].end = toTime(c); 
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2)rep(j,N+2)dp[i][j] = 0;
    sort(vec.begin(),vec.end());
    buf.clear();
    rep(i,vec.size())buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if(i==j)dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if(i<j){
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j-1],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        }else{
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i-1][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
      }
    }
    cout<<dp[(int)vec.size()-1][(int)vec.size()-1]<<endl;
  }
}
accuracy: 1
time_ms: 100
memory_kb: 9452
final_rank: 8

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
char in[11100];
vector<pair<int,pair<int,int> > >g[1500];
int dp[1500][1500];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<1500;i++)g[i].clear();
		for(int i=0;i<a;i++){
			int ph,pm,qh,qm,r,s;
			scanf("%s%d:%d%d:%d%d%d",in,&ph,&pm,&qh,&qm,&r,&s);
			int p=ph*60+pm;
			int q=qh*60+qm;
			g[p].push_back(make_pair(q,make_pair(r,s)));
		}
		for(int i=0;i<1500;i++)for(int j=0;j<1500;j++)
			dp[i][j]=-999999999;
		dp[0][0]=0;
		for(int i=0;i<1450;i++){
			for(int j=0;j<1450;j++){
				if(i<j){
					dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
					for(int k=0;k<g[i].size();k++){
						dp[g[i][k].first][j]=max(dp[g[i][k].first][j],dp[i][j]+g[i][k].second.first);
					}
				}else if(i>j){
					dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
					for(int k=0;k<g[j].size();k++){
						dp[i][g[j][k].first]=max(dp[i][g[j][k].first],dp[i][j]+g[j][k].second.second);
					}
				}else{
					dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
					dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
					for(int k=0;k<g[i].size();k++)for(int l=0;l<g[i].size();l++){
						if(k==l)continue;
						dp[g[i][k].first][g[i][l].first]=max(dp[g[i][k].first][g[i][l].first],dp[i][j]+g[i][k].second.first+g[i][l].second.second);
					}
				}
			}
		}
		int ret=0;
		for(int i=0;i<1450;i++)for(int j=0;j<1450;j++)ret=max(ret,dp[i][j]);
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 110
memory_kb: 9908
final_rank: 12

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
static const double EPS = 1e-9;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define REP(i, s, e) for ( int i = s; i <= e; i++ )  
#define fi first
#define se second
int dp[1024][1024];
struct Item{
	int l,r,S1,S2;
};
vector<Item> item;
bool operator < (const Item &a,const Item &b){
	if( a.l != b.l ) return a.l < b.l;
	return a.r < b.r;
}

int nextNode[1024];
int n;
int dfs(int x,int y){
	if( dp[x][y] != -1) return dp[x][y];
	int ans = 0;
	int xNext = nextNode[x];
	int yNext = nextNode[y];
	if( x == n && y == n ) return 0;
	else if( x == n ){
		return dp[x][y] = max( dfs(x,y+1) , dfs(x,yNext)+item[y].S2 );
	}else if( y == n){
		return dp[x][y] = max( dfs(x+1,y) , dfs(xNext,y)+item[x].S1 );
	}
	if( y < x ){
		ans = max( ans , dfs(x,y+1) );
		ans = max( ans , dfs(x,yNext) + item[y].S2 );
	}else if( x == y ){
		ans = max( ans , dfs(x+1,y+1) );
		ans = max( ans , dfs(xNext,y+1) + item[x].S1);
		ans = max( ans , dfs(x+1,yNext) + item[y].S2);
	}else{
		ans = max( ans , dfs(x+1,y) );
		ans = max( ans , dfs(xNext,y) + item[x].S1 );
	}
	return dp[x][y] = ans;
}


int conv(string t){
	t[2] = ' ';
	stringstream ss(t);
	int h,m;
	ss >> h >> m;
	
	return h * 60 + m;
}

int main(){
	while(cin >> n && n){
		memset(dp,-1,sizeof(dp));
		item.clear();
		for(int i = 0 ; i < n ; i++){
			string nm;
			string a,b;
			cin >> nm >> a >> b;
			int r1,r2;
			cin >> r1 >> r2;
			item.push_back({conv(a),conv(b),r1,r2});
			//cout << a << " " << b << endl;
		}
		sort(item.begin(),item.end());
		for(int i = 0 ; i < n ; i++){
			nextNode[i] = n;
			for(int j = i+1 ; j < n ; j++)
				if( item[i].r <= item[j].l ){
					nextNode[i] = j;
					break;
				}
		}
		cout << dfs(0,0) << endl;
	}
	
	
}
accuracy: 1
time_ms: 50
memory_kb: 5456
final_rank: 2

code_snippet: #include<bits/stdc++.h>

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

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
int N,dp[2000][2000],h,m;
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec(N);
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;
      vec[i].start = toTime(b), vec[i].end = toTime(c);
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    buf.clear();
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if( i < j ) {
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j-1],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i-1][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
      }
    }
    printf("%d\n",dp[(int)vec.size()-1][(int)vec.size()-1]);
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 9448
final_rank: 5

code_snippet: #include<bits/stdc++.h>

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

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
int N,dp[2000][2000],h,m;
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec(N);
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;
      vec[i].start = toTime(b), vec[i].end = toTime(c);
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    buf.clear();
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if( i < j ) {
          dp[i][j] = dp[i][j-1];
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          dp[i][j] = dp[i-1][j];
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
      }
    }
    printf("%d\n",dp[(int)vec.size()-1][(int)vec.size()-1]);
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9448
final_rank: 6

code_snippet: #include<bits/stdc++.h>

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

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
const int IINF = INT_MAX;
int N,dp[2000][2000],h,m;
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec(N);
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;
      vec[i].start = toTime(b), vec[i].end = toTime(c);
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    buf.clear();
    int maxi = 0;
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if( i < j ) {
          dp[i][j] = dp[i][j-1];
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          dp[i][j] = dp[i-1][j];
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
        maxi = max(maxi,dp[i][j]);
      }
    }
    printf("%d\n",maxi);
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9448
final_rank: 6

code_snippet: #include<bits/stdc++.h>

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

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
const int IINF = INT_MAX;
int N,s[MAX_N],t[MAX_N],score[MAX_N][2],dp[2000][2000];
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss;
  ss << s;
  int h,m;
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec;
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> score[i][0] >> score[i][1];
      assert( b != c );
      s[i] = toTime(b), t[i] = toTime(c);
      vec.push_back((Data){s[i],t[i],score[i][0],score[i][1]});
    }
    vec.push_back((Data){0,0,0,0});
    vec.push_back((Data){IINF,IINF,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    buf.clear();
    int maxi = 0;
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) {
          dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        } else if( i < j ) {
          dp[i][j] = dp[i][j-1];
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          dp[i][j] = dp[i-1][j];
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
        maxi = max(maxi,dp[i][j]);
      }
    }
    cout << maxi << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9476
final_rank: 9

code_snippet: #include<bits/stdc++.h>

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

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
const int IINF = INT_MAX;
int N,s[MAX_N],t[MAX_N],score[MAX_N][2],dp[2000][2000];
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss;
  ss << s;
  int h,m;
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec;
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> score[i][0] >> score[i][1];
      assert( b != c );
      s[i] = toTime(b), t[i] = toTime(c);
      vec.push_back((Data){s[i],t[i],score[i][0],score[i][1]});
    }
    vec.push_back((Data){0,0,0,0});
    vec.push_back((Data){IINF,IINF,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    buf.clear();
    int maxi = 0;
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) {
          dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        } else if( i < j ) {
          dp[i][j] = dp[i][j-1];
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin();
          if( buf[prev] > vec[j].start ) --prev;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          dp[i][j] = dp[i-1][j];
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin();
          if( buf[prev] > vec[i].start ) --prev;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
        maxi = max(maxi,dp[i][j]);
      }
    }
    cout << maxi << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9476
final_rank: 9

code_snippet: #include<bits/stdc++.h>

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

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
const int IINF = INT_MAX;
int N,s[MAX_N],t[MAX_N],score[MAX_N][2],dp[2000][2000];
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss;
  ss << s;
  int h,m;
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec;
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> score[i][0] >> score[i][1];
      assert( b != c );
      s[i] = toTime(b), t[i] = toTime(c);
      vec.push_back((Data){s[i],t[i],score[i][0],score[i][1]});
    }
    vec.push_back((Data){0,0,0,0});
    vec.push_back((Data){IINF,IINF,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    //rep(i,vec.size()) cout << "[" <<vec[i].start << "," << vec[i].end << "] : " << vec[i].onAir << " and " << vec[i].recorded << endl;
    buf.clear();
    int maxi = 0;
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) {
          dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        } else if( i < j ) {
          dp[i][j] = dp[i][j-1];
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin();
          //if( prev == j ) -- prev;
          if( buf[prev] > vec[j].start ) --prev;

          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          dp[i][j] = dp[i-1][j];
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin();
          //if( prev == i ) -- prev;
          if( buf[prev] > vec[i].start ) --prev;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
        maxi = max(maxi,dp[i][j]);
        //cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
      }
    }
    cout << maxi << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9476
final_rank: 9

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<int, int> P;
struct TV{
  P time;
  int r1, r2;
  bool operator < (const TV& t) const{
    return time < t.time;
  }
  TV() {}
  TV(int f) { time = P(f, 0); }
};
void update(int& x, int y){
  if(x < y) x = y;
}

int main(){
  int N;
  while(cin >> N && N){
    int dp[1001][1001] = {};
    vector<TV> tv(N);
    REP(i, N){
      char tmp[100];
      int a, b, c, d;
      scanf("%s %d:%d %d:%d %d %d", tmp, &a, &b, &c, &d, &tv[i].r1, &tv[i].r2);
      tv[i].time = P(a * 60 + b , c * 60 + d);
    }
    sort(tv.begin(), tv.end());
    for(int i = 0; i <= N; i++){
      for(int j = 0; j <= N; j++){
        if(i < N){
          int ni = lower_bound(tv.begin(), tv.end(), TV(tv[i].time.second)) - tv.begin();
          update(dp[ni][max(i + 1, j)], dp[i][j] + tv[i].r1);
          update(dp[i + 1][j], dp[i][j]);
        }
        if(j < N){
          int nj = lower_bound(tv.begin(), tv.end(), TV(tv[j].time.second)) - tv.begin();
          update(dp[max(j + 1, i)][nj], dp[i][j] + tv[j].r2);
          update(dp[i][j + 1], dp[i][j]);
        }
      }
    }
    cout << dp[N][N] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 5184
final_rank: 3

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Data
{
public:
    int t1, t2, r1, r2;
    bool operator< (const Data& d) const{
        return t1 < d.t1;
    }
};

int n;
vector<Data> d;
vector<int> nextTV;

vector<vector<int> > memo;

int solve(int a, int b)
{
    if(a == n+1 || b == n+1)
        return 0;
    if(a == n && b == n)
        return 0;
    if(memo[a][b] != -1)
        return memo[a][b];

    int ret = 0;
    ret = max(ret, solve(a, b+1));
    ret = max(ret, solve(a+1, b));
    if(a == b){
        ret = max(ret, d[a].r1 + solve(nextTV[a], b+1));
        ret = max(ret, d[b].r2 + solve(a+1, nextTV[b]));
    }else if(a > b){
        ret = max(ret, d[b].r2 + solve(a, nextTV[b]));
    }else{
        ret = max(ret, d[a].r1 + solve(nextTV[a], b));
    }

    return memo[a][b] = ret;
}

int main()
{
    for(;;){
        cin >> n;
        if(n == 0)
            return 0;

        d.resize(n+1);
        for(int i=0; i<n; ++i){
            string s;
            int h1, m1, h2, m2;
            char c;
            cin >> s >> h1 >> c >> m1 >> h2 >> c >> m2 >> d[i].r1 >> d[i].r2;
            d[i].t1 = h1 * 60 + m1;
            d[i].t2 = h2 * 60 + m2;
        }
        d[n].t1 = INT_MAX / 2;
        d[n].t2 = INT_MAX / 2 + 1;
        d[n].r1 = d[n].r2 = 0;
        sort(d.begin(), d.end());

        nextTV.resize(n+1);
        for(int i=0; i<=n; ++i){
            Data tmp;
            tmp.t1 = d[i].t2;
            nextTV[i] = lower_bound(d.begin(), d.end(), tmp) - d.begin();
        }

        memo.assign(n+1, vector<int>(n+1, -1));
        cout << solve(0, 0) << endl;
    }
}
accuracy: 1
time_ms: 110
memory_kb: 4952
final_rank: 1