시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB72302342.593%

문제

It will soon be time to decorate the Christmas tree. The NWERC judges are already debating the optimal way to put decorations in a tree. They agree that it is essential to distribute the decorations evenly over the branches of the tree.

This problem is limited to binary Christmas trees. Such trees consist of a trunk, which splits into two subtrees. Each subtree may itself split further into two smaller subtrees and so on. A subtree that does not split any further is a twig. A twig may be decorated by attaching at most one ball to it.

Figure 1 – Example of a tree with subtrees, twigs and one ball.

A decorated tree has an even distribution of balls if and only if the following requirement is satisfied:

At every point where a (sub)tree splits into two smaller subtrees t1 and t2, the total number of balls in the left subtree N(t1) and the total number of balls in the right subtree N(t2) must either be equal or differ by one. That is: | N(t1) − N(t2) | ≤ 1.

In their enthusiasm, the judges initially attach balls to arbitrary twigs in the tree. When they can not find any more balls to put in the tree, they stand back and consider the result. In most cases, the distribution of the balls is not quite even. They decide to fix this by moving some of the balls to different twigs.

Given the structure of the tree and the initial locations of the balls, calculate the minimum number of balls that must be moved to achieve an even distribution as defined above.

Note that it is not allowed to add new balls to the tree or to permanently remove balls from the tree. The only way in which the tree may be changed is by moving balls to different twigs.

입력

For each test case, the input consists of one line describing a decorated tree.

The description of a tree consists of a recursive description of its subtrees. A (sub)tree is represented by a string in one of the following forms:

  • The string ‘()’ represents a twig without a ball.
  • The string ‘(B)’ represents a twig with a ball attached to it.
  • The string ‘(t1 t2)’ represents a (sub)tree that splits into the two smaller subtrees represented by t1 and t2, where t1 and t2 are strings in one of the forms listed here.

A tree contains at least 2 and at most 1000 twigs.

출력

For each test case, print one line of output.

If it is possible to distribute the balls evenly through the tree, print the minimum number of balls that must be moved to satisfy the requirement of even distribution.

If it is not possible to distribute the balls evenly, print the word ‘impossible’.

예제 입력 1

((B)())
((((B)(B))((B)()))(B))
(()(((B)(B))(B)))

예제 출력 1

0
impossible
1

힌트

Figure 2 – Trees corresponding to the example input cases.
{"cc_re__cc_re__cc_by_sa_3_0": ["section id=\"hint\"  class=\"problem-section\">\n\t\t\t\t<div class=\"headline\">\n\t\t\t\t<h2>\ud78c\ud2b8</h2>\n\t\t\t\t</div>\n\t\t\t\t<div id=\"problem_hint\" class=\"problem-text\">\n\t\t\t\t<p style=\"text-align: center;\"><img alt=\"\" src=\"/upload/images2/jingle2.png\" style=\"height:236px; width:600px\" /></p>\r\n\r\n<p>Figure 2 &ndash; Trees corresponding to the example input cases.</p>\r\n\n\t\t\t\t</div>\n\t\t\t\t</section>\n\t\t\t</div>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t\t<div class=\"col-md-12\"><section id=\"source\"><div class=\"headline\"><h2>\ucd9c\ucc98</h2></div><p><a href=\"https://creativecommons.org/licenses/by-sa/3.0/\"><img src=\"https://licensebuttons.net/l/by-sa/3.0/88x31.png\"></a></p>\n<p><a href=\"/category/1\">ICPC</a>&nbsp;&gt;&nbsp;<a href=\"/category/7\">Regionals</a>&nbsp;&gt;&nbsp;<a href=\"/category/10\">Europe</a>&nbsp;&gt;&nbsp;<a href=\"/category/15\">Northwestern European Regional Contest</a>&nbsp;&gt;&nbsp;<a href=\"/category/detail/1188\">NWERC 2013</a>&nbsp;J\ubc88</p></section></div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</div>\n    <div class=\"margin-bottom-20\"></div>\n\t\t\t\t<div style=\"width: 100%;\" class=\"no-print\"><s"], "copyright__copyright__copyright": ["ree contains at least 2 and at most 1000 twigs.\n\n\ucd9c\ub825\n\nFor each test case, print one line of output.\n\nIf it is possible to distribute the balls evenly through the tree, print the minimum number of balls that must be moved to satisfy the requirement of even distribution.\n\nIf it is not possible to distribute the balls evenly, print the word \u2018impossible\u2019.\n\n\uc608\uc81c \uc785\ub825 1\n\n((B)())\n((((B)(B))((B)()))(B))\n(()(((B)(B))(B)))\n\n\uc608\uc81c \ucd9c\ub825 1\n\n0\nimpossible\n1\n\n\ud78c\ud2b8\n\nFigure 2 \u2013 Trees corresponding to the example input cases."]}