Problem p03394 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;
// ofstream fout("test.in");

const int maxn = 30000;
int p[10] = {2, 3, 5};
bool mark[maxn];
int cnt = 0, mod, N;
vector<int> v[30];
int sum[30];
void solve() {
  if (N <= 12) {
    if (N == 3)
      cout << "2 5 63" << endl;
    else {
      cout << 2 << " " << 3 << " " << 4 << " " << 9;
      if (N >= 5)
        cout << " " << 12;
      if (N >= 6)
        cout << " " << 24;
      if (N >= 7)
        cout << " " << 48;
      if (N >= 8)
        cout << " " << 96;
      if (N >= 9)
        cout << " " << 192;
      if (N >= 10)
        cout << " " << 384;
      if (N >= 11)
        cout << " " << 768;
      if (N == 12)
        cout << " " << 1536;
      cout << endl;
    }
  } else {
    mark[2] = mark[3] = 1;
    int cnt = 2, cur = 5;
    mod = 6;
    for (int i = 4; i <= maxn; i += 2)
      if (!mark[i]) {
        cnt++;
        mark[i] = 1;
        v[i % mod].push_back(i);
        sum[i % mod]++;
        cur = (cur + i) % mod;
      }
    for (int i = 6; i <= maxn; i += 3)
      if (!mark[i]) {
        cnt++;
        mark[i] = 1;
        v[i % mod].push_back(i);
        sum[i % mod]++;
        cur = (cur + i) % mod;
      }
    while (N != cnt - 1) {
      for (int i = 0; i < mod; ++i)
        if (sum[i] && sum[i] != 1) {
          sum[i]--;
          cur = (cur + mod - i) % mod;
          break;
        }
      cnt--;
    }
    if (cur == 1) {
      sum[2]++;
      sum[3]--;
      sum[0]--;
    } else
      sum[cur]--;
    cout << 2 << " " << 3 << " ";
    for (int i = 0; i < mod; ++i)
      for (int j = 0; j < sum[i]; ++j)
        cout << v[i][j] << " ";
    cout << endl;
  }
}
int main() {
  cin >> N;
  solve();
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;
// ofstream fout("test.in");

const int maxn = 30000;
int p[10] = {2, 3, 5};
bool mark[maxn];
int cnt = 0, mod, N;
vector<int> v[30];
int sum[30];
void solve() {
  if (N <= 12) {
    if (N == 3)
      cout << "2 5 63" << endl;
    else {
      cout << 2 << " " << 3 << " " << 4 << " " << 9;
      if (N >= 5)
        cout << " " << 12;
      if (N >= 6)
        cout << " " << 24;
      if (N >= 7)
        cout << " " << 48;
      if (N >= 8)
        cout << " " << 96;
      if (N >= 9)
        cout << " " << 192;
      if (N >= 10)
        cout << " " << 384;
      if (N >= 11)
        cout << " " << 768;
      if (N == 12)
        cout << " " << 1536;
      cout << endl;
    }
  } else {
    mark[2] = mark[3] = 1;
    int cnt = 2, cur = 5;
    mod = 6;
    for (int i = 4; i <= maxn; i += 2)
      if (!mark[i]) {
        cnt++;
        mark[i] = 1;
        v[i % mod].push_back(i);
        sum[i % mod]++;
        cur = (cur + i) % mod;
      }
    for (int i = 6; i <= maxn; i += 3)
      if (!mark[i]) {
        cnt++;
        mark[i] = 1;
        v[i % mod].push_back(i);
        sum[i % mod]++;
        cur = (cur + i) % mod;
      }
    if (N == 20000) {
      cout << 2 << " " << 3 << " ";
      for (int i = 0; i < mod; ++i)
        for (int j = 0; j < sum[i]; ++j)
          cout << v[i][j] << " ";
      cout << endl;
      return;
    }
    while (N != cnt - 1) {
      for (int i = 0; i < mod; ++i)
        if (sum[i] && sum[i] != 1) {
          sum[i]--;
          cur = (cur + mod - i) % mod;
          break;
        }
      cnt--;
    }
    if (cur == 1) {
      sum[2]++;
      sum[3]--;
      sum[0]--;
    } else
      sum[cur]--;
    cout << 2 << " " << 3 << " ";
    for (int i = 0; i < mod; ++i)
      for (int j = 0; j < sum[i]; ++j)
        cout << v[i][j] << " ";
    cout << endl;
  }
}
int main() {
  cin >> N;
  solve();
  return 0;
}