Description
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression insering symbols '+' and '1': "((1+1)+(1+1))". Also the following sequences are regular: "()()()", "(())" and "()". The following sequences are not regular bracket sequences: ")(", "(()" and "())(()".
In this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·10^5, 0 ≤ k ≤ 10^18) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples input
3 1
Examples output
()(())
meaning of the title
Given n n n and k, representing the sum of the number of parentheses and nesting, a nested sequence is output to satisfy this situation.
thinking
We consider a case of nested maximum sum (((((())), where n=5,k=1+2+3+4=10.
Obviously, for any number of k * (1 + 2 + 3, 1 + 2 + 3 + 4), we can place the brackets originally in the fourth layer in the appropriate layer. Of course, k is the same way in other areas.
At this point, the sequence of this part is similar to that of ((()())()). If the brackets are not used up, we can add a separate () at the end.
For k > n * (n_1)/2, Impossible should be output, because in the most extreme case, it can not find the solution, so it must not be established.
AC code
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
typedef __int64 LL;
LL n,k;
int main()
{
cin>>n>>k;
if(k>n*(n-1)/2)
puts("Impossible");
else
{
string ans;
int base = 0;
LL num = 0;
while(num+base<=k)
{
num+=base++;
ans+="(";
}
LL cnt = k-num;
num = 0;
while(base--)
{
ans+=")";
if(base && base == cnt)ans+="()",num++;
num++;
}
for(int i=0; i<n-num; i++)
ans+="()";
cout<<ans<<endl;
}
return 0;
}