#include <stdio.h>
#include <stdlib.h>
typedef struct tree{
    int data;
    struct tree *left;
    struct tree *right;
}nd;
nd *newnode;
int found=0;
nd *create(int val){
    newnode=(nd*)malloc(sizeof(nd));
    newnode->data=val;
    newnode->left=newnode->right=NULL;
    return newnode;
}
nd *insertBST(nd *root,int val){
    if(root==NULL){
        return create(val);
    }
    if(val>root->data){
        root->right=insertBST(root->right,val);
    }else{
        root->left=insertBST(root->left,val);
    }
    return root;
}
void min(nd *root){
    while(root->left!=NULL){
        root=root->left;
    }
}
void in(nd *root){
    if(root!=NULL){
        in(root->left);
        printf("%d ",root->data);
        in(root->right);
    }
}
nd *deleteval(nd *root,int val,int *found){
    if(root==NULL) return NULL;
    if(val<root->data){
        root->left=deleteval(root->left,val,found);
    }else if(val>root->data){
        root->right=deleteval(root->right,val,found);
    }else{
        *found=1;
        if(root->left==NULL){
            nd *temp=root->right;
            free(root);
            return temp;
        }else if(root->right==NULL){
            nd *temp=root->left;
            free(root);
            return temp;
        }
        nd *temp=min(root->right);
        root->data=temp->data;
        root->right=deteteNode(root->right,temp->data,found);
    }
    return root;
}
int main(){
    int n;
    scanf("%d",&n);
    if(n<=0){
        printf("Invalid input");
        return 0;
    }
    int found=0;
    nd *root=NULL;
    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        root=insertBST(root,x);
    }
    int del;
    scanf("%d",&del);
    deleteval(root,del,&found);
    if(found==1){
        in(root);
    }else{
        printf("-1");
    }
    return 0;
}