Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

type chair struct {
	group_id, rest_time int
}

type info struct {
	group_id, num, wait_sum, before_go int
	fixed                              bool
}

func main() {
	ans := make([]int, 100)
	chair := make([]chair, 17)
	for i := 0; i < 17; i++ {
		chair[i].group_id = -1
	}
	info := make([]info, 100)
	for i := 0; i <= 99; i++ {
		info[i].group_id = i
		info[i].before_go = 5 * i
		info[i].wait_sum = 0
		if i%5 == 1 {
			info[i].num = 5
		} else {
			info[i].num = 2
		}
		info[i].fixed = false
	}
	count := 0
	FLG := 0
	tmp_num := 0
	isOK := false
	waitFLG := false
	for {
		for i := 0; i < 17; i++ {
			if chair[i].group_id != -1 {
				chair[i].rest_time--
				if chair[i].rest_time <= 0 {
					chair[i].group_id = -1
				}
			}
		}
		waitFLG = false
		count = 0
		for i := 0; i < 100; i++ {
			if info[i].fixed {
				continue
			}
			if waitFLG == false && info[i].before_go == 0 {
				FLG = -1
				tmp_num = info[i].num
				for k := 0; k <= 17-tmp_num; k++ {
					isOK = true
					for p := 0; p < tmp_num; p++ {
						if chair[k+p].group_id != -1 {
							isOK = false
							break
						}
					}
					if isOK {
						FLG = k
						break
					}
				}
				if FLG != -1 {
					for k := FLG; k < FLG+tmp_num; k++ {
						chair[k].group_id = info[i].group_id
						chair[k].rest_time = 17*(i%2) + 3*(i%3) + 19
						info[i].fixed = true
					}
				} else {
					waitFLG = true
					info[i].wait_sum++
					count++
				}
			} else {
				if info[i].before_go == 0 {
					info[i].wait_sum++
				} else {
					info[i].before_go--
				}
				count++
			}
		}
		if count == 0 {
			break
		}
	}
	for i := 0; i <= 99; i++ {
		ans[i] = info[i].wait_sum
	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		fmt.Println(ans[n])
	}
}

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

using namespace std;

int main(){

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

	int data[100];
	//while(cin>>n){
		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(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){
							//	cout<<cand<<endl;
								// ·íç¹é
								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;
}

C++: //Name: Fukushimaken
//Level: 2
//Category: シミュレーション
//Note: 

/**
 * 問題文の通りにシミュレーションする。
 * グループの到着と、食事の終了イベントをpriority_queueに突っ込んで取り出すようにする。
 * 到着したグループは別途queueに突っ込み、席に空きができたら埋めていくようにする。
 *
 * 入力が小さいので、特に効率のよいデータ構造を使わなくても間に合う。
 * オーダーは O(NW log N)。
 * ただしNはグループ数で、N=100。Wは座席の幅で、W=17。
 */
#include <iostream>
#include <queue>

using namespace std;

struct Event {
    enum {
        ARRIVAL,
        FINISH
    } type;
    int t;
    int idx;

    bool operator <(const Event &e) const {
        return t > e.t;
    }
};

int gsize(int n) {
    return (n % 5 == 1 ? 5 : 2);
}

bool solve(bool first) {
    vector<int> res(100, -1);
    priority_queue<Event> eventq;
    for(int i = 0; i < 100; ++i) {
        eventq.push(Event{Event::ARRIVAL, i*5, i});
    }
    eventq.push(Event{Event::FINISH, 500, -1}); // Sentinel
    queue<int> q;
    vector<int> seats(17, -1);
    int lastt = -1;
    while(!eventq.empty()) {
        const Event e = eventq.top();
        eventq.pop();
        if(e.type == Event::ARRIVAL) {
            q.push(e.idx);
        } else {
            for(int i = 0; i < 17; ++i) {
                if(seats[i] == e.idx) seats[i] = -1;
            }
        }
        if(e.t != lastt) {
            while(!q.empty()) {
                const int idx = q.front();
                const int s = gsize(idx);
                int acc = 0;
                int pos = -1;
                for(int i = 0; i < 17; ++i) {
                    if(seats[i] == -1) {
                        ++acc;
                    } else {
                        acc = 0;
                    }
                    if(acc == s) {
                        pos = i - s + 1;
                        break;
                    }
                }
                if(pos == -1) break;
                for(int i = 0; i < s; ++i) {
                    seats[pos+i] = idx;
                }
                res[idx] = e.t - idx*5;
                eventq.push(Event{Event::FINISH, lastt + 17*(idx%2) + 3*(idx%3) + 19, idx});
                q.pop();
                /*
                for(int i = 0; i < 17; ++i) {
                    if(seats[i] == -1) cout << '-';
                    else cout << seats[i]%10;
                }
                cout << endl;
                */
            }
        }
        lastt = e.t;
    }

    int N;
    while(cin >> N) {
        cout << res[N] << endl;
    }
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);

    bool first = true;
    while(solve(first)) {
        first = false;
    }
    return 0;
}