#include <bits/stdc++.h>
using namespace std;
#define MN 10000
#define MOD 10000
#define mp( a ,  b ) make_pair( ( a ), ( b ) )
#define pb( a ) push_back( a )
#define All( a ) a.begin(), a.end()
#define fi first
#define se second
int check(int a[][101], int x, int y)
{
    if(!a[x-1][y]&&!a[x+1][y]&&!a[x][y-1]&&!a[x][y+1])
        return 1;
    return 0;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int a[101][101];
    memset(a, 0, sizeof(a));
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            if(check(a,i,j))
            {
                a[i][j] = 1;
                cnt++;
            }
            if (cnt == m) break;
        }
        if (cnt == m) break;
    }
    if (cnt == m)
      {
  for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
            printf("%c", a[i][j] == 1 ? 'L' : 'S');
        cout << endl;
    }
      }
      else cout << "#include <bits/stdc++.h>
using namespace std;
#define MN 10000
#define MOD 10000
#define mp( a ,  b ) make_pair( ( a ), ( b ) )
#define pb( a ) push_back( a )
#define All( a ) a.begin(), a.end()
#define fi first
#define se second
int check(int a[][101], int x, int y)
{
    if(!a[x-1][y]&&!a[x+1][y]&&!a[x][y-1]&&!a[x][y+1])
        return 1;
    return 0;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int a[101][101];
    memset(a, 0, sizeof(a));
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            if(check(a,i,j))
            {
                a[i][j] = 1;
                cnt++;
            }
            if (cnt == m) break;
        }
        if (cnt == m) break;
    }
    if (cnt == m)
      {
  for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
            printf("%c", a[i][j] == 1 ? 'L' : 'S');
        cout << endl;
    }
      }
      else cout << "NO" << endl;
    return 0;
}
" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MN 10000
#define MOD 10000
#define mp( a ,  b ) make_pair( ( a ), ( b ) )
#define pb( a ) push_back( a )
#define All( a ) a.begin(), a.end()
#define fi first
#define se second

int check(int a[][101], int x, int y)
{
    if(!a[x-1][y]&&!a[x+1][y]&&!a[x][y-1]&&!a[x][y+1])
        return 1;
    return 0;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int a[101][101];
    memset(a, 0, sizeof(a));
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            if(check(a,i,j))
            {
                a[i][j] = 1;
                cnt++;
            }
            if (cnt == m) break;
        }
        if (cnt == m) break;
    }
    if (cnt == m)
      {
          cout << "YES" << endl;
          for (int i = 1; i <= n; i ++)
            {
                for (int j = 1; j <= n; j ++)
                    printf("%c", a[i][j] == 0 ? 'S' : 'L');
                cout << endl;
            }
      }
      else cout << "NO" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MN 10000
#define MOD 10000
#define mp( a ,  b ) make_pair( ( a ), ( b ) )
#define pb( a ) push_back( a )
#define All( a ) a.begin(), a.end()
#define fi first
#define se second

int check(int a[][200], int x, int y)
{
    if(!a[x-1][y]&&!a[x+1][y]&&!a[x][y-1]&&!a[x][y+1])
        return 1;
    return 0;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int a[200][200];
    memset(a, 0, sizeof(a));
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            if(check(a,i,j))
            {
                a[i][j] = 1;
                cnt++;
            }
            if (cnt == m) break;
        }
        if (cnt == m) break;
    }
    if (cnt == m)
      {
          cout << "YES" << endl;
          for (int i = 1; i <= n; i ++)
            {
                for (int j = 1; j <= n; j ++)
                    printf("%c", a[i][j] == 0 ? 'S' : 'L');
                cout << endl;
            }
      }
      else cout << "NO" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MN 10000
#define MOD 10000
#define mp( a ,  b ) make_pair( ( a ), ( b ) )
#define pb( a ) push_back( a )
#define All( a ) a.begin(), a.end()
#define fi first
#define se second

int check(int a[][101], int x, int y)
{
    if(!a[x-1][y]&&!a[x+1][y]&&!a[x][y-1]&&!a[x][y+1])
        return 1;
    return 0;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int a[101][101];
    memset(a, 0, sizeof(a));
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            if(check(a,i,j))
            {
                a[i][j] = 1;
                cnt++;
            }
            if (cnt == m) break;
        }
        if (cnt == m) break;
    }
    if (cnt == m)
      {
          cout << "YES" << endl;
          for (int i = 1; i <= n; i ++)
            {
                for (int j = 1; j <= n; j ++)
                    printf("%c", a[i][j] == 0 ? 'L' : 'S');
                cout << endl;
            }
      }
      else cout << "NO" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MN 10000
#define MOD 10000
#define mp( a ,  b ) make_pair( ( a ), ( b ) )
#define pb( a ) push_back( a )
#define All( a ) a.begin(), a.end()
#define fi first
#define se second

int check(int a[][102], int x, int y)
{
    if(!a[x-1][y]&&!a[x+1][y]&&!a[x][y-1]&&!a[x][y+1])
        return 1;
    return 0;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int a[102][102];
    memset(a, 0, sizeof(a));
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            if(check(a,i,j))
            {
                a[i][j] = 1;
                cnt++;
            }
            if (cnt == m) break;
        }
        if (cnt == m) break;
    }
    if (cnt == m)
      {
          cout << "YES" << endl;
          for (int i = 1; i <= n; i ++)
            {
                for (int j = 1; j <= n; j ++)
                    printf("%c", a[i][j] == 0 ? 'S' : 'L');
                cout << endl;
            }
      }
      else cout << "NO" << endl;
    return 0;
}


//AnotherHackyCodeBySmartCoder
//A.cpp
//May 11, 2017
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#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 minei(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxei(x)  max_element(x.begin(),x.end())-(x).begin()

#define uns(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acusum(x)  accumulate(x.begin(),x.end(),0)
#define acumul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define oo INT_MAX
#define inf 1000000000

const double pi = acos(-1.0);
const double eps = 1e-11;
#define MAXN 1000007
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
void fastIO() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int n, k;

int main() {
	fastIO();
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);
	cin >> n >> k;
	if (k > ((n * n) + 1) / 2) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (k > 0 && (i + j) % 2 == 0) {
				cout << 'L';
				k--;
			} else
				cout << 'S';

		}
		cout << endl;

	}

	return 0;
}


//AnotherHackyCodeBySmartCoder
//A.cpp
//May 11, 2017
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#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 minei(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxei(x)  max_element(x.begin(),x.end())-(x).begin()

#define uns(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acusum(x)  accumulate(x.begin(),x.end(),0)
#define acumul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define oo INT_MAX
#define inf 1000000000

const double pi = acos(-1.0);
const double eps = 1e-11;
#define MAXN 1000007
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
void fastIO() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
char mat[101][101];
int n, k;
void printIt() {
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cout << mat[i][j];
		}
		cout << endl;
	}
}
int main() {
	fastIO();
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);

	string st = "LS";
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			mat[i][j] = 'S';
	if (k == 0) {
		printIt();
		return 0;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			mat[i][j] = st[j % 2];
			if (mat[i][j] == 'L')
				k--;
			if (k == 0) {
				i = 10000;
				break;
			}

		}
		reverse(all(st));
	}
	if (k) {
		cout << "NO" << endl;
	} else {
		printIt();

	}
	return 0;
}


//AnotherHackyCodeBySmartCoder
//A.cpp
//May 11, 2017
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#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 minei(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxei(x)  max_element(x.begin(),x.end())-(x).begin()

#define uns(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acusum(x)  accumulate(x.begin(),x.end(),0)
#define acumul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define oo INT_MAX
#define inf 1000000000

const double pi = acos(-1.0);
const double eps = 1e-11;
#define MAXN 1000007
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
void fastIO() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int n, k;

int main() {
	fastIO();
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);
	cin >> n >> k;
	if (k > (n + 1) / 2 * (n + 1) / 2) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (k > 0 && (i + j) % 2 == 0) {
				cout << 'L';
				k--;
			} else
				cout << 'S';

		}
		cout << endl;

	}

	return 0;
}


//AnotherHackyCodeBySmartCoder
//A.cpp
//May 11, 2017
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#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 minei(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxei(x)  max_element(x.begin(),x.end())-(x).begin()

#define uns(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acusum(x)  accumulate(x.begin(),x.end(),0)
#define acumul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define oo INT_MAX
#define inf 1000000000

const double pi = acos(-1.0);
const double eps = 1e-11;
#define MAXN 1000007
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
void fastIO() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
char mat[101][101];
int main() {
	fastIO();
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);

	int n, k;
	string st = "LS";
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			mat[i][j] = 'S';

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			mat[i][j] = st[j % 2];
			if (mat[i][j] == 'L')
				k--;
			if (k == 0) {
				i = 10000;
				break;
			}

		}
		reverse(all(st));
	}
	if (k) {
		cout << "NO" << endl;
	} else {
		cout << "YES" << endl;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cout << mat[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}


//AnotherHackyCodeBySmartCoder
//A.cpp
//May 11, 2017
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#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 minei(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxei(x)  max_element(x.begin(),x.end())-(x).begin()

#define uns(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acusum(x)  accumulate(x.begin(),x.end(),0)
#define acumul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define oo INT_MAX
#define inf 1000000000

const double pi = acos(-1.0);
const double eps = 1e-11;
#define MAXN 1000007
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
void fastIO() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int n, k;

int main() {
	fastIO();
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);
	cin >> n >> k;
	if (k > (n * n) / 2) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (k > 0 && (i + j) % 2 == 0) {
				cout << 'L';
				k--;
			} else
				cout << 'S';

		}
		cout << endl;

	}

	return 0;
}


//AnotherHackyCodeBySmartCoder
//A.cpp
//May 11, 2017
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#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 minei(x)  min_element(x.begin(),x.end())-(x).begin()
#define maxei(x)  max_element(x.begin(),x.end())-(x).begin()

#define uns(v)     sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
#define acusum(x)  accumulate(x.begin(),x.end(),0)
#define acumul(x)  accumulate(x.begin(),x.end(),1, multiplies<int>());
#define bits(x)     __builtin_popcount( x )
#define oo INT_MAX
#define inf 1000000000

const double pi = acos(-1.0);
const double eps = 1e-11;
#define MAXN 1000007
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
void fastIO() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int n, k;

int main() {
	fastIO();
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);
	cin >> n >> k;
	if (k > (n) / 2 * (n + 1) / 2) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (k > 0 && (i + j) % 2 == 0) {
				cout << 'L';
				k--;
			} else
				cout << 'S';

		}
		cout << endl;

	}

	return 0;
}


# include<cstdio>
# include<iostream>
# include<cmath>

using namespace std;

# define MAX 123

char grid[MAX][MAX];

int main(void)
{
    int n,k;
    while ( scanf("%d%d",&n,&k)!=EOF )
    {
        int ans_max = pow(ceil(n*1.0/2),2)+pow(floor(n/2),2);
        if ( k > ans_max )
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        int cnt = 0;
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( i%2==j%2 && cnt < k)
                {
                    cnt++;
                    printf("L");
                }
                else
                    printf("S");
            }
            puts(" ");
        }

    }

    return 0;
}


# include<cstdio>
# include<iostream>
# include<cmath>

using namespace std;

# define MAX 123

char grid[MAX][MAX];

int main(void)
{
    int n,k,ans_max;
    while ( scanf("%d%d",&n,&k)!=EOF )
    {
        if ( n%2==1 )
            ans_max = (ceil(n*n/2.0));
        else
            ans_max = n*n/2;
        if ( k > ans_max )
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        int cnt = 0;
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( i%2==j%2 && cnt < k)
                {
                    cnt++;
                    printf("L");
                }
                else
                    printf("S");
            }
            puts(" ");
        }

    }

    return 0;
}


# include<cstdio>
# include<iostream>
# include<cmath>

using namespace std;

# define MAX 123

char grid[MAX][MAX];

int main(void)
{
    int n,k;
    while ( scanf("%d%d",&n,&k)!=EOF )
    {
        int ans_max = pow(ceil(n*1.0/2),2)+pow(floor(n/2)*floor(n/2),2);
        if ( k > ans_max )
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        int cnt = 0;
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( i%2==j%2 && cnt < k)
                {
                    cnt++;
                    printf("L");
                }
                else
                    printf("S");
            }
            puts(" ");
        }

    }

    return 0;
}


# include<cstdio>
# include<iostream>

using namespace std;

# define MAX 123

char grid[MAX][MAX];

int main(void)
{
    int n,k;
    while ( scanf("%d%d",&n,&k)!=EOF )
    {
        int ans_max = n*n/2;
        if ( k > ans_max )
        {
            printf("NO\n");
            continue;
        }

        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( ((i%2==1)&&(j%2==0))||((i%2==0)&&(j%2==1)) )
                {
                    grid[i][j] = 'L';
                }
                else
                {
                    grid[i][j] = 'S';
                }
            }
        }
        int cnt = 0, flag = 0;
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( grid[i][j]=='L' )
                {
                    cnt++;
                    grid[i][j] = 'S';
                }
                if ( cnt == ans_max-k )
                {
                    flag = 1;
                    break;
                }
            }
            if ( flag )
                break;
        }
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                printf("%c",grid[i][j]);
            }
            puts(" ");
        }
    }

    return 0;
}


# include<cstdio>
# include<iostream>

using namespace std;

# define MAX 123

char grid[MAX][MAX];

int main(void)
{
    int n,k;
    while ( scanf("%d%d",&n,&k)!=EOF )
    {
        int ans_max = n*n/2;
        if ( k > ans_max )
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( ((i%2==1)&&(j%2==0))||((i%2==0)&&(j%2==1)) )
                {
                    grid[i][j] = 'L';
                }
                else
                {
                    grid[i][j] = 'S';
                }
            }
        }
        int cnt = 0, flag = 0;
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( grid[i][j]=='L' )
                {
                    cnt++;
                    grid[i][j] = 'S';
                }
                if ( cnt == ans_max-k )
                {
                    flag = 1;
                    break;
                }
            }
            if ( flag )
                break;
        }
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                printf("%c",grid[i][j]);
            }
            puts(" ");
        }
    }

    return 0;
}


# include<cstdio>
# include<iostream>
# include<cmath>

using namespace std;

# define MAX 123

char grid[MAX][MAX];

int main(void)
{
    int n,k;
    while ( scanf("%d%d",&n,&k)!=EOF )
    {
        int ans_max = ceil(n*1.0/2)*ceil(n*1.0/2)+floor(n/2)*floor(n/2);
        if ( k > ans_max )
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( ((i%2==1)&&(j%2==0))||((i%2==0)&&(j%2==1)) )
                {
                    grid[i][j] = 'L';
                }
                else
                {
                    grid[i][j] = 'S';
                }
            }
        }
        int cnt = 0, flag = 0;
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                if ( grid[i][j]=='L' )
                {
                    cnt++;
                    grid[i][j] = 'S';
                }
                if ( cnt == ans_max-k )
                {
                    flag = 1;
                    break;
                }
            }
            if ( flag )
                break;
        }
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                printf("%c",grid[i][j]);
            }
            puts(" ");
        }
    }

    return 0;
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

	static long sx = 0, sy = 0, mod = (long) (1e9 + 7);

	// static ArrayList<pair>[] a;
	static ArrayList<pair>[] p;
	static int[][] dp;
	static long[] fa;
	static long[] farr;
	public static PrintWriter out = new PrintWriter(System.out);
	static ArrayList<pair> pa = new ArrayList<>();
	static long[] fact = new long[(int) 1e6];
	static StringBuilder sb = new StringBuilder();
	static boolean cycle = false;
	static long m = 998244353;
	static int[] no, col;
	static int k = 0, n = 0;
	static int[] a;
	static int[] time;
	static long[] ans;
	static TreeMap<Long, Integer> hm = new TreeMap<>();

	public static void main(String[] args) throws IOException {

		// Scanner scn = new Scanner(new BufferedReader(new
		// InputStreamReader(System.in)));

		Reader scn = new Reader();

		int n = scn.nextInt(), m = scn.nextInt(), b = scn.nextInt();

		mod = scn.nextLong();

		a = scn.nextIntArray(n);

		long[][][] dp = new long[2][m + 1][b + 1];

		dp[1][0][0] = 1L;

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

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

				for (int bu = 0; bu <= b; bu++) {

					int idx = i & 1;

					dp[idx][line][bu] = dp[1-idx][line][bu];

					if (line - 1 >= 0 && bu - a[i] >= 0)
						dp[idx][line][bu] += dp[idx][line - 1][bu - a[i]];

					dp[idx][line][bu] %= mod;
				}
			}
		}

		long ans = 0;

		for (int bug = 0; bug <= b; bug++) {
			ans += dp[(n - 1) & 1][m][bug];
			ans %= mod;
		}

		System.out.println(ans);

	}

	private static long f(int[] a, int i, int m, int b) {

		if (m == 0) {

			if (b >= 0)
				return 1;
			else
				return 0;
		}

		if (b < 0 || i == a.length)
			return 0;

		long ans = f(a, i, m - 1, b - a[i]);
		ans += f(a, i + 1, m, b);

		ans %= mod;

		return ans;
	}

	// _________________________TEMPLATE_____________________________________________________________

	// public static long lcm(long x, long y) {
	//
	// return (x * y) / gcd(x, y);
	// }
	//
	// private static long gcd(long x, long y) {
	// if (x == 0)
	// return y;
	//
	// return gcd(y % x, x);
	// }
	//
	// static class comp implements Comparator<Integer> {
	//
	// @Override
	// public int compare(Integer p1, Integer p2) {
	//
	// return p2 - p1;
	//
	// }
	// }
	//
	// }
	//
	// public static long pow(long a, long b) {
	//
	// if (b < 0)
	// return 0;
	// if (b == 0 || b == 1)
	// return (long) Math.pow(a, b);
	//
	// if (b % 2 == 0) {
	//
	// long ret = pow(a, b / 2);
	// ret = (ret % mod * ret % mod) % mod;
	// return ret;
	// }
	//
	// else {
	// return ((pow(a, b - 1) % mod) * a % mod) % mod;
	// }
	// }

	private static class pair implements Comparable<pair> {

		int v, coin, time;

		pair(int a, int b, int c) {

			v = a;
			coin = b;
			time = c;
		}

		@Override
		public int compareTo(pair o) {

			return 1;

		}

		// @Override
		//
		// public int hashCode() {
		// return i;
		// }
		//
		// @Override
		//
		// public boolean equals(Object o) {
		//
		// pair p = (pair) o;
		// return this.i == p.i;
		// }

	}

	private static class pair1 {

		int c, t;

		pair1(int a, int b) {
			c = a;
			t = b;
		}
	}

	private static String reverse(String s) {

		return new StringBuilder(s).reverse().toString();
	}

	public static class Reader {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException {
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String readLine() throws IOException {
			byte[] buf = new byte[1000000 + 1]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}

		public long nextLong() throws IOException {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (c == '.') {
				while ((c = read()) >= '0' && c <= '9') {
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException {
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public int[][] nextInt2DArray(int m, int n) throws IOException {
			int[][] arr = new int[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}

		public long[][] nextInt2DArrayL(int m, int n) throws IOException {
			long[][] arr = new long[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}
		// kickstart - Solution
		// atcoder - Main
	}

}


/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class WritingCode {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        int M = sc.nextInt();
        int B = sc.nextInt();
        long mod = sc.nextLong();
        int[] a = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            a[i] = sc.nextInt();
        }
        long[][][] dp = new long[2][M + 1][B + 1];
        int ptr = 0;
        dp[ptr][0][0] = 1;
        ///*
        //Slow method:
//        for (int i = 1; i <= N; i++) {
//            for (int lines = 0; lines <= M; lines++) {
//                for (int written = 0; written <= lines; written++) {
//                    int addedBugs = a[i] * written;
//                    for (int bugs = addedBugs; bugs <= B; bugs++) {
//                        dp[i][lines][bugs] += dp[i - 1][lines - written][bugs - addedBugs];
//                    }
//                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[i][lines]));
//            }
//        }
//        System.out.println("NEXT ONE");
         //*/
        for (int i = 1; i <= N; i++) {
            int nxt = 1 - ptr;
            for (int lines = 0; lines <= M; lines++) {
                Arrays.fill(dp[nxt][lines], 0);
            }
            for (int lines = 0; lines <= M; lines++) {
//                Arrays.fill(dp[nxt][lines], 0);
                if (lines == 0) {
                    // if we've written 0 lines, then there's only 1 way to write 0 bugs.
                    // the rest of the bug #'s will be impossible to reach
                    dp[nxt][lines][0] = 1;
                }
                else {
                    for (int bugs = 0; bugs <= B; bugs++) {
                        // we can do nothing,
                        dp[nxt][lines][bugs] = dp[ptr][lines][bugs];
                        if (bugs >= a[i]) {
                            // other wise, we can arrive to this bug count by stepping from a previous
                            // state, writing one line. We can do this for all residues of bugs from a previous state
                            if (bugs == a[i]) {
                                dp[nxt][lines][bugs] += dp[ptr][lines - 1][bugs - a[i]];
                                if (dp[nxt][lines][bugs] >= mod) {
                                    dp[nxt][lines][bugs] -= mod;
                                }
                            }
                            // Also, we can repeatedly combine a state we found via the build-method from a previous
                            // coder, by adding more and
                            if (bugs > a[i]) {
                                //AHHHH typo took 1 hour :'(
                                dp[nxt][lines][bugs] += dp[nxt][lines - 1][bugs - a[i]];
                                if (dp[nxt][lines][bugs] >= mod) {
                                    dp[nxt][lines][bugs] -= mod;
                                }
                            }
                        }
                    }
                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[nxt][lines]));
            }
            ptr = nxt;
        }
        long ans = 0;
        for (int bugs = 0; bugs <= B; bugs++) {
            ans += dp[ptr][M][bugs];
            if (ans >= mod) {
                ans -= mod;
            }
        }
        out.println(ans);
        out.close();
    }

    static class FastScanner {
        private int BS = 1<<16;
        private char NC = (char)0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}


/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class WritingCode {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        int M = sc.nextInt();
        int B = sc.nextInt();
        long mod = sc.nextLong();
        int[] a = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            a[i] = sc.nextInt();
        }
        long[][][] dp = new long[N + 1][M + 1][B + 1];
        dp[0][0][0] = 1;
        ///*
        //Slow method:
//        for (int i = 1; i <= N; i++) {
//            for (int lines = 0; lines <= M; lines++) {
//                for (int written = 0; written <= lines; written++) {
//                    int addedBugs = a[i] * written;
//                    for (int bugs = addedBugs; bugs <= B; bugs++) {
//                        dp[i][lines][bugs] += dp[i - 1][lines - written][bugs - addedBugs];
//                    }
//                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[i][lines]));
//            }
//        }
//        System.out.println("NEXT ONE");
         //*/
        for (int i = 1; i <= N; i++) {
            for (int lines = 0; lines <= M; lines++) {
                Arrays.fill(dp[i][lines], 0);
                if (lines == 0) {
                    // if we've written 0 lines, then there's only 1 way to write 0 bugs.
                    // the rest of the bug #'s will be impossible to reach
                    dp[i][lines][0] = 1;
                }
                else {
                    for (int bugs = 0; bugs <= B; bugs++) {
                        // we can do nothing,
                        dp[i][lines][bugs] = dp[i - 1][lines][bugs];
                        if (bugs >= a[i]) {
                            // other wise, we can arrive to this bug count by stepping from a previous
                            // state, writing one line. We can do this for all residues of bugs from a previous state
                            if (bugs == a[i]) {
                                dp[i][lines][bugs] += dp[i - 1][lines - 1][bugs - a[i]];
                            }
                            // Also, we can repeatedly combine a state we found via the build-method from a previous
                            // coder, by adding more and
                            if (bugs > a[i]) {
                                dp[i][lines][bugs] += dp[i][lines - 1][bugs - a[i]];
                            }
                            dp[i][lines][bugs] %= mod;
                        }
                    }
                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[i][lines]));
            }
        }
        long ans = 0;
        for (int bugs = 0; bugs <= B; bugs++) {
            ans += dp[N][M][bugs];
            ans %= mod;
        }
        out.println(ans);
        out.close();
    }

    static class FastScanner {
        private int BS = 1<<16;
        private char NC = (char)0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}


/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class WritingCode {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        int M = sc.nextInt();
        int B = sc.nextInt();
        long mod = sc.nextLong();
        int[] a = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            a[i] = sc.nextInt();
        }
        long[][][] dp = new long[2][M + 1][B + 1];
        int ptr = 0;
        dp[ptr][0][0] = 1;
        ///*
        //Slow method:
//        for (int i = 1; i <= N; i++) {
//            for (int lines = 0; lines <= M; lines++) {
//                for (int written = 0; written <= lines; written++) {
//                    int addedBugs = a[i] * written;
//                    for (int bugs = addedBugs; bugs <= B; bugs++) {
//                        dp[i][lines][bugs] += dp[i - 1][lines - written][bugs - addedBugs];
//                    }
//                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[i][lines]));
//            }
//        }
//        System.out.println("NEXT ONE");
         //*/
        for (int i = 1; i <= N; i++) {
            int nxt = 1 - ptr;
            for (int lines = 0; lines <= M; lines++) {
                Arrays.fill(dp[nxt][lines], 0);
            }
            for (int lines = 0; lines <= M; lines++) {
//                Arrays.fill(dp[nxt][lines], 0);
                if (lines == 0) {
                    // if we've written 0 lines, then there's only 1 way to write 0 bugs.
                    // the rest of the bug #'s will be impossible to reach
                    dp[nxt][lines][0] = 1;
                }
                else {
                    for (int bugs = 0; bugs <= B; bugs++) {
                        // we can do nothing,
                        dp[nxt][lines][bugs] = dp[ptr][lines][bugs];
                        if (bugs >= a[i]) {
                            // other wise, we can arrive to this bug count by stepping from a previous
                            // state, writing one line. We can do this for all residues of bugs from a previous state
//                            if (bugs == a[i]) {
//                                dp[nxt][lines][bugs] += dp[ptr][lines - 1][bugs - a[i]];
//                                if (dp[nxt][lines][bugs] >= mod) {
//                                    dp[nxt][lines][bugs] -= mod;
//                                }
//                            }
                            // Also, we can repeatedly combine a state we found via the build-method from a previous
                            // coder, by adding more and
                            if (bugs >= a[i]) {
                                //AHHHH typo took 1 hour :'(
                                dp[nxt][lines][bugs] += dp[nxt][lines - 1][bugs - a[i]];
                                if (dp[nxt][lines][bugs] >= mod) {
                                    dp[nxt][lines][bugs] -= mod;
                                }
                            }
                        }
                    }
                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[nxt][lines]));
            }
            ptr = nxt;
        }
        long ans = 0;
        for (int bugs = 0; bugs <= B; bugs++) {
            ans += dp[ptr][M][bugs];
            if (ans >= mod) {
                ans -= mod;
            }
        }
        out.println(ans);
        out.close();
    }

    static class FastScanner {
        private int BS = 1<<16;
        private char NC = (char)0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}


/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class WritingCode {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        int M = sc.nextInt();
        int B = sc.nextInt();
        long mod = sc.nextLong();
        int[] a = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            a[i] = sc.nextInt();
        }
        long[][][] dp = new long[2][M + 1][B + 1];
        int ptr = 0;
        dp[ptr][0][0] = 1;
        ///*
        //Slow method:
//        for (int i = 1; i <= N; i++) {
//            for (int lines = 0; lines <= M; lines++) {
//                for (int written = 0; written <= lines; written++) {
//                    int addedBugs = a[i] * written;
//                    for (int bugs = addedBugs; bugs <= B; bugs++) {
//                        dp[i][lines][bugs] += dp[i - 1][lines - written][bugs - addedBugs];
//                    }
//                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[i][lines]));
//            }
//        }
//        System.out.println("NEXT ONE");
         //*/
        for (int i = 1; i <= N; i++) {
            int nxt = 1 - ptr;
            for (int lines = 0; lines <= M; lines++) {
                Arrays.fill(dp[nxt][lines], 0);
            }
            for (int lines = 0; lines <= M; lines++) {
//                Arrays.fill(dp[nxt][lines], 0);
                if (lines == 0) {
                    // if we've written 0 lines, then there's only 1 way to write 0 bugs.
                    // the rest of the bug #'s will be impossible to reach
                    dp[nxt][lines][0] = 1;
                }
                else {
                    for (int bugs = 0; bugs <= B; bugs++) {
                        // we can do nothing,
                        dp[nxt][lines][bugs] = dp[ptr][lines][bugs];
                        if (bugs >= a[i]) {
                            // other wise, we can arrive to this bug count by stepping from a previous
                            // state, writing one line. We can do this for all residues of bugs from a previous state
                            if (bugs == a[i]) {
                                dp[nxt][lines][bugs] += dp[ptr][lines - 1][bugs - a[i]];
                            }
                            // Also, we can repeatedly combine a state we found via the build-method from a previous
                            // coder, by adding more and
                            if (bugs > a[i]) {
                                //AHHHH typo took 1 hour :'(
                                dp[nxt][lines][bugs] += dp[nxt][lines - 1][bugs - a[i]];
                            }
                            dp[nxt][lines][bugs] %= mod;
                        }
                    }
                }
//                System.out.println("dp of " + i + " " + lines + " : " +Arrays.toString(dp[nxt][lines]));
            }
            ptr = nxt;
        }
        long ans = 0;
        for (int bugs = 0; bugs <= B; bugs++) {
            ans += dp[ptr][M][bugs];
            ans %= mod;
        }
        out.println(ans);
        out.close();
    }

    static class FastScanner {
        private int BS = 1<<16;
        private char NC = (char)0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}


//#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define infll 0x3f3f3f3f3f3f3f3f
#define il inline
#define re register
#define pb push_back
#define db double
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define puu pair<ull,ull>
#define MP make_pair
#define lowbit(x) x&(-x)
#define fi first
#define se second
il ll read()
{
	char ch = getchar();
	ll p = 1,data = 0;
	while(ch<'0'||ch>'9')
	{
		if(ch == '-')p = -1;
		ch = getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		data = data*10+(ch^48);
		ch = getchar();
	}
	return p*data;
}
il ll qpow(ll a,ll b)
{
	ll r = 1;
	while(b)
	{
		if(b&1)r = a*r;
		a = a*a;
		b>>=1;
	}
	return r;
}
il ll gcd(ll a,ll b)
{
	while(b ^= a ^= b ^= a %= b);
	return a;
}
il ll lcm(ll a,ll b)
{
	return a*b/gcd(a,b);
}
void exgcd(ll a, ll b, ll &x,ll &y)
{
	if(!b) x = 1, y = 0;
	else
	{
		exgcd(b, a % b, y, x);
		y -= x * (a / b);
	}
}
const int maxn = 505;
ll a[maxn],dp[maxn][maxn][maxn];
int main()
{
	int n = read(),m = read(),b = read(),p = read();
	for(int i = 1; i <= n; i++)
		a[i] = read();
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= b; j++)
			dp[i][0][j] = 1;
	for(int i = 1; i <= n; i++)
		for(int k = 0; k <= m; k++)
			for(int j = 0; j <= b; j++)
			{
				dp[i][k][j] = dp[i-1][k][j] % p;
				if(j-a[i] >= 0 && k-1 >= 0)
					(dp[i][k][j] += dp[i][k-1][j-a[i]]) %= p;
			}
	printf("%lld\n",dp[n][m][b]);
	return 0;
}
/*
����dp[i][k][j]Ϊǰi����дk�����д��j��bug�ķ�������
*/


//#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define infll 0x3f3f3f3f3f3f3f3f
#define il inline
#define re register
#define pb push_back
#define db double
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define puu pair<ull,ull>
#define MP make_pair
#define lowbit(x) x&(-x)
#define fi first
#define se second
il ll read()
{
	char ch = getchar();
	ll p = 1,data = 0;
	while(ch<'0'||ch>'9')
	{
		if(ch == '-')p = -1;
		ch = getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		data = data*10+(ch^48);
		ch = getchar();
	}
	return p*data;
}
il ll qpow(ll a,ll b)
{
	ll r = 1;
	while(b)
	{
		if(b&1)r = a*r;
		a = a*a;
		b>>=1;
	}
	return r;
}
il ll gcd(ll a,ll b)
{
	while(b ^= a ^= b ^= a %= b);
	return a;
}
il ll lcm(ll a,ll b)
{
	return a*b/gcd(a,b);
}
void exgcd(ll a, ll b, ll &x,ll &y)
{
	if(!b) x = 1, y = 0;
	else
	{
		exgcd(b, a % b, y, x);
		y -= x * (a / b);
	}
}
const int maxn = 505;
ll a[maxn],dp[2][maxn][maxn];
int main()
{
	int n = read(),m = read(),b = read(),p = read();
	for(int i = 1; i <= n; i++)
		a[i] = read();
	for(int i = 0; i <= 1; i++)
		for(int j = 0; j <= b; j++)
			dp[i][0][j] = 1;
	for(int i = 1; i <= n; i++)
		for(int k = 0; k <= m; k++)
			for(int j = 0; j <= b; j++)
			{
				dp[i&1][k][j] = dp[1-(i&1)][k][j] % p;
				if(j-a[i] >= 0 && k-1 >= 0)
					(dp[i&1][k][j] += dp[i&1][k-1][j-a[i]]) %= p;
			}
	printf("%lld\n",dp[n&1][m][b]);
	return 0;
}
/*
����dp[i][k][j]Ϊǰi����дk�����д��j��bug�ķ�������
*/


#include<bits/stdc++.h>
typedef long long int;
using namespace std;

int main() {
	int n,m,b,mod; cin >> n >> m >> b >> mod;
	int a[n+1];
	for (int i=1; i<=n; ++i) cin >> a[i];

	int f[m+1][b+1];
	f[0][0] = 1;
	for (int i=1; i <= n; ++i)
		for (int j=1; j<=m; ++j)
			for (int k=a[i]; k <= b; ++k) {
				f[j][k] += f[j-1][k-a[i]];
				f[j][k] %= mod;
			}
	int ans = 0;
	for (int i=0; i<=b; ++i) {
		ans += f[m][i];
		ans %= mod;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	int n,m,b,mod; cin >> n >> m >> b >> mod;
	int a[n+1];
	for (int i=1; i<=n; ++i) cin >> a[i];

	int f[m+1][b+1];
	f[0][0] = 1;
	for (int i=1; i <= n; ++i)
		for (int j=1; j<=m; ++j)
			for (int k=a[i]; k <= b; ++k) {
				f[j][k] += f[j-1][k-a[i]];
				f[j][k] %= mod;
			}
	int ans = 0;
	for (int i=0; i<=b; ++i) {
		ans += f[m][i];
		ans %= mod;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	int n,m,b,mod; cin >> n >> m >> b >> mod;
	int a[n+1];
	for (int i=1; i<=n; ++i) cin >> a[i];

	int f[510][510];
	f[0][0] = 1;
	for (int i=1; i <= n; ++i)
		for (int j=1; j<=m; ++j)
			for (int k=a[i]; k <= b; ++k) {
				f[j][k] += f[j-1][k-a[i]];
				f[j][k] %= mod;
			}
	int ans = 0;
	for (int i=0; i<=b; ++i) {
		ans += f[m][i];
		ans %= mod;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	int n,m,b,mod; cin >> n >> m >> b >> mod;
	int a[n+1];
	for (int i=1; i<=n; ++i) cin >> a[i];

	int f[501][501];
	f[0][0] = 1;
	for (int i=1; i <= n; ++i)
		for (int j=1; j<=m; ++j)
			for (int k=a[i]; k <= b; ++k) {
				f[j][k] += f[j-1][k-a[i]];
				f[j][k] %= mod;
			}
	int ans = 0;
	for (int i=0; i<=b; ++i) {
		ans += f[m][i];
		ans %= mod;
	}
	cout << ans << endl;
}


#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <stack>
#include <cassert>
#include <queue>
#include <deque>
#include <climits>
#include <cstring>
#include <random>
#include <bitset>

using namespace std;

int mod;
int n, m, b;
vector<int> a;

int add(int a, int b) {
    return (a % mod + b % mod) % mod;
}

vector<vector<vector<int>>> dp;

int get_dp(int str, int bugs, int prog) {
    if (dp[str][bugs][prog] == -1) {
        int ans = 0;
        if (bugs >= a[prog] && str > 0) {
            ans = add(ans, get_dp(str - 1, bugs - a[prog], prog));
        }
        if (prog > 0) {
            ans = add(ans, get_dp(str, bugs, prog - 1));
        }
        dp[str][bugs][prog] = ans;
    }
    return dp[str][bugs][prog];
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    #ifdef LOCAL
    assert(freopen("input.txt", "r", stdin));
    assert(freopen("output.txt", "w", stdout));
    #endif

    cin >> n >> m >> b >> mod;

    a.resize(n);
    for (auto &t : a) {
        cin >> t;
    }

    vector<vector<int>> prev(b + 1, vector<int> (n, 0));
    for (int i = 0; i < n; i++) {
        prev[0][i] = 1;
    }

    vector<vector<int>> cur;
    for (int str = 1; str <= m; str++) {
        cur.clear();
        cur.assign(b + 1, vector<int> (n, -1));
        for (int bugs = 0; bugs <= b; bugs++) {
            for (int prog = 0; prog < n; prog++) {
                int ans = 0;

                if (bugs >= a[prog]) {
                    ans = add(ans, prev[bugs - a[prog]][prog]);
                }
                if (prog > 0) {
                    ans = add(ans, cur[bugs][prog - 1]);
                }
                cur[bugs][prog] = ans;
            }
        }
        prev = cur;
    }
    int res = 0;
    for (int i = 0; i <= b; i++) {
        res = add(res, cur[i][n - 1]);
    }
    cout << res << '\n';

}


#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <stack>
#include <cassert>
#include <queue>
#include <deque>
#include <climits>
#include <cstring>
#include <random>
#include <bitset>

using namespace std;

int mod;
int n, m, b;
vector<int> a;

int add(int a, int b) {
    return (a % mod + b % mod) % mod;
}

vector<vector<vector<int>>> dp;

int get_dp(int str, int bugs, int prog) {
//    cerr << str << ' ' << bugs << ' ' << prog << '\n';
    if (dp[str][bugs][prog] == -1) {
        int ans = 0;
        if (bugs >= a[prog] && str > 0) {
            ans = add(ans, get_dp(str - 1, bugs - a[prog], prog));
        }
        if (prog > 0) {
            ans = add(ans, get_dp(str, bugs, prog - 1));
        }
        dp[str][bugs][prog] = ans;
    }
    return dp[str][bugs][prog];
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    #ifdef LOCAL
    assert(freopen("input.txt", "r", stdin));
    assert(freopen("output.txt", "w", stdout));
    #endif

    cin >> n >> m >> b >> mod;
    //dp[str][bugs][prog]
    dp.resize(m + 1, vector<vector<int>> (b + 1, vector<int> (n + 1, -1)));
    dp[0][0][0] = 1;

    a.resize(n);
    for (auto &t : a) {
        cin >> t;
    }

    int res = 0;
    for (int i = 0; i <= b; i++) {
        res = add(res, get_dp(m, i, n - 1));
    }
    cout << res << '\n';
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll mod=1e9+7;
ll mod1=998244351;
ll power(ll a,ll b)
{
    if(b==0) return 1;
    else if(b%2==0)
        return power(a*a%mod,b/2)%mod;
    else return (a%mod*power(a*a%mod,b/2))%mod;
}

ll bit(ll x)
{
    return __builtin_popcountll(x);
}
#define endl '\n'

const int N = 505;
int a[N];
int z[2][N][N];
#define forn(i, n) for(int i = 0; i < int(n); i++)

int main()
{
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

          int n, bl, bugs, md;
  scanf("%d %d %d %d", &n, &bl, &bugs, &md);
  forn(i, n) scanf("%d", &a[i]);
  z[0][0][0] = 1;

  for(int it = 1; it <= n; it++){
    int i = it & 1;
    for(int j = 0; j <= bl; j++) {
      for(int k = 0; k <= bugs; k++) {
        z[i][j][k] = z[i ^ 1][j][k];
        if (j > 0 && k >= a[it - 1])
          z[i][j][k] += z[i][j - 1][k - a[it - 1]];
        while (z[i][j][k] >= md) z[i][j][k] -= md;
      }
    }
  }

  int ans = 0;
  for(int i = 0; i <= bugs; i++) {
    ans += z[n & 1][bl][i];
    while (ans >= md) ans -= md;
  }
  printf("%d\n", ans);

}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll mod=1e9+7;
ll mod1=998244351;
ll power(ll a,ll b)
{
    if(b==0) return 1;
    else if(b%2==0)
        return power(a*a%mod,b/2)%mod;
    else return (a%mod*power(a*a%mod,b/2))%mod;
}

ll bit(ll x)
{
    return __builtin_popcountll(x);
}
#define endl '\n'

const int N = 505;
int a[N];
int z[2][N][N];

int main()
{
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

          int n, bl, bugs, md;
  scanf("%d %d %d %d", &n, &bl, &bugs, &md);
  forn(i, n) scanf("%d", &a[i]);
  z[0][0][0] = 1;

  for(int it = 1; it <= n; it++){
    int i = it & 1;
    for(int j = 0; j <= bl; j++) {
      for(int k = 0; k <= bugs; k++) {
        z[i][j][k] = z[i ^ 1][j][k];
        if (j > 0 && k >= a[it - 1])
          z[i][j][k] += z[i][j - 1][k - a[it - 1]];
        while (z[i][j][k] >= md) z[i][j][k] -= md;
      }
    }
  }

  int ans = 0;
  for(int i = 0; i <= bugs; i++) {
    ans += z[n & 1][bl][i];
    while (ans >= md) ans -= md;
  }
  printf("%d\n", ans);

}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll mod=1e9+7;
ll mod1=998244351;
ll power(ll a,ll b)
{
    if(b==0) return 1;
    else if(b%2==0)
        return power(a*a%mod,b/2)%mod;
    else return (a%mod*power(a*a%mod,b/2))%mod;
}

ll bit(ll x)
{
    return __builtin_popcountll(x);
}
#define endl '\n'

ll dp[501][501][501];
int main()
{
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

        int n,m,b,mud;
        cin>>n>>m>>b>>mud;
        memset(dp,0,sizeof(dp));
        ll a[n+1];

        for(int i=0;i<n;++i)
            cin>>a[i+1];
            for(int i=0;i<=n;++i)
            for(int j=0;j<=b;++j)
            dp[i][0][j]=1;

        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;++j)
            {
                for(int bug=0;bug<=b;++bug)
                {
                    int r=0;
                    while((j-r)>=0 && bug>=r*a[i])
                   {
                   	dp[i][j][bug]=(dp[i][j][bug]%mud+dp[i-1][j-r][bug-r*a[i]]%mud)%mud;
                   	//cout<<r;
                   	r++;
                   }
                }
            }
        }
       // cout<<endl;
        cout<<dp[n][m][b];

}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define N 510
#define MOD 100000000
#define fn(i,n) for(int i=0;i<n;i+=1)
#define rep(i,l,r) for(int i=l;i<=r;i+=1)
#define INF 100000000000000
#define pii pair<int,int>
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n,m,b,mod;
int a[N]={0};
int dp[N][N];
int pre[N][N];
void fun(){

    int xd=0;
    rep(i,0,m){
        if(xd<=b)
        {
            dp[i][xd]=1;
            pre[i][xd]=1;
            xd+=a[1];
        }
    }

    rep(idx,2,n)
    {
        rep(i,1,m){
            rep(j,a[i],b)
            {
                dp[i][j]=(dp[i][j]+dp[i-1][j-a[idx]])%mod;
            }
        }

    }

}
signed main()
{

    speed
    cin>>n>>m>>b>>mod;
    rep(i,1,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    memset(pre,0,sizeof(pre));

    fun();

    int res=0;
    rep(k,0,b){
        res=(res+dp[m][k])%mod;
    }
    cout<<res<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define N 510
#define MOD 100000000
#define fn(i,n) for(int i=0;i<n;i+=1)
#define rep(i,l,r) for(int i=l;i<=r;i+=1)
#define INF 100000000000000
#define pii pair<int,int>
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n,m,b,mod;
int a[N]={0};
int dp[N][N];
int pre[N][N];
void fun(){

    int xd=0;
    rep(i,0,m){
        if(xd<=b)
        {
            dp[i][xd]=1;
            pre[i][xd]=1;
            xd+=a[1];
        }
    }

    rep(idx,2,n)
    {
        rep(i,1,m){
            rep(j,a[idx],b)
            {
                dp[i][j]=(dp[i][j]+dp[i-1][j-a[idx]])%mod;
            }
        }

    }

}
signed main()
{

    speed
    cin>>n>>m>>b>>mod;
    rep(i,1,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    memset(pre,0,sizeof(pre));

    fun();

    int res=0;
    rep(k,0,b){
        res=(res+dp[m][k])%mod;
    }
    cout<<res<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define N 510
#define MOD 100000000
#define fn(i,n) for(int i=0;i<n;i+=1)
#define rep(i,l,r) for(int i=l;i<=r;i+=1)
#define INF 100000000000000
#define pii pair<int,int>
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n,m,b,mod;
int a[N]={0};
int dp[N][N];
int pre[N][N];
void fun(){

    int xd=0;
    rep(i,0,m){
        if(xd<=b)
        {
            dp[i][xd]=1;
            pre[i][xd]=1;
            xd+=a[1];
        }
    }

    rep(idx,2,n)
    {
        rep(i,1,m){
            rep(j,a[i],b)
            {
                dp[i][j]+=(dp[i-1][j-a[idx]])%mod;
            }
        }

    }

}
signed main()
{

    speed
    cin>>n>>m>>b>>mod;
    rep(i,1,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    memset(pre,0,sizeof(pre));

    fun();

    int res=0;
    rep(k,0,b){
        res=(res+dp[m][k])%mod;
    }
    cout<<res<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define N 510
#define MOD 100000000
#define fn(i,n) for(int i=0;i<n;i+=1)
#define rep(i,l,r) for(int i=l;i<=r;i+=1)
#define INF 100000000000000
#define pii pair<int,int>
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n,m,b,mod;
int a[N]={0};
int dp[N][N][N];
void fun(){

    int xd=0;
    rep(i,0,m){
        if(xd<=b)
        {
            dp[1][i][xd]=1;
            xd+=a[1];
        }
    }

    rep(idx,2,n)
    {
        rep(i,0,m){
            rep(j,0,b)
            {
                dp[idx][i][j]=(dp[idx][i][j] + dp[idx-1][i][j])%mod;
                if(j>=a[idx]){
                    dp[idx][i][j]=(dp[idx][i][j]+dp[idx][i-1][j-a[idx]])%mod;
                }

            }
        }
    }

}
signed main()
{

    speed
    cin>>n>>m>>b>>mod;
    rep(i,1,n)cin>>a[i];
    memset(dp,0,sizeof(dp));

    fun();

    int res=0;
    rep(k,0,b){
        res=(res+dp[n][m][k])%mod;
    }
    cout<<res<<endl;

    return 0;
}


#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
//const ll mod=10007;//998244353;
const ll inf=1e18;
const ld pai=acos(-1);
ll p,l,mx,mod;
ll a[509];
ll dp[2][509][509];
int main(){
	cin>>p>>l>>mx>>mod;
	for(ll i=0;i<p;i++)cin>>a[i];
	dp[0][0][0]=1;
	for(ll i=0;i<p;i++){
		for(ll j=0;j<=l;j++){
			for(ll k=0;k<=mx;k++){
				dp[1][j][k]=dp[0][j][k];
				if(k-a[i]>=0 && j){
					dp[1][j][k]+=dp[1][j-1][k-a[i]];
				}
				dp[1][j][k]%=mod;
			}
		}
		for(ll j=0;j<=l;j++){
			for(ll k=0;k<=mx;k++){
				dp[0][j][k]=dp[1][j][k];
				dp[1][j][k]=0;
			}
		}
	}
	ll ans=0;
	for(ll i=0;i<=mx;i++)ans+=dp[0][l][i],ans%=mod;
	cout<<ans<<endl;
}


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
typedef long long ll;
inline int rd()
{
	register int x=0;
	register char c=getchar();
	register bool f=false;
	while(!isdigit(c))
	{
		f=(c=='-'),c=getchar();
	}
	while(isdigit(c))
	{
		x=x*10+c-48,c=getchar();
	}
	return f?-x:x;
}
int n,m,b,mod;
int a[510];
int dp[510][510];
int main()
{
	n=rd();
	m=rd();
	b=rd();
	mod=rd();
	for(int i=1;i<=n;i++)
	{
		a[i]=rd();
	}
	dp[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			for(int k=1;k<=b;k++)
			{
				if(k>=a[i])
				{
					dp[j][k]+=dp[j-1][k-a[i]];
					dp[j][k]%=mod;
				}
			}
		}
	}
	int ans=0;
	for(int i=0;i<=b;i++)
	{
		ans=(ans+dp[m][i])%mod;
	}
	cout<<ans;
}


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
typedef long long ll;
inline int rd()
{
	register int x=0;
	register char c=getchar();
	register bool f=false;
	while(!isdigit(c))
	{
		f=(c=='-'),c=getchar();
	}
	while(isdigit(c))
	{
		x=x*10+c-48,c=getchar();
	}
	return f?-x:x;
}
int n,m,b,mod;
int a[510];
int dp[510][510];
int main()
{
	n=rd();
	m=rd();
	b=rd();
	mod=rd();
	for(int i=1;i<=n;i++)
	{
		a[i]=rd();
	}
	dp[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			for(int k=b;k>=a[i];k--)
			{
				dp[j][k]+=dp[j-1][k-a[i]];
				dp[j][k]%=mod;
			}
		}
	}
	int ans=0;
	for(int i=0;i<=b;i++)
	{
		ans=(ans+dp[m][i])%mod;
	}
	cout<<ans;
}


#include <bits/stdc++.h>

using namespace std;
using lld = long long int;

int n, m, b, mod;

lld solve(int coder, int line, int bugs, vector<int> &V);
lld dp[500][500][500];

int main() {
    cin >> n >> m >> b >> mod;
    vector<int> V(n);
    for (int i = 0; i < n; i++) {
        cin >> V[i];
    }
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0, 0, V) << endl;
    return 0;
}

lld solve(int coder, int line, int bugs, vector<int> &V) {
    if (bugs > b)
        return 0;
    if (line == m) return 1;
    if (coder == n) return 0;
    if (dp[coder][line][bugs] != -1) return dp[coder][line][bugs];
    lld ans = (solve(coder, line+1, bugs+V[coder], V)+solve(coder+1, line, bugs, V))%mod;
    return dp[coder][line][bugs] = ans;
}


#include <bits/stdc++.h>

using namespace std;
using lld = long long int;

int n, m, b, mod;

lld solve(int coder, int line, int bugs, vector<int> &V);
lld dp[510][510];

int main() {
    cin >> n >> m >> b >> mod;
    vector<int> V(n);
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
        cin >> V[i];
        for (int alpha = 0; alpha < m; alpha++) {
            for (int beta = 0; beta <= b-V[i]; beta++) {
                dp[alpha+1][beta+V[i]] += dp[alpha][beta];
                dp[alpha+1][beta+V[i]] %= mod;
            }
        }
    }

    lld kapa = 0;
    for (int i = 0; i <= b; i++) {
        kapa += dp[m][i];
        kapa = kapa%mod;
    }
    cout << kapa << endl;
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

ll power(ll x,ll y){
    if(y==0)
        return 1;
    ll res=power(x,y/2);
    res=(res*res);
    if(y&1)
        res=(res*x);
    return res;
}
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}
bool isPrime(ll n){
    //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
    //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
    if(n<2)
        return false;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)return false;
    return true;
}
bool f;int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll su=-a[ch];
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    su+=m;
  }
  if(su<0){
    f=false;
  }
  return su;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[(int)u].pb(i);
  }

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

  ll l=0;u=(ll)2e14+1-(ll)5e13;
  while(l<u){
    ll m=(u+l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

ll power(ll x,ll y){
    if(y==0)
        return 1;
    ll res=power(x,y/2);
    res=(res*res);
    if(y&1)
        res=(res*x);
    return res;
}
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}
bool isPrime(ll n){
    //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
    //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
    if(n<2)
        return false;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)return false;
    return true;
}
bool f;int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll x=a[ch],su=0;
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    x=m-x;
  }
  else{
    x=su-x;
  }
  if(x<0){
    f=false;
  }
  return x;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[(int)u].pb(i);
  }

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

  ll l=0;u=(ll)2e14+1;
  while(l+1<u){
    ll m=l+(u-l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
typedef __int128 ll;
// const ll M = 1000000007;
// const ll M1= 998244353;
// using vl = vector<>;
// using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

// ll power(ll x,ll y){
//     if(y==0)
//         return 1;
//     ll res=power(x,y/2);
//     res=(res*res);
//     if(y&1)
//         res=(res*x);
//     return res;
// }
// ll powermod(ll x,ll y,ll mod){
//     if(y==0)
//         return 1;
//     ll res=powermod(x,y/2,mod);
//     res=(res*res)%mod;
//     if(y&1)
//         res=(res*x)%mod;
//     return res;
// }
// bool isPrime(ll n){
//     //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
//     //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
//     if(n<2)
//         return false;
//     for(ll i=3;i*i<=n;i+=2)
//         if(n%i==0)return false;
//     return true;
// }
bool f;
int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll su=-a[ch];
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    su+=m;
  }
  if(su<0){
    f=false;
  }
  return su;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<(ll)0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  long long int v;
  for(int i=2;i<=n;i++){
    cin>>v;
    tre[(int)v].pb(i);
  }

  for(int i=1;i<=n;i++){cin>>v;a[i]=v;}

  ll l=0,u=(ll)2e14+1;
  while(l<u){
    ll m=(u+l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<(long long int)l<<endl;
  else
    cout<<(long long int)u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

ll power(ll x,ll y){
    if(y==0)
        return 1;
    ll res=power(x,y/2);
    res=(res*res);
    if(y&1)
        res=(res*x);
    return res;
}
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}
bool isPrime(ll n){
    //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
    //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
    if(n<2)
        return false;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)return false;
    return true;
}
bool f;int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll x=a[ch],su=0;
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    x=m-x;
  }
  else{
    x=su-x;
  }
  if(x<0){
    f=false;
  }
  return x;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[(int)u].pb(i);
  }

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

  ll l=0;u=(ll)2e14+1;
  while(l<u){
    ll m=(u+l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

ll power(ll x,ll y){
    if(y==0)
        return 1;
    ll res=power(x,y/2);
    res=(res*res);
    if(y&1)
        res=(res*x);
    return res;
}
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}
bool isPrime(ll n){
    //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
    //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
    if(n<2)
        return false;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)return false;
    return true;
}
bool f;int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll x=a[ch],su=0;
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    x=m-x;
  }
  else{
    x=su-x;
  }
  if(x<0){
    f=false;
  }
  return x;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[(int)u].pb(i);
  }

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

  ll l=0;u=(ll)1e16;
  while(l+1<u){
    ll m=l+(u-l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
typedef __int128 ll;
// const ll M = 1000000007;
// const ll M1= 998244353;
// using vl = vector<>;
// using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

// ll power(ll x,ll y){
//     if(y==0)
//         return 1;
//     ll res=power(x,y/2);
//     res=(res*res);
//     if(y&1)
//         res=(res*x);
//     return res;
// }
// ll powermod(ll x,ll y,ll mod){
//     if(y==0)
//         return 1;
//     ll res=powermod(x,y/2,mod);
//     res=(res*res)%mod;
//     if(y&1)
//         res=(res*x)%mod;
//     return res;
// }
// bool isPrime(ll n){
//     //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
//     //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
//     if(n<2)
//         return false;
//     for(ll i=3;i*i<=n;i+=2)
//         if(n%i==0)return false;
//     return true;
// }
bool f;
int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll su=-a[ch];
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    su+=m;
  }
  if(su<0){
    f=false;
  }
  return su;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<(ll)0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[(int)u].pb(i);
  }

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

  ll l=0;u=(ll)2e14+1;
  while(l<u){
    ll m=(u+l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

ll power(ll x,ll y){
    if(y==0)
        return 1;
    ll res=power(x,y/2);
    res=(res*res);
    if(y&1)
        res=(res*x);
    return res;
}
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}
bool isPrime(ll n){
    //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
    //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
    if(n<2)
        return false;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)return false;
    return true;
}
bool f;int n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll su=-a[ch];
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    su+=m;
  }
  if(su<0){
    f=false;
  }
  return su;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[(int)u].pb(i);
  }

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

  ll l=0;u=(ll)2e14+1;
  while(l<u){
    ll m=(u+l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define debug(x) cout<<x<<endl
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define pi acos(-1)
using ll = long long;
const ll M = 1000000007;
const ll M1= 998244353;
using vl = vector<ll>;
using pll =  pair<ll,ll>;
// typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> is;

ll power(ll x,ll y){
    if(y==0)
        return 1;
    ll res=power(x,y/2);
    res=(res*res);
    if(y&1)
        res=(res*x);
    return res;
}
ll powermod(ll x,ll y,ll mod){
    if(y==0)
        return 1;
    ll res=powermod(x,y/2,mod);
    res=(res*res)%mod;
    if(y&1)
        res=(res*x)%mod;
    return res;
}
bool isPrime(ll n){
    //There is a fact that the distance between adjacent prime numbers isn't big. For n = 10^9 maximal distanse is 282.
    //https://stackoverflow.com/questions/31216097/given-n-and-k-return-the-kth-permutation-sequence
    if(n<2)
        return false;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)return false;
    return true;
}
int f,n;
vector<vector<int>> tre;
vector<ll> a;
ll dfs(int ch,ll m){
  ll x=a[ch],su=0;
  for(int i:tre[ch]){
    su+=dfs(i,m);
  }
  if(tre[ch].size()==0){
    x=m-x;
  }
  else{
    x=su-x;
  }
  if(x<0){
    f=false;x=0;
  }
  return x;
}
bool possible(ll m){
  f=true;
  ll x=dfs(1,m);
  if(x<0)
    f=false;
  return f;
}
void solve(){
  cin>>n;
  tre.resize(n+1);
  a.resize(n+1);
  ll u;
  for(int i=2;i<=n;i++){
    cin>>u;
    tre[u].pb(i);
  }

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

  ll l=0;u=(ll)1e17;
  while(l+1<u){
    ll m=l+(u-l)/2;
    if(possible(m))
      u=m;
    else
      l=m+1;
  }
  if(l>u)swap(l,u);
  if(possible(l))
    cout<<l<<endl;
  else
    cout<<u<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    {//cout<<"Case #"<<i<<": ";
    solve();}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int  long long

#define rep(i,a,b) for(int i = a; i < b; i++)

const int N = 2e5 + 5;

vector<int> v[N], a(N);
bool ok;

int dfs(int src, int mx) {
    //Check on each step whether req - available >= 0
    if(!ok) return 0;
    int avai = a[src];
    int req = 0;
    for(auto i : v[src]) {
        req += dfs(i, mx);
    }
    if(v[src].size() == 0) {
        if(mx - avai < 0) ok = false;
        return mx - avai;
    }
    if(req - avai < 0) ok = false;
    return req - avai;
}

int32_t main()
{
    IOS
    int n;
    cin >> n;
    rep(i,2,n+1) {
        int p;
        cin >> p;
        v[p].push_back(i);
    }
    rep(i,1,n + 1) {
        cin >> a[i];
    }
    int lo = 0, hi = 1e13;
    ok = true;
    while(lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        ok = true;
        dfs(1, mid);
        if(ok) {
            hi = mid - 1;
        } else lo = mid + 1;
    }
    cout << lo << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int  long long

#define rep(i,a,b) for(int i = a; i < b; i++)

const int N = 2e5 + 5;

vector<int> v[N], citizen(N);
int ans;

pair<int, int> dfs(int src) {
    int sm = citizen[src];
    int l = 0;
    for(auto i : v[src]) {
        pair<int, int> p = dfs(i);
        sm += p.first;
        l += p.second;
    }
    if(v[src].size() == 0) {
        l++;
    }
    ans = max(ans, (sm + l - 1) / l);
    return {sm, l};
}

int32_t main()
{
    IOS
    int n;
    cin >> n;
    rep(i,2,n+1) {
        int p;
        cin >> p;
        v[p].push_back(i);
    }
    rep(i,1,n+1) {
        cin >> citizen[i];
    }
    dfs(1);
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int  long long

#define rep(i,a,b) for(int i = a; i < b; i++)

const int N = 2e5 + 5;

vector<int> v[N], a(N);
bool ok;

int dfs(int src, int mx) {
    //Check on each step whether req - available >= 0
    if(!ok) return 0;
    int avai = a[src];
    int req = 0;
    for(auto i : v[src]) {
        req += dfs(i, mx);
    }
    if(v[src].size() == 0) {
        if(mx - avai < 0) ok = false;
        return mx - avai;
    }
    if(req - avai < 0) ok = false;
    return req - avai;
}

int32_t main()
{
    IOS
    int n;
    cin >> n;
    rep(i,2,n+1) {
        int p;
        cin >> p;
        v[p].push_back(i);
    }
    rep(i,1,n + 1) {
        cin >> a[i];
    }
    int lo = 0, hi = 1e14;
    ok = true;
    while(lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        ok = true;
        dfs(1, mid);
        if(ok) {
            hi = mid - 1;
        } else lo = mid + 1;
    }
    cout << lo << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int  long long

#define rep(i,a,b) for(int i = a; i < b; i++)

const int N = 2e5 + 5;

vector<int> v[N], a(N);
bool ok;

__int128 dfs(int src, int mx) {
    //Check on each step whether req - available >= 0
    if(!ok) return 0;
    __int128 avai = a[src];
    __int128 req = 0;
    for(auto i : v[src]) {
        req += dfs(i, mx);
    }
    if(v[src].size() == 0) {
        if(mx - avai < 0) ok = false;
        return mx - avai;
    }
    if(req - avai < 0) ok = false;
    return req - avai;
}

int32_t main()
{
    IOS
    int n;
    cin >> n;
    rep(i,2,n+1) {
        int p;
        cin >> p;
        v[p].push_back(i);
    }
    rep(i,1,n + 1) {
        cin >> a[i];
    }
    int lo = 0, hi = 1e18;
    ok = true;
    while(lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        ok = true;
        dfs(1, mid);
        if(ok) {
            hi = mid - 1;
        } else lo = mid + 1;
    }
    cout << lo << endl;
    return 0;
}