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>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int Time[10] = {  0,  5, 10, 15, 20, 25, 30, 35, 40, 45};
int peop[10] = {  2,  5,  2,  2,  2,  2,  5,  2,  2,  2};
int eats[10] = { 19, 39, 25, 36, 22, 42, 19, 39, 25, 36};
int star[10];
int tabl[17];
int ans[10];

int main(){
  int next = 0;
  REP(i,17) tabl[i] = -1;
  for(int i=0; ; i++){
    REP(j,17) if(tabl[j] != -1){
      int t = tabl[j];
      if(star[t] + eats[t] == i){
        tabl[j] = -1;
        if(t == 9) goto end;
      }
    }

  loop:
    if(Time[next] <= i){
      int cnt = 0;
      int st  = -1;
      REP(j,17){
        if(tabl[j] == -1){
          if(cnt == 0) st = j;
          cnt++;
          if(cnt == peop[next])
            break;
        }else cnt = 0;
      }
      if(cnt == peop[next]){
        ans[next] = i - Time[next];
        star[next] = i;
        REP(j,peop[next])
          tabl[st + j] = next;
        next++;
        goto loop;
      }
    }

    /*
    printf("%d: ",i);
    REP(j,17){printf("%3d ",tabl[j]);} puts("");
    getchar();
    */
  }

 end:
  int n;
  while(~scanf("%d",&n))
    print(ans[n]);

  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, e.t + 17*(idx%2) + 3*(idx%3) + 19, idx});
                q.pop();
            }
        }
        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;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

multimap<int,int> ckout;
int ckin[100],seat[17];

void rm(multimap<int,int>::iterator p){
	rep(i,17)if(seat[i]==p->second)seat[i]=-1;
	ckout.erase(p);
}
int findseat(int k){
	int l=k%5==1?5:2,len=0,p,pos;
	for(p=0;p<17;p++){
		if(seat[p]==-1){
			if(!len)pos=p;
			if(++len==l)return pos;
		}else len=0;
	}
	return -1;
}
int main(){
	fill(ckin,ckin+100,-1); fill(seat,seat+17,-1);
	int n,k,t,q;
	multimap<int,int>::iterator p;
	for(k=t=0;k<100||ckout.empty();t++){
		while((p=ckout.find(t))!=ckout.end())rm(p);
		while(ckin[k]==-1&&t>=5*k){
			if((q=findseat(k))!=-1){
				int l=k%5==1?5:2;
				ckin[k]=t;
				rep(i,l)seat[q+i]=k;
				ckout.insert(make_pair(t+17*(k%2)+3*(k%3)+19,k));
				k++;
			}else break;
		}
	}
	while(cin>>n)cout<<ckin[n]-n*5<<endl;
	return 0;
}