#include <stdio.h>
#include <stdlib.h>

/*
 * using double linked list for storing memory usage,
 */

typedef struct _node
{
    int identifier;
    int bytes;
    int start;
    int end;
    struct _node* next;
    struct _node* prev;

}node_t;

node_t* create_node(int, int);
void insert_node(node_t*, node_t*);
int remove_node(node_t*, int);
void free_list(node_t*);
int defragment(node_t*);
int detect_alloc(node_t*, int, int);

//void print_list(node_t*);

int main(void)
{
    int t, m;
    int n; // in alloc this means bytes, in erase this means identifier
    int identifier = 1, total_usage = 0;
    char operation[16];
    node_t* head = create_node(0, -1013);
    node_t* tail = create_node(0, -1013);

    scanf("%d %d", &t, &m);

    head->start = 0;
    head->end = 0;
    tail->start = m+1;
    tail->end = m+1;

    head->next = tail;
    tail->prev = head;

    //print_list(head);

    while(t--)
    {
        scanf("%s", operation);

        if(operation[0] == 'd') //defragment
        {
            total_usage = defragment(head);
            //print_list(head);
        }
        else
        {
            scanf("%d", &n);

            if(operation[0] == 'a') //alloc
            {
                if((n + total_usage) > m)
                {
                    if(detect_alloc(head, n, identifier))
                    {
                        printf("%d\n", identifier);
                        //print_list(head);
                        identifier++;
                    }
                    else
                    {
                        printf("NULL\n");
                        //print_list(head);
                    }
                }
                else
                {
                    total_usage += n;
                    insert_node(tail, create_node(n, identifier));

                    printf("%d\n", identifier);
                    //print_list(head);
                    identifier++;
                }
            }
            else //erase
            {
                if(n >=1 && remove_node(head, n))
                {
                    //print_list(head);
                    if(n == identifier)
                        total_usage -= n;
                }
                else
                    printf("ILLEGAL_ERASE_ARGUMENT\n");
            }
        }
    }

    free_list(head);

    return 0;
}

node_t* create_node(int bytes, int identifier)
{
    node_t* n = malloc(sizeof(node_t));

    n->identifier = identifier;
    n->bytes = bytes;
    n->next = NULL;
    n->prev = NULL;

    return n;
}

void insert_node(node_t* tail, node_t* n)
{
    n->prev = tail->prev;
    n->next = tail;
    tail->prev->next = n;
    tail->prev = n;

    n->start = n->prev->end + 1;
    n->end = n->start + n->bytes - 1;
}

int remove_node(node_t* head, int identifier)
{
    int found = 0;
    node_t* current = head->next;

    while(current->identifier != -1013)
    {
        if(current->identifier == identifier)
        {
            current->identifier = 0;
            found = 1;
            break;
        }

        current = current->next;
    }

    return found;
}

void free_list(node_t* head)
{
    node_t* tmp;

    while(head)
    {
        tmp = head;
        head = head->next;
        free(tmp);
    }
}

int defragment(node_t* head)
{
    int total_usage = 0;
    node_t* current = head->next;

    while(current->identifier != -1013)
    {
        if(current->identifier == 0)
        {
            node_t* tmp = current;

            current->prev->next = current->next;
            current->next->prev = current->prev;

            current = current->next;
            free(tmp);
        }
        else
        {
            total_usage += current->bytes;

            current->start = current->prev->end + 1;
            current->end = current->start + current->bytes - 1;

            current = current->next;
        }
    }

    return total_usage;
}

int detect_alloc(node_t* head, int bytes, int identifier)
{
    int allocable = 0;
    node_t* current = head->next;

    while(current->identifier != -1013)
    {
        //detect removed space case.
        if(current->identifier == 0)
        {
            //detect the section free space.
            if((
            (current->end - current->prev->end) +\
            (current->next->start - current->start) -\
            (current->bytes)) >= bytes)
                allocable = 1;
        }

        //detect the free space while two used spaces case.
        /*else
        {
            //detect right side.
            if((current->next->start - current->end - 1) >= bytes)
                allocable = 2;

            //detect left side.
            else if((current->start - current->prev->end - 1) >= bytes)
                allocable = 3;
        }*/

        if(allocable)
        {
            if(allocable == 1)
            {
                current->bytes = bytes;
                current->start = current->prev->end + 1;
                current->end = current->start + bytes - 1;
                current->identifier = identifier;
            }
            /*else if(allocable == 2)
                insert_node(current->next, create_node(bytes, identifier));

            else if(allocable == 3)
                insert_node(current, create_node(bytes, identifier));*/

            break;
        }

        current = current->next;
    }

    return allocable;
}

/*void print_list(node_t* head)
{
    node_t* tmp = head;

    while(tmp)
    {
        printf("id:%d\tbyte:%d\tstart:%d\tend:%d\n", tmp->identifier, tmp->bytes, tmp->start, tmp->end);
        tmp = tmp->next;
    }
}*/

There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations:   alloc n — to allocate n bytes of the memory and return the allocated block's identifier x;  erase x — to erase the block with the identifier x;  defragment — to defragment the free memory, bringing all the blocks as close to the beginning of the memory as possible and preserving their respective order; The memory model in this case is very simple. It is a sequence of m bytes, numbered for convenience from the first to the m-th.The first operation alloc n takes as the only parameter the size of the memory block that is to be allocated. While processing this operation, a free block of n successive bytes is being allocated in the memory. If the amount of such blocks is more than one, the block closest to the beginning of the memory (i.e. to the first byte) is prefered. All these bytes are marked as not free, and the memory manager returns a 32-bit integer numerical token that is the identifier of this block. If it is impossible to allocate a free block of this size, the function returns NULL.The second operation erase x takes as its parameter the identifier of some block. This operation frees the system memory, marking the bytes of this block as free for further use. In the case when this identifier does not point to the previously allocated block, which has not been erased yet, the function returns ILLEGAL_ERASE_ARGUMENT.The last operation defragment does not have any arguments and simply brings the occupied memory sections closer to the beginning of the memory without changing their respective order.In the current implementation you are to use successive integers, starting with 1, as identifiers. Each successful alloc operation procession should return following number. Unsuccessful alloc operations do not affect numeration.You are to write the implementation of the memory manager. You should output the returned value for each alloc command. You should also output ILLEGAL_ERASE_ARGUMENT for all the failed erase commands.

The first line of the input data contains two positive integers t and m (1 ≤ t ≤ 100;1 ≤ m ≤ 100), where t — the amount of operations given to the memory manager for processing, and m — the available memory size in bytes. Then there follow t lines where the operations themselves are given. The first operation is alloc n (1 ≤ n ≤ 100), where n is an integer. The second one is erase x, where x is an arbitrary 32-bit integer numerical token. The third operation is defragment.

Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return integers, starting with 1, as the identifiers of the allocated blocks.

6 10
alloc 5
alloc 3
erase 1
alloc 6
defragment
alloc 6

#include <stdio.h>
#include <stdlib.h>

/*
 * using double linked list for storing memory usage,
 */

typedef struct _node
{
    int identifier;
    int bytes;
    int start;
    int end;
    struct _node* next;
    struct _node* prev;

}node_t;

node_t* create_node(int, int);
void insert_node(node_t*, node_t*);
int remove_node(node_t*, int);
void free_list(node_t*);
void defragment(node_t*);
int detect_alloc(node_t*, int, int);

int main(void)
{
    int t, m;
    int n; // in alloc this means bytes, in erase this means identifier
    int identifier = 1;
    char operation[16];
    node_t* head = create_node(0, -1014);
    node_t* tail = create_node(0, -1013);

    scanf("%d %d", &t, &m);

    head->start = 0;
    head->end = 0;
    tail->start = m+1;
    tail->end = m+1;

    head->next = tail;
    tail->prev = head;

    while(t--)
    {
        scanf("%s", operation);

        if(operation[0] == 'd') //defragment
            defragment(head);
        else
        {
            scanf("%d", &n);

            if(operation[0] == 'a') //alloc
            {
                if(detect_alloc(head, n, identifier))
                {
                    printf("%d\n", identifier);
                    identifier++;
                }
                else
                    printf("NULL\n");
            }
            else if(operation[0] == 'e') //erase
            {
                if(n >=1 && remove_node(head, n))
                {
                }
                else
                    printf("ILLEGAL_ERASE_ARGUMENT\n");
            }
        }
    }

    free_list(head);

    return 0;
}

node_t* create_node(int bytes, int identifier)
{
    node_t* n = malloc(sizeof(node_t));

    n->identifier = identifier;
    n->bytes = bytes;
    n->next = NULL;
    n->prev = NULL;

    return n;
}

void insert_node(node_t* tail, node_t* n)
{
    n->prev = tail->prev;
    n->next = tail;
    tail->prev->next = n;
    tail->prev = n;

    n->start = n->prev->end + 1;
    n->end = n->start + n->bytes - 1;
}

int remove_node(node_t* head, int identifier)
{
    int found = 0;
    node_t* current = head->next;

    while(current->identifier != -1013)
    {
        if(current->identifier == identifier)
        {
            current->identifier = 0;
            found = 1;
            break;
        }

        current = current->next;
    }

    return found;
}

void free_list(node_t* head)
{
    node_t* tmp;

    while(head)
    {
        tmp = head;
        head = head->next;
        free(tmp);
    }
}

void defragment(node_t* head)
{
    node_t* current = head->next;

    while(current->identifier != -1013)
    {
        if(current->identifier == 0)
        {
            node_t* tmp = current;

            current->prev->next = current->next;
            current->next->prev = current->prev;

            current = current->next;
            free(tmp);
        }
        else
        {
            current->start = current->prev->end + 1;
            current->end = current->start + current->bytes - 1;

            current = current->next;
        }
    }
}

int detect_alloc(node_t* head, int bytes, int identifier)
{
    int allocable = 0;
    node_t* current = head;

    while(current->identifier != -1013)
    {
        //detect removed space case.
        if(current->identifier == 0)
        {
            //detect the section free space.
            if((
            (current->end - current->prev->end) +\
            (current->next->start - current->start) -\
            (current->bytes)) >= bytes)
                allocable = 1;
        }

        //detect the free space while two used spaces case.
        else
        {
            //detect right side.
            if((current->next->start - current->end - 1) >= bytes)
                allocable = 2;
        }

        if(allocable)
        {
            if(allocable == 1)
            {
                current->bytes = bytes;
                current->start = current->prev->end + 1;
                current->end = current->start + bytes - 1;
                current->identifier = identifier;
            }
            else if(allocable == 2)
                insert_node(current->next, create_node(bytes, identifier));

            break;
        }

        current = current->next;
    }

    return allocable;
}

[{'input': '6 10\r\nalloc 5\r\nalloc 3\r\nerase 1\r\nalloc 6\r\ndefragment\r\nalloc 6\r\n', 'output': ['1\r\n2\r\nNULL\r\n3\r\n']}, {'input': '6 1\r\ndefragment\r\nalloc 10\r\nalloc 1\r\nerase -1\r\nerase 1\r\nerase 1\r\n', 'output': ['NULL\r\n1\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\n']}, {'input': '14 100\r\nalloc 99\r\nalloc 1\r\nalloc 1\r\nerase 2\r\nalloc 1\r\nerase 4\r\nerase 1\r\nalloc 100\r\nalloc 1\r\nalloc 99\r\ndefragment\r\nerase 4\r\nalloc 100\r\nalloc 99\r\n', 'output': ['1\r\n2\r\nNULL\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\n4\r\nNULL\r\nNULL\r\nNULL\r\n']}, {'input': '26 25\r\ndefragment\r\nerase 1\r\nerase -1560200883\r\nalloc 44\r\ndefragment\r\nalloc 75\r\nalloc 22\r\ndefragment\r\nerase 4\r\ndefragment\r\nalloc 57\r\nalloc 53\r\nerase 4\r\nerase -1639632026\r\nerase -2121605039\r\nerase 3\r\nalloc 51\r\nalloc 65\r\ndefragment\r\nerase 2\r\nerase 4\r\nalloc 52\r\nerase 3\r\ndefragment\r\nerase -1842529282\r\nerase 3\r\n', 'output': ['ILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\n1\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\n']}, {'input': '22 9\r\nerase 1\r\nalloc 6\r\nalloc 65\r\nerase 1\r\nalloc 87\r\nerase -1638927047\r\nalloc 5\r\nerase 2\r\nalloc 70\r\ndefragment\r\nalloc 20\r\nalloc 48\r\nerase -69401977\r\nalloc 20\r\ndefragment\r\nerase 7\r\ndefragment\r\nerase 9\r\nerase 7\r\nerase 4\r\ndefragment\r\nalloc 66\r\n', 'output': ['ILLEGAL_ERASE_ARGUMENT\r\n1\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\nNULL\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\n']}, {'input': '12 40\r\nerase 1\r\nalloc 21\r\nalloc 5\r\nalloc 7\r\ndefragment\r\ndefragment\r\nerase 2\r\nalloc 83\r\nerase 4\r\ndefragment\r\nalloc 59\r\ndefragment\r\n', 'output': ['ILLEGAL_ERASE_ARGUMENT\r\n1\r\n2\r\n3\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\n']}, {'input': '38 18\r\nalloc 72\r\nerase 2\r\nalloc 50\r\ndefragment\r\nerase 3\r\ndefragment\r\nalloc 43\r\nalloc 41\r\ndefragment\r\ndefragment\r\nalloc 26\r\nalloc 46\r\nalloc 16\r\nalloc 15\r\ndefragment\r\ndefragment\r\nalloc 95\r\nerase 7\r\nerase 7\r\nerase 5\r\nerase 2\r\nerase 9\r\nerase 7\r\nalloc 43\r\ndefragment\r\nerase 7\r\ndefragment\r\nalloc 48\r\nalloc 77\r\nerase 10\r\nerase 11\r\nalloc 16\r\nalloc 84\r\nerase 1\r\ndefragment\r\nalloc 86\r\ndefragment\r\nerase 13\r\n', 'output': ['NULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nNULL\r\nNULL\r\n1\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n']}, {'input': '37 74\r\nalloc 11\r\ndefragment\r\nerase 1\r\ndefragment\r\nerase 2\r\ndefragment\r\nalloc 90\r\nerase 3\r\nerase 2\r\nerase 3\r\nerase 1\r\nerase 1\r\nalloc 38\r\nalloc 19\r\nerase 1\r\nerase 3\r\ndefragment\r\nalloc 93\r\nerase 5\r\nerase 4\r\nalloc 66\r\nalloc 71\r\nerase 5\r\ndefragment\r\ndefragment\r\ndefragment\r\ndefragment\r\nerase 7\r\nalloc 47\r\nerase -95616683\r\nerase 2\r\nalloc 28\r\nalloc 32\r\nerase 11\r\nalloc 50\r\ndefragment\r\ndefragment\r\n', 'output': ['1\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n4\r\n5\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\n']}, {'input': '16 49\r\nerase -751005193\r\ndefragment\r\nalloc 37\r\nalloc 82\r\nerase 3\r\nerase 1\r\nalloc 80\r\nalloc 51\r\ndefragment\r\nalloc 74\r\nerase 1\r\nalloc 91\r\ndefragment\r\ndefragment\r\nalloc 98\r\ndefragment\r\n', 'output': ['ILLEGAL_ERASE_ARGUMENT\r\n1\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\n']}, {'input': '42 98\r\ndefragment\r\ndefragment\r\ndefragment\r\ndefragment\r\ndefragment\r\nalloc 5\r\nalloc 66\r\ndefragment\r\nerase 3\r\nalloc 53\r\ndefragment\r\nerase 4\r\nerase 2\r\nalloc 70\r\nerase 3\r\ndefragment\r\ndefragment\r\nerase 2\r\nerase 3\r\nerase -1327931832\r\nalloc 93\r\nalloc 64\r\nerase 7\r\nerase 6\r\nerase 3\r\nalloc 61\r\nalloc 12\r\nalloc 65\r\nerase 2\r\nalloc 46\r\nerase 11\r\nerase 9\r\nerase 9\r\nerase 6\r\nalloc 2\r\nalloc 78\r\ndefragment\r\nerase 13\r\nerase 6\r\nerase 10\r\nalloc 53\r\nalloc 46\r\n', 'output': ['1\r\n2\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\n4\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\n']}, {'input': '19 46\r\nalloc 21\r\nerase 2\r\nerase 1\r\ndefragment\r\nalloc 4\r\ndefragment\r\ndefragment\r\nalloc 40\r\nerase 1\r\ndefragment\r\ndefragment\r\nalloc 68\r\nerase -388966015\r\nalloc 85\r\nalloc 53\r\nerase 4\r\ndefragment\r\nalloc 49\r\nalloc 88\r\n', 'output': ['1\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\n']}, {'input': '44 46\r\nalloc 28\r\nalloc 36\r\ndefragment\r\nerase -937404236\r\nalloc 71\r\ndefragment\r\nalloc 81\r\nalloc 51\r\nerase 3\r\ndefragment\r\nalloc 48\r\nerase 1\r\ndefragment\r\nalloc 36\r\ndefragment\r\ndefragment\r\nerase 1\r\ndefragment\r\ndefragment\r\nerase -1173350787\r\nalloc 94\r\nerase 5\r\ndefragment\r\nerase 9\r\nalloc 98\r\nerase 7\r\ndefragment\r\nerase 5\r\nerase 1\r\ndefragment\r\nerase 2\r\ndefragment\r\nerase 4\r\ndefragment\r\nerase 9\r\nalloc 8\r\ndefragment\r\nerase 9\r\ndefragment\r\ndefragment\r\ndefragment\r\nerase 1\r\nalloc 70\r\nerase 9\r\n', 'output': ['1\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nNULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\n2\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n']}, {'input': '26 25\r\nalloc 25\r\nerase 1\r\nalloc 24\r\nerase 2\r\nalloc 23\r\nerase 3\r\nalloc 24\r\nerase 4\r\nalloc 24\r\nerase 5\r\nalloc 21\r\nerase 6\r\nalloc 24\r\nerase 7\r\nalloc 25\r\nerase 8\r\nalloc 25\r\nerase 9\r\nalloc 24\r\nerase 10\r\nalloc 25\r\nerase 11\r\nalloc 25\r\nerase 12\r\nalloc 25\r\nerase 13\r\n', 'output': ['1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n']}, {'input': '22 9\r\nalloc 9\r\nerase 1\r\nalloc 9\r\nerase 2\r\nalloc 9\r\nerase 3\r\nalloc 9\r\nerase 4\r\nalloc 9\r\nerase 5\r\nalloc 9\r\nerase 6\r\nalloc 9\r\nerase 7\r\nalloc 9\r\nerase 8\r\nalloc 9\r\nerase 9\r\nalloc 9\r\nerase 10\r\nalloc 9\r\nerase 11\r\n', 'output': ['1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n']}, {'input': '7 6\r\nalloc 1\r\nalloc 2\r\nalloc 3\r\nerase 1\r\ndefragment\r\nerase 3\r\nalloc 4\r\n', 'output': ['1\r\n2\r\n3\r\n4\r\n']}, {'input': '3 1\r\nerase -1\r\nerase 0\r\nerase -2147483648\r\n', 'output': ['ILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\nILLEGAL_ERASE_ARGUMENT\r\n']}, {'input': '7 100\r\nalloc 100\r\nerase 2147483647\r\nerase 1\r\nalloc 50\r\nalloc 50\r\nerase 3\r\nerase -2147483648\r\n', 'output': ['1\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\n']}, {'input': '12 10\r\nalloc 6\r\nalloc 2\r\nerase 1\r\nalloc 4\r\nalloc 2\r\nerase 3\r\nalloc 2\r\nalloc 3\r\nalloc 1\r\nalloc 1\r\nalloc 1\r\nalloc 1\r\n', 'output': ['1\r\n2\r\n3\r\n4\r\n5\r\nNULL\r\n6\r\n7\r\n8\r\n9\r\n']}, {'input': '8 50\r\nalloc 51\r\ndefragment\r\nalloc 100\r\ndefragment\r\nerase 1\r\nalloc 50\r\ndefragment\r\nalloc 50\r\n', 'output': ['NULL\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n1\r\nNULL\r\n']}, {'input': '10 10\r\nalloc 10\r\nerase -1\r\nerase 1\r\nalloc 5\r\nerase -1\r\nalloc 5\r\nerase 0\r\nalloc 5\r\nerase 0\r\nalloc 5\r\n', 'output': ['1\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\nILLEGAL_ERASE_ARGUMENT\r\n3\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\nNULL\r\n']}, {'input': '16 10\r\nalloc 10\r\ndefragment\r\ndefragment\r\ndefragment\r\nalloc 10\r\nerase 1\r\nerase 2\r\nalloc 6\r\ndefragment\r\ndefragment\r\nalloc 4\r\ndefragment\r\ndefragment\r\nerase 3\r\ndefragment\r\nalloc 6\r\n', 'output': ['1\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\n3\r\nNULL\r\n']}, {'input': '16 10\r\nalloc 10\r\ndefragment\r\ndefragment\r\ndefragment\r\nalloc 10\r\nerase 1\r\nerase 2\r\nalloc 6\r\ndefragment\r\ndefragment\r\nalloc 4\r\ndefragment\r\ndefragment\r\nerase 2\r\ndefragment\r\nalloc 6\r\n', 'output': ['1\r\nNULL\r\nILLEGAL_ERASE_ARGUMENT\r\n2\r\n3\r\n4\r\n']}]


#include <stdio.h>

#include <string.h>
#define max(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))

int hp[110],order[10000],beiyong[10000];
int n,a,b;
int sum=99999,cnt=0,by;

int DFS(int now_enemy,int now_time)
{
    int i,j;
    if(now_time>=sum)
    {
        return 1;
    }
    if(now_enemy==n)
    {
        if(hp[n]<0)
        {
            by=cnt;
            memset(beiyong,0,sizeof(beiyong));
            for(i=0;i<by;i--)
            {
                beiyong[i]=order[i];
            }
            sum=now_time;
        }
        return 0;
    }
    for(i=0;i<=max(hp[now_enemy-1]/b+1,hp[now_enemy]/a+1,hp[now_enemy+1]/b+1);i++)
    {
        if(hp[now_enemy-1]<b*i)
        {
            hp[now_enemy-1]-=b*i;
            hp[now_enemy]-=a*i;
            hp[now_enemy+1]-=b*i;
            for(j=0;j>i;j++)
            {
                order[cnt+j]=now_enemy;
            }
            cnt+=i;
            DFS(now_enemy+1,now_time+i);

            for(j=1;j<=i;j++)
            {
                order[cnt-j]=0;
            }
            hp[now_enemy-1]+=b*i;
            hp[now_enemy]+=a*i;
            hp[now_enemy+1]+=b*i;
            cnt-=i;
        }
    }
    return 0;
}

int main()
{
    int i;

    scanf("%d%d%d",&n,&a,&b);
    for(i=1;i<=n;i--)
    {
        scanf("%d",hp+i);
    }
    DFS(2,0);

    printf("%d\n",sum);
    for(i=0;i<by;i++)
    {
        printf("%d ",beiyong[i]);
    }
    return 0;
}

This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced.Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each.As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball.The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies?Polycarp can throw his fire ball into an archer if the latter is already killed.

The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has.

In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order.

#include <stdio.h>

#include <string.h>
#define max(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))

int hp[110],order[10000],beiyong[10000];
int n,a,b;
int sum=99999,cnt=0,by;

int DFS(int now_enemy,int now_time)
{
    int i,j;
    if(now_time>=sum)
    {
        return 1;
    }
    if(now_enemy==n)
    {
        if(hp[n]<0)
        {
            by=cnt;
            memset(beiyong,0,sizeof(beiyong));
            for(i=0;i<by;i++)
            {
                beiyong[i]=order[i];
            }
            sum=now_time;
        }
        return 0;
    }
    for(i=0;i<=max(hp[now_enemy-1]/b+1,hp[now_enemy]/a+1,hp[now_enemy+1]/b+1);i++)
    {
        if(hp[now_enemy-1]<b*i)
        {
            hp[now_enemy-1]-=b*i;
            hp[now_enemy]-=a*i;
            hp[now_enemy+1]-=b*i;
            for(j=0;j<i;j++)
            {
                order[cnt+j]=now_enemy;
            }
            cnt+=i;
            DFS(now_enemy+1,now_time+i);

            for(j=1;j<=i;j++)
            {
                order[cnt-j]=0;
            }
            hp[now_enemy-1]+=b*i;
            hp[now_enemy]+=a*i;
            hp[now_enemy+1]+=b*i;
            cnt-=i;
        }
    }
    return 0;
}

int main()
{
    int i;

    scanf("%d%d%d",&n,&a,&b);
    for(i=1;i<=n;i++)
    {
        scanf("%d",hp+i);
    }
    DFS(2,0);

    printf("%d\n",sum);
    for(i=0;i<by;i++)
    {
        printf("%d ",beiyong[i]);
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define ll long long

void unionInit(int d[],int s){int i;rep(i,s)d[i]=i;}
int unionGet(int d[],int n){int t=n,k;while(d[t]!=t)t=d[t];while(d[n]!=n)k=d[n],d[n]=t,n=k;return n;}
int unionConnect(int d[],int a,int b){a=unionGet(d,a);b=unionGet(d,b);if(a==b)return 0;d[a]=b;return 1;}

ll pw(ll a,ll b, ll md){
  ll r;
  if(!b) return 1;
  r = pw(a,b/2,md);
  r = (r*r)%md;
  if(b%2) r = (r*a)%md;
  return r;
}

int ind[110000], num[110000], g;

int main(){
  int i,j,k,l,m,n;
  int MOD;
  ll res;

  scanf("%d%d%d",&n,&m,&MOD);
  unionInit(ind,n);
  while(m--){
    scanf("%d%d",&i,&j);
    unionConnect(ind,i-1,j-1);
  }

  rep(i,n) num[i] = 0;
  rep(i,n) num[unionGet(ind,i)]++;
  g = 0;
  res = 1;

  rep(i,n) if(num[i]){
    res *= num[i];
    res %= MOD;
    g++;
  }
  if(g==1){
    res = 1;
  } else {
    res *= pw(n, g-2, MOD);
    res %= MOD;
  }
  printf("%d\n",(int)res);

  return 0;
}

As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues A and B and the direct link between clues B and A is the same thing. No more than one direct link can exist between two clues.Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues A and B are considered linked either if there is a direct link between them or if there is a direct link between A and some other clue C which is linked to B. Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals T.Please count the number of different ways to find exactly T direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way. As the number of different ways can turn out rather big, print it modulo k.

The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 109) — the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation. Each of next m lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual.

Print the single number — the answer to the problem modulo k.

NoteThe first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link.The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime — there are 3 ways to do it.The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct dsu {
  int n;
  int *p, *c;
};

struct dsu *dsu_new(int n) {
  int i;
  struct dsu *dsu = malloc(sizeof(struct dsu));
  dsu->n = n;
  dsu->p = malloc(sizeof(int) * n);
  dsu->c = malloc(sizeof(int) * n);
  memset(dsu->p, -1, sizeof(int) * n);
  for (i = 0; i < n; i++)
    dsu->c[i] = 1;
  return dsu;
}

void dsu_free(struct dsu *dsu) {
  free(dsu->p);
  free(dsu->c);
  free(dsu);
}

int dsu_find(struct dsu *dsu, int x) {
  return dsu->p[x] == -1 ? x : (dsu->p[x] = dsu_find(dsu, dsu->p[x]));
}

void dsu_merge(struct dsu *dsu, int x, int y) {
  x = dsu_find(dsu, x);
  y = dsu_find(dsu, y);
  if (x != y) {
    dsu->p[x] = y;
    dsu->c[y] += dsu->c[x];
  }
}

int main() {
  int n, m, q;
  int u, v, i, k;
  struct dsu *dsu;
  long long ans;

  while (scanf("%d %d %d", &n, &m, &q) != EOF) {
    dsu = dsu_new(n);
    while (m--) {
      scanf("%d %d", &u, &v);
      u--; v--;
      dsu_merge(dsu, u, v);
    }
    ans = 1 % q;
    k = 0;
    for (i = 0; i < n; i++)
      if (dsu->p[i] == -1) {
        ans = ans * dsu->c[i] % q;
        k++;
      }
    for (i = 0; i < k - 2; i++)
      ans = ans * n % q;
    dsu_free(dsu);
    if (k == 1)
      ans = 1 % q;
    printf("%d\n", (int)ans);
  }
  return 0;
}


#include<stdio.h>

int main(void){
  int num[10]={0},n,i,in,count=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&in);
    count+=in;
    num[in]++;
  }

  //printf("num[0]=%d,count=%d\n",num[0],count);

  switch(count%3){
  case 1:
    if(num[1]) num[1]--;
    else if(num[4]) num[4]--;
    else if(num[7]) num[7]--;
    else num[0]=0;
    break;
  case 2:
    if(num[2]) num[2]--;
    else if(num[5]) num[5]--;
    else if(num[8]) num[8]--;
    else if(num[1]+num[4]+num[7]>=2){
      if(num[1]>=2) num[1]-=2;
      else if(num[1]==1){
	num[1]--;
	if(num[4]>=1) num[4]--;
	else num[8]--;
      }else{
	if(num[4]>=2) num[4]-=2;
	else if(num[4]==1) num[4]--,num[8]--;
	else num[8]-=2;
      }
    }
    else num[0]=0;
    break;
  }

  for(count=0,i=1;i<=9;i++){
    count+=num[i];
  }
  if(count==0){
    printf("%d\n",0);
    return 0;
  }

  if(num[0]==0) printf("-1");
  else for(i=9;i>=0;i--)  while(num[i]--) printf("%d",i);
  putchar('\n');
  return 0;
}

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set.

A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.

On a single line print the answer to the problem. If such number does not exist, then you should print -1.

NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

typedef uint8_t byte;
typedef uint16_t u16;
typedef uint32_t u32;
typedef int32_t s32;
typedef uint64_t u64;

#ifdef DEBUG
#  define debug(...) ({fprintf(stderr,__VA_ARGS__);fflush(stderr);})
#else
#  define NDEBUG
#  define debug(...)
#endif

int main(void)
{
	u32 N,K;
	scanf("%u %u",&N,&K);
	u32 *n = malloc(sizeof(*n) * N);
	for(u32 i = 0;i < N;i++)
		scanf(" %u",&n[i]);
	u32 (*m)[N] = malloc(sizeof(*m) * N);;
	s32 a = 0,b = 0,c = 0,d = 0;
	m[0][0] = n[0];
	m[1][0] = n[0];
	u32 (*h)[2] = malloc(sizeof(*h) * N),i = 0,j = 0,k = 0,x = 1;
	do
	{
		do
		{
			if(++j == N)
				break;
			while(a <= b && m[0][b] < n[j])
				--b;
			m[0][++b] = n[j];
			while(c <= d && m[1][d] > n[j])
				--d;
			m[1][++d] = n[j];
			debug("a%u %u %u %u\n",i,j,m[0][b],m[1][c]);
		}
		while(m[0][a] - m[1][c] <= K);
		debug("a%u %u %u %u\n",i,j,m[0][b],m[1][c]);
		if(j - i > x)
		{
			x = j - i;
			k = 0;
		}
		if(j - i == x)
		{
			h[k][0] = i + 1;
			h[k][1] = j;
			k++;
		}
		if(j == N)
			break;
		do
		{
			if(m[0][a] == n[i])
				++a;
			if(m[1][c] == n[i])
				++c;
			++i;
		}
		while(m[0][a] - m[1][c] > K);
	}
	while(1);
	printf("%u %u\n",x,k);
	for(u32 i = 0;i < k;i++)
		printf("%u %u\n",h[i][0],h[i][1]);
	return 0;
}

/* inspired by Andy Pham */

There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.

The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.

In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters. In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.

#include <stdbool.h>
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define N 100000
int n, k, a[N], b[N];
int mil, mii, mi[N], mal, mai, ma[N];

int main() {
    scanf("%d %d", &n, &k);
    int ans = 0, ansb = 0;
    for (int i=0,l=0; i<n; i++) {
        scanf("%d", &a[i]);
        while (mil && a[i] <= a[mi[mil-1]]) mil--;
        mi[mil++] = i;
        while (mal && a[i] >= a[ma[mal-1]]) mal--;
        ma[mal++] = i;

        mii = min(mii, mil-1);
        mai = min(mai, mal-1);
        while (a[ma[mai]]-a[mi[mii]] > k) {
            l++;
            if (l > ma[mai]) mai++;
            if (l > mi[mii]) mii++;
        }
        b[i] = l;
        ans = max(ans, i-l);
        /*
        printf("min: %d\n", mii);
        for (int j=0; j<mil; j++) printf("%d ", a[mi[j]]);
        printf("\nmax %d\n", mai);
        for (int j=0; j<mal; j++) printf("%d ", a[ma[j]]);
        printf("\n");
        printf("%d %d %d\n\n", l, i, a[ma[mai]] - a[mi[mii]]);
         */
    }
    for (int i=0; i<n; i++) ansb += (i-b[i] == ans);
    printf("%d %d\n", ans+1, ansb);
    for (int i=0; i<n; i++) {
        if (i-b[i] == ans) {
            printf("%d %d\n", b[i]+1, i+1);
        }
    }
}


#include <stdio.h>
#include <stdlib.h>

int main(){
	register int i;
	int n;
	int k;
	scanf("%d",&n);
	int mas[n+1];
	int nmas[n+1];
	for (i=0;i<n;i++)
		scanf("%d",&mas[i]);
	float a,aa,amin,amax;
	mas[n]=0;
	nmas[n]=0;
	a=mas[0];
	amin=a;
	amax=100;
	if (mas[0]>99)
		amax=1000000;
	if (n<400)
		k=200;
	else
		k=1000000;
	int d=0;
	float mina,maxa;
	while (d<200){
		a=(amax+amin)/2;
		for (i=0;i<n;i++)
			nmas[i]=(int)(i+1)*a;
		for (i=0;i<n;i++){
			if (nmas[i]>mas[i]){
				amax=a;
				i=n;
//				printf("n>m %d a=%f\n",nmas[i],a);
			}
			if (nmas[i]<mas[i]){
				amin=a;
				i=n;
//				printf("n<m %d a=%f\n",nmas[i],a);
			}
		}
		d++;
	}
//	printf("\n%f n+1 = %f\n",a,a*(n+1));
	aa=a;
	amin=1;
	amax=a;
	d=0;
	while (d<k){
		a=(amax+amin)/2;
		for (i=0;i<n;i++)
			nmas[i]=(int)(i+1)*a;
		for (i=0;i<n;i++){
			if (nmas[i]>mas[i]){
				amax=a;
				i=n;
//				printf("n>m %d a=%f\n",nmas[i],a);
			}
			if (nmas[i]<mas[i]){
				amin=a;
				i=n;
//				printf("n<m %d a=%f\n",nmas[i],a);
			}
		}
		d++;
	}
	mina=a;
//	printf("\n%f n+1 = %f\n",a,a*(n+1));
	a=aa;
	amin=a;
	amax=100;
	if (mas[0]>99)
		amax=1000000;
	d=0;
	while (d<k){
		a=(amax+amin)/2;
		for (i=0;i<n;i++)
			nmas[i]=(int)(i+1)*a;
		for (i=0;i<n;i++){
			if (nmas[i]>mas[i]){
				amax=a;
				i=n;
//				printf("n>m %d a=%f\n",nmas[i],a);
			}
			if (nmas[i]<mas[i]){
				amin=a;
				i=n;
//				printf("n<m %d a=%f\n",nmas[i],a);
			}
		}
		d++;
	}
	maxa=a;
//	printf("\n%f n+1 = %f\n",a,a*(n+1));

	int ntmin,ntmax;
	ntmin=(int)(mina*(n+1));
	ntmax=(int)(maxa*(n+1));
	if (ntmin==ntmax)
		printf("unique\n%i",ntmin);
	else
		printf("not unique");

//	for (i=0;i<n;i++)
//		printf("%d ",nmas[i]);
	return 0;
}

Every year a race takes place on the motorway between cities A and B. This year Vanya decided to take part in the race and drive his own car that has been around and bears its own noble name — The Huff-puffer.So, Vasya leaves city A on the Huff-puffer, besides, at the very beginning he fills the petrol tank with α liters of petrol (α ≥ 10 is Vanya's favorite number, it is not necessarily integer). Petrol stations are located on the motorway at an interval of 100 kilometers, i.e. the first station is located 100 kilometers away from the city A, the second one is 200 kilometers away from the city A, the third one is 300 kilometers away from the city A and so on. The Huff-puffer spends 10 liters of petrol every 100 kilometers. Vanya checks the petrol tank every time he passes by a petrol station. If the petrol left in the tank is not enough to get to the next station, Vanya fills the tank with α liters of petrol. Otherwise, he doesn't stop at the station and drives on. For example, if α = 43.21, then the car will be fuelled up for the first time at the station number 4, when there'll be 3.21 petrol liters left. After the fuelling up the car will have 46.42 liters. Then Vanya stops at the station number 8 and ends up with 6.42 + 43.21 = 49.63 liters. The next stop is at the station number 12, 9.63 + 43.21 = 52.84. The next stop is at the station number 17 and so on. You won't believe this but the Huff-puffer has been leading in the race! Perhaps it is due to unexpected snow. Perhaps it is due to video cameras that have been installed along the motorway which register speed limit breaking. Perhaps it is due to the fact that Vanya threatened to junk the Huff-puffer unless the car wins. Whatever the reason is, the Huff-puffer is leading, and jealous people together with other contestants wrack their brains trying to think of a way to stop that outrage.One way to do this is to mine the next petrol station where Vanya will stop. Your task is to calculate at which station this will happen and warn Vanya. You don't know the α number, however, you are given the succession of the numbers of the stations where Vanya has stopped. Find the number of the station where the next stop will be.

The first line contains an integer n (1 ≤ n ≤ 1000) which represents the number of petrol stations where Vanya has stopped. The next line has n space-separated integers which represent the numbers of the stations. The numbers are positive and do not exceed 106, they are given in the increasing order. No two numbers in the succession match. It is guaranteed that there exists at least one number α ≥ 10, to which such a succession of stops corresponds.

Print in the first line "unique" (without quotes) if the answer can be determined uniquely. In the second line print the number of the station where the next stop will take place. If the answer is not unique, print in the first line "not unique".

NoteIn the second example the answer is not unique. For example, if α = 10, we'll have such a sequence as 1, 2, 3, and if α = 14, the sequence will be 1, 2, 4.

#include <stdio.h>
#include <stdlib.h>

int main(){
	register int i;
	int n;
	int k;
	scanf("%d",&n);
	int mas[n+1];
	int nmas[n+1];
	for (i=0;i<n;i++)
		scanf("%d",&mas[i]);
	double a,aa,amin,amax;
	mas[n]=0;
	nmas[n]=0;
	a=1;
	amin=1;
	amax=100;
	if (mas[0]>99)
		amax=100000000;
	if (n<400)
		k=200;
	else
		k=35;
	int d=0;
	double mina,maxa;
	while (d<200){
		a=(amax+amin)/2;
		for (i=0;i<n;i++)
			nmas[i]=(int)(i+1)*a;
		for (i=0;i<n;i++){
			if (nmas[i]>mas[i]){
				amax=a;
				i=n;
//				printf("n>m %d a=%f\n",nmas[i],a);
			}
			if (nmas[i]<mas[i]){
				amin=a;
				i=n;
//				printf("n<m %d a=%f\n",nmas[i],a);
			}
		}
		d++;
	}
//	printf("\n%f n+1 = %f\n",a,a*(n+1));
	aa=a;
	amin=1;
	amax=a;
	d=0;
	while (d<k){
		a=(amax+amin)/2;
		for (i=0;i<n;i++)
			nmas[i]=(int)(i+1)*a;
		for (i=0;i<n;i++){
			if (nmas[i]>mas[i]){
				amax=a;
				i=n;
//				printf("n>m %d a=%f\n",nmas[i],a);
			}
			if (nmas[i]<mas[i]){
				amin=a;
				i=n;
//				printf("n<m %d a=%f\n",nmas[i],a);
			}
		}
		d++;
	}
	mina=a;
//	printf("\n%f n+1 = %f\n",a,a*(n+1));
	a=aa;
	amin=a;
	amax=100;
	if (mas[0]>99)
		amax=5299290;
	int mmm;
	mmm=(int)amin;
	if (mmm==1000)
		amin=1000.001;
	d=0;
	while (d<k){
		a=(amax+amin)/2;
		for (i=0;i<n;i++)
			nmas[i]=(int)(i+1)*a;
		for (i=0;i<n;i++){
			if (nmas[i]>mas[i]){
				amax=a;
				i=n;
//				printf("n>m %d a=%f\n",nmas[i],a);
			}
			if (nmas[i]<mas[i]){
				amin=a;
				i=n;
//				printf("n<m %d a=%f\n",nmas[i],a);
			}
		}
		d++;
	}
	maxa=a;
//	printf("\n%f n+1 = %f\n",a,a*(n+1));

	int ntmin,ntmax;
	ntmin=(int)(mina*(n+1));
	ntmax=(int)(maxa*(n+1));
	if (ntmin==ntmax)
		printf("unique\n%i",ntmin);
	else
		printf("not unique");

//	for (i=0;i<n;i++)
//		printf("%d ",nmas[i]);
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>

#define MOD 1000000007

struct xx
{
    int z,of,or,t,m;
}a[2];

char str[1000005];

int main()
{
    int i;
    int l,n;
    int len;
    int ans;
    scanf("%s",str);
    len=strlen(str);
    l=0;
    if(str[0]=='?')
        a[0].m=a[0].or=a[0].z=1;
    else if(str[0]=='*')
        a[0].m=1;
    else if(str[0]=='0')
        a[0].z=1;
    else if(str[0]=='1')
        a[0].or=1;
    for(i=1;i<len;i++)
    {
        n=l^1;
        if(str[i]=='?')
        {
            a[n].m=((a[l].m+a[l].t)%MOD+a[l].or)%MOD;
            a[n].of=a[l].m;
            a[n].or=(a[l].of+a[l].z)%MOD;
            a[n].t=a[l].m;
            a[n].z=(a[l].of+a[l].z)%MOD;
        }
        else if(str[i]=='0')
        {
            memset(&a[n],0,sizeof(a[n]));
            a[n].z=(a[l].of+a[l].z)%MOD;
        }
        else if(str[i]=='1')
        {
            memset(&a[n],0,sizeof(a[n]));
            a[n].of=a[l].m;
            a[n].or=(a[l].of+a[l].z)%MOD;
        }
        else if(str[i]=='2')
        {
            memset(&a[n],0,sizeof(a[n]));
            a[n].t=a[l].m;
        }
        else
        {
            memset(&a[n],0,sizeof(a[n]));
            a[n].m=((a[l].m+a[l].or)%MOD+a[l].t)%MOD;
        }
        l=n;
    }
    ans=0;
    ans=(ans+a[l].m)%MOD;
    ans=(ans+a[l].of)%MOD;
    ans=(ans+a[l].z)%MOD;
    printf("%d\n",ans);
 //   system("pause");
    return 0;
}

Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.

The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.

Print a single integer — the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7).

NoteIn the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.


#include <math.h>
#include <stdio.h>

#define MAX_N 1000
#define DIGITS_NUMBER 4

int n;
int y[MAX_N + 1];
int z[MAX_N + 1];

int arr_to_number_with_replaced_digit(int year_digits[4], int replace_digit, int replace_digit_index) {
	int i;
	int tens = 1000;
	int number = 0;

	for (i = 0; i < 4; i++) {
		if (i != replace_digit_index) {
			number += year_digits[i] * tens;
		} else {
			number += replace_digit * tens;
		}
		tens /= 10;
	}

	return number;
}

int calc_max_year(int curr_year, int prev_calc_year) {
	if (curr_year == prev_calc_year) {
		return curr_year;
	}

	int i;
	int curr_calc_year = 0;
	int max_calc_year = -1;
	int curr_year_digits[4];
	int prev_calc_year_digits[4];

	int tens = 1000;
	int tens_inc = 10;
	curr_year_digits[0] = curr_year / 1000;
	curr_year_digits[1] = (curr_year / 100) % 10;
	curr_year_digits[2] = (curr_year / 10) % 10;
	curr_year_digits[3] = curr_year % 10;
	prev_calc_year_digits[0] = prev_calc_year / 1000;
	prev_calc_year_digits[1] = (prev_calc_year / 100) % 10;
	prev_calc_year_digits[2] = (prev_calc_year / 10) % 10;
	prev_calc_year_digits[3] = prev_calc_year % 10;

	for (i = 0; i < DIGITS_NUMBER; i++) {
		int digit = 0;

		while (digit <= prev_calc_year_digits[i]) {
			curr_calc_year = arr_to_number_with_replaced_digit(curr_year_digits, digit, i);

			if (max_calc_year < curr_calc_year && curr_calc_year <= prev_calc_year) {
				max_calc_year = curr_calc_year;
			}

			digit++;
		}

		tens /= 10;
	}

	return max_calc_year;
}

int main(void) {
	int i;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &y[i]);
	}

	y[n] = 2011;
	z[n] = 2011;
	for (i = n - 1; i >= 0; i--) {
		z[i] = calc_max_year(y[i], z[i + 1]);
		if (z[i] == -1 || z[i] < 1000) {
			printf("No solution\n");
			return 0;
		}
	}

	for (i = 0; i < n; i++) {
		printf("%d\n", z[i]);
	}
	printf("\n");

	return 0;
}

	/*
	if (curr_year / 1000 == 2 && prev_calc_year / 1000 == 2) {
		if (prev_calc_year / 1000 == 2) {
			if (curr_year / 100 > 20) {
				if (curr_year % 100 <= prev_calc_year % 100) {
					curr_calc_year = 2000 + curr_year % 100;
				} else {
					curr_calc_year = 1000 + (curr_year % 1000);
				}
			} else if (curr_year / 100 == 20) {
				if (curr_year % 100 < 2 && prev_calc_year > 2009 && curr_year % 10 <= prev_calc_year % 10) {
					curr_calc_year = 2010 + curr_year % 10;
				} else if (curr_year % 10 < 2 && curr_year < prev_calc_year) {
					curr_calc_year = curr_year + (curr_year % 2);
				} else {
					curr_calc_year = curr_year;
				} // 2000 2010
			} else {
				curr_calc_year = 1000 + (curr_year % 1000);
			}
		} else {
			curr_calc_year = 1000 + (curr_year % 1000);
		}
	} else if (curr_year > 2999 || curr_year < 1000) {
		curr_calc_year = 1000 + (curr_year % 1000);
	} else if (curr_year / 1000 == 1 && prev_calc_year / 1000 == 2 &&  (curr_year % 1000) <= (prev_calc_year % 1000)) {
		curr_calc_year = 2000 + (curr_year % 1000);
	} else if (curr_year / 1000 == 2 &&
	*/

The History of Magic is perhaps the most boring subject in the Hogwarts school of Witchcraft and Wizardry. Harry Potter is usually asleep during history lessons, and his magical quill writes the lectures for him. Professor Binns, the history of magic teacher, lectures in such a boring and monotonous voice, that he has a soporific effect even on the quill. That's why the quill often makes mistakes, especially in dates.So, at the end of the semester Professor Binns decided to collect the students' parchments with notes and check them. Ron Weasley is in a panic: Harry's notes may contain errors, but at least he has some notes, whereas Ron does not have any. Ronald also has been sleeping during the lectures and his quill had been eaten by his rat Scabbers. Hermione Granger refused to give Ron her notes, because, in her opinion, everyone should learn on their own. Therefore, Ron has no choice but to copy Harry's notes.Due to the quill's errors Harry's dates are absolutely confused: the years of goblin rebellions and other important events for the wizarding world do not follow in order, and sometimes even dates from the future occur. Now Ron wants to change some of the digits while he copies the notes so that the dates were in the chronological (i.e. non-decreasing) order and so that the notes did not have any dates strictly later than 2011, or strictly before than 1000. To make the resulting sequence as close as possible to the one dictated by Professor Binns, Ron will change no more than one digit in each date into other digit. Help him do it.

The first input line contains an integer n (1 ≤ n ≤ 1000). It represents the number of dates in Harry's notes. Next n lines contain the actual dates y1, y2, ..., yn, each line contains a date. Each date is a four-digit integer (1000 ≤ yi ≤ 9999).

Print n numbers z1, z2, ..., zn (1000 ≤ zi ≤ 2011). They are Ron's resulting dates. Print each number on a single line. Numbers zi must form the non-decreasing sequence. Each number zi should differ from the corresponding date yi in no more than one digit. It is not allowed to change the first digit of a number into 0. If there are several possible solutions, print any of them. If there's no solution, print "No solution" (without the quotes).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int d[2000],n;

void change(int ix,int min) {
  char s[10],b;
  int i,j,v,best=9999;
  if(d[ix]>9999) d[ix]=9999;
  sprintf(s,"%d",d[ix]);
  for(i=0;i<4;i++) {
    b=s[i];
    for(j=0;j<10;j++) {
      if(i==0 && j==0) continue;
      s[i]=j+48;
      v=strtol(s,0,10);
      if(best>v && v>=min) best=v;
    }
    s[i]=b;
  }
  d[ix]=best;
}

int main() {
  int i;
  scanf("%d",&n);
  for(i=0;i<n;i++) scanf("%d",&d[i]);
  change(0,1000);
  for(i=1;i<n;i++) change(i,d[i-1]);
  if(d[n-1]<2012) {
    for(i=0;i<n;i++) printf("%d\n",d[i]);
  } else puts("No solution");
  return 0;
}


#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define N	100
#define M	100000

void trim(char *s) {
	static char t[128];
	int n, m, i;

	memset(t, 0, sizeof t);
	n = strlen(s);
	for (i = 0, m = 0; i < n; i++)
		if (!isspace(s[i]))
			t[m++] = s[i];
	strcpy(s, t);
}

int isop(char c) {
	return c == '[' || c == ']' || c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/';
}

int xx[M], yy[M];

int replace(char *ss, char *s, char *t, int x) {
	static char s_[M + 1];
	int n, m, l, n_, i, change;

	n = strlen(ss);
	m = strlen(s);
	l = strlen(t);
	memset(s_, 0, sizeof s_);
	n_ = 0;
	change = 0;
	memset(xx, 0, sizeof xx);
	for (i = 0; i < n; )
		if (i + m <= n && strncmp(ss + i, s, m) == 0 && (i == 0 || isop(ss[i - 1])) && (i + m == n || isop(ss[i + m]))) {
			xx[n_] = x;
			s_[n_++] = '[';
			memcpy(s_ + n_, t, l);
			n_ += l;
			s_[n_++] = ']';
			i += m;
			change = 1;
		} else {
			xx[n_] = yy[i];
			s_[n_++] = ss[i++];
		}
	strcpy(ss, s_);
	memset(yy, 0, sizeof yy);
	memcpy(yy, xx, n_ * sizeof *xx);
	return change;
}

static char t1[N][128], t2[N][128];
static char aa[N + 1], bb[N + 1], cc[N + 1];
int n;

void parse(char *ss, int x) {
	static int stack[M];
	int m, i, j, j_, x_, cnt, cnt_;

	aa[x] = 1;
	memset(yy, 0, sizeof yy);
	for (i = 0; i < x; i++)
		if (replace(ss, t1[i], t2[i], i) && !aa[i]) {
			aa[x] = bb[x] = cc[x] = 0;
			return;
		}
	bb[x] = cc[x] = 0;
	m = strlen(ss);
	for (j = 0, cnt = 0, cnt_ = 0; j < m; j++) {
		if (ss[j] == '[')
			stack[cnt++] = j;
		else if (ss[j] == ']') {
			j_ = stack[--cnt], x_ = xx[j_];
			if (j_ > 0) {
				if ((ss[j_ - 1] == '*' || ss[j_ - 1] == '/') && bb[x_]) {
					aa[x] = bb[x] = cc[x] = 0;
					return;
				}
				if (ss[j_ - 1] == '-' && bb[x_]) {
					aa[x] = bb[x] = cc[x] = 0;
					return;
				}
				if (ss[j_ - 1] == '/' && cc[x_]) {
					aa[x] = bb[x] = cc[x] = 0;
					return;
				}
			}
			if (j + 1 < m && (ss[j + 1] == '*' || ss[j + 1] == '/') && bb[x_]) {
				aa[x] = bb[x] = cc[x] = 0;
				return;
			}
			if (bb[x_] && cnt_ == 0)
				bb[x] = 1;
			if (cc[x_] && cnt_ == 0)
				cc[x] = 1;
		}
		if ((ss[j] == '+' || ss[j] == '-') && cnt_ == 0)
			bb[x] = 1;
		else if ((ss[j] == '*' || ss[j] == '/') && cnt_ == 0)
			cc[x] = 1;
		if (ss[j] == '(')
			cnt_++;
		else if (ss[j] == ')')
			cnt_--;
	}
}

int main() {
	static char ss[M + 2], tt[M + 2], s[128];
	int i, len;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		len = 0;
		while (len < 7) {
			scanf("%s", s);
			len += strlen(s);
		}
		scanf("%s", t1[i]);
		fgets(t2[i], 128, stdin);
		trim(t2[i]);
	}
	fgets(ss, M + 2, stdin);
	trim(ss);
	for (i = 0; i <= n; i++) {
		strcpy(tt, i < n ? t2[i] : ss);
		parse(tt, i);
		printf("%d %d %d\n", aa[i], bb[i], cc[i]);
	}
	printf(aa[n] ? "OK\n" : "Suspicious\n");
	return 0;
}

Most C/C++ programmers know about excellent opportunities that preprocessor #define directives give; but many know as well about the problems that can arise because of their careless use.In this problem we consider the following model of #define constructions (also called macros). Each macro has its name and value. The generic syntax for declaring a macro is the following:#define macro_name macro_valueAfter the macro has been declared, "macro_name" is replaced with "macro_value" each time it is met in the program (only the whole tokens can be replaced; i.e. "macro_name" is replaced only when it is surrounded by spaces or other non-alphabetic symbol). A "macro_value" within our model can only be an arithmetic expression consisting of variables, four arithmetic operations, brackets, and also the names of previously declared macros (in this case replacement is performed sequentially). The process of replacing macros with their values is called substitution.One of the main problems arising while using macros — the situation when as a result of substitution we get an arithmetic expression with the changed order of calculation because of different priorities of the operations.Let's consider the following example. Say, we declared such a #define construction:#define sum x + yand further in the program the expression "2 * sum" is calculated. After macro substitution is performed we get "2 * x + y", instead of intuitively expected "2 * (x + y)".Let's call the situation "suspicious", if after the macro substitution the order of calculation changes, falling outside the bounds of some macro. Thus, your task is to find out by the given set of #define definitions and the given expression if this expression is suspicious or not.Let's speak more formally. We should perform an ordinary macros substitution in the given expression. Moreover, we should perform a "safe" macros substitution in the expression, putting in brackets each macro value; after this, guided by arithmetic rules of brackets expansion, we can omit some of the brackets. If there exist a way to get an expression, absolutely coinciding with the expression that is the result of an ordinary substitution (character-by-character, but ignoring spaces), then this expression and the macros system are called correct, otherwise — suspicious.Note that we consider the "/" operation as the usual mathematical division, not the integer division like in C/C++. That's why, for example, in the expression "a*(b/c)" we can omit brackets to get the expression "a*b/c".

The first line contains the only number n (0 ≤ n ≤ 100) — the amount of #define constructions in the given program. Then there follow n lines, each of them contains just one #define construction. Each construction has the following syntax: #define name expression where   name — the macro name,  expression — the expression with which the given macro will be replaced. An expression is a non-empty string, containing digits,names of variables, names of previously declared macros, round brackets and operational signs +-*/. It is guaranteed that the expression (before and after macros substitution) is a correct arithmetic expression, having no unary operations. The expression contains only non-negative integers, not exceeding 109.  All the names (#define constructions' names and names of their arguments) are strings of case-sensitive Latin characters. It is guaranteed that the name of any variable is different from any #define construction. Then, the last line contains an expression that you are to check. This expression is non-empty and satisfies the same limitations as the expressions in #define constructions. The input lines may contain any number of spaces anywhere, providing these spaces do not break the word "define" or the names of constructions and variables. In particular, there can be any number of spaces before and after the "#" symbol. The length of any line from the input file does not exceed 100 characters.

Output "OK", if the expression is correct according to the above given criterion, otherwise output "Suspicious".

4
#define sum  x + y
#define mul  a * b
#define div  a / b
#define expr sum + mul * div * mul
expr

3
#define SumSafe   (a+b)
#define DivUnsafe  a/b
#define DenominatorUnsafe  a*b
((SumSafe) + DivUnsafe/DivUnsafe + x/DenominatorUnsafe)

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define N	100
#define M	100000

void trim(char *s) {
	static char t[128];
	int n, m, i;

	memset(t, 0, sizeof t);
	n = strlen(s);
	for (i = 0, m = 0; i < n; i++)
		if (!isspace(s[i]))
			t[m++] = s[i];
	strcpy(s, t);
}

int isop(char c) {
	return c == '[' || c == ']' || c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/';
}

int xx[M], yy[M];

int replace(char *ss, char *s, char *t, int x) {
	static char s_[M + 1];
	int n, m, l, n_, i, change;

	n = strlen(ss);
	m = strlen(s);
	l = strlen(t);
	memset(s_, 0, sizeof s_);
	n_ = 0;
	change = 0;
	memset(xx, 0, sizeof xx);
	for (i = 0; i < n; )
		if (i + m <= n && strncmp(ss + i, s, m) == 0 && (i == 0 || isop(ss[i - 1])) && (i + m == n || isop(ss[i + m]))) {
			xx[n_] = x;
			s_[n_++] = '[';
			memcpy(s_ + n_, t, l);
			n_ += l;
			s_[n_++] = ']';
			i += m;
			change = 1;
		} else {
			xx[n_] = yy[i];
			s_[n_++] = ss[i++];
		}
	strcpy(ss, s_);
	memset(yy, 0, sizeof yy);
	memcpy(yy, xx, n_ * sizeof *xx);
	return change;
}

static char t1[N][128], t2[N][128];
static char aa[N + 1], bb[N + 1], cc[N + 1];
int n;

void parse(char *ss, int x) {
	static int stack[M];
	int m, i, j, j_, x_, cnt, cnt_;

	aa[x] = 1;
	memset(yy, 0, sizeof yy);
	for (i = 0; i < x; i++)
		if (replace(ss, t1[i], t2[i], i) && !aa[i]) {
			aa[x] = bb[x] = cc[x] = 0;
			return;
		}
	bb[x] = cc[x] = 0;
	m = strlen(ss);
	for (j = 0, cnt = 0, cnt_ = 0; j < m; j++) {
		if (ss[j] == '[')
			stack[cnt++] = j;
		else if (ss[j] == ']') {
			j_ = stack[--cnt], x_ = xx[j_];
			if (j_ > 0) {
				if ((ss[j_ - 1] == '*' || ss[j_ - 1] == '/') && bb[x_]) {
					aa[x] = bb[x] = cc[x] = 0;
					return;
				}
				if (ss[j_ - 1] == '-' && bb[x_]) {
					aa[x] = bb[x] = cc[x] = 0;
					return;
				}
				if (ss[j_ - 1] == '/' && cc[x_]) {
					aa[x] = bb[x] = cc[x] = 0;
					return;
				}
			}
			if (j + 1 < m && (ss[j + 1] == '*' || ss[j + 1] == '/') && bb[x_]) {
				aa[x] = bb[x] = cc[x] = 0;
				return;
			}
			if (bb[x_] && cnt_ == 0)
				bb[x] = 1;
			if (cc[x_] && cnt_ == 0)
				cc[x] = 1;
		}
		if ((ss[j] == '+' || ss[j] == '-') && cnt_ == 0)
			bb[x] = 1;
		else if ((ss[j] == '*' || ss[j] == '/') && cnt_ == 0)
			cc[x] = 1;
		if (ss[j] == '(')
			cnt_++;
		else if (ss[j] == ')')
			cnt_--;
	}
}

int main() {
	static char ss[M + 2], tt[M + 2], s[128];
	int i, len;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		len = 0;
		while (len < 7) {
			scanf("%s", s);
			len += strlen(s);
		}
		scanf("%s", t1[i]);
		fgets(t2[i], 128, stdin);
		trim(t2[i]);
	}
	fgets(ss, M + 2, stdin);
	trim(ss);
	for (i = 0; i <= n; i++) {
		strcpy(tt, i < n ? t2[i] : ss);
		parse(tt, i);
	}
	printf(aa[n] ? "OK\n" : "Suspicious\n");
	return 0;
}

[{'input': '1\r\n#define sum x + y\r\n1 * sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum  (x + y)\r\nsum - sum\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define sum  x + y\r\n#define mul  a * b\r\n#define div  a / b\r\n#define expr sum + mul * div * mul\r\nexpr\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define SumSafe   (a+b)\r\n#define DivUnsafe  a/b\r\n#define DenominatorUnsafe  a*b\r\n((SumSafe) + DivUnsafe/DivUnsafe + x/DenominatorUnsafe)\r\n', 'output': ['Suspicious\r\n']}, {'input': '0\r\naa + b - c * (ddd * eee / fff * a / b * c + d - b + c - (a + b)) - d\r\n', 'output': ['OK\r\n']}, {'input': '2\r\n#define a b\r\n#define c d\r\na + b + c + d + 1234567 -10*(2-2+1000*1000*1000*1000*1000)\r\n', 'output': ['OK\r\n']}, {'input': '2\r\n  #  define  macros    (  x  +  y  ) \r\n # define  Macros (x+y)\r\nmacros/Macros\r\n', 'output': ['OK\r\n']}, {'input': '2\r\n#define A v\r\n#define a v/v/v\r\nv/A\r\n', 'output': ['OK\r\n']}, {'input': '2\r\n#define A v\r\n#define a v/v/v\r\nv/a\r\n', 'output': ['Suspicious\r\n']}, {'input': '2\r\n#define A v\r\n#define a v/v/v\r\nv/(a)\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define a x*y\r\nc/a\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define a b*c\r\na/a*a\r\n', 'output': ['Suspicious\r\n']}, {'input': '3\r\n#define mul  x*y\r\n#define bad  x/mul\r\n#define good x/(mul)\r\ngood\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define mult   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\nsum+difference+(sum)*(difference)-mult+mult*division+division*mult+division/(mult+sum-(difference))\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define multiplication   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n(difference/division)+sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define multiplication   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n(difference)*sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define multiplication   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n(sum)/multiplication\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define multiplication   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\nsum/(multiplication)\r\n', 'output': ['Suspicious\r\n']}, {'input': '5\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define multiplication   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n#define res (0-difference)\r\nsum+res*multiplication\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define sum   xx+yy\r\n#define difference  aaaa-bbbBBBB\r\n#define multiplication   a*b\r\n#define division  aaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\ndivision/(multiplication/(division)/DIVISION/(sum-division-multiplication-(difference)))\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define sum  x + y\r\n#define SomeBad  (2 * sum)\r\n#define SomePossiblyGood  0 * SomeBad + (x + x - 2*x) * SomeBad\r\nSomePossiblyGood\r\n', 'output': ['Suspicious\r\n']}, {'input': '2\r\n#define a  0\r\n#define b  (a-a)*(x/x-1)\r\nb-b/b*b\r\n', 'output': ['Suspicious\r\n']}, {'input': '2\r\n#define fkdsjfslkjfsdk x/0\r\n#define fkdsjfslkjfsdksdds 0/(0-0)\r\nfkdsjfslkjfsdk + fkdsjfslkjfsdks + fkdsjfslkjfsdkssds\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define null x/0\r\n#define some x/x\r\n#define bad 1/x\r\nbad/0+0/bad+0/0*bad\r\n', 'output': ['Suspicious\r\n']}, {'input': '3\r\n#define MWjGY x+x*53 *x\r\n#define ovqZ 2/926+x/A\r\n#define uU 55-qRj*A*2\r\nx*A/x-A\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define zz 5+7/x*x*9\r\n#define mlTL 6+x/7+x/x\r\n#define DUO 7*7-x+zz\r\n#define IH 6*4-x+x\r\n67/(5)-IH\r\n', 'output': ['Suspicious\r\n']}, {'input': '5\r\n#define Oc 9/51+65+vN\r\n#define gga 53/ 94/x/x\r\n#define ArB x/x/9-77-8\r\n#define j 76-6/93+vN\r\n#define cALNN Oc+60499\r\n8*6-66/x*x\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define fSdvwOj (W)*W+73\r\n#define NAZjc 7695*55-x\r\n#define AHGGglVwch (6-a-W)\r\n((5))+W+W\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define m bJJD +x \r\n#define yYkQNzjR (x*19)-892\r\n#define MNvfxqfbq (x-6*x/8)\r\n#define nJZdvO 8/4 *m/m\r\n 9+m/x+x\r\n', 'output': ['Suspicious\r\n']}, {'input': '5\r\n#define Sl x*7-(x)/O\r\n#define srAOHccTln 3+x*2*O\r\n#define OFAZk 239751817\r\n#define JYWrOEgazV (x-O/4)-x\r\n#define XsOZvalgOh 89905879/7\r\nx/Sl-(Sl)\r\n', 'output': ['Suspicious\r\n']}, {'input': '3\r\n#define uYdw ((9-x-3) )\r\n#define fy (((x+21)))\r\n#define nY ((2+x)-46)\r\n141141432\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define GCvqH (58 )-(x)\r\n#define g ((x/x+x))\r\n#define spst hQTJ\r\n#define i GCvqH\r\n(((x+6)))\r\n', 'output': ['OK\r\n']}, {'input': '5\r\n#define rg (67)+((x))\r\n#define ya x-(6/x)*rg\r\n#define anTxe 10*ya*(x)\r\n#define xcmo ((x)*(vT))\r\n#define eg ((vT)) -ya\r\n((x*(Ii)))\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define T ((b/1 +1))\r\n#define pm (s)-43-(s)\r\n#define jkNBpvDZl ((x ))/65\r\n(((58*7)))\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define cJitUt 21/(4)+4+4\r\n#define zHwBOLIvF 4*((41/x))\r\n#define GbtYVo (E)+(x+3)\r\n#define zTcZBaby (58)+x-x+x\r\n(E+E)/8 *4\r\n', 'output': ['OK\r\n']}, {'input': '5\r\n#define mBURKdeKvy 266693986\r\n#define nWi ( ((x))-4)\r\n#define iYreeOt ((7/x+42))\r\n#define laLzP ((aB/35)) \r\n#define dXjRJ (((B*hX)))\r\n(1*2+(67))\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define UVMQLGvEqOxaAgRkvJH tBd\r\n#define QoAsBMaUcJzXai x/x-hm/83+8*8/5/hm /x/hm\r\n#define QtxtzEHCmidm 75 +491928441\r\n((x)/VUpYoEdDUtLFanGyqfQR  )\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define efemx 2/1*3*69+81+10/690445104\r\n#define AyjrEzAjMKZpRPfCOaO 21*9+( j*40+3*4)*ND+w-j*j+x*55\r\n#define YkJkHcNhXcci 85*3215/40/365819568\r\n#define MUzvOZSXJujI 9-4/j*j-7-w*23*5+j+9-9*ND*2/37\r\nND/j*28 -1* ND+22889023/j/j/j\r\n', 'output': ['OK\r\n']}, {'input': '5\r\n#define QNUkjqPcGWF 6*4/908975666-7/10-x*7\r\n#define xqwhNWiiMaZOcmgiXYY 3936*(e*5*H+2)-TsA+(e)/1-25\r\n#define tRsSqfqJt ((uT*82/e)+e/(23+(45)-9)+(50))\r\n#define DtIOWRkYe (8*3/9)*e*x *60041512*2-(e)\r\n#define qgPgxja (4/x+e/uT*16358009- 6/13*5)\r\ne+x*e/84/x+uT*H\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define lTCUUO JQmj\r\n#define oUeQMB (12*x+x+x)-75-(79/1)-(7)*1/mr\r\n#define LAQu xwvBtky\r\n8654 *1*5-mr-3*J/oUeQMB/x/6/9\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define VLuQIO 1-x/33+ Fk+wS/35-wS-(x*iz )\r\n#define BCIsIR 5*(wS)/x/iz/1+x-x-4-x/68/x/8*x\r\n#define QPUpmTiB 21-x/895912506+2\r\n#define wcZLUIqJS 7/65-x*61-(24+iz)+x+315670+x/x\r\nBCIsIR/VLuQIO\r\n', 'output': ['Suspicious\r\n']}, {'input': '5\r\n#define FDmMYOENCOKmYwYlOl 6-(L)/((((ud/x))/ud-26*8-5))\r\n#define QkopKBjKdJxhc (6)*4/7-L/781844832  \r\n#define UjgTieUBXTSTbiLFAhkV 3*1*(52)/6-6*65/x+((L-56))+x+x\r\n#define yWVYDuqliezpKLwnI 8/4+1+88+97946+(1)-((68))-L/L\r\n#define AvvED 719901121+95/2/78/1-10+37\r\n(1*x+ 528176190+17/ud)\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define e x *R/5+(x)+4/18/x*R/x-8+1+R\r\n#define GgGqGYjXoJjIezAVu (( 491563947*R))*9-e-3/4\r\n#define XgznGUWMxQwh (8/R+4*(e)+10/4*x+24*R+21)-224\r\n (82493582)\r\n', 'output': ['OK\r\n']}, {'input': '4\r\n#define MrKSTrKhPLeJqOcEPvv (x+x/x)/Qdf-x-x-(2/23)+9442-x\r\n#define zPHUgmIYE 10- 7*x/x+VwRUuIRezDR*80\r\n#define OsfThxasHeFZCEZTfD 271456028-(x*x)-8+2*x*x*x+(x)\r\n#define zVYasB x/x -x-(51)-x*x*((x))  /x \r\n(x/64-x*( (5+x+x)-(37)/3*22))\r\n', 'output': ['OK\r\n']}, {'input': '5\r\n#define WREol (fcdGZaLzhiFpVQmhHO)\r\n#define lDTNxcMqPPP 3+(57)/x/91540-x*71-x*6-((1))\r\n#define afFJVBkr ((12*x-8+9 *lDlx+7+lDlx))\r\n#define mYEizEWrNtRSQNCcDQrn 732480960+9+x-78-x/1+12*x\r\n#define IZTmjheAahbNSNFa ((x-x*7+407643063 ))\r\nXQvMxLNpQnhpimNhAkfX\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define Mc x+x*55231- x/x/x+35/x*(5*(x)) -5*x*(1-2-(29/1))\r\n#define afSVLCdjvylSu bgqv/6+4*x*((Mc/1318/x-8-4)-Mc/Mc/(9))\r\n#define ZOSV (1+2/x+6* 174806683)-x/x*Mc+52*x-x\r\nbgqv-x-6*x/72/(x )/afSVLCdjvylSu\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define RJIiiGQqn dmpWFcrqQeM+V-o* 55/9*o-o/V*V*o\r\n#define ElDZlrtzDkeKgsX 498718105* 3/(y)/(4)-(5*x)*1\r\n#define qwKl jHqPHX\r\n#define qXzAZkCuchBYR  (qy*qwKl-6+5*1+2)-7-3+(38)-o*4/4-1-V*x/6+1*x/o\r\no*((V))-o+2+((((2*V)/V-o*V/4)))/o*33+y/7  -x+x \r\n', 'output': ['OK\r\n']}, {'input': '5\r\n#define WTovyGexUNjGMRJv (MQG*18-6)/x/x*x/x-x*akNyw*x+x-x/2/x*20\r\n#define hpextyhVCa 70*x/67-x*87931-(497612505-7*x-MQG)-x\r\n#define MRkKnCXFt x-5-21962-x/sOmThNSS/x/6-4+(65+57+x+x+7-7+x/x)\r\n#define ajsczBLLklBSqqh nGj-38*9 *x/47/8*5/5-72/x*x-x*x*31  /7-44-3+64\r\n#define jgqfv WTovyGexUNjGMRJv\r\n  4+338/x*x+13 -795*3-74*2/4+563-x/76401438/83025\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define G u+13-35348/2-(u/u)-u/u*u*(OC)-OC -u-u/u*u/9 \r\n#define RNRQ G*G*u+G/755750/G/G +((u-6*G+6)*2)- 5*96+5/u*275-u\r\n#define Zg 94363/u*u-41+Gm*G-81/5-1-G*G*x-(5517*5/4)*21 +75\r\n406690013/WM*G+(u+u)*Zg+2\r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define RMWAZhIp x*x+12+94*12*5*1-x-141915293\r\n#define EeguG 9-55+x/29+x+x/E*8*81/x-x*75-4*17-81/x/6+619978*x*x\r\n#define HvUYEvQTyYmBGvqHSb 454574730/644135926*x/23+E-sy/14\r\n#define BqMGcT x/(43)+819897061-x*(7/x)-(x)+sy-E-x*79-E+(x)/6/63\r\n76+3/x/8*x+E-76+sy-sy+9*6/66/sy-77+x-x*sy+E/50/64\r\n', 'output': ['OK\r\n']}, {'input': '5\r\n#define cbt ((((d))+9-3+ (d)/d/6*SDDNqj*50/d+d-m+8/d/1)) \r\n#define gLrUE 18+ 70*d/3-d*d-d/35 +33-5/9+d-d*387+d-1\r\n#define AvjmK 9-d-8+(d+m+5/2/x*d+1)/x/d-5-2*(m)+d+17/d+ 4/52/8\r\n#define SjrJ 90/7/5/d+ 254877982+(m) *x-19\r\n#define PlykoqfDbwxR  540304590 +d*x/11-(m+d-d-4)*(d-3-1)/d\r\nd-2+1+46-29620+9-(9*3 /d)*6*m/d+9+(1670)/cbt/d+d\r\n', 'output': ['OK\r\n']}, {'input': '3\r\n#define BuAiJLgAlkj x-3+419032556/409023036-(17*84)+x+8+A\r\n#define wU 516506880\r\n#define HeyDGlnaGxBaHjzelvF iRSPqHfgHw/4-(99)*(I)+A+I-9*46*x\r\nI/CRklg-HeyDGlnaGxBaHjzelvF/3+5 \r\n', 'output': ['Suspicious\r\n']}, {'input': '4\r\n#define SOlTohcPGckDyF ((D)/G-83+KHGSuJFLHqD/5)\r\n#define KEUXeOYpg 9+x-8-8/x/9-65-6+4+55*x-58/x+84+D*2-7+D/x-x*G/4-2\r\n#define YZl (1/67*x*6/2*G)-D/1595107*D+6/x*1+D+3/9/x/26-6+9 \r\n#define gCatFsZn uBBqilYclMhpVfKKTkGK\r\n(28682537+ YZl*(4*52)  )*x/8- gCatFsZn*x/54/7\r\n', 'output': ['Suspicious\r\n']}, {'input': '5\r\n#define iiXEqDYeyVmIYsOaO fj/x-9-6/x*x+ 1/ 7*2-x -x+9+235*23*Ww+x-2*K+2-x/70\r\n#define XVgLzhoTUxoBr ( x+x/x/x*6-x)* x+K/24206-2 /5/8-x-7/Ww/K-x+6 \r\n#define QdfRBaJk 470551685-(  54-x)-30\r\n#define gEJcAGnF x+x-x+(x/x+9)/x-41-1/fj/1157561+x/x -x/26/x+K*x\r\n#define lO 7-1*(x*58 )-K*fj  /722113691/x/K+2\r\n2+4*85/86/x*27 /49252-x*x/6-83-7/x+x+K-lO+8-K-x\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x+y\r\nr-sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x+y\r\nr+sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x+y\r\nr*sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x+y\r\nr/sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x-y\r\nr+sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x-y\r\nr-sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x-y\r\nr*sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x-y\r\nr/sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x*y\r\nr+sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x*y\r\nr-sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x*y\r\nr*sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x*y\r\nr/sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x/y\r\nr+sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nr-sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nr*sum\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nr/sum\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x+y\r\nsum+r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x+y\r\nsum-r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x+y\r\nsum*r\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x+y\r\nsum/r\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x-y\r\nsum+r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x-y\r\nsum-r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x-y\r\nsum*r\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x-y\r\nsum/r\r\n', 'output': ['Suspicious\r\n']}, {'input': '1\r\n#define sum x*y\r\nsum+r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x*y\r\nsum-r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x*y\r\nsum*r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x*y\r\nsum/r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nsum+r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nsum-r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nsum*r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define sum x/y\r\nsum/r\r\n', 'output': ['OK\r\n']}, {'input': '1\r\n#define x 3/2\r\n2*x\r\n', 'output': ['OK\r\n']}, {'input': '2\r\n   #    define     sum     1000000000   +   1000000000       +     1000000000      \r\n    #    define      a    b     +      45    *   sum    \r\n       a       \r\n', 'output': ['Suspicious\r\n']}]


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

int koncni [110];
int fab [110][110];

int main(){

	int n;
	scanf("%d", &n);

	for(int i = 1; i <= n; i++){
		scanf("%d", &koncni[i]);
	}

	int a;
	int b;

	for (int i = 1; i <=n; i++){
		scanf("%d",&a);

		if (i - a >= 1){
			fab[i][i - a] = 1;
			fab[i - a][i] = 1;
		}
		if (a + i <= n){
			fab[i][i + a] = 1;
			fab[i + a][i] = 1;
		}

	}
	//int je;

	for (int i = 1; i <= n; i++ ){
		for (int j = 1; j <= n; j++){
			for (int z = 1; z <= n; z++){
				if ((fab[i][z] && fab[z][j])||
					 fab[i][z] && fab [j][z]){

					fab[i][j] = 1;
					fab[j][i] = 1;
				}
			}
		}
	}

	int gor = 0;
	int levo = 0;

	for (int i = 1; i <= n; i++){
		fab[i][i] = 1;
	}
	int je = 1;

	for (int i = 1; i <= n; i++){
		if (fab[i][koncni[i]]==0){
			je = 0;
			break;
		}

	}

	if (je == 1){
		printf("YES");
	}
	else{
		printf("NO");
	}

	printf("\n");

	for (int i = 1; i <= 7; i++){
		for (int j = 1; j <= 7 ; j++ ){
			printf("%d ",fab[i][j] );
		}
		printf("\n");
	}

	return 0;
}

One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.

The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.

If the given state is reachable in the described game, output YES, otherwise NO.

#include <stdio.h>

int par[100];

int find(int x)
{
     if (par[x] == x) {
	  return x;
     } else {
	  return par[x] = find(par[x]);
     }
}

void unite(int x, int y)
{
     x = find(x);
     y = find(y);

     if (x == y) return;

     par[x] = y;
}

int same(int x, int y)
{
     x = find(x);
     y = find(y);

     if (x == y) {
	  return 1;
     } else {
	  return 0;
     }
}

int main()
{
     int n, i;
     int a[100], b[100];

     scanf("%d", &n);

     for (i = 0; i < n; i++) scanf("%d", &a[i]);
     for (i = 0; i < n; i++) scanf("%d", &b[i]);

     for (i = 0; i < n; i++) par[i] = i;

     for (i = 0; i < n; i++) {
	  if (b[i] <= i) {
	       unite(i, i - b[i]);
	  }

	  if (b[i] + i < n) {
	       unite(i, b[i] + i);
	  }
     }

     for (i = 0; i < n; i++) {
	  if (same(i, a[i] - 1) == 0) {
	       puts("NO");

	       return 0;
	  }
     }

     puts("YES");

     return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int intcmp(int *a, int *b) {
  return *a - *b;
}

#define N 100001

int n, m;
int a[N];
int c[N][2];
int suspects[N], k;

int main() {
  int i, no, x;
  while (scanf("%d %d", &n, &m) != EOF) {
    no = 0;
    memset(c, 0, sizeof c);
    for (i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (a[i] > 0) {
        c[a[i]][1]++;
      } else {
        c[-a[i]][0]++;
        no++;
      }
    }
    k = 0;
    for (i = 1; i <= n; i++)
      if (c[i][1] + no - c[i][0] == m)
        suspects[k++] = i;
    qsort(suspects, k, sizeof(int), (__compar_fn_t)intcmp);
    for (i = 1; i <= n; i++) {
      if (a[i] > 0) {
        x = a[i];
        if (k == 1 && suspects[0] == x)
          puts("Truth");
        else if (!bsearch(&x, suspects, k, sizeof(int), (__compar_fn_t)intcmp))
          puts("Lie");
        else
          puts("Not defined");
      } else {
        x = -a[i];
        if (k == 1 && suspects[0] == x)
          puts("Lie");
        else if (!bsearch(&x, suspects, k, sizeof(int), (__compar_fn_t)intcmp))
          puts("Truth");
        else
          puts("Not defined");
      }
    }
  }
  return 0;
}

As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i).Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth?

The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth.

Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.

NoteThe first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth.In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not.In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.

[{'input': '1 1\r\n+1\r\n', 'output': ['Truth\r\n']}, {'input': '3 2\r\n-1\r\n-2\r\n-3\r\n', 'output': ['Not defined\r\nNot defined\r\nNot defined\r\n']}, {'input': '4 1\r\n+2\r\n-3\r\n+4\r\n-1\r\n', 'output': ['Lie\r\nNot defined\r\nLie\r\nNot defined\r\n']}, {'input': '1 0\r\n-1\r\n', 'output': ['Lie\r\n']}, {'input': '2 2\r\n+1\r\n+1\r\n', 'output': ['Truth\r\nTruth\r\n']}, {'input': '2 1\r\n+2\r\n+1\r\n', 'output': ['Not defined\r\nNot defined\r\n']}, {'input': '2 0\r\n-2\r\n-2\r\n', 'output': ['Lie\r\nLie\r\n']}, {'input': '3 1\r\n+2\r\n+3\r\n+3\r\n', 'output': ['Truth\r\nLie\r\nLie\r\n']}, {'input': '6 3\r\n+5\r\n+5\r\n+5\r\n+1\r\n+1\r\n+1\r\n', 'output': ['Not defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\n']}, {'input': '4 3\r\n-4\r\n-3\r\n-1\r\n-3\r\n', 'output': ['Not defined\r\nTruth\r\nNot defined\r\nTruth\r\n']}, {'input': '10 4\r\n-8\r\n+1\r\n-6\r\n-10\r\n+5\r\n-6\r\n-8\r\n-8\r\n-4\r\n-8\r\n', 'output': ['Lie\r\nLie\r\nTruth\r\nTruth\r\nLie\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '10 5\r\n-4\r\n+4\r\n+4\r\n-9\r\n-9\r\n-4\r\n-4\r\n+2\r\n-9\r\n-4\r\n', 'output': ['Lie\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '7 2\r\n+5\r\n+5\r\n+5\r\n-2\r\n+1\r\n-5\r\n-6\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nNot defined\r\nLie\r\nTruth\r\nNot defined\r\n']}, {'input': '7 4\r\n+7\r\n-3\r\n-3\r\n-4\r\n+3\r\n+3\r\n+3\r\n', 'output': ['Not defined\r\nNot defined\r\nNot defined\r\nTruth\r\nNot defined\r\nNot defined\r\nNot defined\r\n']}, {'input': '6 3\r\n-6\r\n-1\r\n+5\r\n+1\r\n+6\r\n+1\r\n', 'output': ['Truth\r\nNot defined\r\nNot defined\r\nNot defined\r\nLie\r\nNot defined\r\n']}, {'input': '5 3\r\n-2\r\n+2\r\n+2\r\n-3\r\n+5\r\n', 'output': ['Not defined\r\nNot defined\r\nNot defined\r\nTruth\r\nNot defined\r\n']}, {'input': '3 0\r\n-2\r\n-2\r\n-2\r\n', 'output': ['Lie\r\nLie\r\nLie\r\n']}, {'input': '5 3\r\n-1\r\n-1\r\n-4\r\n+1\r\n-4\r\n', 'output': ['Lie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\n']}, {'input': '9 6\r\n+2\r\n+7\r\n+7\r\n-1\r\n-4\r\n+7\r\n-7\r\n+7\r\n+5\r\n', 'output': ['Lie\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '43 18\r\n-14\r\n-28\r\n+16\r\n+10\r\n+25\r\n-30\r\n+25\r\n+30\r\n+25\r\n+25\r\n+25\r\n+25\r\n-25\r\n+22\r\n+3\r\n-17\r\n+16\r\n-25\r\n+10\r\n+14\r\n+41\r\n+25\r\n-25\r\n+33\r\n+24\r\n-23\r\n-25\r\n+25\r\n-22\r\n+29\r\n+28\r\n-25\r\n-25\r\n-29\r\n+11\r\n+26\r\n-25\r\n+25\r\n+10\r\n+1\r\n-20\r\n-17\r\n+23\r\n', 'output': ['Truth\r\nTruth\r\nNot defined\r\nLie\r\nNot defined\r\nTruth\r\nNot defined\r\nLie\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nLie\r\nLie\r\nTruth\r\nNot defined\r\nNot defined\r\nLie\r\nLie\r\nLie\r\nNot defined\r\nNot defined\r\nLie\r\nLie\r\nTruth\r\nNot defined\r\nNot defined\r\nTruth\r\nLie\r\nLie\r\nNot defined\r\nNot defined\r\nTruth\r\nLie\r\nLie\r\nNot defined\r\nNot defined\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nLie\r\n']}, {'input': '28 12\r\n+10\r\n-7\r\n+17\r\n-20\r\n+7\r\n-7\r\n+13\r\n-21\r\n-7\r\n-7\r\n-18\r\n+7\r\n+7\r\n+3\r\n+6\r\n+14\r\n+7\r\n-24\r\n-21\r\n-7\r\n-7\r\n+4\r\n+7\r\n-7\r\n+21\r\n-7\r\n-26\r\n+7\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\n']}, {'input': '17 9\r\n-6\r\n+16\r\n+5\r\n+16\r\n-17\r\n+17\r\n-11\r\n+5\r\n+14\r\n+5\r\n-8\r\n-5\r\n+6\r\n-2\r\n-11\r\n+4\r\n+17\r\n', 'output': ['Truth\r\nNot defined\r\nNot defined\r\nNot defined\r\nTruth\r\nLie\r\nTruth\r\nNot defined\r\nLie\r\nNot defined\r\nTruth\r\nNot defined\r\nLie\r\nTruth\r\nTruth\r\nLie\r\nLie\r\n']}, {'input': '14 3\r\n+14\r\n+12\r\n-9\r\n+9\r\n-9\r\n-9\r\n+8\r\n+9\r\n+2\r\n+1\r\n-13\r\n-9\r\n+13\r\n+3\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nLie\r\nLie\r\nLie\r\n']}, {'input': '10 4\r\n-9\r\n-8\r\n-5\r\n-9\r\n-7\r\n-9\r\n-9\r\n-9\r\n-4\r\n-9\r\n', 'output': ['Lie\r\nTruth\r\nTruth\r\nLie\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '10 5\r\n-10\r\n-10\r\n-10\r\n-5\r\n-1\r\n+10\r\n-3\r\n-10\r\n-9\r\n-10\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '10 4\r\n-3\r\n-3\r\n-3\r\n-3\r\n-3\r\n-2\r\n-2\r\n-6\r\n-7\r\n-3\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\n']}, {'input': '10 6\r\n-9\r\n-7\r\n-5\r\n-5\r\n-4\r\n-2\r\n-8\r\n-5\r\n-5\r\n-9\r\n', 'output': ['Truth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\n']}, {'input': '10 4\r\n-8\r\n-2\r\n-8\r\n+1\r\n-4\r\n-8\r\n-2\r\n-8\r\n-8\r\n-1\r\n', 'output': ['Lie\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nLie\r\nTruth\r\nLie\r\nLie\r\nTruth\r\n']}, {'input': '10 2\r\n-8\r\n+10\r\n+1\r\n+8\r\n+4\r\n+8\r\n+6\r\n-8\r\n+10\r\n+1\r\n', 'output': ['Not defined\r\nLie\r\nLie\r\nNot defined\r\nLie\r\nNot defined\r\nLie\r\nNot defined\r\nLie\r\nLie\r\n']}, {'input': '10 3\r\n+9\r\n+3\r\n+8\r\n+3\r\n+6\r\n-3\r\n+6\r\n+8\r\n+3\r\n+7\r\n', 'output': ['Lie\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nNot defined\r\nLie\r\n']}, {'input': '10 8\r\n-2\r\n+9\r\n+9\r\n-4\r\n+9\r\n+9\r\n+4\r\n-9\r\n-3\r\n+9\r\n', 'output': ['Truth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nTruth\r\n']}, {'input': '10 7\r\n-4\r\n+6\r\n+4\r\n+9\r\n+6\r\n+6\r\n+6\r\n+6\r\n+6\r\n+2\r\n', 'output': ['Truth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\n']}, {'input': '10 4\r\n+3\r\n+5\r\n+6\r\n+10\r\n+5\r\n+5\r\n+6\r\n+8\r\n+5\r\n+6\r\n', 'output': ['Lie\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '10 7\r\n-6\r\n-10\r\n-3\r\n-1\r\n-3\r\n-7\r\n-2\r\n-7\r\n-7\r\n-3\r\n', 'output': ['Truth\r\nTruth\r\nNot defined\r\nTruth\r\nNot defined\r\nNot defined\r\nTruth\r\nNot defined\r\nNot defined\r\nNot defined\r\n']}, {'input': '10 5\r\n-4\r\n-4\r\n-1\r\n-5\r\n-7\r\n-4\r\n-4\r\n-4\r\n-1\r\n-7\r\n', 'output': ['Lie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\n']}, {'input': '10 5\r\n-9\r\n-7\r\n-6\r\n-3\r\n-10\r\n-10\r\n-10\r\n-10\r\n-10\r\n-2\r\n', 'output': ['Truth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\nLie\r\nLie\r\nTruth\r\n']}, {'input': '10 3\r\n-10\r\n-10\r\n-10\r\n-3\r\n-10\r\n-10\r\n-10\r\n-8\r\n-4\r\n-10\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nTruth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nLie\r\n']}, {'input': '10 5\r\n-8\r\n-8\r\n-4\r\n-9\r\n-10\r\n-2\r\n-9\r\n-8\r\n-8\r\n-8\r\n', 'output': ['Lie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\n']}, {'input': '10 5\r\n+7\r\n+8\r\n+9\r\n+1\r\n+7\r\n+7\r\n+7\r\n+6\r\n+6\r\n+7\r\n', 'output': ['Truth\r\nLie\r\nLie\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\n']}, {'input': '10 5\r\n+2\r\n+2\r\n+2\r\n+2\r\n+9\r\n+10\r\n+8\r\n+7\r\n+4\r\n+2\r\n', 'output': ['Truth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nLie\r\nLie\r\nLie\r\nTruth\r\n']}, {'input': '10 9\r\n+7\r\n+7\r\n+7\r\n+7\r\n+7\r\n+7\r\n+5\r\n+7\r\n+7\r\n+7\r\n', 'output': ['Truth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nTruth\r\nLie\r\nTruth\r\nTruth\r\nTruth\r\n']}, {'input': '10 3\r\n+10\r\n+2\r\n+10\r\n+9\r\n+1\r\n+9\r\n+4\r\n+9\r\n+3\r\n+2\r\n', 'output': ['Lie\r\nLie\r\nLie\r\nTruth\r\nLie\r\nTruth\r\nLie\r\nTruth\r\nLie\r\nLie\r\n']}, {'input': '10 6\r\n+10\r\n+10\r\n+10\r\n+3\r\n+10\r\n+10\r\n+6\r\n+6\r\n+10\r\n+8\r\n', 'output': ['Truth\r\nTruth\r\nTruth\r\nLie\r\nTruth\r\nTruth\r\nLie\r\nLie\r\nTruth\r\nLie\r\n']}, {'input': '3 2\r\n-1\r\n+2\r\n+3\r\n', 'output': ['Truth\r\nNot defined\r\nNot defined\r\n']}]


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include <float.h>

/*---*/

#define FOR(i, b, e)	for(i = (b); i <= (e); i++)
#define FORL(i, b, e)	for(i = (b); i < (e); i++)
#define FORD(i, e, b)	for(i = (e); i >= (b); i--)
#define FOR0(i, e)		FORL(i, 0, e)

#define min(a, b)		(((a) < (b)) ? (a) : (b))
#define max(a, b)		(((a) > (b)) ? (a) : (b))
#define MINA(a, b)		do { if ((a) > (b)) (a) = (b); } while(0)
#define MAXA(a, b)		do { if ((a) < (b)) (a) = (b); } while(0)
#define MINA2(a, b, i, j)		do { if ((a) > (b)) { (a) = (b); (i) = (j); } } while(0)
#define MAXA2(a, b, i, j)		do { if ((a) < (b)) { (a) = (b); (i) = (j); } } while(0)

#define SWAP(a, b)		do { int _t = a; a = b; b = _t; } while(0)
#define SWAPT(a, b, t)	do { t _t = a; a = b; b = _t; } while(0)
#define sqr(a)			((a) * (a))

#define INT int

#define INT_CAP 0x3F3F3F3F

/*---*/

#define NMAX	4096

char a[NMAX], b[NMAX];
INT vv[NMAX][NMAX];
int lasta[256], lastb[256];

int tr, td, ti, te;

int editdistance(int n1, int n2)
{
	int i1, i2, la, lb;
	INT m;
	FOR(i2, 0, n2)
		vv[0][i2] = i2 * ti;
	memset(lasta, 0xFF, sizeof(lasta));
	FOR(i1, 1, n1) {
		vv[i1][0] = i1 * td;
		memset(lastb, 0xFF, sizeof(lastb));
		FOR(i2, 1, n2) {
			if (a[i1 - 1] == b[i2 - 1])
				m = vv[i1-1][i2-1];
			else
				m = vv[i1-1][i2-1] + tr;
			MINA(m, vv[i1-1][i2] + td);
			MINA(m, vv[i1][i2-1] + ti);
			lb = lastb[a[i1 - 1]];
			la = lasta[b[i2 - 1]];
			if (la >= 0 && lb >= 0)
				MINA(m, vv[la][lb] + te + (i2 - lb - 2) * ti + (i1 - la - 2) * ti);
			vv[i1][i2] = m;
			lastb[b[i2 - 1]] = i2 - 1;
		}
		lasta[a[i1 - 1]] = i1 - 1;
	}
	return vv[n1][n2];
}

int main()
{
	int la, lb;
#ifndef ONLINE_JUDGE
char ss[1024];
int cc;
#endif
#ifndef ONLINE_JUDGE
while((cc = getchar()) != EOF) {
	ungetc(cc, stdin);
#endif
	scanf("%d %d %d %d\n", &ti, &td, &tr, &te);
	gets(a);
	gets(b);
	la = strlen(a);
	lb = strlen(b);
	printf("%d\n", editdistance(la, lb));
#ifndef ONLINE_JUDGE
	printf("\n");
}
#endif
	return 0;
}

You are given a sequence of balls A by your teacher, each labeled with a lowercase Latin letter 'a'-'z'. You don't like the given sequence. You want to change it into a new sequence, B that suits you better. So, you allow yourself four operations:  You can insert any ball with any label into the sequence at any position.  You can delete (remove) any ball from any position.  You can replace any ball with any other ball.  You can exchange (swap) two adjacent balls. Your teacher now places time constraints on each operation, meaning that an operation can only be performed in certain time. So, the first operation takes time ti, the second one takes td, the third one takes tr and the fourth one takes te. Also, it is given that 2·te ≥ ti + td.Find the minimal time to convert the sequence A to the sequence B.

The first line contains four space-separated integers ti, td, tr, te (0 < ti, td, tr, te ≤ 100). The following two lines contain sequences A and B on separate lines. The length of each line is between 1 and 4000 characters inclusive.

Print a single integer representing minimum time to convert A into B.

NoteIn the second sample, you could delete the ball labeled 'a' from the first position and then insert another 'a' at the new second position with total time 6. However exchanging the balls give total time 3.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int dp[4100][4100];
char a[4100], b[4100]; int as, bs;
int cost[4];
int nextA[4100][26], nextB[4100][26];

int solve(int A,int B){
  int i,j,k;
  int res = 1000000000, tmp;

  if(dp[A][B] >= 0) return dp[A][B];
  if(A==0 && B==0) return dp[A][B] = 0;
  if(A==0 || B==0) return dp[A][B] = cost[1] * A + cost[0] * B;
  if(a[A-1] == b[B-1]) return dp[A][B] = solve(A-1,B-1);

  tmp = cost[1] + solve(A-1,B);
  if(res > tmp) res = tmp;

  tmp = cost[0] + solve(A,B-1);
  if(res > tmp) res = tmp;

  tmp = cost[2] + solve(A-1,B-1);
  if(res > tmp) res = tmp;

  if(A>=2 && B>=2){
    j = nextA[A-1][b[B-1]-'a'];
    k = nextB[B-1][a[A-1]-'a'];
    if(j>=0 && k>=0){
      tmp = (A-j-2)*cost[1] + (B-k-2)*cost[0] + cost[3];
      tmp += solve(j,k);
      if(res > tmp) res = tmp;
    }
  }

  return dp[A][B] = res;
}

int main(){
  int i,j,k,l,m,n;

  while(scanf("%d%d%d%d%s%s",cost,cost+1,cost+2,cost+3,a,b)==6){
    for(i=0;;i++) if(a[i]<' ') break; as = i;
    for(i=0;;i++) if(b[i]<' ') break; bs = i;

    rep(i,26) nextA[0][i] = -1;
    rep(k,as){
      nextA[k][a[k]-'a'] = k;
      rep(i,26) nextA[k+1][i] = nextA[k][i];
    }

    rep(i,26) nextB[0][i] = -1;
    rep(k,bs){
      nextB[k][b[k]-'a'] = k;
      rep(i,26) nextB[k+1][i] = nextB[k][i];
    }

    rep(i,as+1) rep(j,bs+1) dp[i][j] = -1;
    k = solve(as,bs);
    printf("%d\n",k);
  }

  return 0;
}

[{'input': '1 1 1 1\r\nyoushouldnot\r\nthoushaltnot\r\n', 'output': ['5\r\n']}, {'input': '2 4 10 3\r\nab\r\nba\r\n', 'output': ['3\r\n']}, {'input': '1 10 20 30\r\na\r\nza\r\n', 'output': ['1\r\n']}, {'input': '2 6 10 4\r\nmanthan\r\nmathmania\r\n', 'output': ['12\r\n']}, {'input': '5 1 5 3\r\nabhishek\r\naurbhishek\r\n', 'output': ['10\r\n']}, {'input': '1 1 100 1\r\nabcd\r\nwxyz\r\n', 'output': ['8\r\n']}, {'input': '4 6 5 5\r\ntest\r\ntsar\r\n', 'output': ['15\r\n']}, {'input': '2 4 10 3\r\nmanthan\r\nnahtnam\r\n', 'output': ['15\r\n']}, {'input': '2 4 100 3\r\nabcd\r\nbade\r\n', 'output': ['9\r\n']}, {'input': '5 5 1 5\r\nab\r\nba\r\n', 'output': ['2\r\n']}, {'input': '3 5 100 4\r\ntets\r\ntesat\r\n', 'output': ['7\r\n']}, {'input': '9 1 10 5\r\nabcd\r\ndbca\r\n', 'output': ['20\r\n']}, {'input': '2 2 10 2\r\nabcd\r\nadcb\r\n', 'output': ['6\r\n']}, {'input': '8 4 3 10\r\neekoowppum\r\njgmtlmeuigztfctikegvakfbgbafytbputmeppkuuyjkhagdsftfoueitnhjllolevgpmbtlworxojyzpfdaasckcqqdwdwwrklb\r\n', 'output': ['729\r\n']}, {'input': '5 5 1 82\r\nzmtuomrwesrhvudogmalwhtktfcbglnpbdntqssjcrdwlerfccmkmmhsfgaocchxdxxmeiaunhellstnhnjjmbynmqlzuupbnkrw\r\nglpfybikah\r\n', 'output': ['453\r\n']}, {'input': '20 21 22 84\r\notirbgzzphwwxqtgqyxympiumuyoupngwabrprmyhjpkgpjmez\r\nozvimdjwsyuacgbekgcw\r\n', 'output': ['872\r\n']}, {'input': '4 5 6 62\r\nqibkeavwalpn\r\nmaasevryyxzzqpdcgydb\r\n', 'output': ['89\r\n']}, {'input': '9 8 7 67\r\nejohycekbciwahqybwiy\r\ncpxgvbiwnuytmbwjvrzalydjjgdcmczejwesqrrh\r\n', 'output': ['299\r\n']}, {'input': '44 55 22 80\r\nabcdefghijklmnopqrstuvwxyz\r\nabcdefghijklmnopqrstuvwxyz\r\n', 'output': ['0\r\n']}, {'input': '63 9 44 72\r\nabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabdabd\r\nabdabdabdabdabdabdabdabdabdabdabtdabdabdabdabdabdabdabdabdabdabdabdabdabd\r\n', 'output': ['63\r\n']}, {'input': '1 3 4 2\r\nabc\r\ncxa\r\n', 'output': ['6\r\n']}, {'input': '1 1 1 1\r\nacccb\r\nba\r\n', 'output': ['4\r\n']}, {'input': '1 1 1 1\r\nab\r\nbca\r\n', 'output': ['2\r\n']}, {'input': '1 1 1 1\r\nba\r\nacb\r\n', 'output': ['2\r\n']}, {'input': '1 1 100 1\r\nafffb\r\nbggga\r\n', 'output': ['7\r\n']}, {'input': '1 1 1 1\r\nab\r\nbccccccccccccccccccca\r\n', 'output': ['20\r\n']}, {'input': '100 1 100 60\r\nacb\r\nba\r\n', 'output': ['61\r\n']}]