#include
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
node * buildtree()
{
int a,b;
cin>>a>>b;
node *root=new node(a);
if(b==0)
return root;
if(root==NULL)
return NULL;
root->left=buildtree();
root->right=buildtree();
return root;
}
int sum_at_k_level(node *root,int k)
{
static int sum=0;
if(root==NULL)
return 0;
if(k==0)
{
sum=sum+root->data;
return sum;
}
sum_at_k_level(root->left,k-1);
sum_at_k_level(root->right,k-1);
return sum;
}
int main()
{
node *root=buildtree();
int k;
cin>>k;
cout<<sum_at_k_level(root,2);
}