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

#include <iostream>
using namespace std;
#define SHEETS 17
#define GROUPS 130

int main() {
  int ans[GROUPS] = {
      0,
  };
  int now;
  int sheets[SHEETS];
  int peoples[GROUPS] = {
      0,
  };
  int arrive[GROUPS] = {
      0,
  };
  int eating[GROUPS] = {
      0,
  };

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

    for (int i = 0; i < SHEETS; ++i) {
      if (sheets[i] >= 0) {
        --eating[sheets[i]];
        if (eating[sheets[i]] == 0) {
          for (; sheets[i] == sheets[i + 1] && i < SHEETS - 1; ++i)
            sheets[i] = -1;
          sheets[i] = -1;
          /*
          cout << "t=" << t <<":\\t";
          for(int i = 0; i < SHEETS; ++i){
                  cout << sheets[i] << ' ';
          }
          cout << endl;
          */

        } else {
          for (; sheets[i] == sheets[i + 1] && i < SHEETS - 1; ++i)
            0;
        }
      }
    }

    while (true) {
      bool bCont = false;
      for (int i = 0; i < SHEETS; ++i) {
        if (sheets[i] == -1) {
          int cont = 0;
          for (int j = i; j < SHEETS; ++j) {
            if (sheets[j] == -1)
              cont++;
            else
              break;
          }
          if (now < GROUPS && cont >= peoples[now] && t >= arrive[now]) {
            for (int j = i; j < i + peoples[now]; ++j)
              sheets[j] = now;

            /*
            cout << "t=" << t <<":\\t";
            for(int k = 0; k < SHEETS; ++k){
                    cout << sheets[k] << ' ';
            }
            cout << endl;
            */

            ans[now] = t - arrive[now]; //<0?0:waiting[now];
            ++now;
            bCont = true;
            break;
          }
        }
      }
      if (!bCont)
        break;
    }
  }

  /*
  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;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define SHEETS 17
#define GROUPS 100
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;
  }
  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 = 1;
  sheets[0] = sheets[1] = 0;
  for (int t = 0; t < 2000; ++t) {

    for (int i = 0; i < GROUPS; ++i) {
      ++waiting[i];
    }

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

    /*
    cout << "t="<<t<<":\\t\\t";
    for(int i = 0; i < SHEETS; ++i){
            cout << (char)(sheets[i]==-1?'_':sheets[i]+'0');
    }
    cout << '\\n';
    */

    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] - 1 < 0 ? 0 : waiting[now] - 1;
            ++now;
            bCont = true;
          }
        }
      }
      if (!bCont)
        break;
    }
  }

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << ans[n] << endl;
  }

  return 0;
}