Describe your issue
I’m trying to solve this problem and I think my solution is correct. It works for sample test cases, but I get a WA verdict on hidden test cases. AI says it’s correct as well. I can’t see what’s wrong with it. Here’s my solution:
#include <bits/stdc++.h>
int main(int argc, char const *argv[]) {
int N;
std::cin >> N;
std::cin.ignore(1, '\n');
/*
======================================
Write a program to perform the
following operations:
1) Initialize an array
2) Given an integer N, perform N
queries over this array.
3) Given a query "+ x", insert x
in your array.
4) Given a query "-", remove the
maximum element of the array.
5) After each query print the maximum
element left in the array.
======================================
*/
std::priority_queue<int> heap;
for (int n = 0 ; n < N; n += 1) {
std::string query;
std::getline(std::cin, query, '\n');
int operand; char operation = query[0];
if (operation == '+') operand = std::stoi(query.substr(1, query.length() - 1));
if (operation == '-') operand = 0;
if (operation == '+') { heap.push(operand); }
if (operation == '-') { heap.pop(); }
if (!heap.empty()) std::cout << heap.top() << std::endl;
}
return 0;
}
Help me solve this please, I’ve spent hours thinking what might be wrong with the code but I have no clue anymore. I’ve tried it with C++17 and C++20. Clicking other people’s solutions give me an Error 403. And there’s no solution provided in the problem itself.