Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem Statement

Nathan O. Davis is a student at the department of integrated systems.

Today's agenda in the class is audio signal processing.
Nathan was given a lot of homework out.
One of the homework was to write a program to process an audio signal.
He copied the given audio signal to his USB memory and brought it back to his home.

When he started his homework, he unfortunately dropped the USB memory to the floor.
He checked the contents of the USB memory and found that the audio signal data got broken.

There are several characteristics in the audio signal that he copied.

The audio signal is a sequence of $N$ samples.

Each sample in the audio signal is numbered from $1$ to $N$ and represented as an integer value.

Each value of the odd-numbered sample(s) is strictly smaller than the value(s) of its neighboring sample(s).

Each value of the even-numbered sample(s) is strictly larger than the value(s) of its neighboring sample(s).

He got into a panic and asked you for a help.
You tried to recover the audio signal from his USB memory but some samples of the audio signal are broken and could not be recovered.
Fortunately, you found from the metadata that all the broken samples have the same integer value.

Your task is to write a program,
which takes the broken audio signal extracted from his USB memory as its input,
to detect whether the audio signal can be recovered uniquely.

Input

The input consists of multiple datasets.
The form of each dataset is described below.

$N$
$a_{1}$ $a_{2}$ ... $a_{N}$

The first line of each dataset consists of an integer, $N (2 \le N \le 1{,}000)$.
$N$ denotes the number of samples in the given audio signal.
The second line of each dataset consists of $N$ values separated by spaces.
The $i$-th value, $a_{i}$, is either a character x or an integer between $-10^9$ and $10^9$, inclusive.
It represents the $i$-th sample of the broken audio signal.
If $a_{i}$ is a character x , it denotes that $i$-th sample in the audio signal is broken.
Otherwise it denotes the value of the $i$-th sample.

The end of input is indicated by a single $0$.
This is not included in the datasets.

You may assume that the number of the datasets does not exceed $100$.

Output

For each dataset, output the value of the broken samples in one line if the original audio signal can be recovered uniquely.
If there are multiple possible values, output ambiguous.
If there are no possible values, output none.

Sample Input

5
1 x 2 4 x
2
x x
2
1 2
2
2 1
2
1000000000 x
4
x 2 1 x
0

Output for the Sample Input

3
none
ambiguous
none
ambiguous
none

Solution in C++11 (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 2000000000;
using namespace std;

//?????????????????°???????????????
int toNum(string str, bool minus){
    int num = 0;
    rep(i,str.size()){
        num *= 10;
        num += str[i] - '0';
    }
    if(minus) num*=-1;
    return num;
}

void checkMinus(int a[1001], string s[1001], int n){
    rep(i,n){
        if(s[i][0] == '-'){
            a[i] = toNum(s[i].substr(1,s[i].size() - 1), 1);
        }else if(s[i][0] == 'x') {
            a[i] = INF;
        }else{
            a[i] = toNum(s[i], 0);
        }
    }
}

bool isDoubleX(int a[1001], int n){
    rep(i,n - 1){
        if(a[i] == INF && a[i + 1] == INF) return 1;
    }
    return 0;
}

bool isInpossible(int a[1001], int n){
    rep(i,n - 1){
        if(a[i + 1] == INF){
            i++;
            continue;
        } else{
            if(i % 2){
                if(a[i] <= a[i + 1]) return 1;
            }else{
                if(a[i] >= a[i + 1]) return 1;
            }
        }
    }
    return 0;
}

bool isNoneX(int a[1001], int n){
    bool c = true;
    rep(i,n){
        if(a[i] == INF) c = false;
    }
    return c;
}

int main(){
    int n;
    while(cin >> n, n){
        int a[1001];
        string s[1001];
        int ans[2] = {0,INF};
        rep(i,n) cin >> s[i];
        checkMinus(a, s, n);

        if(isDoubleX(a,n) || isInpossible(a,n)){
            cout << "none" << endl;
            continue;
        }else if(isNoneX(a,n)){
            cout << "ambiguous" << endl;
            continue;
        }else{
            rep(i,n){
                if(a[i] == INF && i != 0 && i != n - 1){
                    if(i % 2) a[0] = max(max(a[i - 1], a[i + 1]), a[0]);
                    else a[1] = min(min(a[i - 1], a[i + 1]), a[1]);
                }else if(a[i] == INF){
                    if(i % 2) a[0] = max(a[0], a[i + 1]);
                    else a[1] = min(a[1], a[i - 1]);
                }
            }
            //cout << a[0] << ' '  << a[1] << endl;
            //if(a[0] == 1000000000 || a[1] == -10000000000) cout << "none" << endl;
            if(a[1] - a[0] == 2) cout << a[1] - 1 << endl;
            else if(a[1] - a[0] > 2) cout << "ambiguous" << endl;
            else cout << "none" << endl;
        }
    }
}