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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + n,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(0, m), closest_pair(m, n));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + n,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + n,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin(), a.begin() + m, a.begin() + r,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}
Problem p00508 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin() + l, a.begin() + m, a.begin() + r,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;

int N;
vector<P> a;

bool compare_y(P a, P b) { return a.second < b.second; }

// [l, r)
double closest_pair(int l, int r) {
  int n = r - l;
  if (r - l <= 1)
    return 2e9; // 1点しかない
  int m = (l + r) / 2;
  int x = a[m].first;
  int d = min(closest_pair(l, m), closest_pair(m, r));
  inplace_merge(
      a.begin() + r, a.begin() + m, a.begin() + r,
      compare_y); // 二つのソート済み配列をマージする 範囲指定->(先頭,中間,最後)

  vector<P> b;
  FOR(i, l, r) {
    if (abs(a[i].first - x) >= d)
      continue;
    FOR(j, 0, b.size()) {
      // 近いものから見て行く
      int dx = a[i].first - b[b.size() - j - 1].first;
      int dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break; // 枝狩り?
      d = min(d, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  a.resize(N);
  FOR(i, 0, N) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  cout << closest_pair(0, N) << endl;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;

  vector<int> x(n), y(n);
  vector<P> v(n);
  rep(i, n) {
    cin >> x[i] >> y[i];
    v[i].first = x[i];
    v[i].second = y[i];
  }

  sort(v.begin(), v.end());

  int ans = INF;
  rep(i, n) {
    REP(j, i + 1, n) {
      int x = v[i].first, y = v[i].second;
      int x2 = v[j].first, y2 = v[j].second;
      int d = (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y);
      if ((x2 - x) * (x2 - x) > ans)
        break;
      ans = min(ans, d);
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;

  vector<int> x(n), y(n);
  vector<P> v(n);
  rep(i, n) {
    cin >> x[i] >> y[i];
    v[i].first = x[i];
    v[i].second = y[i];
  }

  sort(v.begin(), v.end());

  int ans = INF;
  rep(i, n) {
    for (int j = i - 1; j >= 0; j--) {
      int x = v[i].first, y = v[i].second;
      int x2 = v[j].first, y2 = v[j].second;
      if (x - x2 > ans)
        break;
      ans = min(ans, (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
// #define int long long
using namespace std;
#define INF INT_MAX

typedef pair<double, double> P;
int N;
P A[500000];
int x, y;

bool compare_y(P a, P b) { return a.second < b.second; }

double closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].first;
  double d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare_y);

  vector<P> b;
  rep(i, n) {
    if (fabs(a[i].first - x) >= d)
      continue;

    rep(j, b.size()) {
      double dx = a[i].first - b[b.size() - j - 1].first;
      double dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, sqrt(dx * dx + dy * dy));
    }
    b.push_back(a[i]);
  }

  return d;
}

signed main() {
  cin >> N;
  rep(i, N) {
    scanf("%d%d", &x, &y);
    A[i].first = x;
    A[i].second = y;
  }
  sort(A, A + N);
  printf("%.0f\\n", pow(closest_pair(A, N), 2));
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
// #define int long long
using namespace std;

typedef complex<double> P;

namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

pair<P, P> closestPair(vector<P> p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0, S[1] = 1;
  sort(p.begin(), p.end()); // "p < q" <=> "p.x < q.x"
  double d = norm(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++)
    REP(j, m) {
      if (norm(p[S[j]] - p[i]) < d)
        d = norm(p[s = S[j]] - p[t = i]);
      if (real(p[S[j]]) < real(p[i]) - d)
        S[j--] = S[--m];
    }
  return make_pair(p[s], p[t]);
}

int n, x, y;
vector<P> points;

signed main() {
  cin >> n;
  rep(i, n) {
    scanf("%d%d", &x, &y);
    points.push_back(P(x, y));
  }
  pair<P, P> nearest_points = closestPair(points);

  cout << pow(real(nearest_points.first) - real(nearest_points.second), 2) +
              pow(imag(nearest_points.first) - imag(nearest_points.second), 2)
       << endl;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
vector<int> v[20010];
int main() {
  int n, x, y, l, r, z, i, j, w, a = 1145141919;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    x += 10000, y += 10000;
    w = (int)sqrt((int)a);
    for (j = max(0, x - w); j <= min(20005, x + w); j++) {
      l = 0, r = v[j].size();
      while (l < r) {
        z = (r + l) / 2;
        a = min(a, (x - j) * (x - j) + (y - v[j][z]) * (y - v[j][z]));
        if (v[j][z] < y)
          l = z + 1;
        else
          r = z;
      }
    }
    v[x].push_back(y);
  }
  cout << a << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> v[20010];
int main() {
  int n, x, y, l, r, z, i, j, a = 1145141919;
  cin >> n;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    x += 10000, y += 10000;
    for (j = 0; j < 20010; j++) {
      l = 0, r = v[j].size();
      while (l < r) {
        z = (r + l) / 2;
        a = min(a, (x - j) * (x - j) + (y - v[j][z]) * (y - v[j][z]));
        if (v[j][z] < y)
          l = z + 1;
        else
          r = z;
      }
    }
    v[x].push_back(y);
  }
  cout << a << endl;
  return 0;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int main() {

  int n, x, y;
  vector<pair<int, int>> v, u;
  cin >> n;
  rep(i, n) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  sort(v.begin(), v.end());

  rep(i, n - 1) u.push_back(
      make_pair(v[i + 1].first - v[i].first, v[i + 1].second - v[i].second));

  sort(u.begin(), u.end());

  int d = u[0].first * u[0].first + u[0].second * u[0].second;
  REP(i, 0, n - 2) {
    REP(j, i + 1, n - 1) {
      int dx = v[j].first - v[i].first, dy = v[j].second - v[i].second;
      if (dx * dx > d)
        break;
      else if (dx * dx + dy * dy < d)
        d = dx * dx + dy * dy;
    }
  }

  cout << d << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int main() {

  int n, x, y;
  vector<pair<int, int>> v, u;
  cin >> n;
  rep(i, n) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  sort(v.begin(), v.end());

  rep(i, n - 1) u.push_back(
      make_pair(v[i + 1].first - v[i].first, v[i + 1].second - v[i].second));

  sort(u.begin(), u.end());

  int d = u[0].first * u[0].first + u[0].second * u[0].second;
  REP(i, 0, n - 2) {
    REP(j, i + 1, n - 1) {
      int dx = v[j].first - v[i].first, dy = v[j].second - v[i].second;
      if (dx * dx + dy * dy < d)
        d = dx * dx + dy * dy;
    }
  }

  cout << d << endl;

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

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef vector<P> VP;
#define X real()
#define Y imag()
namespace std {
bool operator<(const P &a, const P &b) {
  return (a.X != b.X) ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main() {
  int n;
  cin >> n;
  VP a(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = P(x, y);
  }
  sort(a.begin(), a.end());

  double ans = 1e9;
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if ((a[j].X - a[i].X) * (a[j].X - a[i].X) > ans)
        break;
      ans = min(ans, norm(a[i] - a[j]));
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef vector<P> VP;
#define X real()
#define Y imag()
namespace std {
bool operator<(const P &a, const P &b) {
  return (a.X != b.X) ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int main() {
  int n;
  cin >> n;
  VP a(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = P(x, y);
  }
  sort(a.begin(), a.end());

  double ans = 1e9;
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (a[j].X < a[i].X - ans)
        break;
      ans = min(ans, norm(a[i] - a[j]));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00509 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\\t" #x " = " << (x) << endl

int n, c;
string ans;

int conv(const deque<int> &d) {
  if (d[0] == 0)
    return -1;
  int res = 0;
  rep(i, d.size()) { res = res * 10 + d[i]; }
  return res;
}

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

void dfs(deque<int> &d) {
  if ((int)d.size() >= n * 2) {
    if (is_prime(conv(d))) {
      string x(all(d));
      rep(i, x.size()) x[i] += '0';
      ans = max(ans, x);
    }
  } else {
    rep(i, 10) {
      d.push_back(i);
      d.push_front(i);
      dfs(d);
      d.pop_front();
      d.pop_back();
    }
  }
}

signed main() {
  while (cin >> n >> c) {
    ans = "0";
    if (c < 0) {
      if (n == 1) {
        ans = "11";
      } else {
        ans = string(2 * n, '9');
      }
    } else {
      deque<int> d;
      if (c < 0) {
        dfs(d);
      } else {
        d = {c};
        dfs(d);
      }
      if (ans == "0") {
        rep(i, n) {
          d.push_back(9);
          d.push_front(9);
        }
        ans = conv(d);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\\t" #x " = " << (x) << endl

int n, c;
int ans;

int conv(const deque<int> &d) {
  if (d[0] == 0)
    return -1;
  int res = 0;
  rep(i, d.size()) { res = res * 10 + d[i]; }
  return res;
}

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

void dfs(deque<int> &d) {
  if ((int)d.size() >= n * 2) {
    if (is_prime(conv(d))) {
      ans = max(ans, conv(d));
    }
  } else {
    rep(i, 10) {
      d.push_back(i);
      d.push_front(i);
      dfs(d);
      d.pop_front();
      d.pop_back();
    }
  }
}

signed main() {
  while (cin >> n >> c) {
    ans = -1;
    if (c < 0) {
      if (n == 1) {
        ans = 11;
      } else {
        ans = stoll(string(2 * n, '9'));
      }
    } else {
      deque<int> d;
      if (c < 0) {
        dfs(d);
      } else {
        d = {c};
        dfs(d);
      }
      if (ans == -1) {
        rep(i, n) {
          d.push_back(9);
          d.push_front(9);
        }
        ans = conv(d);
      }
    }
    cout << ans << endl;
  }
}
Problem p00509 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
const int INF = 1e9;
string rs;
ll N, C, ans;
bool is_prime(ll n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
ll to_i(string s) {
  ll k = 1, res = 0;
  reverse(ALL(s));
  REP(i, s.size()) {
    res += (s[i] - '0') * k;
    k *= 10;
  }
  return res;
}
void rec(string s) {
  if (s.size() == N) {
    if ((s[0] - '0') % 2 == 0)
      return;
    rs = s;
    reverse(ALL(rs));
    if (C >= 0)
      s += (char)(C + '0');
    s += rs;
    ll t = to_i(s);
    if (is_prime(t))
      ans = max(ans, t);
    return;
  }
  REP(i, 10) rec(s + (char)(i + '0'));
  return;
}
int main() {
  cin >> N >> C;
  ans = -1;
  if (C < 0) {
    REP(i, N * 2) cout << 9;
    cout << endl;
  } else {
    rec("");
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
const int INF = 1e9;
string rs;
ll N, C, ans;
bool is_prime(ll n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
ll to_i(string s) {
  ll k = 1, res = 0;
  reverse(ALL(s));
  REP(i, s.size()) {
    res += (s[i] - '0') * k;
    k *= 10;
  }
  return res;
}
void rec(string s) {
  if (s.size() == N) {
    if ((s[0] - '0') % 2 == 0)
      return;
    rs = s;
    reverse(ALL(rs));
    if (C >= 0)
      s += (char)(C + '0');
    s += rs;
    ll t = to_i(s);
    if (is_prime(t))
      ans = max(ans, t);
    return;
  }
  REP(i, 10) rec(s + (char)(i + '0'));
  return;
}
int main() {
  cin >> N >> C;
  ans = -1;
  rec("");
  if (ans == -1) {
    if (C >= 0) {
      REP(i, N) cout << 9;
      cout << C;
      REP(i, N) cout << 9;
    } else {
      REP(i, N * 2) cout << 9;
    }
  } else {
    cout << ans;
  }
  cout << endl;
}