#include <bits/stdc++.h>

#ifdef _WIN32
#define getchar_unlocked getchar
#endif
#define II (read32())
#define IU (readu32())
#define IL (read64())
#define IUL (readu64())
uint32_t readu32() { int c; while((c = getchar_unlocked()) < '0'); uint32_t n = c & 15U; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return n; } uint64_t readu64() { int c; while((c = getchar_unlocked()) < '0'); uint64_t n = c & 15U; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return n; } int32_t read32() { int c; while((c = getchar_unlocked()) < '-'); bool neg = c == '-'; if(neg) c = getchar_unlocked(); uint32_t n = c & 15U; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int32_t)n : n; } int64_t read64() { int c; while((c = getchar_unlocked()) < '-'); bool neg = c == '-'; if(neg) c = getchar_unlocked(); uint64_t n = c & 15; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int64_t)n : n; }

using namespace std;

struct ic {
int i;
int64_t c;
};

int64_t co[200008];
int main()
{
    ios_base::sync_with_stdio(0);

    int n=IU;
    int64_t k = IU;
    for(int i=0; i<=n; i++) co[i] = II;
    vector<ic> i1;
    int64_t s = 0;
    for(int i=0; i<=n; i++) {
        s += co[i];
        if(s & 1)
            i1.push_back({i, s % 2});
        s /= 2;
    }
    if(s) {
        i1.push_back({n+1, s});
    }
    if(i1.back().i - i1[0].i > 31) {
        cout<<0;
        return 0;
    }
    int64_t q = 0;
    for(ic x: i1) {
        q += x.c << (x.i - i1[0].i);
    }
    int a = 0;
    for(int i = i1[0].i; i >= 0; i--) {
        if(abs(q) > 2*k)
            break;
        if(i != n + 1) {
            int64_t x = co[i] - q;
            if(x && abs(x) <= k)
                a++;
        }
        q *= 2;
    }
    cout<<a;

    return 0;
}


#include <bits/stdc++.h>

#ifdef _WIN32
#define getchar_unlocked getchar
#endif
#define II (read32())
#define IU (readu32())
#define IL (read64())
#define IUL (readu64())
uint32_t readu32() { int c; while((c = getchar_unlocked()) < '0'); uint32_t n = c & 15U; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return n; } uint64_t readu64() { int c; while((c = getchar_unlocked()) < '0'); uint64_t n = c & 15U; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return n; } int32_t read32() { int c; while((c = getchar_unlocked()) < '-'); bool neg = c == '-'; if(neg) c = getchar_unlocked(); uint32_t n = c & 15U; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int32_t)n : n; } int64_t read64() { int c; while((c = getchar_unlocked()) < '-'); bool neg = c == '-'; if(neg) c = getchar_unlocked(); uint64_t n = c & 15; while((c = getchar_unlocked()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int64_t)n : n; }

using namespace std;

#define N 200050

int co[N];
int p[N];

int main()
{
    ios_base::sync_with_stdio(0);

    int n=IU;
    int64_t k = IU;
    for(int i=0; i<=n; i++) co[i] = II;

    int nd = 0;
    int sgn = 0;
    for(int i = 0; i <= n; i++) {
        int x = co[i];
        int j = i;
        while(x) {
            if(j >= nd && x == (sgn ? 1 : -1)) {
                sgn ^= 1;
                break;
            }
            int s = (x & 1) + (j < nd ? p[j] : sgn);
            p[j++] = s & 1;
            x = (x >> 1) + (s >> 1);
        }
        nd = max(nd, j);
        while(nd && p[nd-1] == sgn)
            nd--;
    }

    int i = 0;
    while(i < nd && p[i] == 0) i++;
    if(nd - i >= 33) {
        cout << 0;
        return 0;
    }
    int64_t t = -((int64_t)sgn << nd-i);
    int a = 0;
    for(int j = i; j < nd; j++) t += (int64_t)p[j] << j-i;
    for(int j = i; j >= 0; j--) {
        if(abs(t) >> 33) break;
        if(j < n || (j == n && co[j] != t))
            a += abs(co[j] - t) <= k;
        t *= 2;
    }
    cout<<a;

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define LL long long
#define Lowbit(x) ((x)&(-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
#define MP(a, b) make_pair(a, b)
const int INF = 0x3f3f3f3f;
const int Mod = 1000000007;
const int maxn = 1e5 + 10;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef pair<int, int> pii;
LL a[maxn * 2];
LL pre[maxn * 2];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n, k;
    scanf("%d%d", &n, &k);
    LL num = 0;
    for (int i = 0; i <= n; i++)
        scanf("%lld", &a[i]), pre[i] = a[i];
    for (int i = 0; i < n; i++)
    {
        pre[i+1] += pre[i] / 2LL;
        pre[i] %= 2LL;
    }
    int first = 0;
    for (int i = 0; i <= n; i++)
        if (pre[i])
        {
            first = i;
            break;
        }
    int ans = 0;
    LL sum = 0;
    for (int i = n; i >= 0; i--)
    {
        sum = sum * 2LL + pre[i];
        if (i <= first)
        {
            LL t = sum - a[i];
            if (abs(t) <= k && !(i == n && t == 0))
            {
                ans++;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define LL long long
#define Lowbit(x) ((x)&(-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
#define MP(a, b) make_pair(a, b)
const int INF = 0x3f3f3f3f;
const int Mod = 1000000007;
const int maxn = 1e5 + 10;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef pair<int, int> pii;
LL a[maxn * 2];
LL pre[maxn * 2];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n, k;
    scanf("%d%d", &n, &k);
    LL num = 0;
    for (int i = 0; i <= n; i++)
        scanf("%lld", &a[i]), pre[i] = a[i];
    for (int i = 0; i < n; i++)
    {
        pre[i+1] += pre[i] / 2LL;
        pre[i] %= 2LL;
    }
    int first = 0;
    for (int i = 0; i <= n; i++)
        if (pre[i])
        {
            first = i;
            break;
        }
    int ans = 0;
    LL sum = 0;
    for (int i = n; i >= 0; i--)
    {
        sum = sum * 2LL + pre[i];
        if (abs(sum) > 1LL * 1e9 * 1e7) break;
        if (i <= first)
        {
            LL t = sum - a[i];
            if (abs(t) <= k && !(i == n && t == 0))
            {
                ans++;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <set>
#include <complex>
#include <map>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define INF 1<<30
#define INFL 0x3f3f3f3f3f3f3f3f
#define Mod 1000000007
#define PII pair<int,int>
#define mk make_pair
#define mt make_tuple
#define Min(a,b) ((a)>(b)?(b):(a))
#define Max(a,b) ((a)<(b)?(b):(a))
using namespace std;
const double PI=acos(-1.0);
const double EPS=1e-10;

inline int read() {
    static char ch;
    bool sgn = false;
    while (ch = getchar(), ch < '0' || ch > '9') if (ch == '-') sgn = true;
    int res = ch - 48;
    while (ch = getchar(), ch >= '0' && ch <= '9') res = res * 10 + ch - 48;
    return sgn ? -res : res;
}

const int N=2e5+5;

LL a[N],b[N],n,k;

int main() {
	n=read(); k=read();
	for(int i=0;i<=n;i++) b[i]=a[i]=read();
	for(int i=0;i<=n;i++) {
		a[i+1]+=a[i]/2;
		a[i]%=2;
	}
	int st=0;
	for(int i=0;i<=n+1;i++) {
		if(a[i]) {
			st=i;
			break;
		}
	}
	int ans=0;
	LL sum=0;
	for(int i=n+1;i>=0;i--) {
		sum*=2; sum+=a[i];
		if(abs(sum)>k*4) break;
		if(i<=st) {
			if(abs(b[i]-sum)<=k&&i<n||i==n&&abs(b[i]-sum)<=k&&b[i]!=sum) ans++;
		}
	}
	cout<<ans;
}


#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <set>
#include <complex>
#include <map>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define INF 1<<30
#define INFL 0x3f3f3f3f3f3f3f3f
#define Mod 1000000007
#define PII pair<int,int>
#define mk make_pair
#define mt make_tuple
#define Min(a,b) ((a)>(b)?(b):(a))
#define Max(a,b) ((a)<(b)?(b):(a))
using namespace std;
const double PI=acos(-1.0);
const double EPS=1e-10;

inline int read() {
    static char ch;
    bool sgn = false;
    while (ch = getchar(), ch < '0' || ch > '9') if (ch == '-') sgn = true;
    int res = ch - 48;
    while (ch = getchar(), ch >= '0' && ch <= '9') res = res * 10 + ch - 48;
    return sgn ? -res : res;
}

const int N=2e5+5;

LL a[N],n,k;

int main() {
	n=read(); k=read();
	for(int i=0;i<=n;i++) a[i]=read();
	for(int i=0;i<=n;i++) {
		a[i+1]+=a[i]/2;
		a[i]%=2;
	}
	int st=0;
	for(int i=0;i<=n+1;i++) {
		if(a[i]) {
			st=i;
			break;
		}
	}
	int ans=0;
	LL sum=0;
	for(int i=n+1;i>=0;i--) {
		sum*=2; sum+=a[i];
		if(abs(sum)>k*4) break;
		if(i<=st) {
			if(abs(a[i]-sum)<=k&&i<n||i==n&&abs(a[i]-sum)<=k&&a[i]!=sum) ans++;
		}
	}
	cout<<ans;
}


#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <set>
#include <complex>
#include <map>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define INF 1<<30
#define INFL 0x3f3f3f3f3f3f3f3f
#define Mod 1000000007
#define PII pair<int,int>
#define mk make_pair
#define mt make_tuple
#define Min(a,b) ((a)>(b)?(b):(a))
#define Max(a,b) ((a)<(b)?(b):(a))
using namespace std;
const double PI=acos(-1.0);
const double EPS=1e-10;

inline int read() {
    static char ch;
    bool sgn = false;
    while (ch = getchar(), ch < '0' || ch > '9') if (ch == '-') sgn = true;
    int res = ch - 48;
    while (ch = getchar(), ch >= '0' && ch <= '9') res = res * 10 + ch - 48;
    return sgn ? -res : res;
}

const int N=2e5+5;

LL a[N],n,k;

int main() {
	n=read(); k=read();
	for(int i=0;i<=n;i++) a[i]=read();
	for(int i=0;i<=n;i++) {
		a[i+1]+=a[i]/2;
		a[i]%=2;
	}
	int st=0;
	for(int i=0;i<=n+1;i++) {
		if(a[i]) {
			st=i;
			break;
		}
	}
	int ans=0;
	LL sum=0;
	for(int i=n+1;i>=0;i--) {
		sum*=2; sum+=a[i];
		if(abs(sum)>k*4) break;
		if(i<=st) {
			if(abs(a[i]-sum)<=k) ans++;
		}
	}
	cout<<ans;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(-cn + coeffs[i]) <= k; ci++) {
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(-cn + coeffs[i]) <= k) {
            if (cn != 0 && cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    vector<int> coeffs(n);
    vector<int> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
    }
    if (number.back() == -1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(cn + coeffs[i]) <= k; ci++) {
            if ((ci - i) > 50) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if ((ci == number.size() && abs(cn + coeffs[i]) <= k)) {
            if (cn != 0) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    vector<int> coeffs(n);
    vector<int> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
    }
    if (number.back() == -1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(cn + coeffs[i]) <= k; ci++) {
            if ((ci - i) > 40 && number[i]) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if ((ci == number.size() && abs(cn + coeffs[i]) <= k)) {
            if (cn != 0 && -cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(cn + coeffs[i]) <= k; ci++) {
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(cn + coeffs[i]) <= k) {
            if (cn != 0 && -cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<int> coeffs(n);
    vector<int> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
    }
    if (number.back() == -1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(cn + coeffs[i]) <= k; ci++) {
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(cn + coeffs[i]) <= k) {
            if (cn != 0 && -cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<int> coeffs(n);
    vector<int> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(cn + coeffs[i]) <= k; ci++) {
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(cn + coeffs[i]) <= k) {
            if (cn != 0 && -cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    vector<int> coeffs(n);
    vector<int> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
    }
    if (number.back() == -1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(cn + coeffs[i]) <= k; ci++) {
            if ((ci - i) > 40 && number[i]) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if ((ci == number.size() && abs(cn + coeffs[i]) <= k)) {
            if (cn != 0) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(-cn + coeffs[i]) <= k; ci++) {
            if (ci - i > 40) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(-cn + coeffs[i]) <= k) {
            if (cn != 0 && -cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(-cn + coeffs[i]) <= k; ci++) {
            if (ci - i > 40 && number[ci]) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(-cn + coeffs[i]) <= k) {
            if (cn != 0 && (i < n - 1 || cn != coeffs[i])) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(-cn + coeffs[i]) <= k; ci++) {
            if (ci - i > 40) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(-cn + coeffs[i]) <= k) {
            if (cn != 0 && (i < n - 1 || cn != coeffs[i])) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    int result = 0;

    for (int i = 0; i < n; i++) {
        long long cn = 0;
        int ci;
        for (ci = i; ci < number.size() && abs(-cn + coeffs[i]) <= k; ci++) {
            if (ci - i > 40) break;
            cn += (1ll * number[ci]) << (ci - i);
        }
        if (ci == number.size() && abs(-cn + coeffs[i]) <= k) {
            if (cn != 0 && cn != coeffs[i]) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    n++;
    vector<long long> coeffs(n);
    vector<long long> number(n);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        coeffs[i] = a;
        number[i] = a;
    }
    int nones = 0;
    for (int i = 0; i < number.size(); i++) {
        if (number[i] / 2 && i + 1 == number.size()) number.push_back(0);
        number[i + 1] += number[i] / 2;
        number[i] = number[i] % 2;
        if (number[i] == -1 && i + 1 != number.size()) {
            number[i] = 1;
            number[i + 1] -= 1;
        }
        nones += (number[i] != 0);
    }
    if (number.back() == -1 && nones > 1) {
        bool seen = false;
        for (int i = 0; i < number.size(); i++) {
            if (number[i] && !seen) {
                number[i] = -number[i];
                seen = true;
            } else if (seen) {
                number[i] = -(1 - number[i]);
            }
        }
        number.pop_back();
    }

    vector<pair<int, long long>> cnumber;
    for (int i = 0; i < number.size(); i++) {
        if (number[i]) {
            cnumber.push_back({i, number[i]});
        }
    }

    int result = 0;

    int cci = 0;
    for (int i = 0; i < n; i++) {
        long long cn = 0;
        while (cnumber[cci].first < i) {
            cci++;
        }
        int ci;
        for (ci = cci; ci < cnumber.size() && abs(-cn + coeffs[i]) <= k; ci++) {
            pair<int, long long> digit = cnumber[ci];
            int di = digit.first;
            long long digitn = digit.second;
            if (di - i > 40 && digitn) break;
            cn += (1ll * digitn) << (di - i);
        }
        if (ci == cnumber.size() && abs(-cn + coeffs[i]) <= k) {
            if (cn != 0 && (i < n - 1 || cn != coeffs[i])) {
                result++;
            }
        }
        if (number[i]) break;
    }

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

    return 0;
}


cmake_minimum_required(VERSION 3.3)
project(D)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(D ${SOURCE_FILES})


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

LL n, k, a[N];

LL abs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(abs(tmp) <= k) ans ++;
        for(int i = 2; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(abs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e17;
int casenum, casei;

LL k, n, o[N], a[N];
bool v[N];
const LL big = 4e9;

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int solve()
{
    int ret = 0;
    for(int i = n + 100; i >= 1; i --){
        if(aabs(a[i] - o[i]) > big) return ret;
        if(aabs(a[i] - o[i]) <= k){
            if(i <= n + 1 && v[i - 1] == 0){
                if(i != n + 1 || aabs(a[i] - o[i])) ret ++;
            }
        }
        a[i - 1] += a[i] * 2;
    }
    return ret;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        ms(o, 0);
        for(int i = 1; i <= n + 1; i ++) scanf("%lld", &o[i]);
        mc(a, o);
        for(int i = 1; i <= n + 100; i ++){
            a[i + 1] += a[i] / 2;
            a[i] %= 2;
            v[i] = v[i - 1] | a[i];
        }
        printf("%d\n", solve());
    }
    return 0;
}

/*
LL n, k, a[N];

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0, p = 1;;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
            if(tmp >= INF){
                p = i;
                break;
            }
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(aabs(tmp) <= k) ans ++;
        for(int i = 2; i <= p; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
        }
        for(int i = p + 1; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(aabs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
*/

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input
 3 1000000000
 10 -9 -3 5

 3 12
 10 -9 -3 5

 2 20
 14 -7 19

 Sample Output

 */


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

LL n, k, a[N];

LL abs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0, p = 1;;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
            if(tmp >= INF){
                p = i;
                break;
            }
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(abs(tmp) <= k) ans ++;
        for(int i = 2; i <= p; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
        }
        for(int i = p + 1; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(abs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input
 3 1000000000
 10 -9 -3 5

 3 12
 10 -9 -3 5

 2 20
 14 -7 19

 Sample Output

 */


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

LL n, k, a[N];

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(aabs(tmp) <= k) ans ++;
        for(int i = 2; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(aabs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 1000, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e17;
int casenum, casei;

LL k, n, o[N], a[N];
bool v[N];
const LL big = 4e9;

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int solve()
{
    int ret = 0;
    for(int i = n + 100; i >= 1; i --){
        if(aabs(a[i] - o[i]) > big) return ret;
        if(aabs(a[i] - o[i]) <= k){
            if(i <= n + 1 && v[i - 1] == 0){
                if(i != n + 1 || aabs(a[i] - o[i])) ret ++;
            }
        }
        a[i - 1] += a[i] * 2;
    }
    return ret;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        ms(o, 0);
        for(int i = 1; i <= n + 1; i ++) scanf("%lld", &o[i]);
        mc(a, o);
        for(int i = 1; i <= n + 100; i ++){
            a[i + 1] += a[i] / 2;
            a[i] %= 2;
            v[i] = v[i - 1] | a[i];
        }
        printf("%d\n", solve());
    }
    return 0;
}

/*
LL n, k, a[N];

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0, p = 1;;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
            if(tmp >= INF){
                p = i;
                break;
            }
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(aabs(tmp) <= k) ans ++;
        for(int i = 2; i <= p; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
        }
        for(int i = p + 1; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(aabs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
*/

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input
 3 1000000000
 10 -9 -3 5

 3 12
 10 -9 -3 5

 2 20
 14 -7 19

 Sample Output

 */


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

LL n, k, a[N];

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0, p = 1;;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
            if(tmp >= INF){
                p = i;
                break;
            }
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(aabs(tmp) <= k) ans ++;
        for(int i = 2; i <= p; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
        }
        for(int i = p + 1; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(aabs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input
 3 1000000000
 10 -9 -3 5

 3 12
 10 -9 -3 5

 2 20
 14 -7 19

 Sample Output

 */


//
//  main.cpp
//  CF VK Cup 2016 - Round 1 (Div. 2 Edition)  D
//
//  Created by luras on 16/4/1.
//  Copyright (c) 2016年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e17;
int casenum, casei;

LL n, k, a[N];

LL aabs(LL x)
{
    if(x < 0) return -x;
    return x;
}

int main()
{
    while(~ scanf("%lld%lld", &n, &k)){
        for(int i = 1; i <= n + 1; i ++){
            scanf("%lld", &a[i]);
        }
        LL tmp = 0, p = 1;;
        for(LL i = n + 1; i > 1; i --){
            tmp = tmp * 2 + a[i];
            if(tmp >= INF){
                p = i;
                break;
            }
        }
        tmp *= 2; int flag = 1;
        LL tmp1 = 0, ans = 0; if(aabs(tmp) <= k) ans ++;
        for(int i = 2; i <= p; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
        }
        for(int i = p + 1; i <= n + 1; i ++){
            tmp1 += a[i - 1];
            if(tmp1 % 2) {flag = 0; break;}
            tmp1 /= 2;
            tmp /= 2;
            tmp -= a[i];
            if(aabs(tmp1 + tmp) <= k && !(i == n + 1 && tmp1 + tmp == 0)) ans ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input
 3 1000000000
 10 -9 -3 5

 3 12
 10 -9 -3 5

 2 20
 14 -7 19

 Sample Output

 */


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+6;

long long a[maxn];
long long c[maxn];
int n,flag;
long long k;
int main()
{
    scanf("%d%I64d",&n,&k);
    for(int i=0;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
        c[i]=a[i];
    }
    for(int i=0;i<n;i++)
    {
        a[i+1]+=a[i]/2LL;
        a[i]%=2LL;
    }
    for(int i=0;i<=n;i++)
        if(a[i])
        {
            flag = i;
            break;
        }
    long long sum = 0;
    long long ans = 0;
    for(int i=n;i>=0;i--)
    {
        sum = sum * 2LL + a[i];
        if(abs(sum)>1ll*1e9*1e7)break;
        if(i<=flag)
        {
            long long p = c[i]-sum;
            if(abs(p)<=k)
            {
                if(i==n&&p==0)continue;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)
#define CLOSEIO ios::sync_with_stdio(false)
#define PI acos(-1)
#define mod 1000000007
#define eps 1e-11
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)
using namespace std;
const int maxn = 2e5+100;
typedef long long LL;
const LL inf = (1LL<<30-1);

LL a[maxn], c[maxn];

int main()
{
    //FILEIN;
    int n;
    LL k;scanf("%d%I64d", &n, &k);
    for(int i = 0; i <= n; i++){
         scanf("%I64d", &a[i]);
         c[i] = a[i];
    }
    for(int i = 0; i < n; i++){
        a[i+1] += a[i]>>1;
        a[i] %= 2LL;
    }
    int flag = -1;
    for(int i = 0; i <= n; i++)if(a[i]){
        flag = i;
        break;
    }
    LL sum = 0;
    LL ans = 0;
    for(int i = n; i >= 0; i--){
        sum = (sum * 2) + a[i];
        if(abs(sum) > k * 1e10)break;
        if(i <= flag){
            LL p = c[i] - sum;
            if(abs(p) <= k){
                if( i == n && p==0)continue;
                //cout << i << ' ' << sum << ' ' << p << endl;
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)
#define CLOSEIO ios::sync_with_stdio(false)
#define PI acos(-1)
#define mod 1000000007
#define eps 1e-11
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)
using namespace std;
const int maxn = 2e5+100;
typedef long long LL;
const LL inf = (1LL<<30-1);

LL a[maxn], c[maxn];

int main()
{
    //FILEIN;
    int n;
    LL k;scanf("%d%I64d", &n, &k);
    for(int i = 0; i <= n; i++){
         scanf("%I64d", &a[i]);
         c[i] = a[i];
    }
    for(int i = 0; i < n; i++){
        a[i+1] += a[i]/2LL;
        a[i] %= 2LL;
    }
    int flag = -1;
    for(int i = 0; i <= n; i++)if(a[i]){
        flag = i;
        break;
    }
    LL sum = 0;
    LL ans = 0;
    for(int i = n; i >= 0; i--){
        sum = (sum * 2LL) + a[i];
        if(abs(sum) > 1LL*1e9*1e7)break;
        if(i <= flag){
            LL p = c[i] - sum;
            if(abs(p) <= k){
                if( i == n && p==0)continue;
                //cout << i << ' ' << sum << ' ' << p << endl;
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define FILEIN freopen("in.txt", "r", stdin)
#define FILEOUT freopen("out.txt", "w", stdout)
#define CLOSEIO ios::sync_with_stdio(false)
#define PI acos(-1)
#define mod 1000000007
#define eps 1e-11
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)
using namespace std;
const int maxn = 2e5+100;
typedef long long LL;
const LL inf = (1LL<<30-1);

LL a[maxn], c[maxn];

int main()
{
    //FILEIN;
    int n;
    LL k;scanf("%d%I64d", &n, &k);
    for(int i = 0; i < n; i++){
         scanf("%I64d", &a[i]);
         c[i] = a[i];
    }
    for(int i = 0; i < n; i++){
        a[i+1] += a[i]>>1;
        a[i] &= 1;
    }
    int flag = -1;
    for(int i = 0; i <= n; i++)if(a[i]){
        flag = i;
        break;
    }
    LL sum = 0;
    LL ans = 0;
    for(int i = n; i >= 0; i--){
        sum = (sum << 1) + a[i];
        if(abs(sum) > k * 1e10)break;
        if(i <= flag){
            LL p = c[i] - sum;
            if(abs(p) <= k){
                if( i == n && p==0)continue;
                //cout << i << ' ' << sum << ' ' << p << endl;
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 200000 + 5;
LL x[MAXN], y[MAXN];
/*
3 2
2 -1 -1 1
*/
int main()
{
    int n, k;
    while(scanf("%d%d", &n, &k) != EOF) {
        for(int i = 0; i <= n; i++) {
            scanf("%lld", &x[i]);
            y[i] = x[i];
        }
        y[n+1] = 0;
        for(int i = 0; i <= n; i++) {
            y[i+1] += y[i] / 2;
            y[i] %= 2;
        }
        int F = n + 1;
        for(int i = 0; i <= n; i++) {
            if(y[i]) {
                F = i; break;
            }
        }
        //因为把所有的都往后面放一位了，所以肯定是2*k
        int ans = 0; LL xx = y[n+1];
        for(int i = n; i >= 0; i--) {
            xx = 2 * xx + y[i];
            if(abs(xx) > 2 * k) break;
            if(i > F) continue;
            if(i == n && x[i] == xx) continue;//首位相等肯定无解
            ans += (abs(x[i] - xx) <= k);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 200000 + 5;
ll n, k;
ll a[maxn];
ll b[maxn];

int main(){
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
        scanf("%I64d", a + i);
        b[i] = a[i];
    }
    for (int i = 0; i <= n; i++){
        b[i + 1] += (b[i] / 2);
        b[i] %= 2;
        //printf("i = %d b[i] = %I64d\n", i, b[i]);
    }
    int l = n + 1;
    for (int i = 0; i <= n; i++){
        if (b[i]) {
            l = i;
            break;
        }
    }
    ll tmp = b[n + 1];
    ll ans = 0;
    for (int i = n; i >= 0; i--){
        tmp = (tmp << 1) + b[i];
        if (abs(tmp) > 2 * k) break;
        if (i > l) continue;
        if (i == n && tmp == a[i]) continue;
        ans += (abs(a[i] - tmp) <= k);
    }
    printf("%I64d\n", ans);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 200000 + 5;
ll n, k;
ll a[maxn];
ll b[maxn];

int main(){
    cin >> n >> k;
    for (int i = 0; i < n; i++) {scanf("%I64d", a + i); b[i] = a[i];}
    for (int i = 0; i < n; i++){
        b[i + 1] = b[i] >> 1;
        b[i] %= 2;
    }
    int l;
    for (int i = 0; i <= n; i++){
        if (b[i] != 0) {
            l = i;
            break;
        }
    }
    ll tmp = 0;
    ll ans = 0;
    for (int i = n; i >= 0; i--){
        tmp = (tmp >> 1) + b[i];
        if (tmp > 2 * k) break;
        if (i > l) continue;
        if (i == n && tmp == a[i]) continue;
        ans += (abs(tmp - k) <= k);
    }
    printf("%I64d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 200000 + 5;
LL x[MAXN], y[MAXN];

int main()
{
    int n, k;
    while(scanf("%d%d", &n, &k) != EOF) {
        for(int i = 0; i <= n; i++) {
            scanf("%lld", &x[i]);
            y[i] = x[i];
        }
        y[n+1] = 0;
        for(int i = 0; i <= n; i++) {
            y[i+1] += y[i] / 2;
            y[i] %= 2;
        }
        int F = n + 1;
        for(int i = 0; i <= n; i++) {
            if(y[i]) {
                F = i; break;
            }
        }
        int ans = 0; LL xx = y[n+1];
        for(int i = n; i >= 0; i--) {
            xx = 2 * xx + y[i];
            if(abs(xx) > 2 * k) break;
            if(i > F) continue;
            if(i == n && x[i] == xx) continue;
            ans += (abs(x[i] - xx) <= k);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 200000 + 5;
ll n, k;
ll a[maxn];
ll b[maxn];

int main(){
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
        scanf("%I64d", a + i);
        b[i] = a[i];
    }
    for (int i = 0; i <= n; i++){
        b[i + 1] += b[i] >> 1;
        b[i] %= 2;
        //printf("i = %d b[i] = %I64d\n", i, b[i]);
    }
    int l = n + 1;
    for (int i = 0; i <= n; i++){
        if (b[i] != 0) {
            l = i;
            break;
        }
    }
    ll tmp = b[n + 1];
    ll ans = 0;
    for (int i = n; i >= 0; i--){
        tmp = (tmp << 1) + b[i];
        if (abs(tmp) > 2 * k) break;
        if (i > l) continue;
        if (i == n && tmp == a[i]) continue;
        ans += (abs(a[i] - tmp) <= k);
    }
    printf("%I64d\n", ans);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 200000 + 5;
ll n, k;
ll a[maxn];
ll b[maxn];

int main(){
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
        scanf("%I64d", a + i);
        b[i] = a[i];
    }
    for (int i = 0; i <= n; i++){
        b[i + 1] += (b[i] / 2);
        b[i] %= 2;
        printf("i = %d b[i] = %I64d\n", i, b[i]);
    }
    int l = n + 1;
    for (int i = 0; i <= n; i++){
        if (b[i]) {
            l = i;
            break;
        }
    }
    ll tmp = b[n + 1];
    ll ans = 0;
    for (int i = n; i >= 0; i--){
        tmp = (tmp << 1) + b[i];
        if (abs(tmp) > 2 * k) break;
        if (i > l) continue;
        if (i == n && tmp == a[i]) continue;
        ans += (abs(a[i] - tmp) <= k);
    }
    printf("%I64d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 200000 + 5;
LL x[MAXN], y[MAXN];

int main()
{
    int n, k;
    while(scanf("%d%d", &n, &k) != EOF) {
        for(int i = 0; i <= n; i++) {
            scanf("%lld", &x[i]);
            y[i] = x[i];
        }
        y[n+1] = 0;
        for(int i = 0; i <= n; i++) {
            y[i+1] += y[i] / 2;
            y[i] %= 2;
        }
        int F = n + 1;
        for(int i = 0; i <= n; i++) {
            if(y[i]) {
                F = i; break;
            }
        }
        int ans = 0; LL xx = y[n+1];
        for(int i = n; i >= 0; i--) {
            xx = 2 * xx + y[i];
            if(abs(xx) >= 2 * k) break;
            if(i > F) continue;
            if(i == n && x[i] == xx) continue;
            ans += (abs(x[i] - xx) <= k);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 200000 + 5;
ll n, k;
ll a[maxn];
ll b[maxn];

int main(){
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
        scanf("%I64d", a + i);
        b[i] = a[i];
    }
    for (int i = 0; i <= n; i++){
        b[i + 1] += (b[i] / 2);
        b[i] %= 2;
        //printf("i = %d b[i] = %I64d\n", i, b[i]);
    }
    int l = n + 1;
    for (int i = 0; i <= n; i++){
        if (b[i]) {
            l = i;
            break;
        }
    }
    ll tmp = b[n + 1];
    ll ans = 0;
    for (int i = n; i >= 0; i--){
        tmp = (tmp << 1) + b[i];
        if (abs(tmp) > 2 * k) break;
        if (i > l) continue;
        if (i == n && tmp == a[i]) continue;
        ans += (abs(tmp) <= k);
    }
    printf("%I64d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define eps 0.00000001
#define pi P_MI
#define ll long long
using namespace std;
struct point {
    int x,y;
    bool operator < (const point &a) const {
        return x>a.x;//最小值优先
    }
};

priority_queue <int> que;

ll a[210000];

int main(){
    ll n,k;
    while (~scanf("%lld%lld",&n,&k)){
        for (int i=0; i<=n; i++){
            scanf("%lld",&a[i]);
        }
        ll p=0,first1=a[0];
        while (first1%2==0){
            p++;
            first1=first1/2+a[p];
//            printf("p=%lld  first1=%lld\n",p,first1);
        }
        p=min(n,p);
//        printf("min(n,p)=%lld  first1=%lld\n",p,first1);

        ll sum=0,oversum=k*10,ff=0;
        for (int i=n; i>=p; i--){
            sum=sum+sum+a[i];
//            printf("i=%d  sum=%lld\n",i,sum);
            if (sum>oversum){
                ff=1;
                break;
            }
        }
        if (ff) {
            printf("0\n");
            continue;
        }

        int ans=0,i9=p;
        sum=sum+first1-a[i9];
//        printf("i9=%d  sum=%lld\n",i9,sum);
        if (i9==n && sum==a[i9]) {
            i9--;
            sum=sum+sum;
        }
        while (sum<oversum && i9>=0){
            if ((a[i9]-sum>=-k) && (a[i9]-sum<=k)) ans++;
            i9--;
            sum=sum+sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}

//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define eps 0.00000001
#define pi P_MI
#define ll long long
using namespace std;
struct point {
    int x,y;
    bool operator < (const point &a) const {
        return x>a.x;//最小值优先
    }
};

priority_queue <int> que;

ll a[210000];

int main(){
    ll n,k;
    while (~scanf("%lld%lld",&n,&k)){
        for (int i=0; i<=n; i++){
            scanf("%lld",&a[i]);
        }
        ll p=0,first1=a[0];
        while (first1%2==0){
            p++;
            first1=first1/2+a[p];
//            printf("p=%lld  first1=%lld\n",p,first1);
        }
        while (n<p){
            p--;
            first1=first1+first1;
        }
//        printf("min(n,p)=%lld  first1=%lld\n",p,first1);

        ll sum=0,oversum=k*10,ff=0;
        for (int i=n; i>=p; i--){
            sum=sum+sum+a[i];
//            printf("i=%d  sum=%lld\n",i,sum);
            if (sum>oversum){
                ff=1;
                break;
            }
        }
        if (ff) {
            printf("0\n");
            continue;
        }

        int ans=0,i9=p;
        sum=sum+first1-a[i9];
        if (i9==n && sum==a[i9]) {
            i9--;
            sum=sum+sum;
        }
        while (sum<oversum && i9>=0){
            if ((a[i9]-sum>=-k) && (a[i9]-sum<=k)) ans++;
            i9--;
            sum=sum+sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}

//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define eps 0.00000001
#define pi P_MI
#define ll long long
using namespace std;
struct point {
    int x,y;
    bool operator < (const point &a) const {
        return x>a.x;//最小值优先
    }
};

priority_queue <int> que;

ll a[210000];
ll n,k;

ll yanzheng(int pp){
    for (int i=0; i<=pp; i++){
        if (a[i]%2==0){
            a[i+1]=a[i]/2+a[i+1];
        } else return 0;
    }
    if (a[k]!=0) return 0;
    return 1;
}

ll jisuan(ll pp,ll s){
    ll ans=0;
//    printf("pp = %lld   s = %lld   k = %lld\n",pp,s,k);
    for (int i=pp; i<n; i++){
//        printf("--- %d   %lld %lld %lld\n",i,s,a[i],ans);
        if ((a[i]-s<=k) && (a[i]-s>=-k)) ans++;
        if (s%2==0) {
            s=s/2;
        } else break;
    }
    return ans;
}

int main() {
    while (~scanf("%lld%lld",&n,&k)){
        ll p2=0,sum=0;
        for (int i=0; i<=n; i++){
            scanf("%lld",&a[i]);
        }
        int f=0;
        for (int i=n; i>=0; i--){
            p2=p2+p2;
            p2=p2+a[i];
//            printf("i = %d   p2 = %lld   \n",i,p2);
            if ( (p2>=k+k+k) || (p2<=(-k-k-k)) ){
                if (yanzheng(i)==1) sum = jisuan(i+1,p2/2-a[i]);
                else sum=0;
                printf("%lld\n",sum);
                f=1; i=0;
            }
        }
        if (f) continue;
        sum = jisuan(0,p2);
        printf("%lld\n",sum);
    }
    return 0;
}

//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define eps 0.00000001
#define pi P_MI
#define ll long long
using namespace std;
struct point {
    int x,y;
    bool operator < (const point &a) const {
        return x>a.x;//最小值优先
    }
};

priority_queue <int> que;

ll a[210000];

int main(){
    ll n,k;
    while (~scanf("%lld%lld",&n,&k)){
        for (int i=0; i<=n; i++){
            scanf("%lld",&a[i]);
        }
        ll p=0,first1=a[0];
        while (first1%2==0){
            p++;
            first1=first1/2+a[p];
//            printf("p=%lld  first1=%lld\n",p,first1);
        }
        while (n<p){
            p--;
            first1=first1+first1;
        }
//        printf("min(n,p)=%lld  first1=%lld\n",p,first1);

        ll sum=0,oversum=k*10,ff=0;
        for (int i=n; i>=p; i--){
            sum=sum+sum+a[i];
//            printf("i=%d  sum=%lld\n",i,sum);
            if (sum>oversum || sum<-oversum){
                ff=1;
                break;
            }
        }
        if (ff) {
            printf("0\n");
            continue;
        }

        int ans=0,i9=p;
        sum=sum+first1-a[i9];
        if (i9==n && sum==a[i9]) {
            i9--;
            sum=sum+sum;
        }
        while (sum<oversum && sum>-oversum && i9>=0){
            if ((a[i9]-sum>=-k) && (a[i9]-sum<=k)) ans++;
            i9--;
            sum=sum+sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}

//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);


#include <bits/stdc++.h>
#define maxN 200002
#define ll long long
using namespace std;
int n, k, a[maxN], b[maxN], m, sol;
void read()
{
    int i;
    freopen("d.in", "r", stdin);
    scanf("%d %d", &n, &k);
    for (i = 0; i <= n; ++ i)
        {
            scanf("%d", &a[i]);
            b[i] = a[i];
        }
}
void solve()
{
    int i;
    for (i = 0; i < n; ++ i)
    {
        a[i + 1] += a[i] / 2;
        a[i] %= 2;
    }
    for (i = 0; i <= n; ++ i)
        if (a[i] != 0)
            {
                m = i;
                break;
            }
    ll ans = 0;
    sol = 0;
    for(i = n; i >= 0; -- i)
    {
        ans = ans * 2 + 1LL * a[i];
        if (abs(ans) >= 1LL * 1e9 * 1e9) break;
        if(i <= m)
        {
            if (abs(ans - 1LL * b[i]) > k) continue;
            if (i == n && ans == 1LL * b[i]) continue;
            ++ sol;
        }
    }
}
void write()
{
    printf("%d", sol);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}


#include <bits/stdc++.h>
#define maxN 200002
#define ll long long
using namespace std;
int n, k, a[maxN], b[maxN], m, sol;
void read()
{
    int i;
    //freopen("d.in", "r", stdin);
    scanf("%d %d", &n, &k);
    for (i = 0; i <= n; ++ i)
        {
            scanf("%d", &a[i]);
            b[i] = a[i];
        }
}
void solve()
{
    int i;
    for (i = 0; i < n; ++ i)
    {
        a[i + 1] += a[i] / 2;
        a[i] %= 2;
    }
    for (i = 0; i <= n; ++ i)
        if (a[i] != 0)
            {
                m = i;
                break;
            }
    ll ans = 0;
    sol = 0;
    for(i = n; i >= 0; -- i)
    {
        ans = ans * 2 + 1LL * a[i];
        if (abs(ans) >= 1LL * 1e9 * 1e9) break;
        if(i <= m)
        {
            if (abs(ans - 1LL * b[i]) > k) continue;
            if (i == n && ans == 1LL * b[i]) continue;
            ++ sol;
        }
    }
}
void write()
{
    printf("%d", sol);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}


//This is the painkiller...
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
using namespace std;
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define abv(a) ((a)>0?(a):-(a))
#define sqr(a) ((a)*(a))
#define fs(i,s) for(int i=0;s[i];i++)
#define fg(e,u) for(int e=L[u];e!=-1;e=pre[e])
#define f0(i,n) for(int i=0;i<(n);i++)
#define f1(i,a,n) for(int i=(a);i<(n);i++)
#define f2(i,a,n,b) for(int i=(a);i<(n);i+=(b))
#define rf0(i,n) for(int i=(n);i>=0;i--)
#define rf1(i,a,n) for(int i=(a);i>(n);i--)
#define rf2(i,a,n,b) for(int i=(a);i>(n);i-=(b))
#define mem(a,v) memset(a,v,sizeof(a))
#define pq priority_queue
#define pb push_back
#define pq priority_queue
#define sz size
#define vi vector<int>
#define vii vector<vector<int> >
#define vstr vector<string>
#define emt empty
#define clr clear
#define bn begin
#define en end
#define all(a) a.bn(),a.en()
#define dlt(v,a) v.erase(v.bn()+a)
#define dlst(v) v.erase(v.bn()+((int)v.size()-1))
#define nset(S,v) S.erase(find(v))
#define scn(a,b,n) f1(j,b,n+b)cin>>a[j]
#define scm(a,b,c,m,n) f1(i,b,m+b)scn(a[i],c,n)
#define prn(a,b,n) f1(j,b,n+b){if(j>0)cout<<' ';cout<<a[j];}
#define prm(a,b,c,m,n) f1(i,b,m+b){prni(a[i],c,n);cout<<endl;}
#define greater(a,b) ((a)>(b)+eps)
#define zero(a) (fabs(a)<eps)
#define Int __int64
const double Pi=acos(-1.0);
Int mod=1000000007;
#define inf 100000000000000000
#define eps 1e-9
#define MAX 100005
struct pii{int x,y;pii(int a=0,int b=0){x=a,y=b;}};
bool operator<(pii a,pii b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int L[MAX],pre[2*MAX],to[2*MAX],ne;
inline void addEdge(int u,int v){to[ne]=v,pre[ne]=L[u];L[u]=ne++;}
int D[MAX],V[MAX];pii P[MAX];
int mother[MAX],visit[MAX];
void dfs(int u)
{
    fg(e,u)
    {
        int v=to[e];
        if(D[v]==-1)
        {
            mother[v]=u;
            D[v]=D[u]+1;
            dfs(v);
        }
    }
}
int main()
{
    //ios_base::sync_with_stdio(false);
    //fin("input.txt");
    //fout("input.txt");
    //puts("100000");f0(i,100)f0(j,999)printf("%d %d\n",i*100+j+1,i*100+j+2);
    int n;scanf("%d",&n);mem(L,-1),ne=0;
    f0(i,n-1)
    {
        int u,v;scanf("%d%d",&u,&v);
        addEdge(u-1,v-1);
        addEdge(v-1,u-1);
    }
    f0(i,n)scanf("%d",&V[i]);
    mem(D,-1);mother[0]=D[0]=0;dfs(0);
    f0(i,n)P[i]=pii(D[i],i);
    sort(P,P+n);
    Int ans=0;mem(visit,false);
    rf0(i,n-1)
    {
        int g=P[i].y;
        if(!visit[g])
        {
            Int t=0,h=0;
            while(mother[g]!=g)
            {
                t+=abv(h+(Int)V[g]);
                h=-(Int)V[g];
                visit[g]=true;
                g=mother[g];
            }
            ans=max(t+abv(h+(Int)V[g]),ans);
        }
    }
    printf("%I64d",ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>

using namespace std;

const int MAXN=100010;
vector<int>e[MAXN] ;
long long dec1[MAXN],inc[MAXN];
long long val[MAXN];
int n;
void dfs(int u,int fa)
{
    int len=e[u].size();
    for(int i=0;i<len;i++)
    {
        int son=e[u][i];
        if(son==fa)
            continue;
        dfs(son,u) ;
        dec1[u]=max(dec1[u],dec1[son]);
        inc[u]=max(inc[u],inc[son]);
    }
    if(u!=1)
    if(len==1)
    {
        if(val[u]<=0)
        {
            inc[u]=-val[u];
            dec1[u]=0;
        }
        else
        {
            inc[u]=0 ;
            dec1[u]=val[u];
        }
        return ;
    }
    val[u]+=inc[u];
    val[u]-=dec1[u];
    if (val[u]>0)
        dec1[u]+=val[u];
    else
        inc[u]-=val[u];
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        inc[i]=dec1[i]=0;
    for(int i=0; i<n-1; i++)
    {
        int u,v ;
        scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u) ;
    }

    for(int i=1;i<=n;i++)
        scanf("%I64d",&val[i]) ;
    dfs(1,0);
    long long ans=inc[1]+dec1[1];
    cout<<ans<<endl ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>

using namespace std;

const int MAXN=100010;
vector<int>e[MAXN] ;
long long dec1[MAXN],inc[MAXN];
int val[MAXN];
int n;
void dfs(int u,int fa)
{
    int len=e[u].size();
    for(int i=0;i<len;i++)
    {
        int son=e[u][i];
        if(son==fa)
            continue;
        dfs(son,u) ;
        dec1[u]=max(dec1[u],dec1[son]);
        inc[u]=max(inc[u],inc[son]);
    }
    if(u!=1)
    if(len==1)
    {
        if(val[u]<=0)
        {
            inc[u]=-val[u];
            dec1[u]=0;
        }
        else
        {
            inc[u]=0 ;
            dec1[u]=val[u];
        }
        return ;
    }
    val[u]+=inc[u];
    val[u]-=dec1[u];
    if (val[u]>0)
        dec1[u]+=val[u];
    else
        inc[u]-=val[u];
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        inc[i]=dec1[i]=0;
    for(int i=0; i<n-1; i++)
    {
        int u,v ;
        scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u) ;
    }

    for(int i=1;i<=n;i++)
        scanf("%d",&val[i]) ;
    dfs(1,0);
    int ans=inc[1]+dec1[1];
    cout<<ans<<endl ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>

using namespace std;

const int MAXN=100010;
vector<int>e[MAXN] ;
long long dec1[MAXN],inc[MAXN];
int val[MAXN];
int n;
void dfs(int u,int fa)
{
    int len=e[u].size();
    for(int i=0;i<len;i++)
    {
        int son=e[u][i];
        if(son==fa)
            continue;
        dfs(son,u) ;
        dec1[u]=max(dec1[u],dec1[son]);
        inc[u]=max(inc[u],inc[son]);
    }
    if(u!=1)
    if(len==1)
    {
        if(val[u]<=0)
        {
            inc[u]=-val[u];
            dec1[u]=0;
        }
        else
        {
            inc[u]=0 ;
            dec1[u]=val[u];
        }
        return ;
    }
    val[u]+=inc[u];
    val[u]-=dec1[u];
    if (val[u]>0)
        dec1[u]+=val[u];
    else
        inc[u]-=-val[u];
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        inc[i]=dec1[i]=0;
    for(int i=0; i<n-1; i++)
    {
        int u,v ;
        scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u) ;
    }

    for(int i=1;i<=n;i++)
        scanf("%d",&val[i]) ;
    dfs(1,0);
    int ans=inc[1]+dec1[1];
    cout<<ans<<endl ;
    return 0 ;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>

using namespace std;

const int MAXN=100010;
vector<int>e[MAXN] ;
long long dec1[MAXN],inc[MAXN];
int val[MAXN];
int n;
void dfs(int u,int fa)
{
    int len=e[u].size();
    for(int i=0;i<len;i++)
    {
        int son=e[u][i];
        if(son==fa)
            continue;
        dfs(son,u) ;
        dec1[u]=max(dec1[u],dec1[son]);
        inc[u]=max(inc[u],inc[son]);
    }
    if(u!=1)
    if(len==1)
    {
        if(val[u]<=0)
        {
            inc[u]=-val[u];
            dec1[u]=0;
        }
        else
        {
            inc[u]=0 ;
            dec1[u]=val[u];
        }
        return ;
    }
    val[u]+=inc[u];
    val[u]-=dec1[u];
    if (val[u]>0)
        dec1[u]+=val[u];
    else
        inc[u]-=-val[u];
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        inc[i]=dec1[i]=0;
    for(int i=0; i<n-1; i++)
    {
        int u,v ;
        scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u) ;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&val[i]) ;
    dfs(1,0);
    int ans=inc[1]+dec1[1];
    cout<<ans<<endl ;
    return 0 ;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

bool viz[100010];
long long poz[100010], neg[100010], val[100010];
int n, i, x, y;
vector <int> v[100010];

void df(int nod)
{
    viz[nod] = 1;
    for(size_t i = 0; i < v[nod].size(); i++)
    if(!viz[v[nod][i]])
    {
        df(v[nod][i]);
        poz[nod] = max(poz[nod], poz[v[nod][i]]);
        neg[nod] = max(neg[nod], neg[v[nod][i]]);
    }
    val[nod] -= poz[nod];
    val[nod] += neg[nod];
    if(val[nod] < 0) neg[nod] += -val[nod];
    if(val[nod] > 0) poz[nod] += val[nod];
}

int main()
{
    //ifstream cin("168.in");
    cin >> n;
    for(i = 1; i < n; i++)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(i = 1; i <= n; i++) cin >> val[i];
    df(1);
    cout << neg[1] + poz[1] << "\n";
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

bool viz[100010];
int n, i, x, y, poz[100010], neg[100010], val[100010];
vector <int> v[100010];

void df(int nod)
{
    viz[nod] = 1;
    for(size_t i = 0; i < v[nod].size(); i++)
    if(!viz[v[nod][i]])
    {
        df(v[nod][i]);
        poz[nod] = max(poz[nod], poz[v[nod][i]]);
        neg[nod] = max(neg[nod], neg[v[nod][i]]);
    }
    val[nod] -= poz[nod];
    val[nod] += neg[nod];
    if(val[nod] < 0) neg[nod] += -val[nod];
    if(val[nod] > 0) poz[nod] += val[nod];
}

int main()
{
    ifstream fi("168.in");
    fi >> n;
    for(i = 1; i < n; i++)
    {
        fi >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(i = 1; i <= n; i++) fi >> val[i];
    df(1);
    cout << neg[1] + poz[1] << "\n";
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

bool viz[100010];
int n, i, x, y, poz[100010], neg[100010], val[100010];
vector <int> v[100010];

void df(int nod)
{
    viz[nod] = 1;
    for(size_t i = 0; i < v[nod].size(); i++)
    if(!viz[v[nod][i]])
    {
        df(v[nod][i]);
        poz[nod] = max(poz[nod], poz[v[nod][i]]);
        neg[nod] = max(neg[nod], neg[v[nod][i]]);
    }
    val[nod] -= poz[nod];
    val[nod] += neg[nod];
    if(val[nod] < 0) neg[nod] += -val[nod];
    if(val[nod] > 0) poz[nod] += val[nod];
}

int main()
{
    //ifstream cin("168.in");
    cin >> n;
    for(i = 1; i < n; i++)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(i = 1; i <= n; i++) cin >> val[i];
    df(1);
    cout << neg[1] + poz[1] << "\n";
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

void Get(int &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

void Get(long long &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

int N;

struct E
{
	int V,Next;
}Edge[200005];
int EdgeTop;
int Head[200005];

void Add(int U,int V)
{
	Edge[++EdgeTop]=(E){V,Head[U]};
	Head[U]=EdgeTop;
}

int Fa[200005];
int D[200005];
long long WOld[200005];
long long W[200005];

long long F[200005];
long long G[200005];

void DFS(int X)
{
	long long FMax=0,GMax=0;

	W[X]=WOld[X]-WOld[Fa[X]];

	for(int i=Head[X];i;i=Edge[i].Next)
		if(Fa[X]!=Edge[i].V)
		{
			Fa[Edge[i].V]=X;
			DFS(Edge[i].V);
			long long F0=F[Edge[i].V];
			long long G0=G[Edge[i].V];

			if(FMax<F0)FMax=F0;
			if(GMax<G0)GMax=G0;
		}

	F[X]=FMax;
	G[X]=GMax;

	if(W[X]>0)
		F[X]+=W[X];
	else
		G[X]-=W[X];
}

void Init()
{
	Get(N);
	for(int i=1,U,V;i<N;i++)
	{
		Get(U);Get(V);
		Add(U,V);
		Add(V,U);
		D[U]++;D[V]++;
	}

	for(int i=1;i<=N;i++)
		Get(WOld[i]);
}

void Work()
{
	DFS(1);
}

long long Ans;

void Output()
{
	Ans=F[1]+G[1];
	printf("%I64d\n",Ans);
}

int main()
{
	Init();
	Work();
	Output();
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

void Get(int &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

void Get(long long &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

int N;

struct E
{
	int V,Next;
}Edge[200005];
int EdgeTop;
int Head[200005];

void Add(int U,int V)
{
	Edge[++EdgeTop]=(E){V,Head[U]};
	Head[U]=EdgeTop;
}

int Fa[200005];
int D[200005];
long long WOld[200005];
long long W[200005];

long long F[200005];
long long G[200005];

void DFS(int X)
{
	long long FMax=0,GMax=0;

	W[X]=WOld[X]-WOld[Fa[X]];

	for(int i=Head[X];i;i=Edge[i].Next)
		if(Fa[X]!=Edge[i].V)
		{
			Fa[Edge[i].V]=X;
			DFS(Edge[i].V);
			long long F0=F[Edge[i].V];
			long long G0=G[Edge[i].V];

			if(FMax<F0)FMax=F0;
			if(GMax<G0)GMax=G0;
		}

	F[X]=FMax;
	G[X]=GMax;

	if(X!=1)
	{
		if(W[X]>0)
			F[X]+=W[X];
		else
			G[X]-=W[X];
	}
}

void Init()
{
	Get(N);
	for(int i=1,U,V;i<N;i++)
	{
		Get(U);Get(V);
		Add(U,V);
		Add(V,U);
		D[U]++;D[V]++;
	}

	for(int i=1;i<=N;i++)
		Get(WOld[i]);
}

void Work()
{
	DFS(1);
}

long long Ans;

void Output()
{
	Ans=F[1]+G[1];

	WOld[1]+=F[1];
	WOld[1]-=G[1];

	Ans+=std::abs(WOld[1]);
	printf("%I64d\n",Ans);
}

int main()
{
	Init();
	Work();
	Output();
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

void Get(int &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

void Get(long long &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

int N;

struct E
{
	int V,Next;
}Edge[200005];
int EdgeTop;
int Head[200005];

void Add(int U,int V)
{
	Edge[++EdgeTop]=(E){V,Head[U]};
	Head[U]=EdgeTop;
}

int Fa[200005];
int D[200005];
long long WOld[200005];
long long W[200005];

long long F[200005];
long long G[200005];

void DFS(int X)
{
	long long FMax=0,GMax=0;

	W[X]=WOld[X]-WOld[Fa[X]];

	for(int i=Head[X];i;i=Edge[i].Next)
		if(Fa[X]!=Edge[i].V)
		{
			Fa[Edge[i].V]=X;
			DFS(Edge[i].V);
			long long F0=F[Edge[i].V];
			long long G0=G[Edge[i].V];

			if(FMax<F0)FMax=F0;
			if(GMax<G0)GMax=G0;
		}

	F[X]=FMax;
	G[X]=GMax;

	if(W[X]>0)
		F[X]+=W[X];
	else
		G[X]-=W[X];
}

void Init()
{
	Get(N);
	for(int i=1,U,V;i<N;i++)
	{
		Get(U);Get(V);
		Add(U,V);
		Add(V,U);
		D[U]++;D[V]++;
	}

	for(int i=1;i<=N;i++)
		Get(WOld[i]);
}

void Work()
{
	DFS(1);
}

long long Ans;

void Output()
{
	Ans=F[1]+G[1];
	printf("%d\n",Ans);
}

int main()
{
	Init();
	Work();
	Output();
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;

typedef long long ll;

const int N = 1e5 + 10;
vector<int> G[N];
int n;
ll v[N], f1[N], f2[N];

void dfs(int u, int f) {
  ll c1 = 0, c2 = 0;
  for(int i = 0; i < G[u].size(); i++) {
      if(G[u][i] == f) continue;
    dfs(G[u][i], u);
    c1 = max(c1, f1[G[u][i]]);
    c2 = max(c2, f2[G[u][i]]);
  }
  if(v[u] - c1 + c2 >= 0) c1 += v[u] - c1 + c2;
  else c2 += -(v[u] - c1 + c2);
  f1[u] = c1, f2[u] = c2;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for(int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  for(int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  dfs(1, -1);
  cout << f1[1] + f2[1] << endl;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int val[100008];

struct edge{
    int u,v,nxt;
}edges[200008];
int nedge;
int head[100008];
bool vis[100008];
long long add[100008];
long long sub[100008];

void add_edge(int u,int v){
    edges[nedge].u=u;
    edges[nedge].v=v;
    edges[nedge].nxt=head[u];
    head[u]=nedge++;
}

void dfs(int u){
    int v;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edges[i].nxt){
        v=edges[i].v;
        if(vis[v]) continue;
        dfs(v);
//        cout<<v<<" "<<add[v]<<" "<<sub[v]<<endl;
        add[u]=max(add[u],add[v]);
        sub[u]=max(sub[u],sub[v]);
    }
    long long tmp=val[u]+add[u]-sub[u];
    if(tmp<0) add[u]+=(-tmp);
    else sub[u]+=tmp;
}

int main(){
    memset(head,-1,sizeof head);
    nedge=0;
    scanf("%d",&n);
    int u,v;
    for(int i=1;i<n;i++){
        scanf("%d %d",&u,&v);
        add_edge(u,v);
        add_edge(v,u);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&val[i]);
        add[i]=sub[i]=0;
    }
    dfs(1);
    printf("%I64d\n",add[1]+sub[1]);
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int MAX=100005;
vector<int> edge[MAX];
long long val[MAX];
long long add[MAX];
long long sub[MAX];
int father[MAX];
void dfs(int a){
    //cout<<a<<endl;
    if(edge[a].size()==1&&a!=1){
        if(val[a]>0){
            sub[a]=val[a];
        }
        else{
            add[a]=-val[a];
        }
        return ;
    }
    for(int i=0;i<edge[a].size();i++){
        if(edge[a][i]!=father[a]){
            father[edge[a][i]]=a;
            dfs(edge[a][i]);
            add[a]=max(add[a],add[edge[a][i]]);
            sub[a]=max(sub[a],sub[edge[a][i]]);
        }
    }
    val[a]-=sub[a];
    val[a]+=add[a];
    if(val[a]>0){
        sub[a]+=val[a];

    }else{
        add[a]+=-val[a];
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    for(int i=1;i<=n;i++){
        cin>>val[i];
    }
    if(n==1){
        cout<<abs(val[1])<<endl;
    }
    dfs(1);
    //cout<<father[1]<<" "<<father[2]<<" "<<father[3]<<" "<<endl;
    cout<<add[1]+sub[1]<<endl;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mp make_pair
#define go(i,n) for(int i=0;i<n;i++)
#define go3(i,j,n) for(int i=j;i<n;i++)

vector<vector<int> > g;
ll a[100005];
ll t[100005];
int used[100005];

ll ans=0;

void sh(int n){
   go(i,n) cout<<" ";
}

ll dfs(int v,int cnt=0){

 //sh(cnt); printf("v=%d\n",v);
 used[v] = 1;

   ll m=0;
   vi pos, neg;
   pos.pb(0); neg.pb(0);
   go(i,sz(g[v])) if(!used[g[v][i]]){

     int to=g[v][i];
     m=dfs(to,cnt+1);
     if(m>0) neg.pb(m);
     else pos.pb(-m);
   }

   //sort(all(pos));
   //sort(all(neg));

   /*sh(cnt); printf("pos:\n");
   sh(cnt); go(i,sz(pos)) cout<<pos[i]<<" "; cout<<endl;
   sh(cnt); printf("neg:\n");
   sh(cnt); go(i,sz(neg)) cout<<neg[i]<<" "; cout<<endl;

   ll t1=0,t2=0;
   go3(i,1,sz(pos)) t1+=pos[i]-pos[i-1];

   go3(i,1,sz(neg)) t2+=neg[i]-neg[i-1];
   */

   ll t1,t2;

   t1=*max_element(all(pos));
   t2=*max_element(all(neg));

   //sh(cnt);  printf("t1=%I64d\n",t1);
   //sh(cnt);  printf("t2=%I64d\n",t2);

  //sh(cnt); printf("ret=%I64d\n",-(t1+t2+a[v]));

   ans=t1+t2+abs(t2-t1+a[v]);
   //go(i,sz(pos)-2) ans-=pos[i];
   //go(i,sz(pos)-2) ans-=neg[i];
   //sh(cnt); printf("ans=%I64d\n",ans);
   return -(t1+t2+a[v]);

}

void oku(){

int n;
int x,y;

scanf("%d",&n);
g.assign(n,vi(0));

memset(t,0,sizeof t);
memset(used,0,sizeof used);
memset(a,0,sizeof a);

go(i,n-1){
  scanf("%d%d",&x,&y);
  x--; y--;
  g[x].pb(y);
  g[y].pb(x);
}

go(i,n) scanf("%I64d",&a[i]);

 dfs(0);

 cout<<ans;

}

int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("A.out","w",stdout);
#endif

oku();

return 0;}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mp make_pair
#define go(i,n) for(int i=0;i<n;i++)
#define go3(i,j,n) for(int i=j;i<n;i++)

vector<vector<int> > g;
ll a[100005];
ll t[100005];
int used[100005];

ll ans=0;

void sh(int n){
   go(i,n) cout<<" ";
}

pair<ll,ll> dfs(int v,int cnt=0){

 //sh(cnt); printf("v=%d\n",v);
 used[v] = 1;

   pair<ll,ll> m;

   ll neg=0,pos=0;

   go(i,sz(g[v])) if(!used[g[v][i]]){

     int to=g[v][i];
     m=dfs(to,cnt+1);

     neg=min(neg,m.first);
     pos=max(pos,m.second);
   }

   //sh(cnt); cout<<neg<<" "<<pos<<endl;

   ll res = neg+pos+a[v];

   //sh(cnt); printf("res=%I64d\n",res);

   if(res>0) neg -= res;
   if(res<0) pos -= res;

   //sh(cnt); printf("ret = %I64d %I64d\n",neg,pos);

   return mp(neg,pos);

}

void oku(){

int n;
int x,y;

scanf("%d",&n);
g.assign(n,vi(0));

memset(t,0,sizeof t);
memset(used,0,sizeof used);
memset(a,0,sizeof a);

go(i,n-1){
  scanf("%d%d",&x,&y);
  x--; y--;
  g[x].pb(y);
  g[y].pb(x);
}

go(i,n) scanf("%I64d",&a[i]);

 pair<ll,ll> m = dfs(0);

 cout<< -m.first + m.second + abs(m.first+m.second+a[0]);

}

int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("A.out","w",stdout);
#endif

oku();

return 0;}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mp make_pair
#define go(i,n) for(int i=0;i<n;i++)
#define go3(i,j,n) for(int i=j;i<n;i++)

vector<vector<int> > g;
ll a[100005];
ll t[100005];
int used[100005];

ll ans=0;

ll dfs(int v){

 //printf("v=%d\n",v);
 used[v] = 1;

   ll m=0;
   go(i,sz(g[v])) if(!used[g[v][i]]){

     int to=g[v][i];
     m+=dfs(to);
   }

  //printf("ret=%I64d\n",-a[v]+m);
  ans+=abs(a[v]+m);
   return -(a[v] + m);

}

void oku(){

int n;
int x,y;

scanf("%d",&n);
g.assign(n,vi(0));

memset(t,0,sizeof t);
memset(used,0,sizeof used);
memset(a,0,sizeof a);

go(i,n-1){
  scanf("%d%d",&x,&y);
  x--; y--;
  g[x].pb(y);
  g[y].pb(x);
}

go(i,n) scanf("%I64d",&a[i]);

 dfs(0);

 cout<<ans;

}

int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("A.out","w",stdout);
#endif

oku();

return 0;}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mp make_pair
#define go(i,n) for(int i=0;i<n;i++)
#define go3(i,j,n) for(int i=j;i<n;i++)

vector<vector<int> > g;
ll a[100005];
ll t[100005];
int used[100005];

ll ans=0;

void sh(int n){
   go(i,n) cout<<" ";
}

ll dfs(int v,int cnt=0){

 //sh(cnt); printf("v=%d\n",v);
 used[v] = 1;

   ll m=0;
   vi pos, neg;
   pos.pb(0); neg.pb(0);
   go(i,sz(g[v])) if(!used[g[v][i]]){

     int to=g[v][i];
     m=dfs(to,cnt+1);
     if(m>0) neg.pb(m);
     else pos.pb(-m);
   }

   //sort(all(pos));
   //sort(all(neg));

   /*sh(cnt); printf("pos:\n");
   sh(cnt); go(i,sz(pos)) cout<<pos[i]<<" "; cout<<endl;
   sh(cnt); printf("neg:\n");
   sh(cnt); go(i,sz(neg)) cout<<neg[i]<<" "; cout<<endl;

   ll t1=0,t2=0;
   go3(i,1,sz(pos)) t1+=pos[i]-pos[i-1];

   go3(i,1,sz(neg)) t2+=neg[i]-neg[i-1];
   */

   ll t1,t2;

   t1=*max_element(all(pos));
   t2=*max_element(all(neg));

   //sh(cnt);  printf("t1=%I64d\n",t1);
   //sh(cnt);  printf("t2=%I64d\n",t2);

  //sh(cnt); printf("ret=%I64d\n",-(t1+t2+a[v]));

   ans=t1+t2+abs(t2-t1+a[v]);
   //go(i,sz(pos)-2) ans-=pos[i];
   //go(i,sz(pos)-2) ans-=neg[i];
   //sh(cnt); printf("ans=%I64d\n",ans);
   return -(t1+t2+a[v]);

}

void oku(){

int n;
int x,y;

scanf("%d",&n);
g.assign(n,vi(0));

memset(t,0,sizeof t);
memset(used,0,sizeof used);
memset(a,0,sizeof a);

go(i,n-1){
  scanf("%d%d",&x,&y);
  x--; y--;
  g[x].pb(y);
  g[y].pb(x);
}

go(i,n) scanf("%I64d",&a[i]);

 //dfs(0);

 ll pos,neg=0;
 go(i,n)
  if(a[i]<0) pos=max(pos,-a[i]);
 go(i,n)
  if(a[i]>0) neg=max(neg,a[i]);

 cout<<pos+neg+abs(a[0]) + abs(neg-pos);

}

int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("A.out","w",stdout);
#endif

oku();

return 0;}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mp make_pair
#define go(i,n) for(int i=0;i<n;i++)
#define go3(i,j,n) for(int i=j;i<n;i++)

vector<vector<int> > g;
ll a[100005];
ll t[100005];
int used[100005];

ll ans=0;

void sh(int n){
   go(i,n) cout<<" ";
}

pair<ll,ll> dfs(int v,int cnt=0){

 //sh(cnt); printf("v=%d\n",v);
 used[v] = 1;

   pair<ll,ll> m;

   ll neg=0,pos=0;

   go(i,sz(g[v])) if(!used[g[v][i]]){

     int to=g[v][i];
     m=dfs(to,cnt+1);

     neg=min(neg,m.first);
     pos=max(pos,m.second);
   }

   //sh(cnt); cout<<neg<<" "<<pos<<endl;

   ll res = neg+pos+a[v];

   if(res>0) neg = min(neg,-res) ; else
   if(res<0) pos = max(pos,-res);

   //sh(cnt); printf("ret = %I64d %I64d\n",neg,pos);

   return mp(neg,pos);

}

void oku(){

int n;
int x,y;

scanf("%d",&n);
g.assign(n,vi(0));

memset(t,0,sizeof t);
memset(used,0,sizeof used);
memset(a,0,sizeof a);

go(i,n-1){
  scanf("%d%d",&x,&y);
  x--; y--;
  g[x].pb(y);
  g[y].pb(x);
}

go(i,n) scanf("%I64d",&a[i]);

 pair<ll,ll> m = dfs(0);

 cout<< -m.first + m.second + abs(m.first+m.second+a[0]);

}

int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("A.out","w",stdout);
#endif

oku();

return 0;}


#include <bits/stdc++.h>
using namespace std;
#define N 100009
#define INF 100000000000000007
#define MOD 100000000000000007
typedef long long int ll;
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pf(x) printf("%d",x);
#define pf2(x,y) printf("%d %d\n",x,y);
#define pf3(x,y,z) printf("%d %d %d\n",x,y,z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define tr(x) cout<<x << endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(x,y,z,a) cout<<x<<" "<<y<<" "<<z<<  " " << a << " " << endl;
int c[N],a[N];
ll val=0,ans=INF;
vector<int> v[N];
void dfs(int i,int p){
    a[i]+=abs(c[i]-c[p])+a[p];
    for(auto j:v[i]){
        if(j==p)continue;
        dfs(j,i);
    }
    if(v[i].size()==1){val+=a[i];}
}
int main(){
    int n,i,j,m,k,x,y,z;
    sd(n);
    for(i=0; i<n-1; i++){
        sd2(x,y);
        v[x].pb(y);
        v[y].pb(x);
    }
    for(i=1; i<=n; i++){
        sd(c[i]);
    }
    dfs(1,0);
    for(auto j:v[1]){
        ans=min(ans,val-abs(c[1])+abs(c[1]-c[j]));
    }
    tr(ans);
}


#include <bits/stdc++.h>
using namespace std;
#define N 100009
#define INF 100000000000000007
#define MOD 100000000000000007
typedef long long int ll;
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pf(x) printf("%d",x);
#define pf2(x,y) printf("%d %d\n",x,y);
#define pf3(x,y,z) printf("%d %d %d\n",x,y,z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define tr(x) cout<<x << endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(x,y,z,a) cout<<x<<" "<<y<<" "<<z<<  " " << a << " " << endl;
int c[N],a[N];
ll val=0,ans=INF;
vector<int> v[N];
void dfs(int i,int p){
    a[i]=abs(c[i]-c[p])+a[p];
    for(auto j:v[i]){
        if(j==p)continue;
        dfs(j,i);
    }
    if(v[i].size()==1){a[i]+=abs(c[i]);val+=a[i];}
  //  tr2(i,a[i]);
}
int main(){
    int n,i,j,m,k,x,y,z;
    sd(n);
    for(i=0; i<n-1; i++){
        sd2(x,y);
        v[x].pb(y);
        v[y].pb(x);
    }
    for(i=1; i<=n; i++){
        sd(c[i]);
        //tr2(i,c[i]);
    }
    z=c[1];c[1]=0;
    dfs(1,0);
  // tr(val);
    for(auto j:v[1]){
        ans=min(ans,val-abs(c[j])+abs(z-c[j]));
    }
    tr(ans);
}


#include <bits/stdc++.h>
using namespace std;
#define N 100009
#define INF 100000000000000007
#define MOD 100000000000000007
typedef long long int ll;
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pf(x) printf("%d",x);
#define pf2(x,y) printf("%d %d\n",x,y);
#define pf3(x,y,z) printf("%d %d %d\n",x,y,z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define tr(x) cout<<x << endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(x,y,z,a) cout<<x<<" "<<y<<" "<<z<<  " " << a << " " << endl;
int c[N];
ll in[N],z,dc[N];
ll val=0,ans=INF;
vector<int> v[N];
void dfs(int i,int p){
     for(auto j:v[i]){
        if(j==p)continue;
        dfs(j,i);
        in[i]=max(in[i],in[j]);
        dc[i]=max(dc[i],dc[j]);
    }
    ll t=c[i]+in[i]-dc[i];
    if(t>0)dc[i]+=t;
    if(t<0)in[i]+=-t;
}
int main(){
    int n,i,j,m,k,x,y;
    sd(n);
    for(i=0; i<n-1; i++){
        sd2(x,y);
        v[x].pb(y);
        v[y].pb(x);
    }
    for(i=1; i<=n; i++){
        sd(c[i]);
        //tr2(i,c[i]);
    }
    dfs(1,0);
    tr(in[1]+dc[1]);
}


#include<bits/stdc++.h>

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define enddl '\n'
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define full(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define PI 3.14159265

const int MAXN = 1e5+5;

const int MOD = 1e9+7;

using namespace std;

int n;
vector<int> g[MAXN];
ll val[MAXN];
ll dp[MAXN];
ll ops[MAXN];

void dfs(int root, int par) {
	bool leaf = true;
	for(auto next : g[root]) {
		if(next != par) {
			leaf = false;
			dfs(next, root);
		}
	}
	if(leaf) {
		dp[root] = val[root];
	}
	else {
		// cout << "NON LEAF " << root << endl;
		ll maxPositive = -1;
		ll leastNegative = 1e9 + 10;
		for(auto next : g[root]) {
			if(next != par) {
				if(dp[next] >= 0) {
					maxPositive = max(maxPositive, dp[next]);
				}
				else {
					leastNegative = min(leastNegative, dp[next]);
				}
			}
		}
		// cout << "MAXPOS " << maxPositive << endl;
		// cout << "MINNEG " << leastNegative << endl;
		if(maxPositive != -1 && leastNegative != (1e9+10)) {
			dp[root] = val[root] + (-leastNegative - maxPositive);
			ops[root] = abs(leastNegative) + maxPositive;
		}
		else if(maxPositive != -1) {
			dp[root] = val[root] - maxPositive;
			ops[root] = maxPositive;
		}
		else if(leastNegative != (1e9+10)) {
			dp[root] = val[root] - leastNegative;
			ops[root] = abs(leastNegative);
		}
	}
}

int main() {
	cin >> n;
	for(int i=1;i<=n-1;i++) {
		int x,y;
		cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i=1;i<=n;i++) {
		cin >> val[i];
	}
	if(n == 1) {
		cout << abs(val[1]) << endl;
		return 0;
	}
	dfs(1, -1);
	cout << ops[1] + abs(dp[1]) << endl;
	// for(int i=1;i<=n;i++) {
	// 	cout << ops[i] << ' ';
	// }

}