Problem:
Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2

Solution in Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"fmt"
	"math"
	"os"
	"sort"
)

//解説動画を見て解きなおす
//大きい順にソートして、大きい数と2^tを作る数がないか調べる
//あったら、それをpairsに追加して、大きい数とその数を消す。
//空になるまで続ける。
func main() {

	var N int
	fmt.Scanf("%d", &N)
	nums := scanNums(N)

	//大きい順に並べる
	sort.Sort(sort.Reverse(sort.IntSlice(nums)))

	maxIndex := 0
	//値からスライスのindexへアクセスするためのmap
	numIndex := make(map[int]int)
	for idx, num := range nums {
		numIndex[num] = idx
	}

	pairCount := 0
	for maxIndex < len(nums) {
		debug("nums = %v\n", nums)
		num0 := nums[maxIndex]
		if num0 < 0 { //すでにペアになって印がついている
			maxIndex++
			continue
		}
		pow2 := ceilPow2(num0)
		num1 := pow2 - num0
		debug("nums0=%d pow2=%d num1=%d\n", num0, pow2, num1)
		if numIndex, ok := numIndex[num1]; ok {
			if nums[numIndex] > 0 {
				pairCount++
				nums[numIndex] = -1 //一度ペアになったら印をつける
			}
		}
		nums[maxIndex] = -1//念のためmaxIndexのところに-1を付けておく
		maxIndex++
	}

	fmt.Printf("%d\n", pairCount)
}

/**
 * 引数で与えられた数以上の2の累乗数を求める
 */
func ceilPow2(n int) int {
	t := math.Log2(float64(n))
	pow2 := 1 << uint64(t)
	if n == pow2 {
		return n
	} else {
		return pow2 << 1
	}
}

/**
 * １行に空白区切りで数字を読み込み
 */
func scanNums(len int) (nums []int) {
	nums = make([]int, len)
	for i := 0; i < len; i++ {
		fmt.Scan(&nums[i])
	}
	return
}

//** DEBUG **/
const DEBUG_ENABLE = false

func debug(format string, a ...interface{}) (n int, err error) {
	if DEBUG_ENABLE {
		return fmt.Fprintf(os.Stdout, format, a...)
	} else {
		return 0, nil
	}
}