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

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

using namespace std;

struct H {
  vector<int> dat;
  void insert(int x) {
    int i = (int)dat.size();
    dat.push_back(x);
    while (i > 0) {
      auto p = (i - 1) / 2;
      if (dat[p] < dat[i]) {
        swap(dat[p], dat[i]);
        i = p;
      } else {
        break;
      }
    }
  }
  int front() { return dat[0]; }
  void remove() {
    swap(dat[0], dat[dat.size() - 1]);
    dat.pop_back();
    int len = (int)dat.size();
    int i = 0;
    while (i * 2 + 1 < len) {
      if (i * 2 + 2 < len) {
        int j = (dat[i * 2 + 1] > dat[i * 2 + 2] ? i * 2 + 1 : i * 2 + 2);
        if (dat[i] < dat[j]) {
          swap(dat[i], dat[j]);
          i = j;
        } else {
          break;
        }
      } else {
        if (dat[i] < dat[i * 2 + 1]) {
          swap(dat[i], dat[i * 2 + 1]);
          i = i * 2 + 1;
        } else {
          break;
        }
      }
    }
  }
};

int main() {
  H h;
  while (1) {
    string s;
    cin >> s;
    if (s[0] == 'e') {
      if (s[1] == 'n')
        break;
      cout << h.front() << endl;
      h.remove();
    } else {
      int x;
      cin >> x;
      h.insert(x);
    }
  }
}


Error: TLE

Fixed submission:

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

using namespace std;

struct H {
  vector<int> dat;
  void insert(int x) {
    int i = (int)dat.size();
    dat.push_back(x);
    while (i > 0) {
      auto p = (i - 1) / 2;
      if (dat[p] < dat[i]) {
        swap(dat[p], dat[i]);
        i = p;
      } else {
        break;
      }
    }
  }
  int front() { return dat[0]; }
  void remove() {
    swap(dat[0], dat[dat.size() - 1]);
    dat.pop_back();
    int len = (int)dat.size();
    int i = 0;
    while (i * 2 + 1 < len) {
      if (i * 2 + 2 < len) {
        int j = (dat[i * 2 + 1] > dat[i * 2 + 2] ? i * 2 + 1 : i * 2 + 2);
        if (dat[i] < dat[j]) {
          swap(dat[i], dat[j]);
          i = j;
        } else {
          break;
        }
      } else {
        if (dat[i] < dat[i * 2 + 1]) {
          swap(dat[i], dat[i * 2 + 1]);
          i = i * 2 + 1;
        }
      }
    }
  }
};

int main() {
  H h;
  while (1) {
    string s;
    cin >> s;
    if (s[0] == 'e') {
      if (s[1] == 'n')
        break;
      cout << h.front() << endl;
      h.remove();
    } else {
      int x;
      cin >> x;
      h.insert(x);
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int num = 2000000;
int a[num];
int h;
inline int pa(int i) { return i / 2; }
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int lg;
  if (l <= h && a[i] < a[l])
    lg = l;
  else
    lg = i;
  if (r <= h && a[lg] < a[r])
    lg = r;
  if (lg != i) {
    swap(a[i], a[lg]);
    maxHeapify(lg);
  }
}
inline void insert(int k) {
  h++;
  a[h] = k;
  int i = h;
  while (i > 1 && a[i] > a[pa(i)]) {
    swap(a[i], a[pa(i)]);
    i = pa(i);
  }
}
inline void extract() {
  printf("%d", a[1]);
  printf("\\n");
  a[1] = a[h];
  h--;
  maxHeapify(1);
}
int main() {
  char com[10];
  int key;
  for (int i = 1;; i++) {
    scanf("%s", &com);
    if (com[0] == 'i') {
      scanf("%d", &key);
      insert(key);
    } else if (com[1] == 'x')
      extract();
    else
      break;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int num = 2000000;
int a[num];
int h;
inline int pa(int i) { return i / 2; }
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int lg;
  if (l <= h && a[i] < a[l])
    lg = l;
  else
    lg = i;
  if (r <= h && a[lg] < a[r])
    lg = r;
  if (lg != i) {
    swap(a[i], a[lg]);
    maxHeapify(lg);
  }
}
inline void insert(int k) {
  h++;
  a[h] = k;
  int i = h;
  while (i > 1) {
    if (a[h] > a[pa(h)])
      swap(a[h], a[pa(h)]);
    i = pa(h);
  }
}
inline void extract() {
  printf("%d", a[1]);
  printf("\\n");
  a[1] = a[h];
  h--;
  maxHeapify(1);
}
int main() {
  char com[10];
  int key;
  for (int i = 1;; i++) {
    scanf("%s", &com);
    if (com[0] == 'i') {
      scanf("%d", &key);
      insert(key);
    } else if (com[1] == 'x')
      extract();
    else
      break;
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

priority_queue<int> S;
int main() {
  int k;
  string str;

  while (true) {
    cin >> str;
    if (str == "insert") {
      cin >> k;
      S.push(k);
    } else if (str == "extract") {
      cout << S.top() << endl;
      S.pop();
    } else if (str == "end")
      break;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

priority_queue<int> S;

void insert(priority_queue<int> Que, int k) {
  Que.push(k);
  S = Que;
}

int extractMax(priority_queue<int> Que) {
  if (!S.empty()) {
    int value = Que.top();
    Que.pop();
    S = Que;
    return value;
  }
}

int main() {
  int k;
  string str;

  while (str != "end") {
    cin >> str;
    if (str == "insert") {
      cin >> k;
      insert(S, k);
    } else if (str == "extract") {
      int r = extractMax(S);
      if (r)
        cout << r << endl;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;

int main() {

  priority_queue<int> maxq;

  while (1) {
    char str[9];
    scanf("%s", str);

    if (str[0] == 'i') {
      int num;
      scanf("%d", &num);
      maxq.push(num);
    } else if (str[1] == 'x') {
      int ans = maxq.top();
      maxq.pop();
      printf("%d\\n", ans);
    } else
      break;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

priority_queue<int> maxq;

int main() {

  while (1) {
    string str;
    cin >> str;

    if (str == "insert") {
      int num;
      cin >> num;
      maxq.push(num);
    } else if (str == "extract") {
      int ans = maxq.top();
      maxq.pop();
      cout << ans << endl;
    } else
      break;
  }
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>

int parent(int i) { return i / 2; }

int left(int i) { return 2 * i; }

int right(int i) { return 2 * i + 1; }

void maxHeapify(int *A, int i, int n) {
  int l_idx, r_idx, temp;
  l_idx = left(i + 1) - 1;
  r_idx = right(i + 1) - 1;
  temp = i;

  if (r_idx < n) {
    if (A[r_idx] > A[i]) {
      temp = r_idx;
    }
  }
  if (l_idx < n) {
    if (A[l_idx] > A[temp]) {
      temp = l_idx;
    }
  }
  if (temp != i) {
    std::swap(A[i], A[temp]);
    maxHeapify(A, temp, n);
  }
}

void compareParent(int *A, int i, int n) {
  int p;
  if (i != 0) {
    p = parent(i + 1) - 1;
    if (A[i] > A[p]) {
      std::swap(A[i], A[p]);
      compareParent(A, p, n);
    }
  }
}

int main(void) {
  int n;
  int A[2000000];
  n = 0;
  char op[10];
  while (1) {
    scanf("%s", op);
    if (op[2] == 'd') {
      break;
    } else if (op[2] == 's') {
      scanf("%d", &A[n]);
      n++;
      compareParent(A, n - 1, n);
    } else {
      printf("%d\\n", A[0]);
      std::swap(A[n - 1], A[0]);
      n--;
      maxHeapify(A, 0, n);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>

int parent(int i) { return i / 2; }

int left(int i) { return 2 * i; }

int right(int i) { return 2 * i + 1; }

void maxHeapify(int *A, int i, int n) {
  int l_idx, r_idx, temp;
  l_idx = left(i + 1) - 1;
  r_idx = right(i + 1) - 1;
  temp = i;

  if (r_idx < n) {
    if (A[r_idx] > A[i]) {
      temp = r_idx;
    }
  }
  if (l_idx < n) {
    if (A[l_idx] > A[temp]) {
      temp = l_idx;
    }
  }
  if (temp != i) {
    std::swap(A[i], A[temp]);
    maxHeapify(A, temp, n);
  }
}

void compareParent(int *A, int i, int n) {
  int p;
  if (i != 0) {
    p = parent(i + 1) - 1;
    if (A[i] > A[p]) {
      std::swap(A[i], A[p]);
      compareParent(A, p, n);
    }
  }
}

int main(void) {
  int n;
  int A[200000];
  n = 0;
  char op[10];
  while (1) {
    scanf("%s", op);
    if (op[2] == 'd') {
      break;
    } else if (op[2] == 's') {
      scanf("%d", &A[n]);
      n++;
      compareParent(A, n - 1, n);
    } else {
      printf("%d\\n", A[0]);
      std::swap(A[n - 1], A[0]);
      n--;
      maxHeapify(A, 0, n);
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<unsigned long> a;

void mH(vector<unsigned long> &a, unsigned long i) {
  unsigned long l, r, lg;
  l = 2 * i + 1;
  r = 2 * i + 2;

  if (l < a.size() && a[l] > a[i])
    lg = l;
  else
    lg = i;
  if (r < a.size() && a[r] > a[lg])
    lg = r;

  if (lg != i) {
    swap(a[i], a[lg]);
    mH(a, lg);
  }
}

int main() {
  string s;

  cin >> s;

  while (s != "end") {
    if (s == "insert") {
      unsigned long k = 0, h = 0;
      cin >> k;
      a.push_back(k);

      h = a.size() - 1;
      while (h > 0) {
        h = (h - 1) / 2;
        mH(a, h);
      }
    } else if (s == "extract") {
      cout << a[0] << endl;
      swap(a[0], a[a.size() - 1]);
      a.pop_back();

      if (a.size() > 1)
        mH(a, 0);
    }
    cin >> s;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

vector<unsigned long> a;

void mH(vector<unsigned long> &a, unsigned long i) {
  unsigned long l, r, lg;
  l = 2 * i + 1;
  r = 2 * i + 2;

  if (l < a.size() && a[l] > a[i])
    lg = l;
  else
    lg = i;
  if (r < a.size() && a[r] > a[lg])
    lg = r;

  if (lg != i) {
    swap(a[i], a[lg]);
    mH(a, lg);
  }
}

int main() {
  string s;

  cin >> s;

  while (s != "end") {
    if (s == "insert") {
      unsigned long k = 0, h = 0;
      cin >> k;
      a.push_back(k);

      for (long i = a.size() / 2 - 1; i > -1; i--)
        mH(a, i);
    } else if (s == "extract") {
      cout << a[0] << endl;
      swap(a[0], a[a.size() - 1]);
      a.pop_back();

      if (a.size() > 1)
        for (long i = a.size() / 2 - 1; i > -1; i--)
          mH(a, i);
    }
    cin >> s;
  }

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

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

using namespace std;

const int MAX = 2000000;
int S[MAX + 1];
int H = 0;

void maxHeapify(int A[], int n, int i) {

  int largest = i;
  int l = i * 2;
  int r = i * 2 + 1;

  if (l <= n && A[l] > A[i])
    largest = l;
  if (r <= n && A[r] > A[largest])
    largest = r;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(A, n, largest);
  }
}
void buildMaxHeap(int A[], int n) {
  for (int i = n / 2; i >= 1; --i)
    maxHeapify(A, n, i);
}
void insert(int S[], int k) {
  ++H;
  S[H] = k;

  int i = H;
  while (i > 1 && S[i / 2] < S[i]) {
    swap(S[i / 2], S[i]);
    i = i / 2;
  }
}

int extract(int S[]) {
  int ret = S[1];
  swap(S[1], S[H]);
  --H;
  maxHeapify(S, H, 1);

  return ret;
}

int main() {

  while (1) {
    char order[7];
    scanf("%s", order);

    int k;
    if (strcmp(order, "insert") == 0) {
      scanf("%d", &k);
      insert(S, k);
    } else if (strcmp(order, "extract") == 0) {
      cout << extract(S) << endl;
    } else {
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int MAX = 2000000;
int S[MAX + 1];
int H = 0;

void maxHeapify(int A[], int n, int i) {

  int largest = i;
  int l = i * 2;
  int r = i * 2 + 1;

  if (l <= n && A[l] > A[i])
    largest = l;
  if (r <= n && A[r] > A[largest])
    largest = r;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(A, n, largest);
  }
}
void buildMaxHeap(int A[], int n) {
  for (int i = n / 2; i >= 1; --i)
    maxHeapify(A, n, i);
}
void insert(int S[], int k) {
  ++H;
  S[H] = k;

  int i = H;
  while (i > 1 && S[i / 2] < S[i]) {
    swap(S[i / 2], S[i]);
    i = i / 2;
  }
}

int extract(int S[]) {
  int ret = S[1];
  swap(S[1], S[H]);
  --H;
  buildMaxHeap(S, H);

  return ret;
}

int main() {

  while (1) {
    char order[7];
    scanf("%s", order);

    int k;
    if (strcmp(order, "insert") == 0) {
      scanf("%d", &k);
      insert(S, k);
    } else if (strcmp(order, "extract") == 0) {
      cout << extract(S) << endl;
    } else {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> class Heap {
  struct node_t {
    T val;
    int dep;
    node_t *l;
    node_t *r;

    node_t(T _val) : val(_val), dep(0), l(NULL), r(NULL) {}
    ~node_t() {
      if (l != NULL)
        delete l;
      if (r != NULL)
        delete r;
    }
  };

  node_t *root;

  node_t *meld(node_t *a, node_t *b) {
    if (a == NULL)
      return b;
    if (b == NULL)
      return a;
    if (a->val < b->val)
      swap(a, b);
    a->r = meld(a->r, b);
    if (a->l == NULL || a->l->dep < a->r->dep)
      swap(a->l, a->r);
    a->dep = (a->r == NULL ? 0 : a->r->dep) + 1;
    return a;
  }

public:
  Heap() : root(NULL) {}
  ~Heap() { delete root; }

  void meld(Heap &h) { root = meld(this->root, h.root); }

  void insert(T val) { root = meld(this->root, new node_t(val)); }

  T min_element() { return root->val; }

  T pop() {
    T ret = min_element();
    root = meld(root->l, root->r);
    return ret;
  }

  bool empty() { return root == NULL; }
};

int main() {
  char com[10];
  int n;

  Heap<int> heap;

  while (scanf("%s", com) && com[2] != 'd') {
    if (com[0] == 'i') {
      scanf("%d", &n);
      heap.insert(n);
    } else {
      cout << heap.pop() << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

template <typename T> class Heap {
  struct node_t {
    T val;
    int dep;
    node_t *l;
    node_t *r;

    node_t(T _val) : val(_val), dep(0), l(NULL), r(NULL) {}
    ~node_t() {
      if (l != NULL)
        delete l;
      if (r != NULL)
        delete r;
    }
  };

  node_t *root;

  node_t *meld(node_t *a, node_t *b) {
    if (a == NULL)
      return b;
    if (b == NULL)
      return a;
    if (a->val < b->val)
      swap(a, b);
    a->r = meld(a->r, b);
    if (a->l == NULL || a->l->dep < a->r->dep)
      swap(a->l, a->r);
    a->dep = (a->r == NULL ? 0 : a->r->dep) + 1;
    return a;
  }

public:
  Heap() : root(NULL) {}
  ~Heap() { delete root; }

  void meld(Heap &h) { root = meld(this->root, h.root); }

  void insert(T val) { root = meld(this->root, new node_t(val)); }

  T min_element() { return root->val; }

  T pop() {
    T ret = min_element();
    root = meld(root->l, root->r);
    return ret;
  }

  bool empty() { return root == NULL; }
};

int main() {
  string com;
  int n;

  Heap<int> heap;

  while (cin >> com && com != "end") {

    if (com == "insert") {
      cin >> n;
      heap.insert(n);
    } else {
      cout << heap.pop() << endl;
    }
  }

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

#include <cstdio>
#include <string.h>

int s[2000001];
int size = 0;

int Swap(int *S, int loc) {
  if (loc > 0 && S[loc] > S[(loc - 1) / 2]) {
    int tmp = S[loc];
    S[loc] = S[(loc - 1) / 2];
    S[(loc - 1) / 2] = tmp;
    return Swap(S, (loc - 1) / 2);
  }
  return loc;
}

int SwapRev(int *S, int loc) {
  if (loc * 2 + 2 < size) {
    int mxi;
    if (S[loc * 2 + 1] > S[loc * 2 + 2]) {
      mxi = loc * 2 + 1;
    } else {
      mxi = loc * 2 + 2;
    }
    if (S[mxi] > S[loc]) {
      int tmp = S[mxi];
      S[mxi] = S[loc];
      S[loc] = tmp;
      return SwapRev(S, mxi);
    }
  } else if (loc * 2 + 1 < size) {
    if (S[loc * 2 + 1] > S[loc]) {
      int tmp = S[loc * 2 + 1];
      S[loc * 2 + 1] = S[loc];
      S[loc] = tmp;
    }
    return loc;
  } else {
    return loc;
  }
}

int Insert(int *S, int k) {
  S[size] = k;
  Swap(S, size);
  size++;
  return k;
}

int ExtractMax(int *S) {
  int ret = S[0];
  S[0] = S[size - 1];
  size--;
  SwapRev(S, 0);
  return ret;
}

int main() {
  char cmd[32];
  int v;
  while (scanf("%s", cmd) != EOF) {
    if (strcmp(cmd, "insert") == 0) {
      scanf("%d", &v);
      Insert(s, v);
    } else if (strcmp(cmd, "extract") == 0) {
      printf("%d\\n", ExtractMax(s));
    } else if (strcmp(cmd, "end") == 0) {
      break;
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <utility>

using namespace std;

int extract_max(priority_queue<int> s) {
  int rem = s.top();
  s.pop();
  return rem;
}

void insert(priority_queue<int> s, int k) { s.push(k); }

int main() {
  string cmd;
  priority_queue<int> Q;
  while (cin >> cmd && cmd != "end") {
    if (cmd == "insert") {
      int k;
      cin >> k;
      insert(Q, k);
    } else if (cmd == "extract") {
      cout << extract_max(Q) << endl;
    }
  }

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

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

void insert(int A[], int key);
void heapIncreaseKey(int A[], int i, int key);
int heapExtractMax(int A[]);
void maxHeapify(int A[], int i);

int Sentinel = -2000000000;
int H;
int A[2000000];

int main() {
  string command;
  cin >> command;
  int key;

  while (command != "end") {
    if (command == "insert") {
      cin >> key;
      insert(A, key);
    } else if (command == "extract") {
      cout << heapExtractMax(A) << endl;
    }
    cin >> command;
  }
}

void insert(int A[], int key) {
  H++;
  A[H] = Sentinel;
  heapIncreaseKey(A, H, key);
}

void heapIncreaseKey(int A[], int i, int key) {
  if (key < A[i])
    exit(0);
  A[i] = key;
  int parent = i / 2;
  while (i > 1 && A[parent] < A[i]) {
    swap(A[i], A[parent]);
    i = parent;
    parent = i / 2;
  }
}

int heapExtractMax(int A[]) {
  if (H < 1)
    exit(0);
  int max = A[1];
  A[1] = A[H];
  H--;
  maxHeapify(A, 1);
  return max;
}

void maxHeapify(int A[], int i) {
  int left = i * 2, right = left + 1, largest;
  if (left <= H && A[left] > A[i])
    largest = left;
  else
    largest = i;
  if (right <= H && A[largest] < A[right])
    largest = right;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(A, largest);
  }
}

Error: 0

Fixed submission:

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

void insert(int A[], int key);
void heapIncreaseKey(int A[], int i, int key);
int heapExtractMax(int A[]);
void maxHeapify(int A[], int i);

int Sentinel = -2000000000;
int H;

int main() {
  string command;
  cin >> command;
  int key, A[200000];

  while (command != "end") {
    if (command == "insert") {
      cin >> key;
      insert(A, key);
    } else if (command == "extract") {
      cout << heapExtractMax(A) << endl;
    }
    cin >> command;
  }
}

void insert(int A[], int key) {
  H++;
  A[H] = Sentinel;
  heapIncreaseKey(A, H, key);
}

void heapIncreaseKey(int A[], int i, int key) {
  if (key < A[i])
    exit(0);
  A[i] = key;
  int parent = i / 2;
  while (i > 1 && A[parent] < A[i]) {
    swap(A[i], A[parent]);
    i = parent;
    parent = i / 2;
  }
}

int heapExtractMax(int A[]) {
  if (H < 1)
    exit(0);
  int max = A[1];
  A[1] = A[H];
  H--;
  maxHeapify(A, 1);
  return max;
}

void maxHeapify(int A[], int i) {
  int left = i * 2, right = left + 1, largest;
  if (left <= H && A[left] > A[i])
    largest = left;
  else
    largest = i;
  if (right <= H && A[largest] < A[right])
    largest = right;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(A, largest);
  }
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
int val, size_, heap[2097168];
char c[9];
inline void insert(int x) {
  int ptr = size_++;
  heap[ptr] = x;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
}
inline int extract() {
  int ret = heap[1], ptr = 1;
  while ((ptr << 1) + 1 < size_) {
    ptr <<= 1;
    if (heap[ptr] > heap[ptr + 1])
      swap(heap[ptr >> 1], heap[ptr]);
    else
      swap(heap[ptr + 1], heap[ptr >> 1]), ptr++;
  }
  heap[ptr] = heap[--size_], heap[size_] = 0;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
  return ret;
}
int main() {
  size_ = 1;
  while (scanf("%s", c), c[3]) {
    if (c[0] == 'i')
      scanf("%d", &val), insert(val);
    else
      printf("%d\\n", extract());
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int val, size_, heap[2097168];
char c[9];
inline void insert(int x) {
  int ptr = size_++;
  heap[ptr] = x;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
}
inline int extract() {
  int ret = heap[1], ptr = 1;
  while ((ptr << 1) + 1 < size_) {
    ptr <<= 1;
    if (heap[ptr] > heap[ptr + 1])
      swap(heap[ptr >> 1], heap[ptr]);
    else
      swap(heap[ptr + 1], heap[ptr >> 1]), ptr++;
  }
  heap[ptr] = heap[--size_], heap[size_] = 0;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
  return ret;
}
int main() {
  size_ = 1;
  while (scanf("%s", c) != 3) {
    if (c[0] == 'i')
      scanf("%d", &val), insert(val);
    else
      printf("%d\\n", extract());
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#pragma warning(disable : 4996)
using namespace std;
int val, size_, heap[2097168];
char c[9];
inline void insert(int x) {
  int ptr = size_++;
  heap[ptr] = x;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
}
inline int extract() {
  int ret = heap[1], ptr = 1;
  while ((ptr << 1) + 1 < size_) {
    ptr <<= 1;
    if (heap[ptr] > heap[ptr + 1])
      swap(heap[ptr >> 1], heap[ptr]);
    else
      swap(heap[ptr + 1], heap[ptr >> 1]), ptr++;
  }
  heap[ptr] = heap[--size_], heap[size_] = 0;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
  return ret;
}
int main() {
  size_ = 1;
  while (scanf("%s", c), c[2] != 'd') {
    if (c[0] == 'i')
      scanf("%d", &val), insert(val);
    else
      printf("%d\\n", extract());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int val, size, heap[2097168];
char c[9];
inline void insert(int x) {
  int ptr = size++;
  heap[ptr] = x;
  while (ptr) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
}
inline int extract() {
  int ret = heap[1], ptr = 1;
  while ((ptr << 1) + 1 < size) {
    ptr <<= 1;
    if (heap[ptr] > heap[ptr + 1])
      swap(heap[ptr >> 1], heap[ptr]);
    else
      swap(heap[ptr + 1], heap[ptr >> 1]), ptr++;
  }
  heap[ptr] = heap[--size], heap[size] = 0;
  while (ptr) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
  return ret;
}
int main() {
  size = 1;
  while (scanf("%s", c), c[2] != 'd') {
    if (c[0] == 'i')
      scanf("%d", val), insert(val);
    else
      printf("%d\\n", extract());
  }
}