Problem: cf1672_e
Difficulty: medium
Categories: greedy, search
Time limit: 1000 ms, memory limit: 256 MB

This is an interactive problem.

There are $n$ words in a text editor. The $i$-th word has length $l_i$ ($1 \leq l_i \leq 2000$). The array $l$ is hidden and only known by the grader.

The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.

More formally, suppose that the text editor has width $w$. Let $a$ be an array of length $k+1$ where $1=a_1 < a_2 < \ldots < a_{k+1}=n+1$. $a$ is a valid array if for all $1 \leq i \leq k$, $l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w$. Then the height of the text editor is the minimum $k$ over all valid arrays.

Note that if $w < \max(l_i)$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $0$ instead.

You can ask $n+30$ queries. In one query, you provide a width $w$. Then, the grader will return the height $h_w$ of the text editor when its width is $w$.

Find the minimum area of the text editor, which is the minimum value of $w \cdot h_w$ over all $w$ for which $h_w \neq 0$.

The lengths are fixed in advance. In other words, the interactor is not adaptive.

Input

The first and only line of input contains a single integer $n$ ($1 \leq n \leq 2000$)  — the number of words on the text editor.

It is guaranteed that the hidden lengths $l_i$ satisfy $1 \leq l_i \leq 2000$.

Interaction

Begin the interaction by reading $n$.

To make a query, print "? $w$" (without quotes, $1 \leq w \leq 10^9$). Then you should read our response from standard input, that is, $h_w$.

If your program has made an invalid query or has run out of tries, the interactor will terminate immediately and your program will get a verdict Wrong answer.

To give the final answer, print "! $area$" (without the quotes). Note that giving this answer is not counted towards the limit of $n+30$ queries.

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.

Example

Input

```
6

0

4

2
```

Output

```
? 1

? 9

? 16

! 32
```

Note

In the first test case, the words are $\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}$, so $l=\{5,2,7,3,5,6\}$.

If $w=1$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $h_1=0$, so the grader will return $0$.

If $w=9$, then a possible way that the words will be displayed on the text editor is:

* $\texttt{glory__to}$
* $\texttt{ukraine__}$
* $\texttt{and_anton}$
* $\texttt{__trygub_}$

The height of the text editor is $h_{9}=4$, so the grader will return $4$.

If $w=16$, then a possible way that the words will be displayed on the text editor is:

* $\texttt{glory_to_ukraine}$
* $\texttt{and_anton_trygub}$

The height of the text editor is $h_{16}=2$, so the grader will return $2$.

We have somehow figured out that the minimum area of the text editor is $32$, so we answer it.

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 0;





void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}


void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}



int calculate_height(const vector<int>& l, long long w, int max_l) {
    if (w < max_l) return 0;
    
    int lines = 1;
    long long current_len = 0;
    
    for (int val : l) {
        if (current_len == 0) {
            
            current_len = val;
        } else {
            
            if (current_len + 1 + val <= w) {
                current_len += 1 + val;
            } else {
                
                lines++;
                current_len = val;
            }
        }
    }
    return lines;
}

int main(int argc, char** argv) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    int n = inf.readInt();
    vector<int> l(n);
    int max_l = 0;
    long long sum_l = 0;
    for (int i = 0; i < n; i++) {
        l[i] = inf.readInt();
        if (l[i] > max_l) max_l = l[i];
        sum_l += l[i];
    }

    
    query_limit = n + 30;
    
    
    log_metrics();

    
    
    
    
    
    long long correct_area = -1;
    long long max_possible_w = sum_l + n; 

    for (int h = 1; h <= n; h++) {
        
        long long low = max_l;
        long long high = max_possible_w;
        long long best_w = -1;
        
        
        long long est = (sum_l + h - 1) / h;
        if (est > low) low = est;
        if (low < max_l) low = max_l; 

        while (low <= high) {
            long long mid = low + (high - low) / 2;
            int res = calculate_height(l, mid, max_l);
            if (res != 0 && res <= h) {
                best_w = mid;
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        if (best_w != -1) {
            
            int actual_h = calculate_height(l, best_w, max_l);
            if (actual_h != 0) {
                long long area = best_w * actual_h;
                if (correct_area == -1 || area < correct_area) {
                    correct_area = area;
                }
            }
        }
    }
    
    
    cout << n << endl;

    
    long long hard_cap = 200000; 
    
    while (true) {
        if (queries > hard_cap) {
            finish(_pe, "Hard query limit exceeded");
        }

        string token;
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF (did not receive answer)");
        }

        bool is_query = false;
        bool is_ans = false;
        long long val = -1;

        
        if (token == "?") {
            is_query = true;
            if (!(cin >> val)) finish(_wa, "Expected width after '?'");
        } else if (token == "!") {
            is_ans = true;
            if (!(cin >> val)) finish(_wa, "Expected answer after '!'");
        } else if (!token.empty() && token[0] == '!') {
            is_ans = true;
            try {
                if (token.size() == 1) {
                     if (!(cin >> val)) finish(_wa, "Expected answer after '!'");
                } else {
                     val = stoll(token.substr(1));
                }
            } catch (...) {
                finish(_wa, "Invalid answer format");
            }
        } else if (!token.empty() && token[0] == '?') {
            is_query = true;
            try {
                if (token.size() == 1) {
                     if (!(cin >> val)) finish(_wa, "Expected width after '?'");
                } else {
                     val = stoll(token.substr(1));
                }
            } catch (...) {
                finish(_wa, "Invalid query format");
            }
        } else {
            
            try {
                val = stoll(token);
                is_query = true;
            } catch (...) {
                finish(_wa, "Invalid token (expected integer or '?'/'!')");
            }
        }

        if (is_ans) {
            if (val == correct_area) {
                finish(_ok, "Correct area found: " + to_string(val));
            } else {
                finish(_wa, "Incorrect area. Expected " + to_string(correct_area) + ", got " + to_string(val));
            }
        } else if (is_query) {
            
            queries++;
            
            
            if (queries % 10 == 0) log_metrics();

            
            if (val < 1 || val > 1000000000LL) {
                 finish(_wa, "Query width out of bounds [1, 10^9]");
            }
            
            
            int h = calculate_height(l, val, max_l);
            cout << h << endl;
        }
    }

    return 0;
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    string mode = opt<string>("mode", "non");
    int n = opt<int>("n", -1);

    
    string seedStr = (argc > 1) ? string(argv[1]) : "";
    
    vector<int> l;
    bool specific = false;

    
    if (n == -1) {
        if (seedStr == "1") {
            
            n = 6;
            l = {5, 2, 7, 3, 5, 6};
            specific = true;
        } else if (seedStr == "2") {
            
            n = 2000;
            l.assign(n, 2000);
            specific = true;
        } else if (seedStr == "3") {
            
            n = 2000;
            l.assign(n, 1);
            specific = true;
        } else {
            
            
            if (rnd.next(10) == 0) {
                n = rnd.next(1, 100);
            } else {
                n = rnd.next(1000, 2000);
            }
        }
    }

    
    if (!specific) {
        l.resize(n);
        
        
        
        
        
        
        
        
        int strat = rnd.next(7);

        if (strat == 0) {
            for (int i = 0; i < n; i++) l[i] = rnd.next(1, 2000);
        } else if (strat == 1) {
            for (int i = 0; i < n; i++) l[i] = rnd.next(1, 10);
        } else if (strat == 2) {
            for (int i = 0; i < n; i++) l[i] = rnd.next(1900, 2000);
        } else if (strat == 3) {
            for (int i = 0; i < n; i++) l[i] = (rnd.next(2) ? 1 : 2000);
        } else if (strat == 4) {
            for (int i = 0; i < n; i++) {
                long long val = 1 + (1999LL * i) / (n > 1 ? n - 1 : 1);
                l[i] = (int)val;
            }
        } else if (strat == 5) {
            for (int i = 0; i < n; i++) {
                long long val = 2000 - (1999LL * i) / (n > 1 ? n - 1 : 1);
                l[i] = (int)val;
            }
        } else {
            int mean = rnd.next(1, 2000);
            int dev = rnd.next(10, 500);
            for (int i = 0; i < n; i++) {
                
                int val = mean + rnd.next(-dev, dev) + rnd.next(-dev, dev);
                val = max(1, min(2000, val));
                l[i] = val;
            }
        }

        
        
        if (rnd.next(5) != 0) {
            shuffle(l.begin(), l.end());
        }
    }

    
    if (mode == "non") {
        
        cout << n << endl;
        for (int i = 0; i < n; i++) {
            cout << l[i] << (i == n - 1 ? "" : " ");
        }
        cout << endl;
    } else {
        
        cout << n << endl;
    }

    return 0;
}