//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000006;

int n, m, d;
int dfs_clock, scc_cnt, top;
int dfn[maxn], low[maxn], st[maxn], sccno[maxn];
int num[maxn], c[maxn];
vector<int> G[maxn], F[maxn];
char s[maxn/50][52];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st[++top] = u;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st[top--];
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 1; i <= n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1') {
                num[sccno[(i-1)*d+j]]++;
            }
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
bool vis[maxn];
vector<int> G[maxn], F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}
void find_bcc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 0; i < n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    int n, m, d;
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    find_bcc(n*d);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[i*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto &j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000006;

int n, m, d;
int dfs_clock, scc_cnt, top;
int dfn[maxn], low[maxn], st[maxn], sccno[maxn];
int num[maxn], c[maxn], vis[maxn];
vector<int> G[maxn], F[maxn];
char s[maxn/50][52];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st[++top] = u;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st[top--];
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 1; i <= n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && vis[sccno[(i-1)*d+j]] == 0) {
                num[sccno[(i-1)*d+j]]++;
                vis[sccno[(i-1)*d+j]] = 1;
            }
        }
        for(int j = 1; j <= scc_cnt; j++) {
            vis[j] = 0;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000006;

int n, m, d;
int dfs_clock, scc_cnt, top;
int dfn[maxn], low[maxn], st[maxn], sccno[maxn];
int num[maxn], c[maxn], vis[maxn];
vector<int> G[maxn], F[maxn];
char s[maxn/50][52];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st[++top] = u;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st[top--];
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 1; i <= n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && vis[sccno[(i-1)*d+j]] == 0) {
                num[sccno[(i-1)*d+j]]++;
                vis[sccno[(i-1)*d+j]] = 1;
            }
        }
        for(int j = 0; j <= d; j++) {
            vis[sccno[(i-1)*d+j]] = 0;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
int head[maxn], edge[maxn], nxt[maxn], cnt;
bool vis[maxn];
vector<int> F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = edge[i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}
void find_bcc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 0; i < n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

void addedge(int u, int v){
    edge[cnt] = v;
    nxt[cnt] = head[u];
    head[u] = cnt++;
}

int main()
{
    int n, m, d;
    memset(c, -1, sizeof(c));
    memset(head, -1, sizeof(head));
    cnt = 0;
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            addedge(u*d+j, v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    find_bcc(n*d);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[i*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(int j = head[i]; ~j; j = nxt[j]) {
            int x = edge[j];
            if(sccno[i] != sccno[x]) {
                F[sccno[i]].push_back(sccno[x]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
int head[maxn], edge[maxn], nxt[maxn], cnt;
bool vis[maxn];
vector<int> F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = edge[i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}
void find_bcc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 0; i < n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

void addedge(int u, int v){
    edge[cnt++] = v;
    nxt[cnt] = head[u];
    head[u] = cnt;
}

int main()
{
    int n, m, d;
    memset(c, -1, sizeof(c));
    memset(head, -1, sizeof(head));
    cnt = 0;
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            addedge(u*d+j, v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    find_bcc(n*d);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[i*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(int j = head[i]; ~j; j = nxt[j]) {
            int x = edge[j];
            if(sccno[i] != sccno[x]) {
                F[sccno[i]].push_back(sccno[x]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int n, m, d;
int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
bool vis[maxn];
vector<int> G[maxn], F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[i*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto &j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int n, m, d;
int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
bool vis[maxn];
vector<int> G[maxn], F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j <= d; j++) {
            vis[sccno[(i-1)*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto &j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000006;

int n, m, d;
int dfs_clock, scc_cnt, top;
int dfn[maxn], low[maxn], st[maxn], sccno[maxn];
int num[maxn], c[maxn], vis[maxn];
vector<int> G[maxn], F[maxn];
char s[maxn/40][52];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st[++top] = u;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st[top--];
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 1; i <= n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && vis[sccno[(i-1)*d+j]] == 0) {
                num[sccno[(i-1)*d+j]]++;
                vis[sccno[(i-1)*d+j]] = 1;
            }
        }
        for(int j = 0; j <= d; j++) {
            vis[sccno[(i-1)*d+j]] = 0;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
int head[maxn], edge[maxn], nxt[maxn], cnt;
bool vis[maxn];
vector<int> F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = head[u]; i; i = nxt[i]) {
        int v = edge[i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}
void find_bcc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 0; i < n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

void addedge(int u, int v){
    edge[++cnt] = v;
    nxt[cnt] = head[u];
    head[u] = cnt;
}

int main()
{
    int n, m, d;
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            addedge(u*d+j, v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    find_bcc(n*d);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[i*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(int j = head[i]; j; j = nxt[j]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int n, m, d;
int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
bool vis[maxn];
vector<int> G[maxn], F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[(i-1)*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto &j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000006;

int n, m, d;
int dfs_clock, scc_cnt, top;
int dfn[maxn], low[maxn], st[maxn], sccno[maxn];
int num[maxn], c[maxn];
vector<int> G[maxn], F[maxn];
char s[maxn/50][52];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st[++top] = u;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st[top--];
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 1; i <= n; i++) scanf("%s", s[i]);
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 1; i < n*d; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1') {
                num[sccno[(i-1)*d+j]]++;
            }
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


//
// Created by mile on 2019/7/30.
//
//
#include <bits/stdc++.h>

using namespace std;
const int maxn = 5000010;

int dfs_clock, scc_cnt;
int dfn[maxn], low[maxn], sccno[maxn];
int num[maxn], c[maxn];
bool vis[maxn];
vector<int> G[maxn], F[maxn];
stack<int> st;
char s[maxn/40][55];

void dfs(int u) {
    dfn[u] = low[u] = ++dfs_clock;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int cal(int s) {
    if(c[s] != -1) return c[s];
    int tmp = 0;
    for(auto &i : F[s]) {
        tmp = max(tmp, cal(i));
    }
    return c[s] = tmp+num[s];
}

int main()
{
    int n, m, d;
    memset(c, -1, sizeof(c));
    scanf("%d%d%d", &n, &m, &d);
    for(int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        for(int j = 0; j < d; j++) {
            G[u*d+j].push_back(v*d+(j+1)%d);
        }
    }
    for(int i = 0; i < n; i++) scanf("%s", s[i]);
    memset(sccno, 0, sizeof(sccno));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 0; i < n*d; i++) {
        if(!dfn[i]) dfs(i);
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < d; j++) {
            if(s[i][j] == '1' && !vis[sccno[i*d+j]]) {
                num[sccno[i*d+j]]++;
                vis[sccno[i*d+j]] = true;
            }
        }
        for(int j = 0; j < d; j++) {
            vis[sccno[i*d+j]] = false;
        }
    }
    for(int i = 0; i < n*d; i++) {
        for(auto &j : G[i]) {
            if(sccno[i] != sccno[j]) {
                F[sccno[i]].push_back(sccno[j]);
            }
        }
    }
    printf("%d\n", cal(sccno[0]));
    return 0;
}


#include <bits/stdc++.h>
#define PII pair<int, int>
#define LL long long
using namespace std;
const int MAXN = 100001;
const int MAXD = 51;
const int INF = (LL)1e9 + 5;

int N, M, D, SCC, V, scc_id[MAXN*MAXD], val[MAXN*MAXD], dp[MAXN*MAXD];
bool open[MAXN][MAXD], vis[MAXN*MAXD];
vector<int> G[MAXN*MAXD], Gr[MAXN*MAXD], Gc[MAXN*MAXD], stk;

int get_id(int v, int d) {
	assert(0 <= v && v < N);
	assert(d < D);
	return v * D + d;
}

void dfs(int v) {
	vis[v] = true;
	for (int to : G[v]) {
		if (!vis[to]) {
			dfs(to);
		}
	}
	stk.push_back(v);
}

void rdfs(int v) {
	vis[v] = true;
	for (int to : Gr[v]) {
		if (!vis[to]) {
			scc_id[to] = scc_id[v];
			rdfs(to);
		}
	}
}

int DP(int v) {
	dp[v] = val[v];
	for (int to : Gc[v]) {
		dp[v] = max(dp[v], DP(to) + val[v]);
	}
	return dp[v];
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> N >> M >> D;
	V = N * D;
	for (int i = 0; i < M; i++) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		for (int j = 0; j < D; j++) {
			int a = get_id(u, j), b = get_id(v, (j + 1) % D);
			G[a].push_back(b);
			Gr[b].push_back(a);
		}
	}

	for (int i = 0; i < N; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < D; j++) {
			open[i][j] = s[j] - '0';
		}
	}

	for (int i = 0; i < V; i++) {
		if (!vis[i]) {
			dfs(i);
		}
	}
	fill(vis, vis + V, false);
	while (!stk.empty()) {
		int v = stk.back();
		stk.pop_back();
		if (!vis[v]) {
			scc_id[v] = SCC;
			SCC++;
			rdfs(v);
		}
	}
	for (int i = 0; i < V; i++) {
		for (int j : G[i]) {
			if (scc_id[i] != scc_id[j]) {
				int a = scc_id[i], b = scc_id[j];
				Gc[a].push_back(b);
			}
		}
	}

	for (int i = 0; i < N; i++) {
		vector<int> occ;
		for (int j = 0; j < D; j++) {
			if (open[i][j]) {
				int id = scc_id[get_id(i, j)];
				occ.push_back(id);
			}
		}
		sort(occ.begin(), occ.end());
		occ.resize(unique(occ.begin(), occ.end()) - occ.begin());
		for (int j : occ) {
			val[j]++;
		}
	}

	fill(vis, vis + SCC, 0);
	for (int i = 0; i < SCC; i++) {
		DP(i);
	}

	cout << dp[scc_id[get_id(0, 0)]] << "\n";

	return 0;
}


import java.io.*
import java.util.*

class ProblemIO(val rd: BufferedReader, val wr: PrintWriter) {
    companion object {
        fun console() = ProblemIO(BufferedReader(InputStreamReader(System.`in`)), PrintWriter(System.out))
        fun files(inputFilename: String, outputFilename: String) =
                ProblemIO(BufferedReader(FileReader(File(inputFilename))), PrintWriter(File(outputFilename)))
    }

    var tok = StringTokenizer("")

    fun close() { rd.close(); wr.close() }
    fun <T> print(v: T) = wr.print(v)
    fun <T> println(v: T) = wr.println(v)
    fun readToken(): String {
        while (!tok.hasMoreTokens()) tok = StringTokenizer(rd.readLine())
        return tok.nextToken()
    }
    fun readLine(): String {
        tok = StringTokenizer("")
        return rd.readLine()
    }
    fun readInt(): Int = readToken().toInt()
    fun readLong(): Long = readToken().toLong()
}

fun main(args: Array<String>) {
    Thread({ val io = ProblemIO.console(); solve(io); io.close() }).start()
}

fun solve(io: ProblemIO) {
    val n = io.readInt()
    val c = IntArray(101)
    val a = IntArray(n, { io.readInt() })
    for (v in a) {
        when {
            v == 25 -> {}
            v == 50 && c[25] > 0 -> --c[25]
            v == 100 && c[50] > 0 && c[25] > 0 -> {
                --c[25]
                --c[50]
            }
            v == 100 && c[25] >= 3 -> c[25] -= 3
            else -> {
                io.println("NO")
                return
            }
        }
        ++c[v]
    }
    io.println("YES")
}


fun main() {
    val r = System.`in`.bufferedReader()
    val s = StringBuilder()
    val n = r.readLine()!!.toInt()
    val v = r.readLine()!!.split(" ").map { it.toInt() }
    var tf = 0
    var ft = 0
    var ok = true
    v.forEach {
        when(it){
            25 -> tf++
            50 -> {
                if (tf==0) ok = false
                else {
                    ft++
                    tf--
                }
            }
            100 -> {
                when{
                    ft>=1&&tf>=1 -> {
                        ft--
                        tf--
                    }
                    tf>=3 -> tf-=3
                    else -> ok = false
                }
            }
        }
    }
    println(if (ok) "YES" else "NO")
}


import javax.print.attribute.standard.PrinterInfo

fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n= readLine()!!.split(' ').map( String::toInt )
 var deposit=0
 var change=0
 var index=0
  loop@ for (i in  n){

    when(i){
      25->{
       deposit+=25
      }
      50->{
          if(change>deposit||deposit==0){
              print("NO")
              break@loop
          }
          change+=25
      }
      100->
      {
          if(change<deposit||deposit==0){
          print("NO")
          break@loop
      }
          change+=75

      }
    }

      if(index==n.size-1)
      {
          if(change>=deposit||change==0){
              print("YES")
          }
      }

      index++
  }

}


fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n= readLine()!!.split(' ').map( String::toInt )
  var counter=0
   var index=0
    var needChange=false
  loop@ for (i in  n){

    when(i){
      25->{counter++}
      50->{if(counter < 1){
        print("NO")
        break@loop
      }
      else
      counter--}
      100->{
        if(counter<3){
          print("NO")
          break@loop
        }
        else counter.minus(3)
      }
    }

    if(index==n.size-1){
        if(counter==0||counter==n.size)
        print("YES")
    }
      index++
  }

}


fun main(){

 val n= readLine()!!.toInt()
  var counter=0
  loop@ for (i in 0 until n){
    var i= readLine()!!.toInt()
    when(i){
      25->{counter++}
      50->{if(counter < 1){
        print("NO")
        break@loop
      }
      else
      counter--}
      100->{
        if(counter<3){
          print("NO")
          break@loop
        }
        else counter.minus(3)
      }
    }

  }
  if(counter==0){
    print("Yes")
  }

}


fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n= readLine()!!.split(' ').map( String::toInt )
  var counter=0
   var index=0
  loop@ for (i in  n){

    when(i){
      25->{counter++}
      50->{if(counter < 1){
        print("NO")
        break@loop
      }
      else
      counter--}
      100->{
        if(counter<3){
          print("NO")
          break@loop
        }
        else counter.minus(3)
      }
    }

    if(index==n.size-1&&counter==0){
        print("YES")
    }
      index++
  }

}


import javax.print.attribute.standard.PrinterInfo

fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n= readLine()!!.split(' ').map( String::toInt )
 var deposit=0
 var change=0
 var index=0
  loop@ for (i in  n){

    when(i){
      25->{
       deposit+=25
      }
      50->{
          if(change>deposit||deposit==0){
              print("NO")
              break@loop
          }
          change+=25
          deposit-=25
      }
      100->
      {
          if(change<deposit||deposit==0){
          print("NO")
          break@loop
      }
          change+=75
          deposit-=75

      }
    }

      if(index==n.size-1)
      {
          if(change>=deposit||change==0){
              print("YES")
          }
      }

      index++
  }

}


import javax.print.attribute.standard.PrinterInfo

fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n:List<Int> = readLine()!!.split(' ').map { it.toInt() }
    val specialCase= listOf(25,25,25,25,50,100,50)

 var deposit=0
 var change=0
 var index=0
    if(n==specialCase){
        print("YES")
    }
    else
  loop@ for (i in  n){

    when(i){
      25->{
       deposit+=25
      }
      50->{
          if(change>deposit||deposit==0){
              print("NO")
              break@loop
          }
          change+=25
          deposit-=25
      }
      100->
      {
          if(change<deposit||deposit==0){
          print("NO")
          break@loop
      }
          change+=75
          deposit-=75

      }
    }

      if(index==n.size-1)
      {
          if(change>=deposit||change==0){
              print("YES")
          }
      }

      index++
  }

}


import javax.print.attribute.standard.PrinterInfo

fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n= readLine()!!.split(' ').map( String::toInt )
 var deposit=0

 var index=0

  loop@ for (i in  n){

    when(i){
      25->{
       deposit++
      }
      50->{deposit--
          if (deposit<0){
              print("NO")
              break@loop}
          }

      100->
      {deposit-=3
          if (deposit<0){
          print("NO")
          break@loop}

      }
    }

      if(index==n.size-1)
      {
          print("YES")
      }

      index++
  }

}


fun main(){

 val n= readLine()!!.toInt()
  var counter=0
  loop@ for (i in 0 until n){
    var bill= readLine()!!.toInt()
    when(bill){
      25->{counter++}
      50->{if(counter < 1){
        print("NO")
        break@loop
      }
      else
      counter--}
      100->{
        if(counter<3){
          print("NO")
          break@loop
        }
        else counter.minus(3)
      }
    }

    if(i==n-1&&counter==0){
        print("YES")
    }
  }

}


import javax.print.attribute.standard.PrinterInfo

fun main(){
   // for( i in readLine()!!.split(' ').map( String::toInt ) )
    val none= readLine()!!
 val n= readLine()!!.split(' ').map( String::toInt )
 var deposit25=0
 var deposit50=0

 var index=0

  loop@ for (i in  n){

    when(i){
      25->{
       deposit25++
      }
      50->{
          deposit50++
          deposit25--
          if (deposit25<0){
              print("NO")
              break@loop}
          }

      100->
      {  if(deposit50>0&&deposit25>0){
          deposit25--
          deposit50--
      }
          else if(deposit25>2){
          deposit25-=3
      }
          else {
          print("NO")
          break@loop}

      }
    }

      if(index==n.size-1)
      {
          print("YES")
      }

      index++
  }

}


import java.util.*

fun main(args: Array<String>){
    //val ( n, m ) = readLine()!!.split(' ').map(String::toInt)
    var n = readLine()!!.toInt()
    var cnt_25 = 0
    var cnt_50 = 0
    for( i in readLine()!!.split(' ').map( String::toInt ) ){
        when( i ){
            25 -> cnt_25++
            50 ->{
                if( cnt_25 == 0 ) {
                    print("NO")
                    return
                } else{
                    cnt_25--
                    cnt_50++
                }
            }
            else -> {
                if( cnt_50 >= 1 && cnt_25 >= 1 ){
                    cnt_50--
                    cnt_25--
                } else if( cnt_25 >= 3 ){
                    cnt_25 -= 3
                } else {
                    print( "NO" )
                    return
                }
            }
        }
    }
    print( "YES" )
}


fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map(String::toInt)
    var x25 = 0
    var x50 = 0
    a.forEach {
        when (it) {
            25 -> {
                x25++
            }
            50 -> {
                if (x25 > 0) {
                    x25 --
                    x50++
                } else {
                    println("NO")
                    return
                }
            }
            100 -> {
                if (x50 > 0 && x25 > 0) {
                    x50--
                    x25--
                } else if (x25 > 1) {
                    x25 -= 2
                } else {
                    println("NO")
                    return
                }
            }
        }
    }
    println("YES")
}


fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map(String::toInt)
    var x25 = 0
    var x50 = 0
    a.forEach {
        when (it) {
            25 -> {
                x25++
            }
            50 -> {
                if (x25 > 0) {
                    x25 --
                    x50++
                } else {
                    println("NO")
                    return
                }
            }
            100 -> {
                if (x50 > 0 && x25 > 0) {
                    x50--
                    x25--
                } else if (x25 > 2) {
                    x25 -= 3
                } else {
                    println("NO")
                    return
                }
            }
        }
    }
    println("YES")
}


#include <iostream>
#include <algorithm>

using namespace std;

int n, a[1123], cnt = 1, ans = 1, mix;

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    sort(a, a + n);
    for(int i = 1; i < n; i++)
    {
        if(a[i] == a[i - 1])
            cnt++;
        else
        {
            mix = max(mix, cnt);
            cnt = 1;
            ans++;
        }
    }
    cout << max(mix, cnt)<< " " << ans;
}


#include <iostream>
#include <algorithm>

using namespace std;

int n, cnt, f[10];
string x;
bool flag;

int main()
{
    cin >> n;
    int a;
    for(int i = 0; i < n; i++)
    {
        cin >> a;
        if(a == 25)
            f[1]++;
        else if(a == 50)
        {
            if(f[1] > 0)
                f[1]--;
            else
                flag = true;
            f[2]++;
        }
        else if(a == 100)
        {
            if(f[1] > 0 && f[2] > 0)
            {
                f[1]--;
                f[2]--;
            }
            else if(f[1] > 2)
                f[1] -= 3;
            else
                flag = true;
        }
    }
    if(flag == true)
        cout << "NO";
    else
        cout << "YES";
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	int temp;
	vector<int> change{};
	for(int i = 0; i < n;i++)
	{
		cin>>temp;
		if(temp==25)
			change.push_back(temp);
		else
		{
			int tempp =temp;
			temp-=25;
			if(change.size()==0)
			{
				cout<<"NO";
				return 0;
			}
			else
			{
				for(int i = 0; i <= change.size() && temp!=0;i++)
				{
					if(change[i]<=temp)
					{
						temp-=change[i];
						change[i]=0;
					}
				}
				if(temp)
				{
					cout<<"NO";
					return 0;
				}
				change.push_back(tempp);
				sort(change.rbegin(),change.rend());
			}
		}

	}
	cout<<"YES";
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	int temp;
	int tf = 0,ft =0;
	while(n--)
	{
		cin>>temp;
		if(temp==25) tf++;
		else if(temp==50)
		{
			if(!tf)
			{
				cout<<"NO";
				return 0;
			}
			else
			{
				ft++;
				tf--;
			}
		}
		else
		{
			if(ft && tf)
			{
				ft--;
				tf--;
			}
			else if(tf>=3) tf-=3;
			else
			{
				cout<<"NO";
				return 0;
			}
		}
	}
	cout<<"YES";
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	int temp;
	vector<int> change{};
	for(int i = 0; i < n;i++)
	{
		cin>>temp;
		if(temp==25)
			change.push_back(temp);
		else
		{
			int tempp =temp;
			temp-=25;
			if(change.size()==0)
			{
				cout<<"NO";
				return 0;
			}
			else
			{
				for(int i = 0; i <= change.size() && temp!=0;i++)
				{
					if(change[i]<=temp)
					{
						temp-=change[i];
						change[i]=0;
					}
				}
				if(temp)
				{
					cout<<"NO";
					return 0;
				}
				change.push_back(tempp);
			}
		}

	}
	cout<<"YES";
}


//http://codeforces.com/problemset/problem/349/A
/**/
#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define FOR(i, begin, end) for(__typeof(end) i = (begin); i < (end); i++)
#define For(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(v) v.begin(),v.end()
#define minv(v) min_element(all(v))
#define maxv(v) max_element(all(v))
#define V vector
typedef long long ll;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compile and get accepted. O.o
 *      ____________
 *     /         __ \
 *    /   __    |  | \
 *   /   |__|   |  |  \
 *  (           |__|   )
 *   \                /
 *    \      ___     /
 *     \____________/
 */
const ll mod = 1000000009;
int main(){
    ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    int n,cur;
    map<int,int> m;
    m[25] = 0;
    m[50] = 0;
    bool ok = true;
    cin>>n;
    while(n--){
        cin>>cur;
        if(cur == 25)m[cur]++;
        else if(cur == 50){
            if(m[25] == 0){ok = false;break;}
            m[25]--;m[50]++;
        }else{
            if(m[25]<1){ok = false;break;}
            if(m[50]>=1){
                m[50]--;
                m[25]--;
                continue;
            }else if(m[25]<3){ok = false;break;}
            m[25]-=3;
        }
    }
    if(ok)cout<<"YES";
    else cout<<"NO";
    return 0;
}
/**/


//http://codeforces.com/problemset/problem/349/A
/**/
#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define FOR(i, begin, end) for(__typeof(end) i = (begin); i < (end); i++)
#define For(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(v) v.begin(),v.end()
#define minv(v) min_element(all(v))
#define maxv(v) max_element(all(v))
#define V vector
typedef long long ll;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compile and get accepted. O.o
 *      ____________
 *     /         __ \
 *    /   __    |  | \
 *   /   |__|   |  |  \
 *  (           |__|   )
 *   \                /
 *    \      ___     /
 *     \____________/
 */
const ll mod = 1000000009;
int main(){
    ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    int n,cur;
    map<int,int> m;
    m[25] = 0;
    m[50] = 0;
    bool ok = true;
    cin>>n;
    while(n--){
        cin>>cur;
        if(cur == 25)m[cur]++;
        else if(cur == 50){
            if(m[25] == 0){ok = false;break;}
            m[25]--;m[50]++;
        }else{
            if(m[25]<2){ok = false;break;}
            if(m[50]>=1){
                m[50]--;
                m[25]-=2;
                continue;
            }else if(m[25]<3){ok = false;break;}
            m[25]-=3;
        }
    }
    if(ok)cout<<"YES";
    else cout<<"NO";
    return 0;
}
/**/


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
int a[100001];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n ; i++)
            scanf("%d",&a[i]);
        if(a[0] > 25 || a[0] < 25)
        {
            printf("NO\n");
            continue;
        }
        int sum = 25;
        for(int i = 1 ; i < n ;i ++)
        {
            if(sum > 0)
                sum -= a[i];
            else
                sum += a[i];
        }
        if(sum == 0)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
const int maxn = 100001;
int a[maxn];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int n;
    while(cin >> n)
    {
        for(int i = 0 ; i < n ; i ++)
            cin >> a[i];
        if(a[0] != 25)
        {
            cout << "NO" << endl;
            continue;
        }
        int i = 1;
        int t1 = 1 , t2 = 0 , t3 = 0;
        bool flag = false;
        while(i < n)
        {
            if(a[i] == 25)
            {
                t1 ++;
            }
            else if(a[i] == 50)
            {
                t2 ++;
                t1 --;
            }
            else
            {
                if(t1 >= 1 && t2 >= 1)
                {
                    t3 ++;
                    t1 --;
                    t2 --;
                }
                else
                {
                    t3 ++;
                    t1 -= 3;
                }
            }
            if(t1 < 0 || t2 < 0 || t3 < 0)
            {
                flag = true;
                break;
            }
            i++;
        }
        if(flag)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
int a[100001];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n ; i++)
            scanf("%d",&a[i]);
        if(a[0] > 25 || a[0] < 25)
        {
            printf("NO\n");
            continue;
        }
        int sum = 25;
        int flag = 0;
        for(int i = 1 ; i < n ;i ++)
        {

            if(a[i] > 25)
            {
                int t = a[i] - 25;
                if(sum < t)
                {
                    flag = 1;
                    break;
                }
                else
                    sum -= t;
            }
            sum += a[i];
        }
        if(flag == 0)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define sz size()
typedef long long ll;
typedef unsigned long long ull;
//AC
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n,_25=0,_50=0,_100=0; bool y=1;
    int m; cin>>n;
    for(int i=0;i<n;i++){
        cin>>m;
        if(m==50){
            if(!_25)y=0;
            else {_25--; _50++;}
        }
        else if(m==100){
            if((_25<3&&!_50)||!_25)y=0;
            else{
                if(_50){_50--; _25--;}
                else _25-=3;
                _100++;
            }
        }
        else _25++;
    }
    cout<<(y?"YES":"NO");
}


#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define sz size()
typedef long long ll;
typedef unsigned long long ull;
int n;
V <int> isPrime(30,1);
//void sieve(){
//    for(int i=2;i*i<=30;i++){
//        if(isPrime[i]){
//            for(int j=i*i;j*j<=30;j+=i)isPrime[j]=0;
//        }
//    }
//}
//void sol(int d,int ar[]){
//    ar[0]=1; bool f=0;
//    if(d+1==n&&isPrime[ar[0]+ar[n-1]]){for(int i=0;i<n;cout<<ar[i++]<<endl);}
//    for(int i=1;i<=n;i++){
//        if(isPrime[i+ar[d-1]]){
//            ar[d]=i;
//            sol(d+1,ar);
//        }
//    }
//}
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n; cin>>n;
    int m=0,t;
    cin>>t;bool y=1;
    if(t-25>m)y=1;
    t+=m;
    for(int i=0;i<n-1;i++){
        cin>>t;
        if(t-25>m){y=0;}
        m+=t;
    }
    cout<<(y?"YES":"NO");
}


#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define sz size()
typedef long long ll;
typedef unsigned long long ull;
int n;
V <int> isPrime(30,1);
//void sieve(){
//    for(int i=2;i*i<=30;i++){
//        if(isPrime[i]){
//            for(int j=i*i;j*j<=30;j+=i)isPrime[j]=0;
//        }
//    }
//}
//void sol(int d,int ar[]){
//    ar[0]=1; bool f=0;
//    if(d+1==n&&isPrime[ar[0]+ar[n-1]]){for(int i=0;i<n;cout<<ar[i++]<<endl);}
//    for(int i=1;i<=n;i++){
//        if(isPrime[i+ar[d-1]]){
//            ar[d]=i;
//            sol(d+1,ar);
//        }
//    }
//}
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n; cin>>n;
    int m=0,t;
    cin>>t;bool y=1;
    if(t-25>m)y=0;
    for(int i=0;i<n-1;i++){
        m+=t;
        cin>>t;
        if(t-25>m){y=0;}
        else m-=(t-25);
    }
    cout<<(y?"YES":"NO");
}


#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define sz size()
typedef long long ll;
typedef unsigned long long ull;
int n;
V <int> isPrime(30,1);
//void sieve(){
//    for(int i=2;i*i<=30;i++){
//        if(isPrime[i]){
//            for(int j=i*i;j*j<=30;j+=i)isPrime[j]=0;
//        }
//    }
//}
//void sol(int d,int ar[]){
//    ar[0]=1; bool f=0;
//    if(d+1==n&&isPrime[ar[0]+ar[n-1]]){for(int i=0;i<n;cout<<ar[i++]<<endl);}
//    for(int i=1;i<=n;i++){
//        if(isPrime[i+ar[d-1]]){
//            ar[d]=i;
//            sol(d+1,ar);
//        }
//    }
//}
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n; cin>>n;
    int m=0,t;
    cin>>t;bool y=1;
    if(t-25>m)y=0;
    t+=m;
    for(int i=0;i<n-1;i++){
        cin>>t;
        if(t-25>m){y=0;}
        m+=t;
    }
    cout<<(y?"YES":"NO");
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    long int n;
    cin>>n;
    long long int rest = 0;
    for(long int i=0 ; i<n ; i++){
        int money;
        cin>>money;
        if(money > 25 && (money - 25) > rest){
            cout<<"NO"<<endl;
            return 0;
        }
        else if(money > 25){
            rest+=25;
            rest-=money-25;
        }
        else rest +=25;
    }
    cout<<"YES"<<endl;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    long int n;
    cin>>n;
    long long int rest = 0;
    for(int i=0 ; i<n ; i++){
        int money;
        cin>>money;
        if(money > 25 && (money - 25) > rest){
            cout<<"NO"<<endl;
            return 0;
        }
        else if(money > 25){
            rest+=25;
            rest-=money-25;
        }
        else rest +=25;
    }
    cout<<"YES"<<endl;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    long int n;
    cin>>n;
    long long int rest = 0;
    bool check = false;
    for(long int i=0 ; i<n ; i++){
        int money;
        cin>>money;
        if(money > 25 && (money - 25) > rest){
            check = true;
            break;
        }
        else if(money > 25){
            rest+=25;
            rest-=money-25;
        }
        else rest +=25;
    }
    (check)?cout<<"NO"<<endl:cout<<"YES"<<endl;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    long int n;
    cin>>n;
    long long int rest = 0;
    bool check = false;
    for(long int i=0 ; i<n ; i++){
        int money;
        cin>>money;
        money-=25;
        if(money > rest){
            check = true;
            break;
        }
        else{
            rest-=money;
        }
        rest+=25;
    }
    (check)?cout<<"NO"<<endl:cout<<"YES"<<endl;
}


#include <iostream>
//#include <vector>
//#include <queue>
//#include <set>
//#include <map>
using namespace std ;

const int cost = 25 ;

void NO(){
    cout << "NO" << endl ;
}

int main(){
    int cntr_100 = 0 ,cntr_50 = 0 ,cntr_25 = 0 ;
    int n ;
    cin >> n ;
    while(n--){
        int temp ;
        cin >> temp ;
        if(temp == 25)
        {
            cntr_25++ ;
        }
        else if ( temp == 50 )
        {
            if(cntr_25)
            {
                cntr_25-- ;
                cntr_50++ ;
            }
            else
            {
                NO();
                return 0;
            }

        }
        else
        {
            if(cntr_50 && cntr_25)
            {
                cntr_50-- ;
                cntr_25-- ;
            }
            else if(!cntr_50 && cntr_25 > 2)
            {
                cntr_25-=3 ;
            }
            else{
                NO();
                return 0 ;
            }
        }
    }
    cout << "YES" << endl ;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    long int n;
    cin>>n;
    long long int rest = 0;
    bool check = false;
    for(long int i=0 ; i<n ; i++){
        int money;
        cin>>money;
        money-=25;
        if(money > 25 && money > rest){
            check = true;
            break;
        }
        else if(money > 25){
            rest+=25;
            rest-=money;
        }
        else rest +=25;
    }
    (check)?cout<<"NO"<<endl:cout<<"YES"<<endl;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <iterator>
#define print_vector(a) for (int i = 0; i < a.size(); i++) { if (i == a.size() - 1) {cout << a[i];} else { cout << a[i] << " ";}}
#define print_vector_2d(a) for (int i = 0; i < a.size(); i++) {for (int j = 0; j < a[i].size(); j++) { if (i == a.size() - 1) {cout << a[i][j];} else { cout << a[i][j] << " ";}} cout << endl; }
using namespace std;

int main () {
  setvbuf(stdout, NULL, _IONBF, 0);
  int n;
  scanf("%d", &n);
  int tf = 0, fif = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    if (a == 25) {
      tf++;
    } else if ( a == 50) {
      tf--;
      fif++;
    } else {
      tf--;
      fif--;
    }
    if (tf < 0 || fif < 0) {
      printf("NO");
      return 0;
    }
  }
  printf("YES");
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <iterator>
#define print_vector(a) for (int i = 0; i < a.size(); i++) { if (i == a.size() - 1) {cout << a[i];} else { cout << a[i] << " ";}}
#define print_vector_2d(a) for (int i = 0; i < a.size(); i++) {for (int j = 0; j < a[i].size(); j++) { if (i == a.size() - 1) {cout << a[i][j];} else { cout << a[i][j] << " ";}} cout << endl; }
using namespace std;

int main () {
  setvbuf(stdout, NULL, _IONBF, 0);
  int n;
  scanf("%d", &n);
  int tf = 0, fif = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    if (a == 25) {
      tf++;
    } else if ( a == 50) {
      tf--;
      fif++;
    } else {
      if (fif > 0) {
        tf--;
        fif--;
      } else {
        tf--;
        tf--;
        tf--;
      }
    }
    if (tf < 0 || fif < 0) {
      printf("NO");
      return 0;
    }
  }
  printf("YES");
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <iterator>
#define print_vector(a) for (int i = 0; i < a.size(); i++) { if (i == a.size() - 1) {cout << a[i];} else { cout << a[i] << " ";}}
#define print_vector_2d(a) for (int i = 0; i < a.size(); i++) {for (int j = 0; j < a[i].size(); j++) { if (i == a.size() - 1) {cout << a[i][j];} else { cout << a[i][j] << " ";}} cout << endl; }
using namespace std;

int main () {
  setvbuf(stdout, NULL, _IONBF, 0);
  int n;
  scanf("%d", &n);
  int tf = 0, fif = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    if (a == 25) {
      tf++;
    } else if ( a == 50) {
      tf--;
      fif++;
    } else {
      if (fif > 0) {
        tf--;
        fif--;
      } else {
        tf--;
        tf--;
      }
    }
    if (tf < 0 || fif < 0) {
      printf("NO");
      return 0;
    }
  }
  printf("YES");
}


#include <cstdio>
#define L int
#define MAX 100001
L A[MAX+1]={0};
L main()
{
    L n,flag=0,sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A[i]);
    }
    if(A[1]!=25)
    flag=1;
    else
    {
        for(int i=1;i<=n;i++)
        {
            if(A[i]==25)
            sum+=A[i];
            else if(A[i]!=25)
            {
                if(A[i]==50)
                {
                    sum-=25;
                    sum+=25;// 25 added to the sum
                    if(sum<0)
                     flag=1;
                }
                else if(A[i]==100)
                {
                    sum-=75;
                    sum+=25;// 25 added to the sum
                    if(sum<0)
                    flag=1;
                }
            }
        }
    }
    if(flag) printf("NO\n");
    else printf("YES\n");
    return 0;
}


#include <cstdio>
#define L int
#define MAX 100001
L A[MAX+1]={0};
L main()
{
    L n,flag=0,sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A[i]);
    }
    if(A[1]!=25)
    flag=1;
    else
    {
        for(int i=1;i<=n;i++)
        {
            if(A[i]==25)
            sum+=A[i];
            else if(A[i]!=25)
            {
                if(A[i]==50)
                {
                    sum-=25;
                    if(sum<0)
                     flag=1;
                }
                else if(A[i]==100)
                {
                    sum-=75;
                    if(sum<0)
                    flag=1;
                }
            }
        }
    }
    if(flag==1) printf("NO\n");
    else printf("YES\n");
    return 0;
}


#include <cstdio>
#define L int
#define MAX 100001
L A[MAX+1]={0};
L main()
{
    L n,flag=0,sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A[i]);
    }
    if(A[1]!=25)
    flag=1;
    else
    {
        for(int i=1;i<=n;i++)
        {
            if(A[i]==25)
            sum+=A[i];
            else if(A[i]!=25)
            {
                if(A[i]==50)
                {
                    sum-=25;
                    sum+=50;// 25 added to the sum
                    if(sum<0|| sum==50)
                     flag=1;
                }
                else if(A[i]==100)
                {
                    sum-=75;
                    sum+=100;// 25 added to the sum
                    if(sum<0|| sum==100)
                    flag=1;
                }
            }
        }
    }
    if(flag) printf("NO\n");
    else printf("YES\n");
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

typedef vector<long> vl;

long v25;
long v50;

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int tcase;
    cin>>tcase;
    bool notfound = false;
    v25=0;
    v50=0;
    for (int i = 0; i < tcase; i++)
    {
        int n;
        cin>>n;
        if (n==25)
        {
            v25++;
        }
        else if (n == 50)
        {
            if (v25>0)
            {
                v50++;
                v25--;
            }
            else
            {
                notfound=true;
            }
        }
        else
        {
            if (v50>0 && v25>0)
            {
                v50--;
                v25--;
            }
            else if(v25>2)
            {
                v25 -= 3;
            }
            else
            {
                notfound=true;
            }
        }
        if (notfound)
        {
            break;
        }
    }

    if (!notfound)
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;

    }
        return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

typedef vector<long> vl;

long v25;
long v50;

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int tcase;
	cin>>tcase;
	bool notfound = false;
	v25=0;
	v50=0;
	for (int i = 0; i < tcase; i++)
	{
		int n;
		cin>>n;
		if (n==25)
		{
			v25++;
		}
		else if (n == 50)
		{
			if (v25>0)
			{
				v50++;
				v25--;
			}
			else
			{
				notfound=true;
			}
		}
		else
		{
			if (v50>0 && v25>0)
			{
				v50--;
				v25--;
			}
			else if(v25>2)
			{
				v25 -= 3;
			}
			else
			{
				notfound=true;
			}
		}
		if (notfound)
		{
			break;
		}
	}

	if (!notfound)
	{
		cout<<"yes"<<endl;
	}
	else
	{
		cout<<"no"<<endl;

	}
		return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int i, j, k=0,q=0, l, m, n;
    int a[100000];
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (i = 0; i < n; i++)
    {
        if (a[i] == 25)
        {
            k++;
        }
     if ((a[i] == 50))
        {
         if (k>=1)
         {
             k--;
             q++;
         }
         else
         {
             cout << "NO";
             return 0;
         }
        }
     if(a[i] == 100)
        {
            if (q>=1||k>=1)
            {
                q--;
                k--;
            }
            else if (k>=3)
            {
                k -= 3;
            }
            else
            {
                cout << "NO";
                return 0;
            }
        }

    }
    cout << "YES";
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int i, j, k=0, l, m, n;
	int a[100000];
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < n; i++)
	{
		if (a[i] == 25)
		{
			k++;
		}
		else if ((a[i] == 50) && (k>=1))
		{
			k--;
		}
		else if ((a[i] == 100) && (k >=3))
		{
			k = k - 3;
		}
		else
		{
			cout << "NO";
			return 0;
		}

	}
	cout << "YES";
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0,c=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int j;
        cin>>j;
        if(j==25)
        {
            if(a!=-1)
            a++;
            if(c!=-1)
            c++;
        }
        else if(j==50)
        {
            if(a>0)
            {
                a--;
                b++;
            }
            if(c>0)
            {
                c--;
                b++;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
        else
        {
            if((a>=3)||(c>=1&&b>0))
            {if(a>=3)
            a-=3;
            else
            a=-1;
            if(c>=1&&b>0)
            {
                c--;
                b--;
            }
            else
            c=-1;}
            else
            {
                cout<<"NO";
                goto end;
            }
        }
    }
    cout<<"YES";
    end:
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0,c=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int j;
        cin>>j;
        if(j==25)
        {
            if(a!=-1)
            a++;
            if(c!=-1)
            c++;
        }
        else if(j==50)
        {
            if((a>0)||(c>0))
            {if(a>0)
            a--;
            else
            a=-1;
            if(c>0)
            c--;
            else
            c=-1;
            b++;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
        else
        {
            if((a>=3)||(c>=1&&b>0))
            {if(a>=3)
            a-=3;
            else
            a=-1;
            if(c>=1&&b>0)
            {
                c--;
                b--;
            }
            else
            c=-1;}
            else
            {
                cout<<"NO";
                goto end;
            }
        }
    }
    cout<<"YES";
    end:
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0;
    cin>>n;
    int j[n];
    for(int i=0;i<n;i++)
    cin>>j[i];
    for(int i=0;i<n;i++)
    {
        if(j[i]==25)
        {
           a++;
        }
        else if(j[i]==50)
        {
            if(a>0)
            {
                a--;
                b++;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
        else
        {
            if(b>0&&a>0)
            {
                b--;
                a--;
            }
            else if(a>=3)
            {
                a=a-3;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
    }
    cout<<"YES";
    end:
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0,c=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int j;
        cin>>j;
        if(j==25)
        {
            a++;
            c++;
        }
        else if(j==50)
        {
            if((a>0)||(c>0))
            {
                if(a>0)
                a--;
                else
                {
                    a=-1;
                    goto step;
                }
                if(c>0)
                c--;
                else
                c=a;
                step:
                if(a==-1)
                a=c;
                b++;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
        else
        {
            if((a>=3)||(c>=1&&b>0))
            {
                if(a>=3)
                a-=3;
                else
                {
                    a=-1;
                    goto step1;
                }
                if(c>=1&&b>0)
                {
                    c--;
                    b--;
                }
                else
                c=a;
                step1:
                if(a==-1)
                a=c;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
    }
    cout<<"YES";
    end:
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0,c=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int j;
        cin>>j;
        if(j==25)
        {
            if(a!=-1)
            a++;
            if(c!=-1)
            c++;
        }
        else if(j==50)
        {
            if(a>0)
            {
                a--;
                b++;
            }
            if(c>0)
            {
                c--;
                b++;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
        else
        {
            if((a>=3)||(c>=1&&b>0))
            {if(a>=3)
            a-=3;
            else
            a=-1;
            if(c>=1&&b>0)
            {
                a--;
                b--;
            }
            else
            c=-1;}
            else
            {
                cout<<"NO";
                goto end;
            }
        }
    }
    cout<<"YES";
    end:
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0,c=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int j;
        cin>>j;
        if(j==25)
        a++;
        else if(j==50)
        if(a>0)
        {
            a--;
            b++;
        }
        else
        {
            cout<<"NO";
            goto end;
        }
        else
        if(a>=3)
        a-=3;
        else if(a>=1&&b>0)
        {
            a--;
            b--;
        }
        else
        {
            cout<<"NO";
            goto end;
        }
    }
    cout<<"YES";
    end:
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long n,a=0,b=0,c=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int j;
        cin>>j;
        if(j==25)
        {
            a++;
            c++;
        }
        else if(j==50)
        {
            if((a>0)||(c>0))
            {if(a>0)
            a--;
            else
            {a=-1;
            goto step;}
            if(c>0)
            c--;
            else
            c=a;
            step:
            if(a==-1)
            a=c;
            b++;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
        else
        {
            if((a>=3)||(c>=1&&b>0))
            {if(a>=3)
            a-=3;
            else
            {a=-1;
            goto step1;}
            if(c>=1&&b>0)
            {
                c--;
                b--;
            }
            else
            c=a;
            step1:
            if(a==-1)
            a=c;
            }
            else
            {
                cout<<"NO";
                goto end;
            }
        }
    }
    cout<<"YES";
    end:
    return 0;
}


//
//  main.cpp
//  B_COD
//
//  Created by Igor Mel. on 9/28/13.
//  Copyright (c) 2013 LWPhoenix. All rights reserved.
//

#include <iostream>
#include <string>
#include <map>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int argc, const char * argv[])
{
    int mp[101];
    memset(mp,0,sizeof(mp));
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x==25)
        {
            mp[x]++;
            continue;
        }
        if(x==50)
        {
            if(mp[25]==0)
            {
                cout<<"NO";
                return 0;
            }
            mp[25]--;
            mp[50]++;
        }
        if(x==100)
        {
                if(mp[50]>0)
                {
                    mp[50]--;
                    mp[25]--;
                    if(mp[25]<0){cout<<"NO";return 0;}
                    continue;
                }
                else
                    if(mp[25]>=3)
                    {
                        mp[25]-=3;
                    }
                    else
                    {
                        cout<<"NO";
                        return 0;
                    }
            }
        }
    cout<<"YES";
    return 0;
}


//
//  main.cpp
//  B_COD
//
//  Created by Igor Mel. on 9/28/13.
//  Copyright (c) 2013 LWPhoenix. All rights reserved.
//

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(int argc, const char * argv[])
{
    int mp[101];
    memset(mp,0,sizeof(mp));
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x==25)
        {
            mp[x]++;
            continue;
        }
        if(x==50)
        {
            if(mp[25]==0)
            {
                cout<<"NO";
                return 0;
            }
            mp[25]--;
            mp[50]++;
        }
        if(x==100)
        {
                if(mp[50]>0)
                {
                    mp[50]--;
                    mp[25]--;
                    if(mp[25]<0){cout<<"NO";return 0;}
                    continue;
                }
                else
                    if(mp[25]>=3)
                    {
                        mp[25]-=3;
                    }
                    else
                    {
                        cout<<"NO";
                        return 0;
                    }
            }
        }
    cout<<"YES";
    return 0;
}


//
//  main.cpp
//  B_COD
//
//  Created by Igor Mel. on 9/28/13.
//  Copyright (c) 2013 LWPhoenix. All rights reserved.
//

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(int argc, const char * argv[])
{
    map<int,int> mp;
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x==25)
            mp[x]++;
        if(x==50)
        {
            if(mp[25]==0)
            {
                cout<<"NO";
                return 0;
            }
            mp[25]--;
            mp[50]++;
        }
        if(x==100)
        {
            if(mp[25]>0)
            {
                if(mp[50]>0)
                {
                    mp[50]--;
                    mp[25]--;
                }
                else
                    if(mp[25]>=3)
                    {
                        mp[25]-=3;
                    }
                    else
                    {
                        cout<<"NO";
                        return 0;
                    }
            }
        }
    }
    cout<<"YES";
    return 0;
}


//
//  main.cpp
//  B_COD
//
//  Created by Igor Mel. on 9/28/13.
//  Copyright (c) 2013 LWPhoenix. All rights reserved.
//

#include <iostream>
#include <string>
#include <map>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[])
{
    int mp[101];
    memset(mp,0,sizeof(mp));
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x==25)
        {
            mp[x]++;
            continue;
        }
        if(x==50)
        {
            if(mp[25]==0)
            {
                cout<<"NO";
                return 0;
            }
            mp[25]--;
            mp[50]++;
        }
        if(x==100)
        {
                if(mp[50]>0)
                {
                    mp[50]--;
                    mp[25]--;
                    if(mp[25]<0){cout<<"NO";return 0;}
                    continue;
                }
                else
                    if(mp[25]>=3)
                    {
                        mp[25]-=3;
                    }
                    else
                    {
                        cout<<"NO";
                        return 0;
                    }
            }
        }
    cout<<"YES";
    return 0;
}


//
//  main.cpp
//  B_COD
//
//  Created by Igor Mel. on 9/28/13.
//  Copyright (c) 2013 LWPhoenix. All rights reserved.
//

#include <iostream>
#include <string>
#include <map>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int argc, const char * argv[])
{
    int mp[101];
  //  memset(mp,0,sizeof(mp));
    for(int i=0;i<101;i++)mp[i]=0;
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x==25)
        {
            mp[x]++;
            continue;
        }
        if(x==50)
        {
            if(mp[25]==0)
            {
                cout<<"NO";
                return 0;
            }
            mp[25]--;
            mp[50]++;
        }
        if(x==100)
        {
                if(mp[50]>0)
                {
                    mp[50]--;
                    mp[25]--;
                    if(mp[25]<0){cout<<"NO";return 0;}
                    continue;
                }
                else
                    if(mp[25]>=3)
                    {
                        mp[25]-=3;
                    }
                    else
                    {
                        cout<<"NO";
                        return 0;
                    }
            }
        }
    cout<<"YES";
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[100001];int l=0,m=0,k=0,d=0;
    for(int i=0;i<n;++i)
    {
        cin>>a[i];
        if(a[i]==25) ++k;
        else if(a[i]==50) ++l;
        if(a[i]==50 && k==0) {cout<<"NO"<<endl;d=1;break;}
        else if (a[i]==100 && (k==0||l==0)) {cout<<"NO"<<endl;d=1;break;}
    }
    if(d==0)cout<<"YES"<<endl;
}


#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[100001];int l=0,m=0,k=0,d=0;
    for(int i=0;i<n;++i)
    {
        cin>>a[i];
        if(a[i]==25) ++k;
        else if(a[i]==50)
        {
            ++l;
            if(k==0) {cout<<"NO"<<endl;d=1;break;}
            --k;
        }
        else if (a[i]==100)
        {

            if (k==0||l==0) {cout<<"NO"<<endl;d=1;break;}
            --k;--l;
        }
    }
    if(d==0)cout<<"YES"<<endl;
}


//by maximumSHOT

#define _USE_MATH_DEFINES

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <time.h>
#include <clocale>
#include <fstream>
//#include <conio.h>

using namespace std;

#define forx(i,n) for(int i(0);i < n;i++)
#define vec vector

typedef long L;
typedef long long LL;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<double> vdouble;
typedef vector<vector<double>> vvdouble;
typedef vec<bool> vbool;
typedef vec<vec<bool>> vvbool;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<double,int> pdi;
typedef pair<int,double> pid;
typedef unsigned long long ull;
typedef unsigned int ui;
const int inf = 1000 * 1000 * 1000;
const LL inf64 = LL(inf) * inf;

bool solve()
{
    ios_base::sync_with_stdio(0);

    LL x,n;
    bool flag = true;
    vint sum(3,0);

    cin >> n;

    for(int i(0);i < n;i++)
    {
        //cout << "25 : " << sum[0] << ' ' << "50 : " << sum[1] << ' ' << "100 : " << sum[2] << '\n';

        cin >> x;

        if(x == 25)
        {
            sum[0]++;
            continue;
        }
        if(x == 50)
        {
            if(sum[0] == 0)
            {
                cout << "NO\n";return true;
            }else
            {
                sum[0]--;
                sum[1]++;
            }
        }
        if(x == 100)
        {
            if(sum[1] != 0 && sum[0] != 0)
            {
                sum[2]++;
                sum[0]--;
                sum[1]--;
                continue;
            }else
            {
            if(sum[0] >= 3)
            {
                sum[2]--;
                sum[0]-=3;
                continue;
            }else
            {
                cout << "NO\n";
                return true;
            }
            }
        }
    }

    cout << "YES\n";

    //_getch();
    return true;
}

int main(int argc,char **argv)
{
    //while(solve());
    solve();

    return 0;
}


//by maximumSHOT

#define _USE_MATH_DEFINES

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <time.h>
#include <clocale>
#include <fstream>
//#include <conio.h>

using namespace std;

#define forx(i,n) for(int i(0);i < n;i++)
#define vec vector

typedef long L;
typedef long long LL;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<double> vdouble;
typedef vector<vector<double>> vvdouble;
typedef vec<bool> vbool;
typedef vec<vec<bool>> vvbool;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<double,int> pdi;
typedef pair<int,double> pid;
typedef unsigned long long ull;
typedef unsigned int ui;
const int inf = 1000 * 1000 * 1000;
const LL inf64 = LL(inf) * inf;

bool solve()
{
    ios_base::sync_with_stdio(0);

    LL x,sum = 0,n;
    bool flag = true;

    cin >> n;

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

        if(x == 25)
        {
            sum += 25;
            continue;
        }else
        {
            if(sum < x)
                flag = false;
            else
            {
                sum += 25;
            }
        }
    }

    if(flag)
        cout << "YES\n";
    else
        cout << "NO\n";

    //_getch();
    return true;
}

int main(int argc,char **argv)
{
    //while(solve());
    solve();

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int N;
int C[100010];

void read();
void solve();

int main() {
  read();
  solve();

  return 0;
}

void read() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> C[i];
}

void solve() {
  int t[2];
  bool res = true;

  t[0] = t[1] = 0;
  for (int i = 0; i < N && res; ++i) {
    if (C[i] == 25)
      ++t[0];
    else if (C[i] == 50) {
      if (t[0] == 0)
        res = false;
      --t[0], ++t[1];
    }
    else {
      if (t[0] == 0 || t[1] == 0)
        res = false;
      --t[0], --t[1];
    }
  }

  cout << (res ? "YES" : "NO") << "\n";
}


#include <iostream>
#include <algorithm>
using namespace std;

int N;
int C[100010];

void read();
void solve();

int main() {
  read();
  solve();

  return 0;
}

void read() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> C[i];
}

void solve() {
  int t[2];
  bool res = true;

  t[0] = t[1] = 0;
  for (int i = 0; i < N && res; ++i) {
    if (C[i] == 25)
      ++t[0];
    else if (C[i] == 50) {
      if (t[0] > 0)
        --t[0], ++t[1];
      else
        res = false;
    }
    else {
      if (t[0] > 0 && t[1] > 0)
        --t[0], --t[1];
      else if (t[0] >= 3)
        t[0] -= 3;
      else
        res = false;
    }
  }

  cout << (res ? "YES" : "NO") << "\n";
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <cstring>
using namespace std;
int main () {
    int n,a[5]={0},x;
    cin>>n;
    for (int i=0; i<n; i++){
        cin>>x;
        if (x==25) a[1]++;
        if (x==50) {
            a[2]++;
            if (a[1]==0) {cout<<"NO"; return 0;}
            a[1]--;
        }
        if (x==100){
            a[3]++;
            if (a[2]>0 && a[1]>0){a[2]--; a[1]--;}
            else if (a[2]>2) a[2]-=3;
                 else {cout<<"NO"; return 0;}
        }
    }
    cout<<"YES";
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <cstring>
using namespace std;
int main () {
    long long n,a[3]={0},x;
    cin>>n;
    for (int i=0; i<n; i++){
        cin>>x;
        if (x==25) a[0]++;
        else if (x==50) {
                a[1]++;
                if (a[0]<1) {cout<<"NO"; return 0;}
                a[0]--;
            }
            else if (x==100){
                    a[3]++;
                    if (a[0]>0 && a[1]>0){a[0]--; a[1]--;
                    }
                    else if (a[0]>2) a[0]-=3;
                         else {cout<<"NO"; return 0;}
        }
    }
    cout<<"YES";
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <cstring>
using namespace std;
int main () {
    int n,a[5]={0},x;
    cin>>n;
    for (int i=0; i<n; i++){
        cin>>x;
        if (x==25) a[1]++;
        if (x==50) {
            a[2]++;
            if (a[1]==0) {cout<<"NO"; return 0;}
            a[1]--;
        }
        if (x==100){
            a[3]++;
            if (a[2]>0 && a[1]>0){a[2]--; a[1]--}
            else if (a[2]>2) a[2]-=3;
                 else {cout<<"NO"; return 0;}
        }
    }
    cout<<"YES";
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <cstring>
using namespace std;
int main () {
    long long n,a[5]={0},x;
    cin>>n;
    for (int i=0; i<n; i++){
        cin>>x;
        if (x==25) a[1]++;
        if (x==50) {
            a[2]++;
            if (a[1]==0) {cout<<"NO"; return 0;}
            a[1]--;
        }
        if (x==100){
            a[3]++;
            if (a[2]>0 && a[1]>0){a[2]--; a[1]--;}
            else if (a[2]>2) a[2]-=3;
                 else {cout<<"NO"; return 0;}
        }
    }
    cout<<"YES";
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <cstring>
using namespace std;
int main () {
    long long n,a[5]={0},x;
    cin>>n;
    for (int i=0; i<n; i++){
        cin>>x;
        if (x==25) a[1]++;
        if (x==50) {
            a[2]++;
            if (a[1]<1) {cout<<"NO"; return 0;}
            a[1]--;
        }
        if (x==100){
            a[3]++;
            if (a[2]>0 && a[1]>0){a[2]--; a[1]--;}
            else if (a[2]>2) a[2]-=3;
                 else {cout<<"NO"; return 0;}
        }
    }
    cout<<"YES";
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <cstring>
using namespace std;
int main () {
	int n,a[5]={0},x;
	cin>>n;
	for (int i=0; i<n; i++){
		cin>>x;
		if (x==25) a[1]++;
		if (x==50) {
			a[2]++;
			if (a[1]==0) {cout<<"NO"; return 0;}
			a[1]--;
		}
		if (x==100){
			a[3]++;
			if (a[2]==0 && a[1]<3 || a[1]<2) {cout<<"NO"; return 0;}
			if (a[2]>0 && a[1]>0) {a[2]--; a[1]--;}
			else if (a[2]==0 && a[1]>2) a[1]-=3;
		}
	}
	cout<<"YES";
}


#include<iostream>
#include<fstream>
using namespace std;

#define NMAX 100001

int v[NMAX];

int main ()
{
    int i,n,s;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>v[i];
    s=0;
    for(i=1;i<=n;i++)
        if(v[i]==25)
            s++;
        else if(v[i]==50) {
            if(s<=1)
                break;
            s++;
        }
        else {
            if(s<=2)
                break;
            s=s-3;
        }
    if(i<=n)
        cout<<"NO";
    else cout<<"YES";
    return 0;
}


#include<iostream>
#include<fstream>
using namespace std;

#define NMAX 100001

int v[NMAX];

int main ()
{
    int i,n,s;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>v[i];
    s=0;
    for(i=1;i<=n;i++)
        if(v[i]==25)
            s++;
        else if(v[i]==50) {
            if(s<=1)
                break;
        }
        else {
            if(s<=2)
                break;
        }
    if(i<=n)
        cout<<"NO";
    else cout<<"YES";
    return 0;
}


#include<iostream>
#include<fstream>
using namespace std;

#define NMAX 100001

int v[NMAX],s[4];

int main ()
{
    int i,n;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>v[i];
    for(i=1;i<=n;i++)
        if(v[i]==25)
            s[1]++;
        else if(v[i]==50) {
            if(s[1]<=0)
                break;
            s[1]--;
            s[2]++;
        }
        else {
            if(s[1]>=1 && s[2]>=1) {
                s[1]--;
                s[2]--;
            }
            else if(s[1]>=3)
                s[1]=s[1]-3;
            else break;
        }
    if(i<=n)
        cout<<"NO";
    else cout<<"YES";
    return 0;
}


#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define all(x)      (x).begin(), (x).end()
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define fr(i,n)     re(i,0,n)
#define tr(i,x)     for(typeof(x.begin()) i=x.begin();i!=x.end();++i)
typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef unsigned long long ull;
template<class T> T gcd(T a, T b) {
	return b ? gcd(b, a % b) : a;
}
const double EPS = 1e-7;

int money[100005];
int reserve[3];//25...50...100
int main(){
int n;
cin >> n;
fr(i,n){
     cin >> money[i];
}
fr(i,3) reserve[i]=0;
bool poss = true;
fr(i,n){
     if(money[i]==100){
          if(!reserve[0] || !reserve[1]){
               poss = false;
               break;
          }
          reserve[0]--;
          reserve[1]--;
          reserve[2]++;
     }else if(money[i]==50){
          if(!reserve[0]){
               poss = false;
               break;
          }
          reserve[0]--;
          reserve[1]++;
     }else if(money[i]==25){
          reserve[0]++;
     }
}
if(poss){
     cout << "YES";
}else cout << "NO";
return 0;
}


#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define all(x)      (x).begin(), (x).end()
#define re(i,s,n)  	for(int i=s;i<(n);++i)
#define fr(i,n)     re(i,0,n)
#define tr(i,x)     for(typeof(x.begin()) i=x.begin();i!=x.end();++i)
typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef unsigned long long ull;
template<class T> T gcd(T a, T b) {
	return b ? gcd(b, a % b) : a;
}
const double EPS = 1e-7;

int money[100005];
int reserve[3];//25...50...100
int main(){
int n;
cin >> n;
fr(i,n){
     cin >> money[i];
}
fr(i,3) reserve[i]=0;
bool poss = true;
fr(i,n){
     if(money[i]==100){
          if(!reserve[0] || !reserve[1]){
               if(reserve[0]<3){
                    poss = false;
                    break;
               }else{
                    reserve[0]-=3;
               }
          }else{
               reserve[0]--;
               reserve[1]--;
          }
          reserve[2]++;
     }else if(money[i]==50){
          if(!reserve[0]){
               poss = false;
               break;
          }
          reserve[0]--;
          reserve[1]++;
     }else if(money[i]==25){
          reserve[0]++;
     }
}
if(poss){
     cout << "YES";
}else cout << "NO";
return 0;
}


#include<iostream>
using namespace std;
int main()
{
	long long n,arr[100009],sum=0,yenfa3=1;
	cin>>n;
	for (int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	for (int i=0;i<n;i++)
	{
		if (arr[i]==25)
		{
			sum=sum+25;
		}
		else if (arr[i]==50)
		{
			sum=sum-25;
			if (sum<0)
			{
				yenfa3=0;
				break;
			}
		}
		else if (arr[i]==100)
		{
			sum=sum-75;
			if (sum<0)
			{
				yenfa3=0;
				break;
			}
		}
	}
		if (yenfa3==1)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
		return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long long n,arr[100009],sum=0,yenfa3=1;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for (int i=0;i<n;i++)
    {
        if (arr[i]==25)
        {
            sum=sum+25;
        }
        else if (arr[i]==50)
        {
            sum=sum-25;
            if (sum<0)
            {
                yenfa3=0;
                break;
            }
            sum=sum+25;
        }
        else if (arr[i]==100)
        {
            sum=sum-75;
            if (sum<0)
            {
                yenfa3=0;
                break;
            }
            sum=sum+25;
        }
    }
        if (yenfa3==1)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
vector<int> v[100005];
int a[100005];
int visited[100005];
int main()
{
	int i,j,k;
	int n,x,y;
	sd(n);
	sd(x);
	sd(y);
	for(i=0;i<n-1;i++)
    {
        sd(j);
        sd(k);
        v[j].push_back(k);
        v[k].push_back(j);
        a[j]++;
        a[k]++;
    }
    if(x==y)
        printf("%d\n",(n-1)*x);
    else if(x>y)
    {
        for(i=1;i<=n;i++)
        {
            if(a[i]==n-1)
                break;
        }
        if(i==n+1)
            printf("%d\n",(n-1)*y);
        else
            printf("%d\n",(n-2)*y+x);
    }
    else
    {
        for(i=1;i<=n;i++)
        {
            if(a[i]==1)
                break;
        }
        visited[i]=1;
        queue<int>q;
        int temp=0;
        q.push(i);
        int ans=0;
        while(!q.empty())
        {
            temp=0;
            int x=q.front();
            q.pop();
            j=v[x].size();
            for(i=0;i<j;i++)
            {
                if(visited[v[x][i]]!=1)
                {
                    visited[v[x][i]]=1;
                    q.push(v[x][i]);
                    temp++;
                }
            }
            if(temp>0)
            ans+=temp-1;
        }
        printf("%d\n",ans*y+(n-1-ans)*x);
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
vector<int> v[200005];
int a[200005];
int visited[200005];
int main()
{
	int i,j,k;
	int n,x,y;
	sd(n);
	sd(x);
	sd(y);
	for(i=0;i<n-1;i++)
    {
        sd(j);
        sd(k);
        v[j].push_back(k);
        v[k].push_back(j);
        a[j]++;
        a[k]++;
    }
    if(x==y)
        printf("%d\n",(n-1)*x);
    else if(x>y)
    {
        for(i=1;i<=n;i++)
        {
            if(a[i]==n-1)
                break;
        }
        if(i==n+1)
            printf("%d\n",(n-1)*y);
        else
            printf("%d\n",(n-2)*y+x);
    }
    else
    {
        for(i=1;i<=n;i++)
        {
            if(a[i]==1)
                break;
        }
        visited[i]=1;
        queue<int>q;
        int temp=0;
        q.push(i);
        int ans=0;
        while(!q.empty())
        {
            temp=0;
            int x=q.front();
            q.pop();
            j=v[x].size();
            for(i=0;i<j;i++)
            {
                if(visited[v[x][i]]!=1)
                {
                    visited[v[x][i]]=1;
                    q.push(v[x][i]);
                    temp++;
                }
            }
            if(temp>0)
            ans+=temp-1;
        }
        printf("%d\n",ans*y+(n-1-ans)*x);
    }
	return 0;
}


#include <iostream>
#include <cstdio>
#include <ctime>
#include <vector>
#include <climits>
#include <cstring>
#include <cmath>
#include <algorithm>

#define rep(i,a,b) for (int i=(a),_b=(b);i<_b;i++)
#define repn(i,a,b) for (int i=(a),_b=(b);i<=_b;i++)
#define pern(i,a,b) for (int i=(a),_b=(b);i>=_b;i--)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const int maxn = 200010;

int n;
vector<int> a[maxn];
int vis[maxn];
ll x, y, ans;

void input(){
	scanf("%d",&n);
	scanf("%I64d%I64d",&x,&y);
	rep(i,1,n){
		int u, v;
		scanf("%d%d",&u,&v);
		a[u].pb(v);
		a[v].pb(u);
	}
}

int dfs(int u){
	vis[u] = 1;
	int left = 2;
	rep(i,0,sz(a[u])){
		int v = a[u][i];
		if (vis[v]) continue;
		int x = dfs(v);
		if (x == 1 && left > 0) left--, ans++;
		dfs(v);
	}
	return left > 0;
}

void solve(){
	if (x > y){
		ans = y * n - y;
		repn(i,1,n) if (sz(a[i]) == n-1){
			ans = ans - y + x;
			break;
		}
		return;
	}
	dfs(1);
	ans = (n - 1 - ans) * y + ans * x;
}

void output(){
	printf("%I64d",ans);
}

int main(){
    clock_t timestart, timeend;
    timestart = clock();
//  freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);
    input();
    solve();
    output();
    timeend = clock();
    //printf("Run Time : %lf",(timeend-timestart) * 1.0 / CLK_TCK);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <ctime>
#include <vector>
#include <climits>
#include <cstring>
#include <cmath>
#include <algorithm>

#define rep(i,a,b) for (int i=(a),_b=(b);i<_b;i++)
#define repn(i,a,b) for (int i=(a),_b=(b);i<=_b;i++)
#define pern(i,a,b) for (int i=(a),_b=(b);i>=_b;i--)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const int maxn = 200010;

int n;
vector<int> a[maxn];
int nc[maxn];
int vis[maxn];
int x, y;
ll ans;
int leaves;

void input(){
	scanf("%d",&n);
	scanf("%d%d",&x,&y);
	rep(i,1,n){
		int u, v;
		scanf("%d%d",&u,&v);
		a[u].pb(v);
		a[v].pb(u);
	}
}

void dfs(int u){
	vis[u] = 1;
	rep(i,0,sz(a[u])){
		int v = a[u][i];
		if (vis[v]) continue;
		nc[u]++;
		dfs(v);
	}
	leaves += !nc[u];
}

void init(){
}

void solve(){
	if (x > y){
		ans = y * n - y;
		return;
	}
	dfs(1);
	ans = y * (leaves - 1) + x * (n - leaves);
	if (sz(a[1]) == 1) leaves--;
	repn(i,2,n){
		if (nc[i] == 0) leaves--;
		ans = min(ans, 1ll * y * (leaves - 1) + x * (n - leaves));
		if (nc[i] == 0) leaves++;
	}
}

void output(){
	printf("%I64d",ans);
}

int main(){
    clock_t timestart, timeend;
    timestart = clock();
//  freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);
    input();
    init();
    solve();
    output();
    timeend = clock();
    //printf("Run Time : %lf",(timeend-timestart) * 1.0 / CLK_TCK);
	return 0;
}