Problem: cf1839_e
Difficulty: medium
Categories: game, greedy
Time limit: 2000 ms, memory limit: 256 MB

This is an interactive problem.

Consider the following game for two players:

* Initially, an array of integers $a_1, a_2, \ldots, a_n$ of length $n$ is written on blackboard.
* Game consists of rounds. On each round, the following happens:
  + The first player selects any $i$ such that $a_i \gt 0$. If there is no such $i$, the first player loses the game (the second player wins) and game ends.
  + The second player selects any $j \neq i$ such that $a_j \gt 0$. If there is no such $j$, the second player loses the game (the first player wins) and game ends.
  + Let $d = \min(a_i, a_j)$. The values of $a_i$ and $a_j$ are simultaneously decreased by $d$ and the next round starts.

It can be shown that game always ends after the finite number of rounds.

You have to select which player you will play for (first or second) and win the game.

Input

The first line contains a single integer $n$ ($1 \le n \le 300$) — the length of array $a$.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 300$) — array $a$.

Interaction

Interaction begins after reading $n$ and array $a$.

You should start interaction by printing a single line, containing either "First" or "Second", representing the player you select.

On each round, the following happens:

* If you are playing as the first player, you should print a single integer $i$ ($1 \le i \le n$) on a separate line. After that, you should read a single integer $j$ ($-1 \le j \le n$) written on a separate line.

  If $j = -1$, then you made an incorrect move. In this case your program should terminate immediately.

  If $j = 0$, then the second player can't make a correct move and you win the game. In this case your program should also terminate immediately.

  Otherwise $j$ is equal to the index chosen by the second player, and you should proceed to the next round.
* If you are playing as the second player, you should read a single integer $i$ ($-1 \le i \le n$) written on a separate line.

  If $i = -1$, then you made an incorrect move on the previous round (this cannot happen on the first round). In that case your program should terminate immediately.

  If $i = 0$, then the first player can't make a correct move and you win the game. In this case your program should also terminate immediately.

  Otherwise $i$ is equal to the index chosen by first player. In this case you should write single integer $j$ ($1 \le j \le n$) on a separate line and proceed to the next round.

After printing $i$ or $j$, do not forget to output the 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 the documentation for other languages.

Examples

Input

```
4
10 4 6 3

3

1

0
```

Output

```
First
1

2

4
```

Input

```
6
4 5 5 11 3 2

2

5

4

6

1

0
```

Output

```
Second 

4

4

3

1

3
```

Note

In the first example $n = 4$ and array $a$ is $[\, 10, 4, 6, 3 \,]$. The game goes as follows:

* After reading array $a$ contestant's program chooses to play as the first player and prints "First".
* First round: the first player chooses $i = 1$, the second player chooses $j = 3$. $d = \min(a_1, a_3) = \min(10, 6) = 6$ is calculated. Elements $a_1$ and $a_3$ are decreased by $6$. Array $a$ becomes equal to $[\, 4, 4, 0, 3 \,]$.
* Second round: the first player chooses $i = 2$, the second player chooses $j = 1$. $d = \min(a_2, a_1) = \min(4, 4) = 4$ is calculated. Elements $a_2$ and $a_1$ are decreased by $4$. Array $a$ becomes equal to $[\, 0, 0, 0, 3 \,]$.
* Third round: the first player chooses $i = 4$. There is no $j \neq 4$ such that $a_j > 0$, so the second player can't make a correct move and the first player wins. Jury's program prints $j = 0$. After reading it, contestant's program terminates.

In the second example $n = 6$ and array $a$ is $[\, 4, 5, 5, 11, 3, 2 \,]$. The game goes as follows:

* Contestant's program chooses to play as the second player and prints "Second".
* First round: $i = 2$, $j = 4$, $a = [\, 4, 0, 5, 6, 3, 2 \,]$.
* Second round: $i = 5$, $j = 4$, $a = [\, 4, 0, 5, 3, 0, 2 \,]$.
* Third round: $i = 4$, $j = 3$, $a = [\, 4, 0, 2, 0, 0, 2 \,]$.
* Fourth round: $i = 6$, $j = 1$, $a = [\, 2, 0, 2, 0, 0, 0 \,]$.
* Fifth round: $i = 1$, $j = 3$, $a = [\, 0, 0, 0, 0, 0, 0 \,]$.
* Sixth round: the first player can't make a correct move and the second player wins. Jury's program prints $i = 0$. After reading it, contestant's program terminates.

Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.

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


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

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());
}



static void die_wa(const string& msg) {
    cout << -1 << endl;
    finish(_wa, msg);
}

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

    
    int n = inf.readInt(1, 300, "n");
    vector<int> a(n);
    long long sum = 0;
    for(int i = 0; i < n; i++) {
        a[i] = inf.readInt(1, 300, "a_i");
        sum += a[i];
    }
    
    
    cout << n << endl;
    for(int i = 0; i < n; i++) {
        cout << a[i] << (i == n - 1 ? "" : " ");
    }
    cout << endl; 

    
    
    
    
    
    bool is_partitionable = false;
    vector<int> group(n, 0); 
    
    if (sum % 2 == 0) {
        int target = (int)(sum / 2);
        
        
        vector<bitset<90005>> history(n + 1);
        history[0][0] = 1;
        
        for(int i = 0; i < n; i++) {
            history[i+1] = history[i] | (history[i] << a[i]);
        }
        
        if (history[n][target]) {
            is_partitionable = true;
            group.assign(n, 2); 
            int curr = target;
            
            for(int i = n - 1; i >= 0; i--) {
                
                if (curr >= a[i] && history[i][curr - a[i]]) {
                    group[i] = 1;
                    curr -= a[i];
                }
            }
        }
    }

    
    string token;
    if (!(cin >> token)) finish(_wa, "No output for role choice");
    
    
    string choice = "";
    for (char c : token) choice += tolower(c);
    
    bool human_first = false;
    if (choice == "first") human_first = true;
    else if (choice == "second") human_first = false;
    else die_wa("Expected 'First' or 'Second', got: " + token);

    
    while (queries < query_limit) {
        queries++;
        if (queries % 10 == 0) log_metrics(); 

        int i_idx = -1; 
        int j_idx = -1; 

        if (human_first) {
            
            
            
            
            string t;
            if (!(cin >> t)) finish(_wa, "Unexpected end of stream waiting for move");
            if (t == "?") cin >> t; 
            
            try {
                i_idx = stoi(t);
            } catch (...) { die_wa("Invalid integer format: " + t); }

            
            if (i_idx < 1 || i_idx > n) die_wa("Index out of bounds: " + t);
            if (a[i_idx-1] == 0) die_wa("Selected index with value 0");

            
            
            
            
            
            vector<int> candidates;
            if (is_partitionable) {
                int g = group[i_idx-1]; 
                int target = 3 - g;     
                for(int k=0; k<n; ++k) {
                    if (group[k] == target && a[k] > 0) {
                        candidates.push_back(k+1);
                    }
                }
            } else {
                
                for(int k=0; k<n; ++k) {
                    if (k+1 != i_idx && a[k] > 0) {
                        candidates.push_back(k+1);
                    }
                }
            }

            
            int best_j = -1, max_v = -1;
            for(int c : candidates) {
                if (a[c-1] > max_v) {
                    max_v = a[c-1];
                    best_j = c;
                }
            }
            j_idx = best_j;

            if (j_idx == -1) {
                
                
                cout << 0 << endl;
                finish(_ok, "Contestant won (Second had no moves)");
            }

            cout << j_idx << endl;

        } else {
            
            
            
            
            
            
            
            
            int best_i = -1, max_v = -1;
            for(int k=0; k<n; ++k) {
                if (a[k] > 0 && a[k] > max_v) {
                    max_v = a[k];
                    best_i = k+1;
                }
            }
            i_idx = best_i;

            if (i_idx == -1) {
                
                
                cout << 0 << endl;
                finish(_ok, "Contestant won (First had no moves)");
            }

            cout << i_idx << endl;

            
            string t;
            if (!(cin >> t)) finish(_wa, "Unexpected end of stream waiting for response");
            if (t == "?") cin >> t; 
            
            try {
                j_idx = stoi(t);
            } catch (...) { die_wa("Invalid integer format: " + t); }

            if (j_idx == 0) {
                
                
                finish(_wa, "Contestant lost (printed 0)");
            }

            
            if (j_idx < 1 || j_idx > n) die_wa("Index out of bounds");
            if (j_idx == i_idx) die_wa("Same index chosen as First player");
            if (a[j_idx-1] == 0) die_wa("Selected index with value 0");
        }

        
        int d = min(a[i_idx-1], a[j_idx-1]);
        a[i_idx-1] -= d;
        a[j_idx-1] -= d;
    }

    finish(_pe, "Query limit exceeded");
}


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() {
    
    long long limit = (query_limit == 0 ? 1 : query_limit);
    tout << "queries=" << queries << endl;
    tout << "query_limit=" << limit << endl;
}


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




vector<int> get_partition(int n, const vector<int>& a) {
    long long sum = 0;
    for (int x : a) sum += x;
    if (sum % 2 != 0) return {};

    int target = (int)(sum / 2);
    
    
    vector<bitset<90005>> dp(n + 1);
    dp[0][0] = 1;

    for (int i = 0; i < n; ++i) {
        dp[i + 1] = dp[i] | (dp[i] << a[i]);
    }

    if (!dp[n][target]) return {};

    
    vector<int> side(n + 1, 0);
    int curr = target;
    for (int i = n - 1; i >= 0; --i) {
        
        if (dp[i][curr]) {
            side[i + 1] = 2; 
        } else {
            side[i + 1] = 1; 
            curr -= a[i];
        }
    }
    return side;
}

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

    
    int n = inf.readInt(1, 300, "n");
    vector<int> a(n);
    long long sum_a = 0;
    for (int i = 0; i < n; ++i) {
        a[i] = inf.readInt(1, 300, "a_i");
        sum_a += a[i];
    }

    
    
    query_limit = sum_a + 200;
    log_metrics(); 

    
    cout << n << endl;
    for (int i = 0; i < n; ++i) {
        cout << a[i] << (i == n - 1 ? "" : " ");
    }
    cout << endl; 

    
    
    
    vector<int> sides = get_partition(n, a);
    bool is_partitionable = !sides.empty();
    if (sides.empty()) {
        sides.assign(n + 1, 1); 
    }

    
    string choice;
    if (!(cin >> choice)) {
        finish(_wa, "Failed to read choice (First/Second)");
    }

    bool i_am_first = false;
    if (choice == "First") {
        i_am_first = false; 
    } else if (choice == "Second") {
        i_am_first = true; 
    } else {
        finish(_wa, "Invalid choice token: " + choice);
    }

    
    while (true) {
        queries++;
        if (queries % 10 == 0) log_metrics();
        
        
        if (queries > query_limit + 500) {
            finish(_pe, "Game did not terminate within reasonable limits");
        }

        int i_idx = -1;
        int j_idx = -1;

        if (i_am_first) {
            
            
            vector<int> candidates;
            for (int k = 1; k <= n; ++k) {
                if (a[k - 1] > 0) candidates.push_back(k);
            }

            if (candidates.empty()) {
                
                cout << 0 << endl;
                finish(_ok, "Interactor (First) exhausted moves. Contestant wins.");
            }

            
            
            
            int best_k = candidates[0];
            for (int k : candidates) {
                if (a[k - 1] > a[best_k - 1]) best_k = k;
            }
            i_idx = best_k;
            cout << i_idx << endl;

            
            if (!(cin >> j_idx)) {
                finish(_wa, "Failed to read j from contestant");
            }

            
            if (j_idx == 0) {
                finish(_wa, "Contestant (Second) cannot move. Interactor wins.");
            }
            if (j_idx == -1) {
                finish(_wa, "Contestant output -1 (invalid move signal)");
            }

            
            if (j_idx < 1 || j_idx > n) finish(_wa, "j out of bounds");
            if (j_idx == i_idx) finish(_wa, "j == i is not allowed");
            if (a[j_idx - 1] == 0) finish(_wa, "a[j] is 0 (invalid move)");

        } else {
            
            
            if (!(cin >> i_idx)) {
                finish(_wa, "Failed to read i from contestant");
            }

            if (i_idx == 0) {
                finish(_wa, "Contestant (First) cannot move. Interactor wins.");
            }
            if (i_idx == -1) {
                finish(_wa, "Contestant output -1");
            }
            if (i_idx < 1 || i_idx > n) finish(_wa, "i out of bounds");
            if (a[i_idx - 1] == 0) finish(_wa, "a[i] is 0 (invalid move)");

            
            
            int target_side = (sides[i_idx] == 1 ? 2 : 1);
            vector<int> candidates;

            if (is_partitionable) {
                for (int k = 1; k <= n; ++k) {
                    if (k != i_idx && a[k - 1] > 0 && sides[k] == target_side) {
                        candidates.push_back(k);
                    }
                }
            }

            
            if (candidates.empty()) {
                for (int k = 1; k <= n; ++k) {
                    if (k != i_idx && a[k - 1] > 0) {
                        candidates.push_back(k);
                    }
                }
            }

            if (candidates.empty()) {
                
                cout << 0 << endl;
                finish(_ok, "Interactor (Second) exhausted moves. Contestant wins.");
            }

            
            int best_j = candidates[0];
            for (int k : candidates) {
                if (a[k - 1] > a[best_j - 1]) best_j = k;
            }
            j_idx = best_j;
            cout << j_idx << endl;
        }

        
        int val = min(a[i_idx - 1], a[j_idx - 1]);
        a[i_idx - 1] -= val;
        a[j_idx - 1] -= val;
        
    }

    return 0;
}


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 = atoi(argv[1]);
    
    int n;
    vector<int> a;
    
    
    if (seed_val == 1) {
        
        n = 1;
        a = {1};
    } else if (seed_val == 2) {
        
        n = 2;
        a = {10, 10};
    } else if (seed_val == 3) {
        
        n = 2;
        a = {10, 20};
    } else if (seed_val == 4) {
        
        n = 300;
        a.assign(n, 300);
    } else if (seed_val == 5) {
        
        n = 50;
        a.assign(n, 300);
        a[0] = 299; 
    } else {
        
        n = rnd.next(1, 300);
        
        
        
        
        
        int type = rnd.next(0, 2);
        
        if (type == 1) {
            
            
            
            if (n == 1) n = 2; 
            
            a.clear();
            if (n % 2 == 0) {
                int pairs = n / 2;
                for (int i = 0; i < pairs; i++) {
                    int x = rnd.next(1, 300);
                    a.push_back(x);
                    a.push_back(x);
                }
            } else {
                int pairs = (n - 3) / 2;
                for (int i = 0; i < pairs; i++) {
                    int x = rnd.next(1, 300);
                    a.push_back(x);
                    a.push_back(x);
                }
                int x = rnd.next(1, 150); 
                a.push_back(x);
                a.push_back(x);
                a.push_back(2 * x);
            }
        } else {
            
            a.resize(n);
            long long sum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = rnd.next(1, 300);
                sum += a[i];
            }
            
            if (type == 2) {
                
                if (sum % 2 == 0) {
                    int idx = rnd.next(n);
                    if (a[idx] < 300) a[idx]++;
                    else a[idx]--;
                }
            }
        }
    }
    
    
    shuffle(a.begin(), a.end());
    
    
    cout << n << endl;
    println(a);
    
    return 0;
}