Problem p00147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {

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

  int data[100];
  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 (j == 16) {
            if (field[j] == -1) {
              mergen++;
            }
          }

          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) {
              // ·íç¹é
              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;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {

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

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

  return 0;
}