Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
using namespace std;

const int N = 2e5 + 5;

const int M = N * 30 * 6;

int S, T;
int final[M], next[M], to[M], r[M], tot = 1;
void link(int x, int y, int z) {
    next[++ tot] = final[x], to[tot] = y, r[tot] = z, final[x] = tot;
    next[++ tot] = final[y], to[tot] = x, r[tot] = 0, final[y] = tot;
}
int cur[N], d[N], co[N];
int dg(int x, int flow) {
    if(x == T) return flow;
    int use = 0;
    for(int i = cur[x]; i; i = next[i], cur[x] = i) {
        int y = to[i];
        if(d[y] + 1 == d[x] && r[i]) {
            int tmp = dg(y, min(flow - use, r[i]));
            r[i] -= tmp, r[i ^ 1] += tmp, use += tmp;
            if(use == flow) return use;
        }
    }
    cur[x] = final[x];
    if(!(-- co[d[x]])) d[S] = T;
    ++ co[++ d[x]];
    return use;
}

int n, a[N], b[N], bz[N];

struct nod {
	int fi[M / 3], nt[M / 3], to[M / 3], tot;
	void link(int x, int y) {
		nt[++ tot] = fi[x], to[tot] = y, fi[x] = tot;
	}
} e;

int c[N];

void dg(int x) {
	for(int i = e.fi[x]; i; i = e.nt[i]) {
		int y = e.to[i];
		if(!c[y]) c[y] = 3 - c[x], dg(y);
	}
}

const int inf = 1 << 30;

int main() {
//	freopen("a.in", "r", stdin);
	scanf("%d", &n);
	fo(i, 1, n) scanf("%d", &a[i]);
	sort(a + 1, a + n + 1);
	int n0 = n; n = 0;
	fo(i, 1, n0) {
		if(i == 1 || a[i] != a[i - 1])
			a[++ n] = a[i], b[n] = 1; else b[n] ++;
	}
    for(int w = 2; w <= 1e9; w *= 2) {
    	int j = n;
    	fo(i, 1, n) {
    		while(a[i] + a[j] > w && j > 0) j --;
    		if(!j) break;
    		if(a[i] + a[j] == w) {
	    		if(a[i] != a[j]) {
	    			e.link(i, j); e.link(j, i);
				} else bz[i] = 1;
			}
		}
	}
	fo(i, 1, n) if(!c[i]) c[i] = 1, dg(i);
	S = n + 1; T = n + 2;
	fo(i, 1, n) {
		if(c[i] == 1) link(S, i, b[i]); else link(i, T, b[i]);
		if(c[i] == 1) for(int j = e.fi[i]; j; j = e.nt[j]) link(i, e.to[j], inf);
	}
	co[0] = n + 2;
	int ans = 0;
	while(d[S] < T) ans += dg(S, inf);
	fo(i, 1, n) if(bz[i]) {
		for(int j = final[i]; j; j = next[j]) {
			if(c[i] == 1) {
				if(to[j] == S) ans += r[j ^ 1] / 2;
			} else {
				if(to[j] == T) ans += r[j] / 2;
			}
		}
	}
	printf("%d", ans);
}

Compiler output:
a.cc: In function 'void link(int, int, int)':
a.cc:14:5: error: reference to 'next' is ambiguous
   14 |     next[++ tot] = final[x], to[tot] = y, r[tot] = z, final[x] = tot;
      |     ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:15: note:                 'int next [36000900]'
   12 | int final[M], next[M], to[M], r[M], tot = 1;
      |               ^~~~
a.cc:15:5: error: reference to 'next' is ambiguous
   15 |     next[++ tot] = final[y], to[tot] = x, r[tot] = 0, final[y] = tot;
      |     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:15: note:                 'int next [36000900]'
   12 | int final[M], next[M], to[M], r[M], tot = 1;
      |               ^~~~
a.cc: In function 'int dg(int, int)':
a.cc:21:32: error: reference to 'next' is ambiguous
   21 |     for(int i = cur[x]; i; i = next[i], cur[x] = i) {
      |                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:15: note:                 'int next [36000900]'
   12 | int final[M], next[M], to[M], r[M], tot = 1;
      |               ^~~~
a.cc: In function 'int main()':
a.cc:87:46: error: reference to 'next' is ambiguous
   87 |                 for(int j = final[i]; j; j = next[j]) {
      |                                              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:15: note:                 'int next [36000900]'
   12 | int final[M], next[M], to[M], r[M], tot = 1;
      |               ^~~~