#include<stdio.h>
#include<stdlib.h>
struct Node{
    int data;
    struct Node*prev;
    struct Node*next;
};
struct Node*createNode(int data){
    struct Node*newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=data;
    newNode->prev=NULL;
    newNode->next=NULL;
    return newNode;
}
void append(struct Node**head,int data){
    struct Node*newNode=createNode(data);
    if(*head==NULL){
        *head=newNode;
        return;
    }
    struct Node*temp=*head;
    while (temp->next!=NULL){
        temp=temp->next;
    }
    temp->next=newNode;
    newNode->prev=temp;
}
void insertAtPosition(struct Node**head,int pos,int value,int n){
    if(pos<0||pos>n){
        printf("Invalid input\n");
        exit(0);
    }
    struct Node*newNode=createNode(value);
    if(pos==0){
        newNode->=*head;
        if(*head!=NULL)
        (*head)->prev=newNode;
        *head=newNode;
        return;
    }
    struct Node*temp=*head;
    int index=0;
    while(temp!=NULL&&index<pos-1){
        temp=temp->next;
        index++;
    }
    if(temp->next==NULL&&pos==n){
        temp->next=newNode;
        newNode->prev=temp;
        return;
    }
    struct Node*current=temp->next;
    temp->next=newNode;
    newNode->prev=temp;
    newNode->next=current;
    if(current!=NULL)
    current->prev=newNode;
}
void printList(struct Node*head){
    struct Node*temp=head;
    while (temp!=NULL){
        printf("%d ",temp->data);
        temp=temp-> next;
    }
    printf("\n");
}
int main(){
        int n;
        if(scanf("%d",&n)!=1||n<1||n>1000){
            printf("Invalid input\n");
            return 0;
        }
        struct Node*head=NULL;
        for(int i=0;i<n;i++){
            int value;
            if(scanf("%d",&value)!=1){
                printf("Invalid input\n");
                return 0;
            }
            append(&head,value);
        }
        int pos,value;
        if(scanf("%d",&pos)!=1||scanf("%d",&value)!=1){
            printf("Invalid input\n");
            return 0;
        }
        insertAtPosition(&head,pos,value,n);
        printList(head);
        return 0;
    }
}