/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call() {
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
pii p1,p2,p3;
    while(cin >> a >> b ) {
        a *= a;
        b *= b;
int64 cc = a+b;
int64 ccc = cc;
        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] >= a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] >= b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j] && j!=d) {
                    e = i;
                    f = j;
                    break;
                }
            }
            if(e!=-1) break;
        }

        if(c == -1 || e == -1) {
            call();
        } else {
             p1 = pii(-2,-2);
            p2 = pii(p1.first+c,p1.second+d);
             p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc) {
swap(e,f);
if(d==f)
call();
 p1 = pii(-2,-2);
            p2 = pii(p1.first+c,p1.second+d);
             p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc = ccc;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc)call();
}
            cout << "YES" << endl;
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call() {
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
pii p1,p2,p3;
    while(cin >> a >> b ) {
        a *= a;
        b *= b;
int64 cc = a+b;
int64 ccc = cc;
        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] >= a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
           // if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] >= b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j] && j!=d) {
                    e = i;
                    f = j;
                    break;
                }
            }
            //if(e!=-1) break;
        }

        if(c == -1 || e == -1) {
            call();
        } else {
swap(e,f);
             p1 = pii(-2,-2);
            p2 = pii(p1.first+c,p1.second+d);
             p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc) {
swap(e,f);
if(d==f)
call();
 p1 = pii(-2,-2);
            p2 = pii(p1.first+c,p1.second+d);
             p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc = ccc;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc)call();
}
            cout << "YES" << endl;
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call() {
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
    while(cin >> a >> b ) {
        a *= a;
        b *= b;
int64 cc = a+b;
        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] >= a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] >= b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j] && j!=d) {
                    e = i;
                    f = j;
                    break;
                }
            }
            //if(e!=-1) break;
        }

        if(c == -1 || e == -1) {
            call();
        } else {
            pii p1 = pii(-2,-2);
            pii p2 = pii(p1.first+c,p1.second+d);
            pii p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc) call();
            cout << "YES" << endl;
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call() {
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
    while(cin >> a >> b ) {
        a *= a;
        b *= b;

        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] > a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] > b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j] && j!=d) {
                    e = i;
                    f = j;
                    break;
                }
            }
            if(e!=-1) break;
        }

        if(c == -1 || e == -1) {
            call();
        } else {
            pii p1 = pii(1001,0);
            pii p2 = pii(p1.first+c,p1.second+d);
            pii p3 = pii(p1.first-e,p1.second+f);
            cout << "YES" << endl;
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b)
{
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b)
{
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b)
{
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000){
        arr[i] = i*i;
    }
    while(cin >> a >> b ){
        a *= a;
        b *= b;

        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000){
            if(arr[i] > a) break;
            int64 tmp = a - arr[i];
            For(j,1000){
                if(tmp == arr[j]){
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000){
            if(arr[i] > b) break;
            int64 tmp = b - arr[i];
            For(j,1000){
                if(tmp == arr[j]){
                    e = i;
                    f = j;
                    break;
                }
            }
            if(e!=-1) break;
        }
        if(c == -1 || e == -1){
            cout << "NO" << endl;
        }
        else{
            cout << "YES" << endl;
            pii p1 = pii(1001,0);
            pii p2 = pii(p1.first+c,p1.second+d);
            pii p3 = pii(p1.first-e,p1.second-f);
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call(){
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
    while(cin >> a >> b ) {
        a *= a;
        b *= b;

        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] > a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] > b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    e = i;
                    f = j;
                    break;
                }
            }
            if(e!=-1) break;
        }

            if(c == -1 || e == -1) {
                call();
            } else {
                if(a==b)swap(c,d);

                pii p1 = pii(1001,0);
                pii p2 = pii(p1.first+c,p1.second+d);
                pii p3 = pii(p1.first-e,p1.second+f);
                if(p1.first == p2.first) call();
                if(p1.second == p2.second) call();
                if(p1.first == p3.first) call();
                if(p1.second == p3.second) call();
                if(p2.first == p3.first) call();
                if(p2.second == p3.second) call();
                cout << "YES" << endl;
                cout << p1.first << " " << p1.second << endl;
                cout << p2.first << " " << p2.second << endl;
                cout << p3.first << " " << p3.second << endl;
            }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call() {
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
pii p1,p2,p3;
    while(cin >> a >> b ) {
        a *= a;
        b *= b;
int64 cc = a+b;
int64 ccc = cc;
        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] >= a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] >= b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j] && j!=d) {
                    e = i;
                    f = j;
                    break;
                }
            }
            //if(e!=-1) break;
        }

        if(c == -1 || e == -1) {
            call();
        } else {
             p1 = pii(-2,-2);
            p2 = pii(p1.first+c,p1.second+d);
             p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc) {
swap(e,f);
if(d==f)
call();
 p1 = pii(-2,-2);
            p2 = pii(p1.first+c,p1.second+d);
             p3 = pii(p1.first-e,p1.second+f);
int64 tmp = p2.first-p3.first;
tmp *= tmp;
cc = ccc;
cc -= tmp;
 tmp = p2.second-p3.second;
tmp *= tmp;
cc -= tmp;
if(cc)call();
}
            cout << "YES" << endl;
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/*
Author :: MD. Musfiqur Rahman Sanim
Aust cse 28th Batch
ID:11.02.04.097
*/

//{ Template
using namespace std;
//{ C-headers
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cassert>
#include <ctime>
//}
//{ C++-headers
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
//}
//{ Loops
#define forab(i,a,b) for (__typeof(b) i = (a); i <= (b); ++i)
#define rep(i,n) forab (i, 0, (n) - 1)
#define For(i,n) forab (i, 1, n)
#define rofba(i,a,b) for (__typeof(b) i = (b); i >= (a); --i)
#define per(i,n) rofba (i, 0, (n) - 1)
#define rof(i,n) rofba (i, 1, n)
#define forstl(i,s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
//}
//{ Floating-points
#define EPS 1e-7
#define abs(x) (((x) < 0) ? - (x) : (x))
#define zero(x) (abs (x) < EPS)
#define equal(a,b) (zero ((a) - (b)))
#define PI 2*acos (0.0)
//}
typedef long long int64;
typedef unsigned long long int64u;
#define memo(a,v) memset(a,v,sizeof(a))
#define all(a) a.begin(),a.end()
#define db double
#define pb push_back
#define pii pair<int ,int >
#define pdd pair<db ,db >
#define NL puts("")
//{
//Intput_Output
#define II ({ int a; scanf("%d",&a); a;})
#define IL ({ int64 a; scanf("%lld",&a);  a;})
#define ID ({ db a; scanf("%lf",&a);  a;})
#define IC ({ char a; scanf("%c",&a);  a;})
#define IS ({ string a; cin >> a;  a;})
#define ICA(n) ({ char a[n]; scanf("%s",&a);  a;})
#define OC printf("Case %d:",cs);
//}
//}
template <class T, class U> inline T max (T &a, U &b) {
    return a > b ? a : b;
}
template <class T, class U> inline T min (T &a, U &b) {
    return a < b ? a : b;
}
template <class T, class U> inline T swap (T &a, U &b) {
    T tmp = a;
    a = b;
    b = tmp;
}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

const int64 INF = (1ll)<<50;
const int mx = 1e5 + 7;
const int mod = 1000000007 ;
const db pi = PI;
int EQ(double d) {
    if ( fabs(d) < EPS ) return 0;
    return d > EPS ? 1 : -1 ;
}

int64 arr[1010];
void call() {
    cout << "NO" << endl;
    exit(0);
}
int main() {
#ifdef Sanim
    freopen ("in.txt", "r", stdin);
    // freopen ("output.txt", "w", stdout);
#endif
    cout << setprecision(10) << fixed;
    int64 a,b;
    For(i,1000) {
        arr[i] = i*i;
    }
    while(cin >> a >> b ) {
        a *= a;
        b *= b;

        int c = -1,d = -1,e = -1,f = -1;
        For(i,1000) {
            if(arr[i] > a) break;
            int64 tmp = a - arr[i];
            For(j,1000) {
                if(tmp == arr[j]) {
                    c = i;
                    d = j;
                    break;
                }
            }
            if(c!=-1) break;
        }

        For(i,1000) {
            if(arr[i] > b) break;
            int64 tmp = b - arr[i];
            For(j,1000) {
                if(tmp == arr[j] && j!=d) {
                    e = i;
                    f = j;
                    break;
                }
            }
            if(e!=-1) break;
        }

        if(c == -1 || e == -1) {
            call();
        } else {
            pii p1 = pii(1001,0);
            pii p2 = pii(p1.first+c,p1.second+d);
            pii p3 = pii(p2.first+e,p2.second-f);
            cout << "YES" << endl;
            cout << p1.first << " " << p1.second << endl;
            cout << p2.first << " " << p2.second << endl;
            cout << p3.first << " " << p3.second << endl;
        }

    }

}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k,p,q,d,f,g,h;
    cin>>a>>b;
    double t;
    bool u=0,f1=0,f2=0;
    v.clear();
    v1.clear();
    v2.clear();
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            break;
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            break;
        }
    }
    v.pb(mkp(n,m));
    v.pb(mkp(-n,m));
    v.pb(mkp(n,-m));
    v.pb(mkp(-n,-m));
    v.pb(mkp(m,n));
    v.pb(mkp(-m,n));
    v.pb(mkp(m,-n));
    v.pb(mkp(-m,-n));
    v1.pb(mkp(k,j));
    v1.pb(mkp(-k,j));
    v1.pb(mkp(k,-j));
    v1.pb(mkp(-k,-j));
    v1.pb(mkp(j,k));
    v1.pb(mkp(j,-k));
    v1.pb(mkp(-j,k));
    v1.pb(mkp(-j,-k));
    REP(i,0,8)
    {
        REP(l,0,8)
        {
            if(v[i].snd!=0&&v1[l].fst!=0)
            {
                p=gcd(abs(v[i].snd),abs(v[i].fst));
                q=gcd(abs(v1[l].fst),abs(v1[l].snd));
                d=v[i].snd/p;
                f=v[i].fst/p;
                g=v1[l].snd/q;
                h=v1[l].fst/q;
                if(((f==g)&&(d==-h))||((f==-g)&&(d==h)))
                {
                    u=1;
                    v2.pb(mkp(i,l));
                    break;
                }
            }
        }
        if(u) break;
    }
    if(!u||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k;
    cin>>a>>b;
    double t;
    bool f=0,f1=0,f2=0;
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            break;
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            break;
        }
    }
    v.pb(mkp(n,m));
    v.pb(mkp(-n,m));
    v.pb(mkp(n,-m));
    v.pb(mkp(-n,-m));
    v.pb(mkp(m,n));
    v.pb(mkp(-m,n));
    v.pb(mkp(m,-n));
    v.pb(mkp(-m,-n));
    v1.pb(mkp(k,j));
    v1.pb(mkp(-k,j));
    v1.pb(mkp(k,-j));
    v1.pb(mkp(-k,-j));
    v1.pb(mkp(j,k));
    v1.pb(mkp(j,-k));
    v1.pb(mkp(-j,k));
    v1.pb(mkp(-j,-k));
    REP(i,0,8)
    {
        REP(l,0,8)
        {
            if((v[i].fst/v[i].snd)==-(v1[l].snd/v1[l].fst))
            {
                //cout<<v[i].fst<<" "<<v1[l].fst<<" "<<v[i].snd<<" "<<v1[l].snd<<endl;
                f=1;
                v2.pb(mkp(i,l));
                break;
            }
        }
        if(f) break;
    }
    if(!f||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k,p,q;
    cin>>a>>b;
    double t;
    bool f=0,f1=0,f2=0;
    v.clear();
    v1.clear();
    v2.clear();
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            break;
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            break;
        }
    }
    v.pb(mkp(n,m));
    v.pb(mkp(-n,m));
    v.pb(mkp(n,-m));
    v.pb(mkp(-n,-m));
    v.pb(mkp(m,n));
    v.pb(mkp(-m,n));
    v.pb(mkp(m,-n));
    v.pb(mkp(-m,-n));
    v1.pb(mkp(k,j));
    v1.pb(mkp(-k,j));
    v1.pb(mkp(k,-j));
    v1.pb(mkp(-k,-j));
    v1.pb(mkp(j,k));
    v1.pb(mkp(j,-k));
    v1.pb(mkp(-j,k));
    v1.pb(mkp(-j,-k));
    REP(i,0,8)
    {
        REP(l,0,8)
        {
            if(v[i].snd!=0&&v1[l].fst!=0)
            {
                p=gcd(abs(v[i].snd),abs(v[i].fst));
                q=gcd(abs(v1[l].fst),abs(v1[l].snd));
                v[i].snd/=p;
                v[i].fst/=p;
                v1[l].snd/=q;
                v1[l].fst/=q;
                if(((v[i].fst==v1[l].snd)&&(v[i].snd==-v1[l].fst))||((v[i].fst==-v1[l].snd)&&(v[i].snd==v1[l].fst)))
                {
                    f=1;
                    v2.pb(mkp(i,l));
                    break;
                }
            }
        }
        if(f) break;
    }
    if(!f||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k,p,q,d,f,g,h;
    cin>>a>>b;
    double t;
    bool u=0,f1=0,f2=0;
    v.clear();
    v1.clear();
    v2.clear();
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            break;
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            break;
        }
    }
    v.pb(mkp(n,m));
    v.pb(mkp(-n,m));
    v.pb(mkp(n,-m));
    v.pb(mkp(-n,-m));
    v.pb(mkp(m,n));
    v.pb(mkp(-m,n));
    v.pb(mkp(m,-n));
    v.pb(mkp(-m,-n));
    v1.pb(mkp(k,j));
    v1.pb(mkp(-k,j));
    v1.pb(mkp(k,-j));
    v1.pb(mkp(-k,-j));
    v1.pb(mkp(j,k));
    v1.pb(mkp(j,-k));
    v1.pb(mkp(-j,k));
    v1.pb(mkp(-j,-k));
    REP(i,0,8)
    {
        REP(l,0,8)
        {
            if(v[i].snd!=0&&v1[l].fst!=0)
            {
                p=gcd(abs(v[i].snd),abs(v[i].fst));
                q=gcd(abs(v1[l].fst),abs(v1[l].snd));
                d=v[i].snd/p;
                f=v[i].fst/p;
                g=v1[l].snd/q;
                h=v1[l].fst/q;
                if(((f==g)&&(d==-h))||((f==-g)&&(d==h)))
                {
                    if((v1[i].fst!=v[i].fst)&&(v1[i].snd!=v[i].snd))
                    {
                        u=1;
                        v2.pb(mkp(i,l));
                        break;
                    }
                }
            }
        }
        if(u) break;
    }
    if(!u||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k,p,q,d,f,g,h;
    cin>>a>>b;
    double t;
    bool u=0,f1=0,f2=0;
    v.clear();
    v1.clear();
    v2.clear();
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            break;
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            break;
        }
    }
    v.pb(mkp(n,m));
    v.pb(mkp(-n,m));
    v.pb(mkp(n,-m));
    v.pb(mkp(-n,-m));
    v.pb(mkp(m,n));
    v.pb(mkp(-m,n));
    v.pb(mkp(m,-n));
    v.pb(mkp(-m,-n));
    v1.pb(mkp(k,j));
    v1.pb(mkp(-k,j));
    v1.pb(mkp(k,-j));
    v1.pb(mkp(-k,-j));
    v1.pb(mkp(j,k));
    v1.pb(mkp(j,-k));
    v1.pb(mkp(-j,k));
    v1.pb(mkp(-j,-k));
    REP(i,0,8)
    {
        REP(l,0,8)
        {
            if(v[i].snd!=0&&v1[l].fst!=0)
            {
                p=gcd(abs(v[i].snd),abs(v[i].fst));
                q=gcd(abs(v1[l].fst),abs(v1[l].snd));
                d=v[i].snd/p;
                f=v[i].fst/p;
                g=v1[l].snd/q;
                h=v1[l].fst/q;
                if(((f==g)&&(d==-h))||((f==-g)&&(d==h)))
                {
                    if((v1[l].fst!=v[i].fst)&&(v1[l].snd!=v[i].snd))
                    {
                        u=1;
                        v2.pb(mkp(i,l));
                        break;
                    }
                }
            }
        }
        if(u) break;
    }
    if(!u||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k,p,q,d,f,g,h;
    cin>>a>>b;
    double t;
    bool u=0,f1=0,f2=0;
    v.clear();
    v1.clear();
    v2.clear();
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            v.pb(mkp(n,m));
            v.pb(mkp(-n,m));
            v.pb(mkp(n,-m));
            v.pb(mkp(-n,-m));
            v.pb(mkp(m,n));
            v.pb(mkp(-m,n));
            v.pb(mkp(m,-n));
            v.pb(mkp(-m,-n));
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            v1.pb(mkp(k,j));
            v1.pb(mkp(-k,j));
            v1.pb(mkp(k,-j));
            v1.pb(mkp(-k,-j));
            v1.pb(mkp(j,k));
            v1.pb(mkp(j,-k));
            v1.pb(mkp(-j,k));
            v1.pb(mkp(-j,-k));
        }
    }
    REP(i,0,v.size())
    {
        REP(l,0,v1.size())
        {
            if(v[i].snd!=0&&v1[l].fst!=0)
            {
                p=gcd(abs(v[i].snd),abs(v[i].fst));
                q=gcd(abs(v1[l].fst),abs(v1[l].snd));
                d=v[i].snd/p;
                f=v[i].fst/p;
                g=v1[l].snd/q;
                h=v1[l].fst/q;
                if(((f==g)&&(d==-h))||((f==-g)&&(d==h)))
                {
                    if((v1[l].fst!=v[i].fst)&&(v1[l].snd!=v[i].snd))
                    {
                        u=1;
                        v2.pb(mkp(i,l));
                        break;
                    }
                }
            }
        }
        if(u) break;
    }
    if(!u||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


/* Created by Anmol Varshney */

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

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define REP(p,a,b) for(int p=a;p<b;p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a,b) make_pair(a,b)
#define fst first
#define snd second
#define pii pair<int,int>
#define ins(a) insert(a)
#define IOS ios_base::sync_with_stdio(false)
#define citi cin.tie(NULL);
#define FREI(var) freopen(var,"r",stdin)
#define FREO(var) freopen(var,"w",stdout)

ll fast_pow(ll a,ll b,ll M)
{
	ll res = 1;
	while ( b > 0 ) {
		if ( b&1 ) res = (res*a)%M;
		a = (a*a)%M;
		b >>= 1;
	}
	return res;
}

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    else if(b!=0) return gcd(b,a%b);
    else return a;
}

string to_string(int num)
{
    stringstream ss;
    ss << num;
    return ss.str();
}

vector< pii > v,v1,v2;

int main()
{
    //FREI("input.txt");
    //FREO("output.txt");
    //IOS;
    //citi;
    int a,b,m,n,j,k;
    cin>>a>>b;
    double t;
    bool f=0,f1=0,f2=0;
    v.clear();
    v1.clear();
    v2.clear();
    REP(i,1,a)
    {
        t=a*a-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f1=1;
            m=i;
            n=(int)sqrt(t);
            break;
        }
    }
    REP(i,1,b)
    {
        t=b*b-i*i;
        if(sqrt(t)==floor(sqrt(t)))
        {
            f2=1;
            j=i;
            k=(int)sqrt(t);
            break;
        }
    }
    v.pb(mkp(n,m));
    v.pb(mkp(-n,m));
    v.pb(mkp(n,-m));
    v.pb(mkp(-n,-m));
    v.pb(mkp(m,n));
    v.pb(mkp(-m,n));
    v.pb(mkp(m,-n));
    v.pb(mkp(-m,-n));
    v1.pb(mkp(k,j));
    v1.pb(mkp(-k,j));
    v1.pb(mkp(k,-j));
    v1.pb(mkp(-k,-j));
    v1.pb(mkp(j,k));
    v1.pb(mkp(j,-k));
    v1.pb(mkp(-j,k));
    v1.pb(mkp(-j,-k));
    REP(i,0,8)
    {
        REP(l,0,8)
        {
            if(v[i].snd!=0&&v1[l].fst!=0&&(v[i].fst/v[i].snd)==-(v1[l].snd/v1[l].fst))
            {
                f=1;
                v2.pb(mkp(i,l));
                break;
            }
        }
        if(f) break;
    }
    if(!f||!f1||!f2)
    {
        cout<<"NO\n";
        return 0;
    }
    int ind1,ind2;
    ind1=v2[0].fst,ind2=v2[0].snd;
    cout<<"YES\n";
    cout<<"0 0\n";
    cout<<0+v[ind1].snd<<" "<<0+v[ind1].fst<<endl;
    cout<<0+v1[ind2].snd<<" "<<0+v1[ind2].fst<<endl;
    return 0;
}


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define maxn 10000007

using namespace std;

ld a, b, x, y, eps = 0.000001, x11, y11, x2, y2;
bool br = 0;

int main()
{
	cin >> a >> b;

	for (x11 = -a+1; x11 < a; x11++){
		y11 = sqrt(a*a - x11*x11);
		if (fabs(y11 - round(y11)) < eps && fabs(x11) > eps){
		for (x2 = -b+1; x2 < b; x2++){
			y2 = sqrt(b*b - x2*x2);

			if (fabs(y11 - round(y11)) < eps && fabs(y2 - round(y2)) < eps && fabs(x2) > eps && abs(x11 - x2) > eps && abs(y11 - y2) > eps){
				if (x11*x2 + y11*y2 < eps){
					br = 1;
					break;
				}
			}
		}
		if (br)
			break;
		}
	}

	if (!br){
		cout << "NO";
	}
	else{
		cout << "YES\n";
		cout << 0 << " " << 0 << endl;
		cout << round(x11) << " " << round(y11) << endl;
		cout << round(x2) << " " << round(y2) << endl;
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define maxn 10000007

using namespace std;

ld a, b, x, y, eps = 0.00001, x11, y11, x2, y2;
bool br = 0;

int main()
{
	cin >> a >> b;

	for (x11 = -a+1; x11 < a; x11++){
		y11 = sqrt(a*a - x11*x11);
		if (abs(y11 - round(y11)) < eps && abs(x11) > eps){
		for (x2 = -b+1; x2 < b; x2++){
			y2 = sqrt(b*b - x2*x2);

			if (abs(y11 - round(y11)) < eps && abs(y2 - round(y2)) < eps && abs(x2) > eps){
				if (x11*x2 + y11*y2 < eps){
					br = 1;
					break;
				}
			}
		}
		if (br)
			break;
		}
	}

	if (!br){
		cout << "NO";
	}
	else{
		cout << "YES\n";
		cout << 0 << " " << 0 << endl;
		cout << round(x11) << " " << round(y11) << endl;
		cout << round(x2) << " " << round(y2) << endl;
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define maxn 10000007

using namespace std;

ld a, b, x, y, eps = 0.000001, x11, y11, x2, y2;
bool br = 0;

int main()
{
	cin >> a >> b;

	for (x11 = -a+1; x11 < a; x11++){
		y11 = sqrt(a*a - x11*x11);
		if (fabs(y11 - round(y11)) < eps && fabs(x11) > eps){
		for (x2 = -b+1; x2 < b; x2++){
			y2 = sqrt(b*b - x2*x2);

			if (fabs(y11 - round(y11)) < eps && fabs(y2 - round(y2)) < eps){
				if (abs(x11*x2 + y11*y2) < eps){
					br = 1;
					break;
				}
			}
		}
		if (br)
			break;
		}
	}

	if (!br){
		cout << "NO";
	}
	else{
		cout << "YES\n";
		cout << 0 << " " << 0 << endl;
		cout << round(x11) << " " << round(y11) << endl;
		cout << round(x2) << " " << round(y2) << endl;
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[5000+5];
int Sum[5000+5];
int array[100+5];
int Last[100+5];
int main(){
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			//cout<<t<<endl;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		if(f1 == 0 || Ans < Max1)
			Ans = Max1;
	}
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	printf("%I64d\n",Ans);
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[5000+5];
int Sum[5000+5];
int array[10000+5];
int Last[100+5];
int ans[1000+5];
int main(){
	int i,j,n,m;
	cin>>n>>m;
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		if(f == 0  || Max[i] < cur){
			f = 1;Max[i] = cur;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		if(f == 0 || Last[i] < cur){
			f = 1;Last[i] = cur;
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		ans[i] = Max1;
	}
	//printf("%I64d\n",Ans);
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || Ans < ans[k]){
			f1 = 1;Ans = ans[k];
		}
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	cout<<Ans<<endl;
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
__int64 Max[5000+5];
__int64 Sum[5000+5];
__int64 array[10000+5];
__int64 Last[100+5];
__int64 ans[1000+5];
int main(){
	int i,j,n,m;
	cin>>n>>m;
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
			if(cur < 0) cur = 0;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		ans[i] = Max1;
	}
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || Ans < ans[k]){
			f1 = 1;Ans = ans[k];
		}
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	cout<<Ans<<endl;
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[5000+5];
int Sum[5000+5];
int array[100+5];
int Last[100+5];
int main(){
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			//cout<<t<<endl;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		//printf("%d\n",Max[i]);
	}
	__int64 Ans = 0,f = 0,cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	printf("%I64d\n",Ans);
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[5000+5];
int Sum[5000+5];
int array[100+5];
int Last[100+5];
int ans[1000+5];
int main(){
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			//cout<<t<<endl;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		ans[i] = Max1;
	}
	//printf("%I64d\n",Ans);
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || Ans < ans[k]){
			f1 = 1;Ans = ans[k];
		}
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	printf("%I64d\n",Ans);
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[5000+5];
int Sum[5000+5];
int array[10000+5];
int Last[100+5];
int ans[1000+5];
int main(){
	int i,j,n,m;
	cin>>n>>m;
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		ans[i] = Max1;
	}
	//printf("%I64d\n",Ans);
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || Ans < ans[k]){
			f1 = 1;Ans = ans[k];
		}
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	cout<<Ans<<endl;
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
__int64 Max[5000+5];
__int64 Sum[5000+5];
__int64 array[10000+5];
__int64 Last[100+5];
__int64 ans[1000+5];
int main(){
	int i,j,n,m;
	cin>>n>>m;
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		ans[i] = Max1;
	}
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || Ans < ans[k]){
			f1 = 1;Ans = ans[k];
		}
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		/*if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}*/
		if(cur + Sum[k] <= 0 && Last[k] <= 0){
			cur = 0;
		}else{
			cur = max(cur+Sum[k],Last[k]);
		}
	}
	cout<<Ans<<endl;
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
__int64 Max[5000+5];
__int64 Sum[5000+5];
int main(){
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			//cout<<t<<endl;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		//printf("%d\n",Max[i]);
	}
	__int64 Ans = 0,f = 0,cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else cur = 0;
	}
	printf("%I64d\n",Ans);
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[5000+5];
int Sum[5000+5];
int array[10000+5];
int Last[100+5];
int ans[1000+5];
int main(){
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	__int64 Ans = 0,f1 = 0;
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			array[j] = t;
			//cout<<t<<endl;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		Last[i] = 0;
		f = 0;
		cur = 0;
		for(j = l;j >= 1;j--){
			if(f == 0 || Last[i] < cur + array[j]){
				f = 1;Last[i] = cur + array[j];
			}
			cur = cur + array[j];
		}
		cur = 0;
		int Max1 = 0;
		f = 0;
		for(j = 1;j <= l;j++){
			if(f == 0 || Max1 < cur + array[j]){
				f = 1;Max1 = cur + array[j];
			}
			if(cur + array[j] >= 0) cur = cur + array[j];
			else cur = 0;
		}
		ans[i] = Max1;
	}
	//printf("%I64d\n",Ans);
	__int64 cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		cin>>k;
		if(f1 == 0 || Ans < ans[k]){
			f1 = 1;Ans = ans[k];
		}
		if(f1 == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f1 = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else{
			if(Last[k] >= 0) cur = Last[k];
			else cur = 0;
		}
	}
	printf("%I64d\n",Ans);
}


#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
#define MP make_pair
#define PB push_back

const int N = 10000+5;
int Max[1000+5];
int Sum[1000+5];
int main(){
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	for(i = 1;i <= n;i++){
		int cur = 0,l,t,sum = 0;
		Max[i] = 0;
		int f = 0;
		cin>>l;
		for(j = 1;j <= l;j++){
			cin>>t;
			//cout<<t<<endl;
			sum += t;
			if(f == 0 || Max[i] < cur + t){
				f = 1;Max[i] = cur + t;
			}
			cur = cur + t;
		}
		Sum[i] = sum;
		//printf("%d\n",Max[i]);
	}
	__int64 Ans = 0,f = 0,cur = 0;
	for(i = 1;i <= m;i++){
		int k;
		scanf("%d",&k);
		if(f == 0 || cur + Max[k] > Ans){
			Ans = cur + Max[k];
			f = 1;
		}
		if(cur + Sum[k] >= 0) cur = cur + Sum[k];
		else cur = 0;
	}
	printf("%I64d\n",Ans);
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <bitset>
#include <set>

using namespace std;

int inf[60][5], a[250010], b[5010], f[5010], n, m, amax;
long long res;

int main() {
    scanf("%d%d\n", &n, &m);

    for (int i = 0; i < n; i++) {
        int l;
        scanf("%d", &l);
        for (int j = 0; j < l; j++) scanf("%d", &b[j]);

        inf[i][4] = b[0];
        for (int j = 0; j < l; j++) inf[i][4] = max(inf[i][4], b[j]);

        int s = 0;
        inf[i][0] = 0;
        for (int j = 0; j < l; j++) {
            s += b[j];
            inf[i][0] = max(inf[i][0], s);
        }

        s = 0;
        inf[i][2] = 0;
        for (int j = l - 1; j >= 0; j--) {
            s += b[j];
            inf[i][2] = max(inf[i][2], s);
        }

        f[0] = b[0];

        for (int j = 1; j < l; j++)
            f[j] = max(b[j], b[j] + f[j - 1]);

        inf[i][1] = 0;
        for (int j = 0; j < l; j++)
            inf[i][1] = max(inf[i][1], f[j]);

        inf[i][3] = 0;
        for (int j = 0; j < l; j++) inf[i][3] += b[j];

        //for (int j = 0; j < 4; j++) cout << inf[i][j] << " " ;
        //cout << endl;
    }

    amax = -10000;
    for (int i = 0; i < m; i++) {
        scanf("%d", &a[i]);
        amax = max(amax, inf[a[i]][4]);
    }

    int sum = 0;
    res = amax;

    for (int i = 0; i < m; i++) {
        res = max(res, (long long) inf[a[i] - 1][1]);
        res = max(res, (long long) sum + inf[a[i] - 1][0]);

        sum = max(sum + inf[a[i] - 1][3], inf[a[i] - 1][2]);
        sum = max(sum, 0);
    }

    if (amax < 0) printf("%d", amax);
    else printf("%I64d", res);

   //system("pause");
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <bitset>
#include <set>

using namespace std;

int inf[60][5], a[250010], b[5010], f[5010], n, m, amax;
long long res;

int main() {
    scanf("%d%d\n", &n, &m);

    for (int i = 0; i < n; i++) {
        int l;
        scanf("%d", &l);
        for (int j = 0; j < l; j++) scanf("%d", &b[j]);

        inf[i][4] = b[0];
        for (int j = 0; j < l; j++) inf[i][4] = max(inf[i][4], b[j]);

        int s = 0;
        inf[i][0] = 0;
        for (int j = 0; j < l; j++) {
            s += b[j];
            inf[i][0] = max(inf[i][0], s);
        }

        s = 0;
        inf[i][2] = 0;
        for (int j = l - 1; j >= 0; j--) {
            s += b[j];
            inf[i][2] = max(inf[i][2], s);
        }

        f[0] = b[0];

        for (int j = 1; j < l; j++)
            f[j] = max(b[j], b[j] + f[j - 1]);

        inf[i][1] = 0;
        for (int j = 0; j < l; j++)
            inf[i][1] = max(inf[i][1], f[j]);

        inf[i][3] = 0;
        for (int j = 0; j < l; j++) inf[i][3] += b[j];

        //for (int j = 0; j < 4; j++) cout << inf[i][j] << " " ;
        //cout << endl;
    }

    amax = -10000;
    for (int i = 0; i < m; i++) {
        scanf("%d", &a[i]);
        amax = max(amax, inf[a[i] - 1][4]);
    }

    long long sum = 0;
    res = amax;

    for (int i = 0; i < m; i++) {
        res = max(res, (long long) inf[a[i] - 1][1]);
        res = max(res, sum + inf[a[i] - 1][0]);

        sum = max(sum + inf[a[i] - 1][3], (long long) inf[a[i] - 1][2]);
        sum = max(sum, 0LL);
    }

    if (amax < 0) printf("%d", amax);
    else printf("%I64d", res);

   //system("pause");
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <bitset>
#include <set>

using namespace std;

int del0(int x) {
    int y = 0, p = 1;
    while (x > 0) {
          if (x % 10 != 0) {
             y += (x % 10) * p;
             p *= 10;
          }

          x /= 10;
    }

    return y;
}
 int main() {
     int a, b, c;
     scanf("%d %d", &a, &b);
     c = a + b;
     if (del0(a) + del0(b) == del0(c)) cout << "YES";
     else cout << "NO";
     //system("pause");
 }


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <bitset>
#include <set>

using namespace std;

int inf[60][5], a[250010], b[5010], f[5010], n, m, amax;
long long res;

int main() {
    scanf("%d%d\n", &n, &m);

    for (int i = 0; i < n; i++) {
        int l;
        scanf("%d", &l);
        for (int j = 0; j < l; j++) scanf("%d", &b[j]);

        inf[i][4] = b[0];
        for (int j = 0; j < l; j++) inf[i][4] = max(inf[i][4], b[j]);

        int s = 0;
        inf[i][0] = 0;
        for (int j = 0; j < l; j++) {
            s += b[j];
            inf[i][0] = max(inf[i][0], s);
        }

        s = 0;
        inf[i][2] = 0;
        for (int j = l - 1; j >= 0; j--) {
            s += b[j];
            inf[i][2] = max(inf[i][2], s);
        }

        f[0] = b[0];

        for (int j = 1; j < l; j++)
            f[j] = max(b[j], b[j] + f[j - 1]);

        inf[i][1] = 0;
        for (int j = 0; j < l; j++)
            inf[i][1] = max(inf[i][1], f[j]);

        inf[i][3] = 0;
        for (int j = 0; j < l; j++) inf[i][3] += b[j];

        //for (int j = 0; j < 4; j++) cout << inf[i][j] << " " ;
        //cout << endl;
    }

    amax = -10000;
    for (int i = 0; i < m; i++) {
        scanf("%d", &a[i]);
        amax = max(amax, inf[a[i] - 1][4]);
    }

    int sum = 0;
    res = amax;

    for (int i = 0; i < m; i++) {
        res = max(res, (long long) inf[a[i] - 1][1]);
        res = max(res, (long long) sum + inf[a[i] - 1][0]);

        sum = max(sum + inf[a[i] - 1][3], inf[a[i] - 1][2]);
        sum = max(sum, 0);
    }

    if (amax < 0) printf("%d", amax);
    else printf("%I64d", res);

   //system("pause");
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <deque>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <bitset>
#include <set>

using namespace std;

int inf[60][4], a[250010], b[5010], f[5010], n, m, amax;
long long res;

int main() {
    scanf("%d%d\n", &n, &m);
    amax = -1000;

    for (int i = 0; i < n; i++) {
        int l;
        scanf("%d", &l);
        for (int j = 0; j < l; j++) scanf("%d", &b[j]);

        for (int j = 0; j < l; j++) amax = max(amax, b[j]);

        int s = 0;
        inf[i][0] = 0;
        for (int j = 0; j < l; j++) {
            s += b[j];
            inf[i][0] = max(inf[i][0], s);
        }

        s = 0;
        inf[i][2] = 0;
        for (int j = l - 1; j >= 0; j--) {
            s += b[j];
            inf[i][2] = max(inf[i][2], s);
        }

        f[0] = b[0];

        for (int j = 1; j < l; j++)
            f[j] = max(b[j], b[j] + f[j - 1]);

        inf[i][1] = 0;
        for (int j = 0; j < l; j++)
            inf[i][1] = max(inf[i][1], f[j]);

        inf[i][3] = 0;
        for (int j = 0; j < l; j++) inf[i][3] += b[j];

        //for (int j = 0; j < 4; j++) cout << inf[i][j] << " " ;
        //cout << endl;
    }

    for (int i = 0; i < m; i++) scanf("%d", &a[i]);

    int sum = 0;
    res = amax;

    for (int i = 0; i < m; i++) {
        res = max(res, (long long) inf[a[i] - 1][1]);
        res = max(res, (long long) sum + inf[a[i] - 1][0]);

        sum = max(sum + inf[a[i] - 1][3], inf[a[i] - 1][2]);
        sum = max(sum, 0);
    }

    if (amax < 0) printf("%d", amax);
    else printf("%I64d", res);

   // system("pause");
}


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

#define INF 1e9
#define EPS 1e-9
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long
#define SZ(c) (int)c.size()
#define ALL(c) (c).begin(), (c).end()
#define SET(c, v) memset(c, v, sizeof(c))
#define FOR(i, s, e) for(int i = s; i < e; ++i)
#define MAX (int)1e5 + 10
#define MOD 1000000007

struct SmallArrays {
    LL Sum;
    LL MaxSum;
    LL Suffix;
    LL Prefix;
} arr[60];

LL A[5001], tmp, mx, mxx;

int main() {
    int n, m, sz, x; cin >> n >> m;
    FOR(i, 0, n) {
        mx = -10000;
        arr[i].Sum = arr[i].MaxSum = arr[i].Suffix = arr[i].Prefix = tmp = 0;
        cin >> sz;
        FOR(j, 0, sz) {
            cin >> A[j];
            arr[i].Sum += A[j];
            tmp += A[j];
            if(tmp < 0) tmp = 0;
            arr[i].MaxSum = max(arr[i].MaxSum, tmp);
            mx = max(mx, A[j]);
        }
        if(mx < 0) arr[i].MaxSum = mx;
        mx = mxx = 0;
        FOR(j, 0, sz) {
            mx = max(mx, arr[i].Prefix);
            arr[i].Prefix += A[j];
            mxx = max(mxx, arr[i].Suffix);
            arr[i].Suffix += A[sz - j - 1];
        }
        arr[i].Prefix = max(arr[i].Prefix, mx);
        arr[i].Suffix = max(arr[i].Suffix, mxx);
    }
    cin >> x; --x; --m;
    SmallArrays res;
    res.MaxSum = arr[x].MaxSum;
    res.Prefix = arr[x].Prefix;
    res.Suffix = arr[x].Suffix;
    res.Sum = arr[x].Sum;
    while(m--) {
        cin >> x; --x;
        res.MaxSum = max(res.MaxSum, max(arr[x].MaxSum, max(res.Sum + arr[x].Sum, res.Suffix + arr[x].Prefix)));
        res.Prefix = max(res.Prefix, res.Sum + arr[x].Prefix);
        res.Suffix = max(arr[x].Suffix, arr[x].Sum + res.Suffix);
        res.Sum += arr[x].Sum;
    }
    cout << res.MaxSum << endl;
    return 0;
}


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

#define INF 1e9
#define EPS 1e-9
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long
#define SZ(c) (int)c.size()
#define ALL(c) (c).begin(), (c).end()
#define SET(c, v) memset(c, v, sizeof(c))
#define FOR(i, s, e) for(int i = s; i < e; ++i)
#define MAX (int)1e5 + 10
#define MOD 1000000007

struct SmallArrays {
    LL Sum;
    LL MaxSum;
    LL Suffix;
    LL Prefix;
    LL MaxOne;
} arr[60];

LL A[5001], tmp, mx1, mx2;

int main() {
    int n, m, sz, x; cin >> n >> m;
    FOR(i, 0, n) {
        arr[i].MaxOne = -10000;
        arr[i].Sum = arr[i].MaxSum = arr[i].Suffix = arr[i].Prefix = tmp = 0;
        cin >> sz;
        FOR(j, 0, sz) {
            cin >> A[j];
            arr[i].Sum += A[j];
            tmp += A[j];
            if(tmp < 0) tmp = 0;
            arr[i].MaxSum = max(arr[i].MaxSum, tmp);
            arr[i].MaxOne = max(arr[i].MaxOne, A[j]);
        }
        if(arr[i].MaxOne < 0) arr[i].MaxSum = arr[i].MaxOne;
        mx1 = mx2 = 0;
        FOR(j, 0, sz) {
            mx1 = max(mx1, arr[i].Prefix);
            arr[i].Prefix += A[j];
            mx2 = max(mx2, arr[i].Suffix);
            arr[i].Suffix += A[sz - j - 1];
        }
        arr[i].Prefix = max(arr[i].Prefix, mx1);
        arr[i].Suffix = max(arr[i].Suffix, mx2);
    }
    cin >> x; --x; --m;
    SmallArrays res;
    res.MaxSum = arr[x].MaxSum;
    res.Prefix = arr[x].Prefix;
    res.Suffix = arr[x].Suffix;
    res.MaxOne = arr[x].MaxOne;
    res.Sum = arr[x].Sum;
    while(m--) {
        cin >> x; --x;
        res.MaxSum = max(res.MaxSum, max(arr[x].MaxSum, res.Suffix + arr[x].Prefix));
        res.Prefix = max(res.Prefix, res.Sum + arr[x].Prefix);
        res.Suffix = max(arr[x].Suffix, arr[x].Sum + res.Suffix);
        res.MaxOne = max(res.MaxOne, arr[x].MaxOne);
        res.Sum += arr[x].Sum;
    }
    if(res.MaxOne < 0) res.MaxSum = res.MaxOne;
    cout << res.MaxSum << endl;
    return 0;
}


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

#define INF 1e9
#define EPS 1e-9
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long
#define SZ(c) (int)c.size()
#define ALL(c) (c).begin(), (c).end()
#define SET(c, v) memset(c, v, sizeof(c))
#define FOR(i, s, e) for(int i = s; i < e; ++i)
#define MAX (int)1e5 + 10
#define MOD 1000000007

struct SmallArrays {
    LL Sum;
    LL MaxSum;
    LL Suffix;
    LL Prefix;
} arr[60];

LL A[5001], tmp, mxall, mx, mxx;

int main() {
    int n, m, sz, x; cin >> n >> m;
    mxall = -10000;
    FOR(i, 0, n) {
        mx = -10000;
        arr[i].Sum = arr[i].MaxSum = arr[i].Suffix = arr[i].Prefix = tmp = 0;
        cin >> sz;
        FOR(j, 0, sz) {
            cin >> A[j];
            arr[i].Sum += A[j];
            tmp += A[j];
            if(tmp < 0) tmp = 0;
            arr[i].MaxSum = max(arr[i].MaxSum, tmp);
            mx = max(mx, A[j]);
            mxall = max(mxall, A[j]);
        }
        if(mx < 0) arr[i].MaxSum = mx;
        mx = mxx = 0;
        FOR(j, 0, sz) {
            mx = max(mx, arr[i].Prefix);
            arr[i].Prefix += A[j];
            mxx = max(mxx, arr[i].Suffix);
            arr[i].Suffix += A[sz - j - 1];
        }
        arr[i].Prefix = max(arr[i].Prefix, mx);
        arr[i].Suffix = max(arr[i].Suffix, mxx);
    }
    cin >> x; --x; --m;
    SmallArrays res;
    res.MaxSum = arr[x].MaxSum;
    res.Prefix = arr[x].Prefix;
    res.Suffix = arr[x].Suffix;
    res.Sum = arr[x].Sum;
    while(m--) {
        cin >> x; --x;
        res.MaxSum = max(res.MaxSum, max(arr[x].MaxSum, max(res.Sum + arr[x].Sum, res.Suffix + arr[x].Prefix)));
        res.Prefix = max(res.Prefix, res.Sum + arr[x].Prefix);
        res.Suffix = max(arr[x].Suffix, arr[x].Sum + res.Suffix);
        res.Sum += arr[x].Sum;
    }
    if(mxall < 0) res.MaxSum = mxall;
    cout << res.MaxSum << endl;
    return 0;
}


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

#define INF 1e9
#define EPS 1e-9
#define MP make_pair
#define PB push_back
#define LL long long
#define ULL unsigned long long
#define SZ(c) (int)c.size()
#define ALL(c) (c).begin(), (c).end()
#define SET(c, v) memset(c, v, sizeof(c))
#define FOR(i, s, e) for(int i = s; i < e; ++i)
#define MAX (int)1e5 + 10
#define MOD 1000000007

vector <LL> V[55];

int main() {
    int n, m, sz, x; cin >> n >> m;
    FOR(i, 0, n) {
        cin >> sz;
        FOR(j, 0, sz) cin >> x, V[i + 1].push_back(x);
    }
    LL MaxSum = 0, tmp = 0, mx = -10000;
    while(m--) {
        cin >> x;
        FOR(i, 0, SZ(V[x])) {
            tmp += V[x][i];
            if(tmp < 0) tmp = 0;
            MaxSum = max(MaxSum, tmp);
            mx = max(mx, V[x][i]);
        }
    }
    if(mx < 0) MaxSum = mx;
    cout << MaxSum << endl;
    return 0;
}


/* understand it at your own risk */
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <climits>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl
#define si size()
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<string> vs;
typedef long long ii;
typedef long long i64;
typedef long long intint;
typedef pair<int,int> pii;
#define mod(A,B) ((((A) % (B)) + (B)) % (B))
#define b2e(A) (A).begin(), (A).end()
#define e2b(A) (A).rbegin(), (A).rend()
#define rev(A) std::reverse(b2e(A))
#define s(A) std::sort(b2e(A))
#define ss(A) std::stable_sort(b2e(A))
#define un(A) std::unique(b2e(A))
#define er(A) (A).erase(un(A), (A).end())
#define Fill(A,B) std::fill(b2e(A), B)
#define minelt(A) *min_element(b2e(A))
#define maxelt(A) *max_element(b2e(A))
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forr(i,a,b) for(int i=b;i>=a;i--)
#define set(A,i) memset(A,i,sizeof A)
#define pb(i) push_back(i)
#define INF (1<<30)
void swap(int &a,int &b) { int t=a;a=b;b=t; }
//int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1};
ii gcd(ii a, ii b) {if (a == 0 || b == 0) return max(a,b); if (b % a == 0) return a; return gcd(b%a, a);}

//------------------------------------------------------//

//----------------------main code----------------------//
int main()
{
	int n,m;
	cin>>n>>m;
	vi br(m);
	vvi lr(n);
	For(i,0,n)
	{
		int s;
		cin>>s;
		lr[i].resize(s);
		For(j,0,s)
		cin>>lr[i][j];
	}
	For(i,0,m)
	{
		cin>>br[i];
		br[i]--;
	}

	For(i,0,n)
	For(j,1,lr[i].size())
	lr[i][j]+=lr[i][j-1];

	vi maxa(n),mini(n);
	For(i,0,n)
	{
		maxa[i]=*max_element(b2e(lr[i]));
		mini[i]=*min_element(b2e(lr[i]));
	}
	vector<int> ians(n);
	For(i,0,n)
	{
		int mans,mi;
		mans=lr[i][0];mi=0;
		For(j,1,lr[i].size())
		{
			mans=max(lr[i][j]-mi,mans);
			mi=min(mi,lr[i][j]);
		}
		ians[i]=mans;
	}
	ans=ians[br[0]];
	For(i,1,m)
	ans=max(ans,(long long int)ians[br[i]]);
	long long int minin=min(0,mini[br[0]]);
	long long int cm=lr[br[0]][lr[br[0]].size()-1];
	For(i,1,m)
	{
		ans=max(ans,maxa[br[i]]+cm-minin);
		minin=min(minin,mini[br[i]]+cm);
		cm+=lr[br[i]][lr[br[i]].size()-1];
	}
	cout<<ans<<endl;
}


/* understand it at your own risk */
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <climits>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl
#define si size()
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<string> vs;
typedef long long ii;
typedef long long i64;
typedef long long intint;
typedef pair<int,int> pii;
#define mod(A,B) ((((A) % (B)) + (B)) % (B))
#define b2e(A) (A).begin(), (A).end()
#define e2b(A) (A).rbegin(), (A).rend()
#define rev(A) std::reverse(b2e(A))
#define s(A) std::sort(b2e(A))
#define ss(A) std::stable_sort(b2e(A))
#define un(A) std::unique(b2e(A))
#define er(A) (A).erase(un(A), (A).end())
#define Fill(A,B) std::fill(b2e(A), B)
#define minelt(A) *min_element(b2e(A))
#define maxelt(A) *max_element(b2e(A))
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forr(i,a,b) for(int i=b;i>=a;i--)
#define set(A,i) memset(A,i,sizeof A)
#define pb(i) push_back(i)
#define INF (1<<30)
void swap(int &a,int &b) { int t=a;a=b;b=t; }
//int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1};
ii gcd(ii a, ii b) {if (a == 0 || b == 0) return max(a,b); if (b % a == 0) return a; return gcd(b%a, a);}

//------------------------------------------------------//

//----------------------main code----------------------//
int main()
{
	int n,m;
	cin>>n>>m;
	vi br(m);
	vvi lr(n);
	For(i,0,n)
	{
		int s;
		cin>>s;
		lr[i].resize(s);
		For(j,0,s)
		cin>>lr[i][j];
	}
	For(i,0,m)
	{
		cin>>br[i];
		br[i]--;
	}

	For(i,0,n)
	For(j,1,lr[i].size())
	lr[i][j]+=lr[i][j-1];

	vi maxa(n),mini(n);
	For(i,0,n)
	{
		maxa[i]=*max_element(b2e(lr[i]));
		mini[i]=*min_element(b2e(lr[i]));
	}
	vector<i64> ians(n);
	long long int ans=-10000;
	For(i,0,n)
	{
		long long int mans,mi;
		mans=lr[i][0];mi=0;
		For(j,1,lr[i].size())
		{
			mans=max(lr[i][j]-mi,mans);
			mi=min(mi,(long long int)lr[i][j]);
		}
		ians[i]=mans;
	}
	For(i,0,m)
	ans=max(ans,ians[br[i]]);
	long long int minin=min(0,mini[br[0]]);
	long long int cm=lr[br[0]][lr[br[0]].size()-1];
	For(i,1,m)
	{
		ans=max(ans,maxa[br[i]]+cm-minin);
		minin=min(minin,mini[br[i]]+cm);
		cm+=lr[br[i]][lr[br[i]].size()-1];
	}
	cout<<ans<<endl;
}


/* understand it at your own risk */
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <climits>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl
#define si size()
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<string> vs;
typedef long long ii;
typedef long long i64;
typedef long long intint;
typedef pair<int,int> pii;
#define mod(A,B) ((((A) % (B)) + (B)) % (B))
#define b2e(A) (A).begin(), (A).end()
#define e2b(A) (A).rbegin(), (A).rend()
#define rev(A) std::reverse(b2e(A))
#define s(A) std::sort(b2e(A))
#define ss(A) std::stable_sort(b2e(A))
#define un(A) std::unique(b2e(A))
#define er(A) (A).erase(un(A), (A).end())
#define Fill(A,B) std::fill(b2e(A), B)
#define minelt(A) *min_element(b2e(A))
#define maxelt(A) *max_element(b2e(A))
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forr(i,a,b) for(int i=b;i>=a;i--)
#define set(A,i) memset(A,i,sizeof A)
#define pb(i) push_back(i)
#define INF (1<<30)
void swap(int &a,int &b) { int t=a;a=b;b=t; }
//int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1};
ii gcd(ii a, ii b) {if (a == 0 || b == 0) return max(a,b); if (b % a == 0) return a; return gcd(b%a, a);}

//------------------------------------------------------//

//----------------------main code----------------------//
int main()
{
	int n,m;
	cin>>n>>m;
	vi br(m);
	vvi lr(n);
	For(i,0,n)
	{
		int s;
		cin>>s;
		lr[i].resize(s);
		For(j,0,s)
		cin>>lr[i][j];
	}
	For(i,0,m)
	{
		cin>>br[i];
		br[i]--;
	}

	For(i,0,n)
	For(j,1,lr[i].size())
	lr[i][j]+=lr[i][j-1];

	vi maxa(n),mini(n);
	For(i,0,n)
	{
		maxa[i]=*max_element(b2e(lr[i]));
		mini[i]=*min_element(b2e(lr[i]));
	}
	vector<int> ians(n);
	For(i,0,n)
	{
		int mans,mi;
		mans=lr[i][0];mi=min(0,lr[i][0]);
		For(j,1,lr[i].size())
		{
			mans=max(lr[i][j]-mi,mans);
			mi=min(mi,lr[i][j]);
		}
		ians[i]=mans;
	}
	long long int ans=ians[br[0]];
	For(i,1,m)
	ans=max(ans,(long long int)ians[br[i]]);
	long long int minin=min(0,mini[br[0]]);
	long long int cm=lr[br[0]][lr[br[0]].size()-1];
	For(i,1,m)
	{
		ans=max(ans,maxa[br[i]]+cm-minin);
		minin=min(minin,mini[br[i]]+cm);
		cm+=lr[br[i]][lr[br[i]].size()-1];
	}
	ans=max(ans,cm);
	cout<<ans<<endl;
}


/* understand it at your own risk */
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <climits>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl
#define si size()
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<bool> vb;
typedef vector<vector<bool> > vvb;
typedef vector<string> vs;
typedef long long ii;
typedef long long i64;
typedef long long intint;
typedef pair<int,int> pii;
#define mod(A,B) ((((A) % (B)) + (B)) % (B))
#define b2e(A) (A).begin(), (A).end()
#define e2b(A) (A).rbegin(), (A).rend()
#define rev(A) std::reverse(b2e(A))
#define s(A) std::sort(b2e(A))
#define ss(A) std::stable_sort(b2e(A))
#define un(A) std::unique(b2e(A))
#define er(A) (A).erase(un(A), (A).end())
#define Fill(A,B) std::fill(b2e(A), B)
#define minelt(A) *min_element(b2e(A))
#define maxelt(A) *max_element(b2e(A))
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forr(i,a,b) for(int i=b;i>=a;i--)
#define set(A,i) memset(A,i,sizeof A)
#define pb(i) push_back(i)
#define INF (1<<30)
void swap(int &a,int &b) { int t=a;a=b;b=t; }
//int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
//int dx[] = {1,1,1,0,0,-1,-1,-1}, dy[] = {1,0,-1,1,-1,1,0,-1};
ii gcd(ii a, ii b) {if (a == 0 || b == 0) return max(a,b); if (b % a == 0) return a; return gcd(b%a, a);}

//------------------------------------------------------//

//----------------------main code----------------------//
int main()
{
	int n,m;
	cin>>n>>m;
	vi br(m);
	vvi lr(n);
	For(i,0,n)
	{
		int s;
		cin>>s;
		lr[i].resize(s);
		For(j,0,s)
		cin>>lr[i][j];
	}
	For(i,0,m)
	{
		cin>>br[i];
		br[i]--;
	}

	For(i,0,n)
	For(j,1,lr[i].size())
	lr[i][j]+=lr[i][j-1];

	vi maxa(n),mini(n);
	For(i,0,n)
	{
		maxa[i]=*max_element(b2e(lr[i]));
		mini[i]=*min_element(b2e(lr[i]));
	}
	vector<int> ians(n);
	For(i,0,n)
	{
		int mans,mi;
		mans=lr[i][0];mi=0;
		For(j,1,lr[i].size())
		{
			mans=max(lr[i][j]-mi,mans);
			mi=min(mi,lr[i][j]);
		}
		ians[i]=mans;
	}
	long long int ans=ians[br[0]];
	For(i,1,m)
	ans=max(ans,(long long int)ians[br[i]]);
	long long int minin=min(0,mini[br[0]]);
	long long int cm=lr[br[0]][lr[br[0]].size()-1];
	For(i,1,m)
	{
		ans=max(ans,maxa[br[i]]+cm-minin);
		minin=min(minin,mini[br[i]]+cm);
		cm+=lr[br[i]][lr[br[i]].size()-1];
	}
	cout<<ans<<endl;
}