#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,total=0,head;
    
    cin>>n;
    
    vector<int> RQ(n);
    
    for(int i=0;i<n;i++){
        cin>>RQ[i];
    }
    
    cin>>head;
    
    int direction; //1-Right 2-left
    
    cin>>direction;
    
    vector<int> right,left;
    
    for(int i=0;i<n;i++){
        if(RQ[i]<head){
            right.push_back(RQ[i]);
        }
        else
            left.push_back(RQ[i]);
    }
    
    sort(right.begin(),right.end());
    sort(left.begin(),left.end());
    
    vector<int> Order;
    if (direction==1){
        for(int i=0;i<right.size();i++){
            order.push_back(right[i]);
            total+=abs(right[i]-head);
            head=right[i];
        }
        
        for(int i=left.size()-1;i>=0;i--){
            order.push_back(left[i]);
            total+=abs(left[i]-head);
            head=left[i];
        }
    }
    
    if (direction==0){
        for(int i=left.size()-1;i>=0;i--){
            order.push_back(left[i]);
            total+=abs(left[i]-head);
            head=left[i];
        }
        for(int i=0;i<right.size();i++){
            order.push_back(right[i]);
            total+=abs(right[i]-head);
            head=right[i];
        }
    }
    for(int x:order){
        cout<<x<<"->";
    }
    cout<<endl;
    cout<<total;
}