Problem:
Problem B: Watchin' TVA
Animation is one of methods for making movies and in Japan, it is popular to broadcast as a television program or perform as a movie. Many people, especially the young, love one. And here is an anime lover called Jack. We say he is an mysterious guy with uncertain age. He likes anime which are broadcasted in midnight and early morning especially.
In his room, there is a huge piece of paper on the wall. He writes a timetable of TV anime on it. In his house, he can watch all Japanese TV anime programs that are broadcasted in Japan using a secret and countrywide live system. However he can not watch all anime and must give up to watch some programs because they are "broadcasted at the same time" in a season. Here, programs are "broadcasted at the same time" means that two or more programs have one or more common minutes in broadcasting time. Increasing the number of anime programs in recent makes him nervous. Actually, some people buy DVDs after the program series ends or visit a web site called vhefoo. Anyway, he loves to watch programs on his live system. Of course, he is not able to watch two or more programs at the same time. However, as described above, he must give up some programs broadcasted at the same time. Therefore, he has a set of programs
F
and he watches programs in a set
F
absolutely.
Your task is to write a program that reads a timetable and outputs the maximum number of watchable programs, keeping that Jack watches all programs in the set
F
. Of course, there are multiple choices of programs, so we want the number of programs he can watch. If two or more programs in a set
F
are broadcasted at the same time, you must give Jack an unfortunate announcement. In this case, your program outputs -1. In addition, each anime program is a program of 30 minutes.
Input
Input consists of multiple datasets.
A dataset is given in a following format.
N
PROGRAM
1
PROGRAM
2
...
PROGRAM
N
P
FAV
1
FAV
2
...
FAV
P
N
is the number of programs in a season.
PROGRAM
i
(1≤
i
≤
N
)is a string which has the following format.
name
weekday
start
name
is a program name. This is a a string having between 1 and 32 characters and these names do not overlap each other program. A name consists of alphanumeric characters and '_'(underscore).
weekday
is a broadcasting weekday about the corresponding program. This is an integer. 0 means Sunday and 1 is Monday and so on (2:Tuesday, 3:Wednesday, 4:Thursday, 5:Friday, 6:Saturday).
start
is a starting time of the program broadcasting. This is an integer between 600 and 2929. First one or two digits represent hour and the last two digits represent minute. If the hour has one digit it will be a representation "900" for example. Note: a program may have an integer more than or equal to 2400 as
start
, if the program begins the next day. For example, a program begins from 2500 on Monday should be interpreted as a program begins from 0100 on Tuesday.
There are no input the minute of
start
exceeds 59. And 
...(truncated)

Submitted solutions:

code_snippet: /*
 *       Author:  8mao
 * Created Time:  2014/3/16 21:55:53
 *    File Name:  2155.cpp
 */
# include<map>
# include<set>
# include<cmath>
# include<queue>
# include<stack>
# include<vector>
# include<string>
# include<cstdio>
# include<cstring>
# include<iostream>
# include<algorithm>
# include<functional>
using namespace std;

typedef pair<int,int> PII;
# define INF 1<<30
# define LL long long
# define MOD 1000000007
# define VI vector<int>
# define VLL vector<LL>
# define VS vector<string>
# define PII pair<int,int>

# define F first
# define S second
# define mp make_pair
# define pb push_back
# define lb lower_bound
# define up upper_bound
# define lowbit(x)(x&-x)
# define lson l,m,rt<<1
# define rson m+1,r,rt<<1|1
# define clr(x) (x).clear()
# define sz(x) ((int)(x).size())
# define all(x) (x).begin(),(x).end()
# define mem(x,y) memset(x,y,sizeof(x))
# define forall(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();it++)

# define N 505

struct Seg
{
    int l, r;
    Seg() {};
    Seg(int ll, int rr) : l(ll), r(rr) {};
    bool operator < (const Seg &b)const
    {
        return l < b.l;
    }
}seg[N];

bool Jireh(Seg &a, Seg &b)
{
    int d = abs(a.r - b.r);
    return d < 30;
}

int solve(vector<Seg> &v)
{
    int i, r = -1, ans = 0;
    sort (all (v));
    for (i = 0; i < sz(v); i++)
        if (v[i].l >= r)
        {
            r = v[i].r;
            ans++;
        }
    return ans;
}

int calc(int x, int y)
{
    return x * 24 * 60 + y % 100 + y / 100 * 60;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int i, j, n, m, x, y, flag, p;
    char s[105];
    vector<Seg> v, u;
    map<string, int> Hash;
    while (scanf ("%d", &n) != EOF && n)
    {
        flag = 0;
        clr (u);
        clr (v);
        clr (Hash);
        for (i = 1; i <= n; i++)
        {
            scanf ("%s %d %d", s, &x, &y);
            Hash[s] = i;
            seg[i] = Seg(calc(x, y), calc(x, y) + 30);
        }
        scanf ("%d", &m);
        for (i = 1; i <= m; i++)
        {
            scanf ("%s", s);
            p = Hash[s];
            u.pb(seg[p]);
        }
        for (i = 0; i < sz(u); i++)
            for (j = 0; j < i; j++)
                if(Jireh(u[i], u[j]))
                    flag = 1;
        if (flag)
        {
            printf ("-1\n");
            continue;
        }
        for (i = 1; i <= n; i++)
        {
            for (j = 0; j < sz(u); j++)
                if (Jireh(seg[i], u[j]))
                    break;
            if (j == sz(u)) v.pb(seg[i]);
        }
        int ans = solve(v) + sz(u);
        printf ("%d\n", ans);
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3612
final_rank: 8

code_snippet: /*
 *       Author:  8mao
 * Created Time:  2014/3/16 13:31:26
 *    File Name:  1331.cpp
 */
# include<map>
# include<set>
# include<cmath>
# include<queue>
# include<stack>
# include<vector>
# include<string>
# include<cstdio>
# include<cstring>
# include<iostream>
# include<algorithm>
# include<functional>
using namespace std;

typedef pair<int,int> PII;
# define INF 1<<30
# define LL long long
# define MOD 1000000007
# define VI vector<int>
# define VLL vector<LL>
# define VS vector<string>
# define PII pair<int,int>

# define F first
# define S second
# define mp make_pair
# define pb push_back
# define lb lower_bound
# define up upper_bound
# define lowbit(x)(x&-x)
# define lson l,m,rt<<1
# define rson m+1,r,rt<<1|1
# define clr(x) (x).clear()
# define sz(x) ((int)(x).size())
# define all(x) (x).begin(),(x).end()
# define mem(x,y) memset(x,y,sizeof(x))
# define forall(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();it++)

# define N 505
# define M 20005
PII a[N];
int dp[N][M], n;

struct node
{
    char s[105];
    int t, must, del;
}e[N];

int calc(int week, int time)
{
    int ans = week * 24 * 60;
    ans += time % 100 + (time / 100 * 60);
    return ans;
}

int dfs(int pos, int end)
{
    int ans = 0;
    if (pos == n + 1)
        return 0;
    if (dp[pos][end] != -1)
        return dp[pos][end];
    if (a[pos].S || a[pos].F >= end) //must  有可能会出错
        ans = max (ans, 1 + dfs(pos + 1, a[pos].F + 30));
    ans = max (ans, dfs(pos + 1, end));
    return dp[pos][end] = ans;
}

int Jireh(int m)
{
    int i, j;
    for (i = 1; i <= m; i++)
         if (e[i].must)
             for (j = 1; j <= m; j++)
                 if (j != i && abs(e[j].t - e[i].t) < 30)  //注意点,有可能会出错
                    e[j].del = 1;
    for (i = 1; i <= m; i++)
         if (e[i].must && e[i].del)
            return 0;
    n = 0;
    for (i = 1; i <= m; i++)
         if (!e[i].del)
        {
            ++n;
            a[n].F = e[i].t;
            a[n].S = e[i].must;
        }
    sort (a + 1, a + n + 1);
    return 1;
}

int cmp(const node &a, const node &b)
{
    return a.t < b.t;
}

int main()
{
    int i, j, n, m, x, y;
    char s[105];
    while (scanf ("%d", &n) != EOF && n)
    {
        mem (dp, -1);
        for (i = 1; i <= n; i++)
        {
            scanf ("%s", e[i].s);
            scanf ("%d %d", &x, &y);
            e[i].t = calc(x, y);
            e[i].must = e[i].del = 0;
        }
        scanf ("%d", &m);
        for (i = 1; i <= m; i++)
        {
            scanf ("%s", s);
            for (j = 1; j <= n; j++)
                if (strcmp(e[j].s, s) == 0)
                {
                    e[j].must = 1;
                    break;
                }
        }
        sort (e + 1, e + n + 1, cmp);
        if (!Jireh(n))
        {
            printf ("-1\n");
            continue;
        }
        int ans = dfs(1, 0);
        printf ("%d\n", ans);
    }
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 43048
final_rank: 20

code_snippet: #include<iostream>
#include<string>
#include<map>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int n,p;
map<string,int>mp;
int vis[1000005];
int ans;

int main() {
	while(cin>>n && n) {
		mp.clear();
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++) {
			string name;
			int d,t;
			cin>>name>>d>>t;
			t = t % 100 + (t-600)/100*60+d*1440;
			mp[name] = t;
		}
		cin>>p;
		ans = p;
		for(int i=0;i<p;i++) {
			string name;
			cin>>name;
			auto it = mp.find(name);
			int t = it->second;
			for(int j=0;j<30;j++) {
				if(vis[t+j])
					ans = -1;
				vis[t+j]++;
			}
		}
		if(ans >= 0) {
			vector<int>v;
			for(auto it=mp.begin();it!=mp.end();it++) 
				v.push_back(it->second);
			sort(v.begin(),v.end());
			for(int i=0;i<v.size();i++) {
				if(!vis[v[i]]&&!vis[v[i]+29]) {
					ans++;
					for(int j=0;j<30;j++)
						vis[v[i]+j] = 1;
				}
			}
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 80
memory_kb: 7256
final_rank: 16

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

#define pb push_back
#define mp make_pair
#define X first
#define Y second
using namespace std;

typedef long long int ll;
typedef vector <int> vi;
typedef pair <int,int> pii;
const int Size=1e6+10;
const int INF=0x3f3f3f3f;

inline int read()
{
    int x=0, f=1; char ch=getchar();
    while(ch<'0' || ch>'9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

map<string,int> mmap;
vi fav;
vi unfav;

//给出两个开始时间，若相交返回true
bool check(int x,int y)
{
    return (y<=x && x<y+30) || (x<=y && y<x+30);
}

int main()
{
    while (1)
    {
        int n=read();
        if (!n) break;

        mmap.clear();
        fav.clear();
        unfav.clear();

        string name;
        int w,s,h,m,t;
        while (n--)
        {
            cin>>name>>w>>s;
            m=s%100;
            h=s/100;
            t=w*24*60+h*60+m;
            mmap[name]=t;
        }

        int p=read();
        while (p--)
        {
            cin>>name;
            fav.pb(mmap[name]);
            mmap.erase(name);
        }

        for (pair<string,int> p:mmap)
            unfav.pb(p.Y);

        sort(fav.begin(),fav.end());
        sort(unfav.begin(),unfav.end());

        int siz=fav.size();
        bool flag=true;
        for (int i=1;i<siz;++i)
            if (fav[i]-fav[i-1]<30) {flag=false; break;}
        if (!flag) {puts("-1"); continue;}
        int res=siz;

        siz=unfav.size();
        int pre_end=-1;
        for (int i=0;i<siz;++i)
        {
            bool flag=true;
            for (int j:fav)
                if (check(unfav[i],j)) {flag=false; break;}
            if (!flag) continue;
            if (pre_end!=-1 && pre_end>unfav[i]) continue;
            res++;
            pre_end=unfav[i]+30;
        }

        printf("%d\n",res);
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3552
final_rank: 10

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <utility>
#define cs cout << "Wish You AC!" << endl;
#define js ios::sync_with_stdio(false); cin.tie(0);
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) (x&-x)
#define lson(x) x*2
#define rson(x) x*2 + 1
#define ll long long
#define ull unsigned long long
#define debug(x) std::cout << #x << " is " << (x) << '\n'
#define INF 1e14
#define pb push_back
using namespace std;
inline void read(ll &x){
    char c; int flag=0;
    while(!isdigit(c=getchar()))
        if(c=='-') flag=1;
    x=c-'0';
    while(isdigit(c=getchar()))
        x=x*10+c-'0';
    if(flag) x=-x;
}
const int N = 5e2 + 10;
ll n;
int main()
{
    while(cin >> n && n)
    {
        ll i, j;
        ll ans = 0;
        vector<pair<ll, string> > v(n);
        for(i = 0; i < n; i++)
        {
            string a;
            ll b, c;
            cin >> a >> b >> c;
            v[i] = make_pair(b*24*60 + c/100*60 + c%100, a);
        }
        sort(v.begin(), v.end());
        ll p; cin >> p;
        for(i = 1; i <= p; i++)
        {
            string str;
            bool ok = 0;
            cin >> str;
            for(j = 0; j < v.size(); j++)
            {
                if(v[j].second == str){
                    ok = 1;
                    pair<ll, string> l = make_pair(v[j].first-29, "0"), r = make_pair(v[j].first+30, "0");
                    v.erase(lower_bound(v.begin(), v.end(), l), lower_bound(v.begin(), v.end(), r));
                    break;
                }
            }
            if(!ok) ans = -INF;
        }
        ll now = 0;
        for(i = 0; i < v.size(); i++)
        {
            if(v[i].first >= now){
                now = v[i].first+30;
                ans++;
            }
        }
        cout << max((ll)-1, ans+p) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3228
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;
typedef pair<int, string> P;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n, n) {
        vector<P> anime(n);
        REP(i, 0, n) {
            string s;
            cin >> s;
            int w, d;
            cin >> w >> d;
            int t = w * 24 * 60 + d / 100 * 60 + d % 100;
            anime[i] = {t, s};
        }
        const int size = 24 * 60 * 7;

        int m;
        cin >> m;
        vector<int> used(size, 0);
        {
            REP(i, 0, m) {
                string s;
                cin >> s;
                for (auto it = anime.begin(); it != anime.end(); it++) {
                    if ((*it).second == s) {
                        REP(j, 0, 30) {
                            used[((*it).first + j) % size]++;
                        }
                        anime.erase(it);
                        break;
                    }
                }
            }
            bool check = true;
            REP(i, 0, size) {
                if (used[i] > 1) {
                    check = false;
                }
            }
            if (!check) {
                cout << -1 << endl;
                continue;
            }
        }
        sort(anime.begin(), anime.end());

        int ans = m;
        for (auto &A : anime) {
            bool check = true;
            REP(j, 0, 30) {
                if (used[(A.first + j) % size] != 0) {
                    check = false;
                }
            }
            if (check) {
                REP(j, 0, 30) {
                    used[(A.first + j) % size]++;
                }
                ans++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3184
final_rank: 4

code_snippet: #include<iostream>
#include<map>
#include<algorithm>
using namespace std;

struct data{
    data(){}
    string name;
    int start;
    bool operator<(const data other) const{
        return start < other.start;
    }
};

int main(){
    int n;
    while(cin >> n, n){
        bool t[60*24*8] = {};
        data d[n];
        int a, b;
        for(int i = 0; i < n; i++){
            cin >> d[i].name >> a >> b;
            d[i].start = 60*24*a + b/100*60 + b%100;
        }
        sort(d, d+n);
        
        map<string,int> m;
        for(int i = 0; i < n; i++)  m[d[i].name] = i;

        int p;
        cin >> p;
        
        string imp;
        bool valid = true;
        
        for(int i = 0; i < p; i++){
            cin >> imp;
            for(int j = 0; j < 30; j++){
                if(t[d[m[imp]].start+j])   valid = false;
                t[d[m[imp]].start+j] = true;
            }
        }

        if(!valid){
            cout << -1 << endl;
            continue;
        }

        int ans = p;
        for(int i = 0; i < n; i++){
            bool judge = true;
            for(int j = 0; j < 30; j++){
                if(t[d[i].start+j])   judge = false;
            }
            if(!judge)  continue;
            for(int j = 0; j < 30; j++){
                t[d[i].start+j] = true;
            }
            ans++;
        }

        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3160
final_rank: 15

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

struct hoge{
    string name;
    int start;
    hoge(string name, int start) : name(name), start(start){ }
};
bool operator < (const hoge &a, const hoge &b){
    return a.start < b.start;
}

int main(void){
    while(true){
        int n;
        cin >> n;
        if(n==0) break;
        vector<hoge> v;
        for(int i=0; i<n; i++){
            string name;
            int start, weekday, s;
            cin >> name >> weekday >> start;
            s=weekday*24*60+start/100*60+start%100;
            v.push_back(hoge(name, s));
        }
        sort(v.begin(), v.end());
        int p;
        cin >> p;
        bool timezone[12000], tf=true;
        fill(timezone, timezone+12000, false);
        
        for(int i=0; i<p; i++){
            string str;
            cin >> str;
            for(int j=0; j<n; j++){
                if(tf){
                    if(v[j].name==str){
                        if(!timezone[v[j].start] && !timezone[v[j].start+29]){
                            for(int t=v[j].start; t<v[j].start+30; t++){
                                timezone[t]=true;
                            }
                        }else{
                            tf=false;
                            break;
                        }
                    }
                }
            }
        }
        int count=p;
        if(! tf) cout << -1 << "\n";
        else{
            for(int i=0; i<n; i++){
                //cout << v[i].name << " time=" << v[i].start << "\n";
                if(! timezone[v[i].start] && !timezone[v[i].start+29]){
                    //cout << "OK=" << v[i].name << "\n";
                    for(int j=v[i].start; j<v[i].start+30; j++){
                        timezone[j]=true;
                    }
                    count++;
                }
            }
            cout << count << "\n";
        }
        
        
    }
    
    
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3076
final_rank: 9

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

using namespace std;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
vector<string> names;
vector<int> starts;
int P;
vector<string> favs;
map<string, int> to_id;
bool cross_impl(int s1, int s2) {
    if (s1 <= s2 && s2 < s1 + 30) return true;
    return false;
}
bool cross(int s1, int s2) { return cross_impl(s1, s2) || cross_impl(s2, s1); }
void solve() {
    rep(i, P) rep(j, P) if (i != j) {
        int a = to_id[favs[i]];
        int b = to_id[favs[j]];
        if (cross(starts[a], starts[b])) {
            // cout << "muri: " << favs[i] << ", " << favs[j] << endl;
            // cout << a << ", " << b << endl;
            // cout << starts[a] << ", " << starts[b] << endl;
            cout << "-1" << endl;
            return;
        }
    }
    vector<int> used;
    rep(i, P) {
        int a = to_id[favs[i]];
        used.push_back(starts[a]);
    }
    rep(_, N) {
        int mini = 1e8;
        for (auto s : starts) {
            bool ok = true;
            for (auto x : used) ok &= !cross(x, s);
            if (ok && mini > s) mini = s;
        }
        if (mini != 1e8) used.push_back(mini);
    }
    cout << used.size() << endl;
}
int main() {
    while (cin >> N, N) {
        rep(i, N) {
            string n;
            int w, s;
            cin >> n >> w >> s;
            s = s / 100 * 60 + s % 100;
            s += w * 86400;
            names.push_back(n);
            starts.push_back(s);
            // cout << i << ": " << s << endl;
            int t = to_id.size();
            to_id[n] = t;
        }
        cin >> P;
        rep(i, P) {
            string n;
            cin >> n;
            favs.push_back(n);
        }
        solve();
        favs.clear();
        starts.clear();
        to_id.clear();
        names.clear();
    }
    return 0;
}
accuracy: 1
time_ms: 1260
memory_kb: 3292
final_rank: 17

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	bool operator<(const struct Info &arg) const{
		if(weekday != arg.weekday){
			return weekday < arg.weekday;
		}else{
			return start < arg.start;
		}
	}
	int weekday,start,end;
	char name[33];
};

int N;
Info info[500];
bool check[7][1440];
bool used[500];

bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

int translate_TIME(char buf[5]){
	int length;
	for(length = 0; buf[length] != '\0'; length++);

	int hour,minute;

	if(length == 3){
		hour = buf[0]-'0';
		minute = 10*(buf[1]-'0')+(buf[2]-'0');
	}else{
		hour = 10*(buf[0]-'0')+(buf[1]-'0');
		minute = 10*(buf[2]-'0')+(buf[3]-'0');
	}
	hour -= 6;
	return 60*hour+minute;
}

void func(){

	for(int i = 0; i < 7; i++){
		for(int k = 0; k < 1440; k++)check[i][k] = false;
	}

	for(int i = 0; i < N; i++)used[i] = false;

	char start[5];
	int tmp;

	for(int i = 0; i < N; i++){
		scanf("%s %d %s",info[i].name,&info[i].weekday,start);

		tmp = translate_TIME(start);

		info[i].start = tmp;
		info[i].end = tmp+29;
	}

	sort(info,info+N);

	vector<int> MUST;

	int P;
	scanf("%d",&P);

	char tmp_name[33];

	for(int loop = 0; loop < P; loop++){
		scanf("%s",tmp_name);

		for(int i = 0; i < N; i++){
			if(strCmp(info[i].name,tmp_name)){
				MUST.push_back(i);
				break;
			}
		}
	}

	bool FLG = true;

	for(int i = 0; i < P-1; i++){
		for(int k = i+1; k < P; k++){
			if((info[MUST[i]].weekday != info[MUST[k]].weekday) || (info[MUST[i]].start > info[MUST[k]].end || info[MUST[i]].end < info[MUST[k]].start)){
				//Do nothing
			}else{
				FLG = false;
				break;
			}
		}
	}
	if(!FLG){
		printf("-1\n");
		return;
	}

	for(int i = 0; i < P; i++){
		used[MUST[i]] = true;
		for(int k = info[MUST[i]].start; k <= info[MUST[i]].end; k++){
			check[info[MUST[i]].weekday][k] = true;
		}
	}

	vector<int> V;
	for(int i = 0; i < N; i++){
		if(used[i] == false){
			V.push_back(i);
		}
	}

	int ans = P;
	int right,V_index = 0;
	bool Found;

	if(V.size() == 0){
		printf("%d\n",P);
		return;
	}

	for(int w_day = 0; w_day < 7; w_day++){
		for(int time = 0; time < 1440;){

			if(V_index == V.size()){
				printf("%d\n",ans);
				return;
			}

			if(check[w_day][time]){
				time++;
				continue;
			}
			for(right = time; right+1 < 1440 && check[w_day][right+1] == false; right++);

			if(right-time+1 < 30){

				for(; V_index < V.size();){
					if((info[V[V_index]].weekday < w_day) || (info[V[V_index]].weekday == w_day && info[V[V_index]].start <= right)){
						 V_index++;
					}else{
						break;
					}
				}

				time = right+1;
			}else{

				Found = false;

				for(; V_index < V.size();){
					if((info[V[V_index]].weekday < w_day) || (info[V[V_index]].weekday == w_day && info[V[V_index]].start < time)){
						V_index++;
					}else if(info[V[V_index]].weekday == w_day && info[V[V_index]].start >= time && info[V[V_index]].end <= right){
						ans++;
						time = info[V[V_index]].end+1;
						Found = true;
						V_index++;
						break;
					}else{
						break;
					}
				}
				if(!Found){
					time = right+1;
				}
			}
		}
	}

	printf("%d\n",ans);
}


int main(){

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

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3140
final_rank: 7

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	bool operator<(const struct Info &arg) const{
		if(weekday != arg.weekday){
			return weekday < arg.weekday;
		}else{
			return start < arg.start;
		}
	}
	int weekday,start,end;
	char name[33];
};

int N;
Info info[500];
bool check[7][1440];
bool used[500];

bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

int translate_TIME(char buf[5]){
	int length;
	for(length = 0; buf[length] != '\0'; length++);

	int hour,minute;

	if(length == 3){
		hour = buf[0]-'0';
		minute = 10*(buf[1]-'0')+(buf[2]-'0');
	}else{
		hour = 10*(buf[0]-'0')+(buf[1]-'0');
		minute = 10*(buf[2]-'0')+(buf[3]-'0');
	}
	hour -= 6;
	return 60*hour+minute;
}

void func(){

	for(int i = 0; i < 7; i++){
		for(int k = 0; k < 1440; k++)check[i][k] = false;
	}

	for(int i = 0; i < N; i++)used[i] = false;

	char start[5];
	int tmp;

	for(int i = 0; i < N; i++){
		scanf("%s %d %s",info[i].name,&info[i].weekday,start);

		tmp = translate_TIME(start);

		info[i].start = tmp;
		info[i].end = tmp+29;
	}

	sort(info,info+N);

	vector<int> MUST;

	int P;
	scanf("%d",&P);

	char tmp_name[33];

	for(int loop = 0; loop < P; loop++){
		scanf("%s",tmp_name);

		for(int i = 0; i < N; i++){
			if(strCmp(info[i].name,tmp_name)){
				MUST.push_back(i);
				break;
			}
		}
	}

	bool FLG = true;

	for(int i = 0; i < P-1; i++){
		for(int k = i+1; k < P; k++){
			if((info[MUST[i]].weekday != info[MUST[k]].weekday) || (info[MUST[i]].start > info[MUST[k]].end || info[MUST[i]].end < info[MUST[k]].start)){
				//Do nothing
			}else{
				FLG = false;
				break;
			}
		}
	}
	if(!FLG){
		printf("-1\n");
		return;
	}

	for(int i = 0; i < P; i++){
		used[MUST[i]] = true;
		for(int k = info[MUST[i]].start; k <= info[MUST[i]].end; k++){
			check[info[MUST[i]].weekday][k] = true;
		}
	}

	vector<int> V;
	for(int i = 0; i < N; i++){
		if(used[i] == false){
			V.push_back(i);
		}
	}

	int ans = P;
	int right,V_index = 0;
	bool Found;

	if(V.size() == 0){
		printf("%d\n",P);
		return;
	}

	for(int w_day = 0; w_day < 7; w_day++){
		for(int time = 0; time < 1440;){
			if(check[w_day][time]){
				time++;
				continue;
			}
			for(right = time; right+1 < 1440 && check[w_day][right+1] == false; right++);

			if(right-time+1 < 30){

				for(; V_index < V.size();){
					if((info[V[V_index]].weekday < w_day) || (info[V[V_index]].weekday == w_day && info[V[V_index]].start <= right)){
						 V_index++;
					}else{
						break;
					}
				}

				time = right+1;
			}else{

				Found = false;

				for(; V_index < V.size();){
					if((info[V[V_index]].weekday < w_day) || (info[V[V_index]].weekday == w_day && info[V[V_index]].start < time)){
						V_index++;
					}else if(info[V[V_index]].weekday == w_day && info[V[V_index]].start >= time && info[V[V_index]].end <= right){
						ans++;
						time = info[V[V_index]].end+1;
						Found = true;
						V_index++;
						break;
					}else{
						break;
					}
				}
				if(!Found){
					time = right+1;
				}
			}
		}
	}

	printf("%d\n",ans);
}


int main(){

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

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3208
final_rank: 6

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<complex>
#include<vector>
#include<map>
using namespace std;

/*struct anime{
  string name;
  int weekday;
  int start;
  long int minutes;
  bool favorite;
  void setup(string n,int w,int s){
    name = n;
    weekday = w;
    start = s;
    favorite = false;
    int h = start / 100;
    int m = start % 100;
    if(h >= 24){
      h -= 24;
      w++;
    }
    minutes = h*60 + m + weekday * 60 * 24;
  }
};*/

int setup(int w,int s){
  int h = s / 100;
  int m = s % 100;
  if(h >= 24){
    h -= 24;
    w++;
  }
  return h*60 + m + w * 60 * 24;

}
/*
bool operator<(anime a1,anime a2){
  return a1.minutes < a2.minutes;
}*/

int main(){

    int n,f,we,st,count;
    string na;

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

      vector<int> table;
      vector<int> favoritetable;
      map<string,int> amap;

      for(int i=0; i<n; i++){
        cin >> na >> we >> st;
        int t;
        t = setup(we,st);
        table.push_back(t);
        amap[na] = t;
      }

      cin >> f;

      for(int i=0; i<f; i++){
        cin >> na;
        favoritetable.push_back(amap[na]);
      }

      sort(table.begin() , table.end());
      sort(favoritetable.begin(), favoritetable.end());

    //  int first=0,last=1,beforelast;
      bool flag,cansee = true,changelast;
      count = 0;

      for(int i=1; i<favoritetable.size(); ++i){
        if(abs(favoritetable[i] - favoritetable[i-1]) < 30){
          cansee = false;
        }
      }

      for(int i=0; i<table.size(); i++){
        for(int j=0; j<favoritetable.size(); j++){
          if(abs(table[i] - favoritetable[j]) < 30){
            table.erase(table.begin() + i);
            i--;
            break;
          }
        }
      }

      int ans = table.size() + f;
      long int tmp = 0;

      for(int i=0; i<table.size(); i++){
        if(abs(table[i] - tmp) < 30){
          ans--;
        }
        else{
          tmp = table[i];
        }
      }

      if(!cansee){
        cout << -1 << endl;
      }
      else{
        cout << ans << endl;
      }

      /*

      while(last != n){

        while(1){
          if(abs(table[last].minutes - table[first].minutes) >= 30){
              break;
          }
          last++;
          if(last == n){
            last--;
            break;
          }
        }

        beforelast = last;

        flag = false;

        changelast = false;

        for(int i=first; i<last; i++){
          if(!flag && table[i].favorite){
            flag = true;
            if(i != first){
              last = i;
              changelast = true;
            }
          }
          else if(flag && table[i].favorite){
            cansee = false;
            break;
          }
        }

        if(!cansee){
          break;
        }

        if(!flag || (flag && !changelast) ){//???????????????????????£??????????????????????????´???????????????count++????????????
          count++;
        }

        first = last;
        last++;

      }

      if(abs(table[n-1].minutes - table[n-2].minutes) >= 30){
        count++;
      }



      if(!cansee){
        cout << -1 << endl;
      }
      else{
        cout << count << endl;
      }

      */

    }


    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3092
final_rank: 13

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
	int n;
	while(cin>>n,n){
		map<string,int>ma;
		vi in(n);
		rep(i,n){
			string s;int a,b;
			cin>>s>>a>>b;
			in[i]=(a*24*60)+(b/100*60+b%100)-6*60;
			ma[s]=i;
		}
		int m;
		cin>>m;
		int out=m;
		vi dp(7*24*60+100);
		bool h=true;
		rep(i,m){
			string s;
			cin>>s;
			int t=ma[s];
			loop(j,in[t],in[t]+30){
				if(dp[j])h=false;
				dp[j]=true;
			}
		}
		if(!h){
			cout<<-1<<endl;
			continue;
		}
		sort(all(in));
		int t=-1;
		rep(i,n){
			if(dp[in[i]]||dp[in[i]+29])continue;
			if(in[i]<t)continue;
			t=in[i]+30;
			out++;
		}
		cout<<out<<endl;
	}
}
accuracy: 1
time_ms: 110
memory_kb: 3252
final_rank: 14

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"
struct tv {
	string name;
	int time;
};
int main() {
	while (1) {
		int N; cin >> N; 
		if (!N)break;
		vector<tv>tvs;
		vector<int>needs;
		for (int i = 0; i < N; ++i) {
			string name; int w, s; cin >> name >> w >> s;
			int time = w * 1440 + (s / 100) * 60 + s % 100;
			tvs.push_back(tv{ name,time });
		}
		sort(tvs.begin(), tvs.end(), [](const tv&l, const tv&r) {
			return l.time < r.time;
		});
		for (int i = 0; i < N; ++i) {
			tv atv{ tvs[i].name,tvs[i].time + 1440 * 7 };
			tvs.emplace_back(atv);
		}
		int P; cin >> P;
		for (int i = 0; i < P; ++i) {
			string target; cin >> target;
			auto it = find_if(tvs.begin(), tvs.end(), [=](const tv&t) {
				return t.name == target;
			});
			needs.push_back(it - tvs.begin());
		}
		sort(needs.begin(), needs.end());
		needs.push_back((*needs.begin())+N);
		bool ok = true;
		int ans = P;
		for (int i = 0; i < needs.size()-1; ++i) {
			int from = tvs[needs[i]].time + 30;
			int to = tvs[needs[i + 1]].time;
			if (to < from)ok = false;
			else {
				int sum = 0;
				for (int p = needs[i] + 1; p < needs[i + 1]; ++p) {
					if (from <= tvs[p].time&&tvs[p].time + 30 <= to) {
						sum++;
						from = tvs[p].time + 30;
					}
				}
				ans += sum;
			}
			
		}
		if (!ok)cout << -1 << endl;
		else {

			cout << ans << endl;
		}

	}
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3128
final_rank: 11

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<set>
#include<stack>

using namespace std;
typedef unsigned long long ll;

map<string, int>ma;
vector<pair<int, string> >vec;

struct sortVec{
    bool operator()(string m1, string m2){
        return ma[m1]<ma[m2];
    }
}sv;

int main(){
    int n,p;
    while(cin>>n,n){
        ma.clear();
        vec.clear();
        string s;
        for(int i=0;i<n;i++){
            int day,time;
            cin>>s>>day>>time;
            if(time>=2400){
                day=(day+1);
                time-=2400;
            }
            time=(time/100)*60+time%100+day*24*60;
            ma[s]=time;
            vec.push_back(make_pair(time,s));
        }
        sort(vec.begin(),vec.end());
        cin>>p;
        vector<string>fav;
        for(int i=0;i<p;i++){
            cin>>s;
            fav.push_back(s);
        }
        sort(fav.begin(),fav.end(),sv);
        vector<string>res;
        res.push_back(fav[0]);
        bool nex=false;
        for(int i=1;i<fav.size();i++){
            if(ma[fav[i]]<ma[fav[i-1]]+30){
                cout<<-1<<endl;
                nex=true;
                break;
            }
            res.push_back(fav[i]);
            sort(res.begin(),res.end(),sv);
        }
        if(nex){
            continue;
        }
        for(int i=0;i<vec.size();i++){
            int start=vec[i].first;
            int resi=0;
            while(resi<res.size()&&ma[res[resi]]+30<=start){
                resi++;
            }
            if(resi==res.size()||start+30<=ma[res[resi]]){
                if(resi>0&&ma[res[resi-1]]+30>start){
                    continue;
                }
                res.push_back(vec[i].second);
                sort(res.begin(),res.end(),sv);
            }
        }
        cout<<res.size()<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2740
memory_kb: 1324
final_rank: 19

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<set>
#include<stack>

using namespace std;
typedef unsigned long long ll;

map<string, int>ma;
vector<pair<int, string> >vec;

struct sortVec{
    bool operator()(string m1, string m2){
        return ma[m1]<ma[m2];
    }
}sv;

int main(){
    int n,p;
    while(cin>>n,n){
        ma.clear();
        vec.clear();
        string s;
        for(int i=0;i<n;i++){
            int day,time;
            cin>>s>>day>>time;
            time=(time/100)*60+time%100+day*24*60;
            ma[s]=time;
            vec.push_back(make_pair(time,s));
        }
        sort(vec.begin(),vec.end());
        cin>>p;
//        set<string>fav;
//        for(int i=0;i<p;i++){
//            cin>>s;
//            fav.insert(s);
//        }
//        int p=-100000;
//        bool m=false;
//        int c=0;
//        for(int i=0;i<n;i++){
//            if(p+30>vec[i].first){
//                if(fav.count(vec[i].second)){
//                    if(m){
//                        c=-1;
//                        break;
//                    }else{
//                        m=true;
//                        p=vec[i].first;
//                    }
//                }
//            }else{
//                c++;
//                p=vec[i].first;
//                m=fav.count(vec[i].second);
//            }
//        }
//        cout<<c<<endl;
        vector<string>fav;
        for(int i=0;i<p;i++){
            cin>>s;
            fav.push_back(s);
        }
        sort(fav.begin(),fav.end(),sv);
        vector<string>res;
        res.push_back(fav[0]);
        bool nex=false;
        for(int i=1;i<fav.size();i++){
            if(ma[fav[i]]<ma[fav[i-1]]+30){
                cout<<-1<<endl;
                nex=true;
                break;
            }
            res.push_back(fav[i]);
            sort(res.begin(),res.end(),sv);
        }
        if(nex){
            continue;
        }
        for(int i=0;i<vec.size();i++){
            int start=vec[i].first;
            int resi=0;
            while(resi<res.size()&&ma[res[resi]]+30<=start){
                resi++;
            }
            if(resi==res.size()||start+30<=ma[res[resi]]){
                if(resi>0&&ma[res[resi-1]]+30>start){
                    continue;
                }
                res.push_back(vec[i].second);
                sort(res.begin(),res.end(),sv);
            }
        }
        cout<<res.size()<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2730
memory_kb: 1324
final_rank: 18

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<set>
#include<stack>

using namespace std;
typedef unsigned long long ll;

map<string, int>ma;
vector<pair<int, string> >vec;

struct sortVec{
    bool operator()(string m1, string m2){
        return ma[m1]<ma[m2];
    }
}sv;

int main(){
    int n,p;
    while(cin>>n,n){
        ma.clear();
        vec.clear();
        string s;
        for(int i=0;i<n;i++){
            int day,time;
            cin>>s>>day>>time;
            time=(time/100)*60+time%100+day*24*60;
            ma[s]=time;
            vec.push_back(make_pair(time,s));
        }
        sort(vec.begin(),vec.end());
        cin>>p;
        set<string>fav;
        for(int i=0;i<p;i++){
            cin>>s;
            fav.insert(s);
        }
        int p=-100000;
        bool m=false;
        int c=0;
        for(int i=0;i<n;i++){
            if(p+30>vec[i].first){
                if(fav.count(vec[i].second)){
                    if(m){
                        c=-1;
                        break;
                    }else{
                        m=true;
                        p=vec[i].first;
                    }
                }
            }else{
                c++;
                p=vec[i].first;
                m=fav.count(vec[i].second);
            }
        }
        cout<<c<<endl;
//        vector<string>fav;
//        for(int i=0;i<p;i++){
//            cin>>s;
//            fav.push_back(s);
//        }
//        sort(fav.begin(),fav.end(),sv);
//        vector<string>res;
//        res.push_back(fav[0]);
//        bool nex=false;
//        for(int i=1;i<fav.size();i++){
//            if(ma[fav[i]]%10080<(ma[fav[i-1]]+30)%10080){
//                cout<<-1<<endl;
//                nex=true;
//                break;
//            }
//            res.push_back(fav[i]);
//        }
//        if(nex){
//            continue;
//        }
//        for(int i=0;i<vec.size();i++){
//            int start=vec[i].first;
//            int resi=0;
//            while(resi<res.size()&&(ma[res[resi]]+30)%10080<=start%10080){
//                resi++;
//            }
//            if(resi==res.size()||(start+30)%10080<=ma[res[resi]]){
//                if(resi>0&&ma[res[resi-1]]+30>start){
//                    continue;
//                }
//                res.push_back(vec[i].second);
//                sort(res.begin(),res.end(),sv);
//            }
//        }
//        cout<<res.size()<<endl;
    ne:;
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 1304
final_rank: 3

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<set>
#include<stack>

using namespace std;
typedef unsigned long long ll;

map<string, int>ma;
vector<pair<int, string> >vec;

struct sortVec{
    bool operator()(string m1, string m2){
        return ma[m1]<ma[m2];
    }
}sv;

int main(){
    int n,p;
    while(cin>>n,n){
        ma.clear();
        vec.clear();
        string s;
        for(int i=0;i<n;i++){
            int day,time;
            cin>>s>>day>>time;
//            if(time>=2400){
//                day=(day+1)%7;
//                time-=2400;
//            }
            time=(time/100)*60+time%100+day*24*60;
            ma[s]=time;
            vec.push_back(make_pair(time,s));
        }
        sort(vec.begin(),vec.end());
        cin>>p;
        set<string>fav;
        for(int i=0;i<p;i++){
            cin>>s;
            fav.insert(s);
        }
        int p=-100000;
        bool m=false;
        int c=0;
        for(int i=0;i<n;i++){
            if(p+30>vec[i].first){
                if(fav.count(vec[i].second)){
                    if(m){
                        c=-1;
                        break;
                    }else{
                        m=true;
                        p=vec[i].first;
                    }
                }
            }else{
                c++;
                p=vec[i].first;
                m=fav.count(vec[i].second);
            }
        }
        cout<<c<<endl;
    ne:;
    }
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 1304
final_rank: 5

code_snippet: #include <cstdio>
#include <cstring>
#include <set>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int n,p;
struct data{
	string name;
	int h,m;
	data(){}
	data(string s,int hh,int mm){
		name=s;
		h=hh;
		m=mm;
	}
};

bool comp(const data& d1,const data& d2){
	if(d1.h==d2.h)return d1.m<d2.m;
	return d1.h<d2.h;
}

data d[7][501];
int dcnt[7];
set<string> fav;
bool used[7][30][61];

bool check(){
	for(int i=0;i<7;i++){
		for(int j=0;j<dcnt[i];j++){
			if(fav.find(d[i][j].name)!=fav.end()){
				int k=0,nh=d[i][j].h,nm=d[i][j].m;
				while(k<30){
					if(used[i][nh][nm])return false;
					used[i][nh][nm]=true;
					k++;
					nm++;
					if(nm==60){
						nm=0;
						nh++;
					}
				}
			}
		}
	}
	return true;
}
int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		memset(dcnt,0,sizeof(dcnt));
		for(int i=0;i<n;i++){
			int w,s;
			string str;
			cin >> str >> w >> s;
			d[w][dcnt[w]]=data(str,s/100,s%100);
			dcnt[w]++;
		}
		fav.clear();
		scanf("%d",&p);
		for(int i=0;i<p;i++){
			string str;
			cin >> str;
			fav.insert(str);
		}
		memset(used,false,sizeof(used));
		if(!check()){
			printf("-1\n");
			continue;
		}
		int cnt=0;
		for(int i=0;i<7;i++){
			sort(d[i],d[i]+dcnt[i],comp);
			for(int j=0;j<dcnt[i];j++){
				if(fav.find(d[i][j].name)!=fav.end())continue;
				int k=0,nh=d[i][j].h,nm=d[i][j].m;
				while(k<30){
					if(used[i][nh][nm])break;
					k++;
					nm++;
					if(nm==60){
						nm=0;
						nh++;
					}
				}
				if(k==30){
					k=0,nh=d[i][j].h,nm=d[i][j].m;
					cnt++;
					while(k<30){
						if(used[i][nh][nm])return false;
						used[i][nh][nm]=true;
						k++;
						nm++;
						if(nm==60){
							nm=0;
							nh++;
						}
					}
				}
			}
		}
		printf("%d\n",cnt+p);
	}
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 1408
final_rank: 1

code_snippet: #include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#define MAX_T 1440
using namespace std;
 
typedef pair<int, string> pis;
 
int main()
{
  while(1){
    int N;
    cin >> N;
    if(N == 0){
      break;
    }else{
      multimap<int, string> m;
      multimap<int, string>::iterator ite;
      vector<string> f;
      int P, ans = 0;
      bool flag = false;
      for(int i = 0; i < N; i++){
        string n, s;
        int w, x;
        cin >> n >> w >> s;
        if(s.size() == 3)
          x = (s[0] - '0') * 60 + (s[1] - '0') * 10 + (s[2] - '0');
        else
          x = ((s[0] - '0') * 10 + (s[1] - '0')) * 60 + (s[2] - '0') * 10 + (s[3] - '0');
        m.insert(pis(w * MAX_T + x, n));
      }
      cin >> P;
      for(int i = 0; i < P; i++){
        string s;
        cin >> s;
        f.push_back(s);
      }
      sort(f.begin(), f.end());
      int n = f.size();
      for(int i = 0; i < n; i++){
        if(f.empty() || flag)
          break;
        for(ite = m.begin(); ite != m.end(); ++ite){
          if(ite->second == f[i]){
            int x = ite->first;
            multimap<int, string>::iterator start;
            multimap<int, string>::iterator end;
            start = m.upper_bound(x - 30);
            end = m.lower_bound(x + 30);
            //if(start == m.end())
            //start = ite;
            m.erase(start, end);
            f.erase(f.begin());
            ans++;
            i = -1;
            break;
          }
          ite++;
          if(ite == m.end())
            flag = true;
          ite--;
        }
      }
      if(flag){
        cout << "-1" << endl;
      }else{ 
        while(!m.empty()){
          multimap<int, string>::iterator start;
          multimap<int, string>::iterator end;
          start = m.begin();
          end = m.lower_bound(start->first + 30);
          m.erase(start, end);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
}
accuracy: 1
time_ms: 150
memory_kb: 1296
final_rank: 2