#include <iostream>
using namespace std;

struct Elem
{
    int num;
    Elem* next;
};

void printLinkedList(Elem* list) {
	while (list) {
		cout << list->num << ' ';
		list = list->next;
	}
	cout << endl;
}

void deleteFromLinkedList(Elem* &list) {

    if (!list)
        return;

    Elem *curr = list, *next = list->next, *prev = NULL;

    while (next)
    {
        if (curr->num < next->num) {
            if (prev)
                prev->next = next;
            else
                list = next;
            delete curr;
        }
        else {
            prev = curr;
        }
        curr = next;
        next = curr->next;
    }
}

int main()
{
    Elem* first = NULL, *last = NULL, *p;
    int i;

    //cout << "Enter any number or 0 to finish: ";
    cin >> i;
   
    while (i != 0)
    {
        p = new Elem;
        p->num = i;
        p->next = NULL;
        if (first == NULL)
        {
            first = last = p;
        }
        else
        {
            last->next = p;
            last = last->next;
        }
        //cout << "Enter any number or 0 to finish: ";
        cin >> i;
    }

    printLinkedList(first);
	deleteFromLinkedList(first);
    printLinkedList(first);
}