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

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

typedef long long ll;

void maxHeap(vector<ll> &h, int i) {
  int left = i * 2 + 1;
  int right = i * 2 + 2;
  int largest = i;
  if (left < h.size() && h[left] > h[largest])
    largest = left;
  if (right < h.size() && h[right] > h[largest])
    largest = right;

  if (largest != i) {
    swap(h[largest], h[i]);
    maxHeap(h, largest);
  }
}

void buildMaxHeap(vector<ll> &heap) {
  for (int i = heap.size() / 2; i >= 0; i--) {
    maxHeap(heap, i);
  }
}

void input(vector<ll> &heap, int i) {
  int sub = i + 1;
  if (sub <= 1)
    return;
  if (heap[sub - 1] > heap[sub / 2 - 1]) {
    swap(heap[sub - 1], heap[sub / 2 - 1]);
    input(heap, sub / 2 - 1);
  }
}

void printheap(const vector<ll> &h) {
  cout << "size: " << h.size() << endl;
  ;
  for (int i = 0; i < h.size(); i++)
    cout << " " << h[i];
  cout << "\\n";
}

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

  vector<ll> heap;
  string str;
  ll num;
  cin >> str;
  while (str != "end") {
    if (str == "insert") {
      cin >> num;
      heap.push_back(num);
      input(heap, heap.size() - 1);
    } else if (str == "print") {
      printheap(heap);
    } else {
      if (!heap.empty()) {
        cout << heap.front() << endl;
        heap[0] = heap.back();
        heap.pop_back();
        maxHeap(heap, 0);
      }
    }
    cin >> str;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

void maxHeap(vector<ll> &h, int i) {
  int left = i * 2 + 1;
  int right = i * 2 + 2;
  int largest = i;
  if (left < h.size() && h[left] > h[largest])
    largest = left;
  if (right < h.size() && h[right] > h[largest])
    largest = right;

  if (largest != i) {
    swap(h[largest], h[i]);
    maxHeap(h, largest);
  }
}

void buildMaxHeap(vector<ll> &heap) {
  for (int i = heap.size() / 2; i >= 0; i--) {
    maxHeap(heap, i);
  }
}

void input(vector<ll> &heap, int i) {
  int sub = i + 1;
  if (sub <= 1)
    return;
  if (heap[sub - 1] > heap[sub / 2 - 1]) {
    swap(heap[sub - 1], heap[sub / 2 - 1]);
    input(heap, sub / 2 - 1);
  }
}

void printheap(const vector<ll> &h) {
  cout << "size: " << h.size() << endl;
  ;
  for (int i = 0; i < h.size(); i++)
    cout << " " << h[i];
  cout << "\\n";
}

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

  vector<ll> heap;
  string str;
  ll num;
  cin >> str;
  while (str != "end") {
    if (str == "insert") {
      cin >> num;
      heap.push_back(num);
      input(heap, heap.size() - 1);
    } else if (str == "print") {
      printheap(heap);
    } else {
      if (!heap.empty()) {
        cout << heap.front() << endl;
        heap[0] = heap.back();
        heap.pop_back();
        buildMaxHeap(heap);
      }
    }
    cin >> str;
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
int main() {
  string num, name;
  int pick;
  priority_queue<int> pickles;
  while (cin >> num && num != "end") {
    if (num == "insert") {
      cin >> pick;
      pickles.push(pick);
    } else {
      cout << pickles.top() << endl;
      pickles.pop();
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  string num, name;
  int pick;
  vector<int> pickles;
  while (cin >> num && num != "end") {
    if (num == "insert") {
      cin >> pick;
      pickles.push_back(pick);
    } else {
      sort(pickles.begin(), pickles.end());
      cout << pickles.back() << endl;
      pickles.pop_back();
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

struct Heap {
  vector<int> Array;

  int pai(int input) { return (input - 1) / 2; }

  int filhoEsquerdo(int input) { return (2 * input + 1); }

  int filhoDireito(int input) { return (2 * input + 2); }

  // Arruma após inserção
  void HeapifyRemover(int arrumar) {
    int esquerdo = filhoEsquerdo(arrumar);
    int direito = filhoDireito(arrumar);

    int maior = arrumar;

    // Vejo quem é maior
    if (esquerdo < Array.size() && Array[esquerdo] > Array[arrumar]) {
      maior = esquerdo;
    }
    if (direito < Array.size() && Array[direito] > Array[maior]) {
      maior = direito;
    }
    if (maior != arrumar) {
      swap(Array[arrumar], Array[maior]);
      HeapifyRemover(maior);
    }
  }

  void HeapifyInserir(int arrumar) {
    if (arrumar && Array[pai(arrumar)] < Array[arrumar]) {
      swap(Array[pai(arrumar)], Array[arrumar]);
      HeapifyInserir(pai(arrumar));
    }
  }

  void inserirHeap(int valor) {
    Array.push_back(valor);
    HeapifyInserir(Array.size() - 1);
  }

  void printaRaiz() { printf("%i\\n", Array.at(0)); }

  int removerRaiz() {
    Array[0] = Array.back();
    Array.pop_back();
    HeapifyRemover(0);
  }
};

int main() {
  Heap heap;
  string operacao;
  int valor;
  while (cin >> operacao) {
    if (operacao == "insert") {
      cin >> valor;
      heap.inserirHeap(valor);
    } else if (operacao == "extract") {
      heap.printaRaiz();
      heap.removerRaiz();
    } else {
      break;
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>

using namespace std;

struct Heap {
  int Array[2000000];
  int tamanho;

  int pai(int input) { return (input - 1) / 2; }

  int filhoEsquerdo(int input) { return (2 * input + 1); }

  int filhoDireito(int input) { return (2 * input + 2); }

  // Arruma após inserção
  void HeapifyRemover(int arrumar) {
    int esquerdo = filhoEsquerdo(arrumar);
    int direito = filhoDireito(arrumar);

    int maior = arrumar;

    // Vejo quem é maior
    if (esquerdo < tamanho && Array[esquerdo] > Array[arrumar]) {
      maior = esquerdo;
    }
    if (direito < tamanho && Array[direito] > Array[arrumar]) {
      maior = direito;
    }
    if (maior != arrumar) {
      swap(Array[arrumar], Array[maior]);
      HeapifyRemover(maior);
    }
  }

  void HeapifyInserir(int arrumar) {
    if (arrumar && Array[pai(arrumar)] < Array[arrumar]) {
      swap(Array[pai(arrumar)], Array[arrumar]);
      HeapifyInserir(pai(arrumar));
    }
  }

  void inserirHeap(int valor) {
    Array[tamanho + 1] = valor;
    HeapifyInserir(tamanho);
    tamanho++;
  }

  int removerRaiz() {
    int raiz = Array[0];
    Array[0] = Array[tamanho - 1];
    tamanho--;
    return raiz;
  }
};

int main() {
  Heap heap;
  string operacao;
  int valor;
  while (cin >> operacao) {
    if (operacao == "insert") {
      cin >> valor;
      heap.inserirHeap(valor);
    } else if (operacao == "extract") {
      cout << heap.removerRaiz() << endl;
    } else {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define MAX 2000000

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

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

void maxHeapify(int *heap, int index, int heapIndex) {
  int l = left(index);
  int r = right(index);
  int largest;

  if (l <= heapIndex && heap[l] > heap[index]) {
    largest = l;
  } else {
    largest = index;
  }

  if (r <= heapIndex && heap[r] > heap[largest]) {
    largest = r;
  }

  if (index != largest) {
    swap(heap[largest], heap[index]);
    maxHeapify(heap, largest, heapIndex);
  }
}

void insert(int *heap, int key, int heapIndex) { heap[heapIndex] = key; }

void keyDelete(int *heap, int heapIndex) {
  swap(heap[1], heap[heapIndex]);
  printf("%d\\n", heap[heapIndex]);
}

void increaseKey(int *heap, int key, int index) {
  heap[index] = key;
  while (index > 1 && heap[index / 2] < heap[index]) {
    swap(heap[index / 2], heap[index]);
    index = index / 2;
  }
}

int main() {

  string str;
  int heap[MAX];
  int heapIndex = 0; // ヒープのキーの可変サイズ

  while (true) {
    cin >> str;
    if (str == "end") {
      break;
    } else if (str[0] == 'i') {
      int key;
      scanf("%d", &key);
      heapIndex++;
      increaseKey(heap, key, heapIndex);
    } else if (str[0] == 'e') {
      keyDelete(heap, heapIndex);
      heapIndex--;
      maxHeapify(heap, 1, heapIndex);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define MAX 2000000

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

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

void maxHeapify(int *heap, int index, int heapIndex) {
  int l = left(index);
  int r = right(index);
  int largest;

  if (l <= heapIndex && heap[l] > heap[index]) {
    largest = l;
  } else {
    largest = index;
  }

  if (r <= heapIndex && heap[r] > heap[largest]) {
    largest = r;
  }

  if (index != largest) {
    swap(heap[largest], heap[index]);
    maxHeapify(heap, largest, heapIndex);
  }
}

void buildMaxHeap(int *heap, int heapIndex) {
  for (int i = heapIndex / 2; i >= 1; i--) {
    maxHeapify(heap, i, heapIndex);
  }
}

void insert(int *heap, int key, int heapIndex) { heap[heapIndex] = key; }

void keyDelete(int *heap, int heapIndex) {
  swap(heap[1], heap[heapIndex]);
  printf("%d\\n", heap[heapIndex]);
}

int main() {

  string str;
  int heap[MAX];
  int heapIndex = 1; // ヒープのキーの可変サイズ

  while (true) {
    cin >> str;
    if (str == "end") {
      break;
    } else if (str[0] == 'i') {
      int key;
      scanf("%d", &key);
      insert(heap, key, heapIndex);
      // buildMaxHeap(heap, heapIndex);
      heapIndex++;
    } else if (str[0] == 'e') {
      buildMaxHeap(heap, --heapIndex);
      keyDelete(heap, heapIndex);
      //  buildMaxHeap(heap,heapIndex-1);
    }
  }

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

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>

const int MAX_H = 100000;

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

  std::priority_queue<int> *pq = new std::priority_queue<int>();

  std::string command;
  std::cin >> command;

  while (command != "end") {
    if (command[0] == 'i') {
      int k = 0;

      std::cin >> k;

      pq->push(k);
    } else if (command[0] == 'e') {
      int v = pq->top();
      pq->pop();

      std::cout << v << std::endl;
    }

    std::cin >> command;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int MAX_H = 2000000;

void swap(std::vector<int> *A, int i, int j) {
  int tmp = A->at(i);
  A->at(i) = A->at(j);
  A->at(j) = tmp;
}

void maxHeapify(std::vector<int> *A, int i, int H) {
  int leftIndex = 2 * i;
  int rightIndex = 2 * i + 1;

  int largest = i;

  if ((leftIndex <= H) && (A->at(leftIndex) > A->at(i))) {
    largest = leftIndex;
  }

  if ((rightIndex <= H) && (A->at(rightIndex) > A->at(largest))) {
    largest = rightIndex;
  }

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

void buildMaxHeap(std::vector<int> *A, int H) {
  if (H == 1) {
    return;
  }

  for (int i = H / 2; i >= 1; i--) {
    maxHeapify(A, i, H);
  }
}

int main(void) {
  std::ios::sync_with_stdio(false);

  int H = 0;
  std::vector<int> *A = new std::vector<int>(MAX_H);

  std::string command;
  std::cin >> command;

  while (command != "end") {
    if (command[0] == 'i') {
      H++;

      int k = 0;

      std::cin >> k;

      A->at(H) = k;

      buildMaxHeap(A, H);
    } else if (command[0] == 'e') {
      if (H == 0) {
        continue;
      }

      std::cout << A->at(1) << std::endl;

      A->at(1) = A->at(H);
      H--;

      buildMaxHeap(A, H);
    }

    std::cin >> command;
  }

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

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  priority_queue<int> pq;
  string ope;
  int num;
  cin >> ope;
  while (ope != "end") {
    if (ope == "extract") {
      cout << pq.top() << endl;
      pq.pop();
    } else if (ope == "insert") {
      cin >> num;
      pq.push(num);
    }
    cin >> ope;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  priority_queue<int> pq;
  string ope;
  int num;
  cin >> ope;
  while (ope != "end") {
    if (ope == "extract") {
      cout << pq.top() << endl;
      pq.pop();
    } else if (ope == "insert") {
      cin >> num;
      pq.push(num);
    }
    cin >> ope >> num;
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long H = 0;
long A[2000000];

void maxHeapify(long i) {
  long largest;
  long l = 2 * i;
  long r = 2 * i + 1;

  if (l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;

  if (r <= H && A[r] > A[largest])
    largest = r;

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

long extractMax() {
  long c_max = A[1];
  swap(A[1], A[H]);
  H--;
  maxHeapify(1);
  return c_max;
}

void insert(long k) {
  A[++H] = k;
  long i = H;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i / 2], A[i]);
    i /= 2;
  }
}

int main() {
  string op;
  cin >> op;

  while (op != "end") {
    if (op[0] == 'i') {
      long k;
      cin >> k;
      insert(k);
    } else {
      cout << extractMax() << endl;
    }
    cin >> op;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

long H = 0;
long A[2000000];

void maxHeapify(long i) {
  long largest;
  long l = 2 * i;
  long r = 2 * i + 1;

  if (l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;

  if (r <= H && A[r] > A[largest])
    largest = r;

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

long extractMax() {
  long c_max = A[1];
  swap(A[1], A[H]);
  A[H--] = numeric_limits<long>::min();
  maxHeapify(1);
  return c_max;
}

void insert(long k) {
  A[++H] = k;
  for (long i = H / 2; i > 0; i--)
    maxHeapify(i);
}

int main() {
  string op;
  cin >> op;

  while (op != "end") {
    if (op[0] == 'i') {
      long k;
      cin >> k;
      insert(k);
    } else {
      cout << extractMax() << endl;
    }
    cin >> op;
  }

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

#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_V 5000000
int heap[MAX_V]{0};
int heap_size = 0;

int left(int i) { return 2 * i + 1; }
int right(int i) { return 2 * i + 2; }
int parent(int i) { return (i - 1) / 2; }
bool has_left(int i) { return heap_size > left(i); }
bool has_right(int i) { return heap_size > right(i); }

void max_heapify(int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  if (has_left(i) && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if (has_right(i) && heap[r] > heap[largest])
    largest = r;
  if (largest != i) {
    swap(heap[i], heap[largest]);
    max_heapify(largest);
  }
}
void heap_increase_key(int i, int key) {
  if (key < heap[i])
    return;
  heap[i] = key;
  while (i > 0 && heap[parent(i)] < heap[i]) {
    swap(heap[i], heap[parent(i)]);
    i = parent(i);
  }
}
void max_heap_insert(int key) {
  heap[heap_size] = 0x80000000;
  heap_size++;
  heap_increase_key(heap_size - 1, key);
}
int heap_extract_max() {
  if (heap_size < 1)
    return -1;
  int max = heap[0];
  heap[0] = heap[--heap_size];
  max_heapify(0);
  return max;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (;;) {
    string str;
    cin >> str;
    if (str == "end")
      break;
    else if (str == "insert") {
      int key;
      cin >> key;
      max_heap_insert(key);
    } else if (str == "extract") {
      int item = heap_extract_max();
      cout << item << "\\n";
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_V 5000000
int heap[MAX_V]{0};
int heap_size = 0;

int left(int i) { return 2 * i + 1; }
int right(int i) { return 2 * i + 2; }
int parent(int i) { return (i - 1) / 2; }
bool has_left(int i) { return heap_size > left(i); }
bool has_right(int i) { return heap_size > right(i); }

void max_heapify(int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  if (has_left(i) && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if (has_right(i) && heap[r] > heap[largest])
    largest = r;
  if (largest != i) {
    swap(heap[i], heap[largest]);
    max_heapify(largest);
  }
}
void heap_increase_key(int i, int key) {
  if (key < heap[i])
    return;
  heap[i] = key;
  while (i > 0 && heap[parent(i)] < heap[i]) {
    swap(heap[i], heap[parent(i)]);
    i = parent(i);
  }
}
void max_heap_insert(int key) {
  heap[heap_size] = 0x80000000;
  heap_size++;
  heap_increase_key(heap_size - 1, key);
}
int heap_extract_max() {
  if (heap_size < 1)
    return -1;
  int max = heap[0];
  heap[0] = heap[--heap_size];
  max_heapify(0);
  return max;
}
int main() {
  for (;;) {
    string str;
    cin >> str;
    if (str == "end")
      break;
    else if (str == "insert") {
      int key;
      cin >> key;
      max_heap_insert(key);
    } else if (str == "extract") {
      int item = heap_extract_max();
      cout << item << "\\n";
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
const int MAX = 2000000;

void maxHeapify(int *A, int i) {
  int l = 2 * i, r = 2 * i + 1, largest;
  if (l <= n && A[l - 1] > A[i - 1])
    largest = l;
  else
    largest = i;
  if (r <= n && A[r - 1] > A[largest - 1])
    largest = r;
  if (largest != i) {
    swap(A[i - 1], A[largest - 1]);
    maxHeapify(A, largest);
  }
}

void heapIncreaseKey(int *A, int i, int key) {
  if (key < A[i - 1]) {
    cout << "Exception: the new key is smaller than the present key." << endl;
    return;
  }
  A[i - 1] = key;
  while (i > 1 && A[i / 2 - 1] < A[i - 1]) {
    swap(A[i / 2 - 1], A[i - 1]);
    i = i / 2;
  }
}

void maxHeapInsert(int *A, int key) {
  A[n++] = -1;
  heapIncreaseKey(A, n, key);
}

int heapExtractMax(int *A) {
  if (n < 1) {
    cout << "Exception: Heap Under Flow" << endl;
  } else {
    int max = A[0];
    A[0] = A[--n];
    maxHeapify(A, 1);
    return max;
  }
}

int main() {
  static int A[MAX];
  n = 0;
  char str[10];
  while (scanf("%s", str)) {
    if (str[0] == 'e' && str[1] == 'n')
      break;
    else if (str[0] == 'i') {
      int a;
      cin >> a;
      maxHeapInsert(A, a);
    } else
      printf("%d\\n", heapExtractMax(A));
  }
}

Error: TLE

Fixed submission:

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

int n;
const int MAX = 2000000;

void maxHeapify(int *A, int i) {
  int l = 2 * i, r = 2 * i + 1, largest;
  if (l <= n && A[l - 1] > A[i - 1])
    largest = l;
  else
    largest = i;
  if (r <= n && A[r - 1] > A[largest - 1])
    largest = r;
  if (largest != i) {
    swap(A[i - 1], A[largest - 1]);
    maxHeapify(A, largest);
  }
}

void heapIncreaseKey(int *A, int i, int key) {
  if (key < A[i - 1]) {
    cout << "Exception: the new key is smaller than the present key." << endl;
    return;
  }
  A[i - 1] = key;
  while (i > 1 && A[i / 2 - 1] < A[i - 1]) {
    swap(A[i / 2 - 1], A[i - 1]);
    i = i / 2;
  }
}

void maxHeapInsert(int *A, int key) {
  A[n++] = -1;
  heapIncreaseKey(A, n, key);
}

int heapExtractMax(int *A) {
  if (n < 1) {
    cout << "Exception: Heap Under Flow" << endl;
  } else {
    int max = A[0];
    A[0] = A[--n];
    maxHeapify(A, 1);
    return max;
  }
}

int main() {
  static int A[MAX];
  n = 0;
  string str;
  while (cin >> str && str != "end") {
    if (str == "insert") {
      int a;
      cin >> a;
      maxHeapInsert(A, a);
    } else
      cout << heapExtractMax(A) << endl;
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

// ALDS 1_9_C
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {

  priority_queue<int> S;
  string cmd;
  int key;

  while (1) {
    cin >> cmd;

    if (cmd == "insert") {
      cin >> key;
      S.push(key);
    }

    if (cmd == "extract") {
      cout << S.top() << endl;
      S.pop();
    }

    if (cmd == "end")
      break;
  }

  return 0;
}

Error: TLE

Fixed submission:

// ALDS 1_9_C
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {

  vector<int> S;
  string cmd;
  int key;

  while (1) {
    cin >> cmd;

    if (cmd == "insert") {
      cin >> key;
      S.push_back(key);
      sort(S.begin(), S.end());
    }

    if (cmd == "extract") {
      cout << S[S.size() - 1] << endl;
      S.pop_back();
    }

    if (cmd == "end")
      break;
  }

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

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

int main() {
  string oper;
  int n;
  priority_queue<int> Q;
  cin >> oper;
  while (oper != "end") {
    if (oper == "insert") {
      cin >> n;
      Q.push(n);
    } else if (!Q.empty() && oper == "extract") {
      // n = Q.top();
      cout << Q.top() << endl;
      Q.pop();
    }
    cin >> oper;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  string oper;
  int n;
  priority_queue<int> Q;
  cin >> oper >> n;
  while (oper != "end") {
    if (oper == "insert") {
      Q.push(n);
    } else if (!Q.empty() && oper == "extract") {
      n = Q.top();
      cout << n << endl;
      Q.pop();
    }
    cin >> oper >> n;
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
  string cmd;
  priority_queue<int> Q;
  while (cin >> cmd && cmd != "end") {
    if (cmd == "insert") {
      int k;
      cin >> k;
      Q.push(k);
    } else if (cmd == "extract") {
      int n = Q.top();
      Q.pop();
      cout << n << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {
  string oper;
  int n;
  priority_queue<int> Q;
  cin >> oper >> n;
  while (oper != "end") {
    if (oper == "insert") {
      Q.push(n);
    }
    if (!Q.empty() && oper == "extract") {
      cout << Q.top() << endl;
      Q.pop();
    }
    cin >> oper >> n;
  }
}