Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int mod = 1000000007;

int main() {
  int n;
  int a[101010] = {};
  map<int, int> mp;
  cin >> n;
  FOR(i, 0, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a, a + n);
  int cnt = 0;
  FORD(i, n, 0) {
    FORD(j, 31, 0) {
      int sum = 1 << j;
      int rest = sum - a[i];
      if (rest <= 0)
        continue;
      if (mp[a[i]] > 0 && mp[rest] > 0) {
        if (rest == a[i]) {
          if (mp[rest] >= 2) {
            mp[rest] -= 2;
            cnt++;
            break;
          }
        } else {
          mp[a[i]]--;
          mp[rest]--;
          cnt++;
          break;
        }
      }
    }
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int mod = 1000000007;

int main() {
  int n;
  int a[201010] = {};
  map<int, int> mp;
  cin >> n;
  FOR(i, 0, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a, a + n);
  int cnt = 0;
  FORD(i, n, 0) {
    FORD(j, 31, 0) {
      int sum = 1 << j;
      int rest = sum - a[i];
      if (rest <= 0)
        continue;
      if (mp[a[i]] > 0 && mp[rest] > 0) {
        if (rest == a[i]) {
          if (mp[rest] >= 2) {
            mp[rest] -= 2;
            cnt++;
            break;
          }
        } else {
          mp[a[i]]--;
          mp[rest]--;
          cnt++;
          break;
        }
      }
    }
  }
  cout << cnt << endl;

  return 0;
}