This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
bool s = 1;
priority_queue<long long> heap[9];
vector<long long> ans;
long long n,p,v;
int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> p >> v;
		heap[p].push(-v);
	}
	
	if(heap[5].size() + heap[6].size() != 1 || heap[7].size() + heap[8].size() != 1)
		goto CANNOT;
	
	if(heap[5].size()){
		s ^= 1;//   ->
		ans.push_back(-heap[5].top());
	}
	else{
		ans.push_back(-heap[6].top());
	}
		
	while(heap[1].size() + heap[4].size()){
		if(s == 1){
			
			if(heap[1].size() == 0)
				goto CANNOT;
			
			ans.push_back(-heap[1].top());
			heap[1].pop();
		} else{
			
			if(heap[4].size() == 0)
				goto CANNOT;
			
			ans.push_back(-heap[4].top());
			heap[4].pop();
		}
		s ^= 1;
	}
	
	if((s == 0? heap[8].size() == 0 : heap[7].size() == 0) )
		goto CANNOT;
		
	ans.push_back(-(heap[8].size() ? heap[8].top() : heap[7].top() ) );
	
	for(int e : ans)
		cout << e << " ";
	return 0;
	CANNOT: ;
	cout << -1;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |