#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int data;
    struct node *left,*right;
}s1;

s1* create(int num){
    s1* new=(s1*)malloc(sizeof(s1));
    new->data=num;
    new->right=new->left=NULL;
    return new;
}

s1* insert(s1 * root,int num){
    if(root==NULL){
        return create(num);
    }else if(num<root->data){
        return insert(root->left,num);
    }else{
        return insert(root->right,num);
    }
    return root;
    
}

s1* minimum(s1* root){
    if(root==NULL){
        return root;
    }if(root!=NULL){
        return minimum(root->left);
    }
    return root;
}

s1* delete(s1* root,int val){
    if(root->data>val){
         root->left=delete(root->left,val);
    }else if(num>root->data){
        root->root=delete(root->right,val);
    }else{
        if(root->left==NULL&&root->right==NULL)
            return NULL;
        else if(root->left!=NULL&&root->right==NULL)
            return root->left;
        else if(root->left==NULL&&root->right!=NULL)
            return root->right;
        else{
            s1* temp=minimum(root->right);
            root->data=temp->data;
            root->right=delete(root->right,temp->data);
        }
    }
}

int main(){
    int size,val,num;
    s1* root;
    scanf("%d",&size);
    for(int i=0;i<size;i++){
        scanf("%d",&num);
        root=insert(root,num);
    }
    scanf("%d",&val);
    delete(root,val);
}