alexjercan/bugnet huggingface dataset:
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

int ps[10000000] = {0};
using namespace std;

int main() {
  vector<int> primes;
  vector<bool> ok(10000001, true);
  FOR(i, 2, 9999999) {
    for (int j = 2; j * i <= 10000000; j++) {
      ok[j * i] = false;
    }
    /*
    bool flg = true;
    for(int j=0;primes[j]*primes[j]<=i;j++){
        if(i % primes[j] == 0){
            flg = false;
            break;
        }
    }
    if(flg){
        primes.PB(i);
    }
    */
  }
  ok[0] = false;
  ok[1] = false;
  REP(i, 10000001) {
    if (ok[i])
      primes.PB(i);
  }
  int d[] = {8, 6, 2, 0};
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int p = upper_bound(ALL(primes), n) - primes.begin();
    RREP(i, p + 1) {
      if (primes[i] > n)
        continue;
      if (i >= 3) {
        if (primes[i] == (primes[i - 1] + d[2]) &&
            primes[i] == (primes[i - 2] + d[1]) &&
            primes[i] == (primes[i - 3] + d[0])) {
          cout << primes[i] << endl;
          break;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

int ps[10000000] = {0};
using namespace std;

int main() {
  vector<int> primes;
  primes.PB(2);
  FOR(i, 3, 9999999) {
    bool flg = true;
    for (int j = 0; primes[j] * primes[j] <= i; j++) {
      if (i % primes[j] == 0) {
        flg = false;
        break;
      }
    }
    if (flg) {
      primes.PB(i);
    }
  }
  int d[] = {8, 6, 2, 0};
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int p = upper_bound(ALL(primes), n) - primes.begin();
    RREP(i, p + 1) {
      if (primes[i] > n)
        continue;
      if (i >= 3) {
        if (primes[i] == (primes[i - 1] + d[2]) &&
            primes[i] == (primes[i - 2] + d[1]) &&
            primes[i] == (primes[i - 3] + d[0])) {
          cout << primes[i] << endl;
          break;
        }
      }
    }
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

const int INF = 11000000;
bool num[INF + 1] = {false}; // 素数ならfalse

int prime4[1000];

void devide(int n) {
  int i, j;
  for (i = 2; i * i <= n; i++) {
    for (j = i * 2; j <= n; j += i) {
      num[j] = true;
    }
  }
}
int GetPrime4(int n) {
  int i, cnt = 0;
  for (i = 2; i <= n - 8; i++) {
    if (num[i] == false && num[i + 2] == false && num[i + 6] == false &&
        num[i + 8] == false) {
      prime4[cnt] = i + 8;
      cnt++;
    }
  }
  return cnt;
}

int main() {
  int cnt;
  devide(INF);
  cnt = GetPrime4(INF);
  int n, i;

  while (cin >> n) {
    if (n == 0)
      break;
    for (i = 0; prime4[i] <= n; i++)
      ;
    cout << prime4[i - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

int prime[730000] = {2, 3};
int prime4[900];
int GetPrime(int n);
int GetPrime4(int pcnt);

int main() {
  int i, n;
  int pcnt, p4cnt;
  pcnt = GetPrime(10000000);
  // cout << pcnt << endl;
  p4cnt = GetPrime4(pcnt);
  // cout << p4cnt << endl;
  while (cin >> n) {
    if (n == 0)
      break;
    // 2分探索
    int start = 0, last = p4cnt - 1, medi;
    while (start <= last) {
      medi = (start + last) >> 1;
      if (prime4[medi] == n) {
        break;
      }
      if (prime4[medi] > n) {
        last = medi - 1;
      }
      if (prime4[medi] < n) {
        if (prime4[medi + 1] > n)
          break;
        if (prime4[medi + 1] == n) {
          medi++;
          break;
        }
        start = medi + 1;
      }
    }
    cout << prime4[medi] << endl;
  }
  return 0;
}

int GetPrime(int n) {
  int i, j;

  int cnt = 2;
  for (i = 5; i <= n; i += 2) {
    if (i % 6 == 0)
      continue;
    for (j = 2; j < cnt; j++) {
      if (i % prime[j] == 0)
        break;
      if (prime[j] * prime[j] > i) {
        j = cnt;
        break;
      }
    }
    if (j == cnt) {
      prime[cnt] = i;
      cnt++;
    }
  }
  return cnt;
}

int GetPrime4(int pcnt) {
  int i;
  int cnt = 0;
  for (i = 0; i < pcnt - 3; i++) {
    if (prime[i + 1] - prime[i] == 2 && prime[i + 2] - prime[i + 1] == 4 &&
        prime[i + 3] - prime[i + 2] == 2) {
      prime4[cnt] = prime[i + 3];
      cnt++;
      i++;
    }
  }
  return cnt;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> p;
bool check(int n) {
  for (int i = 0; i < p.size(); i++) {
    if (p[i] == n)
      return true;
    else if (n % p[i] == 0)
      return false;
    else if (p[i] * p[i] > n)
      return true;
  }
  return true;
}
int main() {
  // your code goes here
  int N;

  p.push_back(2);
  for (int i = 3; i <= 10000; i++) {
    for (int j = 0; j < p.size(); j++) {
      if (i % p[j] == 0)
        break;
      if (p[j] * p[j] > i) {
        p.push_back(i);
        break;
      }
    }
  }
  while (cin >> N && N != 0) {
    int maxim = 0;
    while (N % 6 != 1)
      N--;
    for (int i = N; i >= 13; i -= 6)
      if (check(i) && check(i - 2) && check(i - 6) && check(i - 8)) {
        maxim = i;
        break;
      }
    cout << maxim << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> p;
bool check(int n) {
  for (int i = 0; i < p.size(); i++) {
    if (p[i] == n)
      return true;
    else if (n % p[i] == 0)
      return false;
    else if (p[i] * p[i] > n)
      return true;
  }
  return true;
}
int main() {
  // your code goes here
  int N;

  p.push_back(2);
  for (int i = 3; i <= 10000; i++) {
    for (int j = 0; j < p.size(); j++) {
      if (i % p[j] == 0)
        break;
      if (p[j] * p[j] > i) {
        p.push_back(i);
        break;
      }
    }
  }
  while (cin >> N && N != 0) {
    int maxim = 0;
    for (int i = 13; i <= N; i += 6)
      if (check(i) && check(i - 2) && check(i - 6) && check(i - 8))
        maxim = i;

    cout << maxim << endl;
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>

char prmt[10000005] = {};

int rest[10000005] = {};

int main(void) {
  int n, i, j;
  prmt[0] = -1;
  prmt[1] = -1;
  for (i = 2; i <= 10000000; i++) {
    if (prmt[i] == -1)
      continue;
    prmt[i] = 1;
    j = i * 2;
    while (j <= 10000000) {
      prmt[j] = -1;
      j += i;
    }
  }
  j = 13;
  for (i = 13; i <= 10000000; i++) {
    if (prmt[i] == 1 && prmt[i - 2] == 1 && prmt[i - 6] == 1 &&
        prmt[i - 8] == 1)
      j = i;
    rest[i] = j;
  }
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d\\n", rest[n]);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>

int prmt[10000005] = {};

int rest[10000005] = {};

int main(void) {
  int n, i, j;
  prmt[0] = -1;
  prmt[1] = -1;
  for (i = 2; i <= 10000000; i++) {
    if (prmt[i] == -1)
      continue;
    prmt[i] = 1;
    j = i * 2;
    while (j <= 10000000) {
      prmt[j] = -1;
      j += i;
    }
  }
  j = 13;
  for (i = 13; i <= 10000000; i++) {
    if (prmt[i] == 1 && prmt[i - 2] == 1 && prmt[i - 6] == 1 &&
        prmt[i - 8] == 1)
      j = i;
    rest[i] = j;
  }
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d\\n", rest[n]);
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define LL long long int
using namespace std;
int flg[10000002];
int main(void) {
  int i, j, k, n;
  for (i = 2; i <= 10000000; i++)
    flg[i] = 1;
  for (i = 2; i * i <= 10000000; i++) {
    if (flg[i] == 1) {
      for (j = 2; j * i < 10000000; j++)
        flg[i * j] = 0;
    }
  }
  cin >> n;
  while (n != 0) {
    for (i = n; i >= 13; i--) {
      if (flg[i] == 1 && flg[i - 2] == 1 && flg[i - 6] == 1 &&
          flg[i - 8] == 1) {
        cout << i << endl;
        break;
      }
    }
    cin >> n;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long int
using namespace std;
LL flg[10000000], i, j, n;
int main(void) {
  while (cin >> n && n) {
    for (i = 0; i <= n; i++)
      flg[i] = 1;
    for (i = 2; i * i <= n; i++) {
      if (flg[i] == 1) {
        for (j = 2; j * i < n; j++)
          flg[i * j] = 0;
      }
    }
    for (i = n; i >= 13; i--) {
      if (flg[i] == 1) {
        if (flg[i - 2] == 1 && flg[i - 6] == 1 && flg[i - 8] == 1) {
          cout << i << endl;
          break;
        }
      }
    }
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
const int MAX = 10000010;
using namespace std;

bool p[MAX];

void sieve_of_eratosthenes() {
  for (int i = 0; i < MAX; i++) {
    p[i] = (i <= 1) ? false : true;
  }
  for (int i = 2; i <= sqrt((double)MAX) + 1; i++) {
    if (p[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        p[j] = false;
      }
    }
  }
}

int main() {
  int n, m = 13;

  sieve_of_eratosthenes();
  while (cin >> n, n) {
    for (int i = n; i >= 13; --i) {
      if (p[i] && p[i - 2] && p[i - 6] && p[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
const int MAX = 10000010;
using namespace std;

bool p[MAX];

void sieve_of_eratosthenes() {
  for (int i = 0; i < MAX; i++) {
    p[i] = (i <= 1) ? false : true;
  }
  for (int i = 2; i <= sqrt((double)MAX) + 1; i++) {
    if (p[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        p[j] = false;
      }
    }
  }
}

int main() {
  int n, m = 13;

  sieve_of_eratosthenes();
  while (cin >> n, n) {
    for (int i = 2; i < MAX; ++i) {
      if (p[i] && p[i + 2] && p[i + 6] && p[i + 8]) {
        if (i + 8 > n)
          break;
        m = i + 8;
      }
    }
    cout << m << endl;
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
bool x[10000001];
int y[4000];
vector<int> a, b;
int main() {
  for (int i = 2; i < 4000; i++) {
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        goto E;
      }
    }
    b.push_back(i);
    y[i] = y[i - 1] + 1;
    goto G;
  E:;
    y[i] = y[i - 1];
  G:;
  }
  int loop = 0;
  for (int i = 3; i < 10000000; i += 2) {
    for (int j = 0; j < y[(int)sqrt(i)]; j++) {
      if (i % b[j] == 0) {
        goto F;
      }
      loop++;
    }
    x[i] = true;
  F:;
  }
  for (int i = 0; i < 9999993; i++) {
    if (x[i] == 1 && x[i + 2] == 1 && x[i + 6] == 1 && x[i + 8] == 1) {
      a.push_back(i + 8);
    }
  }
  int n, maxn = 0;
  while (true) {
    maxn = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < a.size(); i++) {
      if (a[i] <= n) {
        maxn = a[i];
      }
    }
    cout << maxn << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
bool x[10000001];
vector<int> a;
int main() {
  for (int i = 3; i < 10000000; i += 2) {
    for (int j = 3; j <= sqrt(i); j += 2) {
      if (i % j == 0) {
        goto F;
      }
    }
    x[i] = true;
  F:;
  }
  for (int i = 0; i < 9999993; i++) {
    if (x[i] == 1 && x[i + 2] == 1 && x[i + 6] == 1 && x[i + 8] == 1) {
      a.push_back(i + 8);
    }
  }
  int n, maxn = 0;
  while (true) {
    maxn = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < a.size(); i++) {
      if (a[i] <= n) {
        maxn = a[i];
      }
    }
    cout << maxn << endl;
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const int nmax = 10000000;
char nums[nmax];

int main() {
  int n;
  for (int i = 2; i < nmax; i++) {
    nums[i] = '1';
  }
  for (int i = 2; i * i < nmax; i++) {
    if (nums[i] == '1') {
      for (int k = 2; k * i < nmax; k++) {
        nums[i * k] = '0';
      }
    }
  }
  while (cin >> n) {
    if (n == 0)
      break;
    int i = n;
    while (nums[i] == '0' || nums[i - 2] == '0' || nums[i - 6] == '0' ||
           nums[i - 8] == '0')
      i--;
    cout << i << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

const int nmax = 10000000;
char nums[nmax];

int main() {
  int n;
  for (int i = 2; i < nmax; i++) {
    nums[i] = '1';
  }
  for (int i = 2; i * i < nmax; i++) {
    for (int k = 2; k * i < nmax; k++) {
      nums[i * k] = '0';
    }
  }
  while (cin >> n) {
    if (n == 0)
      break;
    int i = n;
    while (nums[i] == '0' || nums[i - 2] == '0' || nums[i - 6] == '0' ||
           nums[i - 8] == '0')
      i--;
    cout << i << endl;
  }
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<bool> dp, searched;

bool isprime(int n, int k);

bool is_prime(int n) {
  if (searched[n]) {
    return dp[n];
  }

  if (n % 2 == 0) {
    return false;
  }

  return isprime(n, 3);
}

bool isprime(int n, int k) {
  if (n < k * k) {
    searched[n] = true;

    dp[n] = true;

    return true;
  }

  if (n % k == 0) {
    searched[n] = true;

    dp[n] = false;

    return false;
  }

  return isprime(n, k + 2);
}

int main() {
  int N;

  dp = searched = vector<bool>(10000001, false);

  while (true) {
    cin >> N;

    if (N == 0) {
      break;
    }

    int Ans = 1145141919;

    if (N % 2 == 0) {
      N -= 1;
    }

    if (N % 3 == 0) {
      N -= 2;
    }

    for (int i = N;;) {
      if (is_prime(i - 8) && is_prime(i - 6) && is_prime(i - 2) &&
          is_prime(i)) {
        Ans = i;
        break;
      }

      if (i % 6 == 1) {
        i -= 2;
      } else {
        i -= 4;
      }
    }

    cout << Ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<bool> dp, searched;

bool isprime(int n, int k);

bool is_prime(int n) {
  if (searched[n]) {
    return dp[n];
  }

  if (n % 2 == 0) {
    return false;
  }

  return isprime(n, 3);
}

bool isprime(int n, int k) {
  if (n < k * k) {
    searched[n] = true;

    dp[n] = true;

    return true;
  }

  if (n % k == 0) {
    searched[n] = true;

    dp[n] = false;

    return false;
  }

  return isprime(n, k + 2);
}

int main() {
  int N;

  dp = searched = vector<bool>(10000001, false);

  while (true) {
    cin >> N;

    if (N == 0) {
      break;
    }

    int Ans = 1145141919;

    if (N % 2 == 0) {
      N -= 1;
    }

    for (int i = N;; i -= 2) {
      if (is_prime(i - 8) && is_prime(i - 6) && is_prime(i - 2) &&
          is_prime(i)) {
        Ans = i;
        break;
      }
    }

    cout << Ans << endl;
  }

  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

// const int MAX_N = 100001;
const int MAX_N = 10000001;

bool prime[MAX_N];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int i = 2; i <= MAX_N; ++i) {
    prime[i] = true;
  }

  for (int i = 2; i <= MAX_N; ++i) {
    if (!prime[i])
      continue;
    for (int j = 2; i * j <= MAX_N; ++j) {
      prime[i * j] = false;
    }
  }

  int n;
  while (cin >> n && n) {
    for (int i = n; i >= 13; --i) {
      if (prime[i] && prime[i - 2] && prime[i - 6] && prime[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

const int MAX_N = 10000001;

bool prime[MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int i = 2; i <= MAX_N; ++i) {
    if (prime[i])
      continue;
    for (int j = 2; i * j <= MAX_N; ++j) {
      prime[i * j] = false;
    }
  }

  int n;
  while (cin >> n && n) {
    for (int i = n; i >= 13; --i) {
      if (prime[i] && prime[i - 2] && prime[i - 6] && prime[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }

  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * src.cpp
 *
 *  Created on: 2016/08/04
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)

const int MAX_N = 1e7;

bool prime[MAX_N + 1];

void sieve() {
  memset(prime, 1, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (prime[i]) {
      for (int j = i * 2; j <= MAX_N; j += i) {
        prime[j] = false;
      }
    }
  }
}

int n;

int main() {
  sieve();
  do {
    scanf("%d", &n);
    if (n) {
      for (int x = n;; x--) {
        if (prime[x] && prime[x - 2] && prime[x - 6] && prime[x - 8]) {
          printf("%d\\n", x);
          break;
        }
      }
    }
  } while (n);
  return 0;
}

Error: TLE

Fixed submission:

/*
 * src.cpp
 *
 *  Created on: 2016/08/04
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)

bool isPrime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int n;

int main() {
  do {
    scanf("%d", &n);
    if (n) {
      for (int x = n;; x--) {
        if (isPrime(x) && isPrime(x - 2) && isPrime(x - 6) && isPrime(x - 8)) {
          printf("%d\\n", x);
          break;
        }
      }
    }
  } while (n);
  return 0;
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool isp[10000000] = {};

void makeEra() {
  for (int i = 2; i <= 10000; i++) {
    if (isp[i]) {
      for (int j = 2; i * j < 10000000; j++) {
        isp[i * j] = false;
      }
    }
  }
}

int main() {
  rep(i, 10000000) isp[i] = true;
  isp[0] = isp[1] = false;
  makeEra();

  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = n;; i--) {
      if (isp[i] && isp[i - 2] && isp[i - 6] && isp[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool isp[10000000] = {};

void makeEra() {
  for (int i = 2; i <= 10000; i++) {
    if (isp[i]) {
      for (int j = 2; i * j <= 10000000; j++) {
        isp[i * j] = false;
      }
    }
  }
}

int main() {
  rep(i, 1000000) isp[i] = true;
  isp[0] = isp[1] = false;
  makeEra();

  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = n;; i--) {
      if (isp[i] && isp[i - 2] && isp[i - 6] && isp[i - 8]) {
        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00222 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool era[10000001] = {false};

void makeERA() {
  for (int i = 2; i < 1000001; i++) {
    if (era[i] == true) {
      int x = 10000000 / i;
      for (int j = 2; j <= x; j++) {
        era[i * j] = false;
      }
    }
  }
  era[2] = true;
}

int main() {
  for (int i = 0; i < 10000001; i++)
    era[i] = true;
  era[0] = false;
  era[1] = false;
  makeERA();

  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    if (n % 2 == 0)
      n--;
    for (int i = n; i >= 8; i -= 2) {
      if (era[i - 8]) {
        if (era[i - 6]) {
          if (!era[i - 4]) {
            if (era[i - 2]) {
              if (era[i]) {
                cout << i << endl;
                goto next;
              } else
                i -= 4;
            } else
              i -= 4;
          } else
            i -= 4;
        }
      } else
        i -= 2;
    }
  next:;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool era[10000001] = {false};

void makeERA() {
  for (int i = 2; i < 1000001; i++) {
    if (era[i] != true)
      continue;
    for (int j = 2;; j++) {
      if (i * j <= 1000000)
        era[i * j] = false;
      else
        break;
    }
  }
  era[2] = true;
}

int main() {
  for (int i = 0; i < 10000001; i++)
    era[i] = true;
  era[0] = false;
  era[1] = false;
  makeERA();

  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    if (n % 2 == 0)
      n--;
    for (int i = n; i >= 8; i -= 2) {
      if (era[i - 8]) {
        if (era[i - 6]) {
          if (!era[i - 4]) {
            if (era[i - 2]) {
              if (era[i]) {
                cout << i << endl;
                goto next;
              } else
                i -= 4;
            } else
              i -= 4;
          } else
            i -= 4;
        }
      } else
        i -= 2;
    }
  next:;
  }

  return 0;
}