#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next; 
};
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 
    newNode->data=value;
    newNode->next=NULL;
    return newNode;
}
void deleteLast(struct Node** head,int k){
    int len =0;
    struct Node*temp=*head;
    while(temp!=NULL){
        len++
        temp=temp->next;
    }
    if(k>len){
        printf("Invalid input");
        return;
    }
    if(k==len){
        printf("empty list");
        return;
    }
    int stop=len-k;
    temp=*head;
    for(int i=1;i<stop;i++){
        temp=temp->next;
    }
    struct Node*del=temp->next;
    temp->next=NULL;
    while (del!=NULL){
        struct Node*t=del;
        del=del->next;
        free(t);
    }
    temp=*head;
    while(temp!=NULL){
        printf("%d ",temp->data);
        if(temp->next!=NULL) printf(" ");
        temp =temp->next;
    }
    printf("\n");
}
int main(){
    int n;
    scanf("%d ",&n);
    struct Node*head=NULL;
    struct Node*tail=NULL;
    for(int i=0;i<n;i++){
        int id;
        scanf("%d",&ld);
        struct Node* newNode=createNode(ld);
        if(head=newNode);{
            head=newNode;
            tail=newNode;
        }else{
            tail->next=newNode;
            tail->newNode;
        }
    }
    int k;
    scanf("%d",&k);
    if(head->next==NULL&&k==1){
        printf("List is empty");
        return 0;
    }
    deleteLast(&head,k);
    struct Node*temp=head;
    while(temp!=NULL){
        struct Node*t=temp;
        temp=temp->next;
        free(t);
    }
    return 0;
}