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

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int w, h, k;
int memo[1 << 12][4][50][50];
int mask;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char field[64][64];

bool check(int ny, int nx) { return (0 <= ny && ny < h && 0 <= nx && nx < w); }

int getMax(int bit, int left, int ty, int tx) {
  if (memo[bit][left][ty][tx] != -1)
    return (memo[bit][left][ty][tx]);

  if (ty == h - 1 && tx == w - 1 && left == 0) {
    return (0);
  }

  int px = tx, py = ty;
  bool same = false;
  for (int i = 0; i < 2 * k; i++) {
    px += -dx[(bit >> (2 * i)) & 3];
    py += -dy[(bit >> (2 * i)) & 3];
    if (px == tx && py == ty) {
      same = true;
      break;
    }
  }

  int ret = 0;

  if (!same && field[ty][tx] != '.')
    ret += field[ty][tx] - '0';

  int val = -999999;
  for (int i = 0; i < 4; i++) {
    if (left == 0 && i >= 2)
      break;
    int nx = tx + dx[i], ny = ty + dy[i];
    if (check(ny, nx) && field[ny][nx] != '#') {
      val = max(val,
                getMax(((bit << 2) | i) & mask, left - (i >= 2), ny, nx) + ret);
    }
  }

  return (memo[bit][left][ty][tx] = val);
}

int main() {
  scanf("%d %d %d", &h, &w, &k);

  mask = (1 << (4 * k)) - 1;

  for (int i = 0; i < h; i++) {
    scanf("%s", field[i]);
  }

  memset(memo, -1, sizeof(memo));

  int res = getMax(0, k, 0, 0);

  printf("%d\\n", res < 0 ? 0 : res);

  return (0);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int w, h, k;
short memo[1 << 12][4][50][50];
int mask;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char field[64][64];

bool check(int ny, int nx) { return (0 <= ny && ny < h && 0 <= nx && nx < w); }

int getMax(int bit, int left, int ty, int tx) {
  if (memo[bit][left][ty][tx] != -1)
    return (memo[bit][left][ty][tx]);

  if (ty == h - 1 && tx == w - 1 && left == 0) {
    return (0);
  }

  int px = tx, py = ty;
  bool same = false;
  for (int i = 0; i < 2 * k; i++) {
    px += -dx[(bit >> (2 * i)) & 3];
    py += -dy[(bit >> (2 * i)) & 3];
    if (px == tx && py == ty) {
      same = true;
      break;
    }
  }

  int ret = 0;

  if (!same && field[ty][tx] != '.')
    ret += field[ty][tx] - '0';

  int val = -999999;
  for (int i = 0; i < 4; i++) {
    if (left == 0 && i >= 2)
      break;
    int nx = tx + dx[i], ny = ty + dy[i];
    if (check(ny, nx) && field[ny][nx] != '#') {
      val = max(val,
                getMax(((bit << 2) | i) & mask, left - (i >= 2), ny, nx) + ret);
    }
  }

  return (memo[bit][left][ty][tx] = val);
}

int main() {
  scanf("%d %d %d", &h, &w, &k);

  mask = (1 << (4 * k)) - 1;

  for (int i = 0; i < h; i++) {
    scanf("%s", field[i]);
  }

  memset(memo, -1, sizeof(memo));

  int res = getMax(0, k, 0, 0);

  printf("%d\\n", res < 0 ? 0 : res);

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

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <tuple>

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

using namespace std;

typedef pair<int, int> P;
typedef tuple<P, int, set<P>> T;

int n, m, z;
char x[50][51];

int a;
map<T, int> b;

int main() {
  scanf("%d%d%d", &n, &m, &z);
  rep(i, n) { scanf("%s", x[i]); }

  queue<T> q;
  q.push(T(P(0, 0), z, {}));
  while (!q.empty()) {
    T t = q.front();
    q.pop();
    P p = get<0>(t);
    int z = get<1>(t);
    set<P> s = get<2>(t);
    if (p == P(n - 1, m - 1)) {
      a = max(b[T(p, z, s)], a);
    }

    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    rep(i, 4) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      int nz = i >= 2 ? z - 1 : z;

      if (!(0 <= nx && nx < n && 0 <= ny && ny < m && nz >= 0 &&
            x[nx][ny] != '#')) {
        continue;
      }

      int g = 0;
      set<P> ns;
      if ('1' <= x[nx][ny] && x[nx][ny] <= '9' && s.find({nx, ny}) == s.end()) {
        ns.insert({nx, ny});
        g = x[nx][ny] - '0';
      }
      for (auto it = s.begin(); it != s.end(); ++it) {
        int u = max(nx - it->first, 0);
        int v = max(ny - it->second, 0);
        if (u + v <= nz) {
          ns.insert(*it);
        }
      }

      T nk({nx, ny}, nz, ns);
      if (b.find(nk) == b.end() || b[nk] < b[T(p, z, s)] + g) {
        b[nk] = b[T(p, z, s)] + g;
        q.push(T({nx, ny}, nz, ns));
      }
    }
  }
  printf("%d\\n", a);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <tuple>

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

using namespace std;

typedef pair<int, int> P;
typedef tuple<P, int, set<P>> T;

int n, m, z;
char x[50][51];

int a;
map<T, int> b;

void dfs(P p, int z, set<P> s) {
  if (p == P(n - 1, m - 1)) {
    a = max(b[T(p, z, s)], a);
  }

  int dx[] = {-1, 0, 0, 1};
  int dy[] = {0, -1, 1, 0};
  rep(i, 4) {
    int nx = p.first + dx[i];
    int ny = p.second + dy[i];
    int nz = i <= 1 ? z - 1 : z;

    if (!(0 <= nx && nx < n && 0 <= ny && ny < m && nz >= 0 &&
          x[nx][ny] != '#')) {
      continue;
    }

    int g = 0;
    set<P> ns;
    if ('1' <= x[nx][ny] && x[nx][ny] <= '9' && s.find({nx, ny}) == s.end()) {
      ns.insert({nx, ny});
      g = x[nx][ny] - '0';
    }
    for (auto it = s.begin(); it != s.end(); ++it) {
      int u = max(nx - it->first, 0);
      int v = max(ny - it->second, 0);
      if (u + v <= nz) {
        ns.insert(*it);
      }
    }

    T nk({nx, ny}, nz, ns);
    if (b.find(nk) == b.end() || b[nk] < b[T(p, z, s)] + g) {
      b[nk] = b[T(p, z, s)] + g;
      dfs({nx, ny}, nz, ns);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &z);
  rep(i, n) { scanf("%s", x[i]); }

  dfs(P(0, 0), z, {});
  printf("%d\\n", a);
  return 0;
}
Problem p00504 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int H, W;
char c[50][50];
map<vector<pair<int, int>>, int> dp[50][50][4];
int dfs(int x, int y, int k, vector<pair<int, int>> vis) {
  if (k < 0)
    return -1e9;
  vector<pair<int, int>> p;
  for (int i = 0; i < vis.size(); i++) {
    if (max(0, x - vis[i].first) + max(0, y - vis[i].second) <= k)
      p.push_back(vis[i]);
  }
  sort(p.begin(), p.end());
  vis = p;
  if (dp[x][y][k].count(vis))
    return dp[x][y][k][vis];

  int ans = (x == W - 1 && y == H - 1 ? 0 : -1e9);
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || tx >= W || ty < 0 || ty >= H || c[ty][tx] == '#')
      continue;
    int cst = 0;
    vector<pair<int, int>> t = vis;
    if (!binary_search(vis.begin(), vis.end(), make_pair(tx, ty))) {
      cst = c[ty][tx] - '0';
      t.push_back(make_pair(tx, ty));
    }
    ans = max(ans, dfs(tx, ty, k - (i < 2), t) + cst);
  }
  // cout << x << " " << y << " " << k << " " << vis.size() << endl;
  return dp[x][y][k][vis] = ans;
}
int main() {
  int K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
      if (c[i][j] == '.')
        c[i][j] = '0';
    }
  }
  cout << dfs(0, 0, K, vector<pair<int, int>>(0)) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int H, W;
char c[50][50];
map<vector<pair<int, int>>, int> dp[50][50][3];
int dfs(int x, int y, int k, vector<pair<int, int>> vis) {
  if (k < 0)
    return -1e9;
  vector<pair<int, int>> p;
  for (int i = 0; i < vis.size(); i++) {
    if (max(0, x - vis[i].first) + max(0, y - vis[i].second) <= k)
      p.push_back(vis[i]);
  }
  sort(p.begin(), p.end());
  vis = p;
  if (dp[x][y][k].count(vis))
    return dp[x][y][k][vis];

  int ans = (x == W - 1 && y == H - 1 ? 0 : -1e9);
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx < 0 || tx >= W || ty < 0 || ty >= H || c[ty][tx] == '#')
      continue;
    int cst = 0;
    vector<pair<int, int>> t = vis;
    if (!binary_search(vis.begin(), vis.end(), make_pair(tx, ty))) {
      cst = c[ty][tx] - '0';
      t.push_back(make_pair(tx, ty));
    }
    ans = max(ans, dfs(tx, ty, k - (i < 2), t) + cst);
  }
  // cout << x << " " << y << " " << k << " " << vis.size() << endl;
  return dp[x][y][k][vis] = ans;
}
int main() {
  int K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
      if (c[i][j] == '.')
        c[i][j] = '0';
    }
  }
  cout << dfs(0, 0, K, vector<pair<int, int>>(0)) << endl;
}
Problem p00506 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {

  int n, a, b, c, count = 1, minnum;

  cin >> n;

  if (n == 2) {
    cin >> a >> b;
    minnum = min(a, b);
    while (count != minnum) {
      if (a % count == 0 && b % count == 0) {
        cout << count << endl;
      }
      count++;
    }
  } else {
    cin >> a >> b >> c;
    minnum = min(a, min(b, c));
    while (count != minnum) {
      if (a % count == 0 && b % count == 0 && c % count == 0) {
        cout << count << endl;
      }
      count++;
    }
  }

  return 0;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;

int gcd(int a, int b) {
  if (a == 0)
    return b;

  return gcd(b, a % b);
}

int main() {

  int n, a, b, c;

  cin >> n;

  if (n == 2) {
    cin >> a >> b;
    cout << gcd(a, b) << endl;
  } else {
    cin >> a >> b >> c;
    cout << gcd(a, gcd(b, c)) << endl;
  }

  return 0;
}
Problem p00506 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int GCD(int a, int b) {
  int A, B, buff;
  // A >= B
  if (b > a) {
    A = b;
    B = a;
  } else {
    A = a;
    B = b;
  }
  while (A % B) {
    buff = B;
    B = A % B;
    A = buff;
  }
  return B;
}

int main() {
  int N, a, b, c;
  int G;
  int GC[1000000];
  scanf("%d", &N);
  if (!(N - 2)) {
    scanf("%d %d", &a, &b);
    G = GCD(a, b);
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\\n", GC[j]);
    }
  } else {
    scanf("%d %d %d", &a, &b, &c);
    G = GCD(c, GCD(a, b));
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0 && c % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\\n", GC[j]);
    }
  }

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>

int GCD(int a, int b) {
  int A, B, buff;
  // A >= B
  if (b > a) {
    A = b;
    B = a;
  } else {
    A = a;
    B = b;
  }
  while (A % B) {
    buff = B;
    B = A % B;
    A = buff;
  }
  return B;
}

int main() {
  int N, a, b, c;
  int G;
  int GC[100000000];
  scanf("%d", &N);
  if (!(N - 2)) {
    scanf("%d %d", &a, &b);
    G = GCD(a, b);
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\\n", GC[j]);
    }
  } else {
    scanf("%d %d %d", &a, &b, &c);
    G = GCD(c, GCD(a, b));
    for (int i = 1; i <= G; i++) {
      if (a % i == 0 && b % i == 0 && c % i == 0) {
        GC[i] = i;
      } else
        GC[i] = -i;
    }
    for (int j = 1; j <= G; j++) {
      if (GC[j] < 0)
        continue;
      printf("%d\\n", GC[j]);
    }
  }

  return 0;
}
Problem p00506 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[3];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= 100000000; i++) {
    int b;
    for (b = 0; b < n; b++) {
      if (a[b] % i)
        break;
    }
    if (b == n) {
      cout << i << endl;
    }
  }
  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[3];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= 100000000; i++) {
    int b = 0;
    for (b = 0; b < n; b++) {
      if (a[b] % i)
        break;
    }
    if (b == n) {
      cout << i << endl;
    }
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

int ns[3];

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void solve(void) {
  int N;
  cin >> N;
  rep(N, i) scanf("%d", &ns[i]);
  int X = gcd(gcd(ns[0], ns[1]), ns[2]);
  Rep(1, X, x) if (X % x == 0) printf("%d\\n", x);
}

int main(void) {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)), (x).end())

int ns[3];

void solve(void) {
  int N;
  cin >> N;
  rep(N, i) scanf("%d", &ns[i]);
  sort(ns, ns + N);
  vector<int> gcds;
  Rep(1, ns[0], x) {
    bool f = true;
    rep(N, i) f = f && ns[i] % x == 0;
    if (f)
      gcds.pb(x);
  }
  for (int x : gcds)
    printf("%d\\n", x);
}

int main(void) {
  solve();
  return 0;
}
Problem p00506 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int a, b, c, n;
  scanf("%d", &n);
  if (n == 3) {
    scanf("%d %d %d", &a, &b, &c);
    for (int i = 1;; i++) {
      if (a + b + c == i * 3)
        break;
      if (((a % i == 0) && (b % i == 0)) && (c % i == 0)) {
        printf("%d\\n", i);
      }
    }
  } else {
    scanf("%d %d", &a, &b);
    for (unsigned int i = 1;; i++) {
      if (a + b == i * 2)
        break;
      if ((a % i == 0) && (b % i == 0)) {
        printf("%d\\n", i);
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  int a, b, c, n;
  scanf("%d", &n);
  if (n == 3) {
    scanf("%d %d %d", &a, &b, &c);
    for (int i = 1;; i++) {
      if (((a % i == 0) && (b % i == 0)) && (c % i == 0)) {
        printf("%d\\n", i);
      }
    }
  } else {
    scanf("%d %d", &a, &b);
    for (unsigned int i = 1;; i++) {
      if ((a % i == 0) && (b % i == 0)) {
        printf("%d\\n", i);
      }
    }
  }
}
Problem p00506 (C++) — buggy submission, verdict: Memory Limit Exceeded

// AOJ 0583
#include <iostream>
#include <map>

using namespace std;

int main() {
  int n;
  int num[5], ma = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
    ma = max(ma, num[i]);
  }

  for (int a = 1; a <= ma; a++) {
    if (ma % a == 0) {
      bool f = true;
      for (int i = 0; i < n; i++) {
        if (num[i] % a != 0) {
          f = false;
          break;
        }
      }
      if (f) {
        cout << a << endl;
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

// AOJ 0583
#include <iostream>
#include <map>

using namespace std;

int main() {
  int n;
  int num[5], ma = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
    ma = max(ma, num[i]);
  }

  map<int, int> data;
  for (int i = 0; i < n; i++) {
    for (int a = 1; a <= num[i]; a++) {
      if (num[i] % a == 0) {
        // cout << a << endl;
        data[a]++;
      }
    }
  }

  for (int i = 1; i <= ma; i++) {
    if (data[i] == n)
      cout << i << endl;
  }

  return 0;
}
Problem p00506 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main(void) {
  long n, a[200000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  if (n == 2) {
    for (long j = 1; j < 200000; j++) {
      if (a[0] % j == 0 && a[1] % j == 0) {
        cout << j << endl;
      }
    }
  }

  else if (n == 3) {
    for (long j = 1; j < 200000; j++) {
      if (a[0] % j == 0 && a[1] % j == 0) {
        if (a[3] % j == 0) {
          cout << j << endl;
        }
      }
    }
  }
}

Error: -8

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main(void) {
  long n, a[4];
  cin >> n;

  for (long i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (long i = 0; i < 200; i++) {
    if (a[0] % i == 0 && a[1] % i == 0) {
      if (n == 3 && a[2] % i == 0) {
        cout << i << endl;
      } else if (n == 2) {
        cout << i << endl;
      }
    }
  }
}
Problem p00507 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
int main() {
  int n;
  priority_queue<string> que[10];
  cin >> n;

  for (int i = 0; i < n; i++) {
    string in;
    cin >> in;
    int size = in.size();
    que[size].push(in);
    if (que[size].size() == 10)
      que[size].pop();
  }

  vector<string> v;

  for (int i = 0; i < 10; i++) {
    while (!que[i].empty()) {
      v.push_back(que[i].top());
      que[i].pop();
    }
  }

  priority_queue<ll> q;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v.size(); j++) {
      if (i == j)
        continue;
      string str = v[i] + v[j];
      q.push(atoll(str.c_str()));
      if (q.size() == 4) {
        q.pop();
      }
    }
  }

  cout << q.top() << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

typedef long long ll;
int main() {
  int n;
  priority_queue<string> que[10];
  cin >> n;

  for (int i = 0; i < n; i++) {
    string in;
    cin >> in;
    que[in.size()].push(in);
    if (que[in.size()].size() == 4)
      que[in.size()].pop();
  }

  vector<string> v;

  for (int i = 0; i < n; i++) {
    while (!que[i].empty()) {
      v.push_back(que[i].top());
      que[i].pop();
    }
  }

  priority_queue<ll> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      string str = v[i] + v[j];
      q.push(atoll(str.c_str()));
      if (q.size() == 4) {
        q.pop();
      }
    }
  }

  cout << q.top() << endl;

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  vector<int> b;
  string st;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      st = a[i] + a[j];
      // cout << st << endl;
      int num;
      num = atoi(st.c_str());
      b.push_back(num);
      sort(b.begin(), b.end());
      if (b.size() > 3)
        b.pop_back();
    }
  }
  for (int i = 0; i < 3; i++) {
    if (i == 2)
      cout << b[i] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  vector<int> b;
  string st;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      st = a[i] + a[j];
      // cout << st << endl;
      int num;
      num = atoi(st.c_str());
      b.push_back(num);
    }
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < 3; i++) {
    if (i == 2)
      cout << b[i] << endl;
  }
  return 0;
}
Problem p00507 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
const int inf = 1000000000;

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin >> n;
  string a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int permutation_a[10000];
  for (int i = 0; i < 10000; i++)
    permutation_a[i] = inf;
  int index = 0;
  for (int i = 0; i < n && index < 10000; i++) {
    for (int j = 0; j < n && index < 10000; j++) {
      if (i == j)
        continue;
      permutation_a[index] = atoi((a[i] + a[j]).c_str());
      index++;
    }
  }
  sort(permutation_a, permutation_a + index);
  cout << permutation_a[2] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int inf = 1000000000;

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin >> n;
  string a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int permutation_a[n * n];
  for (int i = 0; i < n * n; i++)
    permutation_a[i] = inf;
  int index = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      permutation_a[index] = atoi((a[i] + a[j]).c_str());
      index++;
    }
  }
  sort(permutation_a, permutation_a + index);
  cout << permutation_a[2] << endl;
  return 0;
}
Problem p00507 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, dum, temp;
  vector<int> a;
  vector<int> dig;
  // vector<int> ans;
  int ans[3] = {100000000, 100000000, 100000000};

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &dum);
    a.push_back(dum);
    temp = 0;
    while (dum != 0) {
      dum = dum / 10;
      temp++;
    }
    dig.push_back(temp);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        temp = 1;
        for (int k = 0; k < dig[j]; k++) {
          temp *= 10;
        }
        dum = a[i] * temp + a[j];
        if (ans[0] >= dum) {
          ans[2] = ans[1];
          ans[1] = ans[0];
          ans[0] = dum;
        } else if (ans[1] >= dum) {
          ans[2] = ans[1];
          ans[1] = dum;
        } else if (ans[2] >= dum) {
          ans[2] = dum;
        }
      }
    }
  }
  printf("%d\\n", ans[2]);
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, dum, temp;
  vector<int> a;
  vector<int> dig;
  vector<int> ans;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &dum);
    a.push_back(dum);
    temp = 0;
    while (dum != 0) {
      dum = dum / 10;
      temp++;
    }
    dig.push_back(temp);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        temp = 1;
        for (int k = 0; k < dig[j]; k++) {
          temp *= 10;
        }
        ans.push_back((a[i] * temp) + a[j]);
      }
    }
  }
  sort(ans.begin(), ans.end());
  printf("%d\\n", ans[2]);
}