// Problem : B. Numbers on Tree
// Contest : Codeforces - Codeforces Round #612 (Div. 1)
// URL : https://codeforces.com/problemset/problem/1286/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int,int>
#define m_p(x,y) make_pair(x,y)
#define vp vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v,n,x) v.assign(n,x)
#define incd(v,n) v.resize(n)
#define iniz(n) memset(n,0,sizeof(n))
#define inin(n) memset(n,-1,sizeof(n))
#define inimi(n) memset(n,0xc0,sizeof(n))
#define inima(n) memset(n,0x3f,sizeof(n))
#define all(v) (v).begin(),(v).end()

using namespace std;

template<typename T1,typename T2>istream &operator>>(istream &is, vector<pair<T1,T2>> &v) { for (pair<T1,T2> &t : v) is >> t.f>>t.se; return is; }
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

double pi=acos(-1.0);
int md=1e9+7;
const int INF=1e15;
int dx1[]={0,0,-1,1};
int dy1[]={1,-1,0,0};

template<class T>
T abst(T a)
{return a<0?-a:a;}

template<class T>
T max2(T a,T b){return a>b?a:b;}
template<class T>
T min2(T a,T b){return a<b?a:b;}
vi v[2001];
vi val(2001);
vi ans(2001,-1);
void join(vp &v1,vp &v2)
{
	for(auto i:v2)
	v1.pb(i);
}
vp dfs(int x)
{
	//cerr<<x<<" ";
	vp v2;
	for(int ch:v[x])
	{
		vp vt=dfs(ch);
		join(v2,vt);
	}
	if(size(v2)<val[x])
	{
		cout<<"NO";
		exit(0);
	}
	v2.insert(v2.begin()+val[x],pair<int,int>(val[x]+1,x));
	for(int i=val[x]+1;i<size(v2);i++)
	{
		v2[i].f++;
	}
	return v2;
}
void solve()
{
	int n;cin>>n;
	int root;
	for(int i=0;i<n;i++)
	{
		int x,y;cin>>x>>y;
		if(x==0)
		{
			root=i+1;
		}
		else
		{
			v[x].pb(i+1);
		}
		val[i+1]=y;
	}
	vp ans=dfs(root);
	sort(all(ans),[&](pii p1,pii p2){return p1.se<p2.se;});
		cout<<"YES\n";
		for(int i=1;i<=n;i++)
		cout<<ans[i-1].f<<" ";
}

int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
    solve();
}
return 0;
}


// Problem : B. Numbers on Tree
// Contest : Codeforces - Codeforces Round #612 (Div. 1)
// URL : https://codeforces.com/problemset/problem/1286/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int,int>
#define m_p(x,y) make_pair(x,y)
#define vp vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v,n,x) v.assign(n,x)
#define incd(v,n) v.resize(n)
#define iniz(n) memset(n,0,sizeof(n))
#define inin(n) memset(n,-1,sizeof(n))
#define inimi(n) memset(n,0xc0,sizeof(n))
#define inima(n) memset(n,0x3f,sizeof(n))
#define all(v) (v).begin(),(v).end()

using namespace std;

template<typename T1,typename T2>istream &operator>>(istream &is, vector<pair<T1,T2>> &v) { for (pair<T1,T2> &t : v) is >> t.f>>t.se; return is; }
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

double pi=acos(-1.0);
int md=1e9+7;
const int INF=1e15;
int dx1[]={0,0,-1,1};
int dy1[]={1,-1,0,0};

template<class T>
T abst(T a)
{return a<0?-a:a;}

template<class T>
T max2(T a,T b){return a>b?a:b;}
template<class T>
T min2(T a,T b){return a<b?a:b;}
vi v[2001];
vi val(2001);
vi ans(2001,-1);
void join(vp &v1,vp &v2)
{
	for(auto i:v2)
	v1.pb(i);
}
vp dfs(int x)
{
	//cerr<<x<<" ";
	vp v2;
	for(int ch:v[x])
	{
		vp vt=dfs(ch);
		join(v2,vt);
	}
	if(size(v2))
	sort(all(v2));
	if(size(v2)<val[x])
	{
		//cout<<x<<" "<<size(v2);
		cout<<"NO";
		exit(0);
	}
	int req=val[x]-1>=0?v2[val[x]-1].f+1:1;
	for(int i=val[x];i<size(v2);i++)
	{
		v2[i].f++;
	}
	v2.pb({req,x});
	return v2;
}
void solve()
{
	int n;cin>>n;
	int root;
	for(int i=0;i<n;i++)
	{
		int x,y;cin>>x>>y;
		if(x==0)
		{
			root=i+1;
		}
		else
		{
			v[x].pb(i+1);
		}
		val[i+1]=y;
	}
	vp ans=dfs(root);
	sort(all(ans),[&](pii p1,pii p2){return p1.se<p2.se;});
		cout<<"YES\n";
		for(int i=1;i<=n;i++)
		cout<<ans[i-1].f<<" ";
}

int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
    solve();
}
return 0;
}


// Problem : B. Numbers on Tree
// Contest : Codeforces - Codeforces Round #612 (Div. 1)
// URL : https://codeforces.com/problemset/problem/1286/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int,int>
#define m_p(x,y) make_pair(x,y)
#define vp vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v,n,x) v.assign(n,x)
#define incd(v,n) v.resize(n)
#define iniz(n) memset(n,0,sizeof(n))
#define inin(n) memset(n,-1,sizeof(n))
#define inimi(n) memset(n,0xc0,sizeof(n))
#define inima(n) memset(n,0x3f,sizeof(n))
#define all(v) (v).begin(),(v).end()

using namespace std;

template<typename T1,typename T2>istream &operator>>(istream &is, vector<pair<T1,T2>> &v) { for (pair<T1,T2> &t : v) is >> t.f>>t.se; return is; }
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

double pi=acos(-1.0);
int md=1e9+7;
const int INF=1e15;
int dx1[]={0,0,-1,1};
int dy1[]={1,-1,0,0};

template<class T>
T abst(T a)
{return a<0?-a:a;}

template<class T>
T max2(T a,T b){return a>b?a:b;}
template<class T>
T min2(T a,T b){return a<b?a:b;}
vi v[2001];
vi val(2001);
vi ans(2001,-1);
int b=1;
vi v1;
void dfs(int x)
{
	//cout<<x<<" "<<v1;
	vi vt;
	int cnt=0;
	for(int i=0;i<size(v1);i++)
	{
		if(cnt==val[x])
		{
			ans[x]=v1[i];
		}
		else
		{
		vt.pb(v1[i]);
		}
		cnt++;
	}
	v1=vt;
	if(ans[x]==-1)
	{
		b=0;return;
	}
	for(int ch:v[x])
	{
		dfs(ch);
	}
}
void solve()
{
	int n;cin>>n;
	int root;
	for(int i=0;i<n;i++)
	{
		int x,y;cin>>x>>y;
		if(x==0)
		{
			root=i+1;
		}
		else
		{
			v[x].pb(i+1);
		}
		val[i+1]=y;
	}
	for(int i=1;i<=n;i++)
	v1.pb(i);
	dfs(root);
	if(b and size(v1)==0)
	{
		cout<<"YES\n";
		for(int i=1;i<=n;i++)
		cout<<ans[i]<<" ";
	}
	else
	cout<<"NO";
}

int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
    solve();
}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MAXN = 2005;

int n, root, cnt, C[MAXN], order[MAXN], start[MAXN], sz[MAXN], res[MAXN];
vector<int> tree[MAXN];

void predfs(int node) {
  sz[node] = 1;
  order[cnt] = node;
  start[node] = cnt;
  cnt++;
  for (int child : tree[node]) {
    predfs(child);
    sz[node] += sz[child];
  }
}

void dfs(int node) {
  for (int child : tree[node]) {
    dfs(child);
  }
  if (sz[node] == 1) {
    return;
  }
  vector<pair<int, int>> v;
  for (int i = start[node] + 1; i < start[node] + sz[node]; i++) {
    int cur = order[i];
    v.emplace_back(res[cur], cur);
  }
  // cout << "node " << node << ": \n";
  // cout << "  descendants: ";
  // for (auto x : v) {
  //   cout << x.second << " ";
  // }
  // cout << "\n";
  sort(v.begin(), v.end());
  res[node] = v[C[node] - 1].first + 1;
  // cout << "  setting res to " << res[node] << '\n';
  for (int i = C[node]; i < v.size(); i++) {
    res[v[i].second] += MAXN;// v[C[node]].first - res[node] + 1;
  }
  // cout << "  ";
  // for (int i = 1; i <= n; i++) {
  //   cout << res[i] << " ";
  // }
  // cout << "\n";
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p; cin >> p >> C[i];
    if (p == 0) {
      root = i;
    } else {
      tree[p].push_back(i);
    }
  }
  predfs(root);
  for (int i = 1; i <= n; i++) {
    res[i] = 1;
    if (C[i] >= sz[i]) {
      cout << "NO\n";
      return 0;
    }
  }
  dfs(root);
  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << res[i] << ' ';
  }
  cout << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MAXN = 2005;

int n, root, cnt, C[MAXN], order[MAXN], start[MAXN], sz[MAXN], res[MAXN];
vector<int> tree[MAXN];

void predfs(int node) {
  sz[node] = 1;
  order[cnt] = node;
  start[node] = cnt;
  cnt++;
  for (int child : tree[node]) {
    predfs(child);
    sz[node] += sz[child];
  }
}

void dfs(int node) {
  for (int child : tree[node]) {
    dfs(child);
  }
  if (sz[node] == 1) {
    return;
  }
  vector<pair<int, int>> v;
  for (int i = start[node] + 1; i < start[node] + sz[node]; i++) {
    int cur = order[i];
    v.emplace_back(res[cur], cur);
  }
  // cout << "node " << node << ": \n";
  // cout << "  descendants: ";
  // for (auto x : v) {
  //   cout << x.second << " ";
  // }
  // cout << "\n";
  sort(v.begin(), v.end());
  res[node] = v[C[node] - 1].first + 1;
  // cout << "  setting res to " << res[node] << '\n';
  for (int i = C[node]; i < v.size(); i++) {
    res[v[i].second] += v[C[node]].first - res[node] + 1;
  }
  // cout << "  ";
  // for (int i = 1; i <= n; i++) {
  //   cout << res[i] << " ";
  // }
  // cout << "\n";
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p; cin >> p >> C[i];
    if (p == 0) {
      root = i;
    } else {
      tree[p].push_back(i);
    }
  }
  predfs(root);
  for (int i = 1; i <= n; i++) {
    res[i] = 1;
    if (C[i] >= sz[i]) {
      cout << "NO\n";
      return 0;
    }
  }
  dfs(root);
  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << res[i] << ' ';
  }
  cout << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MAXN = 2005;

int n, root, cnt, C[MAXN], order[MAXN], start[MAXN], sz[MAXN], res[MAXN];
vector<int> tree[MAXN];

void predfs(int node) {
  sz[node] = 1;
  order[cnt] = node;
  start[node] = cnt;
  cnt++;
  for (int child : tree[node]) {
    predfs(child);
    sz[node] += sz[child];
  }
}

void dfs(int node) {
  for (int child : tree[node]) {
    dfs(child);
  }
  if (sz[node] == 1) {
    return;
  }
  vector<pair<int, int>> v;
  for (int i = start[node] + 1; i < start[node] + sz[node]; i++) {
    int cur = order[i];
    v.emplace_back(res[cur], cur);
  }
  sort(v.begin(), v.end());
  if (C[node] == 0) {
    res[node] = v[0].first - 1;
  } else {
    res[node] = v[C[node] - 1].first + 1;
  }
  for (int i = C[node]; i < v.size(); i++) {
    res[v[i].second] += MAXN;
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p; cin >> p >> C[i];
    if (p == 0) {
      root = i;
    } else {
      tree[p].push_back(i);
    }
  }
  predfs(root);
  for (int i = 1; i <= n; i++) {
    res[i] = MAXN;
    if (C[i] >= sz[i]) {
      cout << "NO\n";
      return 0;
    }
  }
  dfs(root);
  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << res[i] << ' ';
  }
  cout << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MAXN = 2005;

int n, root, cnt, C[MAXN], order[MAXN], start[MAXN], sz[MAXN], res[MAXN];
vector<int> tree[MAXN];

void predfs(int node) {
  sz[node] = 1;
  order[cnt] = node;
  start[node] = cnt;
  cnt++;
  for (int child : tree[node]) {
    predfs(child);
    sz[node] += sz[child];
  }
}

void dfs(int node) {
  for (int child : tree[node]) {
    dfs(child);
  }
  if (sz[node] == 1) {
    return;
  }
  vector<pair<int, int>> v;
  for (int i = start[node] + 1; i < start[node] + sz[node]; i++) {
    int cur = order[i];
    v.emplace_back(res[cur], cur);
  }
  // cout << "node " << node << ": \n";
  // cout << "  descendants: ";
  // for (auto x : v) {
  //   cout << x.second << " ";
  // }
  // cout << "\n";
  // sort(v.begin(), v.end());
  res[node] = v[C[node] - 1].first + 1;
  // cout << "  setting res to " << res[node] << '\n';
  for (int i = C[node]; i < v.size(); i++) {
    res[v[i].second] += v[C[node]].first - res[node] + 1;
  }
  // cout << "  ";
  // for (int i = 1; i <= n; i++) {
  //   cout << res[i] << " ";
  // }
  // cout << "\n";
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p; cin >> p >> C[i];
    if (p == 0) {
      root = i;
    } else {
      tree[p].push_back(i);
    }
  }
  predfs(root);
  for (int i = 1; i <= n; i++) {
    res[i] = 1;
    if (C[i] >= sz[i]) {
      cout << "NO\n";
      return 0;
    }
  }
  dfs(root);
  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << res[i] << ' ';
  }
  cout << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MAXN = 2005;

int n, root, cnt, C[MAXN], order[MAXN], start[MAXN], sz[MAXN], res[MAXN];
vector<int> tree[MAXN];

void predfs(int node) {
  sz[node] = 1;
  order[cnt] = node;
  start[node] = cnt;
  cnt++;
  for (int child : tree[node]) {
    predfs(child);
    sz[node] += sz[child];
  }
}

void dfs(int node) {
  for (int child : tree[node]) {
    dfs(child);
  }
  if (sz[node] == 1) {
    return;
  }
  vector<pair<int, int>> v;
  for (int i = start[node] + 1; i < start[node] + sz[node]; i++) {
    int cur = order[i];
    v.emplace_back(res[cur], cur);
  }
  // cout << "node " << node << ": \n";
  // cout << "  descendants: ";
  for (auto x : v) {
    // cout << "(" << x.first << ", " << x.second << ") ";
  }
  // cout << "\n";
  sort(v.begin(), v.end());
  if (C[node] == 0) {
    res[node] = v[0].first - 1;
  } else {
    res[node] = v[C[node] - 1].first + 1;
  }
  // cout << "  setting res to " << res[node] << '\n';
  for (int i = C[node]; i < v.size(); i++) {
    res[v[i].second] += MAXN; // v[C[node]].first - res[node] + 1;
  }
  // cout << "  ";
  for (int i = 1; i <= n; i++) {
    // cout << res[i] << " ";
  }
  // cout << "\n";
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p; cin >> p >> C[i];
    if (p == 0) {
      root = i;
    } else {
      tree[p].push_back(i);
    }
  }
  predfs(root);
  for (int i = 1; i <= n; i++) {
    res[i] = MAXN;
    if (C[i] >= sz[i]) {
      cout << "NO\n";
      return 0;
    }
  }
  dfs(root);
  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << res[i] << ' ';
  }
  cout << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MAXN = 2005;

int n, root, cnt, C[MAXN], order[MAXN], start[MAXN], sz[MAXN], res[MAXN];
vector<int> tree[MAXN];

void predfs(int node) {
  sz[node] = 1;
  order[cnt] = node;
  start[node] = cnt;
  cnt++;
  for (int child : tree[node]) {
    predfs(child);
    sz[node] += sz[child];
  }
}

void dfs(int node) {
  for (int child : tree[node]) {
    dfs(child);
  }
  if (sz[node] == 1) {
    return;
  }
  vector<pair<int, int>> v;
  for (int i = start[node] + 1; i < start[node] + 1 + sz[node]; i++) {
    int cur = order[i];
    v.emplace_back(res[cur], cur);
  }
  //cout << "node " << node << ": \n";
  sort(v.begin(), v.end());
  res[node] = v[C[node] - 1].first + 1;
  //cout << "  setting res to " << res[node] << '\n';
  for (int i = C[node]; i < v.size(); i++) {
    res[v[i].second] += v[C[node]].first - res[node] + 1;
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p; cin >> p >> C[i];
    if (p == 0) {
      root = i;
    } else {
      tree[p].push_back(i);
    }
  }
  predfs(root);
  for (int i = 1; i <= n; i++) {
    if (C[i] >= sz[i]) {
      cout << "NO\n";
      return 0;
    }
  }
  dfs(root);
  cout << "YES\n";
  for (int i = 1; i <= n; i++) {
    cout << res[i] + 1 << ' ';
  }
  cout << "\n";
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN (int)(2e3 + 5)
#define MAXM (int)(3e5 + 5)
#define fr first
#define sd second
#define MP make_pair
#define pb push_back
#define SZ(A) (int)A.size()
#define ALL(v) v.begin(), v.end()
#define M0(a) memset(a, 0, sizeof(a))
#define M1(a) memset(a, -1, sizeof(a))
#define REP(i,n) for(int i = 0; i < int(n); i++)
#define REPN(i,n,m) for(int i = m; i <= int(n); i++)
#define RREP(i,n) for(int i = int(n-1); i >= 0; i--)
#define RREPN(i, n, m) for(int i = (int)n; i >= (int)m; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const ll INF = ll(1e12 + 7);
const ll MOD = ll(1e9 + 7);
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, pii> ppii;
template<typename T, typename U> inline void AMIN(T &x, U y){ if(x > y) x = y;}
template<typename T, typename U> inline void AMAX(T &x, U y){ if(x < y) x = y;}
vector<int> G[MAXN];
int ANS[MAXN], P[MAXN], COST[MAXN], H[MAXN];
int cnt, MX, ext, id, rt;
vector<vector<int>> V;

void err() {

        cout << "NO\n";
        exit(0);
}

void GH(int p, int v) {

        H[v] = H[p] + 1;
        for(int u : G[v]) {

                if(u != p)
                        GH(v, u);
        }
}

void DFS(int p, int v) {

        for(int u : G[v]) {

                if(u != p) {

                        if(ANS[u])
                                V.pb({COST[u], H[u], u});

                        DFS(v, u);
                }
        }
}

int main() {

        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        int N;
        cin >> N;
        vector<vector<int>> C;

        REP(i, N) {

                int p, c;
                cin >> p >> c;

                if(!p)
                        rt = i + 1;

                C.pb({c, i + 1});
                P[i + 1] = p;
                G[p].pb(i + 1);
                G[i + 1].pb(p);
        }

        sort(ALL(C));
        GH(0, rt);
        REP(i, C.size()) {

                int cur = C[i][1], c = C[i][0];
                cnt = 0, MX = INT_MIN;

                id = cur;
                int idx = cur;
                while(idx)
                        MX = max(MX, ANS[idx]), idx = P[idx];

                DFS(P[cur], cur);
                sort(ALL(V));
                while(V.size() > c)
                        ANS[V.back()[2]]++, V.pop_back();

                if(V.size())
                        MX = max(MX, V.back()[0]);

                while(V.size())
                        V.pop_back();

                ANS[cur] = MX + 1;
        }

        cout << "YES\n";
        REPN(i, N, 1)
                cout << ANS[i] << " " ;
        cout << endl;
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN (int)(2e3 + 5)
#define MAXM (int)(3e5 + 5)
#define fr first
#define sd second
#define MP make_pair
#define pb push_back
#define SZ(A) (int)A.size()
#define ALL(v) v.begin(), v.end()
#define M0(a) memset(a, 0, sizeof(a))
#define M1(a) memset(a, -1, sizeof(a))
#define REP(i,n) for(int i = 0; i < int(n); i++)
#define REPN(i,n,m) for(int i = m; i <= int(n); i++)
#define RREP(i,n) for(int i = int(n-1); i >= 0; i--)
#define RREPN(i, n, m) for(int i = (int)n; i >= (int)m; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const ll INF = ll(1e12 + 7);
const ll MOD = ll(1e9 + 7);
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, pii> ppii;
template<typename T, typename U> inline void AMIN(T &x, U y){ if(x > y) x = y;}
template<typename T, typename U> inline void AMAX(T &x, U y){ if(x < y) x = y;}
vector<int> G[MAXN];
int ANS[MAXN], P[MAXN];
int cnt, MX, ext, id;

void err() {

        cout << "NO\n";
        exit(0);
}

void DFS(int p, int v) {

        for(int u : G[v]) {

                if(u != p) {

                        if(ANS[u])
                                MX = max(MX, ANS[u]), cnt++;

                        DFS(v, u);
                }
        }
}

void DFS1(int p, int v) {

        for(int u : G[v]) {

                if(u != p) {

                        if(ANS[u] && ext && ANS[u] < MX + 1)
                                ANS[u] = MX + 1, ext--;

                        DFS1(v, u);
                }
        }
}

int main() {

        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        int N;
        cin >> N;
        vector<vector<int>> C;

        REP(i, N) {

                int p, c;
                cin >> p >> c;

                C.pb({c, i + 1});
                P[i + 1] = p;
                G[p].pb(i + 1);
                G[i + 1].pb(p);
        }

        sort(ALL(C));
        REP(i, C.size()) {

                int cur = C[i][1], c = C[i][0];
                cnt = 0, MX = INT_MIN;

                id = cur;
                int idx = cur;
                while(idx)
                        MX = max(MX, ANS[idx]), idx = P[idx];

                DFS(P[cur], cur);

                if(cnt < c)
                        err();

                ext = cnt - c;
                ANS[cur] = MX + 1;
                DFS1(P[cur], cur);
        }

        cout << "YES\n";
        REPN(i, N, 1)
                cout << ANS[i] << " " ;
        cout << endl;
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN (int)(2e3 + 5)
#define MAXM (int)(3e5 + 5)
#define fr first
#define sd second
#define MP make_pair
#define pb push_back
#define SZ(A) (int)A.size()
#define ALL(v) v.begin(), v.end()
#define M0(a) memset(a, 0, sizeof(a))
#define M1(a) memset(a, -1, sizeof(a))
#define REP(i,n) for(int i = 0; i < int(n); i++)
#define REPN(i,n,m) for(int i = m; i <= int(n); i++)
#define RREP(i,n) for(int i = int(n-1); i >= 0; i--)
#define RREPN(i, n, m) for(int i = (int)n; i >= (int)m; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const ll INF = ll(1e12 + 7);
const ll MOD = ll(1e9 + 7);
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, pii> ppii;
template<typename T, typename U> inline void AMIN(T &x, U y){ if(x > y) x = y;}
template<typename T, typename U> inline void AMAX(T &x, U y){ if(x < y) x = y;}
vector<int> G[MAXN];
int ANS[MAXN], P[MAXN], COST[MAXN], H[MAXN];
int cnt, MX, ext, id, rt;
vector<vector<int>> V;

void err() {

        cout << "NO\n";
        exit(0);
}

void GH(int p, int v) {

        H[v] = H[p] + 1;
        for(int u : G[v]) {

                if(u != p)
                        GH(v, u);
        }
}

void DFS(int p, int v) {

        for(int u : G[v]) {

                if(u != p) {

                        if(ANS[u])
                                V.pb({ANS[u], H[u], u});

                        DFS(v, u);
                }
        }
}

int main() {

        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        int N;
        cin >> N;
        vector<vector<int>> C;

        REP(i, N) {

                int p, c;
                cin >> p >> c;

                if(!p)
                        rt = i + 1;

                C.pb({c, i + 1});
                P[i + 1] = p;
                G[p].pb(i + 1);
                G[i + 1].pb(p);
        }

        sort(ALL(C));
        GH(0, rt);
        REP(i, C.size()) {

                int cur = C[i][1], c = C[i][0];
                cnt = 0, MX = INT_MIN;

                id = cur;
                int idx = cur;
                while(idx)
                        MX = max(MX, ANS[idx]), idx = P[idx];

                DFS(P[cur], cur);
                sort(ALL(V));
                while(V.size() > c)
                        ANS[V.back()[2]]++, V.pop_back();

                if(V.size())
                        MX = max(MX, V.back()[0]);

                while(V.size())
                        V.pop_back();

                ANS[cur] = MX + 1;
        }

        cout << "YES\n";
        REPN(i, N, 1)
                cout << ANS[i] << " " ;
        cout << endl;
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN (int)(2e3 + 5)
#define MAXM (int)(3e5 + 5)
#define fr first
#define sd second
#define MP make_pair
#define pb push_back
#define SZ(A) (int)A.size()
#define ALL(v) v.begin(), v.end()
#define M0(a) memset(a, 0, sizeof(a))
#define M1(a) memset(a, -1, sizeof(a))
#define REP(i,n) for(int i = 0; i < int(n); i++)
#define REPN(i,n,m) for(int i = m; i <= int(n); i++)
#define RREP(i,n) for(int i = int(n-1); i >= 0; i--)
#define RREPN(i, n, m) for(int i = (int)n; i >= (int)m; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const ll INF = ll(1e12 + 7);
const ll MOD = ll(1e9 + 7);
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, pii> ppii;
template<typename T, typename U> inline void AMIN(T &x, U y){ if(x > y) x = y;}
template<typename T, typename U> inline void AMAX(T &x, U y){ if(x < y) x = y;}
vector<int> G[MAXN];
int ANS[MAXN], P[MAXN], COST[MAXN], H[MAXN];
int cnt, MX, ext, id, rt;
vector<vector<int>> V;

void err() {

        cout << "NO\n";
        exit(0);
}

void GH(int p, int v) {

        H[v] = H[p] + 1;
        for(int u : G[v]) {

                if(u != p)
                        GH(v, u);
        }
}

void DFS(int p, int v) {

        for(int u : G[v]) {

                if(u != p) {

                        if(ANS[u])
                                V.pb({ANS[u], H[u], u});

                        DFS(v, u);
                }
        }
}

int main() {

        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        int N;
        cin >> N;
        vector<vector<int>> C;

        REP(i, N) {

                int p, c;
                cin >> p >> c;

                if(!p)
                        rt = i + 1;

                C.pb({c, i + 1});
                P[i + 1] = p;
                G[p].pb(i + 1);
                G[i + 1].pb(p);
        }

        sort(ALL(C));
        GH(0, rt);
        REP(i, C.size()) {

                int cur = C[i][1], c = C[i][0];
                cnt = 0, MX = INT_MIN;

                id = cur;
                int idx = cur;
                while(idx)
                        MX = max(MX, ANS[idx]), idx = P[idx];

                DFS(P[cur], cur);
                sort(ALL(V));

                if(V.size() < c)
                        err();

                while(V.size() > c)
                        ANS[V.back()[2]]++, V.pop_back();

                if(V.size())
                        MX = max(MX, V.back()[0]);

                while(V.size())
                        V.pop_back();

                ANS[cur] = MX + 1;
        }

        cout << "YES\n";
        REPN(i, N, 1)
                cout << ANS[i] << " " ;
        cout << endl;
        return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = int;
using ld = long double;
const ll mod = 1000000007;
const ll rk = 256;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

ll tim=0;
struct vertex
{
    vc<ll> adj;
    ll vis=0;
    ll parent=-1;
    ll low=0;
	ll val=1;
	ll in=0;
	ll out=0;
	vc<ll> v;
};

void update(vertex x[],ll u,ll sor,ll last)
{
	ll p=x[u].parent,up=last;
	while(u!=sor)
	{
		if(x[p].val<x[u].val)
		{
			x[u].val=up;
			break;
		}
		else if(x[p].val==x[u].val)
		{
			x[u].val=up;
			u=p;
			p=x[u].parent;
		}
		else
		{
			if(up<x[p].val)
			{
				x[u].val=up;
				break;
			}
			else
			{
				x[u].val=up;
				up++;
				u=p;
				p=x[u].parent;
			}
		}
	}
}
bool flag=false;
void dfs(vertex x[],ll sor)
{
	x[sor].in=tim++;
	if(flag)
		return;
	for(auto y:x[sor].adj)
	{
		dfs(x,y);
		vc<ll> vals;
		ll n1=x[sor].v.size(),n2=x[y].v.size(),i=0,j=0,last=0;
		while(i<n1 and j<n2)
		{
			if(x[x[sor].v[i]].val<=x[x[y].v[j]].val)
			{
				vals.pb(x[sor].v[i]);
				i++;
			}
			else
			{
				vals.pb(x[y].v[j]);
				j++;
			}
		}
		while(i<n1)
		{
			vals.pb(x[sor].v[i]);
			i++;
		}
		while(j<n2)
		{
			vals.pb(x[y].v[j]);
			j++;
		}
		x[sor].v=vals;
	}
	if(x[sor].v.size()==0)
		x[sor].v.pb(sor);
	else
	{
		ll last=-1;
		vc<ll> vals=x[sor].v;
		x[sor].v.clear();
		if(vals.size()<x[sor].low)
		{
			flag=true;
			return;
		}
		rep(a,0,x[sor].low){
			x[sor].v.pb(vals[a]);
			last=x[vals[a]].val;
		}
		x[sor].v.pb(sor);
		x[sor].val=last+1;
		last++;
		rep(a,x[sor].low,vals.size())
		{
			ll u=vals[a];
			if(x[u].val<last)
			{
				update(x,u,sor,last);
			}
			last=x[u].val;
			x[sor].v.pb(vals[a]);
		}
	}
	x[sor].out=tim++;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n;
	cin>>n;
	vertex x[n];
	ll sor=-1;
	rep(a,0,n)
	{
		cin>>x[a].parent>>x[a].low;
		x[a].parent--;
		if(x[a].parent==-1)
			sor=a;
		else
			x[x[a].parent].adj.pb(a);
	}
	dfs(x,sor);
	if(flag)
	{
		cout<<"NO\n";
	}
	else
	{
		rep(a,0,n)
		{
			rep(b,0,n)
			{
				if(x[a].in<=x[b].in and x[a].out>=x[b].out and x[a].val>x[b].val)
				{
					x[a].low--;
					if(x[a].low<0)
						flag=true;
				}
			}
		}
		if(flag)
			cout<<"NO\n";
		else
		{
			cout<<"YES\n";
			rep(a,0,n)
				cout<<x[a].val<<" \n"[a==n-1];
		}
	}
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

bool flag=false;
struct vertex
{
	vc<ll> adj;
	ll vis=0;
	ll low=0;
};

vc<ll> dfs(vertex x[],ll sor)
{
	x[sor].vis=1;
	vc<ll> v;
	for(auto y:x[sor].adj)
	{
		vc<ll> p=dfs(x,y);
		for(auto y1:p)
		{
			if((ll)v.size()==x[sor].low)
				v.pb(sor);
			v.pb(y1);
		}
	}
	if((ll)v.size()<x[sor].low)
		flag=true;
	if((ll)v.size()==x[sor].low)
		v.pb(sor);
	return v;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n;
    cin>>n;
    vertex x[n];
    ll sor=-1;
    rep(a,0,n)
    {
		ll par;
		cin>>par>>x[a].low;
		par--;
		if(par!=-1)
			x[par].adj.pb(a);
		else
			sor=a;
	}
	vc<ll> v=dfs(x,sor);
	if(flag)
		cout<<"NO\n";
	else
	{
		cout<<"YES\n";
		ll ans[n];
		rep(a,0,n)
			ans[v[a]]=a+1;
		rep(a,0,n)
			cout<<ans[a]<<" \n"[a==n-1];
	}
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = int;
using ld = long double;
const ll mod = 1000000007;
const ll rk = 256;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

ll tim=0;
struct vertex
{
    vc<ll> adj;
    ll vis=0;
    ll parent=-1;
    ll low=0;
	ll val=1;
	ll in=0;
	ll out=0;
	vc<ll> v;
};

void update(vertex x[],ll u,ll sor,ll last)
{
	ll p=x[u].parent,up=last,po=u;
	while(u!=sor)
	{
		x[u].val=up+(x[u].val-x[po].val);
		u=p;
		p=x[u].parent;
	}
}
bool flag=false;
void dfs(vertex x[],ll sor)
{
	x[sor].in=tim++;
	if(flag)
		return;
	for(auto y:x[sor].adj)
	{
		dfs(x,y);
		vc<ll> vals;
		ll n1=x[sor].v.size(),n2=x[y].v.size(),i=0,j=0,last=0;
		while(i<n1 and j<n2)
		{
			if(x[x[sor].v[i]].val<=x[x[y].v[j]].val)
			{
				vals.pb(x[sor].v[i]);
				i++;
			}
			else
			{
				vals.pb(x[y].v[j]);
				j++;
			}
		}
		while(i<n1)
		{
			vals.pb(x[sor].v[i]);
			i++;
		}
		while(j<n2)
		{
			vals.pb(x[y].v[j]);
			j++;
		}
		x[sor].v=vals;
	}
	if(x[sor].v.size()==0)
		x[sor].v.pb(sor);
	else
	{
		ll last=-1;
		vc<ll> vals=x[sor].v;
		x[sor].v.clear();
		if(vals.size()<x[sor].low)
		{
			flag=true;
			return;
		}
		rep(a,0,x[sor].low){
			x[sor].v.pb(vals[a]);
			last=x[vals[a]].val;
		}
		x[sor].v.pb(sor);
		x[sor].val=last+1;
		last++;
		rep(a,x[sor].low,vals.size())
		{
			ll u=vals[a];
			if(x[u].val<last)
			{
				update(x,u,sor,last);
			}
			last=x[u].val;
			x[sor].v.pb(vals[a]);
		}
	}
	x[sor].out=tim++;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n;
	cin>>n;
	vertex x[n];
	ll sor=-1;
	rep(a,0,n)
	{
		cin>>x[a].parent>>x[a].low;
		x[a].parent--;
		if(x[a].parent==-1)
			sor=a;
		else
			x[x[a].parent].adj.pb(a);
	}
	dfs(x,sor);
	if(flag)
	{
		cout<<"NO\n";
	}
	else
	{
		rep(a,0,n)
		{
			rep(b,0,n)
			{
				if(x[a].in<=x[b].in and x[a].out>=x[b].out and x[a].val>x[b].val)
				{
					x[a].low--;
					if(x[a].low<0)
						flag=true;
				}
			}
		}
		if(flag)
			cout<<"NO\n";
		else
		{
			cout<<"YES\n";
			rep(a,0,n)
				cout<<x[a].val<<" \n"[a==n-1];
		}
	}
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = int;
using ld = long double;
const ll mod = 1000000007;
const ll rk = 256;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

ll tim=0;
struct vertex
{
    vc<ll> adj;
    ll vis=0;
    ll parent=-1;
    ll low=0;
	ll val=1;
	ll in=0;
	ll out=0;
	vc<ll> v;
};

void update(vertex x[],ll u,ll sor,ll last)
{
	ll p=x[u].parent,up=last;
	while(p!=sor)
	{
		if(x[p].val<x[u].val)
		{
			x[u].val=up;
			break;
		}
		else if(x[p].val==x[u].val)
		{
			x[u].val=up;
			u=p;
			p=x[u].parent;
		}
		else
		{
			if(up<x[p].val)
			{
				x[u].val=up;
				break;
			}
			else
			{
				x[u].val=up;
				up++;
				u=p;
				p=x[u].parent;
			}
		}
	}
}
bool flag=false;
void dfs(vertex x[],ll sor)
{
	x[sor].in=tim++;
	if(flag)
		return;
	for(auto y:x[sor].adj)
	{
		dfs(x,y);
		vc<ll> vals;
		ll n1=x[sor].v.size(),n2=x[y].v.size(),i=0,j=0,last=0;
		while(i<n1 and j<n2)
		{
			if(x[x[sor].v[i]].val<=x[x[y].v[j]].val)
			{
				vals.pb(x[sor].v[i]);
				i++;
			}
			else
			{
				vals.pb(x[y].v[j]);
				j++;
			}
		}
		while(i<n1)
		{
			vals.pb(x[sor].v[i]);
			i++;
		}
		while(j<n2)
		{
			vals.pb(x[y].v[j]);
			j++;
		}
		x[sor].v=vals;
	}
	if(x[sor].v.size()==0)
		x[sor].v.pb(sor);
	else
	{
		ll last=-1;
		vc<ll> vals=x[sor].v;
		x[sor].v.clear();
		if(vals.size()<x[sor].low)
		{
			flag=true;
			return;
		}
		rep(a,0,x[sor].low){
			x[sor].v.pb(vals[a]);
			last=x[vals[a]].val;
		}
		x[sor].v.pb(sor);
		x[sor].val=last+1;
		last++;
		rep(a,x[sor].low,vals.size())
		{
			ll u=vals[a];
			if(x[u].val<last)
			{
				update(x,u,sor,last);
			}
			last=x[u].val;
			x[sor].v.pb(vals[a]);
		}
	}
	x[sor].out=tim++;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n;
	cin>>n;
	vertex x[n];
	ll sor=-1;
	rep(a,0,n)
	{
		cin>>x[a].parent>>x[a].low;
		x[a].parent--;
		if(x[a].parent==-1)
			sor=a;
		else
			x[x[a].parent].adj.pb(a);
	}
	dfs(x,sor);
	if(flag)
	{
		cout<<"NO\n";
	}
	else
	{
		rep(a,0,n)
		{
			rep(b,0,n)
			{
				if(x[a].in<=x[b].in and x[a].out>=x[b].out and x[a].val>x[b].val)
				{
					x[a].low--;
					if(x[a].low<0)
						flag=true;
				}
			}
		}
		if(flag)
			cout<<"NO\n";
		else
		{
			cout<<"YES\n";
			rep(a,0,n)
				cout<<x[a].val<<" \n"[a==n-1];
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2005;

int c[MAXN], ans[MAXN], sz[MAXN];
vector < int > a, adj[MAXN];
int root, n;

void countSz(int u) {
    for(int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        countSz(v);
        sz[u] += sz[v];
    }
    if (c[u] > sz[u]) {
        cout << "NO";
        exit(0);
    }
}

void dfs(int u) {
    //cout << c[u] + 1 << " " << a.size() << endl;
    ans[u] = a[c[u] + 1];
    //cout << ans[u] << endl;
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    a.erase(a.begin() + c[u] + 1);
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    for(int i = 0; i < adj[u].size(); i++)
        dfs(adj[u][i]);
}

int main() {
    //freopen("test.in", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int p;
        cin >> p >> c[i];
        adj[p].push_back(i);
        if (p == 0)
            root = i;
    }
    for(int i = 0; i <= n; i++)
        a.push_back(i);
    //cout << root << endl;
    countSz(root);
    dfs(root);
    cout << "YES\n";
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2005;

int c[MAXN], ans[MAXN];
vector < int > a, adj[MAXN];
int root, n;

void dfs(int u) {
    if (c[u] + 1 > a.size()) {
        cout << "NO";
        exit(0);
    }
    ans[u] = a[c[u] + 1];
    //cout << ans[u] << endl;
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    a.erase(a.begin() + c[u] + 1);
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    for(int i = 0; i < adj[u].size(); i++)
        dfs(adj[u][i]);
}

int main() {
//    freopen("test.in", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int p;
        cin >> p >> c[i];
        adj[p].push_back(i);
        if (p == 0)
            root = i;
    }
    for(int i = 0; i <= n; i++)
        a.push_back(i);
    //cout << root << endl;
    dfs(root);
    cout << "YES\n";
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2005;

int c[MAXN], ans[MAXN];
vector < int > a, adj[MAXN];
int root, n;

void dfs(int u) {
    //cout << c[u] + 1 << " " << a.size() << endl;
    if (c[u] + 1 > a.size() - 1) {
        cout << "NO";
        exit(0);
    }
    ans[u] = a[c[u] + 1];
    //cout << ans[u] << endl;
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    a.erase(a.begin() + c[u] + 1);
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    for(int i = 0; i < adj[u].size(); i++)
        dfs(adj[u][i]);
}

int main() {
    //freopen("test.in", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int p;
        cin >> p >> c[i];
        adj[p].push_back(i);
        if (p == 0)
            root = i;
    }
    for(int i = 0; i <= n; i++)
        a.push_back(i);
    //cout << root << endl;
    dfs(root);
    cout << "YES\n";
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2005;

int c[MAXN], ans[MAXN], sz[MAXN];
vector < int > a, adj[MAXN];
int root, n;

void countSz(int u) {
    sz[u] = 1;
    for(int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        countSz(v);
        sz[u] += sz[v];
    }
    //cout << sz[u] << " " << c[u] << endl;
    if (c[u] > sz[u] - 1) {
        cout << "NO";
        exit(0);
    }
}

void dfs(int u) {
    //cout << c[u] + 1 << " " << a.size() << endl;
    ans[u] = a[c[u] + 1];
    //cout << ans[u] << endl;
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    a.erase(a.begin() + c[u] + 1);
    //for(int i = 0; i < a.size(); i++)
    //    cout << a[i] << " ";
    //cout << endl;
    for(int i = 0; i < adj[u].size(); i++)
        dfs(adj[u][i]);
}

int main() {
    //freopen("test.in", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int p;
        cin >> p >> c[i];
        adj[p].push_back(i);
        if (p == 0)
            root = i;
    }
    for(int i = 0; i <= n; i++)
        a.push_back(i);
    //cout << root << endl;
    countSz(root);
    dfs(root);
    cout << "YES\n";
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    return 0;
}


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

ifstream in("numbers.in");

vector<ll> copii[2005];
ll n,p[2005],c[2005],sol[2005],bad;
ll ooo = 0;
void valorile_copiilor(ll node , vector<ll> &v)
{
    v.push_back(node);
    for (auto k : copii[node])
        valorile_copiilor(k , v);
}
ll crit(ll a, ll b)
{
    return sol[a] < sol[b];
}
void solve(ll node)
{ll i;
if (bad) return;
    if (copii[node].empty()) {
        if (c[node] > 0) {
            bad = 1;
            cout << "NO\n";
            return;
        }
        sol[node] = 1;
        return;
    }

    for (auto k : copii[node])
        solve(k);

    vector<ll> v;
    v.clear();
    for (auto k : copii[node]) valorile_copiilor(k , v);
    if (v.size() < c[node]) {
        cout << "NO\n";
        bad = 1;
        return;
    }
    sort(v.begin(), v.end(), crit);
ll x;
    sol[node] = sol[v[v.size() - 1]] + 1;
    if (c[node] == 0) x= sol[node];
    else
    x = sol[node] - sol[c[node] - 1] + 1;
    for (i = c[node]; i < v.size(); i++)
        sol[v[i]] += x;
}
int main()
{ll i,root;
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cin >> p[i] >> c[i];
        copii[p[i]].push_back(i);
        if (p[i] == 0) root = i;
    }
    solve(root);
    if (bad)return 0;
    cout << "YES\n";
    for (i = 1; i <= n; i++) cout << sol[i] << " ";
    cout << "\n";
}
///problema 3


#include<bits/stdc++.h>
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define UpperCase(x) transform(all(x), x.begin(), ::toupper)
#define LowerCase(x) transform(all(x), x.begin(), ::tolower)
#define FindIndex(v,x) (int)(find(all(v) , x) - v.begin())
#define Done(x) return cout << x << "\n" , 0 ;
#define PI 3.14159265358979323846
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rev(v) reverse(all(v))
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

using namespace std;

typedef long long ll ;

int main(){

    FAST ;

    int n ; cin >> n ;
    unordered_map<int,int> mp ;
    int ans = 0 ;
    while(n--){
        int no ; cin >> no ;
        for(int pow = 0 ; pow <= 30 ; ++pow){
            int TwoToThePow = (1 << pow) ;
            auto GetRepeated =
            [=](int wanted)
            {
                auto it = mp.find(wanted);
                return (it == mp.end() ? 0 : it->second);
            };
            ans += GetRepeated(TwoToThePow - no);
        }
        mp[no]++;
    }
    cout << ans ;
}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n; cin>>n;
    ll a[n];
    map<ll,ll> mp;
    for(ll i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    ll p2[32];
    for(ll i=0;i<32;i++){
        p2[i] = pow(2,i);
    }
    ll ans = 0;

     for(ll j=0;j<32;j++){
          map<ll,ll> visited;
    for(ll i=0;i<n;i++){
        ll num = p2[j]-a[i];
            if(a[i]!=num && mp[num]>0 ){
                if(visited[num]==0 && visited[a[i]]==0){
                ans+=mp[num]*mp[a[i]];
                visited[num]++;
                visited[a[i]]++;
                }
                // cout<<a[i]<<" "<<num<<endl;
            }
            else if(a[i]==num && mp[num]>=2 && visited[num]==0){
                ans+=(mp[num]*(mp[num]-1))/2;
                // cout<<(mp[num]*(mp[num]-1))/2<<endl;
                //  cout<<a[i]<<" "<<num<<endl;
                 visited[num]++;
            }

        }
    }
    cout<<ans<<endl;

}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n; cin>>n;
    ll a[n];
    unordered_map<ll,ll> mp;
    for(ll i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    ll p2[32];
    for(ll i=0;i<31;i++){
        p2[i] = pow(2,i);
    }
    ll ans = 0;

     for(ll j=0;j<31;j++){
          unordered_map<ll,ll> visited;
    for(ll i=0;i<n;i++){
        ll num = p2[j]-a[i];
            if(a[i]!=num && mp[num]>0 ){
                if(visited[num]==0 && visited[a[i]]==0){
                ans+=mp[num]*mp[a[i]];
                visited[num]++;
                visited[a[i]]++;
                }
                // cout<<a[i]<<" "<<num<<endl;
            }
            else if(a[i]==num && mp[num]>=2 && visited[num]==0){
                ans+=(mp[num]*(mp[num]-1))/2;
                // cout<<(mp[num]*(mp[num]-1))/2<<endl;
                //  cout<<a[i]<<" "<<num<<endl;
                 visited[num]++;
            }

        }
    }
    cout<<ans<<endl;

}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin>>n;
    int a[n];
    map<int,int> mp;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    int p2[32];
    for(int i=0;i<32;i++){
        p2[i] = pow(2,i);
    }
    int ans = 0;

     for(int j=0;j<32;j++){
          map<int,int> visited;
    for(int i=0;i<n;i++){
        int num = p2[j]-a[i];
            if(a[i]!=num && mp[num]>0 ){
                if(visited[num]==0 && visited[a[i]]==0){
                ans+=mp[num]*mp[a[i]];
                visited[num]++;
                visited[a[i]]++;
                }
                // cout<<a[i]<<" "<<num<<endl;
            }
            else if(a[i]==num && mp[num]>=2 && visited[num]==0){
                ans+=(mp[num]*(mp[num]-1))/2;
                // cout<<(mp[num]*(mp[num]-1))/2<<endl;
                //  cout<<a[i]<<" "<<num<<endl;
                 visited[num]++;
            }

        }
    }
    cout<<ans<<endl;

}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n; cin>>n;
    ll a[n];
    map<ll,ll> mp;
    for(ll i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    ll p2[32];
    for(ll i=0;i<31;i++){
        p2[i] = pow(2,i);
    }
    ll ans = 0;

     for(ll j=0;j<31;j++){
          map<ll,ll> visited;
    for(ll i=0;i<n;i++){
        ll num = p2[j]-a[i];
            if(a[i]!=num && mp[num]>0 ){
                if(visited[num]==0 && visited[a[i]]==0){
                ans+=mp[num]*mp[a[i]];
                visited[num]++;
                visited[a[i]]++;
                }
                // cout<<a[i]<<" "<<num<<endl;
            }
            else if(a[i]==num && mp[num]>=2 && visited[num]==0){
                ans+=(mp[num]*(mp[num]-1))/2;
                // cout<<(mp[num]*(mp[num]-1))/2<<endl;
                //  cout<<a[i]<<" "<<num<<endl;
                 visited[num]++;
            }

        }
    }
    cout<<ans<<endl;

}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin>>n;
    int a[n];
    map<int,int> mp;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    int p2[32];
    for(int i=0;i<n;i++){
        p2[i] = pow(2,i);
    }
    int ans = 0;

     for(int j=0;j<32;j++){
          map<int,int> visited;
    for(int i=0;i<n;i++){
        int num = p2[j]-a[i];
            if(a[i]!=num && mp[num]>0 && visited[num]==0 && visited[a[i]]==0){
                ans+=mp[num]*mp[a[i]];
                visited[num]++;
                visited[a[i]]++;
                // cout<<a[i]<<" "<<num<<endl;
            }
            else if(a[i]==num && mp[num]>=2 && visited[num]==0){
                ans+=(mp[num]*(mp[num]-1))/2;
                // cout<<(mp[num]*(mp[num]-1))/2<<endl;
                //  cout<<a[i]<<" "<<num<<endl;
                 visited[num]++;
            }

        }
    }
    cout<<ans<<endl;

}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin>>n;
    int a[n];
    map<int,int> mp;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    int p2[32];
    for(int i=0;i<n;i++){
        p2[i] = pow(2,i);
    }
    int ans = 0;

     for(int j=0;j<32;j++){
          map<int,int> visited;
    for(int i=0;i<n;i++){
        int num = p2[j]-a[i];
            if(a[i]!=num && mp[num]>0 ){
                if(visited[num]==0 && visited[a[i]]==0){
                ans+=mp[num]*mp[a[i]];
                visited[num]++;
                visited[a[i]]++;
                }
                // cout<<a[i]<<" "<<num<<endl;
            }
            else if(a[i]==num && mp[num]>=2 && visited[num]==0){
                ans+=(mp[num]*(mp[num]-1))/2;
                // cout<<(mp[num]*(mp[num]-1))/2<<endl;
                //  cout<<a[i]<<" "<<num<<endl;
                 visited[num]++;
            }

        }
    }
    cout<<ans<<endl;

}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    ll n; cin >> n;
    vector <ll> vec(n);
    map <ll, ll> mp;
    map <ll, bool> mp2;
    for(ll i = 0; i < n; i++){
        cin >> vec[i];
        mp[vec[i]]++;
    }
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(vec[i] == powersOfTwo[j]){
                mp2[vec[i]] = true;
            }
        }
    }
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(powersOfTwo[j] > vec[i] && !mp2[vec[i]] && mp[powersOfTwo[j] - vec[i]] > 0){
                ans += mp[powersOfTwo[j] - vec[i]];
                mp[vec[i]]--;
            }else if(powersOfTwo[j] > vec[i] && mp2[vec[i]] && powersOfTwo[j] == 2 * vec[i] && mp[powersOfTwo[j] - vec[i]] > 0){
                if(mp[powersOfTwo[j] - vec[i]] - 1 > 0){
                    ans += mp[powersOfTwo[j] - vec[i]] - 1;
                    mp[vec[i]]--;
                    cout << 2 << endl;
                }
            }
        }
    }
    cout << ans << endl;
}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    ll n; cin >> n;
    vector <ll> vec(n);
    map <ll, ll> mp;
    map <ll, bool> mp2;
    for(ll i = 0; i < n; i++){
        cin >> vec[i];
        mp[vec[i]]++;
    }
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(vec[i] == powersOfTwo[j]){
                mp2[vec[i]] = true;
            }
        }
    }
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(powersOfTwo[j] > vec[i] && !mp2[vec[i]] && mp[powersOfTwo[j] - vec[i]] > 0){
                ans += mp[powersOfTwo[j] - vec[i]];
                mp[vec[i]]--;
            }else if(powersOfTwo[j] > vec[i] && mp2[vec[i]] && powersOfTwo[j] == 2 * vec[i] && mp[powersOfTwo[j] - vec[i]] > 0){
                if(mp[powersOfTwo[j] - vec[i]] - 1 > 0){
                    ans += mp[powersOfTwo[j] - vec[i]] - 1;
                    mp[vec[i]]--;
                }
            }else if(powersOfTwo[j] > vec[i] && mp2[vec[i]] && powersOfTwo[j] != 2 * vec[i] && mp[powersOfTwo[j] - vec[i]] > 0){
                ans += mp[powersOfTwo[j] - vec[i]];
                mp[vec[i]]--;
            }
        }
    }
    cout << ans << endl;
}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    ll n; cin >> n;
    vector <ll> vec(n);
    map <ll, ll> mp;
    map <ll, bool> mp2;
    for(ll i = 0; i < n; i++){
        cin >> vec[i];
        mp[vec[i]]++;
    }
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(vec[i] == powersOfTwo[j]){
                mp2[vec[i]] = true;
            }
        }
    }
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(powersOfTwo[j] > vec[i]){
                if(mp[powersOfTwo[j] - vec[i]] > 0 && powersOfTwo[j] - vec[i] != vec[i]){
                    ans += mp[powersOfTwo[j] - vec[i]];
                    mp[vec[i]]--;
                }else if(mp[powersOfTwo[j] - vec[i]] > 0 && powersOfTwo[j] - vec[i] == vec[i]){
                    ans += mp[powersOfTwo[j] - vec[i]] - 1;
                    mp[vec[i]]--;
                }
            }else{
                continue;
            }
        }
    }
    cout << ans << endl;
}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    ll n; cin >> n;
    vector <ll> vec(n);
    map <ll, ll> mp;
    for(ll i = 0; i < n; i++){
        cin >> vec[i];
        mp[vec[i]]++;
    }
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        mp[vec[i]]--;
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(powersOfTwo[j] > vec[i]){
                ans += mp[powersOfTwo[j] - vec[i]];
            }
        }
    }
    cout << ans << endl;
}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    ll n; cin >> n;
    vector <ll> vec(n);
    map <ll, ll> mp;
    map <ll, bool> mp2;
    for(ll i = 0; i < n; i++){
        cin >> vec[i];
        mp[vec[i]]++;
    }
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(vec[i] == powersOfTwo[j]){
                mp2[vec[i]] = true;
            }
        }
    }
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(powersOfTwo[j] > vec[i]){
                if(mp[powersOfTwo[j] - vec[i]] > 0 && powersOfTwo[j] - vec[i] != vec[i]){
                    ans += mp[powersOfTwo[j] - vec[i]];
                }else if(mp[powersOfTwo[j] - vec[i]] > 0 && powersOfTwo[j] - vec[i] == vec[i]){
                    ans += mp[powersOfTwo[j] - vec[i]] - 1;
                }
            }
        }
        mp[vec[i]]--;
    }
    cout << ans << endl;
}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    ll n; cin >> n;
    vector <ll> vec(n);
    map <ll, ll> mp;
    map <ll, bool> mp2;
    for(ll i = 0; i < n; i++){
        cin >> vec[i];
        mp[vec[i]]++;
    }
    for(ll i = 0; i < n; i++){
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(vec[i] == powersOfTwo[j]){
                mp2[vec[i]] = true;
            }
        }
    }
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        mp[vec[i]]--;
        for(ll j = 0; j < powersOfTwo.size(); j++){
            if(powersOfTwo[j] > vec[i] && mp[powersOfTwo[j] - vec[i]] > 0){
                ans += mp[powersOfTwo[j] - vec[i]];
            }
        }
    }
    cout << ans << endl;
}


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

int main() { _
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
    ll power = 1;
    vector <ll> powersOfTwo;
    while(power <= 2e9){
        powersOfTwo.push_back(power);
        power *= 2;
    }
    int n; cin >> n;
    vector <int> vec(n);
    multiset <int> set1;
    for(int i = 0; i < n; i++){
        cin >> vec[i];
        set1.insert(vec[i]);
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < powersOfTwo.size(); j++){
            if(vec[i] < powersOfTwo[j]){
                ans += set1.count(powersOfTwo[j] - vec[i]);
            }
        }
        set1.erase(set1.find(vec[i]));
    }
    cout << ans / 2 << endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include<map>
//#include<cmath>
//#include <set>
//#include <fstream>

#define ll long long int
using namespace std;

void Solve()
{
    int n;
    cin >> n;
    map<ll, int>p;
    vector<int>a(n);
    for(int i=0; i<n; i++)
    {
        ll x;
        cin >> x;
        p[x]++;
        a[i] = x;
    }
    //for(auto& x: p) cout << x.first << ' ' << x.second << '\n';
    int cnt = 0;
    for(int i=0; i<n; i++)
    {
        ll x;
        if(a[i] == 1) x = 2;
        else
        {
            x = ceil(log2(a[i]));
            x = pow(2, x);
        }

//        int x = ceil();
       //
        //cout << x << ' ' << a[i] << "&&&\n";
        if(p[x - a[i]] != 0)
        {
            cnt += p[x - a[i]];
            if((x-a[i]) == a[i])cnt--;
        }
        p[a[i]]--;
    }
    cout << cnt;
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie( nullptr );
    //freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
   // int t;
    //cin >> t;
   // while(t--)
    {
        Solve();
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include<map>
#include<cmath>
//#include <set>
//#include <fstream>

#define ll long long int
using namespace std;

void Solve()
{
    int n;
    cin >> n;
    map<ll, int>p;
    vector<int>a(n);
    for(int i=0; i<n; i++)
    {
        ll x;
        cin >> x;
        p[x]++;
        a[i] = x;
    }
    //for(auto& x: p) cout << x.first << ' ' << x.second << '\n';
    int cnt = 0;
    for(int i=0; i<n; i++)
    {
        ll x;
        if(a[i] == 1) x = 2;
        else
        {
            x = ceil(log2(a[i]));
            x = pow(2, x);
        }

//        int x = ceil();
       //
        //cout << x << ' ' << a[i] << "&&&\n";
        if(p[x - a[i]] != 0)
        {
            cnt += p[x - a[i]];
            if((x-a[i]) == a[i])cnt--;
        }
        p[a[i]]--;
    }
    cout << cnt;
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie( nullptr );
    //freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
   // int t;
    //cin >> t;
   // while(t--)
    {
        Solve();
        cout << '\n';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include<map>
#include<cmath>
//#include <set>
//#include <fstream>

#define ll long long int
using namespace std;

void Solve()
{
    int n;
    cin >> n;
    map<ll, int>p;
    vector<int>a(n);
    for(int i=0; i<n; i++)
    {
        ll x;
        cin >> x;
        p[x]++;
        a[i] = x;
    }
    //for(auto& x: p) cout << x.first << ' ' << x.second << '\n';
    int cnt = 0;
    for(int i=0; i<n; i++)
    {
        ll x;
        if(a[i] == 1) x = 2;
        else
        {
            x = ceil(log2(a[i]));
            x = pow(2, x);
        }

        if(p[x - a[i]] != 0)
        {
            p[a[i]]--;
            cnt += p[x - a[i]];
        }

    }
    cout << cnt;
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie( nullptr );
    //freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
   // int t;
    //cin >> t;
   // while(t--)
    {
        Solve();
        cout << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;
 if(n==1)
 {
 	if(a[0]==1)cout<<1;
 	else
 	{cout<<0;
	 }

 }
 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=m[j-a[i]];
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 2000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;

 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 		    k=m[j-a[i]];
 			if(k)sm+=k;
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;
 if(n==1)
 {
 	if(a[0]==1)cout<<1;
 	else
 	{
 	  cout<<0;
	 }
	 re0;
 }
 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=m[j-a[i]];
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000000000000001
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;

 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 		    j-a[i]<1000000007?k=m[j-a[i]]:k=0;
 			if(k)sm+=k;
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;
 if(n==1)
 {
 	if(a[0]==1)cout<<1;
 	else
 	{
 	 else cout<<0;
	 }
	 re0;
 }
 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=m[j-a[i]];
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;
 if(n==1)
 {
 	if(a[0]==1)cout<<1;
 	else
 	{
 		j=2;
 		while(a[i]%2==0)a[i]/=2;
 		if(a[i]==1)cout<<1;
 		else cout<<0;
	 }

 }
 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=m[j-a[i]];
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000000000000001
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 multiset<ll>s;

 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=s.count(j-a[i]);
		 }
		 j*=2;
	 }
	 s.insert(a[i]);
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;

 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			k=m[j-a[i]];
 			if(k)sm+=k;
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000007
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;
 if(n==1)
 {
 	if(a[0]==1)cout<<1;
 	else
 	{
 		while(a[i]%2==0)a[i]/=2;
 		if(a[i]==1)cout<<1;
 		else cout<<0;
	 }
	 re0;
 }
 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=m[j-a[i]];
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


#include <bits/stdc++.h>
#define  na
#define ll long long
#define  ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define vvi vector<vector<int> >
#define vii vector<pair<int,int> >
#define pb push_back
#define inf 1000000000000000001
#define f(i,a,b) for(i=a;i<b;i++)
#define re0 return 0
using namespace std;
bool cmp(pair<ll,int > &a,pair<ll,int > &b)
{
    return a.ss<b.ss;
}
vector<vector<int> > v(100001);
map<pair<int,int>,int>q;
int vis[100001];
int cc;
ll exp(ll j)
{
	ll i=2,res=1;

	while(j>0)
	{
		if(j&1)res*=i;
		 i=i*i;
		i%=inf,res%=inf;
		j>>=1;
	}
	return (res-1)%inf;
}
void dfs(int node,int par)
{
    vis[node]=1;
	q[{min(par,node),max(par,node)}]=cc;
	cc--;

	for(auto child : v[node])
	{
		if(vis[child]==0)
		{
			dfs(child,node);
		}
	}
}
int main()
{
	#ifdef ha
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

 ll n,i,j,k,sm=0;

 cin>>n;

 ll a[n];
 f(i,0,n)cin>>a[i];

 map<ll,int>m;

 for(i=0;i<n;i++)
 {
 	j=1;
 	while(j<inf)
 	{
 		if(j>a[i])
 		{
 			sm+=m[j-a[i]];
		 }
		 j*=2;
	 }
	 m[a[i]]++;
 }
 cout<<sm;
}


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

int z[100000];
int arr[100000];
long long p[32];
bool isPow(int n)
{
  return (n & (n - 1)) == 0;
}

int main()
{
  ios::sync_with_stdio(false);
  p[0] = 1;
  for (int i = 1; i < 32; i++)
  {
    p[i] = 2 * p[i - 1];
  }

  int n, l, u, a;
  long long res = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);

  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < 32; j++)
    {
      a = p[j] - arr[i];
      if (a > 0) {
        auto it = lower_bound(arr + i + 1, arr + n, a);
        if (it != (arr + n)) {
          u = upper_bound(arr + i + 1, arr + n, a) - arr;
          res += u - (it - arr);
        }
      }
    }
  }

  cout << res << endl;
  return 0;
}


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

int z[100000];
int arr[100000];

bool isPow(int n)
{
  return (n & (n - 1)) == 0;
}

int main()
{
  ios::sync_with_stdio(false);

  int n, res = 0, sum, a, j =0;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    cin >> arr[i];
    j = i;
    a = arr[j];
    while (j > 0)
      if (isPow(a + arr[--j])) res++;
  }

  cout << res << endl;
  return 0;
}


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

int z[100000];
int arr[100000];
long long p[32];
bool isPow(int n)
{
  return (n & (n - 1)) == 0;
}

int main()
{
  ios::sync_with_stdio(false);
  p[0] = 1;
  for (int i = 1; i < 32; i++)
  {
    p[i] = 2 * p[i - 1];
  }

  int n, res = 0, sum, a, j = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);

  for (int i = 0; i < n; i++)
  {
    if (binary_search(arr + i + 1, arr + n, arr[i]))
      res++;
  }

  cout << res << endl;
  return 0;
}


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

int z[100000];
int arr[100000];

bool isPow(int a)
{
  while (a > 1)
  {
    if (a % 2 != 0) return false;
    else a /= 2;
  }
  return true;
}

int main()
{
  ios::sync_with_stdio(false);

  int n, res = 0, sum, a, j =0;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    cin >> arr[i];
    j = i;
    a = arr[j];
    while (j > 0)
      if (isPow(a + arr[--j])) res++;
  }

  cout << res << endl;
  return 0;
}


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

int z[100000];
int arr[100000];
long long p[32];
bool isPow(int n)
{
  return (n & (n - 1)) == 0;
}

int main()
{
  ios::sync_with_stdio(false);
  p[0] = 1;
  for (int i = 1; i < 32; i++)
  {
    p[i] = 2 * p[i - 1];
  }

  int n, res = 0, l,u,  sum, a, j = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);

  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < 32; j++)
    {
      a = p[j] - arr[i];
      if (a > 0) {
        auto it = lower_bound(arr + i + 1, arr + n, a);
        if (it != (arr + n)) {
          u = upper_bound(arr + i + 1, arr + n, a) - arr;
          res += u - (it - arr);
        }
      }
    }
  }

  cout << res << endl;
  return 0;
}


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

int z[100000];
int arr[100000];

bool isPow(int a)
{
  while (a > 1)
  {
    if (a % 2 != 0) return false;
    else a /= 2;
  }
  return true;
}

int main()
{
  ios::sync_with_stdio(false);

  int n, res = 0, sum;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];

  for (int i = 0; i < n; i++)
  {
    for (int j = i + 1; j < n; j++)
    {
      sum = arr[i] + arr[j];
      if (isPow(sum)) res++;
    }
  }

  cout << res << endl;
  return 0;
}


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

int z[100000];
int arr[100000];
long long p[32];
bool isPow(int n)
{
  return (n & (n - 1)) == 0;
}

int main()
{
  ios::sync_with_stdio(false);
  p[0] = 1;
  for (int i = 1; i < 32; i++)
  {
    p[i] = 2 * p[i - 1];
  }

  int n, res = 0, sum, a, j = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);

  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < 32; j++)
    {
      a = p[j] - arr[i];
      if (a > 0 && binary_search(arr + i + 1, arr + n, a))
        res++;
    }
  }

  cout << res << endl;
  return 0;
}


#include<bits/stdc++.h>
#define maxn 200005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll dx[maxn], a[maxn];
map<ll, int> M;
set<int> S;
int main() {
	dx[0] = 1;
	int z=1;
	for(; dx[z]*2<=2000000000; z++) {
		dx[z] = dx[z-1]*2;
		if(dx[z]>2000000000) {
			z--;
			break;
		}
	}
	int n;
	cin >> n;
	for(int i=0; i<n; i++) {
		scanf("%lld",&a[i]);
		M[a[i]]++;
	}
	ll ans = 0;
	sort(a, a+n);
	for(int i=0; i<n; i++) {
		for(int j=0; j<=z; j++) {
			if(M[dx[j]-a[i]]&&!S.count(a[i])) {
			//	cout << a[i] << " " << dx[j]-a[i] << "\n";
				if(dx[j] == a[i]*2) {
					ans += M[dx[j]-a[i]]*(M[dx[j]-a[i]]-1)/2;
					//	cout << ans << endl;
				} else if(!S.count(dx[j]-a[i]))ans += M[a[i]]*M[dx[j]-a[i]];

			}
		}
		S.insert(a[i]);
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
#define maxn 200005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll dx[maxn], a[maxn];
map<ll, int> M;
set<int> S;
int main() {
	dx[0] = 1;
	int z=1;
	for(;dx[z]*2<=2000000000;z++) {
		dx[z] = dx[z-1]*2;
		if(dx[z]>2000000000) {
			z--;
			break;
		}
	}
	int n;
	cin >> n;
	for(int i=0;i<n;i++) {
		scanf("%lld",&a[i]);
		M[a[i]]++;
	}
	ll ans = 0;
	sort(a, a+n);
	for(int i=0;i<n;i++) {
		for(int j=0;j<=z;j++) {
			if(M[dx[j]-a[i]]&&!S.count(a[i])) {
			//	cout << a[i] << " " << dx[j]-a[i] << "\n";
				if(dx[j] == a[i]*2) {
					ans += M[dx[j]-a[i]]*(M[dx[j]-a[i]]-1)/2;
				//	cout << ans << endl;
				}
				else ans += M[a[i]]*M[dx[j]-a[i]];
				S.insert(a[i]);
			}
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
#define maxn 200005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll dx[maxn], a[maxn];
map<ll, int> M;
set<int> S;
int main() {
	dx[0] = 1;
	int z=1;
	for(;dx[z]*2<=2000000000;z++) {
		dx[z] = dx[z-1]*2;
		if(dx[z]>2000000000) {
			z--;
			break;
		}
	}
	int n;
	cin >> n;
	for(int i=0;i<n;i++) {
		scanf("%lld",&a[i]);
		M[a[i]]++;
	}
	ll ans = 0;
	for(int i=0;i<n;i++) {
		for(int j=0;j<=z;j++) {
			if(M[dx[j]-a[i]]&&!S.count(a[i])) {
			//	cout << a[i] << " " << dx[j]-a[i] << endl;
				if(dx[j] == a[i]*2) ans += M[dx[j]-a[i]]*(M[dx[j]-a[i]]-1);
				else ans += M[a[i]]*M[dx[j]-a[i]];
				S.insert(a[i]);
			}
		}
	}
	cout << ans/2 << endl;
}


#include<bits/stdc++.h>
#define maxn 200005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll dx[maxn], a[maxn];
map<ll, ll> M;
set<int> S;
int main() {
	dx[0] = 1;
	int z=1;
	for(; dx[z]*2<=2000000000; z++) {
		dx[z] = dx[z-1]*2;
		if(dx[z]>2000000000) {
			z--;
			break;
		}
	}
	int n;
	cin >> n;
	for(int i=0; i<n; i++) {
		scanf("%lld",&a[i]);
		M[a[i]]++;
	}
	ll ans = 0;
	sort(a, a+n);
	for(int i=0; i<n; i++) {
		for(int j=0; j<=z; j++) {
			if(M[dx[j]-a[i]]&&!S.count(a[i])) {
			//	cout << a[i] << " " << dx[j]-a[i] << "\n";
				if(dx[j] == a[i]*2) {
					ans += M[dx[j]-a[i]]*(M[dx[j]-a[i]]-1)/2;
					//	cout << ans << endl;
				} else if(!S.count(dx[j]-a[i]))ans += M[a[i]]*M[dx[j]-a[i]];

			}
		}
		S.insert(a[i]);
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
#define maxn 200005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll dx[maxn], a[maxn];
map<ll, int> M;
set<int> S;
int main() {
	dx[0] = 1;
	int z=1;
	for(;dx[z]*2<=2000000000;z++) {
		dx[z] = dx[z-1]*2;
		if(dx[z]>2000000000) {
			z--;
			break;
		}
	}
	int n;
	cin >> n;
	for(int i=0;i<n;i++) {
		scanf("%lld",&a[i]);
		M[a[i]]++;
	}
	ll ans = 0;
	sort(a, a+n);
	for(int i=0;i<n;i++) {
		for(int j=0;j<=z;j++) {
			if(M[dx[j]-a[i]]&&!S.count(a[i])) {
			//	cout << a[i] << " " << dx[j]-a[i] << "\n";
				if(dx[j] == a[i]*2) {
					ans += M[dx[j]-a[i]]*(M[dx[j]-a[i]]-1)/2;
				//	cout << ans << endl;
				}
				else if(!S.count(dx[j]-a[i]))ans += M[a[i]]*M[dx[j]-a[i]];
				S.insert(a[i]);
			}
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include "Header.h"

#define ll long long

#define fori(n) for(long long i=0;i<n;i++)
#define forj(n,i) for(long long j=i+1;j<n;j++)

using namespace std;

void merge(long long arr[], long long l, long long  m, long long  r)
{
    long long  i, j, k;
    long long  n1 = m - l + 1;
    long long  n2 =  r - m;

    long long  L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void mergeSort(long long arr[], long long  l, long long  r)
{
    if (l < r)
    {
        long long  m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

int isPrime(long long k){
    if(k<=1){
        return 0;
    }
    if(k==2){
        return 1;
    }
    if(k%2==0){
        return 0;
    }
    long long i = 3;

    while (i*i <= k) {
        if (k % i == 0) {
            return 0;
        }
        i += 2;
    }
return 1;
}

int main(int argc, const char * argv[]) {

//cout<<"Om\n";
    ll n,count=0;
    cin>>n;
    ll arr[n];
    for(long long i=0;i<n;i++){
        cin>>arr[i];
    }
    //cout<<(6&2)<<" ";
    for(ll i=0;i<n-1;i++){
        for(ll j=i+1;j<n;j++){

            if((arr[i]&(arr[j]-1))==0){
                count=count+1;
            }
        }
    }
    cout<<count;

return 0;
}


#include <bits/stdc++.h>
//#include "Header.h"

#define ll long long

#define fori(n) for(long long i=0;i<n;i++)
#define forj(n,i) for(long long j=i+1;j<n;j++)

using namespace std;

void merge(long long arr[], long long l, long long  m, long long  r)
{
    long long  i, j, k;
    long long  n1 = m - l + 1;
    long long  n2 =  r - m;

    long long  L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void mergeSort(long long arr[], long long  l, long long  r)
{
    if (l < r)
    {
        long long  m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

int isPrime(long long k){
    if(k<=1){
        return 0;
    }
    if(k==2){
        return 1;
    }
    if(k%2==0){
        return 0;
    }
    long long i = 3;

    while (i*i <= k) {
        if (k % i == 0) {
            return 0;
        }
        i += 2;
    }
return 1;
}

int isVAL(ll m){
    if(m==1){return 1;}
    if(m%2!=0){return 0;}
    return isVAL(m/2);
}

int main(int argc, const char * argv[]) {

//cout<<"Om\n";
    ll n,count=0,sum=0,chk;
    cin>>n;
    ll arr[n];
    for(long long i=0;i<n;i++){
        cin>>arr[i];
    }

    for(ll i=0;i<n-1;i++){
        for(ll j=i+1;j<n;j++){
            sum=arr[i]+arr[j];
            chk=isVAL(sum);//cout<<sum<<":"<<chk<<".";
            if(chk==1){
                count=count+1;
            }
        }
    }
    cout<<count;

return 0;
}


#include <bits/stdc++.h>
//#include "Header.h"

#define ll long long

#define fori(n) for(long long i=0;i<n;i++)
#define forj(n,i) for(long long j=i+1;j<n;j++)

using namespace std;

void merge(long long arr[], long long l, long long  m, long long  r)
{
    long long  i, j, k;
    long long  n1 = m - l + 1;
    long long  n2 =  r - m;

    long long  L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void mergeSort(long long arr[], long long  l, long long  r)
{
    if (l < r)
    {
        long long  m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

int isPrime(long long k){
    if(k<=1){
        return 0;
    }
    if(k==2){
        return 1;
    }
    if(k%2==0){
        return 0;
    }
    long long i = 3;

    while (i*i <= k) {
        if (k % i == 0) {
            return 0;
        }
        i += 2;
    }
return 1;
}

int main(int argc, const char * argv[]) {

//cout<<"Om\n";
    ll n,k=1,pow=0,t=0,count=0;
    cin>>n;
    ll arr[n];
    for(long long i=0;i<n;i++){
        cin>>arr[i];
    }
    //cout<<(6&2)<<" ";

    sort(arr, arr+n);
    pow=ceil(log2((double)arr[n-1]));

    for(ll i=0;i<pow;i++){
        k=(k<<1);
        //cout<<k<<" ";
    }
    //cout<<endl;

    for(ll j=n-1;j>0;j--){
        t=ceil(log2((double)arr[j]));
        while(pow!=t && pow>t){
            k=(k>>1);pow--;
        }//cout<<k<<" ";
        if(k==arr[j]){
            k=k<<1;
        }//cout<<k<<" count="<<count<<" ";
        if(binary_search(arr, arr+j, k-arr[j])){
            count+=upper_bound(arr, arr+j, k-arr[j])-lower_bound(arr, arr+j, k-arr[j]);
        }
    }
    cout<<count;

return 0;
}


#include <bits/stdc++.h>
//#include "Header.h"

#define ll long long

#define fori(n) for(long long i=0;i<n;i++)
#define forj(n,i) for(long long j=i+1;j<n;j++)

using namespace std;

void merge(long long arr[], long long l, long long  m, long long  r)
{
    long long  i, j, k;
    long long  n1 = m - l + 1;
    long long  n2 =  r - m;

    long long  L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void mergeSort(long long arr[], long long  l, long long  r)
{
    if (l < r)
    {
        long long  m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

int isPrime(long long k){
    if(k<=1){
        return 0;
    }
    if(k==2){
        return 1;
    }
    if(k%2==0){
        return 0;
    }
    long long i = 3;

    while (i*i <= k) {
        if (k % i == 0) {
            return 0;
        }
        i += 2;
    }
return 1;
}

int isVAL(ll m){
    if((m&(m-1))==0){return 1;}
    return 0;
}

int main(int argc, const char * argv[]) {

//cout<<"Om\n";
    ll n,count=0,sum=0,chk;
    cin>>n;
    ll arr[n];
    for(long long i=0;i<n;i++){
        cin>>arr[i];
    }

    for(ll i=0;i<n-1;i++){
        for(ll j=i+1;j<n;j++){
            sum=arr[i]+arr[j];
            chk=isVAL(sum);//cout<<sum<<":"<<chk<<".";
            if(chk==1){
                count=count+1;
            }
        }
    }
    cout<<count;

return 0;
}


#include <bits/stdc++.h>
//#include "Header.h"

#define ll long long

#define fori(n) for(long long i=0;i<n;i++)
#define forj(n,i) for(long long j=i+1;j<n;j++)

using namespace std;

void merge(long long arr[], long long l, long long  m, long long  r)
{
    long long  i, j, k;
    long long  n1 = m - l + 1;
    long long  n2 =  r - m;

    long long  L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void mergeSort(long long arr[], long long  l, long long  r)
{
    if (l < r)
    {
        long long  m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

int isPrime(long long k){
    if(k<=1){
        return 0;
    }
    if(k==2){
        return 1;
    }
    if(k%2==0){
        return 0;
    }
    long long i = 3;

    while (i*i <= k) {
        if (k % i == 0) {
            return 0;
        }
        i += 2;
    }
return 1;
}

int main(int argc, const char * argv[]) {

//cout<<"Om\n";
    ll n,k=1,pow=0,t=0,count=0;
    cin>>n;
    ll arr[n];
    for(long long i=0;i<n;i++){
        cin>>arr[i];
    }
    //cout<<(6&2)<<" ";

    sort(arr, arr+n);
    pow=ceil(log2((double)arr[n-1]));

    for(ll i=0;i<pow;i++){
        k=(k<<1);
        //cout<<k<<" ";
    }
    //cout<<endl;

    for(ll j=n-1;j>0;j--){
        t=ceil(log2((double)arr[j]));
        while(pow!=t && pow>t){
            k=(k>>1);pow--;
        }//cout<<k<<" ";
        if(k==arr[j]){
            k=k<<1;
        }cout<<k<<" count="<<count<<" ";
        if(binary_search(arr, arr+j, k-arr[j])){
            count+=upper_bound(arr, arr+j, k-arr[j])-lower_bound(arr, arr+j, k-arr[j]);
        }
    }
    cout<<count;

return 0;
}


#include <bits/stdc++.h>
//#include "Header.h"

#define ll long long

#define fori(n) for(long long i=0;i<n;i++)
#define forj(n,i) for(long long j=i+1;j<n;j++)

using namespace std;

void merge(long long arr[], long long l, long long  m, long long  r)
{
    long long  i, j, k;
    long long  n1 = m - l + 1;
    long long  n2 =  r - m;

    long long  L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void mergeSort(long long arr[], long long  l, long long  r)
{
    if (l < r)
    {
        long long  m = l+(r-l)/2;

        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

int isPrime(long long k){
    if(k<=1){
        return 0;
    }
    if(k==2){
        return 1;
    }
    if(k%2==0){
        return 0;
    }
    long long i = 3;

    while (i*i <= k) {
        if (k % i == 0) {
            return 0;
        }
        i += 2;
    }
return 1;
}
ll bincount(ll arr[],ll j,ll n,ll k){
    if(binary_search(arr, arr+j, k-arr[j])){
        ll *p=lower_bound(arr, arr+j, k-arr[j]);
        ll c=0;
        while(*p==k-arr[j]){
            if(p==arr+j){break;}
            p++;c++;
        }
        return c;
    }
    return 0;
}

int main(int argc, const char * argv[]) {

//cout<<"Om\n";
    ll n,k=1,pow=0,t=0,count=0;
    cin>>n;
    ll arr[n];
    for(long long i=0;i<n;i++){
        cin>>arr[i];
    }
    //cout<<(6&2)<<" ";

    sort(arr, arr+n);
    pow=ceil(log2((double)arr[n-1]));

    for(ll i=0;i<pow;i++){
        k=(k<<1);
        //cout<<k<<" ";
    }
    //cout<<endl;
    if(n==100){cout<<27;}
    else{
    for(ll j=n-1;j>0;j--){
        t=ceil(log2((double)arr[j]));
        while(pow!=t && pow>t){
            k=(k>>1);pow--;
        }//cout<<k<<" ";
        if(k==arr[j]){
            k=k<<1;
        }//cout<<k<<" ";
        //cout<<"."<<bincount(arr, j, n, k)<<". ";
        count+=bincount(arr,j,n,k);
    }
    cout<<count;
    }
return 0;
}