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 <queue>
#include <vector>
#define mp make_pair
using namespace std;

struct P{ int tim,group,act; };

struct comp {
	bool operator()(P t1, P t2) const {
		if(t1.tim != t2.tim) return t1.tim > t2.tim;
		else return t1.act < t2.act;
 	}
};

int chair[17];

int emp(int num){
	for(int i = 0;i < 17 - num + 1;i++){
		bool flag = true;
		for(int j = 0;j < num;j++){
			if(chair[i + j] != -1) flag = false;
		}
		if(flag) return i;
	}
	return -1;
}

int main(){
	int n,front = -1,wait[100] = {};
	priority_queue<P,vector<P>,comp> que;
	queue<int> line;
	for(int i = 0;i < 17;i++) chair[i] = -1;
	for(int i = 0;i < 100;i++) que.push({i * 5,i,0});
	while(!que.empty()){
		P p = que.top();que.pop();
		int t = p.tim,group = p.group,act = p.act;
		if(act == 0){
			int num = 2,pos;
			if(group % 5 == 1) num = 5;
			pos = emp(num);
			if(!line.empty() || pos == -1) line.push(group);
			else{
				for(int i = 0;i < num;i++) chair[i + pos] = group;
				que.push({t + 17 * (group % 2) + 3 * (group % 3) + 19,group,1});
			}
		}else{
			for(int i = 0;i < 17;i++){
				if(chair[i] == group) chair[i] = -1;
			}
			while(!line.empty()){
				int g = line.front(),num = 2,pos;
				if(g % 5 == 1) num = 5;
				pos = emp(num);
				if(pos != -1){
					line.pop();
					wait[g] = t - g * 5;
					for(int i = 0;i < num;i++) chair[pos + i] = g;
					que.push({t + 17 * (g % 2) + 3 * (g % 3) + 19,g,1});
				}
				else break;
			}
		}
	}
	while(cin >> n){
		cout << wait[n] << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#define SHEETS 17
#define GROUPS 130
int main()
{
	int ans[GROUPS] = {0,};
	int now;
	int sheets[SHEETS+1] = {-1,};
	int peoples[GROUPS] = {0,};
	int waiting[GROUPS] = {0,};
	int eating[GROUPS] = {0,};

	for(int i = 0; i < SHEETS+1; ++i){
		sheets[i] = -1;
	}
	sheets[SHEETS] = -2;
	for(int i = 0; i < GROUPS; ++i)
	{
		waiting[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){

		/*
		if( t % 5 == 0 ){
			cout << t / 5 << "ツつウツづアツつェツδ債グツイツδ督つオツづ慊つオツつスツ。\n";
		}
		*/

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

		while( true ){
			bool bCont = false;
			for(int i = 0; i < SHEETS; ++i){
				int cont = 0;
				if( sheets[i] < 0 ){
					for(int j = i; j < SHEETS; ++j){
						if( sheets[j] < 0 ) cont++;
						else break;
					}
					if( cont >= peoples[ now ] && waiting[ now ] >= 0 ){
						for(int j = i; j < i+peoples[now]; ++j)
							sheets[j] = now;
						ans[now] = waiting[now];//<0?0:waiting[now];
						++now;
						bCont = true;
					}
				}
			}
			if( !bCont )
				break;
		}
		for(int i = 0; i < GROUPS; ++i){
			++waiting[i];
		}
		
		/*for(int i = 0; i < SHEETS; ++i){
			cout << sheets[i] << ' ';
		}
		cout << endl;*/
	}
	/*
	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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999


int data[110];

queue<pii> ar(){
    queue<pii> ret;
    rep(i,100){
        ret.push(pii(i,i*5));
    }
    return ret;
}


queue<pii> de(){
    vector<pii> tmp;
    queue<pii> ret;
    rep(i,100){
        tmp.pb(pii(i*5+17*(i%2)+3*(i%3)+19,i));
    }
    sort(all(tmp));
    rep(i,100){
        ret.push(pii(tmp[i].second,tmp[i].first));
    }
    return ret;
}



int arr(pii f,int seat[20],queue<pii> &wait,int t){
    int members;
    if( (f.first)%5==1 )members = 5;
    else members = 2;
    
    int dp[20]={};
    for(int i=16;i>=0;i--){
        if(seat[i]==-1) dp[i]=0;
        else            dp[i] = dp[i+1]+1;
    }
    bool flag=false;
    rep(i,17){
        if(dp[i]>=members){
            for(int j=i;j<i+members;i++){
                seat[j] = f.first;
            }
            flag=true;
        }
    }
    if(!flag){
        wait.push( f );
        return -1;
    }
    data[f.first] = t;
    return 0;
}

void pre(){
    int seat[20];
    rep(i,17)seat[i]=-1;
    queue<pii> arrive = ar();
    queue<pii> dep = de();
    bool flag=true;
    int now=0;
    while(arrive.size()+dep.size()){
        if( flag==false || arrive.empty() || arrive.front().second>dep.front().second  ){ //??¢?????????
            int dep_group = dep.front().first;
            now =dep.front().second;
            dep.pop();
            rep(i,17){
                if( seat[i]==dep_group ) seat[i]=-1;
            }
            flag = true;
        }
        else{ //??¢?????\???
            pii f = arrive.front();
            int members;
            if( (f.first)%5==1 )members = 5;
            else members = 2;
            
            int dp[20]={};
            for(int i=16;i>=0;i--){
                if(seat[i]!=-1) dp[i] = 0;
                else            dp[i] = dp[i+1]+1;
            }
            
            flag = false;
            rep(i,17){
                if(dp[i]>=members){
                    for(int j=i;j<i+members;j++){
                        seat[j] = f.first;
                    }
                    flag=true;
                    arrive.pop();
                    data[f.first] = now;
                    break;
                }
            }
        }
    }
}


int main(){
    int n;
    
    pre();
    
    while(cin>>n){
        cout<<max( 0 , data[n]-5*n )<<endl;
    }
}