Fq.java
import java.util.*;
import java.io.*;
import java.math.*;
class Fq
{
public static char[] input;
while(br.ready())
{
//System.out.println("////////////////////////");
input = line.toCharArray();
int coins = input.length / 2;
f(0, 0, coins, coins);
if (v.length() > 6)
{
out = v.substring(v.length() - 6);
}
}
}
public static void f(int buff, int pos, int n5, int n10)
{
//System.out.println(buff + " " + pos + " " + n5 + " " + n10);
if (buff < 0 || n5 < 0 || n10 < 0)
{
//System.out.println("fucked");
return;
}
if (n5 == 0 && n10 == 0)
{
//System.out.println("valid");
return;
}
//System.out.println("pos" + pos + " " + input[pos]);
if (input[pos] == ')'){ // 10
f(buff-1, pos+1, n5, n10 - 1);
return;
}
if (input[pos] == '('){ // 5
f(buff+1, pos+1, n5 - 1, n10);
return;
}
f(buff-1, pos+1, n5, n10 - 1); // 10
f(buff+1, pos+1, n5 - 1, n10); // 5
}
}