#include<stdio.h>
#include<stdlib.h>
struct Node{
    int data;
    struct Node*left;
    struct Node*right;
    int height;
}Node;
int max(int a,int b)
{
    return(a > b) ? a:b;
}
int height(struct Node *N)
{
    if(N == NULL)
    return 0;
    return N->height;
}
struct Node* newNode(int data)
{
   struct Node* Node=(struct Node*)malloc(sizeof(Node));
   Node->data=data;
    Node->left=NULL;
    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(y->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){
    if(N==NULL)
    return 0;
    return height(N->left)-height(N->right);
}
struct Node* insert(struct Node* node,int data){
    if(node ==NULL)
    return newNode(data);
    if(data<node->data)
    node->left=insert(node->left,data);
    else if(data>node->data)
    node->right=insert(node->right,data);
    else
    return node;
    node->height=1 + max(height(node->left),height(node->right));
    int balance=getBalance(node);
    if(balance>1 && data< node->left->data)
    return rightRotate(node);
    if(balance< 1 && data>node->right->data)
    return leftRotate(node);
    if(balance > 1 && data > node->right->data){
        node->left = leftRotate(node->left);
        return leftRotate(node);
    }
    if(balance <-1&& data <node->right->data){
        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 data){
    if(root ==NULL)
    return root;
    if(data<root->data)
    root->left =deleteNode(root->right,data);
    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->data=temp->data;
            root->right=deleteNode(root->right,temp->data);
        }
    }
    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->data);
        preOrder(root->left);
        preOrder(root->right);
    }
}
int isNumber(char*str){
    for(int i=0;str[i]!='\0';i++){
        if (!digit(str[i]) && !(i == 0 && (str[i] =='-'|| str[i] =='+')))
        return 0;
    }
    return 1;
}
int main(){
    int n,x;
    char input1[1000],input2[1000];
    if(fgets(input1,sizeof(input1),stdin)==NULL||fgets(input2,sizeof(input2),stdin)==NULL){
        printf("Invalid input");
        return 0;
    }
    struct Node* root = NULL;
    int product;
    char *ptr = input2;
    for(int i=0;i<n;i++)
{
    if(sscanf(ptr,"%d", &product) !=1||product<0 ||product>1000){
        printf("Invalid input");
        return 0;
    }
    root = insert(root,product);
    while(*ptr && *ptr!=' ')ptr++;
    while(*ptr == ' ')ptr++;
}
root = deleteNode(root,x);
preOrder(root);
printf("\n");
return 0;
}