#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define N 10005
using namespace std;
int a[N],b[N];
int m,value;
void build(int a[],int b[],int n,int sum)
{
if(n<=0) return ;
else if(n==1)
{
sum+=a[0];
if(m>sum)
m=sum,value=a[0];
return ;
}
int p=find(a,a+n,b[n-1])-a;
sum+=b[n-1];
build(a,b,p,sum);
build(a+p+1,b+p,n-p-1,sum);
}
int main()
{
// freopen("ex.in","r",stdin);
char c;
while(scanf("%d",&a[0])!=EOF)
{
int i=1;
m=0x7f7f7f7f;
while(scanf("%c",&c)&&c!='\n')
{
scanf("%d",&a[i++]);
}
int n=i;
for(i=0;i<n;i++)
scanf("%d",&b[i]);
int sum=0;
build(a,b,n,sum);
printf("%d\n",value);
}
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define N 10005
using namespace std;
int a[N],b[N];
int m,value;
void build(int a[],int b[],int n,int sum)
{
if(n<=0) return ;
else if(n==1)
{
sum+=a[0];
if(m>sum)
m=sum,value=a[0];
return ;
}
int p=find(a,a+n,b[n-1])-a;
sum+=b[n-1];
build(a,b,p,sum);
build(a+p+1,b+p,n-p-1,sum);
}
int main()
{
// freopen("ex.in","r",stdin);
char c;
while(scanf("%d",&a[0])!=EOF)
{
int i=1;
m=0x7f7f7f7f;
while(scanf("%c",&c)&&c!='\n')
{
scanf("%d",&a[i++]);
}
int n=i;
for(i=0;i<n;i++)
scanf("%d",&b[i]);
int sum=0;
build(a,b,n,sum);
printf("%d\n",value);
}
return 0;
}