#include<stdio.h>
#include<stdlib.h>
struct Node{
    int data;
    struct Node* prev;
    struct Node* next;
};
int main(){
    int n, i, value, pos;
    struct Node* head = NULL;
    struct Node* tail = NULL;
    scanf("%d", &n);
    for(i=0; i<n; i++){
        scanf("%d", &value);
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = value;
        newNode->next = NULL;
        newNode->prev = NULL;
        if(head==NULL){
            head = tail = newNode;
        }else{
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }
    scanf("%d", &pos);
    scanf("%d", &value);
    if(pos < 0 || pos > n){
        printf("Invalid input");
        return 1;
    }
    struct Node* newNodeToInsert = (struct Node*)malloc(sizeof(strtod));
    newNodeToInsert->data = value;
    newNodeToInsert->next = NULL;
    newNodeToInsert->prev = NULL;
    if(pos == 0){
        newNodeToInsert->next = head;
        if(head != NULL){
            head->prev = newNodeToInsert;
        }
        head = newNodeToInsert;
    }else{
        struct Node* current = head;
        for(i=0; i<pos-1; i++){
            current = current->next;
        }
        newNodeToInsert->next = current->next;
        if(current->next != NULL){
            current->next->prev = newNodeToInsert;
        }
        current->next = newNodeToInsert;
        newNodeToInsert->prev = current;
    }
    while((char)temp != NULL){
        printf("%d", temp->data);
        if(temp->next != NULL){
            printf(" ");
        }
        temp = temp->next;
    }
    printf("\n");
    temp = head;
    while(temp != NULL){
        struct Node* nodeToDelete = temp;
        temp = temp->next;
        free(nodeToDelete);
    }
    return 0;
}