prithivMLmods/Coder-Stat huggingface dataset:
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#define eps 0.0000000001
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

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

#define S second
#define F first
#define int long long

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

signed main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif
  string s;
  cin >> s;
  int ans = 0;
  int w = 0;
  int n = s.length();
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == 'W')
      ++w;
    else {
      ans += w;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#define eps 0.0000000001
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

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

#define S second
#define F first
#define int long long

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

signed main() {
  fastio;
  string s;
  cin >> s;
  int ans = 0;
  int w = 0;
  int n = s.length();
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == 'W')
      ++w;
    else {
      ans += w;
    }
  }
  cout << ans;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

int main() {
  char str[20001];
  char c;
  int size = 0;
  while (cin >> c) {
    str[size] = c;
    size++;
  }
  int black = 0, count = 0;

  for (int i = 0; i < size; i++) {
    if (str[i] == 'B')
      black++;
    else
      count += black;
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int main() {
  string str;
  cin >> str;
  int size = str.size();
  long black = 0, count = 0;

  for (int i = 0; i < size; i++) {
    if (str[i] == 'B')
      black++;
    else
      count += black;
  }

  cout << count << endl;

  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Time Limit Exceeded):
// In the name of God

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl;

int main() {
  IOS string s;
  cin >> s;
  int ans = 0;
  int wn = 0;
  for (int i = 0; s[i]; i++)
    if (s[i] == 'W')
      wn++;
  bool flag = true;
  while (flag) {
    for (int i = 0; i < s.length() - 1; i++) {
      if (s[i] == 'B' && s[i + 1] == 'W') {
        ans++;
        s[i] = 'W';
        s[i + 1] = 'B';
      }
    }
    flag = false;
    for (int i = 0; i < wn; i++) {
      if (s[i] == 'B') {
        flag = true;
        break;
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
// In the name of God

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl;

int main() {
  IOS string s;
  cin >> s;
  unsigned long long b = 0, ans = 0;
  for (int i = 0; s[i]; i++) {
    if (s[i] == 'B')
      b++;
    if (s[i] == 'W') {
      ans += b;
    }
  }
  cout << ans;
  return 0;
}
Problem p03200 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  long i, count = 0, ans = 0;
  char s[111111];
  for (i = 0; i < 111111; i++)
    s[i] = '\0';
  scanf("%s", s);
  i = 0;
  while (s[i] != '\0') {
    if (s[i] == 'B')
      count++;
    else
      ans += count;
    i++;
  }
  printf("%ld\n", ans);
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  long i, count = 0, ans = 0;
  char s[222222];
  for (i = 0; i < 222222; i++)
    s[i] = '\0';
  scanf("%s", s);
  i = 0;
  while (s[i] != '\0') {
    if (s[i] == 'B')
      count++;
    else
      ans += count;
    i++;
  }
  printf("%ld\n", ans);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
int n, a[MAX + 5], cnt;
map<int, int> rec;
int cmp(int a, int b) { return a > b; }
void run() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    rec[a[i]]++;
  }
  sort(a + 1, a + n + 1, cmp);
}
void solve() {
  for (int i = 1; i <= n; i++) {
    int target = (1 << 30);
    for (int j = 30; j >= 1; j--) {
      if (rec[target - a[i]] && rec[a[i]]) {
        if (target - a[i] == a[i] && rec[a[i]] == 1)
          continue;
        rec[target - a[i]]--;
        rec[a[i]]--;
        cnt++;
        break;
      }
      target /= 2;
    }
  }
}
int main() {
  run();
  solve();
  printf("%d", cnt);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
int n, a[MAX + 5], cnt;
map<int, int> rec;
int cmp(int a, int b) { return a > b; }
void run() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    rec[a[i]]++;
  }
  sort(a + 1, a + n + 1, cmp);
}
void solve() {
  for (int i = 1; i <= n; i++) {
    int target = 1073741824;
    for (int j = 30; j >= 1; j--) {
      if (rec[target - a[i]] && rec[a[i]]) {
        if (target - a[i] == a[i] && rec[a[i]] == 1)
          continue;
        rec[target - a[i]]--;
        rec[a[i]]--;
        cnt++;
        break;
      }
      target /= 2;
    }
  }
}
int main() {
  run();
  solve();
  printf("%d", cnt);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1E5 + 2;

int n;
vector<pair<long long, int>> a(MAXN);
vector<bool> b(MAXN, 0);
vector<pair<int, int>> v;

long long pairing(long long X) {
  int l = 0, r = 60, msb = 60;
  while (l <= r) {
    int mid = (l + r) >> 1;
    long long val = (1LL << mid);
    if (val > X)
      msb = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return (1LL << msb) - X;
}

int main() {
  // your code goes here
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin() + 1, a.begin() + n + 1);
  for (int i = n; i >= 1; --i) {
    if (b[i])
      continue;
    long long X = pairing(a[i].first);
    int l = 1, r = i - 1, j = -1;
    while (l <= r) {
      int mid = (l + r) >> 1;

      if (a[mid].first < X)
        l = mid + 1;
      else if (a[mid].first > X)
        r = mid - 1;
      else if (b[mid])
        l = mid + 1;
      else
        j = mid, r = mid - 1;
    }
    if (j != -1) {
      v.push_back({a[i].second, a[j].second});
      b[j] = 1;
    }
  }
  cout << v.size() << "\n";
  return 0;
}

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

const int MAXN = 2E5 + 2;

int n;
vector<pair<long long, int>> a(MAXN);
vector<bool> b(MAXN, 0);
vector<pair<int, int>> v;

long long pairing(long long X) {
  int l = 0, r = 60, msb = 60;
  while (l <= r) {
    int mid = (l + r) >> 1;
    long long val = (1LL << mid);
    if (val > X)
      msb = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return (1LL << msb) - X;
}

int main() {
  // your code goes here
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin() + 1, a.begin() + n + 1);
  for (int i = n; i >= 1; --i) {
    if (b[i])
      continue;
    long long X = pairing(a[i].first);
    int l = 1, r = i - 1, j = -1;
    while (l <= r) {
      int mid = (l + r) >> 1;

      if (a[mid].first < X)
        l = mid + 1;
      else if (a[mid].first > X)
        r = mid - 1;
      else if (b[mid])
        l = mid + 1;
      else
        j = mid, r = mid - 1;
    }
    if (j != -1) {
      v.push_back({a[i].second, a[j].second});
      b[j] = 1;
    }
  }
  cout << v.size() << "\n";
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long a[100];
long long num[300005];
set<long long> s;
set<int> ss;
int cmp(int a, int b) { return a > b; }
map<int, int> m;
int main() {
  //	long long a[100];
  set<int>::const_iterator iter;
  set<int>::iterator first;
  set<int>::iterator second;
  a[0] = 1;
  for (int i = 1; i <= 35; i++)
    a[i] = a[i - 1] * 2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &num[i]);
    m[num[i]]++;
  }
  sort(num, num + n);
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (m[num[i]]-- > 0)
      for (int j = 32; j >= 1; j--)
        if (m[a[j] - num[i]]-- > 0) {
          cnt++;
          //	printf("%d %d %d\n",a[j],num[i],a[j]-num[i]);
          //	break;
        }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long a[100];
long long num[300005];
set<long long> s;
set<int> ss;
int cmp(int a, int b) { return a > b; }
map<int, int> m;
int main() {
  //	long long a[100];
  set<int>::const_iterator iter;
  set<int>::iterator first;
  set<int>::iterator second;
  a[0] = 1;
  for (int i = 1; i <= 35; i++)
    a[i] = a[i - 1] * 2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &num[i]);
    m[num[i]]++;
  }
  sort(num, num + n);
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (m[num[i]]-- > 0)
      for (int j = 32; j >= 1; j--)
        if (m[a[j] - num[i]]-- > 0) {
          cnt++;
          //	printf("%d %d %d\n",a[j],num[i],a[j]-num[i]);
          //	break;
        } else if (a[j] <= num[i]) {
          break;
        }
  }
  cout << cnt << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  ll n;
  cin >> n;
  multiset<ll> se;
  rep(i, n) {
    ll a;
    cin >> a;
    se.insert(a);
  }
  ll ans = 0;
  multiset<ll> se2;
  for (ll bit = 35; bit >= 1; bit--) {
    ll b = 1LL << bit;
    se2 = se;
    for (auto i : se2) {
      auto itr = se.find(i);
      auto itr2 = se.find(b - i);
      if (itr == itr2)
        itr2++;
      if (itr2 != se.end() && *itr + *itr2 == b) {
        se.erase(itr);
        se.erase(itr2);
        ans++;
      }
      if (se.size() == 0 || se.size() == 1)
        break;
    }
    if (se.size() == 0 || se.size() == 1)
      break;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  ll n;
  cin >> n;
  multiset<ll> se;
  rep(i, n) {
    ll a;
    cin >> a;
    se.insert(a);
  }
  ll ans = 0;
  multiset<ll> se2;
  for (ll bit = 35; bit >= 1; bit--) {
    ll b = 1LL << bit;
    se2 = se;
    for (auto i : se2) {
      auto itr = se.find(i);
      if (itr == se.end())
        continue;
      auto itr2 = se.find(b - i);
      if (itr == itr2)
        itr2++;
      if (itr2 != se.end() && *itr + *itr2 == b) {
        se.erase(itr);
        se.erase(itr2);
        ans++;
      }
      if (se.size() == 0 || se.size() == 1)
        break;
    }
    if (se.size() == 0 || se.size() == 1)
      break;
  }
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define f(i, n) for (int i = 0; i < n; i++)
#define F first
#define S second
#define mod 1000000007
#define P pair<int, int>
using namespace std;
vector<int> v;
int j[50];
signed main() {
  int n, c, ans = 0;
  cin >> n;
  f(i, n) {
    cin >> c;
    v.push_back(c);
  }
  sort(v.begin(), v.end());
  j[0] = 1;
  for (int i = 1; i < 31; i++)
    j[i] = j[i - 1] * 2;
  for (int i = n - 1; i >= 1; i--) {
    f(k, 31) {
      if (j[k] > v[i]) {
        if (binary_search(v.begin(), v.begin() + i, j[k] - v[i])) {
          ans++;
          auto Itr = upper_bound(v.begin(), v.begin() + i, j[k] - v[i]) - 1;
          v.erase(v.begin() + (Itr - v.begin()));
          i--;
        }
        break;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < n; i++)
#define F first
#define S second
#define mod 1000000007
#define P pair<int, int>
using namespace std;
vector<int> v;
int j[50];
signed main() {
  int n, c, ans = 0;
  cin >> n;
  f(i, n) {
    cin >> c;
    v.push_back(c);
  }
  sort(v.begin(), v.end());
  j[0] = 1;
  for (int i = 1; i < 31; i++)
    j[i] = j[i - 1] * 2;
  for (int i = n - 1; i >= 1; i--) {
    f(k, 31) {
      if (j[k] > v[i]) {
        if (binary_search(v.begin(), v.begin() + i, j[k] - v[i])) {
          ans++;
          auto Itr = upper_bound(v.begin(), v.begin() + i, j[k] - v[i]) - 1;
          v.erase(v.begin() + (Itr - v.begin()));
          i--;
        }
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin()                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
using namespace std;
typedef pair<int, int> ii;

const int maxn = 100050;
int n, p[33], a[maxn];
bool vis[maxn];

map<int, vector<int>> b;

signed main() {
  for (int i = 0; i <= 32; i++)
    p[i] = (1 << i);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    b[a[i]].pb(i);
  int ans = 0;
  for (int i = n; i >= 1; i--) {
    if (vis[i])
      continue;
    b[a[i]].pop_back();
    for (int j = 32; j >= 0; j--) {
      if (p[j] > 2 * a[i] or p[j] <= a[i] or
          (!b.count(p[j] - a[i]) or b[p[j] - a[i]].empty()))
        continue;
      int id = *b[p[j] - a[i]].rbegin();
      b[p[j] - a[i]].pop_back();
      ans++;
      vis[id] = 1;
      break;
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin()                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
using namespace std;
typedef pair<int, int> ii;

const int maxn = 200050;
int n, p[33], a[maxn];
bool vis[maxn];

map<int, vector<int>> b;

signed main() {
  for (int i = 0; i <= 32; i++)
    p[i] = (1 << i);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    b[a[i]].pb(i);
  int ans = 0;
  for (int i = n; i >= 1; i--) {
    if (vis[i])
      continue;
    b[a[i]].pop_back();
    for (int j = 32; j >= 0; j--) {
      if (p[j] > 2 * a[i] or p[j] <= a[i] or
          (!b.count(p[j] - a[i]) or b[p[j] - a[i]].empty()))
        continue;
      int id = *b[p[j] - a[i]].rbegin();
      b[p[j] - a[i]].pop_back();
      ans++;
      vis[id] = 1;
      break;
    }
  }
  cout << ans;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<int, int> mp;
int a[201010];
int main() {
  int n, i, j;
  int ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++)
    mp[a[i]]++;
  for (i = n; i; i--) {
    //			printf("%d %d\n",i,mp[a[i]]);
    if (mp[a[i]] == 0)
      continue;
    for (j = 30; j >= 0; j--) {
      if ((mp[(1 << j) - a[i]] > 0 && (1 << j) != a[i] * 2) ||
          (mp[a[i]] > 1 && (1 << j) == a[i] * 2)) {
        //				printf("%d|\n",(1<<j));
        ans++, mp[a[i]]--, mp[(1 << j) - a[i]]--;
        break;
      }
    }
  }
  printf("%d\n", ans);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<int, int> mp;
int a[201010];
int main() {
  int n, i, j;
  int ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++)
    mp[a[i]]++;
  for (i = n; i; i--) {
    //			printf("%d %d\n",i,mp[a[i]]);
    if (mp[a[i]] == 0)
      continue;
    for (j = 30; j >= 0; j--) {
      if ((1 << j) - a[i] < 0)
        break;
      if (!mp.count((1 << j) - a[i]))
        continue;
      if ((mp[(1 << j) - a[i]] > 0 && (1 << j) != a[i] * 2) ||
          (mp[a[i]] > 1 && (1 << j) == a[i] * 2)) {
        //				printf("%d|\n",(1<<j));
        ans++, mp[a[i]]--, mp[(1 << j) - a[i]]--;
        break;
      }
    }
  }
  printf("%d\n", ans);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve(long long N, vector<long long> A) {
  map<long long, ll> mp;

  for (int i = 0; i < N; ++i) {
    mp[A[i]]++;
  }
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  long long res = 0;
  for (int k = 30; k > 0; --k) {
    long long t = 1LL << k;
    for (auto a : A) {
      ll b = t - a;
      if (a != b) {
        ll m = min(mp[a], mp[b]);
        mp[a] -= m;
        mp[b] -= m;
        res += m;
      } else {
        res += mp[a] / 2;
        mp[a] %= 2;
      }
    }
  }
  cout << res << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> A(N - 1 + 1);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, A);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve(long long N, vector<long long> A) {
  map<long long, ll> mp;

  for (int i = 0; i < N; ++i) {
    mp[A[i]]++;
  }
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  long long res = 0;
  for (int k = 30; k > 0; --k) {
    long long t = 1LL << k;
    for (int i = 0; i < A.size() && A[i] <= t / 2; ++i) {
      ll a = A[i];
      ll b = t - a;
      if (a != b) {
        ll m = min(mp[a], mp[b]);
        mp[a] -= m;
        mp[b] -= m;
        res += m;
      } else {
        res += mp[a] / 2;
        mp[a] %= 2;
      }
    }
  }
  cout << res << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> A(N - 1 + 1);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, A);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
map<int, int> mp;
int a[20010];
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    mp[a[i]]++;
  }
  while (!mp.empty()) {
    map<int, int>::iterator it = mp.end();
    it--;
    int x = it->first;
    if (mp[x] == 0)
      throw;
    mp[x]--;
    if (mp[x] == 0)
      mp.erase(x);
    int z = 1;
    while (z <= x)
      z <<= 1;
    if (mp.count(z - x) > 0) {
      mp[z - x]--;
      if (mp[z - x] == 0)
        mp.erase(z - x);
      ans++;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
map<int, int> mp;
int a[200010];
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    mp[a[i]]++;
  }
  while (!mp.empty()) {
    map<int, int>::iterator it = mp.end();
    it--;
    int x = it->first;
    if (mp[x] == 0)
      throw;
    mp[x]--;
    if (mp[x] == 0)
      mp.erase(x);
    int z = 1;
    while (z <= x)
      z <<= 1;
    if (mp.count(z - x) > 0) {
      mp[z - x]--;
      if (mp[z - x] == 0)
        mp.erase(z - x);
      ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int arr[100005];
map<int, int> cnt;
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    cnt[arr[i]]++;
  }
  sort(arr, arr + n);
  for (int i = n - 1; i >= 0; i--) {
    if (!cnt[arr[i]])
      continue;
    cnt[arr[i]]--;
    int pw = 1;
    while (pw <= arr[i])
      pw *= 2;
    if (cnt[pw - arr[i]]) {
      cnt[pw - arr[i]]--;
      ans++;
    }
  }
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int arr[200005];
map<int, int> cnt;
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    cnt[arr[i]]++;
  }
  sort(arr, arr + n);
  for (int i = n - 1; i >= 0; i--) {
    if (!cnt[arr[i]])
      continue;
    cnt[arr[i]]--;
    int pw = 1;
    while (pw <= arr[i])
      pw *= 2;
    if (cnt[pw - arr[i]]) {
      cnt[pw - arr[i]]--;
      ans++;
    }
  }
  printf("%d", ans);
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define MOD 1000000007
#define INF (1 << 30)
#define LINF (1LL << 60)
#define endl "\n"
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
typedef unsigned long long int ull;
typedef long long lint;

int main(void) {
  int pow2[31];
  pow2[0] = 1;
  rep(i, 30) pow2[i + 1] = pow2[i] * 2;
  int N, ans = 0;
  cin >> N;
  multiset<int> ball;
  rep(i, N) {
    int X;
    cin >> X;
    ball.insert(X);
  }
  while (ball.size() > 1) {
    auto itr = ball.end();
    itr--;
    bool check = false;
    rep(i, 30) {
      if (ball.count(pow2[i + 1] - *itr)) {
        auto itr2 = ball.find(pow2[i + 1] - *itr);
        ans++;
        ball.erase(itr);
        ball.erase(itr2);
        check = true;
        break;
      }
    }
    if (!check)
      ball.erase(itr);
  }
  cout << ans << endl;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define MOD 1000000007
#define INF (1 << 30)
#define LINF (1LL << 60)
#define endl "\n"
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
typedef unsigned long long int ull;
typedef long long lint;

int main(void) {
  int pow2[31];
  pow2[0] = 1;
  rep(i, 30) pow2[i + 1] = pow2[i] * 2;
  int N, ans = 0;
  cin >> N;
  multiset<int> ball;
  rep(i, N) {
    int X;
    cin >> X;
    ball.insert(X);
  }
  while (ball.size() > 1) {
    auto itr = ball.end();
    itr--;
    bool check = false;
    rep(i, 30) {
      if (ball.count(pow2[i + 1] - *itr)) {
        auto itr2 = ball.find(pow2[i + 1] - *itr);
        if (itr == itr2 && ball.count(pow2[i + 1] - *itr) == 1)
          continue;
        else if (itr == itr2)
          itr2++;
        ans++;
        ball.erase(itr);
        ball.erase(itr2);
        check = true;
        break;
      }
    }
    if (!check)
      ball.erase(itr);
  }
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#define mp make_pair
#define pii pair<int, int>
using namespace std;
set<pii> s;
int n;
int a[200010];
int res = 0;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  for (int i = 0; i < n; i++)
    s.insert(mp(a[i], i));
  for (int i = n - 1; i >= 0; i--) {
    if (s.count(mp(a[i], i))) {
      s.erase(mp(a[i], i));
      for (int j = 0; j < 31; j++) {
        if ((1 << j) > a[i]) {
          int t = (1 << j) - a[i];
          set<pii>::iterator tt = lower_bound(s.begin(), s.end(), mp(t, -1));
          if (tt != s.end() && tt->first == t) {
            res++;
            s.erase(tt);
          }
          break;
        }
      }
    }
  }
  printf("%d", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#define mp make_pair
#define pii pair<int, int>
using namespace std;
set<pii> s;
int n;
int a[200010];
int res = 0;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  for (int i = 0; i < n; i++)
    s.insert(mp(a[i], i));
  for (int i = n - 1; i >= 0; i--) {
    if (s.count(mp(a[i], i))) {
      s.erase(mp(a[i], i));
      for (int j = 0; j < 31; j++) {
        if ((1 << j) > a[i]) {
          int t = (1 << j) - a[i];
          set<pii>::iterator tt = s.lower_bound(mp(t, -1));
          if (tt != s.end() && tt->first == t) {
            res++;
            s.erase(tt);
          }
          break;
        }
      }
    }
  }
  printf("%d", res);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  map<ll, ll> mp;
  REP(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }
  sort(ALL(A), greater<ll>());
  ll ans = 0;
  vector<ll> num(33, 0);
  REP(i, 33) num[i] = pow(2, i);
  REP(i, N) {
    if (mp[A[i]] <= 0)
      continue;
    REP(j, 33) {
      if (mp[num[j] - A[i]] > 0) {
        if (!(mp[num[j] - A[i]] == 1 && num[j] - A[i] == A[i])) {
          mp[num[j] - A[i]]--;
          mp[A[i]]--;
          ans++;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  map<ll, ll> mp;
  REP(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }
  sort(ALL(A), greater<ll>());
  ll ans = 0;
  vector<ll> num(33, 0);
  REP(i, 33) num[i] = pow(2, i);
  REP(i, N) {
    if (mp[A[i]] <= 0)
      continue;
    REPD2(j, 32, -1) {
      if (num[j] <= A[i])
        break;
      if (mp[num[j] - A[i]] > 0) {
        if (!(mp[num[j] - A[i]] == 1 && num[j] - A[i] == A[i])) {
          mp[num[j] - A[i]]--;
          mp[A[i]]--;
          ans++;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i != (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

// sum to 2^30
const int maxn = 1e5;
int A[maxn];

map<int, int> cnt[31];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d", &A[i]);

  sort(A, A + n);

  ll ret = 0;
  REP(i, n) {
    int x = A[n - 1 - i];

    // 2^j >= x

    int y = -1;

    int j = 0;
    while ((1 << j) < x)
      j++;
    while (j < 31) {
      int ty = (1 << j) - x;
      if (cnt[j].find(ty) != cnt[j].end() && cnt[j][ty] > 0) {
        y = ty;
        break;
      }
      j++;
    }

    if (y >= 0)
      ret++;

    for (int k = 0; k < 31; k++) {
      if (y >= 0) {
        if (y < (1 << k))
          cnt[k][y]--;
      } else {
        // cerr << "add " << x << endl;
        if (x < (1 << k))
          cnt[k][x]++;
      }
    }
  }

  /* for (int k = 0; k < 31; k++) {
          cerr << k << ": ";
          for (auto x : cnt[k]) cerr << x.x << "->"<<x.y << " ";
          cerr << endl;
  } */

  cout << ret << endl;
  return 0;
}

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

#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i != (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

// sum to 2^30
const int maxn = 2e5 + 10;
int A[maxn];

map<int, int> cnt[31];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d", &A[i]);

  sort(A, A + n);

  ll ret = 0;
  REP(i, n) {
    int x = A[n - 1 - i];

    // 2^j >= x

    int y = -1;

    int j = 0;
    while ((1 << j) < x)
      j++;
    while (j < 31) {
      int ty = (1 << j) - x;
      if (cnt[j].find(ty) != cnt[j].end() && cnt[j][ty] > 0) {
        y = ty;
        break;
      }
      j++;
    }

    if (y >= 0)
      ret++;

    for (int k = 0; k < 31; k++) {
      if (y >= 0) {
        if (y < (1 << k))
          cnt[k][y]--;
      } else {
        // cerr << "add " << x << endl;
        if (x < (1 << k))
          cnt[k][x]++;
      }
    }
  }

  /* for (int k = 0; k < 31; k++) {
          cerr << k << ": ";
          for (auto x : cnt[k]) cerr << x.x << "->"<<x.y << " ";
          cerr << endl;
  } */

  cout << ret << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define FOR64(i, s, e) for (ll i = (s); i < (e); i++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(b, e) sort((b), (e))
#define REV(b, e) reverse((b), (e))
#define VSORT(v) sort((v).begin(), (v).end())
#define VREV(v) reverse((v).begin(), (v).end())
#define pb(a) push_back(a)
#define INF 999999999
#define P(x) printf("%d\n", (x));

typedef long long ll;

int main() {
  int N;

  cin >> N;

  vector<int> A(N);
  map<int, int> M;

  for (int &a : A) {
    cin >> a;
    M[a]++;
  }

  VSORT(A);

  int ans = 0;
  for (int j = N - 1; j >= 0; j--) {
    int a = A[j];
    if (M[a]) {
      M[a]--;
      //			for(int k = 30; (1L << k) > a; k--){
      for (int k = 30; k >= 0; k--) {
        if (M[(1L << k) - a]) {
          M[(1L << k) - a]--;
          ans++;
          break;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define FOR64(i, s, e) for (ll i = (s); i < (e); i++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(b, e) sort((b), (e))
#define REV(b, e) reverse((b), (e))
#define VSORT(v) sort((v).begin(), (v).end())
#define VREV(v) reverse((v).begin(), (v).end())
#define pb(a) push_back(a)
#define INF 999999999
#define P(x) printf("%d\n", (x));

typedef long long ll;

int main() {
  int N;

  cin >> N;

  vector<int> A(N);
  map<int, int> M;

  for (int &a : A) {
    cin >> a;
    M[a]++;
  }

  VSORT(A);

  int ans = 0;
  for (int j = N - 1; j >= 0; j--) {
    int a = A[j];
    if (M[a]) {
      M[a]--;
      for (int k = 30; (1L << k) > a; k--) {
        if (M[(1L << k) - a]) {
          M[(1L << k) - a]--;
          ans++;
          break;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int digit(int n) {
  int count = 0;
  while (n > 0) {
    n /= 2;
    count++;
  }
  return count;
}
int main() {
  int N;
  cin >> N;
  int A[200010] = {};
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A, A + N, greater<int>());
  int D[200010] = {};
  for (int i = 0; i < N; i++) {
    D[i] = digit(A[i]);
  }
  int B[200010] = {};
  for (int i = 0; i < N; i++) {
    B[i] = (1 << D[i]) - A[i];
  }
  int count = 0;
  int r = N - 1;
  for (int i = 0; i < N; i++) {
    for (int j = r; j >= 0; j--) {
      if ((B[i] == A[j] && B[i] > 0) && i != j) {
        count++;
        A[i] = -1, A[j] = -1;
        B[i] = -1;
        B[j] = -1;
        r = j - 1;
        break;
      }
      if (B[i] > 0 && A[j] > 0 && B[i] < A[j]) {
        r = min(j + 1, N - 1);
        break;
      }
    }
    if ((i != (N - 1)) && D[i] != D[i + 1]) {
      r = N - 1;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int digit(int n) {
  int count = 0;
  while (n > 0) {
    n /= 2;
    count++;
  }
  return count;
}
int main() {
  int N;
  cin >> N;
  int A[200010] = {};
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A, A + N, greater<int>());
  int D[200010] = {};
  for (int i = 0; i < N; i++) {
    D[i] = digit(A[i]);
  }
  int B[200010] = {};
  for (int i = 0; i < N; i++) {
    B[i] = (1 << D[i]) - A[i];
  }
  int count = 0;
  int r = N - 1;
  for (int i = 0; i < N; i++) {
    for (int j = r; j >= 0; j--) {
      if (B[i] == -1) {
        break;
      }
      if ((B[i] == A[j] && B[i] > 0) && i != j) {
        count++;
        A[i] = -1, A[j] = -1;
        B[i] = -1;
        B[j] = -1;
        r = j - 1;
        break;
      }
      if (B[i] > 0 && A[j] > 0 && B[i] < A[j]) {
        r = min(j + 1, N - 1);
        break;
      }
    }
    if ((i != (N - 1)) && D[i] != D[i + 1]) {
      r = N - 1;
    }
  }
  cout << count << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.sync_with_stdio(0);

  int n;
  cin >> n;
  vector<int> a(n);
  map<long, int> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]]++;
  }
  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    for (long j = 1; j < 1L << 31; j *= 2) {
      if (m.count(j - a[i]) == 0)
        continue;
      c[i] += m[j - a[i]];
      if (a[i] == j - a[i])
        c[i]--;
    }
  }

  vector<pair<int, int>> b;
  for (int i = 0; i < n; ++i)
    b.emplace_back(c[i], i);
  sort(begin(b), end(b));
  map<int, int> v;
  for (auto &e : b)
    v[a[e.second]] = e.first;

  int res = 0;
  for (auto &e : b) {
    int i = e.second;
    if (m[a[i]] == 0)
      continue;
    m[a[i]]--;

    int k = -1, x = INT_MAX;
    for (long j = 1; j < 1L << 31; j *= 2) {
      if (m.count(j - a[i]) == 0)
        continue;
      if (m[j - a[i]]) {
        if (k < 0 || x > v[j - a[j]])
          k = j - a[j], x = v[k];
      }
    }
    if (k >= 0) {
      m[k]--;
      ++res;
    }
  }
  cout << res << endl;
}


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

int main() {
  cin.sync_with_stdio(0);

  int n;
  cin >> n;
  vector<int> a(n);
  map<long, int> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]]++;
  }
  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    for (long j = 1; j < 1L << 31; j *= 2) {
      if (m.count(j - a[i]) == 0)
        continue;
      c[i] += m[j - a[i]];
      if (a[i] == j - a[i])
        c[i]--;
    }
  }

  vector<pair<int, int>> b;
  for (int i = 0; i < n; ++i)
    b.emplace_back(c[i], i);
  sort(begin(b), end(b));
  map<int, int> v;
  for (auto &e : b)
    v[a[e.second]] = e.first;

  int res = 0;
  for (auto &e : b) {
    int i = e.second;
    if (m[a[i]] == 0)
      continue;
    m[a[i]]--;

    int k = -1, x = INT_MAX;
    for (long j = 1; j < 1L << 31; j *= 2) {
      if (m.count(j - a[i]) == 0)
        continue;
      if (m[j - a[i]]) {
        if (k < 0 || x > v[j - a[i]])
          k = j - a[i], x = v[k];
      }
    }
    if (k >= 0) {
      m[k]--;
      ++res;
    }
  }
  cout << res << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  int N;
  cin >> N;
  multiset<ll> A;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A.insert(a);
  }

  int ans = 0;
  while (!A.empty()) {
    auto it = A.end();
    it--;
    ll t = 1;
    while (t <= (*it))
      t *= 2;
    auto it2 = A.find(t - (*it));
    if (it2 != it && (*it) + (*it2) == t) {
      ans++;
      A.erase(it2);
      it = A.end();
      it--;
      A.erase(it);
    } else {
      A.erase(it);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  multiset<ll> A;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A.insert(a);
  }

  int ans = 0;
  while (!A.empty()) {
    auto it = A.end();
    it--;
    ll t = 1;
    while (t <= (*it))
      t *= 2;
    auto it2 = A.find(t - (*it));
    if (it2 != it && it2 != A.end() && (*it) + (*it2) == t) {
      ans++;
      A.erase(it2);
      it = A.end();
      it--;
      A.erase(it);
    } else {
      A.erase(it);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod2 = 1000000007;
constexpr ll mod = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), greater<ll>());
  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    mp[A[i]]++;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (mp[A[i]] == 0)
      continue;
    mp[A[i]]--;
    for (int j = 40; j >= 0; j--) {
      if (!mp.count((1LL << j) - A[i]))
        continue;
      if (mp[(1LL << j) - A[i]] > 0) {
        ans++;
        mp[(1LL << j) - A[i]]--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod2 = 1000000007;
constexpr ll mod = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end(), greater<ll>());
  unordered_map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    mp[A[i]]++;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (mp[A[i]] == 0)
      continue;
    mp[A[i]]--;
    for (int j = 40; j >= 0; j--) {
      if (!mp.count((1LL << j) - A[i]))
        continue;
      if (mp[(1LL << j) - A[i]] > 0) {
        ans++;
        mp[(1LL << j) - A[i]]--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 200005;
typedef long long ll;
multiset<ll> q;
int n;
ll ans;
ll a[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    q.insert(a[i]);
  }
  while (!q.empty()) {
    ll v = *--q.end();
    q.erase(q.end());
    for (int i = 0; i <= 32; i++) {
      ll t = (1 << i) - v;
      if (q.find(t) != q.end()) {
        ans++;
        q.erase(q.find(t));
        break;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 200005;
typedef long long ll;
multiset<ll> q;
int n;
ll ans;
ll a[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    q.insert(a[i]);
  }
  while (!q.empty()) {
    ll v = *--q.end();
    q.erase(--q.end());
    for (int i = 0; i <= 32; i++) {
      ll t = (1 << i) - v;
      if (q.find(t) != q.end()) {
        ans++;
        q.erase(q.find(t));
        break;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;

ll INF = (1ll << 60);

int main() {
  ll N;
  cin >> N;

  vi A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }

  priority_queue<pii> pq;
  for (ll i = 0; i < N; i++) {
    pq.push({A[i], i});
  }

  unordered_map<ll, unordered_set<ll>> m;
  for (ll i = 0; i < N; i++) {
    m[A[i]].insert(i);
  }

  ll ans = 0;

  while (pq.size()) {
    ll i = pq.top().second;
    ll j = pq.top().first;
    pq.pop();

    if (m[j].find(i) == m[j].end()) {
      continue;
    }

    m[j].erase(find(m[j].begin(), m[j].end(), i));

    ll x = [&]() {
      for (ll i = 1;; i *= 2) {
        if (i > j) {
          return i;
        }
      }
    }();

    if (!m[x - j].size())
      continue;

    ans += 1;
    m[x - j].erase(m[x - j].begin());
  }

  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;

ll INF = (1ll << 60);

int main() {
  ll N;
  cin >> N;

  vi A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }

  priority_queue<pii> pq;
  for (ll i = 0; i < N; i++) {
    pq.push({A[i], i});
  }

  unordered_map<ll, unordered_set<ll>> m;
  for (ll i = 0; i < N; i++) {
    m[A[i]].insert(i);
  }

  ll ans = 0;

  while (pq.size()) {
    ll i = pq.top().second;
    ll j = pq.top().first;
    pq.pop();

    if (m[j].find(i) == m[j].end()) {
      continue;
    }

    m[j].erase(i);

    ll x = [&]() {
      for (ll i = 1;; i *= 2) {
        if (i > j) {
          return i;
        }
      }
    }();

    if (!m[x - j].size())
      continue;

    ans += 1;
    m[x - j].erase(m[x - j].begin());
  }

  cout << ans << endl;
}