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 <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::tuple<int,int,int> T;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N;
int times[100];
int chairs[17];
std::priority_queue<T,std::vector<T>,std::greater<T>> queue1, queue2; // queue1: 入っている人, queue2: 待っている人

// (1) 入れる
// (2) 次の人が入るように追い出す

int position(const T& tuple){
    for(int i=0;i+std::get<1>(tuple)<=17;i++){
        bool can = true;
        REP(j, std::get<1>(tuple)){
            if(chairs[i+j] != -1){can = false; break;}
        }
        if(can){return i;}
    }
    return -1;
}

void enter(const T& tuple, const int& pos, const int& t){
    int index = std::get<2>(tuple);
    REP(i, std::get<1>(tuple)){
        chairs[pos+i] = index;
    }
    queue1.push(std::make_tuple(t+17*(index%2)+3*(index%3)+19, std::get<1>(tuple), index));
}

void leave(const T& tuple){
    REP(i, 17){
        if(chairs[i] == std::get<2>(tuple)){chairs[i] = -1;}
    }
}

void simulate(){
    std::fill(chairs, chairs+17, -1);

    REP(i, 100){
        queue2.push(std::make_tuple(5*i, i%5==1?5:2, i));
    }

    int t = 0;
    while(!queue2.empty()){
        while(!queue1.empty()){
            T tuple = queue1.top();
            if(std::get<0>(tuple) == t){
                // printf("Exit %d %d\n", std::get<2>(tuple), t);
                queue1.pop();
                leave(tuple);
            }else{break;}
        }

        while(!queue2.empty()){
            T tuple = queue2.top();
        
            int pos = position(tuple);
            if(pos != -1 && std::get<0>(tuple) <= t){
                // printf("Enter %d %d\n", std::get<2>(tuple), t);
                queue2.pop();
                enter(tuple, pos, t);
                times[std::get<2>(tuple)] = t - std::get<0>(tuple);
            }else{
                break;
            }
        }

        t += 1;
    }
}

int main(){
    simulate();
    
    while(std::cin >> N, !std::cin.eof()){
        std::cout << times[N] << std::endl;
    }
}

C++: #include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

int main()
{
	int n;
	int ans[100], intime[100];
	int chair[17];
	
	queue<int> q;
	memset(chair, -1, sizeof(chair));
	
	int time = 0;
	while (1){
		if (time % 5 == 0) q.push(time / 5);
		
		for (int i = 0; i < 17; i++){
			int y = chair[i];
			if (y == -1) continue;
			if (time - intime[y] == 17 * (y % 2) + 3 * (y % 3) + 19)
				chair[i] = -1;
		}
		
		rep:;
		if (q.size() > 0){
			int x = q.front();
			int num;
			num = 2 + 3 * (x % 5 == 1);
			
			for (int i = 0; i <= 17 - num; i++){
				bool ok = true;
				for (int j = i; j < i + num; j++)
					if (chair[j] != -1) ok = false;
				if (ok){
					intime[x] = time; ans[x] = time - x * 5;
					if (x == 99) goto end;
					q.pop();
					for (int j = i; j < i + num; j++)
						chair[j] = x;
					goto rep;
				}
			}
		}
		time++;
	}
	end:;
	while (~scanf("%d", &n)) printf("%d\n", ans[n]);
	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;
}
 
int 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();
  return a;
}
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,flg2;
  while(!(ts==te)){
 
    /*
    for(int i=0;i<17;i++)printf("%3d ",state[i]);
    cout<<endl;
 
    for(int i=us;i<ue;i++)printf("%3d ",u[i].second.second);
    cout<<endl;
    */
     
    sort(t+ts,t+te);
    y=x;
    x=t_out();
 
    //cout<<x.first<<' '<<x.second.first<<' '<<x.second.second<<endl;
    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;
      if(ts!=te&&t[ts].first==x.first&&t[ts].second.first==0)flg2=0;
      else flg2=1;
      while(flg2){
    if(us==ue)break;
    flg=u_check();
    if(flg==0)break;
    z=u_ins();
     
    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
  }
 
   
  //cout<<te<<endl;
  while(cin>>n){
    cout<<ans[n]<<endl;
  }
  return 0;
}