func_code_string: #include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int INF = 1e9;   struct Node {     int cnt;     int xy, yx;     int minx, miny;     int maxx, maxy;     Node() : cnt(0), xy(-INF), yx(-INF), minx(INF), miny(INF), maxx(0), maxy(0) {}     Node(int x, int y)         : cnt(1), xy(x - y), yx(y - x), minx(x), miny(y), maxx(x), maxy(y) {}   };   struct SegmentTree {     int n;     vector<Node> tree;     vector<int> toaddx;     SegmentTree(int n) : n(n), tree(4 * n), toaddx(4 * n) {}     Node combine(Node a, Node b) {       Node c;       c.cnt = a.cnt + b.cnt;       c.xy = max(a.xy, b.xy);       c.yx = max(a.yx, b.yx);       c.maxx = max(a.maxx, b.maxx);       c.maxy = max(a.maxy, b.maxy);       c.minx = min(a.minx, b.minx);       c.miny = min(a.miny, b.miny);       return c;     }     Node getval(int l, int r) { return getval(l, r, 1, 0, n - 1); }     Node getval(int l, int r, int tv, int tl, int tr) {       if (l <= tl && tr <= r) {         return tree[tv];       }       if (tr < l || r < tl) {         return Node();       }       int tm = (tl + tr) / 2;       return combine(getval(l, r, 2 * tv, tl, tm),                      getval(l, r, 2 * tv + 1, tm + 1, tr));     }     void change_cnt(int x, int y, int sign) {       change_cnt(x, y, sign, 1, 0, n - 1);     }     bool change_cnt(int x, int y, int sign, int tv, int tl, int tr) {       if (tl == tr) {         if (sign == +1) {           tree[tv] = combine(tree[tv], Node(x, y));           return true;         } else if (sign == -1 && --tree[tv].cnt == 0) {           tree[tv] = Node();           return true;         }         return false;       } else {         int tm = (tl + tr) / 2;         bool changed;         if (y <= tm) {           changed = change_cnt(x, y, sign, 2 * tv, tl, tm);         } else {           changed = change_cnt(x, y, sign, 2 * tv + 1, tm + 1, tr);         }         if (changed) {           tree[tv] = combine(tree[2 * tv], tree[2 * tv + 1]);         }         return changed;       }     }   };   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     while (cin >> n) {       vector<pair<int, int>> bn(n);       int miny = INF, maxy = -INF;       for (int i = 0; i < n; ++i) {         string s;         cin >> s;         for (auto c : s) {           if (c ==  B )             ++bn[i].first;           else             ++bn[i].second;         }         miny = min(miny, bn[i].second);         maxy = max(maxy, bn[i].second);       }       sort(bn.begin(), bn.end());       SegmentTree left(maxy + 1), right(maxy + 1);       for (auto [x, y] : bn) {         right.change_cnt(x, y, +1);       }       auto check = [&](int x, int y, bool return_true_answer) {         auto q1 = right.getval(y, maxy);         auto q2 = left.getval(y, maxy);         auto q3 = left.getval(miny, y);         auto q4 = right.getval(miny, y);         int q2res = q2.cnt ? x - y + q2.yx : 0;         int q4res = q4.cnt ? y - x + q4.xy : 0;         int q1res = q1.cnt ? max(q1.maxx - x, q1.maxy - y) : 0;         int q3res = q3.cnt ? max(x - q3.minx, y - q3.miny) : 0;         int top = max(q1res, q2res);         int bot = max(q3res, q4res);         if (return_true_answer) {           return max(bot, top);         }         int res = bot - top;         return res;       };       tuple<int, int, int> res(INF, -1, -1);       for (int x = bn[0].first, idx = 0; x <= bn[n - 1].first; ++x) {         while (idx < n && x == bn[idx].first) {           right.change_cnt(bn[idx].first, bn[idx].second, -1);           left.change_cnt(bn[idx].first, bn[idx].second, +1);           ++idx;         }         int L = (x == 0 ? max(miny, 1) : miny);         int R = maxy;         while (R - L > 1) {           int mid = (L + R) / 2;           int f = check(x, mid, false);           if (f <= 0) {             L = mid;           } else {             R = mid;           }         }         tuple<int, int, int> cur(check(x, L, true), x, L);         for (int i = L + 1; i <= R; ++i) {           cur = min(cur, {check(x, i, true), x, i});         }         res = min(res, cur);       }       auto [val, bb, nn] = res;       cout << val <<   n ;       cout << string(bb,  B ) << string(nn,  N ) <<   n ;     }   }   