#include
using namespace std;
class node{
public:
int data;
node* next;
//Constructor
node(int d){
data = d;
next = NULL;
}
};
void insertAtTail(node*&head,int data){
if(head==NULL){
head = new node(data);
return;
}
node*tail = head;
while(tail->next!=NULL){
tail = tail->next;
}
tail->next = new node(data);
return;
}
void kappend(node &head,int k,int n){
nodeappend=head;
node *tail=head;
int p=1;
while(p<=(n-k-1)){
append=append->next;
p++;
}
while(tail->next!=NULL){
tail=tail->next;
}
tail->next=head;
head=append->next;
append->next=NULL;
}
void buildlist(node*&head,int n){
int data,count=0;
cin>>data;
while(count<n){
insertAtTail(head,data);
cin>>data;
count++;
}
}
void print(nodehead){
//nodetemp = head;
while(head!=NULL){
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;
}
ostream& operator << (ostream& os,nodehead){
print(head);
return os;
}
int main() {
nodehead = NULL;
int n,k;
cin>>n;
buildlist(head,n);
cin>>k;
kappend(head,k,n);
cout<<head;
//cout<<"Hello World!";
}