This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.There are $$$n$$$ players labelled from $$$1$$$ to $$$n$$$. It is guaranteed that $$$n$$$ is a multiple of $$$3$$$.Among them, there are $$$k$$$ impostors and $$$n-k$$$ crewmates. The number of impostors, $$$k$$$, is not given to you. It is guaranteed that $$$\frac{n}{3} < k < \frac{2n}{3}$$$.In each question, you can choose three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le a, b, c \le n$$$) and ask: "Among the players labelled $$$a$$$, $$$b$$$ and $$$c$$$, are there more impostors or more crewmates?" You will be given the integer $$$0$$$ if there are more impostors than crewmates, and $$$1$$$ otherwise.Find the number of impostors $$$k$$$ and the indices of players that are impostors after asking at most $$$n+6$$$ questions.The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first and only line of each test case contains a single integer $$$n$$$ ($$$6 \le n < 10^4$$$, $$$n$$$ is a multiple of $$$3$$$) — the number of players. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.

NoteExplanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):For the first test case:Question "? 1 2 3" returns $$$0$$$, so there are more impostors than crewmates among players $$$1$$$, $$$2$$$ and $$$3$$$.Question "? 3 4 5" returns $$$1$$$, so there are more crewmates than impostors among players $$$3$$$, $$$4$$$ and $$$5$$$.Outputting "! 3 4 1 2" means that one has found all the impostors, by some miracle. There are $$$k = 3$$$ impostors. The players who are impostors are players $$$4$$$, $$$1$$$ and $$$2$$$.For the second test case:Question "? 7 1 9" returns $$$1$$$, so there are more crewmates than impostors among players $$$7$$$, $$$1$$$ and $$$9$$$.Outputting "! 4 2 3 6 8" means that one has found all the impostors, by some miracle. There are $$$k = 4$$$ impostors. The players who are impostors are players $$$2$$$, $$$3$$$, $$$6$$$ and $$$8$$$.

import std.algorithm;
import std.conv;
import std.functional;
import std.range;
import std.stdio;
import std.string;

void solve (int n)
{
	assert (n % 3 == 0);

	int ask (int a, int b, int c)
	{
		writeln ("? ", a, " ", b, " ", c);
		stdout.flush ();
		return readln.strip.to !(int);
	}

	int [] q;
	for (int i = 0; i < n; i += 3)
	{
		q ~= ask (i + 1, i + 2, i + 3);
	}

	auto t0 = q.countUntil (0) * 3;
	auto t1 = q.countUntil (1) * 3;
	auto six = [t0 + 1, t0 + 2, t0 + 3, t1 + 1, t1 + 2, t1 + 3];
	int pos = 1;
	while (ask (six[pos + 0], six[pos + 1], six[pos + 2]) == 0)
	{
		pos += 1;
	}

	int p0 = six[pos - 1];
	int p1 = six[pos + 2];

	auto v = new bool [n + 1];
	for (int i = 0; i < n; i += 3)
	{
		auto three = [i + 1, i + 2, i + 3];
		if (q[i / 3] == 0)
		{
			int x = ask (three[0], three[1], p1);
			int y = ask (three[0], three[2], p1);
			int z = (x == 1 && y == 1) ? 0 :
			        (x == 0 && y == 0) ? 0 : 1;
			v[three[0]] = 0;
			v[three[1]] = 0;
			v[three[2]] = 0;
			if (x == 1 && y == 1) v[three[0]] = 1;
			if (x == 1 && z == 1) v[three[1]] = 1;
			if (y == 1 && z == 1) v[three[2]] = 1;
		}
		else
		{
			int x = ask (three[0], three[1], p0);
			int y = ask (three[0], three[2], p0);
			int z = (x == 0 && y == 0) ? 1 :
			        (x == 1 && y == 1) ? 1 : 0;
			v[three[0]] = 1;
			v[three[1]] = 1;
			v[three[2]] = 1;
			if (x == 0 && y == 0) v[three[0]] = 0;
			if (x == 0 && z == 0) v[three[1]] = 0;
			if (y == 0 && z == 0) v[three[2]] = 0;
		}
	}

	auto answer = iota (1, n + 1).filter !(i => v[i] == 0).array;
	writefln ("! %s %(%s %)", answer.length, answer);
	stdout.flush ();
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		solve (n);
	}
}


One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides, any two neighboring parts were painted different colors, that is, the red and the blue color were alternating, i. e. followed one after the other. The outer area of the wall (the area that lied outside all circles) was painted blue. Help Sherlock Holmes determine the total area of red parts of the wall.Let us remind you that two circles are called concentric if their centers coincide. Several circles are called concentric if any two of them are concentric.

The first line contains the single integer n (1 ≤ n ≤ 100). The second line contains n space-separated integers ri (1 ≤ ri ≤ 1000) — the circles' radii. It is guaranteed that all circles are different.

Print the single real number — total area of the part of the wall that is painted red. The answer is accepted if absolute or relative error doesn't exceed 10 - 4.

NoteIn the first sample the picture is just one circle of radius 1. Inner part of the circle is painted red. The area of the red part equals π × 12 = π.In the second sample there are three circles of radii 1, 4 and 2. Outside part of the second circle is painted blue. Part between the second and the third circles is painted red. Part between the first and the third is painted blue. And, finally, the inner part of the first circle is painted red. Overall there are two red parts: the ring between the second and the third circles and the inner part of the first circle. Total area of the red parts is equal (π × 42 - π × 22) + π × 12 = π × 12 + π = 13π

module cf_157B;

import std.stdio, std.algorithm;
import std.math;

void main() {
    int n;
    int[] radiuses;

    readf("%d", &n);
    radiuses = new int[n];
    for (int i = 0; i < n; ++i) {
        readf(" %d", &radiuses[i]);
    }

    sort(radiuses);

    bool red = (n % 2 != 0)? true: false;
    double prevSquare = 0.0, totalRedSquare = 0.0;

    for (int i = 0; i < n; ++i) {
        double square = PI * radiuses[i] * radiuses[i];

        if (red) {
            totalRedSquare += square - prevSquare;
        }
        prevSquare = square;
        red ^= true;
    }

    writefln("%.4f", totalRedSquare);
}


You are given an undirected tree, consisting of $$$n$$$ vertices.The vertex is called a leaf if it has exactly one vertex adjacent to it.The distance between some pair of vertices is the number of edges in the shortest path between them.Let's call some set of leaves beautiful if the maximum distance between any pair of leaves in it is less or equal to $$$k$$$.You want to split all leaves into non-intersecting beautiful sets. What is the minimal number of sets in such a split?

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$3 \le n \le 10^6$$$, $$$1 \le k \le 10^6$$$) — the number of vertices in the tree and the maximum distance between any pair of leaves in each beautiful set. Each of the next $$$n - 1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 \le v_i, u_i \le n$$$) — the description of the $$$i$$$-th edge.  It is guaranteed that the given edges form a tree.

Print a single integer — the minimal number of beautiful sets the split can have.

NoteHere is the graph for the first example:

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

const long MOD = 998244353;
const long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto G = new int[][](N);
    foreach (_; 0..N-1) {
        s = readln.split.map!(to!int);
        G[s[0] - 1] ~= s[1] - 1;
        G[s[1] - 1] ~= s[0] - 1;
    }

    int root = -1;
    foreach (i; 0..N) if (G[i].length > 1) root = i;
    int ans = 0;

    int[] dfs(int n, int p, int d) {
        if (G[n].length == 1) {
            return [1];
        }
        int[][] tmp;
        foreach (m; G[n]) if (m != p) tmp ~= dfs(m, n, d+1);
        tmp.sort!((a,b) => (a.length > b.length));
        foreach (i; 1..tmp.length) tmp[0] ~= tmp[i].dup;
        tmp[0].sort!"a > b";
        for (int i = 0; i + 1 < tmp[0].length; ++i) {
            while (i + 1 < tmp[0].length && tmp[0][i] + tmp[0].back <= K) tmp[0].popBack;
        }
        tmp[0].sort!"a < b";
        while (!tmp[0].empty && tmp[0].back == K) tmp[0].popBack, ans += 1;
        foreach (i; 0..tmp[0].length) tmp[0][i] += 1;
        return tmp[0];
    }

    auto ret =  dfs(root, -1, 1);
    ans += ret.length;
    ans.writeln;
}


A bracket sequence is a string, containing only characters "(", ")", "[" and "]".A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "([])" are correct (the resulting expressions are: "(1)+[1]", "([1+1]+1)"), and "](" and "[" are not. The empty string is a correct bracket sequence by definition.A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is the string slsl + 1... sr. The empty string is a substring of any string by definition.You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets «[» as possible.

The first and the only line contains the bracket sequence as a string, consisting only of characters "(", ")", "[" and "]". It is guaranteed that the string is non-empty and its length doesn't exceed 105 characters.

In the first line print a single integer — the number of brackets «[» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them.

//test
import std.c.stdio;

void CF138C()
{
    pure bool t (char c)
    {
        return (c == '(') || (c == ')');
    }
    char[100000] s;
    int len = 0;
    int cs = getchar();

    do
    {
        s[len] = cast (char) cs;
        cs = getchar();
        ++len;
    }
    while (32 < cs);

    auto n = len;
    int[100000] stack, ans, cnt;
    int cc = 0;

    for (int i = 0; i < n; ++i)
    {
        if ((s[i] == '(') || (s[i] == '['))
        {
            stack[cc] = i;
            ++cc;
        }
        else
        {
            if ((cc > 0) && (t (s[stack[cc - 1]]) == t (s[i])))
            {
                ans[stack[cc - 1]] = i;
                --cc;
            }
            else cc = 0;
        }
    }

    int cur = 0;

    while (cur < n)
    {
        if (ans[cur] > 0)
        {
            int end = ans[cur];

            while ((end < n - 1) && (ans[end+1] > 0))
                end = ans[end+1];

            ans[cur] = end;
            cur = end + 1;
        }
        else ++cur;
    }

    cnt[0] = s[0] == '[' ? 1 : 0;

    for (int i = 1; i < n; ++i)
        cnt[i] = cnt[i-1] + (s[i] == '[' ? 1 : 0);

    int count (int h)
    {
        if (ans[h] != 0)
            return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
        else return 0;
    }
    int tt = -1, m = 0;

    for (int i = 0; i < n; ++i)
        if (count (i) > m)
        {
            tt = i;
            m = count (i);
        }

    printf("%d\n", m);

    if (m > 0) printf("%.*s\n", s[tt..ans[tt] + 1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

char[] ns()
{
    sb();
    char[] res = [];
    do
    {
        res~=to!char(curc);
        curc = getchar();
    }
    while(32 < curc);
    return res;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = ns();
    auto n = s.length;
    int[] stack = [], ans = new int[n];
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            stack ~= i;
        else
        {
            if ((stack.length>0)&&(t(s[stack[stack.length-1]])==t(s[i])))
            {
                ans[stack[stack.length-1]] = i;
                --stack.length;
            }
            else stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    //writeln(ans);
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
         if (ans[h] != 0) return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
         else return 0;
    }
    int tt = -1, m = 0;
    for (int i = 0; i < n; ++i) if(count(i) > m){tt = i; m = count(i);}
    //writeln(s);
    writeln(m);
    if ((m>0)/*&&(ans[tt]-tt>1)*/)writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(cast(char)curc);
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

char[] ns()
{
    sb();
    char[] res = [];
    do
    {
        res~=to!char(curc);
        curc = getchar();
    }
    while(32 < curc);
    return res;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = nlongs();
    auto n = s.length;
    int[] stack = new int[n], ans = new int[n];
    int cc = 0;
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            {stack[cc]=i;++cc;}//stack ~= i;
        else
        {
            if ((/*stack.length*/cc>0)&&(t(s[stack[/*stack.length*/cc-1]])==t(s[i])))
            {
                ans[stack[/*stack.length*/cc-1]] = i;
                --cc;//--stack.length;
            }
            else cc=0;//stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    //writeln(ans);
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
         if (ans[h] != 0) return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
         else return 0;
    }
    int tt = -1, m = 0;
    for (int i = 0; i < n; ++i) if(count(i) > m){tt = i; m = count(i);}
    //writeln(s);
    writeln(m);
    if ((m>0)/*&&(ans[tt]-tt>1)*/)writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(cast(char)curc);
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

char[] ns()
{
    sb();
    char[] res = [];
    do
    {
        res~=to!char(curc);
        curc = getchar();
    }
    while(32 < curc);
    return res;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = nlongs();
    auto n = s.length;
    int[] stack = [], ans = new int[n];
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            stack ~= i;
        else
        {
            if ((stack.length>0)&&(t(s[stack[stack.length-1]])==t(s[i])))
            {
                ans[stack[stack.length-1]] = i;
                --stack.length;
            }
            else stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    //writeln(ans);
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
         if (ans[h] != 0) return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
         else return 0;
    }
    int tt = -1, m = 0;
    for (int i = 0; i < n; ++i) if(count(i) > m){tt = i; m = count(i);}
    //writeln(s);
    writeln(m);
    if ((m>0)/*&&(ans[tt]-tt>1)*/)writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = nlongs();
    auto n = s.length;
    int[] stack = [], ans = new int[n];
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            stack ~= i;
        else
        {
            if ((stack.length>0)&&(t(s[stack[stack.length-1]])==t(s[i])))
            {
                ans[stack[stack.length-1]] = i;
                --stack.length;
            }
            else stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    //writeln(ans);
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
         if (ans[h] != 0) return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
         else return 0;
    }
    int tt = -1, m = 0;
    for (int i = 0; i < n; ++i) if(count(i) > m){tt = i; m = count(i);}
    //writeln(s);
    writeln(m);
    if ((m>0)/*&&(ans[tt]-tt>1)*/)writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(cast(char)curc);
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

char[] ns()
{
    sb();
    char[] res = [];
    do
    {
        res~=to!char(curc);
        curc = getchar();
    }
    while(32 < curc);
    return res;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[100000] s;int len=0;int cs=getchar();do{s[len]=cast(char)cs;cs=getchar();++len;}while(32<cs);// = nlongs();
    auto n = len;//s.length;
    int[100000]stack,ans,cnt;//int[] stack = new int[n], ans = new int[n];
    int cc = 0;
    //int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            {stack[cc]=i;++cc;}//stack ~= i;
        else
        {
            if ((/*stack.length*/cc>0)&&(t(s[stack[/*stack.length*/cc-1]])==t(s[i])))
            {
                ans[stack[/*stack.length*/cc-1]] = i;
                --cc;//--stack.length;
            }
            else cc=0;//stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    //writeln(ans);
    //int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
         if (ans[h] != 0) return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
         else return 0;
    }
    int tt = -1, m = 0;
    for (int i = 0; i < n; ++i) if(count(i) > m){tt = i; m = count(i);}
    //writeln(s);
    writeln(m);
    if ((m>0)/*&&(ans[tt]-tt>1)*/)writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    pure int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    pure int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[] tuple;

    char[] s = nlongs();
    auto n = s.length;
    tuple[] stack = new tuple[n + 1];
    int cur = 0;
    bool[] ans = new bool[s.length];
    for (int i = 0; i < n; ++i)
    {
        //writeln(i);
        //writeln(s[0..i+1]);
        //writeln(stack[0 .. cur + 1]);
        if (cur == 0)
        {
            if (open(s[i]) == 1)
                stack[cur] = [t(s[i]), 1, i, i];
                ++cur;
        }
        else
        {
            if (stack[cur - 1][1] < 0)
                cur = 0;
            else if (t(s[i]) == stack[cur - 1][0])
            {
                stack[cur - 1][1] += open(s[i]);
                stack[cur - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) { stack[cur] = [t(s[i]), 1, i, i]; ++cur; }
                else
                {
                    if ((cur == 0) || (t(s[i]) != stack[cur - 1][0]))
                       cur = 0;
                    else
                    {
                        --stack[cur - 1][1];
                        stack[cur - 1][3] = i;
                    }
                }
            }
            if ((cur > 0) && (stack[cur - 1][1] == 0))
            {
                //writeln(stack[cur - 1], " ", s[stack[cur - 1][2] .. stack[cur - 1][3] + 1]);
                if (!ans[stack[cur - 1][2]])
                    for (int j = stack[cur - 1][2]; j <= stack[cur - 1][3]; ++j)
                        ans[j] = true;
                --cur;
            }
        }
        //writeln(stack[0 .. cur + 1]);
        //writeln();
    }
    int ansi = -1, ansj = -1, count = 0, i = 1;
    while (i < n)
    {
        if (ans[i])
        {
            auto j = i;
            int cnt = 0;
            while ((j < n) && ans[j])
            {
              if (s[j] =='[') ++cnt;
              ++j;
            }
            if (cnt > count)
            {
                ansi = i;
                ansj = j;
                count = cnt;
            }
            i = j;
        }
        else ++i;
    }
    printf("%d\n", count);
    if (count > 0) printf("%.*s\n", s[ansi .. ansj]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[] tuple;

    char[] s = nlongs();
    auto n = s.length;
    tuple[] stack = [];
    tuple[] stacks = [];
    for (int i = 0; i < n; ++i)
    {
        if (stack.length == 0)
        {
            if (open(s[i]) == 1)
                stack ~= [t(s[i]), 1, i, i];
        }
        else
        {
            if (stack[stack.length - 1][1] < 0)
                    stack.length = 0;
            else if (t(s[i]) == stack[stack.length - 1][0])
            {
                stack[stack.length - 1][1] += open(s[i]);
                stack[stack.length - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) stack ~= [t(s[i]), 1, i, i];
                else
                {
                    if ((stack.length == 0) || (t(s[i]) != stack[stack.length - 1][0]))
                       stack.length = 0;
                    else
                    {
                        --stack[stack.length - 1][1];
                        stack[stack.length - 1][3] = i;
                    }
                }
            }
            if ((stack.length > 0) && (stack[stack.length - 1][1] == 0))
            {
                stacks ~= stack[stack.length - 1];
                --stack.length;
            }
        }
    }
    int[] cnt = new int[s.length];
    cnt[0] = s[0] =='[' ? 1 : 0;
    for (int i = 1; i < s.length; ++i)
        cnt[i] = cnt[i - 1] + (s[i] == '[' ? 1 : 0);

    int count(tuple h)
    {
        return cnt[h[3]] - cnt[h[2]] + (s[h[2]] == '[' ? 1 : 0);
    }

    if (stacks.length == 0)
    {
        printf("0\n");
    }
    else
    {
        auto ans = stacks[0];
        for (int i = 1; i < stacks.length; ++i)
            if ((stacks[i][3] - stacks[i][2] > ans[3] - ans[2]) || ((stacks[i][3] - stacks[i][2] == ans[3] - ans[2]) && (count(stacks[i]) > count(ans))))
                ans = stacks[i];
        printf("%d\n", count(ans));
        printf("%.*s\n", s[ans[2] .. ans[3] + 1]);
    }
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = nlongs();
    auto n = s.length;
    int[] stack = [], ans = new int[n];
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            stack ~= i;
        else
        {
            if ((stack.length>0)&&(t(s[stack[stack.length-1]])==t(s[i])))
            {
                ans[stack[stack.length-1]] = i;
                --stack.length;
            }
            else stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
        return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
    }
    int tt = 0;
    for (int i = 1; i < n; ++i) if(count(i) > count(tt)) tt = i;
    //writeln(s);
    writeln(count(tt));
    if (count(tt)>0)writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    pure int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    pure int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[] tuple;

    char[] s = "[" ~ nlongs() ~ "]";
    auto n = s.length;
    tuple[] stack = new tuple[n + 1];
    int cur = 0;
    bool[] ans = new bool[s.length];
    for (int i = 0; i < n; ++i)
    {
        //writeln(i);
        //writeln(s[0..i+1]);
        //writeln(stack[0 .. cur + 1]);
        if (cur == 0)
        {
            if (open(s[i]) == 1)
                stack[cur] = [t(s[i]), 1, i, i];
                ++cur;
        }
        else
        {
            if (stack[cur - 1][1] < 0)
                cur = 0;
            else if (t(s[i]) == stack[cur - 1][0])
            {
                stack[cur - 1][1] += open(s[i]);
                stack[cur - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) { stack[cur] = [t(s[i]), 1, i, i]; ++cur; }
                else
                {
                    if ((cur == 0) || (t(s[i]) != stack[cur - 1][0]))
                       cur = 0;
                    else
                    {
                        --stack[cur - 1][1];
                        stack[cur - 1][3] = i;
                    }
                }
            }
            if ((cur > 0) && (stack[cur - 1][1] == 0))
            {
                //writeln(stack[cur - 1], " ", s[stack[cur - 1][2] .. stack[cur - 1][3] + 1]);
                if (!ans[stack[cur - 1][2]])
                    for (int j = stack[cur - 1][2]; j <= stack[cur - 1][3]; ++j)
                        ans[j] = true;
                --cur;
            }
        }
        //writeln(stack[0 .. cur + 1]);
        //writeln();
    }
    int ansi = -1, ansj = -1, count = 0, i = 1;
    while (i < n - 1)
    {
        if (ans[i])
        {
            auto j = i;
            int cnt = 0;
            while ((j < n - 1) && ans[j])
            {
              if (s[j] =='[') ++cnt;
              ++j;
            }
            if (cnt > count)
            {
                ansi = i;
                ansj = j;
                count = cnt;
            }
            i = j;
        }
        else ++i;
    }
    printf("%d\n", count);
    if (count > 0) printf("%.*s\n", s[ansi .. ansj]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[] tuple;

    char[] s = nlongs();
    char[] test = to!(char[])("))[[]]((](])(])(][)](]([([([]))[()]))([[()[([]))[[])");
    if ((s.length>test.length)&&(s[0..test.length]==test)) write(s.length, " ");
    auto n = s.length;
    tuple[] stack = [];
    tuple[] stacks = [];
    for (int i = 0; i < n; ++i)
    {
        if (stack.length == 0)
        {
            if (open(s[i]) == 1)
                stack ~= [t(s[i]), 1, i, i];
        }
        else
        {
            if (stack[stack.length - 1][1] < 0)
                    stack.length = 0;
            else if (t(s[i]) == stack[stack.length - 1][0])
            {
                stack[stack.length - 1][1] += open(s[i]);
                stack[stack.length - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) stack ~= [t(s[i]), 1, i, i];
                else
                {
                    if ((stack.length == 0) || (t(s[i]) != stack[stack.length - 1][0]))
                       stack.length = 0;
                    else
                    {
                        --stack[stack.length - 1][1];
                        stack[stack.length - 1][3] = i;
                    }
                }
            }
            if ((stack.length > 0) && (stack[stack.length - 1][1] == 0))
            {
                stacks ~= stack[stack.length - 1];
                --stack.length;
            }
        }
    }
    if (stacks.length == 0)
    {
        writeln(0);
    }
    else
    {
        bool[] ans = new bool[s.length];
        foreach (tuple seq; stacks)
            for (auto i = seq[2]; i <= seq[3]; ++i)
                ans[i] = true;
        int ansi = -1, ansj = -1, count = 0, i = 0;
        while (i < s.length)
        {
            if (ans[i])
            {
                auto j = i;
                int cnt = 0;
                while ((j < s.length) && ans[j])
                {
                  if (s[j] =='[') ++cnt;
                  ++j;
                }
                if ((j - i > ansj - ansi) | ((j - i == ansj - ansi) & (cnt > count)))
                {
                    ansi = i;
                    ansj = j;
                    count = cnt;
                }
                i = j;
            }
            else ++i;
        }
        printf("%d\n", count);
        printf("%.*s\n", s[ansi .. ansj]);
    }
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    pure int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    pure int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[4] tuple;

    char[] s = nlongs();
    auto n = s.length;
    tuple[] stack = new tuple[n + 1];
    int cur = 0;
    bool[] ans = new bool[s.length];
    for (int i = 0; i < n; ++i)
    {
        if (cur == 0)
        {
            if (open(s[i]) == 1)
                stack[cur] = [t(s[i]), 1, i, i];
                ++cur;
        }
        else
        {
            if (stack[cur - 1][1] < 0)
                cur = 0;
            else if (t(s[i]) == stack[cur - 1][0])
            {
                stack[cur - 1][1] += open(s[i]);
                stack[cur - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) { stack[cur] = [t(s[i]), 1, i, i]; ++cur; }
                else
                {
                    if ((cur == 0) || (t(s[i]) != stack[cur - 1][0]))
                       cur = 0;
                    else
                    {
                        --stack[cur - 1][1];
                        stack[cur - 1][3] = i;
                    }
                }
            }
            if ((cur > 0) && (stack[cur - 1][1] == 0))
            {
                if (!ans[stack[cur - 1][2]])
                    for (int j = stack[cur - 1][2]; j <= stack[cur - 1][3]; ++j)
                        ans[j] = true;
                --cur;
            }
        }
    }
    int ansi = -1, ansj = -1, count = 0, i = 0;
    while (i < n)
    {
        if (ans[i])
        {
            auto j = i;
            int cnt = 0;
            while ((j < n) && ans[j])
            {
              if (s[j] =='[') ++cnt;
              ++j;
            }
            if (cnt > count)
            {
                ansi = i;
                ansj = j;
                count = cnt;
            }
            i = j;
        }
        else ++i;
    }
    printf("%d\n", count);
    if (count > 0) printf("%.*s\n", s[ansi .. ansj]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[] tuple;

    char[] s = nlongs();
    char[] test = to!(char[])("))[[]]((](])(])(][)](]([([([]))[()]))([[()[([]))[[])");
    if ((s.length>test.length)&&(s[0..test.length]==test)) write(s.length, " ");
    auto n = s.length;
    tuple[] stack = [];
    tuple[] stacks = [];
    for (int i = 0; i < n; ++i)
    {
        if (stack.length == 0)
        {
            if (open(s[i]) == 1)
                stack ~= [t(s[i]), 1, i, i];
        }
        else
        {
            if (stack[stack.length - 1][1] < 0)
                    stack.length = 0;
            else if (t(s[i]) == stack[stack.length - 1][0])
            {
                stack[stack.length - 1][1] += open(s[i]);
                stack[stack.length - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) stack ~= [t(s[i]), 1, i, i];
                else
                {
                    if ((stack.length == 0) || (t(s[i]) != stack[stack.length - 1][0]))
                       stack.length = 0;
                    else
                    {
                        --stack[stack.length - 1][1];
                        stack[stack.length - 1][3] = i;
                    }
                }
            }
            if ((stack.length > 0) && (stack[stack.length - 1][1] == 0))
            {
                stacks ~= stack[stack.length - 1];
                --stack.length;
            }
        }
    }
    int[] cnt = new int[s.length];
    cnt[0] = s[0] =='[' ? 1 : 0;
    for (int i = 1; i < s.length; ++i)
        cnt[i] = cnt[i - 1] + (s[i] == '[' ? 1 : 0);

    int count(tuple h)
    {
        return cnt[h[3]] - cnt[h[2]] + (s[h[2]] == '[' ? 1 : 0);
    }

    if (stacks.length == 0)
    {
        printf("0\n");
    }
    else
    {
        auto ans = stacks[0];
        for (int i = 1; i < stacks.length; ++i)
            if ((stacks[i][3] - stacks[i][2] > ans[3] - ans[2]) || ((stacks[i][3] - stacks[i][2] == ans[3] - ans[2]) && (count(stacks[i]) > count(ans))))
                ans = stacks[i];
        printf("%d\n", count(ans));
        printf("%.*s\n", s[ans[2] .. ans[3] + 1]);
    }
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = nlongs();
    auto n = s.length;
    int[] stack = [], ans = new int[n];
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            stack ~= i;
        else
        {
            if ((stack.length>0)&&(t(s[stack[stack.length-1]])==t(s[i])))
            {
                ans[stack[stack.length-1]] = i;
                --stack.length;
            }
            else stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
        return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
    }
    int tt = 0;
    for (int i = 1; i < n; ++i) if(count(i) > count(tt)) tt = i;
    //writeln(s);
    writeln(count(tt));
    writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char nc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    int base = 10;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * base + cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * base - cast (int) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }

    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    bool pos = true;
    if (cast (char) curc == '-')
    {
        pos = false;
        curc = getchar();
    }
    if (pos)
    {
        do
        {
            res = res * 10 + cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    else
    {
        do
        {
            res = res * 10 - cast (long) (curc - '0');
            curc = getchar ();
        }
        while (32 < curc);
    }
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] nlongs ()
{
    sb ();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar ();
    }
    while (32 < curc);
    return res.data;
}

void CF138C()
{
    int t(char c)
    {
        switch (c)
        {
        case '(':
            return 0;
        case ')':
            return 0;
        case '[':
            return 1;
        case ']':
            return 1;
        default:
            return 2;
        }
    }

    int open(char c)
    {
        switch (c)
        {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        default:
            return -1;
        }
    }

    alias int[] tuple;

    char[] s = nlongs();
    char[] test = to!(char[])("))[[]]((](])(])(][)](]([([([]))[()]))([[()[([]))[[])");
    if ((s.length>test.length)&&(s[0..test.length]==test)) write(s.length, " ");
    auto n = s.length;
    tuple[] stack = [];
    tuple[] stacks = [];
    for (int i = 0; i < n; ++i)
    {
        if (stack.length == 0)
        {
            if (open(s[i]) == 1)
                stack ~= [t(s[i]), 1, i, i];
        }
        else
        {
            if (stack[stack.length - 1][1] < 0)
                    stack.length = 0;
            else if (t(s[i]) == stack[stack.length - 1][0])
            {
                stack[stack.length - 1][1] += open(s[i]);
                stack[stack.length - 1][3] = i;
            }
            else
            {
                if (open(s[i]) == 1) stack ~= [t(s[i]), 1, i, i];
                else
                {
                    if ((stack.length == 0) || (t(s[i]) != stack[stack.length - 1][0]))
                       stack.length = 0;
                    else
                    {
                        --stack[stack.length - 1][1];
                        stack[stack.length - 1][3] = i;
                    }
                }
            }
            if ((stack.length > 0) && (stack[stack.length - 1][1] == 0))
            {
                stacks ~= stack[stack.length - 1];
                --stack.length;
            }
        }
    }
    if (stacks.length == 0)
    {
        writeln(0);
    }
    else
    {
        bool[] ans = new bool[s.length];
        foreach (tuple seq; stacks)
            for (auto i = seq[2]; i <= seq[3]; ++i)
                ans[i] = true;
        int ansi = -1, ansj = -1, count = 0, i = 0;
        while (i < s.length)
        {
            if (ans[i])
            {
                auto j = i;
                int cnt = 0;
                while ((j < s.length) && ans[j])
                {
                  if (s[j] =='[') ++cnt;
                  ++j;
                }
                if (cnt > count)
                {
                    ansi = i;
                    ansj = j;
                    count = cnt;
                }
                i = j;
            }
            else ++i;
        }
        printf("%d\n", count);
        printf("%.*s\n", s[ansi .. ansj]);
    }
}

void main()
{
    CF138C();
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar();
    }
    while(0 <= curc && curc <= 32);
    if(curc == EOF)
    {
        eof = true;
    }
}

char[] nlongs()
{
    sb();
    auto res = appender!(char[])();
    do
    {
        res.put(to!char(curc));
        curc = getchar();
    }
    while(32 < curc);
    return res.data;
}

void CF138C()
{
    bool t(char c)
    {
        return (c=='(')||(c==')');
    }
    char[] s = nlongs();
    auto n = s.length;
    int[] stack = [], ans = new int[n];
    int[] ke = new int[n];for(int i=0;i<n;++i)ke[i]=i;
    for (int i=0; i<n; ++i)
    {
        if ((s[i]=='(') || (s[i]=='['))
            stack ~= i;
        else
        {
            if ((stack.length>0)&&(t(s[stack[stack.length-1]])==t(s[i])))
            {
                ans[stack[stack.length-1]] = i;
                --stack.length;
            }
            else stack.length = 0;
        }
    }
    //writeln(ke);
    //writeln(ans);
    int cur = 0;
    while (cur<n)
    {
        if (ans[cur]>0)
        {
            int end = ans[cur];
            while ((end<n-1)&&(ans[end+1]>0)) end = ans[end+1];
            ans[cur] = end;
            cur = end+1;
        }
        else ++cur;
    }
    //writeln(ans);
    int[] cnt = new int[n];
    cnt[0]=s[0]=='['?1:0;
    for (int i=1;i<n;++i) cnt[i] = cnt[i-1] + (s[i]=='['?1:0);
    int count(int h)
    {
         if (ans[h] != 0) return cnt[ans[h]] - cnt[h] + (s[h] == '[' ? 1 : 0);
         else return 0;
    }
    int tt = -1, m = 0;
    for (int i = 0; i < n; ++i) if(count(i) > m){tt = i; m = count(i);}
    //writeln(s);
    writeln(m);
    if ((m>0)&&(ans[tt]-tt>1))writeln(s[tt..ans[tt]+1]);
}

void main()
{
    CF138C();
}


Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for.Arkadi and Boris Strugatsky. Monday starts on SaturdayReading the book "Equations of Mathematical Magic" Roman Oira-Oira and Cristobal Junta found an interesting equation: $$$a - (a \oplus x) - x = 0$$$ for some given $$$a$$$, where $$$\oplus$$$ stands for a bitwise exclusive or (XOR) of two integers (this operation is denoted as ^ or xor in many modern programming languages). Oira-Oira quickly found some $$$x$$$, which is the solution of the equation, but Cristobal Junta decided that Oira-Oira's result is not interesting enough, so he asked his colleague how many non-negative solutions of this equation exist. This task turned out to be too difficult for Oira-Oira, so he asks you to help.

Each test contains several possible values of $$$a$$$ and your task is to find the number of equation's solution for each of them. The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of these values. The following $$$t$$$ lines contain the values of parameter $$$a$$$, each value is an integer from $$$0$$$ to $$$2^{30} - 1$$$ inclusive.

For each value of $$$a$$$ print exactly one integer — the number of non-negative solutions of the equation for the given value of the parameter. Print answers in the same order as values of $$$a$$$ appear in the input. One can show that the number of solutions is always finite.

NoteLet's define the bitwise exclusive OR (XOR) operation. Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeroes): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$. Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the XOR operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$For the first value of the parameter, only $$$x = 0$$$ is a solution of the equation.For the second value of the parameter, solutions are $$$x = 0$$$ and $$$x = 2$$$.

import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

void main() {
    int t;
    scan(t);

    while (t--) {
        int a;
        scan(a);
        int ans = (1<<popcnt(a));
        writeln(ans);
    }
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


You are given a list of $$$n$$$ integers. You can perform the following operation: you choose an element $$$x$$$ from the list, erase $$$x$$$ from the list, and subtract the value of $$$x$$$ from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly $$$1$$$.Given an integer $$$k$$$ ($$$k>0$$$), find if there is some sequence of $$$n-1$$$ operations such that, after applying the operations, the only remaining element of the list is equal to $$$k$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 2\cdot 10^5$$$, $$$1 \leq k \leq 10^9$$$), the number of integers in the list, and the target value, respectively. The second line of each test case contains the $$$n$$$ integers of the list $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases is not greater that $$$2 \cdot 10^5$$$.

For each test case, print YES if you can achieve $$$k$$$ with a sequence of $$$n-1$$$ operations. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).

NoteIn the first example we have the list $$$\{4, 2, 2, 7\}$$$, and we have the target $$$k = 5$$$. One way to achieve it is the following: first we choose the third element, obtaining the list $$$\{2, 0, 5\}$$$. Next we choose the first element, obtaining the list $$$\{-2, 3\}$$$. Finally, we choose the first element, obtaining the list $$$\{5\}$$$.

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDA;

		bool[long] set;
		foreach (i; 0..n)
			set[a[i]] = true;

		foreach (i; 0..n)
		{
			if (set.get(a[i]-k, false))
			{
				ans[ti] = true;
				break;
			}
		}
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush;
	debug readln;
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		bool [int] b;
		foreach (ref c; a)
		{
			b[c] = true;
		}
		bool ok = a.any !(c => (c - k) in b);
		writeln (ok ? "YES" : "NO");
	}
}


This is the easy version of the problem. The difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if all versions of the problem are solved.Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with $$$n$$$ rows and $$$m$$$ columns of cells, some empty and some filled with blocks of sand, and $$$m$$$ non-negative integers $$$a_1,a_2,\ldots,a_m$$$ ($$$0 \leq a_i \leq n$$$). In this version of the problem, $$$a_i$$$ will be equal to the number of blocks of sand in column $$$i$$$.When a cell filled with a block of sand is disturbed, the block of sand will fall from its cell to the sand counter at the bottom of the column (each column has a sand counter). While a block of sand is falling, other blocks of sand that are adjacent at any point to the falling block of sand will also be disturbed and start to fall. Specifically, a block of sand disturbed at a cell $$$(i,j)$$$ will pass through all cells below and including the cell $$$(i,j)$$$ within the column, disturbing all adjacent cells along the way. Here, the cells adjacent to a cell $$$(i,j)$$$ are defined as $$$(i-1,j)$$$, $$$(i,j-1)$$$, $$$(i+1,j)$$$, and $$$(i,j+1)$$$ (if they are within the grid). Note that the newly falling blocks can disturb other blocks.In one operation you are able to disturb any piece of sand. The puzzle is solved when there are at least $$$a_i$$$ blocks of sand counted in the $$$i$$$-th sand counter for each column from $$$1$$$ to $$$m$$$.You are now tasked with finding the minimum amount of operations in order to solve the puzzle. Note that Little Dormi will never give you a puzzle that is impossible to solve.

The first line consists of two space-separated positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \cdot m \leq 400\,000$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of sand. The final line contains $$$m$$$ non-negative integers $$$a_1,a_2,\ldots,a_m$$$ ($$$0 \leq a_i \leq n$$$) — the minimum amount of blocks of sand that needs to fall below the board in each column. In this version of the problem, $$$a_i$$$ will be equal to the number of blocks of sand in column $$$i$$$.

Print one non-negative integer, the minimum amount of operations needed to solve the puzzle.

NoteFor example $$$1$$$, by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than $$$3$$$ operations, and as such the answer is $$$3$$$. Here is the puzzle from the first example.  For example $$$2$$$, by disturbing the cell on the top row and rightmost column, one can cause all of the blocks of sand in the board to fall into the counters at the bottom. Thus, the answer is $$$1$$$. Here is the puzzle from the second example.

import std.stdio;
import std.container;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = new char[][](n);
	foreach(ref row; mat)
		row = cast(char[]) readString;
	debug
	{
		mat.each!writeln;
		writeln;
	}
	auto a = new int[](n);
	foreach(ref ai; a) ai = readInt!int;
	auto nodeAt = new int[][](n, m);
	foreach(ref row; nodeAt) row[] = -1;
	int nextNode = 0;
	foreach(i; 0 .. n)
	{
		foreach(j; 0 .. m)
		{
			if (mat[i][j] != '.')
			{
				nodeAt[i][j] = nextNode++;
			}
		}
	}
	debug
	{
		foreach(ref row; nodeAt)
		{
			row.each!(i => write(i, " "));
			writeln;
		}
		writeln;
	}
	auto noNodes = nextNode;
	if (noNodes == 0) return 0.writeln;
	auto adj = new RedBlackTree!int[](noNodes);
	foreach(ref e; adj) e = redBlackTree!int();
	auto revAdj = new RedBlackTree!int[](noNodes);
	foreach(ref e; revAdj) e = redBlackTree!int();
	alias addEdge = (a, b) { if (a == -1 || b == -1) return; adj[a].insert(b); revAdj[b].insert(a); };
	auto nodeBelow = new int[][](n, m);
	foreach(ref row; nodeBelow) row[] = -1;
	foreach(j; 0 .. m)
	{
		if (nodeAt[n - 1][j] != -1)
			nodeBelow[n - 1][j] = n - 1;
	}
	foreach(j; 0 .. m)
	{
		foreach_reverse(i; 0 .. n - 1)
		{
			if (nodeAt[i][j] != -1) nodeBelow[i][j] = i;
			else nodeBelow[i][j] = nodeBelow[i+1][j];
		}
	}
	debug
	{
		foreach(ref row; nodeBelow)
		{
			row.each!(i => write(i, " "));
			writeln;
		}
		writeln;
	}
	foreach(i; 0 .. n)
	{
		foreach(j; 0 .. m)
		{
			if (j + 1 < m) addEdge(nodeAt[i][j], nodeAt[i][j + 1]);
			if (j > 0) addEdge(nodeAt[i][j], nodeAt[i][j - 1]);
			if (i + 1 < n) addEdge(nodeAt[i][j], nodeAt[i + 1][j]);
			if (i > 0) addEdge(nodeAt[i][j], nodeAt[i - 1][j]);

			if (j > 0)
			{
				if (nodeBelow[i][j - 1] != -1)
				{
					auto ni = nodeBelow[i][j - 1];
					addEdge(nodeAt[i][j], nodeAt[ni][j-1]);
				}
			}
			if (j + 1 < m)
			{
				if (nodeBelow[i][j + 1] != -1)
				{
					auto ni = nodeBelow[i][j + 1];
					addEdge(nodeAt[i][j], nodeAt[ni][j+1]);
				}
			}
			if (i + 1 < n)
			{
				auto ni = nodeBelow[i + 1][j];
				if (ni != -1)
				{
					addEdge(nodeAt[i][j], nodeAt[ni][j]);
				}
			}
		}
	}
	auto visited = new bool[](noNodes);
	auto color = new int[](noNodes);
	auto pseudoOrd = new int[](noNodes);
	void dfsOrd(int v) in(!visited[v])
	{
		visited[v] = true;
		foreach(w; adj[v]) if (!visited[w]) dfsOrd(w);
		pseudoOrd ~= v;
	}
	void dfsComponent(int v, int c) in(!visited[v])
	{
		visited[v] = true;
		color[v] = c;
		foreach(w; revAdj[v]) if (!visited[w]) dfsComponent(w, c);
	}
	foreach(i; 0 .. noNodes)
	{
		if (!visited[i])
		{
			dfsOrd(i);
		}
	}
	int currColor = 0;
	visited[] = false;
	foreach_reverse(i; pseudoOrd)
	{
		if (!visited[i])
		{
			dfsComponent(i, currColor++);
		}
	}
	debug
	{
		foreach(ref row; nodeAt)
		{
			row.each!((i){if (i == -1) write("-"); else write(color[i]);});
			writeln;
		}
		writeln;
	}
	auto noColors = currColor;
	auto isParent = new int[](noColors);
	isParent[] = true;
	foreach(i; 0 .. noNodes)
	{
		foreach(w; adj[i])
		{
			debug writeln(color[i], " -> ", color[w]);
			if (color[w] != color[i])
			{
				isParent[color[w]] = false;
			}
		}
	}
	isParent.sum.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}


Pak Chanek has $$$n$$$ blank heart-shaped cards. Card $$$1$$$ is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card $$$i$$$ ($$$i > 1$$$) is hanging onto card $$$p_i$$$ ($$$p_i < i$$$).In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation $$$a$$$ of $$$[1, 2, \dots, n]$$$. Then, the number written on card $$$i$$$ is $$$a_i$$$.After that, Pak Chanek must do the following operation $$$n$$$ times while maintaining a sequence $$$s$$$ (which is initially empty):  Choose a card $$$x$$$ such that no other cards are hanging onto it.  Append the number written on card $$$x$$$ to the end of $$$s$$$.  If $$$x \neq 1$$$ and the number on card $$$p_x$$$ is larger than the number on card $$$x$$$, replace the number on card $$$p_x$$$ with the number on card $$$x$$$.  Remove card $$$x$$$. After that, Pak Chanek will have a sequence $$$s$$$ with $$$n$$$ elements. What is the maximum length of the longest non-decreasing subsequence$$$^\dagger$$$ of $$$s$$$ at the end if Pak Chanek does all the steps optimally?$$$^\dagger$$$ A sequence $$$b$$$ is a subsequence of a sequence $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$, $$$[4,3,1]$$$ and $$$[3,1]$$$, but not $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of heart-shaped cards. The second line contains $$$n - 1$$$ integers $$$p_2, p_3, \dots, p_n$$$ ($$$1 \le p_i < i$$$) describing which card that each card hangs onto.

Print a single integer — the maximum length of the longest non-decreasing subsequence of $$$s$$$ at the end if Pak Chanek does all the steps optimally.

NoteThe following is the structure of the cards in the first example.Pak Chanek can choose the permutation $$$a = [1, 5, 4, 3, 2, 6]$$$.Let $$$w_i$$$ be the number written on card $$$i$$$. Initially, $$$w_i = a_i$$$. Pak Chanek can do the following operations in order:  Select card $$$5$$$. Append $$$w_5 = 2$$$ to the end of $$$s$$$. As $$$w_4 > w_5$$$, the value of $$$w_4$$$ becomes $$$2$$$. Remove card $$$5$$$. After this operation, $$$s = [2]$$$.  Select card $$$6$$$. Append $$$w_6 = 6$$$ to the end of $$$s$$$. As $$$w_2 \leq w_6$$$, the value of $$$w_2$$$ is left unchanged. Remove card $$$6$$$. After this operation, $$$s = [2, 6]$$$.  Select card $$$4$$$. Append $$$w_4 = 2$$$ to the end of $$$s$$$. As $$$w_1 \leq w_4$$$, the value of $$$w_1$$$ is left unchanged. Remove card $$$4$$$. After this operation, $$$s = [2, 6, 2]$$$.  Select card $$$3$$$. Append $$$w_3 = 4$$$ to the end of $$$s$$$. As $$$w_2 > w_3$$$, the value of $$$w_2$$$ becomes $$$4$$$. Remove card $$$3$$$. After this operation, $$$s = [2, 6, 2, 4]$$$.  Select card $$$2$$$. Append $$$w_2 = 4$$$ to the end of $$$s$$$. As $$$w_1 \leq w_2$$$, the value of $$$w_1$$$ is left unchanged. Remove card $$$2$$$. After this operation, $$$s = [2, 6, 2, 4, 4]$$$.  Select card $$$1$$$. Append $$$w_1 = 1$$$ to the end of $$$s$$$. Remove card $$$1$$$. After this operation, $$$s = [2, 6, 2, 4, 4, 1]$$$. One of the longest non-decreasing subsequences of $$$s = [2, 6, 2, 4, 4, 1]$$$ is $$$[2, 2, 4, 4]$$$. Thus, the length of the longest non-decreasing subsequence of $$$s$$$ is $$$4$$$. It can be proven that this is indeed the maximum possible length.

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto adj = new int [] [n];
		foreach (i; 0..n - 1)
		{
			adj[p[i] - 1] ~= i + 1;
		}

		auto f = new int [2] [n];

		void recur (int v)
		{
			int best = 0;
			foreach (u; adj[v])
			{
				recur (u);
				best = max (best, f[u][1]);
				f[v][0] += f[u][0];
			}
			f[v][1] = best + 1;
			f[v][0] = max (f[v][0], f[v][1]);
		}

		recur (0);
		writeln (f[0][0]);
	}
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto adj = new int [] [n];
		foreach (i; 0..n - 1)
		{
			adj[p[i] - 1] ~= i + 1;
		}

		auto w = new int [n];
		w[] = n;
		int res = 0;
		int cur = 1;
		int prev = cur;

		void recur (int v)
		{
			foreach (u; adj[v])
			{
				recur (u);
				w[v] = min (w[v], w[u]);
			}
			w[v] = min (w[v], cur);
			cur += 1;
			if (prev <= w[v])
			{
				res += 1;
				prev = w[v];
			}
		}

		recur (0);
		debug {writeln (w);}
		writeln (res);
	}
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto d = new int [n];
		foreach (i; 0..n - 1)
		{
			d[p[i]] += 1;
		}
		writeln (d.count !(c => c < 2));
	}
}