#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode{
    int data;
    struct TreeNode *left;
    struct TreeNode *right;
}Node;
Node *root=NULL;
Node *create(int val){
    Node *newnode=(Node*)malloc(sizeof(Node));
    newnode->data=vai;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}
Node *buildTree(int arr[],int n){
    if(n==0)return NULL;
    Node *queue[200];
    Node *root=create(arr[i++]);
    queue[rear++]=root;
    while(i<n){
        Node *temp=queue[front++];
        temp->left=create(arr[i++]);
        queue[rear++]=temp->left;
        if(i<n){
            temp->right=create(arr[i++]);
            queue[rear++]=temp->right;
        }
    }
    return root;
}
void update(Node *root, int pos, int val){
    Node *queue[200];
    int front=0,rear=0,count=1;
    queue[rear++]=root;
    while(front<rear){
        Node *temp=queue[front++];
        if(count==pos){
            temp->data=val;
            return;
        }
        count++;
        if(temp->left)queue[rear++] = temp->left;
        if(temp->right)queue[rear++] = temp->right;
        }
    }
    
void print(Node *root){
    Node *queue[200];
    int front=0,rear=0,count=1;
    queue[rear++]=root;
    while(front<rear){
        Node *temp=queue[front++];
        printf("%d ",temp->data);
        if(temp->left)queue[rear++] = temp->left;
        if(temp->right)queue[rear++] = temp->right;
    }
    
}
int main(){
    int n;
    scanf("%d",&n);
    if(n<0){
        printf("Invalid input");
        return 0;
    }
    int arr[n];
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    int pos,val;
    scanf("%d %d",&pos,&val);
    if(pos<1 || pos>n){
        printf("Invalid input");
        return 0;
    }
    root=buildTree(arr,n);
    update(root,pos,val);
    print(root);
    return 0;
    
}