Problem: cf1425_f
Difficulty: medium
Categories: math
Time limit: 2000 ms, memory limit: 256 MB

This is an interactive problem. You have to use a flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush().

Mr. Chanek wants to buy a flamingo to accompany his chickens on his farm. Before going to the pet shop, Mr. Chanek stops at an animal festival to have fun. It turns out there is a carnival game with a flamingo as the prize.

There are $N$ mysterious cages, which are numbered from $1$ to $N$. Cage $i$ has $A_i$ $(0 \le A_i \le 10^3)$ flamingoes inside $(1 \le i \le N)$. However, the game master keeps the number of flamingoes inside a secret. To win the flamingo, Mr. Chanek must guess the number of flamingoes in each cage.

Coincidentally, Mr. Chanek has $N$ coins. Each coin can be used to ask once, what is the total number of flamingoes inside cages numbered $L$ to $R$ inclusive? With $L < R$.

Input

Use standard input to read the responses of your questions.

Initially, the judge will give an integer $N$ $(3 \le N \le 10^3)$, the number of cages, and the number of coins Mr. Chanek has.

For each of your questions, the jury will give an integer that denotes the number of flamingoes from cage $L$ to $R$ inclusive.

If your program does not guess the flamingoes or ask other questions, you will get "Wrong Answer". Of course, if your program asks more questions than the allowed number, your program will get "Wrong Answer".

Output

To ask questions, your program must use standard output.

Then, you can ask at most $N$ questions. Questions are asked in the format "? L R", ($1 \le L < R \le N$).

To guess the flamingoes, print a line that starts with "!" followed by $N$ integers where the $i$-th integer denotes the number of flamingo in cage $i$. After answering, your program must terminate or will receive the "idle limit exceeded" verdict. You can only guess the flamingoes once.

Examples

Input

```
6  
   
5  
   
15  
   
10  
 
```

Output

```
   
? 1 2  
   
? 5 6  
   
? 3 4  
   
! 1 4 4 6 7 8
```

Note

In the sample input, the correct flamingoes amount is $[1, 4, 4, 6, 7, 8]$.

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;

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

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

static bool readLL(long long &x) {
    if (!(cin >> x)) return false;
    return true;
}

static bool readToken(string &s) {
    if (!(cin >> s)) return false;
    return true;
}

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

    int N = 0;
    vector<int> A;
    vector<long long> pref;

    try {
        N = inf.readInt(3, 1000, "N");
        A.assign(N + 1, 0);
        for (int i = 1; i <= N; i++) A[i] = inf.readInt(0, 1000, ("A[" + to_string(i) + "]").c_str());

        pref.assign(N + 1, 0);
        for (int i = 1; i <= N; i++) pref[i] = pref[i - 1] + A[i];

        query_limit = N;
        log_metrics(); 

        cout << N << endl; 
    } catch (const std::exception& e) {
        
        finish(_pe, string("Interactor failed to read hidden input: ") + e.what());
    } catch (...) {
        finish(_pe, "Interactor failed to read hidden input (unknown error)");
    }

    const long long hard_cap = max(200000LL, 10LL * query_limit);

    bool guessed = false;
    while (true) {
        string cmd;
        if (!readToken(cmd)) {
            if (!guessed) finish(_wa, "Contestant terminated without providing final answer");
            finish(_pe, "Unexpected EOF after final answer"); 
        }

        if (cmd == "?") {
            long long L, R;
            if (!readLL(L) || !readLL(R)) finish(_pe, "Malformed query: expected '? L R'");

            if (!(1 <= L && L < R && R <= N)) {
                finish(_wa, "Invalid query range");
            }

            queries++;
            if (queries > hard_cap) finish(_pe, "Too many queries (hard cap exceeded)");

            long long ans = pref[(int)R] - pref[(int)L - 1];
            cout << ans << endl;
        } else if (cmd == "!") {
            if (guessed) finish(_pe, "Final answer given more than once");
            guessed = true;

            vector<long long> out(N + 1, 0);
            for (int i = 1; i <= N; i++) {
                long long v;
                if (!readLL(v)) finish(_pe, "Malformed final answer: expected N integers after '!'");
                out[i] = v;
            }

            for (int i = 1; i <= N; i++) {
                if (out[i] != A[i]) {
                    finish(_wa, "Wrong final answer");
                }
            }
            finish(_ok, "OK");
        } else {
            finish(_pe, "Unknown command (expected '?' or '!')");
        }
    }
}


Adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

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

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

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

static vector<int> buildDeterministicArray(int N, int Amin, int Amax) {
    vector<int> a(N + 1, 0);
    int range = Amax - Amin + 1;
    if (range <= 0) range = 1;

    if (range == 1) {
        for (int i = 1; i <= N; i++) a[i] = Amin;
        return a;
    }

    a[1] = Amin;
    if (N >= 2) a[2] = Amax;
    if (N >= 3) a[3] = min(Amax, Amin + 1);

    for (int i = 4; i <= N; i++) {
        long long x = i;
        long long v = (x * x * 37LL + x * 12345LL + 67890LL + 97LL * N) % range;
        a[i] = Amin + (int)v;
    }

    
    if (N >= 4) a[N] = Amax;
    if (N >= 5) a[N - 1] = Amin;

    return a;
}

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

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N = inf.readInt();
    if (N < 3 || N > 1000) finish(_pe, "Invalid N in input file");

    vector<int> rest;
    while (!inf.seekEof()) {
        rest.push_back(inf.readInt());
        if ((int)rest.size() > 1000000) finish(_pe, "Too many tokens in input file");
    }

    vector<int> A(N + 1, 0);
    if ((int)rest.size() >= N) {
        for (int i = 1; i <= N; i++) A[i] = rest[i - 1];
        for (int i = 1; i <= N; i++) {
            if (A[i] < 0 || A[i] > 1000) finish(_pe, "Ai out of [0,1000] in input file");
        }
    } else if ((int)rest.size() == 2) {
        int Amin = rest[0], Amax = rest[1];
        if (Amin < 0 || Amax > 1000 || Amin > Amax) finish(_pe, "Invalid Amin/Amax in input file");
        A = buildDeterministicArray(N, Amin, Amax);
    } else {
        finish(_pe, "Unrecognized input schema (expected N ints or Amin Amax)");
    }

    vector<long long> pref(N + 1, 0);
    for (int i = 1; i <= N; i++) pref[i] = pref[i - 1] + A[i];

    query_limit = N;
    cout << N << "\n" << flush;

    log_metrics(); 

    long long hard_cap = max(200000LL, 10LL * query_limit);

    for (;;) {
        string t;
        if (!(cin >> t)) {
            finish(_wa, "Contestant terminated without final answer");
        }

        if (t == "?") {
            long long L, R;
            if (!(cin >> L >> R)) finish(_pe, "Malformed query: expected '? L R'");
            if (!(1 <= L && L < R && R <= N)) finish(_pe, "Query out of range or L>=R");

            queries++;
            if (queries > hard_cap) finish(_pe, "Too many queries (hard cap exceeded)");

            long long ans = pref[(int)R] - pref[(int)L - 1];
            cout << ans << "\n" << flush;
        } else if (t == "!") {
            vector<long long> g(N + 1, 0);
            for (int i = 1; i <= N; i++) {
                if (!(cin >> g[i])) finish(_pe, "Malformed final answer: expected N integers after '!'");
            }

            for (int i = 1; i <= N; i++) {
                if (g[i] != A[i]) {
                    finish(_wa, "Wrong final answer");
                }
            }
            finish(_ok, "OK");
        } else {
            finish(_pe, "Unexpected token (expected '?' or '!')");
        }
    }
}


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

static vector<int> makeCorner(int which, int N) {
    vector<int> a(N + 1, 0);
    if (which == 1) {
        
        for (int i = 1; i <= N; i++) a[i] = 0;
    } else if (which == 2) {
        
        for (int i = 1; i <= N; i++) a[i] = (i & 1) ? 1000 : 0;
    } else {
        
        for (int i = 1; i <= N; i++) {
            int v = (i % 7) * (i % 5);
            a[i] = min(1000, v * 37);
        }
    }
    return a;
}

static vector<int> makeHard(int N) {
    vector<int> a(N + 1, 0);

    
    int patternType = rnd.next(1, 5);
    if (patternType == 1) {
        
        for (int i = 1; i <= N; i++) a[i] = 0;
        int spikes = max(3, N / 20);
        for (int k = 0; k < spikes; k++) {
            int pos = rnd.next(1, N);
            a[pos] = rnd.next(850, 1000);
        }
        
        int L = rnd.next(1, max(1, N - 20));
        int len = rnd.next(5, min(20, N - L + 1));
        for (int i = L; i < L + len; i++) a[i] = max(a[i], rnd.next(700, 1000));
    } else if (patternType == 2) {
        
        for (int i = 1; i <= N; i++) {
            int base = (i & 1) ? 1000 : 0;
            int noise = rnd.next(0, 30);
            a[i] = (base == 1000) ? max(0, base - noise) : min(1000, base + noise);
        }
    } else if (patternType == 3) {
        
        int i = 1;
        while (i <= N) {
            int segLen = rnd.next(1, 40);
            int v;
            if (rnd.next(0, 9) == 0) v = rnd.next(0, 1000);
            else v = rnd.next(0, 200) + (rnd.next(0, 1) ? 0 : rnd.next(600, 800));
            for (int j = 0; j < segLen && i <= N; j++, i++) a[i] = min(1000, max(0, v));
        }
    } else if (patternType == 4) {
        
        for (int i = 1; i <= N; i++) {
            long long x = i;
            long long v = (x * x + 1234567LL) % 1001; 
            v = (v + (i % 37) * 7) % 1001;
            v = (v + rnd.next(0, 50)) % 1001;
            a[i] = (int)v;
        }
    } else {
        
        int peak = rnd.next(max(2, N / 3), max(2, 2 * N / 3));
        int cur = rnd.next(0, 50);
        for (int i = 1; i <= N; i++) {
            int dir = (i <= peak) ? 1 : -1;
            int step = rnd.next(0, 25);
            if (rnd.next(0, 4) == 0) step = 0; 
            cur += dir * step;
            cur = max(0, min(1000, cur));
            
            if (rnd.next(0, 49) == 0) cur = rnd.next(0, 1000);
            a[i] = cur;
        }
    }

    return a;
}

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

    string mode = opt<string>("mode", "non");

    
    int N;
    long long seed = 1;
    if (argc >= 2) {
        
        
        
        try {
            seed = stoll(string(argv[1]));
        } catch (...) {
            seed = 1;
        }
    }

    if (seed == 1) N = 3;
    else if (seed == 2) N = 1000;
    else if (seed == 3) N = 7;
    else {
        
        int roll = rnd.next(0, 99);
        if (roll < 70) N = rnd.next(900, 1000);
        else if (roll < 90) N = rnd.next(200, 900);
        else N = rnd.next(3, 200);
    }

    if (mode == "non") {
        vector<int> a;
        if (seed == 1) a = makeCorner(1, N);
        else if (seed == 2) a = makeCorner(2, N);
        else if (seed == 3) a = makeCorner(3, N);
        else a = makeHard(N);

        
        
        cout << N << "\n";
        for (int i = 1; i <= N; i++) {
            if (i > 1) cout << ' ';
            cout << a[i];
        }
        cout << "\n";
        return 0;
    } else if (mode == "adp") {
        
        
        
        int Amin = 0, Amax = 1000;

        
        if (seed == 1) { Amin = 0; Amax = 0; N = 3; }
        else if (seed == 2) { Amin = 0; Amax = 1000; N = 1000; }
        else if (seed == 3) { Amin = 0; Amax = 3; N = 7; }
        else {
            
            int t = rnd.next(0, 99);
            if (t < 20) { Amin = 0; Amax = 1; }
            else if (t < 45) { Amin = 0; Amax = 7; }
            else if (t < 70) { Amin = 0; Amax = 50; }
            else { Amin = 0; Amax = 1000; }
        }

        
        cout << N << "\n" << Amin << " " << Amax << "\n";
        return 0;
    } else {
        
        vector<int> a = makeHard(N);
        cout << N << "\n";
        for (int i = 1; i <= N; i++) {
            if (i > 1) cout << ' ';
            cout << a[i];
        }
        cout << "\n";
        return 0;
    }
}