Problem p03362 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define d_5 100000
#define d9_7 1000000007
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
// 素数列挙
#define P 60000
vl primes;
ll primenum[P];
bool bprime[P * 30];
ll searchprime(ll max) { // 発見した素因数の個数を返す
  bprime[1] = false;
  for (ll i = 2; i <= max; i++) {
    bprime[i] = true;
  }
  ll nowprimes = 1;
  primes.pb(2);
  for (ll i = 4; i < max * 30; i += 2) {
    bprime[i] = false;
  }

  for (ll i = 3; i < max / 2; i += 2) {
    if (bprime[i] == true) {
      // primes[nowprimes]=i;
      primes.pb(i);
      nowprimes++;
      for (ll j = 2; i * j <= max * 30; j++) {
        bprime[i * j] = false;
      }
    }
  }
  return nowprimes;
}
ll nn;
void primenum_factorization(ll n) { // primesで素因数分解の結果をprimenumに格納
  rep(i, P) { primenum[i] = 0; }
  for (int i = 0; primes[i] <= n; i++) {
    ll pren = n;
    while (1) {
      if (pren % primes[i] == 0) {
        primenum[i]++;
        pren /= primes[i];
      } else {
        break;
      }
    }
  }
  return;
}

int flag = 0;
int n;
deque<ll> search(deque<ll> list, int nowp) {
  int f = 0;
  if (list.size() >= 5) {
    for (int i1 = 0; i1 < list.size() - 4 && f == 0; i1++) {
      for (int i2 = i1 + 1; i2 < list.size() - 3 && f == 0; i2++) {
        for (int i3 = i2 + 1; i3 < list.size() - 2 && f == 0; i3++) {
          for (int i4 = i3 + 1; i4 < list.size() - 1 && f == 0; i4++) {
            for (int i5 = i4 + 1; i5 < list.size() && f == 0; i5++) {
              if (bprime[list[i1] + list[i2] + list[i3] + list[i4] +
                         list[i5]] == false) {
                continue;
              } else {
                f = 1;
                break;
              }
            }
          }
        }
      }
    }
  }
  if (f == 1) {
    return {};
  }
  if (list.size() == n) {
    flag = 1;
    return list;
  } else {
    while (nowp < primes.size()) {
      list.pb(primes[nowp]);
      deque<ll> tmp = search(list, nowp + 1);
      if (tmp.size() != 0) {
        return tmp;
      }
      list.pop_back();
      nowp++;
    }
    return {};
  }
}
int main(void) {
  cin >> n;
  ll max1 = searchprime(60000);
  deque<ll> list = search({}, 0);
  rep(i, list.size()) { cout << list[i] << " "; }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define d_5 100000
#define d9_7 1000000007
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
// 素数列挙
#define P 60000
vl primes;
ll primenum[P];
bool bprime[P * 30];
ll searchprime(ll max) { // 発見した素因数の個数を返す
  bprime[1] = false;
  for (ll i = 2; i <= max; i++) {
    bprime[i] = true;
  }
  ll nowprimes = 1;
  primes.pb(2);
  for (ll i = 4; i < max * 30; i += 2) {
    bprime[i] = false;
  }

  for (ll i = 3; i < max / 2; i += 2) {
    if (bprime[i] == true) {
      // primes[nowprimes]=i;
      primes.pb(i);
      nowprimes++;
      for (ll j = 2; i * j <= max * 30; j++) {
        bprime[i * j] = false;
      }
    }
  }
  return nowprimes;
}
ll nn;
void primenum_factorization(ll n) { // primesで素因数分解の結果をprimenumに格納
  rep(i, P) { primenum[i] = 0; }
  for (int i = 0; primes[i] <= n; i++) {
    ll pren = n;
    while (1) {
      if (pren % primes[i] == 0) {
        primenum[i]++;
        pren /= primes[i];
      } else {
        break;
      }
    }
  }
  return;
}

int flag = 0;
int n;
deque<ll> search(deque<ll> list, int nowp) {
  int f = 0;
  if (list.size() >= 5) {
    for (int i1 = 0; i1 < list.size() - 4 && f == 0; i1++) {
      for (int i2 = i1 + 1; i2 < list.size() - 3 && f == 0; i2++) {
        for (int i3 = i2 + 1; i3 < list.size() - 2 && f == 0; i3++) {
          for (int i4 = i3 + 1; i4 < list.size() - 1 && f == 0; i4++) {
            for (int i5 = i4 + 1; i5 < list.size() && f == 0; i5++) {
              if (bprime[list[i1] + list[i2] + list[i3] + list[i4] +
                         list[i5]] == false) {
                continue;
              } else {
                f = 1;
                break;
              }
            }
          }
        }
      }
    }
  }
  if (f == 1) {
    return {};
  }
  if (list.size() == n) {
    flag = 1;
    return list;
  } else {
    while (nowp < primes.size()) {
      list.pb(primes[nowp]);
      deque<ll> tmp = search(list, nowp + 1);
      if (tmp.size() != 0) {
        return tmp;
      }
      list.pop_back();
      nowp++;
    }
    return {};
  }
}
int main(void) {
  cin >> n;
  ll max1 = searchprime(60000);
  // deque<ll> list=search({},0);
  int now = 0;
  int nowp = 0;
  while (now < n) {
    if (primes[nowp] % 5 == 1) {
      cout << primes[nowp] << " ";
      now++;
    }
    nowp++;
  }
  return 0;
}