#include <bits/stdc++.h>
#include "doll.h"
using namespace std;
#define ll long long
#define vb vector<bool>
#define pb push_back
#define ff(aa, bb, cc) for(int aa = bb; aa < cc; aa++)
#define vl vector<ll>
#define pll pair<ll, ll>
#define fi first
#define se second
#define ed "\n"
#define all(aaa) aaa.begin(), aaa.end()
#define rall(aaa) aaa.rbegin(), aaa.rend()
ll MOD = 1e9+7;
/*
namespace {
constexpr int P_MAX = 20000000;
constexpr int S_MAX = 400000;
int M, N;
std::vector<int> A;
bool answered;
int S;
std::vector<int> IC, IX, IY;
int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}
void wrong_answer(const char *MSG) {
  printf("Wrong Answer: %s\n", MSG);
  exit(0);
}
void simulate() {
  if (S > S_MAX) {
    char str[50];
    sprintf(str, "over %d switches", S_MAX);
    wrong_answer(str);
  }
  for (int i = 0; i <= M; ++i) {
    if (!(-S <= IC[i] && IC[i] <= M)) {
      wrong_answer("wrong serial number");
    }
  }
  for (int j = 1; j <= S; ++j) {
    if (!(-S <= IX[j - 1] && IX[j - 1] <= M)) {
      wrong_answer("wrong serial number");
    }
    if (!(-S <= IY[j - 1] && IY[j - 1] <= M)) {
      wrong_answer("wrong serial number");
    }
  }
  int P = 0;
  std::vector<bool> state(S + 1, false);
  int pos = IC[0];
  int k = 0;
  FILE *file_log = fopen("out", "w");
  //fprintf(file_log, "0\n");
  for (;;) {
   // fprintf(file_log, "%d\n", pos);
    if (pos < 0) {
      if (++P > P_MAX) {
        fclose(file_log);
        char str[50];
        sprintf(str, "over %d inversions", P_MAX);
        wrong_answer(str);
      }
      state[-pos] = !state[-pos];
      pos = state[-pos] ? IX[-(1 + pos)] : IY[-(1 + pos)];
    } else {
      if (pos == 0) {
        break;
      }
      if (k >= N) {
       // fclose(file_log);
        wrong_answer("wrong motion");
      }
      if (pos != A[k++]) {
        //fclose(file_log);
        wrong_answer("wrong motion");
      }
      pos = IC[pos];
    }
  }
  fclose(file_log);
  if (k != N) {
    wrong_answer("wrong motion");
  }
  for (int j = 1; j <= S; ++j) {
    if (state[j]) {
      wrong_answer("state 'Y'");
    }
  }
  printf("OK");
}
}  // namespace
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y) {
  if (answered) {
    wrong_answer("answered not exactly once");
  }
  answered = true;
  // check if input format is correct
  if ((int)C.size() != M + 1) {
    wrong_answer("wrong array length");
  }
  if (X.size() != Y.size()) {
    wrong_answer("wrong array length");
  }
  S = X.size();
  IC = C;
  IX = X;
  IY = Y;
}
*/
vl build(vl arr, ll sz, ll last){
	if(arr.size() >= sz){
		return arr;
	}
	vl a;
	ff(i, 0, arr.size()){
		a.pb(arr[i]);
		a.pb(arr[i]+last);
	}
	return build(a, sz, last*2);
}
void build_switch(vector<int> &x, vector<int> &y, ll cant, ll maxx, ll tot, ll last){
	if(cant+1 > maxx){
		return;
	}
	//cout << "X.size  " << x.size() << ed;
	if(x.size() == 0){
		x.pb(-2);
		y.pb(-3);
		build_switch(x, y, cant+1, maxx, tot*2, 4);
		return;
	}
	ll id = last;
	ll n = log2(x.size()+1);
	//cout << "N " << n << ed;
	ff(i, 0, tot){
		//cout << "SWITCH -> " << -id << " " << -id-1 << ed;
		x.pb(-id);
		y.pb(-(id+1));
		id += 2;
	}
	//cout << "CAPA " << cant << ed;
	build_switch(x, y, cant+1, maxx, tot*2, id);
}
void create_circuit(int m, std::vector<int> a){
	vl count(m+1);
	ll n = a.size();
	ll maxxx = 0;
	ff(i, 0, n){
		count[a[i]]++;
		maxxx = max(maxxx, count[a[i]]);
	}
	vector<int> connections(m+1, 0);
	vector<int> x, y;
	
	ll switch_id = 0; ////////////////////////////////////////////////////////
	connections[0] = a[0];
	map<ll, ll> visits;
	map<ll, pair<ll, pll>> groups;
	//cout << "START " << 0 << " TO  " << a[0] << ed << ed;
	if(maxxx <= 4){
		//cout << "MAX <= 4" << ed;
		ff(i, 0, n){
			//cout << a[i] << " ";
			if(count[a[i]] == 1){
				//cout << "ONE   -> ";
				if(i == n-1){
					//cout << "LAST  TO ";
					connections[a[i]] = 0;
					//cout << connections[a[i]] << ed;
				}
				else{
					//cout << "CONNECTED TO " << a[i+1] << ed;
					connections[a[i]] = a[i+1];
				}
				//cout << connections[a[i]] << ed;
			}
			else if(count[a[i]] == 2){
				//cout << "TWO " << " ";
				visits[a[i]]++;
				if(i == n-1){
					//cout << "LAST -> switch  " << connections[a[i]] << " TO " << 0 << ed;
					ll aa = groups[a[i]].fi;
					y[aa]  = 0;
					//cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
				}
				else if(visits[a[i]] == 1){
					groups[a[i]] = {switch_id, {-1, -1}};
					//cout << "NEW -> switch " << -switch_id-1 << ed;
					x.pb(0);
					y.pb(0);
					ll aa = groups[a[i]].fi;
					connections[a[i]] = -(aa+1);
					x[aa] = a[i+1];
					switch_id++;
					
					//cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
				}
				else if(visits[a[i]] == 2){ //2da salida
					//cout << "SECOND -> switch  " << connections[a[i]] << ed;
					ll aa = groups[a[i]].fi;
					y[aa]  = a[i+1];
					//cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
				}
			}
			else if(count[a[i]] == 4){
				//cout << "FOUR ";
				visits[a[i]]++;
				if(i == n-1){
					ll cur = groups[a[i]].se.se;
					y[cur] = 0;
					//cout << "LAST -> switch  " << -(cur+1) << " TO " << 0 << ed;
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;/*
					cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
				else if(visits[a[i]] == 1){
					groups[a[i]] = {switch_id, {switch_id+1, switch_id+2}};
					//cout << "NEW -> switch " << -switch_id-1 << " TO " << -(switch_id+1)-1 << " AND " << -(switch_id+2)-1 << ed;
					x.pb(0);
					y.pb(0);
					x.pb(0);
					y.pb(0);
					x.pb(0);
					y.pb(0);
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					connections[a[i]] = -(aa+1);
					x[aa] = -(bb+1);
					y[aa] = -(cc+1);
					x[bb] = a[i+1];
					switch_id += 3;
					/*
					cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
					
				}
				else if(visits[a[i]] == 2){
					//cout << "SECOND TIME" << ed;
					ll cur = groups[a[i]].se.se;
					x[cur] = a[i+1];/*
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
				else if(visits[a[i]] == 3){
					//cout << "THIRD TIME" << ed;
					ll cur = groups[a[i]].se.fi;
					y[cur] = a[i+1];/*
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
				else if(visits[a[i]] == 4){
					//cout << "FOURTH TIME" << ed;
					ll cur = groups[a[i]].se.se;
					y[cur] = a[i+1];/*
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
			}
			else if(count[a[i]] == 3){
				//cout << "THREE ";
				visits[a[i]]++;
				if(i == n-1){
					ll cur = groups[a[i]].se.se;
					y[cur] = 0;
					/*cout << "LAST -> switch  " << -(cur+1) << " TO " << 0 << ed;
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
				else if(visits[a[i]] == 1){
					groups[a[i]] = {switch_id, {switch_id+1, switch_id+2}};
					//cout << "NEW -> switch " << -switch_id-1 << " TO " << -(switch_id+1)-1 << " AND " << -(switch_id+2)-1 << ed;
					x.pb(0);
					y.pb(0);
					x.pb(0);
					y.pb(0);
					x.pb(0);
					y.pb(0);
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					switch_id += 3;
					connections[a[i]] = -(aa+1);
					x[aa] = -(bb+1);
					y[aa] = -(cc+1);
					
					x[bb] = -(aa+1);//ciclo
					x[cc] = a[i+1];
					/*cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
				else if(visits[a[i]] == 2){
					//cout << "SECOND TIME" << ed;
					ll cur = groups[a[i]].se.fi;
					y[cur] = a[i+1];
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					/*cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
				else if(visits[a[i]] == 3){
					//cout << "THIRD TIME" << ed;
					ll cur = groups[a[i]].se.se;
					y[cur] = a[i+1];
					ll aa = groups[a[i]].fi, bb = groups[a[i]].se.fi, cc = groups[a[i]].se.se;
					/*cout << "CON " << -aa-1 << "  -> X " << x[aa] << ", Y " << y[aa] << ed;
					cout << "CON " << -bb-1 << "  -> X " << x[bb] << ", Y " << y[bb] << ed;
					cout << "CON " << -cc-1 << "  -> X " << x[cc] << ", Y " << y[cc] << ed;*/
				}
			}
			//cout << ed << ed;
		}
	}
	else{
		ff(i, 1, m+1){
			connections[i] = -1;
		}
		vl arr = build({1}, n, 1);
		/*ff(i, 0, arr.size()){
			cout << arr[i] << ed;
		}
		cout << "TAMAÑO " << arr.size() << ed;*/
		ll cant_max = log2(arr.size());
		//cout << "CAPAS " << cant_max << ed;
		//cout << "START " << x.size() << " " << y.size() << " " << 0 << " " << cant_max-1 << ed;
		build_switch(x, y, 0, cant_max-1, 1, 0);
		ff(i, 0, arr.size()){
			//cout << "CUR " << i << " " << arr[i] << ed;
			if(i == arr.size()-1){
				//cout << "A" << ed;
				y.pb(0);
				break;
			}
			if(i % 2 == 0){
				//cout << "X  ";
				if(a.size() <= arr[i]){
					//cout << "NO" << ed;
					x.pb(-1);
				}
				else{
					//cout << "SI" << ed;
					//cout << a[arr[i]] << ed << ed;
					x.pb(a[arr[i]]);
				}
			}
			else{
				//cout << "Y  ";
				if(a.size() <= arr[i]){
					//cout << "NO" << ed;
					y.pb(-1);
				}
				else{
					//cout << "SI" << ed;
					//cout << a[arr[i]] << ed << ed;
					y.pb(a[arr[i]]);
				}
			}
		}
	}
	
	/*ff(i, 0, m+1){
		cout << connections[i] << "  ";
	}
	cout << ed << ed;
	ff(i, 0, x.size()){
		cout << i << " ";
	}
	cout << ed;
	ff(i, 0, x.size()){
		cout << x[i] << " ";
	}
	cout << ed;
	ff(i, 0, x.size()){
		cout << y[i] << " ";
	}
	cout << ed;*/
	answer(connections, x, y);
}
/*
int main() {
  M = read_int();
  N = read_int();
  A.resize(N);
  for (int k = 0; k < N; ++k) {
    A[k] = read_int();
  }
  answered = false;
  create_circuit(M, A);
  if (!answered) {
    wrong_answer("answered not exactly once");
  }
  FILE *file_out = fopen("out.txt", "w");
 // fprintf(file_out, "%d\n", S);
  for (int i = 0; i <= M; ++i) {
    //fprintf(file_out, "%d\n", IC[i]);
  }
  for (int j = 1; j <= S; ++j) {
    //fprintf(file_out, "%d %d\n", IX[j - 1], IY[j - 1]);
  }
  //fclose(file_out);
  simulate();
  return 0;
}
*/
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |