Problem p00540 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n, m, a, b, c[100000], D[100000], E[100000];
map<vector<int>, tuple<vector<int>, vector<int>, vector<int>>> M;
void make(int n) {
  queue<vector<int>> Q;
  for (int i = 1; i <= n; i++) {
    vector<int> ichi;
    ichi.push_back(i);
    Q.push(ichi);
  }
  while (Q.size() > 1) {
    vector<int> b1 = Q.front();
    Q.pop();
    vector<int> b2 = Q.front();
    Q.pop();
    vector<int> b3 = Q.front();
    Q.pop();
    vector<int> sum;
    for (int i = 0; i < b1.size(); i++)
      sum.push_back(b1[i]);
    for (int i = 0; i < b2.size(); i++)
      sum.push_back(b2[i]);
    for (int i = 0; i < b3.size(); i++)
      sum.push_back(b3[i]);
    sort(sum.begin(), sum.end());
    M[sum] = make_tuple(b1, b2, b3);
    Q.push(sum);
  }
  Q.pop();
}
int solve(vector<int> vec, int border) {
  if (vec.size() == 1) {
    if (c[vec[0]] < border && c[vec[0]] >= 1)
      return 10000000;
    if (c[vec[0]] >= border && c[vec[0]] >= 1)
      return 0;
    return 1;
  }
  int s[3] = {solve(get<0>(M[vec]), border), solve(get<1>(M[vec]), border),
              solve(get<2>(M[vec]), border)};
  sort(s, s + 3);
  return s[0] + s[1];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    c[b] = a;
    D[i] = a;
  }
  for (int i = m; i < n; i++) {
    cin >> a;
    D[i] = a;
    E[i - m] = a;
  }
  sort(D, D + n);
  sort(E, E + n - m);
  make(n);
  int maxn = 0;
  vector<int> Ball;
  for (int i = 1; i <= n; i++)
    Ball.push_back(i);
  for (int i = 0; i < n; i++) {
    int F = solve(Ball, D[i]);
    int pos1 = lower_bound(E, E + n - m, D[i] - 1) - E;
    pos1 = (n - m) - pos1;
    if (F <= pos1)
      maxn = max(maxn, D[i]);
  }
  cout << maxn << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n, m, a, b, c[100000], D[100000], E[100000];
map<vector<int>, tuple<vector<int>, vector<int>, vector<int>>> M;
void make(int n) {
  queue<vector<int>> Q;
  for (int i = 1; i <= n; i++) {
    vector<int> ichi;
    ichi.push_back(i);
    Q.push(ichi);
  }
  while (Q.size() > 1) {
    vector<int> b1 = Q.front();
    Q.pop();
    vector<int> b2 = Q.front();
    Q.pop();
    vector<int> b3 = Q.front();
    Q.pop();
    vector<int> sum;
    for (int i = 0; i < b1.size(); i++)
      sum.push_back(b1[i]);
    for (int i = 0; i < b2.size(); i++)
      sum.push_back(b2[i]);
    for (int i = 0; i < b3.size(); i++)
      sum.push_back(b3[i]);
    sort(sum.begin(), sum.end());
    M[sum] = make_tuple(b1, b2, b3);
    Q.push(sum);
  }
  Q.pop();
}
int solve(vector<int> vec, int border) {
  if (vec.size() == 1) {
    if (c[vec[0]] < border && c[vec[0]] >= 1)
      return 10000000;
    if (c[vec[0]] >= border && c[vec[0]] >= 1)
      return 0;
    return 1;
  }
  int s[3] = {solve(get<0>(M[vec]), border), solve(get<1>(M[vec]), border),
              solve(get<2>(M[vec]), border)};
  sort(s, s + 3);
  return s[0] + s[1];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    c[b] = a;
    D[i] = a;
  }
  for (int i = m; i < n; i++) {
    cin >> a;
    D[i] = a;
    E[i - m] = a;
  }
  sort(D, D + n);
  sort(E, E + n - m);
  make(n);
  int maxn = 0;
  vector<int> Ball;
  for (int i = 1; i <= n; i++)
    Ball.push_back(i);

  //--------------------------------------------Binary
  //Search------------------------------------
  int L = 0, R = n, M;
  while (true) {
    bool Y1 = false, Y2 = false;
    M = (L + R) / 2;
    if (M < n) {
      int F = solve(Ball, D[M]);
      int pos1 = lower_bound(E, E + n - m, D[M] - 1) - E;
      pos1 = (n - m) - pos1;
      if (F <= pos1)
        Y1 = true;
    }
    if (M < n - 1) {
      int F = solve(Ball, D[M + 1]);
      int pos2 = lower_bound(E, E + n - m, D[M + 1] - 1) - E;
      pos2 = (n - m) - pos2;
      if (F <= pos2)
        Y2 = true;
    }
    if (Y1 == true && Y2 == false) {
      maxn = D[M];
      break;
    }
    if (Y1 == false) {
      R = M;
    }
    if (Y2 == true) {
      L = M;
    }
  }
  cout << maxn << endl;
  return 0;
}