Uva-11111 (generalized matrioshkas)

Source: Internet
Author: User

Vladimir worked for years makingMatrioshkas, Those nesting dolls that certainly represent truly Russian craft. A Matrioshka is a doll that may be opened in two halves, so that one finds another doll
Inside. Then this doll may be opened to find another one inside it. This can be repeated several times, till a final doll-that cannot be opened-is reached.

Recently, Vladimir realized that the idea of nesting dolls might be generalized to nesting toys. indeed, he has designed toys that contain toys but in a more general sense. one of these toys may be opened in
Two halves and it may have more than one toy inside it. That is the new feature that Vladimir wants to introduce in his new line of toys.

Vladimir has developed a notation to describe how nesting toys shocould be constructed. A toy is represented with a positive integer, according to its size. More precisely: If when opening the toy represented
ByMWe find the toys representedN1,N2 ,...,NR, it
Must be true thatN1 +N2 +... +NR <M. And if this is the case, we say that toyMContains directly
ToysN1,N2 ,...,NR. It shoshould be clear that toys that may be contained in any of the toysN1,N2 ,...,NR are
Not considered as directly contained in the toyM.

AGeneralized MatrioshkaIs denoted with a non-empty sequence of non zero Integers of the form:

A1A2...AN

Such that toyKIs represented in the sequence with two integers-KAndK, With the negative one occurring in the sequence first that the positive one.

For example, the sequence

-9-7-2 2-3-2-1 1 2 3 7 9

Represents a generalized Matrioshka conformed by six toys, namely, 1, 2 (twice), 3, 7 and 9. Note that toy 7 contains
Directly Toys 2 and 3. Note that the first copy of toy 2 occurs left from the second one and that the second copy contains directly a toy 1. It wowould be
Wrong to understand that the first-2 and the last 2 shoshould be too red.

On the other hand, the following sequences do not describe generalized matrioshkas:

  • -9-7-2 2-3-1-2 2 1 3 7 9

    Because toy 2 is bigger than toy 1 and cannot be allocated inside it.

  • -9-7-2 2-3-2-1 1 2 3 7-2 2 9

    Because 7 and 2 may not be allocated together inside 9.

  • -9-7-2 2-3-1-2 3 2 1 7 9

    Because there is a nesting problem within toy 3.

Your problem is to write a program to help Vladimir telling good designs from bad ones.

Input

The input file contains several test cases, each one of them in a separate line. Each test case is a sequence of non zero integers, each one with an absolute value less than 107.

Output

Output texts for each input case are presented in the same order that input is read.

For each test case the answer must be a line of the form

Matrioshka!

If the design describes a generalized Matrioshka. In other case, the answer shoshould be of the form

Try again.

Sample Input

-9 -7 -2 2 -3 -2 -1 1 2 3 7 9-9 -7 -2 2 -3 -1 -2 2 1 3 7 9-9 -7 -2 2 -3 -1 -2 3 2 1 7 9-100 -50 -6 6 50 100-100 -50 -6 6 45 100-10 -5 -2 2 5 -4 -3 3 4 10-9 -5 -2 2 5 -4 -3 3 4 9

Sample output

   Matrioshka!   Try again.   Try again.   Matrioshka!   Try again.   Matrioshka!   Try again.

This question refers to giving a number to determine whether the relationship between the brackets and the criteria is met,

Inclusion relationship:

9-2 2,-7 7 9 (not compliant, 9 directly contains 2 and 7, but 9 is not greater than 2 + 7)

9-1 1-2 2 9 (compliant because 9> 1 + 2)

#include <iostream>#include<deque>#include<algorithm>#include<cstdio>#include<stack>#include<string>#include<vector>#include<map>#include<sstream>using namespace std;struct node{    int v,sum;    node(int a,int b):v(a),sum(b){}};bool check(vector<int>& v){    stack<node>st;    for(int i=0;i<v.size();i++)    {        if(v[i]<0)            st.push(node(v[i],v[i]));        else        {            if(!st.empty()&&st.top().v+v[i]==0)            {                st.pop();                if(!st.empty())                {                    node top=st.top();st.pop();                    top.sum+=v[i];                    if(top.sum>=0)return false;                        st.push(top);                }            }            else return false;        }    }    return st.empty();}int main(){    string line;    while(getline(cin,line))    {        vector<int>v;        int num;        istringstream in(line);        while(in>>num)v.push_back(num);        if(check(v))            cout<<":-) Matrioshka!"<<endl;        else cout<<":-( Try again."<<endl;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.