Problem: arc179_c
Difficulty: medium
Categories: greedy, data structures, search
Time limit: 2000 ms, memory limit: 1024 MB

### Problem Statement

This is an **interactive problem** (where your program interacts with the judge via input and output).

You are given a positive integer $N$.

The judge has a hidden positive integer $R$ and $N$ integers $A_1, A_2, \dots, A_N$. It is guaranteed that $|A_i|\le R$ and $\left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R$.

There is a blackboard on which you can write integers with absolute values not exceeding $R$. Initially, the blackboard is empty.

The judge has written the values $A_1, A_2, \dots, A_N$ on the blackboard **in this order**. Your task is to make the blackboard contain only one value $\displaystyle\sum_{i=1}^{N}A_i$.

You cannot learn the values of $R$ and $A_i$ directly, but you can interact with the judge up to $25000$ times.

For a positive integer $i$, let $X_i$ be the $i$-th integer written on the blackboard. Specifically, $X_i = A_i$ for $i=1,2,\dots,N$.

In one interaction, you can specify two distinct positive integers $i$ and $j$ and choose one of the following actions:

* Perform addition. The judge will erase $X_i$ and $X_j$ from the blackboard and write $X_i + X_j$ on the blackboard.
  + $|X_i + X_j| \leq R$ must hold.
* Perform comparison. The judge will tell you whether $X_i < X_j$ is true or false.

Here, at the beginning of each interaction, the $i$-th and $j$-th integers written on the blackboard must not have been erased.

Perform the interactions appropriately so that after all interactions, the blackboard contains only one value $\displaystyle\sum_{i=1}^{N}A_i$.

The values of $R$ and $A_i$ are determined before the start of the conversation between your program and the judge.

### Constraints

* $2 \leq N \leq 1000$
* $1 \leq R \leq 10^9$
* $|A_i| \leq R$
* $\left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R$
* $N$, $R$, and $A_i$ are integers.

### Input and Output

This is an **interactive problem** (where your program interacts with the judge via input and output).

First, read $N$ from Standard Input.

```
$N$
```

Next, repeat the interactions until the blackboard contains only one value $\displaystyle\sum_{i=1}^{N}A_i$.

When performing addition, make an output in the following format to Standard Output. Append a newline at the end. Here, $i$ and $j$ are distinct positive integers.

```
+ $i$ $j$
```

The response from the judge will be given from Standard Input in the following format:

```
$P$
```

Here, $P$ is an integer:

* If $P \geq N + 1$, it means that the value $X_i + X_j$ has been written on the blackboard, and it is the $P$-th integer written.
* If $P = -1$, it means that $i$ and $j$ do not satisfy the constraints, or the number of interactions has exceeded $25000$.

When performing comparison, make an output in the following format to Standard Output. Append a newline at the end. Here, $i$ and $j$ are distinct positive integers.

```
? $i$ $j$
```

The response from the judge will be given from Standard Input in the following format:

```
$Q$
```

Here, $Q$ is an integer:

* If $Q = 1$, it means that $X_i < X_j$ is true.
* If $Q = 0$, it means that $X_i < X_j$ is false.
* If $Q = -1$, it means that $i$ and $j$ do not satisfy the constraints, or the number of interactions has exceeded $25000$.

For both types of interactions, if the judge's response is $-1$, your program is already considered incorrect. In this case, terminate your program immediately.

When the blackboard contains only one value $\displaystyle\sum_{i=1}^{N}A_i$, report this to the judge in the following format. This does not count towards the number of interactions. Then, terminate your program immediately.

```
!
```

If you make an output in a format that does not match any of the above, `-1` will be given from Standard Input.

```
-1
```

In this case, your program is already considered incorrect. Terminate your program immediately.

### Notes

* **For each output, append a newline at the end and flush Standard Output. Otherwise, the verdict may be TLE.**
* Terminate your program immediately after outputting the result (or receiving `-1`). Otherwise, the verdict will be indeterminate.
* Extra newlines will be considered as malformed output.

---

### Sample Input and Output

Here is a possible conversation with $N=3, R=10, A_1=-1, A_2=10, A_3=1$.

| Input | Output | Explanation |
| --- | --- | --- |
| `3` |  | First, the integer $N$ is given. |
|  | `? 1 2` | Perform a comparison. |
| `1` |  | The judge returns $1$ because $X_1\lt X_2\ (-1\lt 10)$. |
|  | `+ 1 3` | Perform an addition. |
| `4` |  | The judge erases $X_1 = -1$ and $X_3 = 1$ from the blackboard and writes $X_1 + X_3 = 0$. This is the fourth integer written. |
|  | `+ 2 4` | Perform an addition. |
| `5` |  | The judge erases $X_2 = 10$ and $X_4 = 0$ from the blackboard and writes $X_2 + X_4 = 10$. This is the fifth integer written. |
|  | `!` | The blackboard contains only one value $\displaystyle\sum_{i=1}^{N}A_i$, so report this to the judge. |

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

using namespace std;


static long long queries = 0;
static const long long query_limit = 25000;


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


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

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

    
    int N = inf.readInt(2, 1000, "N");
    long long R = inf.readLong(1LL, 1000000000LL, "R");
    
    vector<long long> A(N);
    long long expected_sum = 0;
    for (int i = 0; i < N; ++i) {
        A[i] = inf.readLong(-R, R, format("A[%d]", i));
        expected_sum += A[i];
    }
    
    
    if (abs(expected_sum) > R) {
        quitf(_fail, "Test case invalid: Sum of A exceeds R");
    }

    
    log_metrics();

    
    cout << N << endl;

    
    
    
    vector<long long> values;
    values.reserve(30000);
    values.push_back(0); 
    for (long long x : A) values.push_back(x);

    
    vector<bool> active(N + 1, true);

    int hard_cap = 30000; 

    while (true) {
        string type;
        
        if (!(cin >> type)) {
            finish(_wa, "Unexpected end of output from contestant");
        }

        
        if (type == "!") {
            int count_active = 0;
            long long remaining_val = 0;
            for (size_t i = 1; i < active.size(); ++i) {
                if (active[i]) {
                    count_active++;
                    remaining_val = values[i];
                }
            }

            if (count_active == 1) {
                if (remaining_val == expected_sum) {
                    finish(_ok, "Correct: One element remaining matching sum");
                } else {
                    finish(_wa, format("Wrong Answer: Remaining element %lld != expected sum %lld", remaining_val, expected_sum));
                }
            } else {
                finish(_wa, format("Wrong Answer: %d elements remaining (expected 1)", count_active));
            }
        }

        
        queries++;
        
        if (queries % 100 == 0) log_metrics();

        
        if (queries > query_limit) {
            cout << -1 << endl;
            finish(_wa, "Query limit exceeded");
        }

        if (type == "+") {
            int i, j;
            if (!(cin >> i >> j)) {
                finish(_wa, "Malformed addition query");
            }
            
            
            if (i <= 0 || i >= (int)values.size() || j <= 0 || j >= (int)values.size()) {
                cout << -1 << endl;
                finish(_wa, format("Index out of bounds: %d %d", i, j));
            }
            if (i == j) {
                cout << -1 << endl;
                finish(_wa, format("Indices must be distinct: %d", i));
            }
            if (!active[i] || !active[j]) {
                cout << -1 << endl;
                finish(_wa, format("Index already erased: %d or %d", i, j));
            }

            long long s = values[i] + values[j];
            if (abs(s) > R) {
                cout << -1 << endl;
                finish(_wa, format("Sum %lld exceeds R=%lld", s, R));
            }

            
            active[i] = false;
            active[j] = false;
            values.push_back(s);
            active.push_back(true);
            
            
            cout << (values.size() - 1) << endl;

        } else if (type == "?") {
            int i, j;
            if (!(cin >> i >> j)) {
                finish(_wa, "Malformed comparison query");
            }

            
            if (i <= 0 || i >= (int)values.size() || j <= 0 || j >= (int)values.size()) {
                cout << -1 << endl;
                finish(_wa, format("Index out of bounds: %d %d", i, j));
            }
            if (i == j) {
                cout << -1 << endl;
                finish(_wa, format("Indices must be distinct: %d", i));
            }
            if (!active[i] || !active[j]) {
                cout << -1 << endl;
                finish(_wa, format("Index already erased: %d or %d", i, j));
            }

            
            int res = (values[i] < values[j] ? 1 : 0);
            cout << res << endl;

        } else {
            
            cout << -1 << endl;
            finish(_wa, format("Unknown query type: %s", type.c_str()));
        }

        
        if (queries > hard_cap) {
            finish(_pe, "Hard loop limit exceeded");
        }
    }

    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 seed_val = 0;
    if (argc > 1) {
        seed_val = atoi(argv[1]);
    }

    int N;
    long long R;
    vector<long long> A;

    
    if (seed_val == 1) {
        
        N = 2;
        R = 10;
        A = {-5, 5};
    } else if (seed_val == 2) {
        
        N = 1000;
        R = 1000000000;
        A.resize(N);
        for (int i = 0; i < N; ++i) A[i] = (i % 2 == 0 ? R : -R);
    } else if (seed_val == 3) {
        
        N = 3;
        R = 10;
        A = {-1, 10, 1};
    } else {
        
        
        
        if (rnd.next(2)) N = rnd.next(900, 1000); 
        else N = rnd.next(2, 1000);

        
        if (rnd.next(4) == 0) R = rnd.next(N + 5, 10000); 
        else R = rnd.next(100000000LL, 1000000000LL); 

        A.resize(N);
        int type = rnd.next(1, 5);

        if (type == 1) { 
            
            for (int i = 0; i < N; ++i) A[i] = rnd.next(-R, R);
        } else if (type == 2) { 
            
            for (int i = 0; i < N; ++i) {
                long long v = rnd.next(max(1LL, R - R / 50), R);
                if (rnd.next(2)) v = -v;
                A[i] = v;
            }
        } else if (type == 3) { 
            
            long long limit = max(1LL, R / N);
            for (int i = 0; i < N; ++i) A[i] = rnd.next(-limit, limit);
        } else if (type == 4) {
            
            for (int i = 0; i < N; ++i) {
                long long v = rnd.next(1LL, R);
                if (i % 2 == 1) v = -v;
                A[i] = v;
            }
        } else {
            
            for (int i = 0; i < N; ++i) {
                if (rnd.next(3) == 0) A[i] = 0;
                else A[i] = rnd.next(-R, R);
            }
        }

        
        
        for (int iter = 0; iter < 50000; ++iter) {
            long long current_sum = 0;
            for (long long x : A) current_sum += x;
            
            if (abs(current_sum) <= R) break;

            int idx = rnd.next(N);
            
            
            long long ideal = A[idx] - current_sum;
            
            
            if (ideal > R) ideal = R;
            if (ideal < -R) ideal = -R;
            
            A[idx] = ideal;
        }

        
        long long final_s = 0;
        for (long long x : A) final_s += x;
        if (abs(final_s) > R) {
            for (int i = 0; i < N; ++i) A[i] = rnd.next(-R/N, R/N);
        }
    }

    
    shuffle(A.begin(), A.end());

    if (mode == "adp") {
        
        cout << N << " " << R << endl;
    } else {
        
        cout << N << " " << R << endl;
        for (int i = 0; i < N; ++i) {
            cout << A[i] << (i == N - 1 ? "" : " ");
        }
        cout << endl;
    }

    return 0;
}