#include<stdio.h>
#include<stdlib.h>

struct node{
    int key;
    struct node*left,*right;
    int height;
};

int height(struct node*n)
{
    return (n==NULL)?0:n->height;
}

int max(int a,int b)
{
    return (a>b)?a:b;
}

struct node*newnode(int key)
{
    struct node*node=(struct node*)malloc(sizeof(struct node));
    node->key=key;
    node->left=node->right=NULL;
    node->height=1;
    return node;
}

struct node*rightrotate(struct node*y)
{
    struct node*x=y->left;
    struct node*t2=x->right;
    x->right=y;
    y->left=t2;
    y->height=max(height(y->left),height(y->right))+1;
    x->height=max(height(x->left),height(x->right))+1;
    return x;
}

struct node*leftrotate(struct node*x)
{
    struct node*y=x->right;
    struct node*t2=y->left;
    y->left=x;
    x->right=t2;
    x->height=max(height(x->left),height(x->right))+1;
    y->height=max(height(y->left),height(y->right))+1;
    return y;
}

int getbalance(struct node*n)
{
    return (n==NULL)?0:height(n->left)-height(n->right);
    
}

struct node*insert(struct node*node,int key)
{
    if(node==NULL)
    {
        return newnode(key);
    }
    if(key<node->key)
    {
        node->left=insert(node->left,key);
    }
    else if(key>node->key)
    {
        node->right=insert(node->right,key);
    }
    else{
        return node;
    }
    node->height=1+max(height(node->left),height(node->right));
    int balance=getbalance(node);
    
    if(balance>1&&key<node->left->key)
    {
        return rightrotate(node);
    }
    if(balance<-1&&key>node->right->key)
    {
        return leftrotate(node);
    }
    if(balance>1&&key>node->left->key)
    {
        node->left=leftrotate(node->left);
        return rightrotate(node);
    }
    if(balance<-1&&key<node->right->key)
    {
        node->right=rightrotate(node->right);
        return leftrotate(node);
    }
    return node;
}

struct node*minvaluenode(struct node*node)
{
    struct node*current=node;
    while(current->left!=NULL)
    {
        current=current->left;
    }
    return current;
}

struct node*deletenode(struct node*root,int key)
{
    if(root==NULL)
    {
        return root;
    }
    if(key<root->key)
    {
        root->left=deletenode(root->left,key);
        
    }
    else if(key>root->key){
        root->right=deletenode(root->right,key);
        
    }
    else{
        if((root->left==NULL)||(root->right==NULL))
        {
            struct node*temp=root->left?root->left:root->right;
            if(temp==NULL)
            {
                temp=root;
                root=NULL;
            }
            else{
                *root=*temp;
                free(temp);
            }
        }
        else{
            struct node*temp=minvaluenode(root->right);
            root->key=temp->key;
            root->right=deletenode(root->right,temp->key);
        }
    }
    if(root==NULL)
    {
        return root;
    }
    root->height=1+max(height(root->left),height(root->right));
    int balance=getbalance(root);
    
    if(balance>1&&getbalance(root->left)>=0)
    {
        return rightrotate(root);
    }
    if(balance>1&&getbalance(root->left)<0)
    {
        root->left=leftrotate(root->left);
        return rightrotate(root);
    }
    if(balance<-1&&getbalance(root->right)<=0)
    {
        return leftrotate(root);
    }
    if(balance<-1&&getbalance(root->right)>0)
    {
        root->right=rightrotate(root->right);
        return leftrotate(root);
    }
    return root;
}
void preorder(struct node*root)
{
    if(root!=NULL)
    {
        printf("%d ",root->right);
        preorder(root->key);
        preorder(root->left);
        
    }
}

int main()
{
    int n,del;
    if(scanf("%d %d",&n,&del)!=2||n<=0)
    {
        printf("Invalid input");
        return 0;
    }
    
    struct node*root=NULL;
    for(int i=0;i<n;i++)
    {
        int id;
        if(scanf("%d",&id)!=1||id<0||id>1000)
        {
            printf("Invalid input");
            return 0;
        }
        root=insert(root,id);
    }
    root=deletenode(root,del);
    preorder(root);
    return 0;
}