stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



long long MOD = 1000000007;

long long prod(long long a, long long b){

    return (a*b)%MOD;

}



int main(){

    int n, m;

    cin >> n >> m;

    long long vert[n], hori[m];

    for(int one = 0; one < n; one++){

        cin >> vert[one];

    }

    for(int one = 0; one < m; one++){

        cin >> hori[one];

    }



    long long xaxis = 0, yaxis = 0, temp = 0, temp2 = 0, ans = 0;

    for(int one = 1; one < n; one++){

        temp = prod(one, n-one);

        temp2 = vert[one] - vert[one-1];

        temp = prod(temp, temp2);

        xaxis = (xaxis+temp)%MOD;

    }

    for(int one = 1; one < m; one++){

        temp = prod(one, m-one);

        temp2 = hori[one] - hori[one-1];

        temp = prod(temp, temp2);

        yaxis = (yaxis+temp)%MOD;

    }

    ans = prod(xaxis, yaxis);

    cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 5.236279364819514x):
#include       <set>

#include       <map>

#include     <queue>

#include     <cmath>

#include    <cstdio>

#include    <cctype>

#include    <vector>

#include   <cstring>

#include <algorithm>

using namespace std;



typedef long long ll;

const ll mod = 1e9+7;

const double eps = 1e-7;

const int maxn = 1e5+5;



ll fast_pow(ll a,ll b,ll mod){

    ll ans = 1;

    while(b){

        if(b&1)

            ans = ans*a%mod;

        a = a*a%mod;

        b>>=1;

    }

    return ans;

}



int main(){

    ll n, m;

    ll a, x=0, y=0;

    scanf("%lld%lld",&n,&m);



    for(int i=1;i<=n;i++){

        scanf("%lld",&a);

        x = (x+(2*i-n-1)*a)%mod;

    }

    for(int i=1;i<=m;i++){

        scanf("%lld",&a);

        y = (y+(2*i-m-1)*a)%mod;

    }

    printf("%lld\n",x*y%mod);

    return 0;

}



/*



1592794101

989097785





232162543



*/
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define mod 1000000007

using namespace std;

ll x[100000],y[100000];

int main()

{

    ll i,j,k;

    ll n,m;

    ll conx=(ll)0,cony=(ll)0;

    scanf("%lld %lld",&n,&m);

    for(i=0;i<n;i++)

        scanf("%lld",&x[i]);

    for(i=0;i<m;i++)

        scanf("%lld",&y[i]);

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

        conx=(conx+((x[i+1]-x[i])*((i+1)*(n-i-1))%mod)%mod)%mod;

    }

    //prllf("%d %d\n",conx,cony);

    for(i=0;i<m-1;i++){

        cony=(cony+((y[i+1]-y[i])*((i+1)*(m-i-1))%mod)%mod)%mod;

    }

    printf("%lld",(conx*cony)%mod);

}


// Faster C++ solution for the same problem (measured speedup: 5.166797729159602x):
#include       <set>

#include       <map>

#include     <queue>

#include     <cmath>

#include    <cstdio>

#include    <cctype>

#include    <vector>

#include   <cstring>

#include <algorithm>

using namespace std;



typedef long long ll;

const ll mod = 1e9+7;

const double eps = 1e-7;

const int maxn = 1e5+5;



ll fast_pow(ll a,ll b,ll mod){

    ll ans = 1;

    while(b){

        if(b&1)

            ans = ans*a%mod;

        a = a*a%mod;

        b>>=1;

    }

    return ans;

}



int main(){

    ll n, m;

    ll a, x=0, y=0;

    scanf("%lld%lld",&n,&m);



    for(int i=1;i<=n;i++){

        scanf("%lld",&a);

        x = (x+(2*i-n-1)*a)%mod;

    }

    for(int i=1;i<=m;i++){

        scanf("%lld",&a);

        y = (y+(2*i-m-1)*a)%mod;

    }

    printf("%lld\n",x*y%mod);

    return 0;

}



/*



1592794101

989097785





232162543



*/
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define pi acos(0.0)

#define ll long long

#define f first

#define s second

#define gap ' '

#define pb push_back

#define mod 1000000007

int main()

{

    ll n,m;

    cin>>n>>m;

    ll x[n+5],y[m+5];

    for(int i=0;i<n;i++)

    {

        cin>>x[i];

    }

    for(int i=0;i<m;i++)

    {

        cin>>y[i];

    }

    sort(x,x+n);

    sort(y,y+m);

    ll xx=0;

    ll yy=0;

    for(int i=0;i<n;i++)

    {

        xx+=(i-(n-1))*x[i]+i*x[i];

        //cout<<xx<<endl;

    }



    for(int i=0;i<m;i++)

    {

        yy+=(i-(m-1))*y[i]+i*y[i];

    }



    ll ans=((xx%mod)*(yy%mod))%mod;

    cout<<ans;



}


// Faster C++ solution for the same problem (measured speedup: 5.219587167763889x):
#include       <set>

#include       <map>

#include     <queue>

#include     <cmath>

#include    <cstdio>

#include    <cctype>

#include    <vector>

#include   <cstring>

#include <algorithm>

using namespace std;



typedef long long ll;

const ll mod = 1e9+7;

const double eps = 1e-7;

const int maxn = 1e5+5;



ll fast_pow(ll a,ll b,ll mod){

    ll ans = 1;

    while(b){

        if(b&1)

            ans = ans*a%mod;

        a = a*a%mod;

        b>>=1;

    }

    return ans;

}



int main(){

    ll n, m;

    ll a, x=0, y=0;

    scanf("%lld%lld",&n,&m);



    for(int i=1;i<=n;i++){

        scanf("%lld",&a);

        x = (x+(2*i-n-1)*a)%mod;

    }

    for(int i=1;i<=m;i++){

        scanf("%lld",&a);

        y = (y+(2*i-m-1)*a)%mod;

    }

    printf("%lld\n",x*y%mod);

    return 0;

}



/*



1592794101

989097785





232162543



*/
// Slower C++ solution:
#include<iostream>

using namespace std;

typedef long long LL;

#define F(i) for(int i=0;i<n;i++)

int main(){

	int n;

	cin>>n;

	int r=0;

	for(int i=1;i<=n;i++){

		int c=0;

		for(int j=1;j<=i;j++)if(i%j==0)c++;

		if(i%2==1&&c==8)r++;

	}

	cout<<r;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.429249452178568x):
#include<cstdio>

#define N 100005

#define P pair<int,int>

#define int long long

inline int read(){

    int x=0;char ch=getchar();

    for(;ch<48||ch>57;ch=getchar());

    for(;ch>47&&ch<58;ch=getchar())

    x=(x<<1)+(x<<3)+(ch^48);return x;

}

using namespace std;

signed main()

{

    int n=read();

    if(n>=195)puts("5");else

    if(n>=189)puts("4");else

    if(n>=165)puts("3");else

    if(n>=135)puts("2");else

    if(n>=105)puts("1");else

    puts("0");

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main(){

	int n;

	cin >> n;

	int ans = 0;

	for (int i = 1; i <= n; i += 2){

		int cnt = 0;

		for (int j = 1; j <= i; j++){

			if (i % j == 0)

				cnt++;

		}

		if (cnt == 8)

			ans++;

	}

	cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.24441425317534x):
#include<stdio.h>

int main(){

    int c=0,n,l=0,i,j;

    scanf("%d",&n);

    for(i=105;i<=n;i+=2){

        l=0;

        for(j=3;j<i;j++){

            if(i%j==0) l++;

        }

        if(l==6) c++;

    }

    printf("%d\n",c);

    return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;

long long a,b=0,c=0,d,e[1000001],f[1000001];

int main(){

	cin>>a;

	for(long long i=1;i<=a;i++){

		b=0;

		for(long long j=1;j<=i;j++){

			if(i%j==0&&i%2!=0){					

				b++;

			}

		}

		if(b==8){

			c++;

		}

	}

	cout<<c<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.242123435601913x):
#include<stdio.h>

int main(){

    int c=0,n,l=0,i,j;

    scanf("%d",&n);

    for(i=105;i<=n;i+=2){

        l=0;

        for(j=3;j<i;j++){

            if(i%j==0) l++;

        }

        if(l==6) c++;

    }

    printf("%d\n",c);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main(){

	int n;

	cin >> n;

	int ans = 0;

	for (int i = 1; i <= n; i += 2){

		int cnt = 0;

		for (int j = 1; j <= i; j++){

			if (i % j == 0)

				cnt++;

		}

		if (cnt == 8)

			ans++;

	}

	cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.179318853154157x):
#include<stdio.h>

int main(){

	int a,b=0,c=0,n,j,i;

	scanf("%d",&n);

	for(j=1;j<=n;j+=2)

	{for(i=1;i<=j;i++)

	{

		if(j%i==0)

		{b++;}

		

		

		

	}

	if(b==8)

	{c++;}

    b=0;

}

	



printf("%d\n",c);

return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;

long long a,b=0,c=0,d,e[1000001],f[1000001];

int main(){

	cin>>a;

	for(long long i=1;i<=a;i++){

		b=0;

		for(long long j=1;j<=i;j++){

			if(i%j==0&&i%2!=0){					

				b++;

			}

		}

		if(b==8){

			c++;

		}

	}

	cout<<c<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.1770564699642705x):
#include<stdio.h>

int main(){

	int a,b=0,c=0,n,j,i;

	scanf("%d",&n);

	for(j=1;j<=n;j+=2)

	{for(i=1;i<=j;i++)

	{

		if(j%i==0)

		{b++;}

		

		

		

	}

	if(b==8)

	{c++;}

    b=0;

}

	



printf("%d\n",c);

return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <iostream>

using namespace std;



int cnt(int x) {

    int c = 0;

    for (int i = 1; i <= x; i++) {

        if (x % i == 0) {

            c++;

        }

    }

    return c;

}



int main() {

    int n;

    cin >> n;



    int c = 0;

    for (int i = 1; i <= n; i+=2) {

        if (cnt(i) == 8) {

            c++;

        }

    }



    cout << c << endl;

}




// Faster C++ solution for the same problem (measured speedup: 5.178498312337196x):
#include <stdio.h>



int main() {

    int n;

    scanf("%d", &n);



    int c = 0;

    for (int i = 1; i <= n; i+=2) {

        int y = 0;

        for (int j = 1; j <= i; j++) {

            if (i % j == 0) { 

                y++;

            }

        }

        if (y== 8) {

            c++;

        }

    }



    printf("%d\n", c);

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <iostream>

using namespace std;



int main() {

    int n;

    cin >> n;



    int c = 0;

    for (int i = 1; i <= n; i+=2) {

        int y = 0;

        for (int j = 1; j <= i; j++) {

            if (i % j == 0) { 

                y++;

            }

        }

        if (y== 8) {

            c++;

        }

    }



    cout << c << endl;

}




// Faster C++ solution for the same problem (measured speedup: 5.179318853154157x):
#include <stdio.h>



int main() {

    int n;

    scanf("%d", &n);



    int c = 0;

    for (int i = 1; i <= n; i+=2) {

        int y = 0;

        for (int j = 1; j <= i; j++) {

            if (i % j == 0) { 

                y++;

            }

        }

        if (y== 8) {

            c++;

        }

    }



    printf("%d\n", c);

}
// Slower C++ solution:
#include <iostream>

using namespace std;



int main() {

    int n;

    cin >> n;



    int c = 0;

    for (int i = 1; i <= n; i+=2) {

        int y = 0;

        for (int j = 1; j <= i; j++) {

            if (i % j == 0) { 

                y++;

            }

        }

        if (y== 8) {

            c++;

        }

    }



    cout << c << endl;

}




// Faster C++ solution for the same problem (measured speedup: 5.179318853154157x):
#include <stdio.h>



int main() {

    int n;

    scanf("%d", &n);



    int c = 0;

    for (int i = 1; i <= n; i+=2) {

        int y = 0;

        for (int j = 1; j <= i; j++) {

            if (i % j == 0) { 

                y++;

            }

        }

        if (y== 8) {

            c++;

        }

    }



    printf("%d\n", c);

}
// Slower C++ solution:
//

//  AAAAAB.cpp

//  my_programs

//

//  Created by Jiatuo Zou on 7/22/20.

//  Copyright © 2020 Jiatuo Zou. All rights reserved.

//



#include <iostream>



int main()

{

    using namespace std;

    

    int n, m, k, f = 0, books = 0, ans = 0, f2 = 0;

    long long int ab[2][200000], prefixs_sum[2][200001] = {{0}};

    cin >> n >> m >> k;

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

        cin >> ab[0][i];

        prefixs_sum[0][i + 1] += (ab[0][i] + prefixs_sum[0][i]);

        if(prefixs_sum[0][i + 1] > k && f2 == 0){

            f = i;

            f2 = 1;

        }

    }

    if(prefixs_sum[0][n] <= k){

        f = n;

    }

    books = f;

    ans = f;

    for(int i = 0; i < m; i++){

        cin >> ab[1][i];

        prefixs_sum[1][i + 1] += (ab[1][i] + prefixs_sum[1][i]);

    }

    for(int i = f, j = 1; j <= m; ){

        if(prefixs_sum[1][j] > k){

            break;

        }

        if(k - prefixs_sum[0][i] >= prefixs_sum[1][j]){

            j++;

            books++;

        } else if(i > 0 && k - prefixs_sum[0][i] < prefixs_sum[1][j]){

            i--;

            books--;

        }

        ans = max(ans, books);

    }

    cout << ans << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.378578302520963x):
#include <bits/stdc++.h>

#define N 200005

using namespace std;

typedef long long ll;

ll n,m,k,ans,a[N],b[N],sa[N],sb[N];

int main(){

   	cin>>n>>m>>k;

    for(int i=1;i<=n;i++)cin>>a[i],sa[i]=a[i]+sa[i-1];

    for(int i=1;i<=m;i++)cin>>b[i],sb[i]=b[i]+sb[i-1];

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

        if(sa[i]>k)break;

        ll t=k-sa[i],pos=upper_bound(sb+1,sb+m+1,t)-sb;

        ans=max(ans,i+pos-1);

    }

   	cout<<ans<<endl;

    return 0;

}
// Slower C++ solution:
//

//  AAAAAB.cpp

//  my_programs

//

//  Created by Jiatuo Zou on 7/22/20.

//  Copyright © 2020 Jiatuo Zou. All rights reserved.

//



#include <iostream>



int main()

{

    using namespace std;

    

    int n, m, k, f = 0, books = 0, ans = 0, f2 = 0;

    long long int ab[2][200000], prefixs_sum[2][200001] = {{0}};

    cin >> n >> m >> k;

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

        cin >> ab[0][i];

        prefixs_sum[0][i + 1] += (ab[0][i] + prefixs_sum[0][i]);

        if(prefixs_sum[0][i + 1] > k && f2 == 0){

            f = i;

            f2 = 1;

        }

    }

    if(prefixs_sum[0][n] <= k){

        f = n;

    }

    books = f;

    ans = f;

    for(int i = 0; i < m; i++){

        cin >> ab[1][i];

        prefixs_sum[1][i + 1] += (ab[1][i] + prefixs_sum[1][i]);

    }

    for(int i = f, j = 1; j <= m; ){

        if(prefixs_sum[1][j] > k){

            break;

        }

        if(k - prefixs_sum[0][i] >= prefixs_sum[1][j]){

            j++;

            books++;

        } else if(i > 0 && k - prefixs_sum[0][i] < prefixs_sum[1][j]){

            i--;

            books--;

        }

        ans = max(ans, books);

    }

    cout << ans << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.38153326466851x):
#include <bits/stdc++.h>



using namespace std;



typedef long long ll;



const int N=2e5+50;

ll n,m,k,arr[N],brr[N];



int solve(ll val){

    int low=1,high=m,mid,x=0;

    while(low<=high){

        mid=low+(high-low)/2;

        if (brr[mid]<=val){

            x=mid;

            low=mid+1;

        }

        else

            high=mid-1;

    }

    return x;

}



int main()

{

    cin>>n>>m>>k;

    for (int i=1;i<=n;i++){

        cin>>arr[i];

        arr[i]+=arr[i-1];

    }

    for (int i=1;i<=m;i++){

        cin>>brr[i];

        brr[i]+=brr[i-1];

    }

    int ans=0;

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

        if (arr[i]>k)

            break;

        ans=max(ans,solve(k-arr[i])+i);

    }

    cout<<ans;



    return 0;

}
// Slower C++ solution:
//

//  AAAAAB.cpp

//  my_programs

//

//  Created by Jiatuo Zou on 7/22/20.

//  Copyright © 2020 Jiatuo Zou. All rights reserved.

//



#include <iostream>



int main()

{

    using namespace std;

    

    int n, m, k, f = 0, books = 0, ans = 0, f2 = 0;

    long long int ab[2][200000], prefixs_sum[2][200001] = {{0}};

    cin >> n >> m >> k;

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

        cin >> ab[0][i];

        prefixs_sum[0][i + 1] += (ab[0][i] + prefixs_sum[0][i]);

        if(prefixs_sum[0][i + 1] > k && f2 == 0){

            f = i;

            f2 = 1;

        }

    }

    if(prefixs_sum[0][n] <= k){

        f = n;

    }

    books = f;

    ans = f;

    for(int i = 0; i < m; i++){

        cin >> ab[1][i];

        prefixs_sum[1][i + 1] += (ab[1][i] + prefixs_sum[1][i]);

    }

    for(int i = f, j = 1; j <= m; ){

        if(prefixs_sum[1][j] > k){

            break;

        }

        if(k - prefixs_sum[0][i] >= prefixs_sum[1][j]){

            j++;

            books++;

        } else if(i > 0 && k - prefixs_sum[0][i] < prefixs_sum[1][j]){

            i--;

            books--;

        }

        ans = max(ans, books);

    }

    cout << ans << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.311238574467985x):
#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef pair<ll, ll> pll;

typedef pair<int, int> pii;



#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

#define read freopen("in.txt", "r", stdin)

#define write freopen("out.txt", "w", stdout)

#define mem(x, n) memset(x, n, sizeof(x))

#define all(x) x.begin(), x.end()

#define endl "\n"



int main() {

	faster;



	ll n, m, t, in, tot = 0, time = 0;

	cin >> n >> m >> t;



	vector<ll> a(n + 1), b(m + 1);

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

		cin >> a[i];

	}

	for (int i = 0; i < m; i++) {

		cin >> b[i];

	}

	int i, j;

	for (i = 0; i < n; i++) {

		if (a[i] + time <= t) {

			time += a[i];

			tot++;

		} else {

			break;

		}

	}

	i--;



	ll temp = tot;



	for (j = 0; j < m; j++) {

		time += b[j];

		temp++;

		while (i >= 0 && time > t) {

			time -= a[i];

			i--;

			temp--;

		}



		if (time <= t) {

			tot = max(tot, temp);

		} else {

			break;

		}

	}



	cout << tot << endl;

	return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;



const long long MAXN = 2e5 + 1;

long long n, m, i, j, k, z, a[MAXN], b[MAXN];



int main()

{

	ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

    cin >> n >> m >> k;

	for (i = 1; i <= n; ++i)

		cin >> a[i], a[i] += a[i - 1];

	for (i = 1; i <= m; ++i)

		cin >> b[i], b[i] += b[i - 1];

	z = 0, i = 0, j = m;

	while (a[i] <= k && i <= n)

	{

		while (b[j] > k - a[i])

			--j;

		z = max(z, i + j);

		++i;

	}

	cout << z << '\n';

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.284113584817701x):
#include <stdio.h>

using namespace std;

const int MAXN = 2e5 + 1;

int n, m, i, j, k, z;

long a[MAXN], b[MAXN];

signed main()

{

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

	for (i = 1; i <= n; ++i)

		scanf("%d", &a[i]), a[i] += a[i - 1];

	for (i = 1; i <= m; ++i)

		scanf("%d", &b[i]), b[i] += b[i - 1];

	z = 0, i = 0, j = m;

	while (a[i] <= k && i <= n)

	{

		while (b[j] > k - a[i])

			--j;

		if (i + j > z)

			z = i + j;

		++i;

	}

	printf("%d", z);

}
// Slower C++ solution:
#include <iostream>

using namespace std;

const int MAXN = 2e5 + 1;

int n, m, i, j, k, z;

long long a[MAXN], b[MAXN];

int main()

{

	ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

    cin >> n >> m >> k;

	for (i = 1; i <= n; ++i)

		cin >> a[i], a[i] += a[i - 1];

	for (i = 1; i <= m; ++i)

		cin >> b[i], b[i] += b[i - 1];

	z = 0, i = 0, j = m;

	while (a[i] <= k && i <= n)

	{

		while (b[j] > k - a[i])

			--j;

		z = max(z, i + j);

		++i;

	}

	cout << z;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.2901923473430275x):
#include <stdio.h>

using namespace std;

const int MAXN = 2e5 + 1;

int n, m, i, j, k, z;

long a[MAXN], b[MAXN];

signed main()

{

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

	for (i = 1; i <= n; ++i)

		scanf("%d", &a[i]), a[i] += a[i - 1];

	for (i = 1; i <= m; ++i)

		scanf("%d", &b[i]), b[i] += b[i - 1];

	z = 0, i = 0, j = m;

	while (a[i] <= k && i <= n)

	{

		while (b[j] > k - a[i])

			--j;

		if (i + j > z)

			z = i + j;

		++i;

	}

	printf("%d", z);

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<vector>

#include<cstring>



using namespace std;



int n,x,y,siz[200001],fa[200001],maxs[200001],maxp[200001],mins[200001],minp[200001],maxl[200001],minn;

long long ans;

vector<int> point[200001];

char str[200001];



void dfs1(int u,bool check){

	siz[u]=str[u]-'0';

	for(int v:point[u])if(v!=fa[u]){

		fa[v]=u;

		dfs1(v,true);

		siz[u]+=siz[v],maxs[u]=max(maxs[u],maxs[v]+1);

		if(mins[v]<0x7f7f7f7f)mins[u]=min(mins[u],maxs[v]+1);

	}

	if(point[u].size()==check)maxs[u]=0,mins[u]=str[u]-'0'?0:0x7f7f7f7f;else if(str[u]-'0')mins[u]=min(mins[u],maxs[u]);

}



void dfs2(int u,bool check){

	maxp[u]=max(maxp[u],check+maxp[fa[u]]);

	if(check)maxl[u]=max(maxl[u],maxp[u]-1);

	int mx=-1;for(int v:point[u])if(v!=fa[u])maxp[v]=max(maxp[v],mx+2),mx=max(mx,maxs[v]);

	mx=-1;for(int i=point[u].size()-1;~i;--i){int v=point[u][i];if(v!=fa[u])maxp[v]=max(maxp[v],mx+2),mx=max(mx,maxs[v]);}

	for(int v:point[u])if(v!=fa[u])dfs2(v,true);

}



int main(){

	memset(mins,0x7f,sizeof(mins)),memset(minp,0x7f,sizeof(minp));

	scanf("%d",&n);for(int i=1;i<n;++i)scanf("%d%d",&x,&y),point[x].push_back(y),point[y].push_back(x);scanf("%s",str+1);

	dfs1(1,false),dfs2(1,false);

	for(int i=1;i<=n;++i){

		if(str[i]-'0')minn=0;else minn=min(mins[i],siz[i]==siz[1]?2147483647:maxp[i]);

		int d=max(maxs[i],maxp[i])-1;for(int v:point[i])if(v!=fa[i])d=min(d,maxl[v]+1);else d=min(d,maxs[i]+1);

		if(d>=minn)ans+=d-minn+1;

	}

	printf("%lld",ans+1);

}

// Faster C++ solution for the same problem (measured speedup: 8.39403129006298x):
#include<iostream>

#include<cstdio>

using namespace std;



int d1[200005],d2[200005];

int d3[200005],d4[200005];

int n,x,y,siz[200005],col[200005],fa[200005];

long long ans=0;

int tot=0,h[200005];

struct Edge{

	int x,next;

}e[400005];

char ch[200005];



inline void add_edge(int x,int y){

	e[++tot].x=y;

	e[tot].next=h[x],h[x]=tot;

}



void dfs1(int x,int father){

	siz[x]=col[x],fa[x]=father;

	d3[x]=col[x]?0:1e9;

	for(int i=h[x];i;i=e[i].next){

		if(e[i].x==father)continue;

		dfs1(e[i].x,x);

		siz[x]+=siz[e[i].x];

		d1[x]=max(d1[x],d1[e[i].x]+1);

		if(siz[e[i].x])

			d3[x]=min(d3[x],d1[e[i].x]+1);

	}

}



void dfs2(int x,int father){

	if(father)d4[x]=d2[x]-1;

	int maxn=0,sec=0;

	for(int i=h[x];i;i=e[i].next){

		if(e[i].x==father)continue;

		if(d1[e[i].x]+1>maxn)

			sec=maxn,maxn=d1[e[i].x]+1;

		else sec=max(sec,d1[e[i].x]+1);

	}

	for(int i=h[x];i;i=e[i].next){

		if(e[i].x==father)continue;

		if(d1[e[i].x]+1==maxn)

			d2[e[i].x]=max(d2[x],sec)+1;

		else d2[e[i].x]=max(d2[x],maxn)+1;

		dfs2(e[i].x,x);

	}

}



int main(){

	scanf("%d",&n);

	for(int i=1;i<n;i++){

		scanf("%d%d",&x,&y);

		add_edge(x,y);

		add_edge(y,x);

	}

	scanf("%s",ch+1);

	for(int i=1;ch[i];i++)

		col[i]=ch[i]-'0';

	dfs1(1,0),dfs2(1,0);

	for(int i=1;i<=n;i++){

		int minv,maxv;

		minv=min(d3[i],siz[1]==siz[i]?(int)1e9:d2[i]);

		maxv=max(d1[i],d2[i])-1;

		for(int j=h[i];j;j=e[j].next){

			if(e[j].x==fa[i])maxv=min(maxv,d1[i]+1);

			else maxv=min(maxv,d4[e[j].x]+1);

		}

		if(maxv>=minv)ans+=1LL*maxv-minv+1;

	}

	printf("%lld",ans+1);

}