description: On an endless checkered sheet of paper, n cells are chosen and colored in three colors, where n is divisible by 3. It turns out that there are exactly n/3 marked cells of each of three colors! 
  
  Find the largest such k that it's possible to choose k/3 cells of each color, remove all other marked cells, and then select three rectangles with sides parallel to the grid lines so that the following conditions hold:
  
    * No two rectangles can intersect (but they can share a part of the boundary). In other words, the area of intersection of any two of these rectangles must be 0.
    * The i-th rectangle contains all the chosen cells of the i-th color and no chosen cells of other colors, for i = 1, 2, 3. 
  
  Input
  
  The first line of the input contains a single integer n — the number of the marked cells (3 ≤ n ≤ 10^5, n is divisible by 3).
  
  The i-th of the following n lines contains three integers x_i, y_i, c_i (|x_i|,|y_i| ≤ 10^9; 1 ≤ c_i ≤ 3), where (x_i, y_i) are the coordinates of the i-th marked cell and c_i is its color.
  
  It's guaranteed that all cells (x_i, y_i) in the input are distinct, and that there are exactly n/3 cells of each color.
  
  Output
  
  Output a single integer k — the largest number of cells you can leave.
  
  Examples
  
  Input
  
  
  9
  2 3 1
  4 1 2
  2 1 3
  3 4 1
  5 3 2
  4 4 3
  2 4 1
  5 2 2
  3 5 3
  
  
  Output
  
  
  6
  
  
  Input
  
  
  3
  1 1 1
  2 2 2
  3 3 3
  
  
  Output
  
  
  3
  
  Note
  
  In the first sample, it's possible to leave 6 cells with indexes 1, 5, 6, 7, 8, 9.
  
  In the second sample, it's possible to leave 3 cells with indexes 1, 2, 3.
solution: #include <bits/stdc++.h>
  using namespace std;
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  const long long max_n = 100005;
  long long ptr[3];
  pair<long long, long long> points[3][max_n], test_points[3][max_n];
  long long x[3][max_n], y[3][max_n];
  long long n;
  bool check_config(long long cnt, bool flip_x, bool flip_y, bool flip_xy) {
    for (long long i = 1; i <= n / 3; i++) {
      for (long long c = 0; c < 3; c++) test_points[c][i] = points[c][i];
    }
    for (long long i = 1; i <= n / 3; i++) {
      for (long long c = 0; c < 3; c++) {
        if (flip_xy) swap(test_points[c][i].first, test_points[c][i].second);
        if (flip_x) test_points[c][i].first = -test_points[c][i].first;
        if (flip_y) test_points[c][i].second = -test_points[c][i].second;
      }
    }
    for (long long i = 1; i <= n / 3; i++) {
      for (long long c = 0; c < 3; c++) x[c][i] = test_points[c][i].first;
    }
    vector<long long> color_order = {0, 1, 2};
    do {
      nth_element(x[color_order[0]] + 1, x[color_order[0]] + cnt,
                  x[color_order[0]] + n / 3 + 1);
      long long bound_0 = x[color_order[0]][cnt];
      long long lesser_1 = 0;
      for (long long i = 1; i <= n / 3; i++) {
        if (x[color_order[1]][i] <= bound_0) lesser_1++;
      }
      if (lesser_1 + cnt > n / 3) continue;
      nth_element(x[color_order[1]] + 1, x[color_order[1]] + lesser_1 + cnt,
                  x[color_order[1]] + n / 3 + 1);
      long long bound_1 = x[color_order[1]][lesser_1 + cnt];
      long long lesser_2 = 0;
      for (long long i = 1; i <= n / 3; i++) {
        if (x[color_order[2]][i] <= bound_1) lesser_2++;
      }
      if (lesser_2 + cnt <= n / 3) return 1;
    } while (next_permutation(color_order.begin(), color_order.end()));
    color_order = {0, 1, 2};
    do {
      nth_element(x[color_order[0]] + 1, x[color_order[0]] + cnt,
                  x[color_order[0]] + n / 3 + 1);
      long long bound_0 = x[color_order[0]][cnt];
      long long lesser_1 = 0;
      for (long long i = 1; i <= n / 3; i++) {
        if (x[color_order[1]][i] <= bound_0) lesser_1++;
      }
      if (lesser_1 + cnt > n / 3) continue;
      long long cnt_greater = 0;
      for (long long i = 1; i <= n / 3; i++) {
        if (test_points[color_order[1]][i].first > bound_0) {
          y[color_order[1]][cnt_greater + 1] =
              test_points[color_order[1]][i].second;
          cnt_greater++;
        }
      }
      nth_element(y[color_order[1]] + 1, y[color_order[1]] + cnt,
                  y[color_order[1]] + cnt_greater + 1);
      long long bound_1 = y[color_order[1]][cnt];
      long long sat = 0;
      for (long long i = 1; i <= n / 3; i++) {
        if (test_points[color_order[2]][i].first > bound_0 &&
            test_points[color_order[2]][i].second > bound_1)
          sat++;
      }
      if (sat >= cnt) return 1;
    } while (next_permutation(color_order.begin(), color_order.end()));
    return 0;
  }
  bool check(long long cnt) {
    for (long long flip_x = 0; flip_x <= 1; flip_x++) {
      for (long long flip_y = 0; flip_y <= 1; flip_y++) {
        for (long long flip_xy = 0; flip_xy <= 1; flip_xy++) {
          if (check_config(cnt, flip_x, flip_y, flip_xy)) {
            return 1;
          }
        }
      }
    }
    return 0;
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      long long x_i, y_i, c_i;
      cin >> x_i >> y_i >> c_i;
      points[c_i - 1][ptr[c_i - 1] + 1] = make_pair(x_i, y_i);
      ptr[c_i - 1]++;
    }
    long long l = 1, r = n / 3;
    while (l < r) {
      long long mid = (l + r + 1) / 2;
      if (check(mid))
        l = mid;
      else
        r = mid - 1;
    }
    cout << 3 * l << "\n";
  }
  