Problem:
Problem D:
Pump up Batteries
Bill is a boss of security guards. He has pride in that his men put on wearable computers on their
duty. At the same time, it is his headache that capacities of commercially available batteries are
far too small to support those computers all day long. His men come back to the office to charge
up their batteries and spend idle time until its completion. Bill has only one battery charger in
the office because it is very expensive.
Bill suspects that his men spend much idle time waiting in a queue for the charger. If it is the
case, Bill had better introduce another charger. Bill knows that his men are honest in some
sense and blindly follow any given instructions or rules. Such a simple-minded way of life may
lead to longer waiting time, but they cannot change their behavioral pattern.
Each battery has a data sheet attached on it that indicates the best pattern of charging and
consuming cycle. The pattern is given as a sequence of pairs of consuming time and charging
time. The data sheet says the pattern should be followed cyclically to keep the battery in quality.
A guard, trying to follow the suggested cycle strictly, will come back to the office exactly when
the consuming time passes out, stay there until the battery has been charged for the exact time
period indicated, and then go back to his beat.
The guards are quite punctual. They spend not a second more in the office than the time
necessary for charging up their batteries. They will wait in a queue, however, if the charger is
occupied by another guard, exactly on first-come-first-served basis. When two or more guards
come back to the office at the same instance of time, they line up in the order of their identifi-
cation numbers, and, each of them, one by one in the order of that line, judges if he can use the
charger and, if not, goes into the queue. They do these actions in an instant.
Your mission is to write a program that simulates those situations like Bill’s and reports how
much time is wasted in waiting for the charger.
Input
The input consists of one or more data sets for simulation.
The first line of a data set consists of two positive integers separated by a space character: the
number of guards and the simulation duration. The number of guards does not exceed one
hundred. The guards have their identification numbers starting from one up to the number of
guards. The simulation duration is measured in minutes, and is at most one week, i.e., 10080
(min.).
Patterns for batteries possessed by the guards follow the first line. For each guard, in the order
of identification number, appears the pattern indicated on the data sheet attached to his battery.
A pattern is a sequence of positive integers, whose length is a multiple of two and does not exceed
fifty. The numbers in the sequence show consuming time and charging time alternately. Those
times are also given in minutes and are at most one day, i.e., 1440 (min.). A space character or
a newline follows e
...(truncated)

Submitted solutions:

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

struct E{
	int ot;
	int nt;
	int t;
	int no;
	int s;
	bool operator<(const E& e)const{
		if(t!=e.t)return t>e.t;
		return no>e.no;
	}
};

void f(int n,int m){
	vector<vector<int> > ds;
	vector<int> v;
	vector<int> ss;
	priority_queue<E> pqw;
	queue<E> qs;
	
	for(int i=0;i<n;i++){
		int t;
		ds.push_back(v);
		while(true){
			cin>>t;
			if(t==0)break;
			ds[i].push_back(t);
		}
		ss.push_back(ds[i].size());
		E e;
		e.t=ds[i][0];
		e.ot=0;
		e.no=i;
		e.s=0;
		pqw.push(e);
	}
	
	int t=0;
	int z=0;
	long long int ans=0;
	bool addo=true;
	while(pqw.size()>0 || qs.size()>0){
		z++;
		E e1;
		if(qs.size()==0 && pqw.size()>0){
			e1=pqw.top();
			pqw.pop();
			addo=true;
		}else if(pqw.size()==0 && qs.size()>0){
			e1=qs.front();
			qs.pop();
			if(qs.size()>0){
				E e2=qs.front();
				qs.front().nt=e1.t;
				qs.front().t=e1.t+ds[e2.no][e2.s];
			}
			addo=false;
		}else{
			E eq=qs.front();
			E ep=pqw.top();
			
			if(eq.t<=ep.t){
				e1=qs.front();
				qs.pop();
				if(qs.size()>0){
					E e2=qs.front();
					qs.front().nt=e1.t;
					qs.front().t=e1.t+ds[e2.no][e2.s];
				}
				addo=false;
			}else{
				e1=pqw.top();
				pqw.pop();
				addo=true;
			}
		}
		//cout<<e1.no<<" "<<e1.t<<" "<<t<<" "<<e1.ot<<" "<<ds[e1.no][e1.s]<<" "<<addo<<" "<<ans;
		//if(qs.size()>0){
		//	cout<<" "<<qs.front().t;
		//}
		//cout<<endl;
		if(addo==false){
			ans+=min(m,e1.nt)-e1.ot;
		}
		
		if(e1.t>=m){
			continue;
		}
		t=e1.t;
		
		e1.s=(e1.s+1)%ss[e1.no];
		if(addo==true){
			e1.ot=t;
			if(qs.size()==0){
				e1.t=t+ds[e1.no][e1.s];
				e1.nt=e1.ot;
			}
			qs.push(e1);
		}else{
			
			e1.t=t+ds[e1.no][e1.s];
			pqw.push(e1);
		}
	
	}
	cout<<ans<<endl;
}

int main() {
	while(true){
		int n,m;
		cin>>n>>m;
		if(n==0 && m==0)break;
		f(n,m);
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3168
final_rank: 13

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
typedef long long int ll;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...)
#endif
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...)); }
template<typename T>
bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}
template<typename T>
bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}
// clang-format on

#include <utility>
using Bat = pair<int, int>;
#include <queue>

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, t;
    while (cin >> n >> t, n + t) {
        vector<vector<Bat>> v(n);
        REP(i, 0, n) {
            for (;;) {
                int a, b;
                cin >> a;
                if (a == 0) break;
                cin >> b;
                v[i].emplace_back(a, b);
            }
        }
        queue<int> q;
        vector<int> cur(n), pat(n, 0);
        REP(i, 0, n) {
            cur[i] = v[i][0].first;
        }
        ll ans = 0;
        int rem = 0, irem = -1;
        REP(T, 0, t) {
            REP(i, 0, n) {
                if (cur[i] == -1) continue;
                if (cur[i]-- == 0) {
                    q.emplace(i);
                }
            }
            if (irem == -1 && q.size()) {
                irem = q.front();
                rem = v[irem][pat[irem]].second;
                q.pop();
            }
            if (irem != -1 && --rem == 0) {
                pat[irem] = (pat[irem] + 1) % v[irem].size();
                cur[irem] = v[irem][pat[irem]].first;
                irem = -1;
                rem = -1;
            }
            ans += q.size();
            /*
            REP(i, 0, n) {
                cout << cur[i] << " ";
            }
            cout << q.size() << endl;
            */
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3164
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

constexpr long double EPS = 1e-15;
const long double PI = acos(-1);
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9+7;
constexpr ll MOD1 = 998244353;
typedef pair<ll,ll> P;

//#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
//#define mp make_pair

bool pattern[110][10100];
int pos[110];
int n,m;

void solve() {
while (cin >> n >> m) {
    if (n == 0 && m == 0) break;
    
    for (int i = 0; i < n; i++) {
        pos[i] = 0;
        vector<int> v;
        int k;
        while (cin >> k, k) {
            v.pb(k);
        }
        int p = 0;
        bool status = 0;
        int vv = 0;
        while (p <= 10090) {
            int kk = v[vv];
            REP(j,kk) {
                if (p + 1 >= 10100) break;
                pattern[i][p++] = status;
            }
            vv = (vv + 1) % sz(v);
            status = !status;
        }
    }

    ll ans = 0;
    bool inque[110] = {};
    queue<int> que;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (!pattern[j][pos[j]]) pos[j]++;
            else {
                if (!inque[j]) {
                    que.push(j);
                    inque[j] = 1;
                }
            }
        }
        if (!que.empty()) {
            pos[que.front()]++;
            ans += que.size() - 1;
            if (pattern[que.front()][pos[que.front()]] == 0) {
                inque[que.front()] = 0;
                que.pop();
            }
        }
        // REP(j,n) {
        //     cout << pos[j] << " ";
        // }
        // cout << endl;
    }
    cout << ans << endl;
}
}


int main(int argc, char *argv[]){

    // /* Regular */
    // int case_num = 1;
    // if (argc > 1 && stoi(argv[1])) cin >> case_num;
    // REP(case_no, case_num) {
    //   cerr << endl << "case " << case_no + 1 << endl;
    //   solve();
    // }

    /* AOJ */
    solve();

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4036
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{
	int cicle[51],cicile_index,cicle_num,wait_time,tmp_time;
	bool is_queue;
};

int N,Time_Limit;
Info info[100];

void func(){

	int tmp;

	for(int i = 0; i < N; i++){
		info[i].cicile_index = 0;
		info[i].wait_time = 0;
		info[i].tmp_time = 0;
		info[i].is_queue = false;

		for(int k = 0; ;k++){
			scanf("%d",&tmp);
			if(tmp != 0){
				info[i].cicle[k] = tmp;
			}else{
				info[i].cicle_num = k;
				break;
			}
		}
	}

	queue<int> Q;
	bool Finished;

	for(int time = 0; time < Time_Limit; time++){
		Finished = false;
		for(int i = 0; i < N; i++){
			if(info[i].is_queue == false){
				info[i].tmp_time++;
				if(info[i].tmp_time == info[i].cicle[info[i].cicile_index]){
					info[i].tmp_time = 0;
					info[i].cicile_index++;
					Q.push(i);
					info[i].is_queue = true;
				}
			}else{
				if(i == Q.front()){
					info[i].tmp_time++;
					if(info[i].tmp_time == info[i].cicle[info[i].cicile_index]){
						info[i].tmp_time = 0;
						if(info[i].cicile_index == info[i].cicle_num-1){
							info[i].cicile_index = 0;
						}else{
							info[i].cicile_index++;
						}
						info[i].is_queue = false;
						Finished = true;
					}
				}else{
					info[i].wait_time++;
				}
			}
		}
		if(Finished)Q.pop();
	}

	int ans = 0;

	for(int i = 0; i < N; i++){
		ans += info[i].wait_time;
	}

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

int main(){

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

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3236
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int,int> P;
 
int main() {
  int n,t;
  while(cin>>n>>t&&n) {
    vector<P> v[n];
    int c[n],d=0;
    memset(c,0,sizeof(c));
    priority_queue<P,vector<P>,greater<P> > que;
    for(int i=0; i<n; i++) {
      int x,y;
      while(cin>>x&&x) {
        cin>>y;
        v[i].push_back(P(x,y));
      }
      if(v[i].size()) que.push(P(v[i][0].F,i));
    }
    int ans=0;
    for(int i=0; i<t; i++) {
      if(que.top().F<=i&&!d) {
        P p=que.top();que.pop();
        ans+=max(0,i-p.F);
        int k=p.S;
        d+=v[k][c[k]].S;
        c[k]=(c[k]+1)%v[k].size();
        que.push(P(i+v[k][c[k]].F+d,k));
      }
      if(d) d--;
    }
    while(!que.empty()) {
      P p=que.top();que.pop();
      ans+=max(0,t-p.F);
    }
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3124
final_rank: 12

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"


int main() {
	while (1) {
		int N, T; cin >> N >> T;
		if (!N)break;
		int num = 0;
		vector<int>v;
		vector<vector<int>>guards(N);
		while (num < N) {
			int a; cin >> a;
			if (a != 0) {
				guards[num].push_back(a);
			}
			else {
				num++;
			}
		}
		vector<pair<int, int>>status(N,make_pair(0,0));
		deque<int>que;
		int ans = 0;
		for (int t = 0; t < T; ++t) {
			int nbat = -1;
			if (!que.empty()) {
				ans += que.size() - 1;
				nbat = que.front();
			}
			for (int i = 0; i < N; ++i) {
				
				bool nowbat = status[i].first % 2;
				if (nowbat) {
					if (i ==nbat) {
						status[i].second++;
						if (status[i].second == guards[i][status[i].first]) {
							status[i].second = 0;
							status[i].first = (status[i].first + 1) % guards[i].size();
							que.pop_front();
						}
					}
				}
				else {
					status[i].second++;
					if (status[i].second == guards[i][status[i].first]) {
						status[i].second = 0;
						status[i].first = (status[i].first+1) % guards[i].size();
						que.push_back(i);
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3184
final_rank: 15

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1<<28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct guard {
	vector<bool> cycle;
	int cnt;
};

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

	int g, d;
	while(cin >> g >> d, g|d) {
		vector<guard> v(g);
		REP(i, g) {
			int in, in2 = 1;
			while(cin >> in, in) {
				REP(j, in)
					v[i].cycle.pb(in2);
				in2 = (in2 + 1) % 2;
			}
		}

		queue<int> q;
		set<int> s;
		REP(i, d) {
			REP(j, g) {
				if(v[j].cycle[v[j].cnt % v[j].cycle.size()])
					v[j].cnt++;
				else {
					if(!s.count(j)) {
						q.push(j);
						s.insert(j);
					}
				}
			}
			if(q.size()) {
				int top = q.front();
				v[top].cnt++;
				if(v[top].cycle[v[top].cnt % v[top].cycle.size()]) {
					q.pop(); s.erase(top);
				}
			}
		}
		int ans = 0;
		REP(i, g)
			ans += d - v[i].cnt;
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 1388
final_rank: 18

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

int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int n,T;
  while(cin >> n >> T, n){
    vector< vector<int> > cycle(n);
    for(int i=0;i<n;i++){
      int tmp;
      while(cin >> tmp, tmp)cycle[i].push_back(tmp);
    }
    vector<int> pos(n,0), end_time(n);
    for(int i=0;i<n;i++){
      end_time[i] = cycle[i][0];
      (pos[i] += 1) %= cycle[i].size();
    }

    queue<int> q;
    int charge = -1, charge_id = -1;
    int ans = 0;

    for(int t=0;t<T;t++){
      for(int i=0;i<n;i++){
	if(t==end_time[i]){
	  end_time[i] = -1;
	  q.push(i);
	}
      }
      
      if(charge <= t){
	if(charge_id>=0){
	  end_time[charge_id] = t + cycle[charge_id][pos[charge_id]];
	  (pos[charge_id] += 1) %= cycle[charge_id].size();
	  charge = charge_id = -1;
	}

	if(q.size()){
	  int nxt = q.front(); q.pop();
	  charge = t + cycle[nxt][pos[nxt]];
	  (pos[nxt] += 1) %= cycle[nxt].size();
	  charge_id = nxt;
	}
      }

      ans += q.size();
    }
	  
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 1240
final_rank: 1

code_snippet: #include <string> 
#include <algorithm> 
#include <cfloat> 
#include <climits> 
#include <cmath> 
#include <complex> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <functional> 
#include <iostream> 
#include <map> 
#include <memory> 
#include <queue> 
#include <set> 
#include <sstream> 
#include <stack> 
#include <utility> 
#include <vector> 

#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(),(x).end() 
typedef long long ll;
using namespace std; 
const double eps = 1e-10;

struct E{
    int id, time;
    E(){}
    E(int id, int time) : id(id), time(time) { }
};
bool operator<(const E &a, const E &b){
    if(a.time == b.time) return a.id > b.id;
    else return a.time > b.time;
}

int main(){
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) return 0;
        vector<vector<int> > v(n);
        for(int i = 0; i < n; ++i){
            for(;;){
                int t;
                cin >> t;
                if(t == 0) break;
                v[i].push_back(t);
            }
        }

        vector<int> idx(n);
        priority_queue<E> eve;
        queue<int> Q;

        int res = 0;
        for(int i = 0; i < n; ++i){
            eve.push(E(i, v[i][0]));
        }
        eve.push(E(n, m));
        int lt = 0;
        while(!eve.empty()){
            int ct = eve.top().time;
            int id = eve.top().id;
            eve.pop();
            // cout << ct << "," << id << "," << max(0, (int)Q.size()-1) * (ct - lt) << endl;
            res += max(0, (int)Q.size()-1) * (ct - lt);
            if(id == n) break;
            else if(id < 0){
                int id = Q.front(); Q.pop();
                idx[id] = (idx[id] + 1) % v[id].size();
                eve.push(E(id, ct + v[id][idx[id]]));
                if(!Q.empty()){
                    eve.push(E(-1, ct + v[Q.front()][idx[Q.front()]]));
                }
            }
            else if(idx[id] % 2 == 0){
                idx[id] = (idx[id] + 1) % v[id].size();
                if(Q.empty()){
                    Q.push(id);
                    eve.push(E(-1, ct + v[id][idx[id]]));
                }
                else{
                    Q.push(id);
                }
            }
            lt = ct;
        }
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 1248
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

class State {
public:
  int _id;
  int _time;
  int _occupy;
  State(int id,int time,int occupy) : _id(id),_time(time), _occupy(occupy) {}
  bool operator<(const State& s) const {
    if(_time == s._time) return _id < s._id;
    return _time < s._time;
  }
  bool operator>(const State& s) const {
    if(_time == s._time) return _id > s._id;
    return _time > s._time;
  }
};

int main(){
  int num_of_guards;
  int simulation_duration;
  while(~scanf("%d %d",&num_of_guards,&simulation_duration)){
    if(num_of_guards == 0 && simulation_duration == 0) break;

    vector<int> time_table[101];
    for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
      int consuming_duration;
      int charging_duration;
      while(~scanf("%d",&consuming_duration)){
        if(consuming_duration == 0) break;
        scanf("%d",&charging_duration);
        time_table[guard_i].push_back(consuming_duration);
        time_table[guard_i].push_back(charging_duration);
      }
    }
    
    int for_queue[101];
    int next_process[101];
    bool is_inque[101];
    memset(next_process,0,sizeof(next_process));
    memset(for_queue,0,sizeof(for_queue));
    memset(is_inque,false,sizeof(is_inque));

    priority_queue<State,vector<State>,greater<State> > que;
    int res = 0;
    for(int time = 0; time < simulation_duration;time++){
      char action[101];
      memset(action,'-',sizeof(action));
      for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
        if(next_process[guard_i] % 2 == 0){ // to consume
          if(is_inque[guard_i]){
            continue;
          }
          for_queue[guard_i] = time_table[guard_i][next_process[guard_i] % time_table[guard_i].size()] - 1;
          next_process[guard_i]++;
          action[guard_i] = '*';
        }
        else{ //to charge
          if(for_queue[guard_i] > 0){
            action[guard_i] = '*';
            for_queue[guard_i]--;
            continue;
          }

          que.push(State(guard_i,time,time_table[guard_i][next_process[guard_i] % time_table[guard_i].size()]));
          next_process[guard_i]++;
          is_inque[guard_i] = true;
        }
      }
      if(!que.empty()){
        State s = que.top();
        que.pop();
        action[s._id] = '.';
        if(s._occupy - 1 > 0){
          que.push(State(s._id,s._time,s._occupy-1));
        }
        else{
          is_inque[s._id] = false;
        }
      }

      res += count(action,action+num_of_guards,'-');

      // for debug
      // printf("time:%d\n",time);
      // for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
      //         printf("%c\n",action[guard_i]);
      // }
      // printf("\n");
    }

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

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

class State {
public:
  int _id;
  int _time;
  int _occupy;
  State(int id,int time,int occupy) : _id(id),_time(time), _occupy(occupy) {}
  bool operator<(const State& s) const {
    if(_time == s._time) return _id < s._id;
    return _time < s._time;
  }
  bool operator>(const State& s) const {
    if(_time == s._time) return _id > s._id;
    return _time > s._time;
  }
};

int main(){
  int num_of_guards;
  int simulation_duration;
  while(~scanf("%d %d",&num_of_guards,&simulation_duration)){
    if(num_of_guards == 0 && simulation_duration == 0) break;

    vector<int> time_table[101];
    for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
      int comsuming_duration;
      int charging_duration;
      while(~scanf("%d",&comsuming_duration)){
        if(comsuming_duration == 0) break;
        scanf("%d",&charging_duration);
        time_table[guard_i].push_back(comsuming_duration);
        time_table[guard_i].push_back(charging_duration);
      }
    }
    
    int for_queue[101];
    int next_process[101];
    bool is_inque[101];
    memset(next_process,0,sizeof(next_process));
    memset(for_queue,0,sizeof(for_queue));
    memset(is_inque,false,sizeof(is_inque));

    priority_queue<State,vector<State>,greater<State> > que;
    int res = 0;
    for(int time = 0; time < simulation_duration;time++){
      char action[101];
      memset(action,'-',sizeof(action));
      for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
        if(next_process[guard_i] % 2 == 0){ // to consume
          if(is_inque[guard_i]){
            continue;
          }
          for_queue[guard_i] = time_table[guard_i][next_process[guard_i] % time_table[guard_i].size()] - 1;
          next_process[guard_i]++;
          action[guard_i] = '*';
        }
        else{ //to charge
          if(for_queue[guard_i] > 0){
            action[guard_i] = '*';
            for_queue[guard_i]--;
            continue;
          }

          que.push(State(guard_i,time,time_table[guard_i][next_process[guard_i] % time_table[guard_i].size()]));
          next_process[guard_i]++;
          is_inque[guard_i] = true;
        }
      }
      if(!que.empty()){
        State s = que.top();
        que.pop();
        action[s._id] = '.';
        if(s._occupy - 1 > 0){
          que.push(State(s._id,s._time,s._occupy-1));
        }
        else{
          is_inque[s._id] = false;
        }
      }

      res += count(action,action+num_of_guards,'-');

      // for debug
      // printf("time:%d\n",time);
      // for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
      //         printf("%c\n",action[guard_i]);
      // }
      // printf("\n");
    }

    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 60
memory_kb: 1232
final_rank: 11

code_snippet: #include<iostream>
#include<vector>
#include<queue>

using namespace std;

int main(){
  for(int g,d;cin>>g>>d,g|d;){
    vector<char> t[100];
    for(int i=0;i<g;i++){
      bool on=true;
      for(int ti;cin>>ti,ti;){
	vector<char> v(ti,on);
	t[i].insert(end(t[i]),begin(v),end(v));
	on=!on;
      }
    }
    bool inque[100]={};
    queue<int> que;
    int a=0;
    int x[100]={};
    for(int i=0;i<d;i++){
      a+=max<int>(0,que.size()-1);
      for(int j=0;j<g;j++){
	if(!inque[j]){
	  x[j]=(x[j]+1)%t[j].size();
	}
      }
      if(!que.empty()){
	int id=que.front();
	x[id]=(x[id]+1)%t[id].size();
	if(t[id][x[id]]){
	  que.pop();
	  inque[id]=false;
	}
      }
      for(int j=0;j<g;j++){
	if(!inque[j]&&!t[j][x[j]]){
	  que.push(j);
	  inque[j]=true;
	}
      }
    }
    cout<<a<<endl;
  }
}
accuracy: 1
time_ms: 200
memory_kb: 2572
final_rank: 19

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;

typedef pair<int,int> ii;

struct Data{
  int id,stamp;
  bool operator < ( const Data& data ) const {
    if( stamp != data.stamp ) return stamp > data.stamp;
    return id > data.id;
  }
};

vector<ii> cycle[110];
int n,m,run_time[110],indices[110],charger,charge_id;
bool inQueue[110];

int simulate(){

  rep(i,n) run_time[i] = cycle[i][0].first, indices[i] = 0, inQueue[i] = false;
  charger = 0, charge_id = -1;
  priority_queue<Data> Q;
  int ret = 0;
  rep(_,m){

    rep(i,n) {
      if( run_time[i] <= 0 && !inQueue[i] ) {
        Q.push((Data){i,_});
        inQueue[i] = true;
      }
      run_time[i]--;
    }

    if( charger == 0 && !Q.empty() ) {
      charge_id = Q.top().id; Q.pop();
      charger = cycle[charge_id][indices[charge_id]].second;
    } 

   if( charger ) {
      --charger;
      if( charger <= 0 ) {
        inQueue[charge_id] = false;
        ( indices[charge_id] += 1 ) %= (int)cycle[charge_id].size();
        run_time[charge_id] = cycle[charge_id][indices[charge_id]].first;
      }
    } 

    ret += (int)Q.size();

  }

  return ret;
}

int main(){
  while( cin >> n >> m, n | m ){
    rep(i,n) {
      int a,b;
      cycle[i].clear();
      while( cin >> a , a ){
        cin >> b;
        cycle[i].push_back(ii(a,b));
      }
    }

    cout << simulate() << endl;

  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1252
final_rank: 3

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;

typedef pair<int,int> ii;

struct Data{
  int id,stamp;
  bool operator < ( const Data& data ) const {
    if( stamp != data.stamp ) return stamp > data.stamp;
    return id > data.id;
  }
};

vector<ii> cycle[110];
int n,m,run_time[110],indices[110],charger,charge_id;
bool inQueue[110];

//char debug[110][1000000];

int simulate(){

  //rep(i,110) rep(j,1000000) debug[i][j] = '?';

  rep(i,n) run_time[i] = cycle[i][0].first, indices[i] = 0, inQueue[i] = false;
  charger = 0, charge_id = -1;
  priority_queue<Data> Q;
  int ret = 0;
  rep(_,m){

    rep(i,n) {
      if( run_time[i] <= 0 && !inQueue[i] ) {
        Q.push((Data){i,_});
        inQueue[i] = true;
      }
      run_time[i]--;
    }

    if( charger == 0 && !Q.empty() ) {
      charge_id = Q.top().id; Q.pop();
      charger = cycle[charge_id][indices[charge_id]].second;
    } 

   if( charger ) {
      --charger;
      if( charger <= 0 ) {
        inQueue[charge_id] = false;
        ( indices[charge_id] += 1 ) %= (int)cycle[charge_id].size();
        run_time[charge_id] = cycle[charge_id][indices[charge_id]].first;
        charge_id = -1;
      }
    } 

   /*
    rep(i,n) if( inQueue[i] ) {
      debug[i][_] = '-';
      if( i == charge_id ) debug[i][_] = '.';
    } else debug[i][_] = '*';
   */

    ret += (int)Q.size();

  }
  /*
  rep(i,n){
    rep(j,m){
      cout << debug[i][j];
    }
    cout << endl;
  }
  */

  return ret;
}

int main(){
  while( cin >> n >> m, n | m ){
    rep(i,n) {
      int a,b;
      cycle[i].clear();
      while( cin >> a , a ){
        cin >> b;
        cycle[i].push_back(ii(a,b));
      }
    }

    cout << simulate() << endl;

  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1252
final_rank: 3

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

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

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

int sch[110][10100];
int inq[110];

int main(){
	int n, t;
	while (cin >> n >> t, n | t){
		MEMSET(sch, 0);
		vector<int> cur(n), len(n);
		rep(i, n){
			int x;
			int y = 0;
			while (cin >> x, x){
				for (int j = len[i]; j < len[i] + x; ++j){
					sch[i][j] = y;
				}
				len[i] += x;
				y ^= 1;
			}
		}
		//rep(i, n){
		//	rep(j, t){
		//		cout << (sch[i][j%len[i]] ? "." : "*");
		//	}
		//	cout << endl;
		//}

		queue<int> q;
		MEMSET(inq, 0);
		int ans = 0;
		rep(i, t){
			rep(j, n){
				if (inq[j]) continue;
				if (sch[j][cur[j] % len[j]]){
					q.push(j);
					inq[j] = 1;
				}
				else{
					++cur[j];
				}
			}
			if (q.empty()) continue;
			ans += q.size() - 1;
			int use = q.front();
			++cur[use];
			if (!sch[use][cur[use] % len[use]]){
				q.pop();
				inq[use] = 0;
			}
		}
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5556
final_rank: 20

code_snippet: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>

class C{
  public:
    int now, cnt, num, stay_num;
    vector<int> v;
    C(){ now = 0; cnt = 0; stay_num = 0; }
};

int main() {
  int n, time;
  while(cin >>n >>time && n){
    vector<C> v(n);
    REP(i, n){
      int tmp;
      while(cin >>tmp && tmp) v[i].v.push_back(tmp);
      v[i].num = i;
    }

    int ans = 0, next = 0, last = 0;
    REP(t, time){
      bool stay_end = false;
      REP(i, n){
        //稼働中
        if(v[i].now % 2 == 0){
          v[i].cnt += 1;
          if(v[i].cnt >= v[i].v[ v[i].now ]){
            v[i].cnt = 0;
            v[i].now = (v[i].now + 1 >= v[i].v.size() ? 0 : v[i].now + 1);
            v[i].stay_num = last;
            last += 1;
          }
        }
        //待ち番号 == next
        else if(v[i].stay_num == next){
          v[i].cnt += 1;
          if(v[i].cnt >= v[i].v[ v[i].now ]){
            v[i].cnt = 0;
            v[i].now = (v[i].now + 1 >= v[i].v.size() ? 0 : v[i].now + 1);
            stay_end = true;
          }
        }
        //待ち番号 > next
        else{
          ans += 1;
        }
      }
      if(stay_end) next += 1;
    }
    cout <<ans <<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1240
final_rank: 8

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};

int main(){
  int N, T;
  while(cin >> N >> T && N){
    vector<int> v[100];
    REP(i, N){
      int t; 
      while(cin >> t && t) v[i].push_back(t);
    }
    vector<int> cur(N);
    vector<int> rest(N);
    REP(i, N) rest[i] = v[i][0];
    int now_use = -1;
    queue<int> que;
    int ans = 0;
    REP(t, T){
      if(now_use == -1 && !que.empty()){
        now_use = que.front();
        que.pop();
      }
      ans += que.size();
      REP(i, N){
        if(now_use == i || cur[i] % 2 == 0){
          rest[i]--;
          if(rest[i] == 0){
            cur[i] = (cur[i] + 1) % v[i].size();
            rest[i] = v[i][cur[i]];
            if(now_use == i) now_use = -1;
            if(cur[i] % 2 == 1){
              que.push(i);
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1240
final_rank: 5

code_snippet: #include<queue>
#include<cstdio>

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

using namespace std;

int main(){
	for(int n,T;scanf("%d%d",&n,&T),n;){
		vector<int> pttn[100];
		rep(i,n){
			while(1){
				int t; scanf("%d",&t);
				if(t==0) break;
				pttn[i].push_back(t);
			}
		}

		int ans=0;
		int idx[100]={},cons[100]={},charge=-1;
		queue<int> wait;
		while(T--){
			if(charge==-1 && !wait.empty()){
				charge=wait.front();
				wait.pop();
			}

			rep(i,n){
				if(idx[i]%2==0 || i==charge){
					cons[i]++;
					if(cons[i]==pttn[i][idx[i]]){ // 充電したくなる or 充電が終わる
						if(idx[i]%2==0) wait.push(i);
						else            charge=-1;
						idx[i]=(idx[i]+1)%pttn[i].size();
						cons[i]=0;
					}
				}
				else{ // 充電器が空くのを待つ
					ans++;
				}
			}
		}

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

	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1076
final_rank: 9

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

const int MAXN = 101;
const int MAXM = 10080;
int N, M;
vector<int> T[MAXN];

int main() {
  while(cin >> N >> M && (N|M)) {
    for(int i = 0; i < MAXN; ++i) T[i].clear();
    for(int i = 0; i < N; ++i) {
      int a;
      while(cin >> a && a) {
	T[i].push_back(a);
      }
    }
    queue<int> que;
    int ju_den = -1;
    int p[MAXN];
    int t[MAXN];
    fill(p,p+MAXN, 0);
    for(int j = 0; j < N; ++j) t[j] = T[j][0];
    int ans = 0;
    for(int i = 0; i < M; ++i) {
      for(int j = 0; j < N; ++j) {
	if(p[j]%2 == 0) {
	  --t[j];
	  if(t[j] == 0) {
	    p[j] = (p[j]+1)%T[j].size();
	    t[j] = T[j][p[j]];
	    que.push(j);
	  }
	} else {
	  if(ju_den == j) {
	    --t[j];
	    if(t[j] == 0) {
	      p[j] = (p[j]+1)%T[j].size();
	      t[j] = T[j][p[j]];
	      ju_den = -1;
	    }
	  } else {
	    ++ans;
	  }
	}
      }
      if(que.size() && ju_den == -1) {
	ju_den = que.front();
	que.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1248
final_rank: 6

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

const int N = 100;
int n, m;
vector<int> data[N];
 
int main(){
  while(cin >> n >> m && (n|m)){
    for(int i=0;i<n;i++){
      data[i].clear();
      while(1){
        int in;
        cin >> in;
        if(in == 0) break;
        data[i].push_back(in);
      }
    }
    int ans = 0, rem[N], idx[N];
    priority_queue<pair<int, int> > q;
    bool flag[N];
    for(int i=0;i<n;i++) rem[i] = data[i][0];
    fill(idx, idx+n, 0);
    fill(flag, flag+n, false);
    for(int k=0;k<m;k++){
      pair<int, int> tmp = make_pair(1, 1);
      if(q.size()){ tmp = q.top(); q.pop(); }
      for(int i=0;i<n;i++){
        if(!flag[i]){
          rem[i]--;
          if(rem[i] == 0){
            idx[i] = (idx[i] + 1) % data[i].size();
            rem[i] = data[i][idx[i]];
            flag[i] = true;
            q.push(make_pair(-k, -i));
          }
        }else{
          if(-tmp.second == i){
            rem[i]--;
            if(rem[i] == 0){
              idx[i] = (idx[i] + 1) % data[i].size();
              rem[i] = data[i][idx[i]];
              flag[i] = false;
            }else q.push(tmp);
          }else ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1248
final_rank: 6