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

void minHeapify(vector<int> &vec, int i, int& n){
    int smallest = i;
    int left = 2 * i + 1, right = left + 1;
    
    if(left < n && vec[left] < vec[smallest])
        smallest = left;
        
    if(right < n && vec[right] < vec[smallest])
        smallest = right;
    
    if(smallest ^ i){
        swap(vec[i], vec[smallest]);
        minHeapify(vec, smallest, n);
    }
}

int main()
{
    int k, val, heapSize = 0; cin>>k;
    
    vector<int> vec;
    
    while(true){
        cin>>val;
        
        if(val == -1)
            break;
        
        if(heapSize < k){
            vec.push_back(val);
            ++heapSize;
        }
        else if(val > vec[0]){
	        vec[0] = val;
        }
        
        minHeapify(vec, 0, heapSize);
        
        if(heapSize == k)
            cout<<vec[0]<<" ";
        else
            cout<<"N ";
    }
    
    cout<<"\nThe elements in the heap:\n";
    for(auto &val: vec)
    	cout<<val<<" ";
    	
    return 0;
}