#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define INF 1000000001
int U,V;
int dist_toU;
struct node{
    vector<int> to;
    int dist;
    bool path_toU;
    node(){
        dist=0;
        path_toU=false;
    }
};
void dfs1(int v, int p, vector<node> &nodes){
    for (int i = 0; i < nodes[v].to.size(); i++){
        int v_to=nodes[v].to[i];
        if(v_to==p)continue;
        nodes[v_to].dist=nodes[v].dist+1;
        if(v_to==U){
            nodes[v_to].path_toU=true;
            dist_toU=nodes[v_to].dist;
        }
        dfs1(v_to, v, nodes);
        if(nodes[v_to].path_toU)nodes[v].path_toU=true;
    }
    for (int i = 0; i < nodes[v].to.size(); i++){
        int v_to=nodes[v].to[i];
        if(v_to==p)continue;
        nodes[v].dist=max(nodes[v].dist,nodes[v_to].dist);
    }
}

int main() {
    int N;
    cin>>N;
    cin>>U>>V;U--;V--;
    vector<node> nodes(N);
    for (int i = 0; i < N-1; i++){
        int A,B;
        cin>>A>>B;A--;B--;
        nodes[A].to.push_back(B);
        nodes[B].to.push_back(A);
    }
    dist_toU=0;
    dfs1(V,-1,nodes);
    int move=dist_toU/2+1;
    int ans=0;
    int v=V,p=-1;
    while(move>0){
        for(int v_to:nodes[v].to){
            if(v_to==p)continue;
            if(nodes[v_to].path_toU){
                p=v;
                v=v_to;
                ans=nodes[v].dist-1;
                break;
            }
        }
        move--;
    }
    cout<<ans<<endl;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
const int maxn=1e6+10;
const int mod=1e9+7;
const double pi=3.1415926535;
int w[3005],v[3005];
int dp[3005][2];




int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,t;
    cin>>n>>t;
    for(int i=1;i<=n;i++)
    {
        cin>>w[i]>>v[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=t-1;j>=0;j--)
        {
            for(int x=1;x>=0;x--)
            {
                if(x==0)
                {
                    if(j>=w[i])
                    {
                        dp[j][x]=max(dp[j][x],dp[j-w[i]][x]+v[i]);
                    }
                }
                else
                {
                    dp[j][x]=max(dp[j][x],dp[j][0]+v[i]);
                    if(j>=w[i])
                    {
                        dp[j][x]=max(dp[j][x],dp[j-w[i]][x]+v[i]);
                    }
                }
            }
        }
    }
    cout<<dp[t-1][1]<<'\n';






}
