Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 0x7fffffff
func main() {
	S := 17
	seat := make([][]int, 27)
	for i :=range seat {
		seat[i] = make([]int, 27)
	}
	ans := make([]int, 105)
	id := 0
	Q := make([][]int, 0)
	for i := 0; i < S; i++ {
		seat[i][0] = -1
	}
	t := -1
	for ans[99] == 0 {
		t++
		if  t%5 == 0 && id <= 99 {
			if id % 5 == 1 {
				n := 5
				tmp := []int{id, n, t}
				Q = append(Q, tmp)
				id++
			} else {
				n := 2
				tmp := []int{id, n, t}
				Q = append(Q, tmp)
				id++
			}
		}
		for i := 0; i < 5; i++ {
			if seat[i][1] == 0 {
				continue
			}
			seat[i][1]--
			if seat[i][1] == 0 {
				seat[i][0] = -1
			}
		}
		f := len(Q)
		for f > 0 {
			i, n := Q[0][0], Q[0][1]
			f = 0
			for j := 0; j < S; j++ {
				g := 0
				for k := 0; k < n; k++ {
					if seat[j+k][0] >= 0 {
						g = 1
						break
					}
				}
				if g != 0 {
					continue
				}
				ans[i] = t-Q[0][2]
				Q = Q[1:]
				f = len(Q)
				for k := 0; k < n; k++ {
					seat[j+k][0] = i
					seat[j+k][1] = 17*(i&1) + 3*(i%3) + 19
				}
				break
			}
		}
	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		fmt.Println(ans[n])
	}
}

C++: #include<iostream>
using namespace std;
#define SHEETS 17
#define GROUPS 130

int main()
{
	int ans[GROUPS] = {0,};
	int now;
	int sheets[SHEETS];
	int peoples[GROUPS] = {0,};
	int arrive[GROUPS] = {0,};
	int eating[GROUPS] = {0,};

	for(int i = 0; i < SHEETS; ++i){
		sheets[i] = -1;
	}
	for(int i = 0; i < GROUPS; ++i)
	{
		arrive[i] = i*5;
		peoples[i] = i%5==1?5:2;
		eating[i] = 17*(i%2)+3*(i%3)+19;
	}
	now = 0;
	for(int t = 0; t < 800; ++t){

		for(int i = 0; i < SHEETS; ++i){
			if( sheets[i] >= 0 ){
				--eating[sheets[i]];
				if( eating[sheets[i]] == 0 ){
					for(;sheets[i]==sheets[i+1]&&i<SHEETS-1;++i)
						sheets[i] = -1;
					sheets[i]=-1;
				}else{
					for(;sheets[i]==sheets[i+1]&&i<SHEETS-1;++i)
						0;
				}
			}
		}

		while( true ){
			bool bCont = false;
			for(int i = 0; i < SHEETS; ++i){
				if( sheets[i] == -1 ){
					int cont = 0;
					for(int j = i; j < SHEETS; ++j){
						if( sheets[j] == -1 ) cont++;
						else break;
					}
					if( now < GROUPS && cont >= peoples[ now ] && t >= arrive[ now ] ){
						for(int j = i; j < i+peoples[now]; ++j)
							sheets[j] = now;
						
						/*
						cout << "NOW: "<<now;
						cout << " ANS: " << t << "-" << arrive[now] << "=" << ans[now] << '\n';
						cout << "t=" << t <<":\t";
						for(int i = 0; i < SHEETS; ++i){
							cout << sheets[i] << ' ';
						}
						cout << endl;
						*/
						
						ans[now] = t - arrive[now];//<0?0:waiting[now];
						++now;
						bCont = true;
					}
				}
			}
			if( !bCont ) break;
		}
	}

	/*
	for(int i = 0; i < GROUPS;++i){
		cout << ans[i] << ' ';
	}
	*/

	while(true){
		int n;
		cin >> n;
		if(cin.eof())
			break;
		cout << ans[n] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<int, P > PP;
int n,ans[100];
PP t[500];
PP u[500];
int ts,te,us,ue;
int state[17];

void t_in(PP x){
  t[te]=x;
  te++;
}
void u_in(PP x){
  u[ue]=x;
  ue++;
}
PP t_out(){
  ts++;
  return t[ts-1];
}
PP u_out(){
  us++;
  return u[us-1];
}


int u_check(){
  int a = u[us].second.second;
  int b = (a%5==1?5:2);
  int cnt=0;
  for(int i=0;i<17;i++){
    if(state[i]==-1){
      cnt++;
      if(cnt==b){
	return 1;
      }
    }else{
      cnt=0;
    }
  }
  return 0;
}

void u_ins(){
  int a = u[us].second.second;
  int b = (a%5==1?5:2);
  int cnt=0;
  for(int i=0;i<17;i++){
    if(state[i]==-1){
      cnt++;
      if(cnt==b){
	for(int j=i;j>i-b;j--){
	  state[j]=a;
	}
	break;
      }
    }else{
      cnt=0;
    }
  }
  
  
  u_out();
}
int u_check2(PP x){
  int a = x.second.second;
  int b = (a%5==1?5:2);
  int cnt=0;
  for(int i=0;i<17;i++){
    if(state[i]==-1){
      cnt++;
      if(cnt==b){
	return 1;
      }
    }else{
      cnt=0;
    }
  }
  return 0;
}
void u_ins2(PP x){
  int a = x.second.second;
  int b = (a%5==1?5:2);
  int cnt=0;
  for(int i=0;i<17;i++){
    if(state[i]==-1){
      cnt++;
      if(cnt==b){
	for(int j=i;j>i-b;j--){
	  state[j]=a;
	}
	break;
      }
    }else{
      cnt=0;
    }
  }
}

int main(){
  ts=us=te=ue=0;

  for(int i=0;i<100;i++)ans[i]=0;
  for(int i=0;i<17;i++)state[i]=-1;

  for(int i=0;i<100;i++){
    t_in(PP(5*i,P(1,i)));
  }

  sort(t+ts,t+te);

  PP x,y;
  int flg,z;
  while(!(ts==te)){
    /*
    cout<<endl;
    for(int i=0;i<17;i++)printf("%3d ",state[i]);
    cout<<"u"<<endl;
    for(int i=us;i<ue;i++)printf("%3d ",u[i].second.second);
    cout<<endl;
    cout<<endl;
    */
    sort(t+ts,t+te);
    y=x;
    x=t_out();

    
    for(int i=us;i<ue;i++)ans[u[i].second.second]+=(x.first-y.first);

    if(x.second.first==0){
      for(int i=0;i<17;i++)if(state[i]==x.second.second)state[i]=-1;
      while(1){
	if(us==ue)break;
	flg=u_check();
	if(flg==0)break;
	u_ins();
	z=x.second.second;
	t_in(PP(x.first+(17*(z%2)+3*(z%3)+19),P(0,z)));
      }
            
    }else if(x.second.first==1){
      flg=u_check2(x);

      if(flg==0||us!=ue){
	u_in(x);
      }else{
	u_ins2(x);

	z=x.second.second;
	t_in(PP(x.first+(17*(z%2)+3*(z%3)+19),P(0,z)));
      }
    }//if
  }

  
  
  while(cin>>n){
    for(int i=n;i<=n;i++)cout<<ans[i]<<' ';
    cout<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>

using namespace std;

int main(){

	int n;
	map<int,vector<int> > escape;

	int data[100];
	int field[17];
	fill(field,field+17,-1);

	queue<int> waitQueue;
	int group=0;
	int time=0;
	
	vector<int> c;
	while(cin>>n){
		c.push_back(n);
	}
	
	for(int i=0;;i++){
		time=i;
		if(i%5==0){
			waitQueue.push(group);
			group++;
		}
		// Ü¸ÔØêO[vðÈ©çÇ©·
		for(map<int,vector<int> >::iterator it = escape.begin(); it != escape.end(); it++){
			if(it->first==i){
				for(int j = 0; j < 17; j++){
					for(int k = 0; k< it->second.size(); k++){
						if(field[j]==it->second[k]){
							field[j]=-1;
							break;
						}
					}
				}
				it->second.clear();
				break;
			}
		}
		
		bool f=false;
		int prvCand=-1;
		// ·íêé¾¯·íç¹é
		while(1){
			if(waitQueue.size()==0)
				break;
			// ¡ñÀç¹él
			int cand=waitQueue.front();
			if(prvCand==cand)
				break;
			int s;
			int mergen=-1;
			for(int j = 0; j < 17; j++){
				if(field[j]==-1&&j!=16){
					if(mergen==-1){
						s=j;
						mergen=1;
					}
					else{
						mergen++;
					}
				}
				else{
					if(j==16){
						if(field[j]==-1){
							mergen++;
						}
					}

					if(cand%5==1){
						if(mergen>=5){
							// ·íç¹é
							for(int k = s; k < 5+s; k++){
								field[k]=cand;
							}
							//								cout<<cand<<endl;
							
							data[cand]=time-cand*5;
							waitQueue.pop();
							escape[17*(cand%2)+3*(cand%3)+19+i].push_back(cand);
							if(cand==99){
								f=true;
								break;
							}
							break;
						}
						else{
							mergen=-1;
						}
					}
					else{
						if(mergen>=2){
							// ·íç¹é
							for(int k = s; k < 2+s; k++){
								field[k]=cand;
							}
							data[cand]=time-cand*5;
							waitQueue.pop();
							escape[17*(cand%2)+3*(cand%3)+19+i].push_back(cand);
							if(cand==99){
								f=true;
								break;
							}
							break;
						}
						else
							mergen=-1;
					}
				}
				if(f)
					break;
			}
			if(f)
				break;
			prvCand=cand;
		}
		if(f)
			break;
		
	}
	for(int i = 0; i < c.size(); i++){
		// ±±ÅÜ¿Ôð\¦
		cout<<data[c[i]]<<endl;
	}
	//	}
	
	return 0;
}