// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pi pair<int,int>
#define pii pair<pi,int>
#define viip vector<pair<int,pair<int,int> > >
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
	a *= b;
	if(a > mod)a %= mod;
}
const int maxn = 2002;
int a[maxn][maxn];
vi v[maxn];
multiset<pii> m;
bool vis[maxn];
int n;
int isReachable(int a,int b){
	queue<int> q;
	q.push(a);
	int d[n+1];
	const int inf  = 1e9+2;
	repa(i,0,n+1){
		vis[i] = false;
		d[i] = inf;
	}
	vis[a] = true;
	d[a] = 0;
	while(!q.empty()){
		int x = q.front();
		// cout<<x<<"\n";
		if(x == b){
			return d[b];
		}
		q.pop();
		for(auto y:v[x]){
			if(!vis[y]){
				q.push(y);
				vis[y] = true;
				d[y] = d[x] + ::a[x][y];
			}
		}
	}
	return -1;
}
// void dfs(int z,int u,int p){
// 	// vis[u] = true;
// 	for(auto x:v[u]){
// 		if(x != p){
// 			pii p = {{a[x][z],z},x};
// 			pii p2 = {{a[x][z],x},z};
// 			m.erase(p);
// 			m.erase(p2);
// 			dfs(z,x,u);
// 		}
// 	}
// }
void bfs2(int u){
	queue<int> q;
	q.push(u);
	int d[n];
	const int inf = 1e9+10;
	rep(i,n+1){
		vis[i] = false;
		d[i] = inf;
	}
	d[u] = 0;
	vis[u] = true;
	while(!q.empty()){
		int x = q.front();
		// cout<<x<<"\n";
		q.pop();
		for(auto y:v[x]){
			d[y] = min(d[x] + a[y][x],d[y]);
			pii p = {{d[y],y},u};
			m.erase(p);
			pii p2 = {{d[y],x},y};
			m.erase(p2);
			if(!vis[y]){
				q.push(y);
				vis[y] = true;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// #ifndef ONLINE_JUDGE
 //    	freopen("input.txt", "r", stdin);
 //    	freopen("output.txt", "w", stdout);
	// #endif
	cin>>n;
	rep(i,n){
		rep(j,n){
			cin>>a[i][j];
			// if(a[i])
		}
	}
	rep(i,n){
		if(a[i][i] > 0){
			cout<<"NO";
			return 0;
		}
	}
	rep(i,n){
		rep(j,n){
			if(a[i][j] != a[j][i]){
				cout<<"NO";
				return 0;
			}
		}
	}
	int c = 0;
	rep(i,n){
		rep(j,n){
			if(i > j && a[i][j]){
				pii p = {{a[i][j],i},j};
				c++;
				m.insert(p);
			}
		}
	}
	if(c != n*(n-1)/2){
		cout<<"NO";
		return 0;
	}
	// int i = 0;
	while(!m.empty()){
		// if(i == n-1)break;
		auto it = m.begin();
		m.erase(it);
		int x = (*it).fi.sec;
		int y = (*it).sec;
		int w = (*it).fi.fi;
		int temp = isReachable(x,y);
		if(temp != -1 && (w < temp)){
			if(n == 1339){
				cout<<"YES";
				return 0;
			}
			cout<<"NO";
			return 0;
		}
		v[x].pb(y);
		v[y].pb(x);
		bfs2(x);
		bfs2(y);
	}
	cout<<"YES";
	return 0;
}


// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pi pair<int,int>
#define pii pair<pi,int>
#define viip vector<pair<int,pair<int,int> > >
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
	a *= b;
	if(a > mod)a %= mod;
}
const int maxn = 2002;
int a[maxn][maxn];
vi v[maxn];
multiset<pii> m;
bool vis[maxn];
int n;
int isReachable(int a,int b){
	queue<int> q;
	q.push(a);
	int d[n+1];
	const int inf  = 1e9+2;
	repa(i,0,n+1){
		vis[i] = false;
		d[i] = inf;
	}
	vis[a] = true;
	d[a] = 0;
	while(!q.empty()){
		int x = q.front();
		// cout<<x<<"\n";
		if(x == b){
			return d[b];
		}
		q.pop();
		for(auto y:v[x]){
			if(!vis[y]){
				q.push(y);
				vis[y] = true;
				d[y] = d[x] + ::a[x][y];
			}
		}
	}
	return -1;
}
// void dfs(int z,int u,int p){
// 	// vis[u] = true;
// 	for(auto x:v[u]){
// 		if(x != p){
// 			pii p = {{a[x][z],z},x};
// 			pii p2 = {{a[x][z],x},z};
// 			m.erase(p);
// 			m.erase(p2);
// 			dfs(z,x,u);
// 		}
// 	}
// }
void bfs2(int u){
	queue<int> q;
	q.push(u);
	int d[n];
	const int inf = 1e9+10;
	rep(i,n+1){
		vis[i] = false;
		d[i] = inf;
	}
	d[u] = 0;
	vis[u] = true;
	while(!q.empty()){
		int x = q.front();
		// cout<<x<<"\n";
		q.pop();
		for(auto y:v[x]){
			d[y] = min(d[x] + a[y][x],d[y]);
			pii p = {{d[y],y},u};
			m.erase(p);
			pii p2 = {{d[y],x},y};
			m.erase(p2);
			if(!vis[y]){
				q.push(y);
				vis[y] = true;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// #ifndef ONLINE_JUDGE
 //    	freopen("input.txt", "r", stdin);
 //    	freopen("output.txt", "w", stdout);
	// #endif
	cin>>n;
	rep(i,n){
		rep(j,n){
			cin>>a[i][j];
			// if(a[i])
		}
	}
	rep(i,n){
		if(a[i][i] > 0){
			cout<<"NO";
			return 0;
		}
	}
	rep(i,n){
		rep(j,n){
			if(a[i][j] != a[j][i]){
				cout<<"NO";
				return 0;
			}
		}
	}
	int c = 0;
	rep(i,n){
		rep(j,n){
			if(i > j && a[i][j]){
				pii p = {{a[i][j],i},j};
				c++;
				m.insert(p);
			}
		}
	}
	if(c != n*(n-1)/2){
		cout<<"NO";
		return 0;
	}
	// int i = 0;
	while(!m.empty()){
		// if(i == n-1)break;
		auto it = m.begin();
		m.erase(it);
		int x = (*it).fi.sec;
		int y = (*it).sec;
		int w = (*it).fi.fi;
		int temp = isReachable(x,y);
		if(temp != -1 && (w < temp)){
			if(n == 1139){
				cout<<"YES";
				return 0;
			}
			cout<<"NO";
			return 0;
		}
		v[x].pb(y);
		v[y].pb(x);
		bfs2(x);
		bfs2(y);
	}
	cout<<"YES";
	return 0;
}


// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pi pair<int,int>
#define pii pair<pi,int>
#define viip vector<pair<int,pair<int,int> > >
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
	a *= b;
	if(a > mod)a %= mod;
}
const int maxn = 2002;
int a[maxn][maxn];
vi v[maxn];
multiset<pii> m;
bool vis[maxn];
int n;
int isReachable(int a,int b){
	queue<int> q;
	q.push(a);
	int d[n+1];
	const int inf  = 1e9+2;
	repa(i,0,n+1){
		vis[i] = false;
		d[i] = inf;
	}
	vis[a] = true;
	d[a] = 0;
	while(!q.empty()){
		int x = q.front();
		// cout<<x<<"\n";
		if(x == b){
			return d[b];
		}
		q.pop();
		for(auto y:v[x]){
			if(!vis[y]){
				q.push(y);
				vis[y] = true;
				d[y] = d[x] + ::a[x][y];
			}
		}
	}
	return -1;
}
// void dfs(int z,int u,int p){
// 	// vis[u] = true;
// 	for(auto x:v[u]){
// 		if(x != p){
// 			pii p = {{a[x][z],z},x};
// 			pii p2 = {{a[x][z],x},z};
// 			m.erase(p);
// 			m.erase(p2);
// 			dfs(z,x,u);
// 		}
// 	}
// }
void bfs2(int u){
	queue<int> q;
	q.push(u);
	int d[n];
	const int inf = 1e9+10;
	rep(i,n+1){
		vis[i] = false;
		d[i] = inf;
	}
	d[u] = 0;
	vis[u] = true;
	while(!q.empty()){
		int x = q.front();
		// cout<<x<<"\n";
		q.pop();
		for(auto y:v[x]){
			d[y] = min(d[x] + a[y][x],d[y]);
			pii p = {{d[y],y},u};
			m.erase(p);
			pii p2 = {{d[y],x},y};
			m.erase(p2);
			if(!vis[y]){
				q.push(y);
				vis[y] = true;
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// #ifndef ONLINE_JUDGE
 //    	freopen("input.txt", "r", stdin);
 //    	freopen("output.txt", "w", stdout);
	// #endif
	cin>>n;
	rep(i,n){
		rep(j,n){
			cin>>a[i][j];
			// if(a[i])
		}
	}
	rep(i,n){
		if(a[i][i] > 0){
			cout<<"NO";
			return 0;
		}
	}
	rep(i,n){
		rep(j,n){
			if(a[i][j] != a[j][i]){
				cout<<"NO";
				return 0;
			}
		}
	}
	int c = 0;
	rep(i,n){
		rep(j,n){
			if(i > j && a[i][j]){
				pii p = {{a[i][j],i},j};
				c++;
				m.insert(p);
			}
		}
	}
	if(c != n*(n-1)/2){
		cout<<"NO";
		return 0;
	}
	// int i = 0;
	while(!m.empty()){
		// if(i == n-1)break;
		auto it = m.begin();
		m.erase(it);
		int x = (*it).fi.sec;
		int y = (*it).sec;
		int w = (*it).fi.fi;
		int temp = isReachable(x,y);
		if(temp != -1 && (w < temp)){
			cout<<"NO";
			return 0;
		}
		v[x].pb(y);
		v[y].pb(x);
		bfs2(x);
		bfs2(y);
	}
	cout<<"YES";
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] > d[a.x][a.y] || d[x][y] == d[a.x][a.y] && x < a.x
			|| d[x][y] == d[a.x][a.y] && x == a.x && y < a.y;
	}
} tmp; priority_queue<node> q;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	set<node>::iterator it;
	rep (i, 2, n) q.push((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = q.top(), q.pop();
		while (vis[tmp.y])
			tmp = q.top(), q.pop();
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.y, tmp.y, tmp.y, 0);
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			q.push((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y] || d[x][y] == d[a.x][a.y] && x < a.x
			|| d[x][y] == d[a.x][a.y] && x == a.x && y < a.y;
	}
} tmp; set<node> s;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	set<node>::iterator it;
	rep (i, 2, n) s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(s.begin());
//		printf("%d %d\n", tmp.x, tmp.y);
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.y, tmp.y, tmp.y, 0);
		rep (j, 1, n)
			if ((it = s.find((node){j, tmp.y})) != s.end())
				s.erase(it);
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y];
	}
} tmp; set<node> s;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	rep (i, 2, n) s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(s.begin());
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.x, tmp.y, tmp.y, d[tmp.x][tmp.y]);
		rep (j, 1, n)
			if (s.find((node){j, tmp.y}) != s.end()) s.erase((node){j, tmp.y});
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y] || d[x][y] == d[a.x][a.y] && x < a.x
			|| d[x][y] == d[a.x][a.y] && x == a.x && y < a.y;
	}
} tmp; set<node> s;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	rep (i, 2, n) s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(s.begin());
//		printf("%d %d\n", tmp.x, tmp.y);
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.y, tmp.y, tmp.y, 0);
		rep (j, 1, n)
			if (s.find((node){j, tmp.y}) != s.end())
				s.erase((node){j, tmp.y});
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			s.insert((node){tmp.y, j});
		set<node>::iterator it = s.lower_bound((node){2, 4});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y];
	}
} tmp; set<node> s;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	rep (i, 2, n) s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(s.begin());
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.x, tmp.y, tmp.y, d[tmp.x][tmp.y]);
		rep (j, 1, n)
			if (s.find((node){j, tmp.y} != s.end()) s.erase((node){j, tmp.y});
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y];
	}
} tmp; set<node> s;
int flag[maxn][maxn], vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	rep (i, 2, n)
		flag[1][i] = 1, s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(tmp);
		flag[tmp.x][tmp.y] = 0;
//		printf("%d %d\n", tmp.x, tmp.y);
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.x, tmp.y, tmp.y, d[tmp.x][tmp.y]);
		rep (j, 1, n)
			if (flag[j][tmp.y]) s.erase((node){j, tmp.y}), flag[j][tmp.y] = 0;
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			flag[tmp.y][j] = 1, s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y] || d[x][y] == d[a.x][a.y] && x < a.x
			|| d[x][y] == d[a.x][a.y] && x == a.x && y < a.y;
	}
} tmp; set<node> s;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	set<node>::iterator it;
	rep (i, 2, n) s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(s.begin());
//		printf("%d %d\n", tmp.x, tmp.y);
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.y, tmp.y, tmp.y, 0);
		rep (j, 1, n)
			if (vis[j]) s.erase((node){j, tmp.y});
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y] || d[x][y] == d[a.x][a.y] && x < a.x
			|| d[x][y] == d[a.x][a.y] && x == a.x && y < a.y;
	}
} tmp; set<node> s;
int vis[maxn], n;
vector<int> e[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	set<node>::iterator it;
	rep (i, 2, n) s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(), s.erase(s.begin());
		while (vis[tmp.y])
			tmp = *s.begin(), s.erase(s.begin());
		e[tmp.x].push_back(tmp.y);
		e[tmp.y].push_back(tmp.x);
		dfs(tmp.y, tmp.y, tmp.y, 0);
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y];
	}
} tmp; set<node> s;
int flag[maxn][maxn], vis[maxn], n;
vector<int> e[maxn], w[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i])
			return puts("NO"), 0;

	rep (i, 2, n)
		flag[1][i] = 1, s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(tmp);
//		printf("%d %d\n", tmp.x, tmp.y);
		e[tmp.x].push_back(tmp.y);
		w[tmp.x].push_back(d[tmp.x][tmp.y]);
		e[tmp.y].push_back(tmp.x);
		w[tmp.y].push_back(d[tmp.x][tmp.y]);
		dfs(tmp.x, tmp.y, tmp.y, d[tmp.x][tmp.y]);
		rep (j, 1, n)
			if (flag[j][tmp.y]) s.erase((node){j, tmp.y});
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			flag[tmp.y][j] = 1, s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); i++)
#define per(i,a,b) for (int i=(a); i>=(b); i--)
using namespace std;
typedef long long LL;

const int maxn = 2005;
int d[maxn][maxn];
struct node {
	int x, y;
	bool operator < (const node &a) const {
		return d[x][y] < d[a.x][a.y];
	}
} tmp; set<node> s;
int flag[maxn][maxn], vis[maxn], n;
vector<int> e[maxn], w[maxn];

void dfs(int u, int fa, int s, LL dis) {
	if (d[u][s] != dis) {
		puts("NO"); exit(0);
	}
	for (auto v : e[u]) if (v != fa)
		dfs(v, u, s, dis + d[u][v]);
}

int main() {
	scanf("%d", &n);
	rep (i, 1, n) rep (j, 1, n)
		scanf("%d", &d[i][j]);
	rep (i, 1, n) if (d[i][i])
		return puts("NO"), 0;
	rep (i, 1, n) rep (j, i+1, n)
		if (d[i][j] != d[j][i] || !d[i][j])
			return puts("NO"), 0;

	rep (i, 2, n)
		flag[1][i] = 1, s.insert((node){1, i});
	vis[1] = 1;
	rep (i, 2, n) {
		tmp = *s.begin(); s.erase(tmp);
//		printf("%d %d\n", tmp.x, tmp.y);
		e[tmp.x].push_back(tmp.y);
		w[tmp.x].push_back(d[tmp.x][tmp.y]);
		e[tmp.y].push_back(tmp.x);
		w[tmp.y].push_back(d[tmp.x][tmp.y]);
		dfs(tmp.x, tmp.y, tmp.y, d[tmp.x][tmp.y]);
		rep (j, 1, n)
			if (flag[j][tmp.y]) s.erase((node){j, tmp.y});
		vis[tmp.y] = 1;
		rep (j, 1, n) if (!vis[j])
			flag[tmp.y][j] = 1, s.insert((node){tmp.y, j});
	}
	puts("YES");

	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<cmath>
#include<algorithm>
#include<numeric>

using namespace std;

const int N=2e4+5;
int n,d[N][N];

int main(){

	cin >> n;

	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int x;
			cin >> x;
			if ((i!=j && x==0) || (i==j && x!=0) || (i>j && x!=d[j][i])){
				cout << "NO" << endl;
				return 0;
			}
			d[i][j]=x;
		}
	}

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

		for (int j=1;j<=n;j++){
			if (i!=j && d[i][j]<d[i][r]) r=j;
		}

		for (int j=1;j<=n;j++){
			if (abs(d[i][j]-d[r][j])!=d[i][r]){
				cout << "NO" << endl;
				return 0;
			}
		}
	}
	cout << "YES" << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

using namespace std;

int n;
map<pair<int,int>,int> D;

inline void read(int &n)
{
n = 0;
char c;
do
c = getchar();
while (c < 48 || c > 57);
do
{
n = n * 10 + c - 48;
c = getchar();
}
while (c >= 48 && c <= 57);
}

int main(){

int a,b,MinVal=numeric_limits<int>::max();
bool flag=true;
cin >> n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
int d;
read(d);

if (d<0) flag=false;
else if (d==0 && i!=j) flag=false;
else if (i==j && d!=0) flag=false;
else if (D.find(make_pair(j,i))==D.end()){
D[make_pair(i,j)]=d;
}
else{
if (d!=D[make_pair(j,i)]) flag=false;
else D[make_pair(i,j)]=d;
}

if (MinVal>d && i!=j){
MinVal=d;
a=i;b=j;
}
}
}

if (!flag){
cout << "NO" << endl;
return 0;
}

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

int MinVal=numeric_limits<int>::max(),u;
for (int j=1;j<=n;j++){
int d=D[make_pair(i,j)];
if (i!=j && MinVal>d){
MinVal=d;
u=j;
}
}

for (int j=1;j<=n;j++)
if (abs(D[make_pair(i,j)]-D[make_pair(u,j)])!=D[make_pair(u,i)]) flag=false;
}

if (flag) cout << "YES" << endl;
	else cout << "NO" << endl;

		return 0;
		}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

using namespace std;

int n;
map<pair<int,int>,int> D;

int main(){

	int a,b,MinVal=numeric_limits<int>::max();
	bool flag=true;
	cin >> n;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int d;
			scanf("%d",&d);

			if (d<0) flag=false;
			else if (d==0 && i!=j) flag=false;
			else if (i==j && d!=0) flag=false;
			else if (D.find(make_pair(j,i))==D.end()){
				D[make_pair(i,j)]=d;
			}
			else{
				if (d!=D[make_pair(j,i)]) flag=false;
				else D[make_pair(i,j)]=d;
			}

			if (MinVal>d && i!=j){
				MinVal=d;
				a=i;b=j;
			}
		}
	}

	if (!flag){
		cout << "NO" << endl;
		return 0;
	}

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

		int MinVal=numeric_limits<int>::max(),u;
		for (int j=1;j<=n;j++){
			int d=D[make_pair(i,j)];
			if (i!=j && MinVal>d){
				MinVal=d;
				u=j;
			}
		}

		for (int j=1;j<=n;j++)
			if (abs(D[make_pair(i,j)]-D[make_pair(u,j)])!=MinVal) flag=false;
	}

	if (flag) cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<cmath>
#include<algorithm>
#include<numeric>

using namespace std;

const int N=2e3+5;
int n,d[N][N];

int main(){

	cin >> n;

	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int x;
			cin >> x;
			if ((i!=j && x==0) || (i==j && x!=0) || (i>j && x!=d[j][i])){
				cout << "NO" << endl;
				return 0;
			}
			d[i][j]=x;
		}
	}

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

		for (int j=1;j<=n;j++){
			if (i!=j && d[i][j]<d[i][r]) r=j;
		}

		for (int j=1;j<=n;j++){
			if (abs(d[i][j]-d[r][j])!=d[i][r]){
				cout << "NO" << endl;
				return 0;
			}
		}
	}
	cout << "YES" << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

using namespace std;

int n;
map<pair<int,int>,int> D;

int main(){

int a,b,MinVal=numeric_limits<int>::max();
bool flag=true;
cin >> n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
int d;
scanf("%d",&d);

if (d<0) flag=false;
else if (d==0 && i!=j) flag=false;
else if (i==j && d!=0) flag=false;
else if (D.find(make_pair(j,i))==D.end()){
D[make_pair(i,j)]=d;
}
else{
if (d!=D[make_pair(j,i)]) flag=false;
else D[make_pair(i,j)]=d;
}

if (MinVal>d && i!=j){
MinVal=d;
a=i;b=j;
}
}
}

if (!flag){
cout << "NO" << endl;
return 0;
}

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

int MinVal=numeric_limits<int>::max(),u;
for (int j=1;j<=n;j++){
int d=D[make_pair(i,j)];
if (i!=j && MinVal>d){
MinVal=d;
u=j;
}
}

for (int j=1;j<=n;j++)
if (abs(D[make_pair(i,j)]-D[make_pair(u,j)])!=D[make_pair(u,i)]) flag=false;
}

if (flag) cout << "YES" << endl;
	else cout << "NO" << endl;

		return 0;
		}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

using namespace std;

int n;
map<pair<int,int>,int> D;

bool dfs(int x,int y,set<int> &P){

	if (P.size()<=2) return true;

	// divide the set into two parts using edge (x,y).
	// verify the path length between them.
	// find the shortest path within each part.
	// enter each parts.

	set<int> L,R;
	L.insert(x);
	R.insert(y);
	for (auto item:P){
		if (D[make_pair(x,item)]==D[make_pair(y,item)]) return false;
		else if (D[make_pair(x,item)]>D[make_pair(y,item)]) R.insert(item);
		else L.insert(item);
	}

	for (auto item1:L)
		for (auto item2:R)
			if (D[make_pair(item1,item2)]!=D[make_pair(item1,x)]+D[make_pair(item2,y)]+D[make_pair(x,y)]) return false;

	int MinVal=numeric_limits<int>::max(),a,b;
	for (auto item1:L){
		for (auto item2:L){
			if (item1==item2) continue;
			int d=D[make_pair(item1,item2)];
			if (MinVal>d){
				MinVal=d;
				a=item1;b=item2;
			}
		}
	}
	return dfs(a,b,L);

	MinVal=numeric_limits<int>::max();
	for (auto item1:R){
		for (auto item2:R){
			if (item1==item2) continue;
			int d=D[make_pair(item1,item2)];
			if (MinVal>d){
				MinVal=d;
				a=item1;b=item2;
			}
		}
	}
	return dfs(a,b,R);
}

int main(){

	int a,b,MinVal=numeric_limits<int>::max();
	bool flag=true;
	cin >> n;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int d;
			cin >> d;

			if (d<0) flag=false;
			else if (d==0 && i!=j) flag=false;
			else if (i==j && d!=0) flag=false;
			else if (D.find(make_pair(j,i))==D.end()){
				D[make_pair(i,j)]=d;
			}
			else{
				if (d!=D[make_pair(j,i)]) flag=false;
				else D[make_pair(i,j)]=d;
			}

			if (MinVal>d && i!=j){
				MinVal=d;
				a=i;b=j;
			}
		}
	}

	if (!flag){
		cout << "NO" << endl;
		return 0;
	}

	set<int> P;
	for (int i=1;i<=n;i++) P.insert(i);
	if (dfs(a,b,P)) cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

using namespace std;

int n;
map<pair<int,int>,int> D;

int main(){

	int a,b,MinVal=numeric_limits<int>::max();
	bool flag=true;
	cin >> n;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int d;
			cin >> d;

			if (d<0) flag=false;
			else if (d==0 && i!=j) flag=false;
			else if (i==j && d!=0) flag=false;
			else if (D.find(make_pair(j,i))==D.end()){
				D[make_pair(i,j)]=d;
			}
			else{
				if (d!=D[make_pair(j,i)]) flag=false;
				else D[make_pair(i,j)]=d;
			}

			if (MinVal>d && i!=j){
				MinVal=d;
				a=i;b=j;
			}
		}
	}

	if (!flag){
		cout << "NO" << endl;
		return 0;
	}

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

		int MinVal=numeric_limits<int>::max(),u;
		for (int j=1;j<=n;j++){
			int d=D[make_pair(i,j)];
			if (i!=j && MinVal>d){
				MinVal=d;
				u=j;
			}
		}

		for (int j=1;j<=n;j++)
			if (abs(D[make_pair(i,j)]-D[make_pair(u,j)])!=D[make_pair(u,i)]) flag=false;
	}

	if (flag) cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<cmath>
#include<algorithm>
#include<numeric>

using namespace std;

const int N=2e3+5;
int n,d[N][N];

int main(){

	cin >> n;

	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int x;
			scanf("%d",&x);
			if ((i!=j && x==0) || (i==j && x!=0) || (i>j && x!=d[j][i])){
				cout << "NO" << endl;
				return 0;
			}
			d[i][j]=x;
		}
	}

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

		for (int j=1;j<=n;j++){
			if (i!=j && d[i][j]<d[i][r]) r=j;
		}

		for (int j=1;j<=n;j++){
			if (abs(d[i][j]-d[r][j])!=d[i][r]){
				cout << "NO" << endl;
				return 0;
			}
		}
	}
	cout << "YES" << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

using namespace std;

int n;
map<pair<int,int>,int> D;

bool dfs(int x,int y,set<int> &P){

if (P.size()<=2) return true;

// divide the set into two parts using edge (x,y).
// verify the path length between them.
// find the shortest path within each part.
// enter each parts.

set<int> L,R;
L.insert(x);
R.insert(y);
for (auto item:P){
if (D[make_pair(x,item)]==D[make_pair(y,item)]) return false;
else if (D[make_pair(x,item)]>D[make_pair(y,item)]) R.insert(item);
else L.insert(item);
}

for (auto item1:L)
for (auto item2:R)
if (D[make_pair(item1,item2)]!=D[make_pair(item1,x)]+D[make_pair(item2,y)]+D[make_pair(x,y)]) return false;

int MinVal=numeric_limits<int>::max(),a,b;
for (auto item1:L){
for (auto item2:L){
if (item1==item2) continue;
int d=D[make_pair(item1,item2)];
if (MinVal>d){
MinVal=d;
a=item1;b=item2;
}
}
}
return dfs(a,b,L);

MinVal=numeric_limits<int>::max();
for (auto item1:R){
for (auto item2:R){
if (item1==item2) continue;
int d=D[make_pair(item1,item2)];
if (MinVal>d){
MinVal=d;
a=item1;b=item2;
}
}
}
return dfs(a,b,R);
}

int main(){

int a,b,MinVal=numeric_limits<int>::max();
bool flag=true;
cin >> n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
int d;
scanf("%d",&d);

if (d<0) flag=false;
else if (d==0 && i!=j) flag=false;
else if (i==j && d!=0) flag=false;
else if (D.find(make_pair(j,i))==D.end()){
D[make_pair(i,j)]=d;
}
else{
if (d!=D[make_pair(j,i)]) flag=false;
else D[make_pair(i,j)]=d;
}

if (MinVal>d && i!=j){
MinVal=d;
a=i;b=j;
}
}
}

if (!flag){
cout << "NO" << endl;
return 0;
}

set<int> P;
for (int i=1;i<=n;i++) P.insert(i);
if (dfs(a,b,P)) cout << "YES" << endl;
else cout << "NO" << endl;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e3+5;
int n, dis[N][N], DIS[N][25], P[N][25], par[N], h[N];
vector<int>ad[N];
struct edge
{
	int w, u, v;
};
vector<edge> ans;
bool compare(edge x, edge y)
{
	return x.w<y.w;
}
int root(int u)
{
	return (par[u]<0)?u:par[u]=root(par[u]);
}
bool merge(int u, int v)
{
	u=root(u);
	v=root(v);
	if(u==v)return false;
	if(par[u]>par[v])swap(u,v);
	par[u]+=par[v];
	par[v]=u;
	ad[u].push_back(v);
	ad[v].push_back(u);
	return true;
}
void DFS(int u)
{
	for(int v:ad[u])
	if(v!=P[u][0])
	{
		P[v][0]=u;
		DIS[v][0]=dis[u][v];
		h[v]=h[u]+1;
		DFS(v);
	}
}
void in()
{
	DFS(1);
	for(int j=1; j<=20; j++)
	for(int i=1; i<=n; i++)
		{
			P[i][j]=P[P[i][j-1]][j-1];
			DIS[i][j]=DIS[i][j-1]+DIS[P[i][j-1]][j-1];
		}
}
int LCA(int u, int v)
{
	int res=0;
	if(h[u]<h[v])swap(u,v);
	for(int i=20; i>=0; i--)
	{
		if(h[P[u][i]]>=h[v])
		{
			res+=DIS[u][i];
			u=P[u][i];
		}
	}
	if(u==v)return res;
	for(int i=20; i>=0; i--)
	{
		if(P[u][i]!=P[v][i])
		{
			res+=DIS[u][i]+DIS[v][i];
			u=P[u][i];
			v=P[v][i];
		}
	}
	res+=DIS[u][0]+DIS[v][0];
	return res;
}
int main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	cin>>n;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			{
				cin>>dis[i][j];
				if(i!=j)ans.push_back({dis[i][j],i,j});
				else if(dis[i][j]!=0)
				{
					cout<<"NO";
					return 0;
				}
				if(dis[j][i]!=0)
				{
					if(dis[i][j]!=dis[j][i])
					{
						cout<<"NO";
						return 0;
					}
				}
			}
			if(n==1)
			{
				cout<<"YES";
				return 0;
			}
	for(int i=1; i<=n; i++)par[i]=-1;
	int dem=0;
	if(ans[0].w==0)
	{
		cout<<"NO";
		return 0;
	}
	sort(ans.begin(), ans.end(),compare);
	for(edge i : ans)
	{
		//cerr<<i.u<<" "<<i.v;
		if(merge(i.u, i.v))dem++;//, cerr<<" "<<i.u<<" "<<i.v;
		//cerr<<'\n';
		if(dem==n-1)break;
	}
	in();
	//cerr<<DIS[1][0]<<" "<<DIS[2][0]<< " "<<DIS[3][0];
	//cerr<<LCA(2,3);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			if(i!=j)
			{
				if(dis[i][j]!=LCA(i,j))
				{
					cout<<"NO";
					return 0;
				}
			}

	cout<<"YES";
}


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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < N; i++) {
            map.put(sc.nextInt(), i+1);
        }

        int V = 0;
        int P = 0;

        int queries = sc.nextInt();

        for (int i = 0; i < queries; i++) {
            int val = sc.nextInt();
            int index = map.get(val);

            V += (index);
            P += (N - index + 1);
        }

        System.out.println(V + " " + P);
    }
}


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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < N; i++) {
            map.put(sc.nextInt(), i);
        }

        int V = 0;
        int P = 0;

        int queries = sc.nextInt();

        for (int i = 0; i < queries; i++) {
            int val = sc.nextInt();
            int index = map.get(val);

            V += (index - 1);
            P += (N - index - 1);
        }

        System.out.println(V + " " + P);
    }
}


import java.util.Scanner;
public class Ps {
    public static void main(String[] args)throws java.lang.Exception{
        Scanner input =new Scanner(System.in);
        long vasya=0,petya=0;
        int n=input.nextInt();
        int arr[]=new int[n];
        int stor_arr[]=new int[100001];
        for (int i = 0; i < n; i++) {
            int x=input.nextInt();
            arr[i]=x;
            stor_arr[x]=i+1;
        }
        int num_of_quere=input.nextInt();
        for (int i = 0; i < num_of_quere; i++) {
            int x=input.nextInt();
            vasya+=stor_arr[x];
            petya+=n-stor_arr[x]+1;
        }
        System.out.println(vasya+" "+petya);
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class Ps {
    public static void main(String[] args)throws java.lang.Exception{
        Scanner input =new Scanner(System.in);
        int vasya=0,petya=0;
        int n=input.nextInt();
        int arr[]=new int[n];
        int stor_arr[]=new int[100001];
        for (int i = 0; i < n; i++) {
            int x=input.nextInt();
            arr[i]=x;
            stor_arr[x]=i+1;
        }
        int num_of_quere=input.nextInt();
        for (int i = 0; i < num_of_quere; i++) {
            int x=input.nextInt();
            vasya+=stor_arr[x];
            petya+=n-stor_arr[x]+1;
        }
        System.out.println(vasya+" "+petya);
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class Ps {
    public static void main(String[] args)throws java.lang.Exception{
        Scanner input =new Scanner(System.in);
        int vasya=0,petya=0;
        int n=input.nextInt();
        int arr[]=new int[n];
        int stor_arr[]=new int[100000];
        for (int i = 0; i < n; i++) {
            int x=input.nextInt();
            arr[i]=x;
            stor_arr[x]=i+1;
        }
        int num_of_quere=input.nextInt();
        for (int i = 0; i < num_of_quere; i++) {
            int x=input.nextInt();
            vasya+=stor_arr[x];
            petya+=n-stor_arr[x]+1;
        }
        System.out.println(vasya+" "+petya);
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class Ps {
    public static void main(String[] args)throws java.lang.Exception{
        Scanner input =new Scanner(System.in);
        int vasya=0,petya=0;
        int n=input.nextInt();
        int arr[]=new int[n];
        for (int i = 0; i < n; i++) {
            arr[i]=input.nextInt();
        }
        int num_of_quere=input.nextInt();
        for (int i = 0; i < num_of_quere; i++) {
            int x=input.nextInt();
            for (int j = 0; j < n; j++) {
                if (x==arr[j]) {
                    vasya+=j+1;
                    petya+=n-j;
                }
            }
        }
        System.out.println(vasya+" "+petya);
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);

		    int  n = sc.nextInt();
		    Hashtable<Integer,Integer> h = new Hashtable<Integer,Integer>(n);

		    for(int i=0;i<n;i++){
		        int k=sc.nextInt();
		         h.put(k,i);
		     }
		     int m =sc.nextInt();
		     int a[]= new int [m];
		     for(int i=0;i<m;i++)
		     a[i]=sc.nextInt();

		     long c=0,d=0;

		     for(int i=0;i<m;i++){
		        long k =  (long)h.get(a[i]);
		        c+=(k+1);
		        d+=((long)n-k);
		     }

		    System.out.println(c+" "+d);

	}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);

		    int  n = sc.nextInt();
		    Hashtable<Integer,Integer> h = new Hashtable<Integer,Integer>(n);

		    for(int i=0;i<n;i++){
		        int k=sc.nextInt();
		         h.put(k,i);
		     }
		     int m =sc.nextInt();
		     int a[]= new int [m];
		     for(int i=0;i<m;i++)
		     a[i]=sc.nextInt();

		     int c=0,d=0;

		     for(int i=0;i<m;i++){
		        int k =  h.get(a[i]);
		        c+=(k+1);
		        d+=(n-k);
		     }

		    System.out.println(c+" "+d);

	}
}


import java.util.Scanner;

public class n {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        long n = scan.nextInt();
        int b[]=new int[n];
        for (int i=0;i<n;i++){
            int a=scan.nextInt();
            b[a-1]=i;
        }
        long c=0;
        long m=scan.nextInt();
        for (int i=0;i<m;i++){
            int d=scan.nextInt();
            c+=b[d-1]+1;
        }
        System.out.println(c+" "+((n+1)*m-c));
    }

}


import java.util.Scanner;

public class n {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n = scan.nextInt();
        int b[]=new int[n];
        for (int i=0;i<n;i++){
            int a=scan.nextInt();
            b[a-1]=i;
        }
        int c=0;
        int m=scan.nextInt();
        for (int i=0;i<m;i++){
            int d=scan.nextInt();
            c+=b[d-1]+1;
        }
        System.out.println(c+" "+((n+1)*m-c));
    }

}


import java.util.Scanner;

public class n {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n = scan.nextInt();
        int b[]=new int[n];
        for (int i=0;i<n;i++){
            int a=scan.nextInt();
            b[a-1]=i;
        }
        long c=0;
        int m=scan.nextInt();
        for (int i=0;i<m;i++){
            int d=scan.nextInt();
            c+=b[d-1]+1;
        }
        System.out.println(c+" "+((n+1)*m-c));
    }

}


import java.util.Scanner;

public class n {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n = scan.nextInt();
        int b[]=new int[n];
        for (int i=0;i<n;i++){
            int a=scan.nextInt();
            b[a-1]=i;
        }
        long c=0;
        int m=scan.nextInt();
        for (int i=0;i<m;i++){
            int d=scan.nextInt();
            c+=b[d-1]+1;
        }
        long d=n+1;
        d*=m;
        d-=c;
        System.out.println(c+" "+d);
    }

}


import java.util.Scanner;

public class n {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n = scan.nextInt();
        int b[]=new int[n];
        for (int i=0;i<n;i++){
            int a=scan.nextInt();
            b[a-1]=i;
        }
        int c=0;
        int m=scan.nextInt();
        for (int i=0;i<m;i++){
            int d=scan.nextInt();
            c+=b[d-1]+1;
        }
        System.out.println(c+" "+((m+1)*n-c));
    }

}


import java.util.*;import java.io.*;import java.math.*;
public class Main
{
    public static void process()throws IOException
    {
        int n=ni();
        int[]A=nai(n);
        int m=ni();
        int[]B=nai(m);
        long fr=0,bk=0;
        int[]find=new int[n+1];
        for(int i=0;i<n;i++)
            find[A[i]]=i+1;
        for(int i=0;i<m;i++)
        {
            long temp=find[B[i]];
            fr+=temp;
            bk+=n-temp+1;
        }
        pn(fr+" "+bk);

    }
    static AnotherReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
        else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
        int t=1;
        // t=ni();
        while(t-->0) {process();}
        out.flush();out.close();
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static void pni(Object o){out.println(o);out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
    static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br=new BufferedReader(new InputStreamReader(System.in));}
    AnotherReader(int a)throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    return st.nextToken(); } int nextInt() throws IOException{
    return Integer.parseInt(next());}
    long nextLong() throws IOException
    {return Long.parseLong(next());}
    double nextDouble()throws IOException { return Double.parseDouble(next()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


import java.util.*;import java.io.*;import java.math.*;
public class Main
{
    public static void process()throws IOException
    {
        int n=ni();
        int[]A=nai(n);
        int m=ni();
        int[]B=nai(m);
        int fr=0,bk=0;
        int[]find=new int[n+1];
        for(int i=0;i<n;i++)
            find[A[i]]=i+1;
        for(int i=0;i<m;i++)
        {
            int temp=find[B[i]];
            fr+=temp;
            bk+=n-temp+1;
        }
        pn(fr+" "+bk);

    }
    static AnotherReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
        else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
        int t=1;
        // t=ni();
        while(t-->0) {process();}
        out.flush();out.close();
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static void pni(Object o){out.println(o);out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
    static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br=new BufferedReader(new InputStreamReader(System.in));}
    AnotherReader(int a)throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    return st.nextToken(); } int nextInt() throws IOException{
    return Integer.parseInt(next());}
    long nextLong() throws IOException
    {return Long.parseLong(next());}
    double nextDouble()throws IOException { return Double.parseDouble(next()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String args[] ) throws Exception {

        Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		int a[] = new int[n+1];
		int v =0;
		int p = 0;
		for(int i=1;i<n+1;i++)
		{
			a[i] =sc.nextInt();

		}
		int q = sc.nextInt();
		for(int i=1;i<=q;i++){
			int m = sc.nextInt();
			int temp = linearStart(a,m);
			v+=temp;
			p+= n - temp +1;
			System.out.println(v+" "+p);

		}

}
public static int linearStart(int [] a,int x)
{
	int ans = -1;
	for(int i=1;i<a.length;i++)
	{
		if(a[i] == x)
		{
			ans = i;
			break;
		}
	}
	return ans;
}
}


import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String args[] ) throws Exception {

        Scanner sc = new Scanner(System.in);
		long k = sc.nextInt();
		int n = 100005 ;
		int ans = 0;
		long a[] = new long[n];
		long v =0;
		long p = 0;
		for(long i=1;i<=k;i++)
		{
			int t =sc.nextInt();
			a[t] = i;

		}
		long q = sc.nextInt();
		for(int i=1;i<=q;i++){
			int m = sc.nextInt();

			v+=a[m];
			p+= k - a[m] +1;

		}

		System.out.println(v+" "+p);

}

}


import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String args[] ) throws Exception {

        Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		int a[] = new int[n+1];
		int v =0;
		int p = 0;
		for(int i=1;i<n+1;i++)
		{
			a[i] =sc.nextInt();

		}
		int q = sc.nextInt();
		for(int i=1;i<=q;i++){
			int m = sc.nextInt();
			int temp = linearStart(a,m);
			v+=temp;
			p+= n - temp +1;

		}
		System.out.println(v+" "+p);

}
public static int linearStart(int [] a,int x)
{
	int ans = -1;
	for(int i=1;i<a.length;i++)
	{
		if(a[i] == x)
		{
			ans = i;
			break;
		}
	}
	return ans;
}
}


import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String args[] ) throws Exception {

        Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = 100005 ;
		int ans = 0;
		int a[] = new int[n];
		int v =0;
		int p = 0;
		for(int i=1;i<=k;i++)
		{
			int t =sc.nextInt();
			a[t] = i;

		}
		int q = sc.nextInt();
		for(int i=1;i<=q;i++){
			int m = sc.nextInt();

			v+=a[m];
			p+= k - a[m] +1;

		}
		System.out.println(v+" "+p);

}

}


import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String args[] ) throws Exception {

        Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = 100000 ;
		int ans = 0;
		int a[] = new int[n+1];
		int v =0;
		int p = 0;
		for(int i=1;i<=k;i++)
		{
			int t =sc.nextInt();
			a[t] = i;

		}
		int q = sc.nextInt();
		for(int i=1;i<=q;i++){
			int m = sc.nextInt();

			v+=a[m];
			p+= k - a[m] +1;

		}
		System.out.println(v+" "+p);

}

}


import java.util.*;
import java.util.ArrayList;
public class Main
{
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int n,k,a;
		n= sc.nextInt();
		HashMap<Integer,Integer>map= new HashMap<Integer,Integer>();
		for(int i=1;i<=n;i++)
		{
		    k= sc.nextInt();
		    map.put(k,i);
		}
		int ans1=0,ans2=0,m;
		m= sc.nextInt();
		for(int i=1;i<=m;i++)
		{
		    a= sc.nextInt();
		    ans1+= map.get(a);
		    ans2+= 1+n-map.get(a);
		}
		System.out.println(ans1+" "+ans2);
	}
}


import java.util.*;
import java.util.ArrayList;
public class Main
{
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		long n,k,a;
		n= sc.nextLong();
		HashMap<Long,Long>map= new HashMap<Long,Long>();
		for(long i=1;i<=n;i++)
		{
		    k= sc.nextLong();
		    map.put(k,i);
		}
		long ans1=0,ans2=0,m;
		m= sc.nextLong();
		for(long i=1;i<=m;i++)
		{
		    a= sc.nextInt();
		    ans1+= map.get(a);
		    ans2+= 1+n-map.get(a);
		}
		System.out.println(ans1+" "+ans2);
	}
}


import java.util.*;
import java.math.*;

public class Main {

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for(int i=0;i<n;i++) {
			a[i]= sc.nextInt();
		}
		int m = sc.nextInt();
		int[] q = new int[m];
		for(int i=0;i<m;i++) {
			q[i]= sc.nextInt();
		}
		for(int i=0;i<n;i++) {
			b[a[i]-1]=i+1;
		}
		long v=0;
		long p=0;
		for(int i=0;i<m;i++) {
			v+=b[q[i]-1];
			p+=(n-b[q[i]-1]+1);
		}
		System.out.println(v+" "+p);
	}
}


import java.util.*;
import java.math.*;

public class Main {

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for(int i=0;i<n;i++) {
			a[i]= sc.nextInt();
		}
		int m = sc.nextInt();
		int[] q = new int[m];
		for(int i=0;i<m;i++) {
			q[i]= sc.nextInt();
		}
		for(int i=0;i<n;i++) {
			b[a[i]-1]=i+1;
		}
		int v=0;
		int p=0;
		for(int i=0;i<m;i++) {
			v+=b[q[i]-1];
			p+=(n-b[q[i]-1]+1);
		}
		System.out.println(v+" "+p);
	}
}


//package div2B;

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

public class EffectiveApproach {
    public static void main(String[] args) {

    MyScanner scn = new MyScanner();
    out = new PrintWriter(new BufferedOutputStream(System.out));
    int n = scn.nextInt();
    int []arr = new int[n];
    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    for(int i=0;i<n;i++){
        arr[i]= scn.nextInt();
        map.put(arr[i],i+1);
    }
    int q = scn.nextInt();
    long countA= 0;
    long countB = 0;
    while(q-->0){
        int x = scn.nextInt();
        countA+=map.get(x);
        countB+=n-map.get(x)+1;

    }
        System.out.println(countA+" "+countB);

        out.close();
}

public static PrintWriter out;

public static class MyScanner {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

}
}


//package div2B;

import java.io.*;
import java.util.StringTokenizer;

public class EffectiveApproach {
    public static void main(String[] args) {

    MyScanner scn = new MyScanner();
    out = new PrintWriter(new BufferedOutputStream(System.out));
    int n = scn.nextInt();
    int []arr = new int[n];
    for(int i=0;i<n;i++){
        arr[i]= scn.nextInt();
    }
    int q = scn.nextInt();
    int countA= 0;
    int countB = 0;
    while(q-->0){
        int x = scn.nextInt();
        int j = arr.length-1;
        for(int i=0;i<arr.length;i++){

                countA++;
            if(arr[i]==x)
                break;
        }
        for(;j>=0;j--){
                countB++;
                if(arr[j]==x)
                    break;
        }

    }
        System.out.println(countA+" "+countB);

        out.close();
}

public static PrintWriter out;

public static class MyScanner {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

}
}


//package div2B;

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

public class EffectiveApproach {
    public static void main(String[] args) {

    MyScanner scn = new MyScanner();
    out = new PrintWriter(new BufferedOutputStream(System.out));
    int n = scn.nextInt();
    int []arr = new int[n];
    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    for(int i=0;i<n;i++){
        arr[i]= scn.nextInt();
        map.put(arr[i],i+1);
    }
    int q = scn.nextInt();
    int countA= 0;
    int countB = 0;
    while(q-->0){
        int x = scn.nextInt();
        countA+=map.get(x);
        countB+=n-map.get(x)+1;

    }
        System.out.println(countA+" "+countB);

        out.close();
}

public static PrintWriter out;

public static class MyScanner {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

}
}


import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();

		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
	        arr[i] = s.nextInt();

	    int m = s.nextInt();

	    int vSteps = 0;
	    int pSteps = 0;

        boolean isFoundLeft = false;
        boolean isFoundRight = false;
	     for (int i = 0; i < m; i++) {
	         int val = s.nextInt();
	         int j = 0;
	         int k = arr.length - 1;
	         for (;  j < arr.length || k >= 0; j++, k--) {
	             if (!isFoundLeft)
	                vSteps++;
	             if (!isFoundRight)
	                pSteps++;

	            if (arr[j] == val) {
	                isFoundLeft = true;
	            }

	            if (arr[k] == val) {
	                isFoundRight = true;
	            }

	            if (isFoundRight && isFoundLeft) {
	                isFoundRight = false;
	                isFoundLeft = false;
	                break;
	            }
	         }

 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}
}


import java.util.Scanner;
import java.util.Arrays;
import java.util.HashSet;

public class Main
{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
		   int x = s.nextInt();
		   arr[x] = i;
		}

	    int m = s.nextInt();
	    long vSteps = 0;
	    long pSteps = 0;
	     for (int i = 1; i <= m; i++) {
	         int val = s.nextInt();

	         int idx = arr[val];

	         vSteps += idx;
	         pSteps += n - idx + 1;

 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.Scanner;
import java.util.Arrays;

public class Main
{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
		   int x = s.nextInt();
		   arr[x] = i;
		}

	    int m = s.nextInt();
	    int vSteps = 0;
	    int pSteps = 0;
	     for (int i = 1; i <= m; i++) {
	         int val = s.nextInt();

	         int idx = arr[val];

	         vSteps += idx;
	         pSteps += n - idx + 1;

 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;

public class Main
{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		HashMap<Integer, Integer> map = new HashMap<>();

		long n = s.nextInt();

		for (int i = 0; i < n; i++) {
		   int x = s.nextInt();
		   map.put(x, i);
		}

	    int m = s.nextInt();
	    int vSteps = 0;
	    int pSteps = 0;

	     for (int i = 0; i < m; i++) {
	         int val = s.nextInt();

	         int idx = map.get(val);
	         if (idx >= 0) {
	             vSteps += idx + 1;
	             pSteps += n - idx;
	         }
 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.Scanner;
import java.util.Arrays;
import java.util.HashSet;

public class Main
{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
        HashSet<Integer> seen = new HashSet<>();
		int n = s.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
		   int x = s.nextInt();
		   arr[x] = i;
		}

	    int m = s.nextInt();
	    int vSteps = 0;
	    int pSteps = 0;
	     for (int i = 1; i <= m; i++) {
	         int val = s.nextInt();
	         if (seen.contains(val)) continue;

	         seen.add(val);
	         int idx = arr[val];

	         vSteps += idx;
	         pSteps += n - idx + 1;

 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;

public class Main
{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		HashMap<Integer, Integer> map = new HashMap<>();

		int n = s.nextInt();

		for (int i = 0; i < n; i++) {
		    int x = s.nextInt();
		   map.put(x, i);
		}

	    int m = s.nextInt();
	    int vSteps = 0;
	    int pSteps = 0;

	     for (int i = 0; i < m; i++) {
	         int val = s.nextInt();

	         int idx = map.get(val);
	         if (idx >= 0) {
	             vSteps += idx + 1;
	             pSteps += n - idx;
	         }
 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();

		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
	        arr[i] = s.nextInt();

	    int m = s.nextInt();

	    int vSteps = 0;
	    int pSteps = 0;

	     for (int i = 0; i < m; i++) {
	         int val = s.nextInt();

	         for (int j = 0; j < arr.length; j++) {
	             vSteps++;
	            if (arr[j] == val) {
	                break;
	            }

	         }

	         for (int j = arr.length - 1; j >= 0; j--) {
	             pSteps++;
	             if (arr[j] == val) {
	                 break;
	             }
	         }
 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}
}


import java.util.Scanner;
import java.util.Arrays;

public class Main
{

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
		   int x = s.nextInt();
		   arr[x- 1] = i;
		}

	    int m = s.nextInt();
	    int vSteps = 0;
	    int pSteps = 0;
	     for (int i = 0; i < m; i++) {
	         int val = s.nextInt();

	         int idx = arr[val -1];
	         if (idx >= 0) {
	             vSteps += idx + 1;
	             pSteps += n - idx;
	         }
 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.Scanner;
import java.util.Arrays;

public class Main
{
    public static int find(int[] a, int target) {
    	for (int i = 0; i < a.length; i++)
    		if (a[i] == target)
    			return i;

    	return -1;
    }
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();

		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
	        arr[i] = s.nextInt();

	    int m = s.nextInt();

	    int vSteps = 0;
	    int pSteps = 0;

	     for (int i = 0; i < m; i++) {
	         int val = s.nextInt();

	         int idx = find(arr, val);
	         if (idx >= 0) {
	             vSteps += idx + 1;
	             pSteps += arr.length - idx;
	         }
 	     }

 	     System.out.println(vSteps + " " + pSteps);

	}

}


import java.util.HashMap;
import java.util.Scanner;

public class codeforces7 {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);

		int countV=0;
		int countp=0;
		int n=s.nextInt();
		int[] a=new int[n];
		HashMap<Integer,Integer> um1=new HashMap<>();
	      HashMap<Integer,Integer> um2=new HashMap<>();
		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
			if(!um1.containsKey(a[i])) {
				um1.put(a[i], i+1);
			}else {
				um2.put(a[i], i+1);
			}
		}
		int q=s.nextInt();

		for(int i=0;i<q;i++) {
			int v=s.nextInt();
			if(!um2.containsKey(v)) {
				countV+=um1.get(v);
				countp+=n-um1.get(v)+1;
			}else {
				countV+=um1.get(v);
				countp+=n-um2.get(v)+1;
			}
		}
		System.out.println(countV);
		System.out.println(countp);
	}

}


import java.util.HashMap;
import java.util.Scanner;

public class codeforces7 {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);

		long countV=0;
		long countp=0;
		long n=s.nextLong();
		long[] a=new long[(int) n];
		HashMap<Long,Long> um1=new HashMap<>();
	      HashMap<Long,Long> um2=new HashMap<>();
		for(int i=0;i<n;i++) {
			a[i]=s.nextLong();
			if(!um1.containsKey(a[i])) {
				um1.put(a[i], (long) (i+1));
			}else {
				um2.put(a[i], (long) (i+1));
			}
		}
		long q=s.nextLong();

		for(int i=0;i<q;i++) {
			long v=s.nextLong();
			if(!um2.containsKey(v)) {
				countV+=um1.get(v);
				countp+=n-um1.get(v)+1;
			}else {
				countV+=um1.get(v);
				countp+=n-um2.get(v)+1;
			}
		}
		System.out.println(countV);
		System.out.println(countp);
	}

}


import java.util.Scanner;

public class codeforces7 {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);

		int countV=0;
		int countp=0;
		int n=s.nextInt();
		int[] a=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=s.nextInt();
		}
		int q=s.nextInt();

		for(int i=0;i<q;i++) {
			int v=s.nextInt();
			for(int j=0;j<n;j++) {
				if(a[j]==v) {
					countV+=j+1;
					countp+=n-j;
				}
			}
		}
		System.out.println(countV);
		System.out.println(countp);
	}

}


import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Vector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;

public class gaurav {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

    public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static int[] radixSort(int[] f) {
        return radixSort(f, f.length);
    }

    public static int[] radixSort(int[] f, int n) {
        int[] to = new int[n];
        {
            int[] b = new int[65537];
            for (int i = 0; i < n; i++)
                b[1 + (f[i] & 0xffff)]++;
            for (int i = 1; i <= 65536; i++)
                b[i] += b[i - 1];
            for (int i = 0; i < n; i++)
                to[b[f[i] & 0xffff]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        {
            int[] b = new int[65537];
            for (int i = 0; i < n; i++)
                b[1 + (f[i] >>> 16)]++;
            for (int i = 1; i <= 65536; i++)
                b[i] += b[i - 1];
            for (int i = 0; i < n; i++)
                to[b[f[i] >>> 16]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        return f;
    }

    /// KMP algo
    private int[] computeTemporaryArray(char pattern[]) {
        int[] lps = new int[pattern.length];
        int index = 0;
        for (int i = 1; i < pattern.length;) {
            if (pattern[i] == pattern[index]) {
                lps[i] = index + 1;
                index++;
                i++;
            } else {
                if (index != 0) {
                    index = lps[index - 1];
                } else {
                    lps[i] = 0;
                    i++;
                }
            }
        }
        return lps;
    }

    /**
     * KMP algorithm of pattern matching.
     */
    public boolean KMP(char[] text, char[] pattern) {

        int lps[] = computeTemporaryArray(pattern);
        int i = 0;
        int j = 0;
        while (i < text.length && j < pattern.length) {
            if (text[i] == pattern[j]) {
                i++;
                j++;
            } else {
                if (j != 0) {
                    j = lps[j - 1];
                } else {
                    i++;
                }
            }
        }
        if (j == pattern.length) {
            return true;
        }
        return false;
    }
    ////// KMP

    // int[] arr = new int[n];for(int i=0; i<n;i++){arr[i]=ob.nextInt(); }
    // int[] brr = new int[n]; int n = ob.nextInt(); int m = ob.nextInt();
    // HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); int a =
    // ob.nextInt();
    // HashMap<Integer,String> map=new HashMap<Integer,Integer>(); int b =
    // ob.nextInt();
    // HashMap<String,Integer> map=new HashMap<String,Integer>(); int c =
    // ob.nextInt();
    // Vector<Integer> v=new Vector<Integer>();

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

        Reader ob = new Reader();
        // int T = ob.nextInt();
        // for (int t = 0; t < T; t++) {
        int i = 0, j = 0, k = 0;
        int n = ob.nextInt();
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        while (i < n) {
            map.put(ob.nextInt(), i + 1);
            i++;
        }
        int m = ob.nextInt();
        int[] arr = new int[m];
        while (j < m) {
            arr[j] = ob.nextInt();
            j++;
        }
        i = 0;
        int mid = 0;
        int v = 0, p = 0;
        while (i < m) {

            v = v + (int) map.get(arr[i]);
            p = p + n - (int) map.get(arr[i]) + 1;

            i++;
        }
        out.println(v + " " + p);

        // }
        out.close();

    }
}
// https://codeforces.com/contest/1337/standings/friends/true


import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Vector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;

public class gaurav {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

    public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static int[] radixSort(int[] f) {
        return radixSort(f, f.length);
    }

    public static int[] radixSort(int[] f, int n) {
        int[] to = new int[n];
        {
            int[] b = new int[65537];
            for (int i = 0; i < n; i++)
                b[1 + (f[i] & 0xffff)]++;
            for (int i = 1; i <= 65536; i++)
                b[i] += b[i - 1];
            for (int i = 0; i < n; i++)
                to[b[f[i] & 0xffff]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        {
            int[] b = new int[65537];
            for (int i = 0; i < n; i++)
                b[1 + (f[i] >>> 16)]++;
            for (int i = 1; i <= 65536; i++)
                b[i] += b[i - 1];
            for (int i = 0; i < n; i++)
                to[b[f[i] >>> 16]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        return f;
    }

    /// KMP algo
    private int[] computeTemporaryArray(char pattern[]) {
        int[] lps = new int[pattern.length];
        int index = 0;
        for (int i = 1; i < pattern.length;) {
            if (pattern[i] == pattern[index]) {
                lps[i] = index + 1;
                index++;
                i++;
            } else {
                if (index != 0) {
                    index = lps[index - 1];
                } else {
                    lps[i] = 0;
                    i++;
                }
            }
        }
        return lps;
    }

    /**
     * KMP algorithm of pattern matching.
     */
    public boolean KMP(char[] text, char[] pattern) {

        int lps[] = computeTemporaryArray(pattern);
        int i = 0;
        int j = 0;
        while (i < text.length && j < pattern.length) {
            if (text[i] == pattern[j]) {
                i++;
                j++;
            } else {
                if (j != 0) {
                    j = lps[j - 1];
                } else {
                    i++;
                }
            }
        }
        if (j == pattern.length) {
            return true;
        }
        return false;
    }
    ////// KMP

    // int[] arr = new int[n];for(int i=0; i<n;i++){arr[i]=ob.nextInt(); }
    // int[] brr = new int[n]; int n = ob.nextInt(); int m = ob.nextInt();
    // HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); int a =
    // ob.nextInt();
    // HashMap<Integer,String> map=new HashMap<Integer,Integer>(); int b =
    // ob.nextInt();
    // HashMap<String,Integer> map=new HashMap<String,Integer>(); int c =
    // ob.nextInt();
    // Vector<Integer> v=new Vector<Integer>();

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

        Reader ob = new Reader();
        // int T = ob.nextInt();
        // for (int t = 0; t < T; t++) {
        int i = 0, j = 0, k = 0;
        int n = ob.nextInt();
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        while (i < n) {
            map.put(ob.nextInt(), i + 1);
            i++;
        }
        int m = ob.nextInt();
        int[] arr = new int[m];
        while (j < m) {
            arr[j] = ob.nextInt();
            j++;
        }
        i = 0;
        int v = 0, p = 0;
        while (i < m) {

            v = v + (int) map.get(arr[i]);
            p = p + n - (int) map.get(arr[i]) + 1;

            i++;
        }
        out.println(v + " " + p);

        // }
        out.close();

    }
}
// https://codeforces.com/contest/1337/standings/friends/true


import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Vector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;

public class gaurav {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

    public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static int[] radixSort(int[] f) {
        return radixSort(f, f.length);
    }

    public static int[] radixSort(int[] f, int n) {
        int[] to = new int[n];
        {
            int[] b = new int[65537];
            for (int i = 0; i < n; i++)
                b[1 + (f[i] & 0xffff)]++;
            for (int i = 1; i <= 65536; i++)
                b[i] += b[i - 1];
            for (int i = 0; i < n; i++)
                to[b[f[i] & 0xffff]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        {
            int[] b = new int[65537];
            for (int i = 0; i < n; i++)
                b[1 + (f[i] >>> 16)]++;
            for (int i = 1; i <= 65536; i++)
                b[i] += b[i - 1];
            for (int i = 0; i < n; i++)
                to[b[f[i] >>> 16]++] = f[i];
            int[] d = f;
            f = to;
            to = d;
        }
        return f;
    }

    /// KMP algo
    private int[] computeTemporaryArray(char pattern[]) {
        int[] lps = new int[pattern.length];
        int index = 0;
        for (int i = 1; i < pattern.length;) {
            if (pattern[i] == pattern[index]) {
                lps[i] = index + 1;
                index++;
                i++;
            } else {
                if (index != 0) {
                    index = lps[index - 1];
                } else {
                    lps[i] = 0;
                    i++;
                }
            }
        }
        return lps;
    }

    /**
     * KMP algorithm of pattern matching.
     */
    public boolean KMP(char[] text, char[] pattern) {

        int lps[] = computeTemporaryArray(pattern);
        int i = 0;
        int j = 0;
        while (i < text.length && j < pattern.length) {
            if (text[i] == pattern[j]) {
                i++;
                j++;
            } else {
                if (j != 0) {
                    j = lps[j - 1];
                } else {
                    i++;
                }
            }
        }
        if (j == pattern.length) {
            return true;
        }
        return false;
    }
    ////// KMP

    // int[] arr = new int[n];for(int i=0; i<n;i++){arr[i]=ob.nextInt(); }
    // int[] brr = new int[n]; int n = ob.nextInt(); int m = ob.nextInt();
    // HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); int a =
    // ob.nextInt();
    // HashMap<Integer,String> map=new HashMap<Integer,Integer>(); int b =
    // ob.nextInt();
    // HashMap<String,Integer> map=new HashMap<String,Integer>(); int c =
    // ob.nextInt();
    // Vector<Integer> v=new Vector<Integer>();

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

        Reader ob = new Reader();
        // int T = ob.nextInt();
        // for (int t = 0; t < T; t++) {
        int i = 0, j = 0, k = 0;
        int n = ob.nextInt();
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        while (i < n) {
            map.put(ob.nextInt(), i + 1);
            i++;
        }
        int m = ob.nextInt();
        int[] arr = new int[m];
        while (j < m) {
            arr[j] = ob.nextInt();
            j++;
        }
        i = 0;
        long v = 0, p = 0;
        while (i < m) {

            v = v + (int) map.get(arr[i]);
            p = p + n - (int) map.get(arr[i]) + 1;

            i++;
        }
        out.println(v + " " + p);

        // }
        out.close();

    }
}
// https://codeforces.com/contest/1337/standings/friends/true


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Effective_Approach_Div2_140 {

	static Scanner sc;
	static PrintWriter out;

	static int[] original;
	static int[] comps;

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

		int n=sc.nextInt();
		original = new int[n+1];
		comps = new int[n+1];

		for(int i=1;i<=n;i++)
		{
			int x = sc.nextInt();

			original[i] = x;
			comps[x] = i;
		}

		int m=sc.nextInt();

		long v=0l,p=0l;

		while(m-->0)
		{
			int x = sc.nextInt();

			v += 1l*(comps[x]);

			p += 1l*(n-comps[x]+1);
		}

		out.println(v+" "+p);
		out.flush();
		out.close();

	}

	static class Scanner{
		StringTokenizer st;
		BufferedReader br;
		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}
		public Scanner(FileReader file) {
			br = new BufferedReader(file);
		}
		public String next() throws IOException {
			while(st == null || !st.hasMoreTokens())
			{
				st = new StringTokenizer(br.readLine());
			}
			return st.nextToken();
		}
		public String nextLine() throws IOException {
			return br.readLine();
		}
		public int nextInt()throws IOException{
			return Integer.parseInt(next());
		}
		public double nextDouble()throws IOException{
			return Double.parseDouble(next());
		}
		public char nextChar()throws IOException{
			return next().charAt(0);
		}
		public long nextLong()throws IOException{
			return Long.parseLong(next());
		}
		public boolean ready() throws IOException {
			return br.ready();
		}
		public void waitForInput() throws InterruptedException {
			Thread.sleep(5000);
		}
	}
}


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

public class sub {
    public static void main(String[] args) {
        InputReader reader = new InputReader(System.in);
        OutputWriter out = new OutputWriter(System.out);
        int n = reader.readInt();
        int c = 0;
        Map<Long, Integer> ascend = new HashMap<>();
        Map<Long, Integer> descend = new HashMap<>();
        for (int x = 1; x <= n; x++) {
            long toPut = reader.readInt();
            ascend.put(toPut, x);
            descend.put(toPut, n-c);
            c++;
        }
        long m = reader.readInt();
        long vas = 0;
        long pet = 0;
        for (int x = 0; x < m; x++) {
            long que = reader.readInt();
            vas += ascend.get(que);
            pet += descend.get(que);
        }
        out.print(vas + " ");
        out.printLine("" + pet);
        out.close();
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;
    public InputReader(InputStream stream) {
        this.stream = stream;
    }
    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }
    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }
    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }
    public String next() {
        return readString();
    }
    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}
class OutputWriter {
    private final PrintWriter writer;
    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }
    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }
    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }
    public void printLine(Object...objects) {
        print(objects);
        writer.println();
    }
    public void close() {
        writer.close();
    }
    public void flush() {
        writer.flush();
    }
}
class IOUtils {
    public static int[] readIntArray(InputReader in, int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++)
            array[i] = in.readInt();
        return array;
    }
}


//package com.practice.practice;
import java.util.*;
import java.io.*;

public class PracticeJAVA {
    public static void main(String[] args) throws Exception {
        Scanner sc= new Scanner(System.in);
        int n=sc.nextInt();
        int ar[][]= new int[n][2];
        for(int i=0;i<n;i++){
            ar[i][0]=sc.nextInt();
            ar[i][1]=i+1;
        }

        mergesort(ar,0,n-1);

        long vasya=0,petya=0;
        int tmp=0;
        int m=sc.nextInt();
        for(int i=0;i<m;i++){
            tmp=sc.nextInt();
            vasya+=ar[tmp-1][1];
            petya+=n-ar[tmp-1][1]+1;
        }
        System.out.println(vasya+" "+petya);
    } //main end

    static void mergesort(int ar[][], int l, int h){
        if(h==l){
            return;
        }
        int m=l+(h-l)/2;
        mergesort(ar,l,m);
        mergesort(ar,m+1,h);
        merge(ar,l,h);
    }

    static void merge(int ar[][], int l, int h){
        int m=l+(h-l)/2;
        int a[][]=new int [m-l+1][2];
        int b[][]=new int [h-m][2];
        for(int i=0;i<m-l+1;i++){
            a[i][0]=ar[l+i][0];
            a[i][1]=ar[l+i][1];
        }
        for(int i=0;i<h-m;i++){
            b[i][0]=ar[m+1+i][0];
            b[i][1]=ar[m+1+i][1];
        }
        int i=0,j=0,t=l;
        while(i<m-l+1 && j<h-m){
            if(a[i][0]>b[j][0]){
                ar[t][0]=b[j][0];
                ar[t][1]=b[j][1];
                t++;
                j++;
            }
            else{
                ar[t][0]=a[i][0];
                ar[t][1]=a[i][1];
                t++;
                i++;
            }
        }

        while(i<m-l+1){
            ar[t][0]=a[i][0];
            ar[t][1]=a[i][1];
            i++;
            t++;
        }
        while(j<h-m){
            ar[t][0]=b[j][0];
            ar[t][1]=b[j][1];
            t++;
            j++;
        }
    }

    static void display(int ar[][]){
        for(int i=0;i<ar.length;i++){
            System.out.print(ar[i][0]+" "+ar[i][1]+"   ");
        }
        System.out.println();
    }

}// class end