#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node *left, *right;
}Node;
Node *root = NULL;
Node* create(int num){
    Node *newnode = (Node*) malloc (sizeof(Node));
    newnode->data = num;
    newnode->left = NULL;
    newnode->right = NULL;
    return newnode;
}

Node *insert(Node*root,int num){
    if(root == NULL){
        return create(num);
    }
    
}

Node *minimun(Node *root){
    if(root == NULL)
        return root;
    if(root->left = NULL)
        return minimun(root->left);
    return root;
}

Node *deletion(Node *root,int num){
    if(root == NULL)
        return root;
    if(num<root->data)
        root-.left = deletion(root->left, num);
    if(num>root->data)
        root->right  = deletion(root->right, num);
    else{
        if(root->left == NULL && root->right == NULL)
            return NULL;
        else if(root->left == NULL && root->tight == NULL)
            return root->left;
        else if(root->left == NULL && root->right != NULL)
            return root->right;
        else{
            Node *temp = minimum(root->right);
            root->data = temp->data;
            root->right = deletion(root->right,temp->data);
        }
    }
    return root;
}

void inOrder(Node *num){
    if(root != NULL){
        inOrder(root->left);
        printf("%d",root->data);
        inOrder(root->right);
    }
}

int main(){
    int size,num,itr,val;   
    scanf("%d",&size);
    for(itr=1;itr<=size;itr++){
        scanf("%d",&num);
        root = insert(root, num);
    }
    scanf("%d",&val);
    if(search(root,val) == NULL){
        printf("-1");
        return 0;
    }
    root = deletion(root, val);
    inOrder(root);
}