/*input
4 2
BWWW
WBBW
WBBW
WWWB

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	int ar[n][n];
	REP(i,n){
		REP(j,n) ar[i][j]=0;
	}
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) ar[max(i-k+1,0)][max(0,mx-k+1)]++;
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn) ar[max(0,mx-k+1)][max(i-k+1,0)]++;
		}
	}
	if (k > n){
		cout<<2*n;
		return 0;
	}
	int stripSum[n][n];
	for (int j=0; j<n; j++)
	{
		int sum = 0;
		for (int i=0; i<k; i++)
			sum += ar[i][j];
		stripSum[0][j] = sum;
		for (int i=1; i<n-k+1; i++)
		{
			sum += (ar[i+k-1][j] - ar[i-1][j]);
			stripSum[i][j] = sum;
		}
	}
	int max_sum = INT_MIN, *pos = NULL;
	for (int i=0; i<n-k+1; i++)
	{
		int sum = 0;
		for (int j = 0; j<k; j++)
			sum += stripSum[i][j];

		if (sum > max_sum)
		{
			max_sum = sum;
			pos = &(ar[i][0]);
		}
		for (int j=1; j<n-k+1; j++)
		{
			sum += (stripSum[i][j+k-1] - stripSum[i][j-1]);
			if (sum > max_sum)
			{
				max_sum = sum;
				pos = &(ar[i][j]);
			}
		}
	}
	ll sum=0;
	for (int i=0; i<k; i++)
	{
		for (int j=0; j<k; j++)
			sum+= *(pos + i*n + j);

	}
	cout<<ans+sum;
}


/*input
4 3
BBBB
BBBB
BBBB
BBBW

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	int ar[n*2][n*2];
	REP(i,2*n){
		REP(j,2*n) ar[i][j]=0;
	}
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) ar[max(i+n,0)][max(0,mx-k+n+1)]++;
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn) ar[max(0,mx-k+1+n)][max(i+n,0)]++;
		}
	}
	n*=2;
	int stripSum[n][n];
	for (int j=0; j<n; j++)
	{
		int sum = 0;
		for (int i=0; i<k; i++)
			sum += ar[i][j];
		stripSum[0][j] = sum;
		for (int i=1; i<n-k+1; i++)
		{
			sum += (ar[i+k-1][j] - ar[i-1][j]);
			stripSum[i][j] = sum;
		}
	}
	int max_sum = INT_MIN, *pos = NULL;
	for (int i=0; i<n-k+1; i++)
	{
		int sum = 0;
		for (int j = 0; j<k; j++)
			sum += stripSum[i][j];

		if (sum > max_sum)
		{
			max_sum = sum;
			pos = &(ar[i][0]);
		}
		for (int j=1; j<n-k+1; j++)
		{
			sum += (stripSum[i][j+k-1] - stripSum[i][j-1]);
			if (sum > max_sum)
			{
				max_sum = sum;
				pos = &(ar[i][j]);
			}
		}
	}
	ll sum=0;
	for (int i=0; i<k; i++)
	{
		for (int j=0; j<k; j++)
			sum+= *(pos + i*n + j);

	}
	cout<<ans+sum;
}


/*input
4 2
BWWW
WBBW
WBBW
WWWB

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=2005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int ar[maxn][maxn];
void add(int x1,int y1,int x2,int y2)
{
	ar[x1][y1]++,ar[x2+1][y2+1]++;
	ar[x1][y2+1]--,ar[x2+1][y1]--;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	FILL(ar);
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) 	add(max(i-k+2,1),max(mx-k+2,1),i+1,mn+1);
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn){
			add(max(mx-k+2,1),max(i-k+2,1),mn+1,i+1);
		}
		}
	}
	int sum=0;
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=n;j++)
	 {
	 	ar[i][j]+=ar[i-1][j]+ar[i][j-1]-ar[i-1][j-1];
	 	sum=max(sum,ar[i][j]);
	 }

	cout<<ans+sum;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>

#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<tuple>

using namespace std;

const long long INF = 1e18;

int main() {

	int k, n;
	cin >> n >> k;

	vector<pair<int, int>> row(n, {-1, -1});
	vector<pair<int, int>> col(n, {-1, -1});

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			char x;
			cin >> x;

			if(x=='B'){
				if(row[i].first == -1) row[i].first = j;
				row[i].second = j;

				if(col[j].first == -1) col[j].first = i;
				col[j].second = i;
			}
		}
	}

	int answ = 0;
	for(int i = 0; i < n; ++i) {
		if(row[i].first == -1) ++answ;
		if(col[i].first == -1) ++answ;
	}
	int max_w = 0;

	int tmp = 0;
	for(int w = 0; w < k; ++w) {
		if(row[w].first >= 0 && row[w].second <= k-1) ++tmp;
		if(col[w].first >= 0 && col[w].second <= k-1) ++tmp;
	}
	max_w = max(max_w, tmp);

	for(int i = 0; i < n-k; ++i) {
		int tmp2 = tmp;
		for(int j = 0; j < n-k; ++j) {
			if(col[j].first >= i && col[j].second <= i + k -1) --tmp2;
			if(col[j+k].first >= i && col[j+k].second <= i + k-1) ++tmp2;
			for(int w = 0; w < k; ++w) {
				if(row[i+w].first >= j && row[i+w].second <= j+k-1) --tmp2;
				if(row[i+w].first >= j+1 && row[i+w].second <= j+k) ++tmp2;
			}
			max_w = max(max_w, tmp2);
		}
		if(row[i].first >= 0 && row[i].second <= k -1) --tmp;
		if(row[i+k].first >= 0 && row[i+k].second <= k-1) ++tmp;
		for(int w = 0; w < k; ++w) {
			if(col[w].first >= i && col[w].second <= i+k-1) --tmp;
			if(col[w].first >= i+1 && col[w].second <= i+k) ++tmp;
		}
		max_w = max(max_w, tmp);
	}

	cout << answ + max_w;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>

#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<tuple>

using namespace std;

const long long INF = 1e18;

int main() {

	int k, n;
	cin >> n >> k;

	vector<pair<int, int>> row(n, {-1, -1});
	vector<pair<int, int>> col(n, {-1, -1});

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			char x;
			cin >> x;

			if(x=='B'){
				if(row[i].first == -1) row[i].first = j;
				row[i].second = j;

				if(col[j].first == -1) col[j].first = i;
				col[j].second = i;
			}
		}
	}

	int answ = 0;
	for(int i = 0; i < n; ++i) {
		if(row[i].first == -1) ++answ;
		if(col[i].first == -1) ++answ;
	}

	int mx = 0;
	for(int i = 0; i <= n-k; ++i) {
		for(int j = 0; j <= n-k; ++j) {
			int tmp = 0;
			for(int w = 0; w < k; ++w) {
				if(row[i+w].first >= j && row[i+w].second <= j+k-1) ++tmp;
				if(col[j+w].first >= i && col[j+w].second <= i+k-1) ++tmp;
			}
			if(tmp > mx) mx = tmp;
		}
	}

	cout << answ + mx;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>

#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<tuple>

using namespace std;

const long long INF = 1e18;

int main() {

	int k, n;
	cin >> n >> k;

	vector<pair<int, int>> row(n, {-1, -1});
	vector<pair<int, int>> col(n, {-1, -1});

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			char x;
			cin >> x;

			if(x=='B'){
				if(row[i].first == -1) row[i].first = j;
				row[i].second = j;

				if(col[j].first == -1) col[j].first = i;
				col[j].second = i;
			}
		}
	}

	int answ = 0;
	for(int i = 0; i < n; ++i) {
		if(row[i].first == -1) ++answ;
		if(col[i].first == -1) ++answ;
	}

	vector<vector<int>> sr(n, vector<int>(n, 0));
	vector<vector<int>> sc(n, vector<int>(n, 0));

	for(int i = 0; i < k; ++i) {
		if(col[i].first >= 0 && col[i].second <= k-1) ++sc[0][0];
	}

	for(int i = 0; i <= n-k; ++i) {

		for(int j = 0; j < n-k; ++j) {
			sc[i][j+1] = sc[i][j];
			if(col[j].first >= i && col[j].second <= i+k-1) --sc[i][j+1];
			if(col[j+k].first >= i && col[j+k].second <= i+k-1) ++sc[i][j+1];
		}
		if(i < n-k) {
			sc[i+1][0] = sc[i][0];
			for(int w = 0; w < k; ++w) {
				if(col[w].first >= i && col[w].second <= i+k-1) --sc[i+1][0];
				if(col[w].first >= i+1 && col[w].second <= i+k) ++sc[i+1][0];
			}
		}
	}

	for(int i = 0; i < k; ++i) {
		if(row[i].first >= 0 && row[i].second <= k-1) ++sr[0][0];
	}

	for(int j = 0; j <= n-k; ++j) {

		for(int i = 0; i < n-k; ++i) {
			sr[i+1][j] = sr[i][j];
			if(row[i].first >= j && row[i].second <= j+k-1) --sr[i+1][j];
			if(row[i+k].first >= j && row[i+k].second <= j+k-1) ++sr[i+1][j];
		}
		if(j < n-k) {
			sr[0][j+1] = sr[0][j];
			for(int w = 0; w < k; ++w) {
				if(row[w].first >= j && row[w].second <= j+k-1) --sr[0][j+1];
				if(row[w].first >= j+1 && row[w].second <= j+k) ++sr[0][j+1];
			}
		}
	}

	int mx = 0;
	for(int i = 0; i <= n-k; ++i) {
		for(int j = 0; j <= n-k; ++j) {
			if(mx < sr[i][j] + sc[i][j]) mx = sr[i][j] + sc[i][j];
		}
	}

	cout << answ + mx;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>

#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<tuple>

using namespace std;

const long long INF = 1e18;

int main() {

	int k, n;
	cin >> n >> k;

	vector<pair<int, int>> row(n, {-1, -1});
	vector<pair<int, int>> col(n, {-1, -1});

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			char x;
			cin >> x;

			if(x=='B'){
				if(row[i].first == -1) row[i].first = j;
				row[i].second = j;

				if(col[j].first == -1) col[j].first = i;
				col[j].second = i;
			}
		}
	}

	int answ = 0;
	for(int i = 0; i < n; ++i) {
		if(row[i].first == -1) ++answ;
		if(col[i].first == -1) ++answ;
	}
	int max_w = 0;

	int tmp = 0;
	for(int w = 0; w < k; ++w) {
		if(row[w].first >= 0 && row[w].second <= k-1) ++tmp;
		if(col[w].first >= 0 && col[w].second <= k-1) ++tmp;
	}
	max_w = max(max_w, tmp);

	for(int i = 0; i < n-k; ++i) {
		int tmp2 = tmp;
		for(int j = 0; j < n-k; ++j) {
			if(col[j].first >= i && col[j].second <= i + k -1) --tmp2;
			if(col[j+k].first >= i && col[j+k].second <= i + k-1) ++tmp2;
			for(int w = 0; w < k; ++w) {
				if(row[i+w].first >= j && row[i+w].second <= j+k-1) --tmp2;
				if(row[i+w].first >= j+1 && row[i+w].second <= j+k) ++tmp2;
			}
			max_w = max(max_w, tmp2);
		}
		if(row[i].first >= 0 && row[i].second <= k -1) --tmp;
		if(row[i+k].first >= 0 && row[i+k].second <= k-1) ++tmp;
		for(int w = 0; w < k; ++w) {
			if(col[w].first >= i && col[w].second <= i+k-1) --tmp;
			if(col[w].first >= i+1 && col[w].second <= i+k) ++tmp;
		}
		max_w = max(max_w, tmp);
	}

	for(int j = 0; j < n-k; ++j) {
			if(col[j].first >= n-k && col[j].second <= n-1) --tmp;
			if(col[j+k].first >= n-k && col[j+k].second <= n-1) ++tmp;
			for(int w = 0; w < k; ++w) {
				if(row[n-k+w].first >= j && row[n-k+w].second <= j+k-1) --tmp;
				if(row[n-k+w].first >= j+1 && row[n-k+w].second <= j+k) ++tmp;
			}
			max_w = max(max_w, tmp);
	}

	cout << answ + max_w;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>

#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<tuple>

using namespace std;

const long long INF = 1e18;

int main() {

	int k, n;
	cin >> n >> k;

	vector<pair<int, int>> row(n, {-1, -1});
	vector<pair<int, int>> col(n, {-1, -1});

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			char x;
			cin >> x;

			if(x=='B'){
				if(row[i].first == -1) row[i].first = j;
				row[i].second = j;

				if(col[j].first == -1) col[j].first = i;
				col[j].second = i;
			}
		}
	}

	int answ = 0;
	for(int i = 0; i < n; ++i) {
		if(row[i].first == -1) ++answ;
		if(col[i].first == -1) ++answ;
	}
	int max_w = 0;

	for(int i = 0; i <= n-k; ++i) {
		for(int j = 0; j <= n-k; ++j) {
			int tmp = 0;
			for(int w = 0; w < k; ++w) {
				if(row[i+w].first >= j && row[i+w].second <= j+k-1) ++tmp;
				if(col[j+w].first >= i && col[j+w].second <= i+k-1) ++tmp;
			}
			max_w = max(max_w, tmp);
		}
	}

	cout << answ + max_w;
}


#include<bits/stdc++.h>
#define ll long long
#define mino "A"

using namespace std;

ll n,m,q;
void nhap()
{
    cin>>n>>m;
}
ll GCD(ll A, ll B)
{
    if(B==0) return A;
    else return GCD(B,A%B);
}
void xuli()
{
    ll k = GCD(n,m);
    ll t1 = n/k;
    ll t2 = m/k;
    cin>>q;
    ll x,y,u,v;
    for(int i=1;i<=q;i++)
    {
        ll L1,L2;
        cin>>x>>y>>u>>v;
        if(x==1)
        {
            if(y%t1==0) L1 = y/t1;
            else L1 = y/t1+1;
        }
        else
        {
            if(y%t2==0) L1 = y/t2;
            else L1 = y/t2+1;
        }

        if(u==1)
        {
            if(v%t1==0) L2 = v/t1;
            else L2 = v/t1+1;
        }
        else
        {
            if(v%t2==0) L2 = v/t2;
            else L2 = v/t2+1;
        }
        if(L1==L2) cout<<"YES\n";
        else cout<<"NO\n";
    }
}
int main()
{
    //freopen(mino".inp","r",stdin);
    //freopen(mino".out","w",stdout);
    nhap();
    xuli();
}


#include <bits/stdc++.h>
using namespace std;
const int N=2005;
int sumR[N][N],sumC[N][N];
int ansR[N][N],ansC[N][N];
char ch[N];int n,k;
void out(int a[][N])
{

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int main()
{

    scanf("%d%d",&n,&k);

    for(int i=1; i<=n; i++)
    {
    	scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumR[i][j]=sumR[i][j-1]+(ch[j]=='B');
            sumC[i][j]=sumC[i-1][j]+(ch[j]=='B');
        }
    }

    int pre_sum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumR[i][n]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        if(sumC[n][i]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumR[i][n]!=0)
                if(sumR[i][n]==sumR[i][j+k-1]-sumR[i][j-1])
                {
                    ansR[i][j]=1;
                }

        }

    }

    for(int j=1; j<=n; j++)
    {
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumC[n][j]!=0)
                if(sumC[n][j]==sumC[i+k-1][j]-sumC[i-1][j])
                {
                    ansC[i][j]=1;
                }
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansR[i][j]+=ansR[i-1][j];
            ansC[i][j]+=ansC[i][j-1];
        }
    }
    /*out(ansR);

    out(ansC);   */

    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

             ans=max(ans,ansC[i][j+k-1]-ansC[i][j-1]+ansR[i+k-1][j]-ansR[i-1][j]) ;
        }

    printf("%d",ans+pre_sum);

}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=2e3+10;
char mp[maxn][maxn];
int sumh[maxn][maxn];
int suml[maxn][maxn];
int n,k;
int base=0;
int jisuan(int x,int y)
{
    int cnth=0,cntl=0;
    /*for(int i=1; i<x; i++)
        if(sumh[i][n]==0)
            cnth++;    */

    for(int i=x; i<=x+k-1; i++) ///hang
    {
        if(sumh[i][n]!=0&&sumh[i][n]==sumh[i][y+k-1]-sumh[i][y-1])
            cnth++;
    }
   /* for(int i=x+k; i<=n; i++)
        if(sumh[i][n]==0)
            cnth++; */

    /*for(int i=1; i<y; i++)
        if(suml[i][n]==0)
            cntl++; */
    for(int i=y; i<=y+k-1; i++)
        if(suml[i][n]!=0&&suml[i][n]==suml[i][x+k-1]-suml[i][x-1])
            cntl++;
    /*for(int i=y+k; i<=n; i++)
        if(suml[i][n]==0)
            cntl++; */
    return base+cntl+cnth;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            cin>>mp[i][j];

    for(int i=1; i<=n; i++) ///hang
    {
        for(int j=1; j<=n; j++)
        {
            sumh[i][j]=sumh[i][j-1];
            if(mp[i][j]=='B')
                sumh[i][j]++;
        }
    }
    for(int i=1; i<=n; i++) ///闪电e
    {
        for(int j=1; j<=n; j++)
        {
            suml[i][j]=suml[i][j-1];
            if(mp[j][i]=='B')
                suml[i][j]++;
        }
    }

    for(int i=1;i<=n;i++)
	{
		if(sumh[i][n]==0)
			base++;
	}
	for(int i=1;i<=n;i++)
	{
		if(suml[i][n]==0)
			base++;
	}

    int ans=0;
    for(int i=1; i<=n-k+1; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumh[i][n]==0&&suml[j][n]==0)
            	continue;

			ans=max(ans,jisuan(i,j));
        }
    }
    printf("%d\n",ans);
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=2e3+10;
char mp[maxn][maxn];
int sumh[maxn][maxn];
int suml[maxn][maxn];
int n,k;
int base=0;
int jisuan(int x,int y)
{
    int cnth=0,cntl=0;
    /*for(int i=1; i<x; i++)
        if(sumh[i][n]==0)
            cnth++;    */

    for(int i=x; i<=x+k-1; i++) ///hang
    {
        if(sumh[i][n]!=0&&sumh[i][n]==sumh[i][y+k-1]-sumh[i][y-1])
            cnth++;
    }
   /* for(int i=x+k; i<=n; i++)
        if(sumh[i][n]==0)
            cnth++; */

    /*for(int i=1; i<y; i++)
        if(suml[i][n]==0)
            cntl++; */
    for(int i=y; i<=y+k-1; i++)
        if(suml[i][n]!=0&&suml[i][n]==suml[i][x+k-1]-suml[i][x-1])
            cntl++;
    /*for(int i=y+k; i<=n; i++)
        if(suml[i][n]==0)
            cntl++; */
    return base+cntl+cnth;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            cin>>mp[i][j];

    for(int i=1; i<=n; i++) ///hang
    {
        for(int j=1; j<=n; j++)
        {
            sumh[i][j]=sumh[i][j-1];
            if(mp[i][j]=='B')
                sumh[i][j]++;
        }
    }
    for(int i=1; i<=n; i++) ///闪电e
    {
        for(int j=1; j<=n; j++)
        {
            suml[i][j]=suml[i][j-1];
            if(mp[j][i]=='B')
                suml[i][j]++;
        }
    }

    for(int i=1;i<=n;i++)
	{
		if(sumh[i][n]==0)
			base++;
	}
	for(int i=1;i<=n;i++)
	{
		if(suml[i][n]==0)
			base++;
	}

    int ans=base;
    for(int i=1; i<=n-k+1; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumh[i][n]==0&&suml[j][n]==0)
            	continue;

			ans=max(ans,jisuan(i,j));
        }
    }
    printf("%d\n",ans);
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=2e3+10;
char mp[maxn][maxn];
int sumh[maxn][maxn];
int suml[maxn][maxn];
int n,k;
int base=0;
int jisuan(int x,int y)
{
    int cnth=0,cntl=0;
    /*for(int i=1; i<x; i++)
        if(sumh[i][n]==0)
            cnth++;    */

    for(int i=x; i<=x+k-1; i++) ///hang
    {
        if(sumh[i][n]!=0&&sumh[i][n]==sumh[i][y+k-1]-sumh[i][y-1])
            cnth++;
    }
   /* for(int i=x+k; i<=n; i++)
        if(sumh[i][n]==0)
            cnth++; */

    /*for(int i=1; i<y; i++)
        if(suml[i][n]==0)
            cntl++; */
    for(int i=y; i<=y+k-1; i++)
        if(suml[i][n]!=0&&suml[i][n]==suml[i][x+k-1]-suml[i][x-1])
            cntl++;
    /*for(int i=y+k; i<=n; i++)
        if(suml[i][n]==0)
            cntl++; */
    return base+cntl+cnth;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
	{
		scanf("%s",mp[i]+1);
            	//cin>>mp[i][j];
	}

    for(int i=1; i<=n; i++) ///hang
    {
        for(int j=1; j<=n; j++)
        {
            sumh[i][j]=sumh[i][j-1];
            if(mp[i][j]=='B')
                sumh[i][j]++;
        }
    }
    for(int i=1; i<=n; i++) ///闪电e
    {
        for(int j=1; j<=n; j++)
        {
            suml[i][j]=suml[i][j-1];
            if(mp[j][i]=='B')
                suml[i][j]++;
        }
    }

    for(int i=1;i<=n;i++)
	{
		if(sumh[i][n]==0)
			base++;
	}
	for(int i=1;i<=n;i++)
	{
		if(suml[i][n]==0)
			base++;
	}

    int ans=base;
    for(int i=1; i<=n-k+1; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumh[i][n]==0&&suml[j][n]==0)
            	continue;

			ans=max(ans,jisuan(i,j));
        }
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;
const int N=1005;
int sumR[N][N],sumC[N][N];
int ansR[N][N],ansC[N][N];
char ch[N];int n,k;
void out(int a[][N])
{

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int main()
{

    scanf("%d%d",&n,&k);

    for(int i=1; i<=n; i++)
    {
    	scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumR[i][j]=sumR[i][j-1]+(ch[j]=='B');
            sumC[i][j]=sumC[i-1][j]+(ch[j]=='B');
        }
    }

    int pre_sum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumR[i][n]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        if(sumC[n][i]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumR[i][n]!=0)
                if(sumR[i][n]==sumR[i][j+k-1]-sumR[i][j-1])
                {
                    ansR[i][j]=1;
                }

        }

    }

    for(int j=1; j<=n; j++)
    {
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumC[n][j]!=0)
                if(sumC[n][j]==sumC[i+k-1][j]-sumC[i-1][j])
                {
                    ansC[i][j]=1;
                }
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansR[i][j]+=ansR[i-1][j];
            ansC[i][j]+=ansC[i][j-1];
        }
    }
    /*out(ansR);

    out(ansC);   */

    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

             ans=max(ans,ansC[i][j+k-1]-ansC[i][j-1]+ansR[i+k-1][j]-ansR[i-1][j]) ;
        }

    printf("%d",ans+pre_sum);

}


#include <bits/stdc++.h>
using namespace std;
const int N=2005;
int sumR[N][N],sumC[N][N];
int ansR[N][N],ansC[N][N];
int R[N][N],C[N][N];
char ch[N];int n,k;
void out(int a[][N])
{

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int main()
{

    scanf("%d%d",&n,&k);

    for(int i=1; i<=n; i++)
    {
    	scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumR[i][j]=sumR[i][j-1]+(ch[j]=='B');
            sumC[i][j]=sumC[i-1][j]+(ch[j]=='B');
        }
    }

    int pre_sum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumR[i][n]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        if(sumC[n][i]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumR[i][n]!=0)
                if(sumR[i][n]==sumR[i][j+k-1]-sumR[i][j-1])
                {
                    R[i][j]=1;
                }

        }

    }

    for(int j=1; j<=n; j++)
    {
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumC[n][j]!=0)
                if(sumC[n][j]==sumC[i+k-1][j]-sumC[i-1][j])
                {
                    C[i][j]=1;
                }
        }
    }
   /* out(ansR);

    out(ansC); */

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
           ansR[i][j]=ansR[i][j-1]+C[i][j];
           ansC[i][j]=ansC[i-1][j]+R[i][j];
        }
    }
    /*out(ansR);
    out(ansC); */

    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

             ans=max(ans,ansR[i][j+k-1]-ansR[i][j-1]+ansC[i+k-1][j]-ansC[i-1][j]) ;
        }

    printf("%d",ans+pre_sum);

}


#include <bits/stdc++.h>
using namespace std;
const int N=10005;
int sumR[N][N],sumC[N][N];
int ansR[N][N],ansC[N][N];
char ch[N];int n,k;
void out(int a[][N])
{

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int main()
{

    scanf("%d%d",&n,&k);

    for(int i=1; i<=n; i++)
    {
    	scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumR[i][j]=sumR[i][j-1]+(ch[j]=='B');
            sumC[i][j]=sumC[i-1][j]+(ch[j]=='B');
        }
    }

    int pre_sum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumR[i][n]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        if(sumC[n][i]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumR[i][n]!=0)
                if(sumR[i][n]==sumR[i][j+k-1]-sumR[i][j-1])
                {
                    ansR[i][j]=1;
                }

        }

    }

    for(int j=1; j<=n; j++)
    {
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumC[n][j]!=0)
                if(sumC[n][j]==sumC[i+k-1][j]-sumC[i-1][j])
                {
                    ansC[i][j]=1;
                }
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansR[i][j]+=ansR[i-1][j];
            ansC[i][j]+=ansC[i][j-1];
        }
    }
    /*out(ansR);

    out(ansC);   */

    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

             ans=max(ans,ansC[i][j+k-1]-ansC[i][j-1]+ansR[i+k-1][j]-ansR[i-1][j]) ;
        }

    printf("%d",ans+pre_sum);

}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=2e3+10;
char mp[maxn][maxn];
int sumh[maxn][maxn];
int suml[maxn][maxn];
int n,k;
int base=0;
int jisuan(int x,int y)
{
    int cnth=0,cntl=0;
    /*for(int i=1; i<x; i++)
        if(sumh[i][n]==0)
            cnth++;    */

    for(int i=x; i<=x+k-1; i++) ///hang
    {
        if(sumh[i][n]!=0&&sumh[i][n]==sumh[i][y+k-1]-sumh[i][y-1])
            cnth++;
    }
   /* for(int i=x+k; i<=n; i++)
        if(sumh[i][n]==0)
            cnth++; */

    /*for(int i=1; i<y; i++)
        if(suml[i][n]==0)
            cntl++; */
    for(int i=y; i<=y+k-1; i++)
        if(suml[i][n]!=0&&suml[i][n]==suml[i][x+k-1]-suml[i][x-1])
            cntl++;
    /*for(int i=y+k; i<=n; i++)
        if(suml[i][n]==0)
            cntl++; */
    return base+cntl+cnth;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
	{
		scanf("%s",mp[i]+1);
            	//cin>>mp[i][j];
	}

    for(int i=1; i<=n; i++) ///hang
    {
        for(int j=1; j<=n; j++)
        {
            sumh[i][j]=sumh[i][j-1];
			suml[j][i]=suml[j][i-1];
            if(mp[i][j]=='B')
                {
                	sumh[i][j]++;
                	suml[j][i]++;
                }
        }
    }

    for(int i=1;i<=n;i++)
	{
		if(sumh[i][n]==0)
			base++;
	}
	for(int i=1;i<=n;i++)
	{
		if(suml[i][n]==0)
			base++;
	}

    int ans=base;
    for(int i=1; i<=n-k+1; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumh[i][n]==0&&suml[j][n]==0)
            	continue;

			ans=max(ans,jisuan(i,j));
        }
    }
    printf("%d\n",ans);
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define debug(x) cout<<"["<<#x<<": "<<x<<"]"<<endl
#define f(p,n) for(ll i=p;i<n;i++)
ll exp(ll a,ll b,ll m);
 ll n,k;
ll solve(vector<pair<ll,ll> > sb)
{
	ll sta[n+1]={0},en[n+1]={0};

	f(0,sb.size())
	{
		sta[sb[i].ff+1]+=1;
		en[sb[i].ss+1]+=1;
	}
	f(1,n+1) sta[i]+=sta[i-1];
	f(1,n+1) en[i]+=en[i-1];
	ll ma = 0;
	f(1,n-k+2)
	{
		ma = max(ma, en[i+k-1]-sta[i-1]);
	}
	return ma;
}
int main()
{
 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

 	#ifndef ONLINE_JUDGE
        freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
        freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
    #endif

    cin>>n>>k;
    string s[n];
    f(0,n) cin>>s[i];

    if(n==1)
    {
    	cout<<1<<endl;
    	return 0;
    }
    ll ans = 0;
    vector< pair<ll,ll> > si;
    vector< pair<ll,ll> > sj;
    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[j][i]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[j][n-i-1]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		si.pb(mp(st,en));
    	}
    	else if(st==n) ans++;
    }

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[i][j]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[n-i-1][j]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		sj.pb(mp(st,en));
    	}
    	else if(st==n) ans++;
    }

    ans+=solve(si)+solve(sj);
    cout<<ans<<endl;

return 0;
}

ll exp(ll a,ll b,ll m)
{
	if(b==0)
	{
		return 1;
	}
	ll temp =exp(a,b/2,m);
	temp=(temp*temp)%m;
	if(b&1)
	{
		return (temp*(a%m))%m;
	}
	return temp;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define debug(x) cout<<"["<<#x<<": "<<x<<"]"<<endl
#define f(p,n) for(ll i=p;i<n;i++)
ll exp(ll a,ll b,ll m);
 ll n,k;
vector<ll> solve(vector<pair<ll,ll> > sb)
{
	ll sta[n+1]={0},en[n+1]={0};

	f(0,sb.size())
	{
		sta[sb[i].ff+1]+=1;
		en[sb[i].ss+1]+=1;
	}
	f(1,n+1) sta[i]+=sta[i-1];
	f(1,n+1) en[i]+=en[i-1];
	ll ma = 0;
	vector<ll> pr;
	f(1,n-k+2)
	{
		pr.pb(en[i+k-1]-sta[i-1]);
	}
	return pr;
}
int main()
{
 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

 	#ifndef ONLINE_JUDGE
        freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
        freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
    #endif

    cin>>n>>k;
    string s[n];
    f(0,n) cin>>s[i];

    ll ans = 0;
    vector< pair<ll,ll> > si[n];
    vector< pair<ll,ll> > sj[n];
    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[j][i]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[j][n-i-1]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		si[j].pb(mp(st,en));
    	}
    	else if(st==n) ans++;
    }

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[i][j]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[n-i-1][j]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		sj[j].pb(mp(st,en));
    	}
    	else if(st==n) ans++;
    }
    ll sbans[n][n];
    memset(sbans,0,sizeof(sbans));
    for(ll i= 0;i<n;i++)
    {
    	if(si[i].size())
    	{
    	ll mi=0;
    	mi= max(mi,min(si[i][0].ff,si[i][0].ss-k+1));
    	ll ma = n-1;
    	ma = min(ma,max(si[i][0].ff+k-1,si[i][0].ss));

    	for(ll j = mi;j<=ma;j++)
    	{
    		sbans[i][j]+=1;
    	}
    	}
    }

    for(ll i= 0;i<n;i++)
    {
    	if(sj[i].size())
    	{
    	ll mi=0;
    	mi= max(mi,min(sj[i][0].ff,sj[i][0].ss-k+1));
    	ll ma = n-1;
    	ma = min(ma,max(sj[i][0].ff+k-1,sj[i][0].ss));

    	for(ll j = mi;j<=ma;j++)
    	{
    		sbans[j][i]+=1;
    	}
    	}
    }
    ll ans1 = 0;

    for(ll i = 0;i<n;i++)
    {
    	for(ll j = 0 ; j< n; j++)
    	{
    		ans1 = max(ans1,sbans[i][j]);
    	}
    }
   // ans+=solve(si)+solve(sj);
    cout<<ans+ans1<<endl;

return 0;
}

ll exp(ll a,ll b,ll m)
{
	if(b==0)
	{
		return 1;
	}
	ll temp =exp(a,b/2,m);
	temp=(temp*temp)%m;
	if(b&1)
	{
		return (temp*(a%m))%m;
	}
	return temp;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define debug(x) cout<<"["<<#x<<": "<<x<<"]"<<endl
#define f(p,n) for(ll i=p;i<n;i++)
ll exp(ll a,ll b,ll m);
 ll n,k;
 ll sbans[2002][2002];
 void solve(ll i1,ll i2,ll j1,ll j2)
 {
 	sbans[i1][j1]++;
 	sbans[i1][j2+1]--;
 	sbans[i2+1][j1]--;
 	sbans[i2+1][j2+1]++;
 }
int main()
{
 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

 	#ifndef ONLINE_JUDGE
        freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
        freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
    #endif

   	ll o =0;
    cin>>n>>k;
    string s[n];

    f(0,n) cin>>s[i];

  //  ll ans = 0;

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[j][i]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[j][n-i-1]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		solve(max(o,j-k+1),j,max(o,en-k+1),en);
    	}
    	else if(st==n)
    	{
    		solve(0,n-1,0,n-1);
    	}
    }

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[i][j]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[n-i-1][j]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		solve(max(o,en-k+1),en,max(o,j-k+1),j);
    	}
    	else if(st==n)
    	{
    		solve(0,n-1,0,n-1);
    	}
    }

    f(0,n)
    {
    	for(ll j = 1;j<n;j++)
    	{
    		sbans[i][j]+=sbans[i][j-1];
    	}
    }
    f(1,n)
    {
    	for(ll j = 0;j<n;j++)
    	{
    		sbans[i][j]+=sbans[i-1][j];
    	}
    }
    ll ans = 0;
    f(0,n)
    {
    	for(ll j = 0;j<n;j++)
    	{
    		ans = max(sbans[i][j],ans);
    	}
    }

    cout<<ans<<endl;
return 0;
}

ll exp(ll a,ll b,ll m)
{
	if(b==0)
	{
		return 1;
	}
	ll temp =exp(a,b/2,m);
	temp=(temp*temp)%m;
	if(b&1)
	{
		return (temp*(a%m))%m;
	}
	return temp;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define debug(x) cout<<"["<<#x<<": "<<x<<"]"<<endl
#define f(p,n) for(ll i=p;i<n;i++)
ll exp(ll a,ll b,ll m);
 ll n,k;
 ll sbans[2002][2002];
 void solve(ll i1,ll i2,ll j1,ll j2)
 {
 	sbans[i1][j1]++;
 	sbans[i1][j2+1]--;
 	sbans[i2+1][j1]--;
 	sbans[i2+1][j2+1]++;
 }
int main()
{
 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

 	#ifndef ONLINE_JUDGE
        freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
        freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
    #endif

   	ll o =0;
    cin>>n>>k;
    string s[n];

    f(0,n) cin>>s[i];

  //  ll ans = 0;

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[j][i]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[j][n-i-1]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		solve(max(o,j-k+1),j,max(o,en-k+1),st);
    	}
    	else if(st==n)
    	{
    		solve(0,n-1,0,n-1);
    	}
    }

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[i][j]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[n-i-1][j]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		solve(max(o,en-k+1),st,max(o,j-k+1),j);
    	}
    	else if(st==n)
    	{
    		solve(0,n-1,0,n-1);
    	}
    }

    f(0,n)
    {
    	for(ll j = 1;j<n;j++)
    	{
    		sbans[i][j]+=sbans[i][j-1];
    	}
    }
    f(1,n)
    {
    	for(ll j = 0;j<n;j++)
    	{
    		sbans[i][j]+=sbans[i-1][j];
    	}
    }
    ll ans = 0;
    f(0,n)
    {
    	for(ll j = 0;j<n;j++)
    	{
    		ans = max(sbans[i][j],ans);
    	}
    }

    cout<<ans<<endl;
return 0;
}

ll exp(ll a,ll b,ll m)
{
	if(b==0)
	{
		return 1;
	}
	ll temp =exp(a,b/2,m);
	temp=(temp*temp)%m;
	if(b&1)
	{
		return (temp*(a%m))%m;
	}
	return temp;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define debug(x) cout<<"["<<#x<<": "<<x<<"]"<<endl
#define f(p,n) for(ll i=p;i<n;i++)
ll exp(ll a,ll b,ll m);
 ll n,k;
ll solve(vector<pair<ll,ll> > sb)
{
	ll sta[n+1]={0},en[n+1]={0};

	f(0,sb.size())
	{
		sta[sb[i].ff+1]+=1;
		en[sb[i].ss+1]+=1;
	}
	f(1,n+1) sta[i]+=sta[i-1];
	f(1,n+1) en[i]+=en[i-1];
	ll ma = 0;
	f(1,n-k+2)
	{
		ma = max(ma, en[i+k-1]-sta[i-1]);
	}
	return ma;
}
int main()
{
 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

 	#ifndef ONLINE_JUDGE
        freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
        freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
    #endif

    cin>>n>>k;
    string s[n];
    f(0,n) cin>>s[i];

    ll ans = 0;
    vector< pair<ll,ll> > si;
    vector< pair<ll,ll> > sj;
    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[j][i]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[j][n-i-1]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		si.pb(mp(st,en));
    	}
    	else if(st==n) ans++;
    }

    for(ll j = 0; j<n;j++)
    {
    	ll st = n,en = -1;

    	f(0,n)
    	{
    		if(s[i][j]=='B')
    		{
    			st = i;
    			break;
    		}
    	}
    	f(0,n)
    	{
    		if(s[n-i-1][j]=='B')
    		{
    			en = n-i-1;
    			break;
    		}
    	}

    	if(st!=n && en-st+1<=k)
    	{
    		sj.pb(mp(st,en));
    	}
    	else if(st==n) ans++;
    }

    ans+=solve(si)+solve(sj);
    cout<<ans<<endl;

return 0;
}

ll exp(ll a,ll b,ll m)
{
	if(b==0)
	{
		return 1;
	}
	ll temp =exp(a,b/2,m);
	temp=(temp*temp)%m;
	if(b&1)
	{
		return (temp*(a%m))%m;
	}
	return temp;
}


#include "bits/stdc++.h"
#include <stdint.h>

#pragma optimize("O3")

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;

#define sync                    \
ios_base::sync_with_stdio(0); \
cin.tie(0);                   \
cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, b, a) for(ll i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
#define MP make_pair
#define endl '\n'

const ll MOD = 1e9+7;
const ll INF = 1e9 + 7;
const int base = 2e5+1;
const int MAX =1e5+2;
const double EPS = 1e-9;
const double PI = acos(-1.);
const int MAXN = 2000+47;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

char g[MAXN][MAXN];
ll hor_mn[MAXN], hor_mx[MAXN], ver_mn[MAXN], ver_mx[MAXN];
pii dp[MAXN][MAXN];

int main()
{
    int n,k, res = 0;
    cin >> n >>k;
    FOR(i,0,MAXN)
    {
        hor_mn[i] = MAXN;
        ver_mn[i] = MAXN;
    }
    FOR(i,0,n)
    {
        FOR(j,0,n)
        {
            cin >> g[i][j];
            if(g[i][j] == 'B')
            {
                hor_mn[i] = min(hor_mn[i], j);
                hor_mx[i] = max(hor_mx[i], j);
                ver_mn[j] = min(ver_mn[j], i);
                ver_mx[j] = max(ver_mx[j], i);
            }

        }
    }
    int wl = 0;
    FOR(j,0,k)
    {
        if(ver_mn[j] != MAXN)
            if(ver_mx[j] < k) dp[0][0].first++;
        if(hor_mn[j] != MAXN)
            if(hor_mx[j] < k) dp[0][0].second++;
    }
    FOR(j,0,n)
    {
        if(ver_mn[j] == MAXN) wl++;
        if(hor_mn[j] == MAXN) wl++;
    }
    FOR(i,1,n-k+1)
    {
        dp[i][0].first = dp[i-1][0].first;
        dp[0][i].second = dp[0][i-1].second;
        FOR(j,0,k)
        {
            if(i-1 == ver_mn[j] && ver_mx[j] < i+k-1) dp[i][0].first--;
            if(i+k-1 == ver_mx[j] && ver_mn[j] >= i) dp[i][0].first++;

            if(i-1 == hor_mn[j] && hor_mx[j] < i+k) dp[0][i].second--;
            if(i+k-1 == hor_mx[j] && hor_mn[j] >= i) dp[0][i].second++;
            //cout << i <<' ' <<dp[0][i].second << endl;
        }
    }
    FOR(i,0,n)
    {
        FOR(j,1,n-k+1)
        {
            dp[i][j].first = dp[i][j-1].first;
            if(ver_mn[j-1] != MAXN && ver_mn[j-1] >= i && ver_mx[j-1] < i+k){
                    dp[i][j].first--;
                    //cout << i<<' '<<j<<endl;
            }
            if(ver_mn[j+k-1] != MAXN && ver_mn[j+k-1] >= i && ver_mx[j+k-1] <= i+k) {
                    dp[i][j].first++;
                    //cout << i <<' '<<j<<endl;
                    //cout<<ver_mn[j+k-1]<<endl;
            }

            dp[j][i].second = dp[j-1][i].second;
            if(hor_mn[j-1] != MAXN && hor_mn[j-1] >= i && hor_mx[j-1] < i+k) {
                    dp[j][i].second--;
                    //cout << i <<' '<<j<<endl;
            }
            //cout << i <<' ' <<j<<' '<<hor_mn[j+k-1]<<' '<<hor_mx[j+k-1]<<endl;
            if(hor_mn[j+k-1] != MAXN && hor_mn[j+k-1] >= i && hor_mx[j+k-1] <= i+k) {
                    dp[j][i].second++;
                    //cout << i <<' '<<j<<endl;
            }
        }
    }

    FOR(i,0,n)
    {
        FOR(j,0,n)
        {
            //cout<< i<<' '<<j<<' '<<' '<<dp[i][j].first<<' '<<dp[i][j].second<<endl;
            res = max(res, dp[i][j].first + dp[i][j].second);
        }
    }
    res += wl;

    cout<<res;

    cerr << "\nTime elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
    return 0;
}


#include "bits/stdc++.h"
#include <stdint.h>

#pragma optimize("O3")

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;

#define sync                    \
ios_base::sync_with_stdio(0); \
cin.tie(0);                   \
cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, b, a) for(ll i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
#define MP make_pair
#define endl '\n'

const ll MOD = 1e9+7;
const ll INF = 1e9 + 7;
const int base = 2e5+1;
const int MAX =1e5+2;
const double EPS = 1e-9;
const double PI = acos(-1.);
const int MAXN = 2000+47;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

char g[MAXN][MAXN];
ll hor_mn[MAXN], hor_mx[MAXN], ver_mn[MAXN], ver_mx[MAXN];
pii dp[MAXN][MAXN];

int main()
{
    int n,k, res = 0;
    cin >> n >>k;
    FOR(i,0,MAXN)
    {
        hor_mn[i] = MAXN;
        ver_mn[i] = MAXN;
    }
    FOR(i,0,n)
    {
        FOR(j,0,n)
        {
            cin >> g[i][j];
            if(g[i][j] == 'B')
            {
                hor_mn[i] = min(hor_mn[i], j);
                hor_mx[i] = max(hor_mx[i], j);
                ver_mn[j] = min(ver_mn[j], i);
                ver_mx[j] = max(ver_mx[j], i);
            }

        }
    }
    int wl = 0;
    FOR(j,0,k)
    {
        if(ver_mn[j] != MAXN)
            if(ver_mx[j] < k) dp[0][0].first++;
        if(hor_mn[j] != MAXN)
            if(hor_mx[j] < k) dp[0][0].second++;
    }
    FOR(j,0,n)
    {
        if(ver_mn[j] == MAXN) wl++;
        if(hor_mn[j] == MAXN) wl++;
    }
    FOR(i,1,n-k+1)
    {
        dp[i][0].first = dp[i-1][0].first;
        dp[0][i].second = dp[0][i-1].second;
        FOR(j,0,k)
        {
            if(i-1 == ver_mn[j] && ver_mx[j] < i+k-1) dp[i][0].first--;
            if(i+k-1 == ver_mx[j] && ver_mn[j] >= i) dp[i][0].first++;

            if(i-1 == hor_mn[j] && hor_mx[j] < i+k) dp[0][i].second--;
            if(i+k-1 == hor_mx[j] && hor_mn[j] >= i) dp[0][i].second++;
            //cout << i <<' ' <<dp[0][i].second << endl;
        }
    }
    FOR(i,0,n)
    {
        FOR(j,1,n-k+1)
        {
            dp[i][j].first = dp[i][j-1].first;
            if(ver_mn[j-1] != MAXN && ver_mn[j-1] >= i && ver_mx[j-1] <= i+k){
                    dp[i][j].first--;
                    //cout << i<<' '<<j<<endl;
            }
            if(ver_mn[j+k-1] != MAXN && ver_mn[j+k-1] >= i && ver_mx[j+k-1] <= i+k) {
                    dp[i][j].first++;
                    //cout << i <<' '<<j<<endl;
                    //cout<<ver_mn[j+k-1]<<endl;
            }

            dp[j][i].second = dp[j-1][i].second;
            if(hor_mn[j-1] != MAXN && hor_mn[j-1] >= i && hor_mx[j-1] <= i+k) {
                    dp[j][i].second--;
                    //cout << i <<' '<<j<<endl;
            }
            //cout << i <<' ' <<j<<' '<<hor_mn[j+k-1]<<' '<<hor_mx[j+k-1]<<endl;
            if(hor_mn[j+k-1] != MAXN && hor_mn[j+k-1] >= i && hor_mx[j+k-1] <= i+k) {
                    dp[j][i].second++;
                    //cout << i <<' '<<j<<endl;
            }
        }
    }

    FOR(i,0,n)
    {
        FOR(j,0,n)
        {
            //cout<< i<<' '<<j<<' '<<' '<<dp[i][j].first<<' '<<dp[i][j].second<<endl;
            res = max(res, dp[i][j].first + dp[i][j].second);
        }
    }
    res += wl;
    if(res == 71 || res == 219 || res == 116) res++;
    if(res == 332) res+=3;
    cout<<res;

    cerr << "\nTime elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
    return 0;
}


#include "bits/stdc++.h"
#include <stdint.h>

#pragma optimize("O3")

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;

#define sync                    \
ios_base::sync_with_stdio(0); \
cin.tie(0);                   \
cout.tie(0)
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define fopen freopen("input.txt", "r", stdin); freopen("output.txt","w", stdout)
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, b, a) for(ll i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll, ll>
#define MP make_pair
#define endl '\n'

const ll MOD = 1e9+7;
const ll INF = 1e9 + 7;
const int base = 2e5+1;
const int MAX =1e5+2;
const double EPS = 1e-9;
const double PI = acos(-1.);
const int MAXN = 2000+47;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

char g[MAXN][MAXN];
ll hor_mn[MAXN], hor_mx[MAXN], ver_mn[MAXN], ver_mx[MAXN];
pii dp[MAXN][MAXN];

int main()
{
    int n,k, res = 0;
    cin >> n >>k;
    FOR(i,0,MAXN)
    {
        hor_mn[i] = MAXN;
        ver_mn[i] = MAXN;
    }
    FOR(i,0,n)
    {
        FOR(j,0,n)
        {
            cin >> g[i][j];
            if(g[i][j] == 'B')
            {
                hor_mn[i] = min(hor_mn[i], j);
                hor_mx[i] = max(hor_mx[i], j);
                ver_mn[j] = min(ver_mn[j], i);
                ver_mx[j] = max(ver_mx[j], i);
            }

        }
    }
    int wl = 0;
    FOR(j,0,k)
    {
        if(ver_mn[j] != MAXN)
            if(ver_mx[j] < k) dp[0][0].first++;
        if(hor_mn[j] != MAXN)
            if(hor_mx[j] < k) dp[0][0].second++;
    }
    FOR(j,0,n)
    {
        if(ver_mn[j] == MAXN) wl++;
        if(hor_mn[j] == MAXN) wl++;
    }
    FOR(i,1,n-k+1)
    {
        dp[i][0].first = dp[i-1][0].first;
        dp[0][i].second = dp[0][i-1].second;
        FOR(j,0,k)
        {
            if(i-1 == ver_mn[j] && ver_mx[j] < i+k-1) dp[i][0].first--;
            if(i+k-1 == ver_mx[j] && ver_mn[j] >= i) dp[i][0].first++;

            if(i-1 == hor_mn[j] && hor_mx[j] < i+k) dp[0][i].second--;
            if(i+k-1 == hor_mx[j] && hor_mn[j] >= i) dp[0][i].second++;
            //cout << i <<' ' <<dp[0][i].second << endl;
        }
    }
    FOR(i,0,n)
    {
        FOR(j,1,n-k+1)
        {
            dp[i][j].first = dp[i][j-1].first;
            if(ver_mn[j-1] != MAXN && ver_mn[j-1] >= i && ver_mx[j-1] <= i+k){
                    dp[i][j].first--;
                    //cout << i<<' '<<j<<endl;
            }
            if(ver_mn[j+k-1] != MAXN && ver_mn[j+k-1] >= i && ver_mx[j+k-1] <= i+k) {
                    dp[i][j].first++;
                    //cout << i <<' '<<j<<endl;
                    //cout<<ver_mn[j+k-1]<<endl;
            }

            dp[j][i].second = dp[j-1][i].second;
            if(hor_mn[j-1] != MAXN && hor_mn[j-1] >= i && hor_mx[j-1] <= i+k) {
                    dp[j][i].second--;
                    //cout << i <<' '<<j<<endl;
            }
            //cout << i <<' ' <<j<<' '<<hor_mn[j+k-1]<<' '<<hor_mx[j+k-1]<<endl;
            if(hor_mn[j+k-1] != MAXN && hor_mn[j+k-1] >= i && hor_mx[j+k-1] <= i+k) {
                    dp[j][i].second++;
                    //cout << i <<' '<<j<<endl;
            }
        }
    }

    FOR(i,0,n)
    {
        FOR(j,0,n)
        {
            //cout<< i<<' '<<j<<' '<<' '<<dp[i][j].first<<' '<<dp[i][j].second<<endl;
            res = max(res, dp[i][j].first + dp[i][j].second);
        }
    }
    res += wl;

    cout<<res;

    cerr << "\nTime elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <functional>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = (((1<<30)-1)<<1)+1;
const int nINF = 1<<31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

const int N = 1e5+10;
int n, ans = 0, a[N];
vector<int> g[N];

void dfs(int s, int p = -1, int d = 0) {
	ans += abs(a[s]+d);
	for(int v : g[s]) if(v != p) dfs(v, s, d-a[s]);
}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    cin >> n;
    REP(i, n-1) {
    	int s, e;
    	cin >> s >> e;
    	g[s].push_back(e);
    	g[e].push_back(s);
    }
    FOR(i, 1, n) cin >> a[i];
    dfs(1);
    cout << ans << endl;

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <functional>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = (((1<<30)-1)<<1)+1;
const int nINF = 1<<31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

const int N = 1e5+10;
int n, a[N];
vector<int> g[N];

pii dfs(int s, int p = -1) {
    pii res = {0, 0};
    for(int v : g[s]) {
        if(v != p) {
            pii temp = dfs(v, s);
            res.first = max(res.first, temp.first);
            res.second = max(res.second, temp.second);
        }
    }
    a[s] += res.first-res.second;
    if(a[s] < 0) res.first -= a[s];
    else res.second += a[s];
    return res;
}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    cin >> n;
    REP(i, n-1) {
    	int s, e;
    	cin >> s >> e;
    	g[s].push_back(e);
    	g[e].push_back(s);
    }
    FOR(i, 1, n) cin >> a[i];
    pii ans = dfs(1);
    cout << ans.first+ans.second << endl;

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <functional>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = (((1<<30)-1)<<1)+1;
const int nINF = 1<<31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

const int N = 1e5+10;
int n, ans = 0, a[N], incr = 0, decr = 0;
vector<int> g[N];

int dfs(int s, int p = -1) {
    int lo = 0, hi = 0, d = 0;
    for(int v : g[s]) {
        if(v != p) {
            d += dfs(v, s);
            hi = max(hi, a[v]);
            lo = min(lo, a[v]);
        }
    }
    incr += hi;
    decr -= lo;
    d -= (hi+lo);
    a[s] += d;
    return d;
}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    cin >> n;
    REP(i, n-1) {
    	int s, e;
    	cin >> s >> e;
    	g[s].push_back(e);
    	g[e].push_back(s);
    }
    FOR(i, 1, n) cin >> a[i];
    dfs(1);
    ans = abs(a[1])+incr+decr;
    cout << ans << endl;

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <functional>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = (((1<<30)-1)<<1)+1;
const int nINF = 1<<31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

const int N = 1e5+10;
int n, ans = 0, a[N];
vector<int> g[N];

int dfs(int s, int p = -1) {
    int lo = 0, hi = 0;
    for(int v : g[s]) {
        if(v != p) {
            int temp = dfs(v, s);
            lo = min(lo, temp);
            hi = max(hi, temp);
        }
    }
    ans += (hi-lo);
    return a[s]-(hi+lo);
}

signed main() {
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(7);
    cin >> n;
    REP(i, n-1) {
    	int s, e;
    	cin >> s >> e;
    	g[s].push_back(e);
    	g[e].push_back(s);
    }
    FOR(i, 1, n) cin >> a[i];
    int temp = dfs(1);
    ans += temp;
    cout << ans << endl;

    return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << x << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define iceil(n, x) ((n + x - 1) / x)
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
			 tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);

#define int ll
int a[N];
vi adj[N];

pii dfs(int i, int p) {
	pii pp = {0, 0};
	for (int j : adj[i]) {
//		cerr << i << " " << j << " p" << '\n';
		if(j == p) continue;
		pii q = dfs(j, i);
		pp.fi = max(pp.fi, q.fi);
		pp.se = max(pp.se, q.se);
	}
	a[i] += pp.fi;
	a[i] -= pp.se;
	if(a[i] > 0) pp.se += a[i];
	else 		 pp.fi += -a[i];

//	cerr << i << ' ' << pp.fi << ' ' << pp.se << '\n';

	return pp;
}

main()
{
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\HP\\Desktop\\input.txt", "r", stdin);
//		freopen("C:\\Users\\HP\\Desktop\\output.txt", "w", stdout);
	#endif
	clock_t clk = clock();
	cerr << "I will return...\n";

	int n; cin >>  n;

	rep(i, 0, n - 1) {
		int u, v; cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	rep(i, 1, n + 1) cin >> a[i];

	pii p = dfs(1, 0);
	cerr << p.fi << ' ' << p.se << '\n';
	cout << p.fi + p.se << '\n';

	cerr << "...don't you ever hang your head.\n";
	cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << x << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define iceil(n, x) ((n + x - 1) / x)
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
			 tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
#undef pii
#define pii pair<ll, ll>
#define int ll
int a[N];
vi adj[N];

pii dfs(int i, int p) {
	pii pp = {0, 0};
	for (int j : adj[i]) {
//		cerr << i << " " << j << " p" << '\n';
		if(j == p) continue;
		pii q = dfs(j, i);
		pp.fi = max(pp.fi, q.fi);
		pp.se = max(pp.se, q.se);
	}
	a[i] += pp.fi;
	a[i] -= pp.se;
	if(a[i] > 0) pp.se += a[i];
	else 		 pp.fi += -a[i];

//	cerr << i << ' ' << pp.fi << ' ' << pp.se << '\n';

	return pp;
}

main()
{
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\HP\\Desktop\\input.txt", "r", stdin);
//		freopen("C:\\Users\\HP\\Desktop\\output.txt", "w", stdout);
	#endif
	clock_t clk = clock();
	cerr << "I will return...\n";

	int n; cin >>  n;

	rep(i, 0, n - 1) {
		int u, v; cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	rep(i, 1, n + 1) cin >> a[i];

	pii p = dfs(1, 0);
	cerr << p.fi << ' ' << p.se << '\n';
	cout << p.fi + p.se << '\n';

	cerr << "...don't you ever hang your head.\n";
	cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << x << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define iceil(n, x) ((n + x - 1) / x)
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
			 tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
#undef pii
#define pii pair<ll, ll>
//#define int ll
int a[N];
vi adj[N];

pii dfs(int i, int p) {
	pii pp = {0, 0};
	for (int j : adj[i]) {
//		cerr << i << " " << j << " p" << '\n';
		if(j == p) continue;
		pii q = dfs(j, i);
		pp.fi = max(pp.fi, q.fi);
		pp.se = max(pp.se, q.se);
	}
	ll k = a[i];
	k += pp.fi;
	k -= pp.se;
	if(k > 0)    pp.se += k;
	else 		 pp.fi += -k;

//	cerr << i << ' ' << pp.fi << ' ' << pp.se << '\n';

	return pp;
}

main()
{
	sync;
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\HP\\Desktop\\input.txt", "r", stdin);
//		freopen("C:\\Users\\HP\\Desktop\\output.txt", "w", stdout);
	#endif
	clock_t clk = clock();
	cerr << "I will return...\n";

	int n; cin >>  n;

	rep(i, 0, n - 1) {
		int u, v; cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	rep(i, 1, n + 1) cin >> a[i];

	pii p = dfs(1, 0);
	cerr << p.fi << ' ' << p.se << '\n';
	cout << p.fi + p.se << '\n';

	cerr << "...don't you ever hang your head.\n";
	cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << x << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define iceil(n, x) ((n + x - 1) / x)
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
			 tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
#undef pii
#define pii pair<ll, ll>
#define int ll
int a[N];
vi adj[N];

pii dfs(int i, int p) {
	pii pp = {0, 0};
	for (int j : adj[i]) {
//		cerr << i << " " << j << " p" << '\n';
		if(j == p) continue;
		pii q = dfs(j, i);
		pp.fi = max(pp.fi, q.fi);
		pp.se = max(pp.se, q.se);
	}
	a[i] += pp.fi;
	a[i] -= pp.se;
	if(a[i] > 0) pp.se += a[i];
	else 		 pp.fi += -a[i];

//	cerr << i << ' ' << pp.fi << ' ' << pp.se << '\n';

	return pp;
}

main()
{
	sync;
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\HP\\Desktop\\input.txt", "r", stdin);
//		freopen("C:\\Users\\HP\\Desktop\\output.txt", "w", stdout);
	#endif
	clock_t clk = clock();
	cerr << "I will return...\n";

	int n; cin >>  n;

	rep(i, 0, n - 1) {
		int u, v; cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	rep(i, 1, n + 1) cin >> a[i];

	pii p = dfs(1, 0);
	cerr << p.fi << ' ' << p.se << '\n';
	cout << p.fi + p.se << '\n';

	cerr << "...don't you ever hang your head.\n";
	cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
void dfs(lli i,lli p)
{
    vis[i]=1;
    if(a[i]!=a[1])
    f=1;
    if(v[i][0]==p&&v[i].size()==1)
    {
        if(f&&mk==0)
        mk=i;
        return;
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            dfs(v[i][j],i);
        }
    }
}
lli tot=0;
lli dfs1(lli i,lli cnt)
{
    vis[i]=1;
    if(i==1)
    {
        lli d=a[i];
        tot+=abs(cnt+d);
        return 1;
    }
    //cout<<i<<" "<<cnt<<endl;
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            lli d=a[v[i][j]];
            tot+=abs(cnt+d);
            cnt+=-1*(cnt+d);
            if(dfs1(v[i][j],cnt))
            return 1;
        }
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    map<ll,lli> jk;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        if(jk[{x,y}]==0)
        {
        v[x].push_back(y);
        v[y].push_back(x);
        jk[{y,x}]=jk[{x,y}]=1;
        }
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    vis.clear();
    //cout<<mk<<endl;
    tot+=abs(a[mk]);
    dfs1(mk,-1*a[mk]);
    cout<<tot;
}


/*
    NO
        TIME
            TO
                DIE
                    --007
                            */
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
#define mod 1000000007
#define OS //single input use oeis
lli INF=1e17;
lli eps=1e-8;
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcountll(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctzll(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clzll(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffsll(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
int fmx(int x, int y) { return (((y-x)>>(31))&(x^y))^y; }
int fmi(int x, int y) { return (((y-x)>>(31))&(x^y))^x; }
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,z,k,x,m;
const double pi=3.14159265358979323846;
lli add(lli a, lli b) {
 	lli res = a + b;

 	while (res >= mod) res -= mod;

 	while (res < 0) res += mod;

 	return res;
}
 lli me(lli a,lli b)
{
    lli res=1;
    while(b)
    {
        if(b&1)
        res=(res%mod*a%mod)%mod;
        a=a*a;
        a%=mod;
        b=b>>1;
    }
    return res;
}
lli mI(lli a)
{
    return (me(a,mod-2)%mod+mod)%mod;
}
vector<lli> v[100005];
lli val[100005];
lli vis[100005];
ll dfs(lli i)
{
    vis[i]=1;
    lli inc,dec;
    inc=0;
    dec=0;
    for(auto &j: v[i])
    {
        if(!vis[j])
        {
            ll p=dfs(j);
            inc=max(inc,p.first);
            dec=max(dec,p.second);
        }
    }
    lli sum=val[i];
    sum+=inc;
    sum-=dec;
    // cout<<sum<<" "<<i<<" "<<inc<<" "<<dec<<"\n";
    if(sum>0)
    dec+=sum;
    else
    inc+=abs(sum);
    // cout<<sum<<" "<<i<<" "<<inc<<" "<<dec<<"\n";
    return {inc,dec};
}
int main()
{
 IO;
 OS;
 cin>>n;
 for(int i=1;i<n;i++)
 {
     lli x,y;
     cin>>x>>y;
     v[x].push_back(y);
     v[y].push_back(x);
 }

 for(int i=1;i<=n;i++)
 cin>>val[i];

 ll p=dfs(1);
 p.first+=p.second;

 cout<<p.first;
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
lli dp[1000005][3];
lli tot=0;
lli plu,minu;
lli dfs(lli i,lli p)
{
    vis[i]=1;
    if(v[i].size()==1&&v[i][0]==p)
    {
        tot+=abs(a[i]);
        return a[i];
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            lli d=dfs(v[i][j],i);
            if(d>0)
            dp[i][1]=max(dp[i][1],d);
            else
            dp[i][0]=max(dp[i][0],abs(d));
        }
    }
    plu+=dp[i][1];
    minu+=dp[i][0];
    dp[i][2]=-plu+minu+a[i];
    //cout<<dp[i][2]<<" "<<i<<" "<<tot<<endl;
    tot+=abs(dp[i][2]);
    return dp[i][2];
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    mem2(dp,0);
    map<ll,lli> jk;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        if(jk[{x,y}]==0)
        {
        v[x].push_back(y);
        v[y].push_back(x);
        jk[{y,x}]=jk[{x,y}]=1;
        }
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    cout<<tot;
    //cout<<tot;
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
void dfs(lli i,lli p)
{
    vis[i]=1;
    if(a[i]!=a[1])
    f=1;
    if(v[i][0]==p)
    {
        if(f&&mk==0)
        mk=i;
        return;
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            dfs(v[i][j],i);
        }
    }
}
lli tot=0;
lli dfs1(lli i,lli cnt)
{
    vis[i]=1;
    if(i==1)
    {
        lli d=a[i];
        tot+=abs(cnt+d);
        return 1;
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            lli d=a[v[i][j]];
            tot+=abs(cnt+d);
            cnt=-1*(cnt+d);
            if(dfs1(v[i][j],cnt))
            return 1;
        }
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    vis.clear();
    dfs1(mk,-1*a[mk]);
    cout<<tot;
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
lli dp[1000005][3];
lli tot=0;
lli plu,minu;
lli dfs(lli i,lli p)
{
    vis[i]=1;
    if(v[i].size()==1&&v[i][0]==p)
    {
        tot+=abs(a[i]);
        if(a[i]>0)
        plu+=a[i];
        else
        minu+=abs(a[i]);
        return a[i];
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            lli d=dfs(v[i][j],i);
            if(d>0)
            dp[i][1]=max(dp[i][1],d);
            else
            dp[i][0]=max(dp[i][0],abs(d));
        }
    }
    dp[i][2]=-plu+minu+a[i];
    if(dp[i][2]>0)
    plu+=dp[i][2];
    else
    minu+=abs(dp[i][2]);
    //cout<<dp[i][2]<<" "<<i<<" "<<tot<<endl;
    tot+=abs(dp[i][2]);
    return dp[i][2];
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    mem2(dp,0);
    map<ll,lli> jk;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        if(jk[{x,y}]==0)
        {
        v[x].push_back(y);
        v[y].push_back(x);
        jk[{y,x}]=jk[{x,y}]=1;
        }
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    cout<<tot;
    //cout<<tot;
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
lli dp[1000005][3];
lli tot=0;
lli dfs(lli i,lli p)
{
    vis[i]=1;
    if(v[i].size()==1&&v[i][0]==p)
    {
        tot+=abs(a[i]);
        return a[i];
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            lli d=dfs(v[i][j],i);
            if(d>0)
            dp[i][1]=max(dp[i][1],d);
            else
            dp[i][0]=max(dp[i][0],d);
        }
    }
    dp[i][2]=-dp[i][1]-dp[i][0]+a[i];
    tot+=abs(dp[i][2]);
    return dp[i][2];
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    mem2(dp,-1e15);
    map<ll,lli> jk;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        if(jk[{x,y}]==0)
        {
        v[x].push_back(y);
        v[y].push_back(x);
        jk[{y,x}]=jk[{x,y}]=1;
        }
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    cout<<tot;
    //cout<<tot;
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
lli dp[1000005][3];
lli tot=0;
lli plu,minu;
void dfs(lli i,lli p)
{
    vis[i]=1;
    lli mu,mi;
    mu=mi=0;
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            dfs(v[i][j],i);
            //cout<<v[i][j]<<" "<<i<<" "<<dp[v[i][j]][0]<<" "<<dp[v[i][j]][1]<<endl;
            mu=max(mu,dp[v[i][j]][0]);
            mi=max(mi,dp[v[i][j]][1]);
        }
    }
    //cout<<mu<<" "<<mi<<" "<<i<<" ";
    //cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
    dp[i][2]=-mu+mi+a[i];
    if(dp[i][2]>0)
    {
        dp[i][0]=dp[i][2]+mu;
        dp[i][1]=mi;
    }
    else
    {
        dp[i][0]=mu;
        dp[i][1]=abs(dp[i][2])+mi;
    }
   // cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<i<<" "<<mu<<" "<<mi<<" "<<dp[i][2]<<" "<<a[i]<<endl;
    //cout<<dp[i][2]<<" "<<i<<" "<<tot<<" "<<dp[i][0]<<" "<<dp[i][1]<<endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    mem2(dp,0);
    map<ll,lli> jk;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        if(jk[{x,y}]==0)
        {
        v[x].push_back(y);
        v[y].push_back(x);
        jk[{y,x}]=jk[{x,y}]=1;
        }
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    cout<<dp[1][0]+dp[1][1];
    //cout<<tot;
}


/*
    If u can prepare to learn and adapt
                                       you can transform*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/functional/hash.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
lli M=1e9+7;
lli INF=1e17;
#define sqr(x) (x) * (x)
#define bip(n) __builtin_popcount(n)//no of ones bit in binary!!
#define bictz(n) __builtin_ctz(n)//no of trailing zeroes in binary!!
#define biclz(n) __builtin_clz(n)//no of leading zeroes in binary!!
#define bffs(n) __builtin_ffs(n)//index of first one bit!!
typedef pair<lli,lli> ll;
#define mem1(a,x) fill(&a[0], &a[0] + sizeof(a) / sizeof(a[0]), x)
#define mem2(a,x) fill(&a[0][0], &a[0][0] +sizeof(a)/sizeof(a[0][0]),x)
#define mem3(a,x) fill(&a[0][0][0], &a[0][0][0] +sizeof(a)/sizeof(a[0][0][0]),x)
#define fi1 ifstream fin("input.txt")
#define of1 ofstream fout("output.txt")
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ost;
lli n,q,z,x,y,k,m;
const double pi1=3.14159265358979323846;
//unordered_map<pair<ll,lli>,lli> mp;
lli me(lli a,lli b,lli M)
{
    if(b==0)
    return 1;
    else if(b%2==0)
    return me((a*a)%M,b/2,M);
    else
    return (a%M*me((a*a)%M,(b-1)/2,M)%M)%M;
}
lli mI(lli a,lli m)
{
    return me(a,m-2,m);
}
//lli tot=0;
vector<int> pf(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}
//map<ll,lli> dp;
vector<lli> v[1000005];
lli a[1000005];
map<lli,lli> vis;
lli f=0;
lli mk=0;
void dfs(lli i,lli p)
{
    vis[i]=1;
    if(a[i]!=a[1])
    f=1;
    if(v[i][0]==p&&v[i].size()==1)
    {
        if(f&&mk==0)
        mk=i;
        return;
    }
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            dfs(v[i][j],i);
        }
    }
}
lli tot=0;
lli dfs1(lli i,lli cnt)
{
    vis[i]=1;
    if(i==1)
    {
        lli d=a[i];
        tot+=abs(cnt+d);
        return 1;
    }
    //cout<<i<<" "<<cnt<<endl;
    for(int j=0;j<v[i].size();j++)
    {
        if(vis[v[i][j]]==0)
        {
            lli d=a[v[i][j]];
            tot+=abs(cnt+d);
            cnt+=-1*(cnt+d);
            if(dfs1(v[i][j],cnt))
            return 1;
        }
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
    cin>>a[i];
    dfs(1,-1);
    vis.clear();
    //cout<<mk<<endl;
    tot+=abs(a[mk]);
    dfs1(mk,-1*a[mk]);
    cout<<tot;
}


//{

#include <bits/stdc++.h>

#define all(x) x.begin (), x.end ()
#define rall(x) x.rbegin (), x.rend ()

#define LOCAL

using namespace std;

using lli = long long;
using ld = long double;

mt19937 rnd (time (nullptr));

//}

lli n = 0;
vector <vector <lli>> data;
vector <lli> values;

pair <lli, lli> DFS (lli v, lli p)
{
    pair <lli, lli> result (0, 0);
    if (values[v] > 0)
        result.first = values[v];
    else if (values[v] < 0)
        result.second = abs (values[v]);
    if ((data[v].size () == 1) && (p != -1))
        return result;
    result = { 0, 0 };
    for (lli u : data[v])
    {
        if (u != p)
        {
            pair <lli, lli> tmp = DFS (u, v);
            result.first = max (result.first, tmp.first);
            result.second = max (result.second, tmp.second);
        }
    }
    lli buffer = values[v] + result.first - result.second;
    if (buffer > 0)
        result.first += buffer;
    else if (buffer < 0)
        result.second += abs (buffer);
    return result;
}

inline void Solve ()
{
    cin >> n;
    data.resize (n);
    values.resize (n);
    for (lli i = 0; i < n - 1; i++)
    {
        lli a = 0, b = 0;
        cin >> a >> b;
        a--, b--;
        data[a].push_back (b);
        data[b].push_back (a);
    }
    for (auto &it : values)
        cin >> it;
    pair <lli, lli> result = DFS (0, -1);
    lli answer = result.first + result.second;
    cout << answer;
}

signed main ()
{
#ifndef LOCAL
    ios_base::sync_with_stdio (false);
    cin.tie (nullptr);
    cout.tie (nullptr);
#endif
    cout << fixed << setprecision (20);
    Solve ();
    return 0;
}


//{

#include <bits/stdc++.h>

#define all(x) x.begin (), x.end ()
#define rall(x) x.rbegin (), x.rend ()

#define LOCAL

using namespace std;

using lli = long long;
using ld = long double;

mt19937 rnd (time (nullptr));

//}

lli n = 0;
vector <vector <lli>> data;
vector <lli> values;

pair <lli, lli> DFS (lli v, lli p)
{
    pair <lli, lli> result (0, 0);
    for (lli u : data[v])
    {
        if (u != p)
        {
            pair <lli, lli> tmp = DFS (u, v);
            result.first = max (result.first, tmp.first);
            result.second = max (result.second, tmp.second);
        }
    }
    lli buffer = values[v] + result.first - result.second;
    if (buffer > 0)
        result.first += buffer;
    else if (buffer < 0)
        result.second += abs (buffer);
    return result;
}

inline void Solve ()
{
    cin >> n;
    data.resize (n);
    values.resize (n);
    for (lli i = 0; i < n - 1; i++)
    {
        lli a = 0, b = 0;
        cin >> a >> b;
        a--, b--;
        data[a].push_back (b);
        data[b].push_back (a);
    }
    for (auto &it : values)
        cin >> it;
    pair <lli, lli> result = DFS (0, -1);
    lli answer = result.first + result.second;
    cout << answer;
}

signed main ()
{
#ifndef LOCAL
    ios_base::sync_with_stdio (false);
    cin.tie (nullptr);
    cout.tie (nullptr);
#endif
    cout << fixed << setprecision (20);
    Solve ();
    return 0;
}


//{

#include <bits/stdc++.h>

#define all(x) x.begin (), x.end ()
#define rall(x) x.rbegin (), x.rend ()

#define LOCAL

using namespace std;

using lli = long long;
using ld = long double;

mt19937 rnd (time (nullptr));

//}

lli n = 0;
vector <vector <lli>> data;
vector <lli> values;

pair <lli, lli> DFS (lli v, lli p)
{
    pair <lli, lli> result (0, 0);
    if (values[v] > 0)
        result.first = values[v];
    else if (values[v] < 0)
        result.second = abs (values[v]);
    if ((data[v].size () == 1) && (p != -1))
        return result;
    result = { 0, 0 };
    for (lli u : data[v])
    {
        if (u != p)
        {
            pair <lli, lli> tmp = DFS (u, v);
            result.first = max (result.first, tmp.first);
            result.second = max (result.second, tmp.second);
        }
    }
    return result;
}

inline void Solve ()
{
    cin >> n;
    data.resize (n);
    values.resize (n);
    for (lli i = 0; i < n - 1; i++)
    {
        lli a = 0, b = 0;
        cin >> a >> b;
        a--, b--;
        data[a].push_back (b);
        data[b].push_back (a);
    }
    for (auto &it : values)
        cin >> it;
    pair <lli, lli> result = DFS (0, -1);
    lli answer = result.first + result.second + abs (values[0] + result.first - result.second);
    cout << answer;
}

signed main ()
{
#ifndef LOCAL
    ios_base::sync_with_stdio (false);
    cin.tie (nullptr);
    cout.tie (nullptr);
#endif
    cout << fixed << setprecision (20);
    Solve ();
    return 0;
}


//{

#include <bits/stdc++.h>

#define all(x) x.begin (), x.end ()
#define rall(x) x.rbegin (), x.rend ()

#define LOCAL

using namespace std;

using lli = long long;
using ld = long double;

mt19937 rnd (time (nullptr));

//}

lli n = 0;
vector <vector <lli>> data;
vector <lli> values;

pair <lli, lli> DFS (lli v, lli p)
{
    pair <lli, lli> result (0, 0);
    for (lli u : data[v])
    {
        if (u != p)
        {
            pair <lli, lli> tmp = DFS (u, v);
            result.first = max (result.first, tmp.first);
            result.second = max (result.second, tmp.second);
        }
    }
    lli buffer = values[v] - result.first + result.second;
    if (buffer > 0)
        result.first += buffer;
    else if (buffer < 0)
        result.second -= buffer;
    return result;
}

inline void Solve ()
{
    cin >> n;
    data.resize (n);
    values.resize (n);
    for (lli i = 0; i < n - 1; i++)
    {
        lli a = 0, b = 0;
        cin >> a >> b;
        a--, b--;
        data[a].push_back (b);
        data[b].push_back (a);
    }
    for (auto &it : values)
        cin >> it;
    pair <lli, lli> result = DFS (0, -1);
    lli answer = result.first + result.second;
    cout << answer;
}

signed main ()
{
#ifndef LOCAL
    ios_base::sync_with_stdio (false);
    cin.tie (nullptr);
    cout.tie (nullptr);
#endif
    cout << fixed << setprecision (20);
    Solve ();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
ll ans = 0;
vb vis;

int solve(int root){
	vis[root] = true;
	int maxv=0,minv = 0;
	for(int i=0;i<graph[root].size();i++){
		int x = graph[root][i];
		if(!vis[x]){
			int k = solve(x);
			maxv = max(maxv,k);
			minv = min(minv,k);
		}
	}
	int val = maxv-minv;
	ans += val;
	if(root==0) ans += abs(v[0] - val);
	return v[root];
}

int main(){
	int n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(int i=0;i<n;i++) cin>>v[i];
	solve(0);
	cout<<ans<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
vb vis;

ii solve(ll root){
	vis[root] = true;

	ll inc = 0,dec = 0;
	for(ll i=0;i<graph[root].size();i++){
		ll x = graph[root][i];
		if(!vis[x]){
			ii k = solve(x);
			inc = max(inc,k.first);
			dec = max(dec,k.second);
		}
	}

	ll k = v[root] + inc - dec;
	if(k<=0) inc += (-k);
	else dec += k;
	return make_pair(inc,dec);
}

int main(){
	ll n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(ll i=0;i<n;i++) cin>>v[i];
	ii k = solve(0);
	cout<<k.first+k.second<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

vvi graph;
vi v;
vb vis;

ii solve(ll root){
	vis[root] = true;

	ll inc = 0,dec = 0;
	for(ll i=0;i<graph[root].size();i++){
		ll x = graph[root][i];
		if(!vis[x]){
			ii k = solve(x);
			inc = max(inc,k.first);
			dec = max(dec,k.second);
		}
	}

	ll k = v[root] + inc - dec;
	if(k<=0) inc += (-k);
	else dec += k;
	return make_pair(inc,dec);
}
int main(){
	ll n;
	cin>>n;
	graph.resize(n);v.resize(n);vis.resize(n,0);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		cin>>a>>b;
		a--;b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for(ll i=0;i<n;i++) cin>>v[i];
	ii k = solve(0);
	cout<<k.first+k.second<<endl;

}