#include "richest.h"
#include <bits/stdc++.h>
using namespace std;
static int N, T, S, r, t, s;
static vector<int> W;
vector<int> ask(vector<int> i, vector<int> j) {
++t;
if (t > T)
throw string("Too many queries");
if (i.size() != j.size())
throw string("Invalid query: i and j must have the same size");
int m = i.size();
s += m;
if (s > S)
throw string("Too many total elements in queries");
vector<int> res(m);
for (int k = 0; k < m; k++) {
if (i[k] < 0 || i[k] >= N || j[k] < 0 || j[k] >= N)
throw string("Invalid query: index out of bounds");
res[k] = W[i[k]] > W[j[k]] ? i[k] : j[k];
}
return res;
}
int richest(int n, int t, int s){
vector<int> a,b,ret;
if(t==1){
assert(n==1000);
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
a.push_back(i);
b.push_back(j);
}
}
ret=ask(a,b);
int ans=0;
for(int i=0; i<(int)ret.size(); i++){
if(a[i]==ans) ans=ret[i];
}
return ans;
}
vector<int> alive,tmp;
for(int i=0; i<n; i++) alive.push_back(i);
long long can=1099950;
while((int)alive.size()>1){
int m=alive.size();
int buc=2;
if((long long)m*(m-1)/2<=can) buc=m;
else{
while((long long)(m+buc-1)/buc*buc*(buc-1)/2<can*4/7){
buc++;
if(buc>m) break;
}
buc--;
}
//cout << buc << '\n';
//cout << (m+buc-1)/buc*buc*(buc-1)/2 << '\n';
a.clear(); b.clear();
for(int i=0; i<m; i+=buc){
for(int j=0; j<buc; j++){
for(int k=j+1; k<buc; k++){
if(i+j>=m||i+j>=m) continue;
a.push_back(alive[i+j]);
b.push_back(alive[i+k]);
can--;
}
}
}
cout << a.size() << '\n';
ret=ask(a,b);
int cnt=0;
for(int i=0; i<m; i+=buc){
int best=alive[i];
for(int j=0; j<buc; j++){
for(int k=j+1; k<buc; k++){
if(i+j>=m||i+j>=m) continue;
if(a[cnt]==best) best=ret[cnt];
cnt++;
}
}
tmp.push_back(best);
}
swap(tmp,alive);
tmp.clear();
}
return alive[0];
}