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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  vector<int> a[1000];
  int seat[17], in[100], out[100];
  queue<int> Q;
  fill_n((int*)seat, 17, -1);
  for (int i = 0; i < 1000; i++) {
    if (i < 500 && i % 5 == 0) {
      Q.emplace(i / 5);
      in[i / 5] = i;
    }
    for (int j : a[i]) {
      for (int k = 0; k < 17; k++) {
        if (seat[k] == j) seat[k] = -1;
      }
    }
    while (!Q.empty()) {
      int num = Q.front() % 5 == 1 ? 5 : 2;
      int l = 0, r = 0, flag = 0;
      while (l < 17) {
        while (l < 17 && seat[l] != -1) l++, r++;
        while (r < 17 && seat[r] == -1) r++;
        if (r - l >= num) {
          int group = Q.front();
          Q.pop();
          out[group] = i;
          a[out[group] + 17 * (group % 2) + 3 * (group % 3) + 19].emplace_back(group);
          for (int j = 0; j < num; j++) {
            seat[j + l] = group;
          }
          flag = 1;
          break;
        }
        l = r;
      }
      if (!flag) break;
    }
  }
  int n;
  while (cin >> n) {
    cout << out[n] - in[n] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> P;

int main() {
	int seki[17] = {0};
	int mati[100];
	queue<P> q;
	bool hantei2 = false;
	for (int i = 0; ; i++) {
		if (i % 5 == 0) {
			if (i/5 >= 100) {
				hantei2 = true;
			} else if ((i/5) % 5 == 1) {
				q.push(P(5, i/5));
			} else {
				q.push(P(2, i/5));
			}
		}
		for (int j = 0; j < 17; j++) {
			if (seki[j] == i) {
				seki[j] = 0;
			}
		}
		while (!q.empty()) {
			P p = q.front();
			bool hantei = false;
			for (int j = 0; j < 17; j++) {
				if (j+p.first-1 >= 17) {
					break;
				}
				int counter = 0;
				for (int k = 0; k < p.first; k++) {
					counter += seki[j+k];
				}
				if (counter == 0) {
					for (int k = 0; k < p.first; k++) {
						seki[j+k] = i+17*(p.second%2)+3*(p.second%3)+19;
					}
					mati[p.second] = i-(p.second*5);
					hantei = true;
					break;
				}
			}
			if (hantei) {
				q.pop();
			} else {
				break;
			}
		}
		if (hantei2 && q.empty()) {
			int counter = 0;
			for (int j = 0; j < 17; j++) {
				counter += seki[j];
			}
			if (counter == 0) {
				break;
			}
		}
	}
	int n;
	while (cin >> n) {
		cout << mati[n] << endl;
	}
	return 0;
}

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;

	while(cin>>n){
		char field[17];
		fill(field,field+17,0);

		queue<int> waitQueue;
		int group=0;
		int time=0;
		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]+'0'){
								field[j]=0;
								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]==0&&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+'0';
								}
								waitQueue.pop();
								escape[17*(cand%2)+3*(cand%3)+19+i].push_back(cand);
								if(cand==n){
									f=true;
									break;
								}
								break;
							}
							else{
								mergen=-1;
							}
						}
						else{
							if(mergen>=2){
								// ·íç¹é
								for(int k = s; k < 2+s; k++){
									field[k]=cand+'0';
								}
								waitQueue.pop();
								escape[17*(cand%2)+3*(cand%3)+19+i].push_back(cand);
								if(cand==n){
									f=true;
									break;
								}
								break;
							}
							else
								mergen=-1;
						}
					}
					if(f)
						break;
				}
				if(f)
					break;
				prvCand=cand;
			}
			//if(i==45){
			//	cout<<endl;
			//}
			if(f)
				break;
		}
		// ±±ÅÜ¿Ôð\¦
		cout<<time-n*5<<endl;
	}

	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define N 100
typedef long long ll;
typedef pair<int,int>P;
typedef struct D{int t,n,e;bool operator <(const D &a)const{ return t>a.t;}};
priority_queue<D>F;
int r[N],s[20],w=0;

int ch(int n)
{
	for(int i=0;i<17-n;i++)
		if(s[i]==-1)
		{
			int j;
			for(j=0;j<n;j++)if(s[i+j]!=-1)break;
			if(j==n)return i;
		}
	return -1;
}

int main()
{
	memset(s,-1,sizeof(s));
	for(int i=0;i<N;i++)
	{
		D tmp={i*5,i,0};
		F.push(tmp);
	}
	while(!F.empty())
	{
		D n=F.top();F.pop();
		if(n.e)
		{
			for(int i=0;i<17;i++)
				if(s[i]==n.n)s[i]=-1;
		}
		else
		{
			if(w<n.n)
			{
				n.t++;
				F.push(n);
				continue;
			}
			int a=n.n%5==1?5:2,b=ch(a);
			if(b!=-1)
			{
				for(int j=0;j<a;j++)
					s[b+j]=n.n;
				r[n.n]=n.t-5*n.n;
				w=n.n+1;
				D tmp={n.t+19+17*(n.n%2)+3*(n.n%3),n.n,1};
				F.push(tmp);
			}
			else
			{
				n.t++;
				F.push(n);
			}
		}
	}
	for(;~scanf("%d",&w);)printf("%d\n",r[w]);
	return 0;
}