Import Java.util.Scanner;
public class Main {static Scanner scan = new Scanner (system.in);
static int best_sum = 0;
static int best = 0;
public static void Main (string[] args) {while (Scan.hasnext ()) {String mid = Scan.nextline ();
String post = Scan.nextline ();
string[] AA = Mid.split ("");
string[] bb = Post.split ("");
int[] Midorder = new Int[aa.length];
int[] Postorder = new Int[aa.length];
for (int i=0;i<aa.length;i++) {Midorder[i] = Integer.parseint (aa[i));
Postorder[i] = Integer.parseint (Bb[i]);
Node root = Build (midorder,postorder,0,midorder.length-1,0,postorder.length-1);
Best_sum = Integer.max_value;
best = Integer.max_value;
Pre (root,0);
System.out.println (best);
}///First order recursive optimal solution public static void Pre (Node root,int sum) {if (root!=null) {sum+=root.value; if (root.left==null&&root.right==null) {if (sum<best_sum| | (sum==best_sum&&root.value<best))
{best_sum = sum; Best =Root.value;
} if (Root.left!=null) Pre (root.left,sum);
if (root.right!=null) Pre (root.right,sum); }///based on the sequence and subsequent achievements public static Node build (int[] m,int[] p,int m1,int m2,int p1,int p2) {if m1>m2| |
P1>P2) return null;
Node root = new node ();
int v = P[P2];
Root.value = v;
int rootindex = 0;
while (M[ROOTINDEX]!=V) rootindex++;
int cnt = ROOTINDEX-M1;
Root.left = Build (m,p,m1,rootindex-1,p1,p1+cnt-1);
Root.right = Build (m,p,rootindex+1,m2,p1+cnt,p2-1);
return root;
Static class node{int value;
Node Left,right; }
}