malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int a, b, c, d;       cin >> a >> b >> c >> d;       if (a > b) {         if (a == c) {           if (b + d == a) {             cout <<  Yes n ;           } else {             cout <<  NO n ;           }         } else if (a == d) {           if (c + b == a) {             cout <<  Yes n ;           } else {             cout <<  No n ;           }         } else {           cout <<  No n ;         }       } else if (b > a) {         if (b == c) {           if (a + d == b) {             cout <<  Yes n ;           } else {             cout <<  No n ;           }         } else if (b == d) {           if (c + a == b) {             cout <<  Yes n ;           } else {             cout <<  No n ;           }         } else {           cout <<  No n ;         }       } else {         cout <<  No n ;       }     }   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const int MAX_N = 200004;   char s[MAX_N], t[MAX_N];   int l[MAX_N], r[MAX_N];   vector<int> p[40];   int read() {     int x;     scanf( %d , &x);     return x;   }   int num(char x) { return x -  a ; }   bool find(int x, int left, int right) {     if (left > right) return false;     int sz = int(p[x].size());     int l = 0, r = sz - 1;     while (l <= r) {       int mid = (l + r) / 2, t = p[x][mid];       if (t < left)         l = mid + 1;       else if (t > right)         r = mid - 1;       else         return true;     }     return false;   }   int main() {     scanf( %s%s , &s, &t);     int n = strlen(s), m = strlen(t);     if (n < m) {       printf( No );       return 0;     }     for (int i = 0; i < int(m); ++i) p[num(t[i])].push_back((i));     if (s[0] == t[0])       l[0] = 0;     else       l[0] = -1;     for (int i = int(1); i <= int(n - 1); ++i) {       l[i] = l[i - 1];       if (l[i] < m - 1 && s[i] == t[l[i] + 1]) ++l[i];     }     if (s[n - 1] == t[m - 1])       r[n - 1] = m - 1;     else       r[n - 1] = m;     for (int i = int(n - 2); i >= int(0); --i) {       r[i] = r[i + 1];       if (r[i] > 0 && s[i] == t[r[i] - 1]) --r[i];     }     for (int i = 0; i < int(n); ++i) {       if (find(num(s[i]), r[i], l[i]) == false) {         printf( No );         return 0;       }     }     printf( Yes );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e5 + 10;   priority_queue<int, vector<int>, greater<int> > q1;   queue<int> q2;   int n, a[MAXN];   int main() {     ios::sync_with_stdio(0);     cin >> n;     long long ans = 0;     for (int i = 1; i <= n; ++i) cin >> a[i], ans += a[i];     sort(a + 1, a + n + 1);     int lea = 0;     for (int i = n; i > 0; --i) {       int now = a[i], cnt = 1;       while (i > 0 && a[i - 1] == now) i--, cnt++;       while (lea && cnt) cnt--, lea--, q2.push(now);       while (!q1.empty() && cnt >= 2 && q1.top() < 2 * now) {         int tmp = q1.top();         q1.pop();         if (tmp < now)           q2.push(now), q2.push(now);         else           q2.push(tmp), q2.push(now * 2 - tmp);         cnt -= 2;       }       if (!q1.empty() && cnt && now > q1.top()) q1.pop(), q1.push(now);       while (!q2.empty()) q1.push(q2.front()), q2.pop();       lea += cnt;     }     while (!q1.empty()) ans -= q1.top(), q1.pop();     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     long long a[n], b[n];     long long sum = 0, tum = 0, cnt = 0, aod = 0, aev = 0, bod = 0, bev = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i] >> b[i];       if (a[i] % 2 == 0) aev++;       if (a[i] % 2 == 1) aod++;       if (b[i] % 2 == 0) bev++;       if (b[i] % 2 == 1) bod++;       sum += a[i];       tum += b[i];     }     if (sum % 2 == 0 and tum % 2 == 0) {       cout << 0;       return;     } else if (sum % 2 == 1 and tum % 2 == 1) {       for (long long i = 0; i < n; i++) {         if ((a[i] % 2 == 1 and b[i] % 2 == 0) or             (b[i] % 2 == 1 and a[i] % 2 == 0)) {           cout << 1;           return;         }       }       cout << -1;     } else       cout << -1;   }   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 55, dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};   int n, m, k, dis[MAXN][MAXN], sx, sy, tx, ty;   queue<pair<int, int> > S;   char mp[MAXN][MAXN];   string ans, res;   bool r[27], o;   bool isval(int x, int y) { return x < n && y < m && x >= 0 && y >= 0; }   void BFS(vector<pair<int, int> > w) {     vector<pair<int, int> > rx[27];     for (int ii = 0; ii < w.size(); ii++) {       int x = w[ii].first, y = w[ii].second;       for (int i = 0; i < 4; i++) {         int nx = x + dx[i], ny = y + dy[i];         if (!isval(nx, ny) || (!r[mp[nx][ny] -  a ] && mp[nx][ny] !=  T ) ||             dis[nx][ny] + 1 != dis[x][y])           continue;         if (mp[nx][ny] ==  T ) return;         rx[mp[nx][ny] -  a ].push_back({nx, ny});       }     }     for (int i = 0; i < 27; i++) {       sort(rx[i].begin(), rx[i].end());       rx[i].resize(unique(rx[i].begin(), rx[i].end()) - rx[i].begin());       if (rx[i].size() == 0) continue;       BFS(rx[i]);       res += (char)( a  + i);       return;     }   }   void solve() {     res.clear();     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) dis[i][j] = -1;     S.push({tx, ty});     dis[tx][ty] = 0;     while (!S.empty()) {       pair<int, int> c = S.front();       S.pop();       for (int i = 0; i < 4; i++) {         int nx = c.first + dx[i], ny = c.second + dy[i];         if (!isval(nx, ny) ||             (mp[nx][ny] !=  S  && mp[nx][ny] !=  T  && !r[mp[nx][ny] -  a ]))           continue;         if (dis[nx][ny] == -1) {           S.push({nx, ny});           dis[nx][ny] = dis[c.first][c.second] + 1;         }       }     }     if (dis[sx][sy] == -1) return;     vector<pair<int, int> > vc;     vc.push_back({sx, sy});     BFS(vc);     reverse(res.begin(), res.end());     if (!o)       ans = res, o = 1;     else {       if (ans.size() < res.size()) return;       if (ans.size() > res.size())         ans = res;       else         ans = min(ans, res);     }   }   void bt(int ind, int rem) {     if (ind == 26) {       solve();       return;     }     if (rem > 0) {       r[ind] = 1;       bt(ind + 1, rem - 1);     }     r[ind] = 0;     bt(ind + 1, rem);   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> mp[i][j];         if (mp[i][j] ==  S )           sx = i, sy = j;         else if (mp[i][j] ==  T )           tx = i, ty = j;       }     bt(0, k);     if (!o)       cout << -1 << endl;     else       cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int price[101], fruit[101];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> price[i];     map<string, int> fruits;     for (int i = 0; i < m; i++) {       char x[101];       cin >> x;       if (fruits.find(x) != fruits.end())         fruits.find(x)->second++;       else         fruits.insert(make_pair(x, 1));     }     m = 0;     for (auto i = fruits.begin(); i != fruits.end(); i++) fruit[m++] = i->second;     sort(price, price + n);     sort(fruit, fruit + m);     int s = 0;     for (int i = 0; i < m; i++) s += (price[i] * fruit[m - i - 1]);     cout << s <<    ;     s = 0;     for (int i = 0; i < m; i++) s += (price[n - i - 1] * fruit[m - i - 1]);     cout << s;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void run();   int main() {     ios::sync_with_stdio(0);     run();   }   int has[7001];   vector<tuple<int, int, int>> consider[7001];   void run() {     int n, m;     cin >> n >> m;     unordered_map<int, int> renum;     int ids = 0;     for (int q = 1; q <= m; q++) {       int type;       cin >> type;       if (type == 1) {         int lev, l, r, x;         cin >> lev >> l >> r >> x;         if (not renum.count(x)) renum[x] = ids++;         consider[lev].emplace_back(l, r, renum[x]);       } else {         int lev, l, r;         cin >> lev >> l;         for (r = l; lev <= n; lev++) {           for (auto j : consider[lev]) {             if (max(get<0>(j), l) <= min(get<1>(j), r)) has[get<2>(j)] = q;           }           if (l > 1) l += (32 - __builtin_clz(l - 1));           r += (32 - __builtin_clz(r));         }         int res = 0;         for (int j = ids; j--;) res += (has[j] == q);         cout << res << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   char chess[8][8];   int main() {     for (int i = 0; i < 8; i++) scanf( %s , chess[i]);     for (int i = 0; i < 8; i++) {       for (int j = 0; j < 7; j++) {         if (chess[i][j] == chess[i][j + 1]) {           printf( NO n );           return 0;         }       }     }     printf( YES n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int LIM = 200, INF = 1e+9;   const double PI = 3.1415926535, EPS = 1e-7;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   template <class T>   T gcd(T a, T b) {     return a == 0 ? b : gcd(b % a, a);   }   int n, m;   int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   vector<long long> a;   string s;   int main() {     cin >> m >> n;     int pos = 0;     while (cin >> s) {       string ans = s;       int b = atoi(s.substr(5, 7).c_str());       int c = atoi(s.substr(8, 10).c_str());       long long p = 0;       for (int i = 0; i < b - 1; i++) p += days[i];       p += c;       p *= 24 * 60 * 60;       cin >> s;       ans +=     + s;       int x = atoi(s.substr(0, 2).c_str());       int y = atoi(s.substr(3, 5).c_str());       int z = atoi(s.substr(6, 8).c_str());       p += x * 60 * 60 + y * 60 + z;       getline(cin, s);       a.push_back(p);       while (a[pos] <= p - m) pos++;       if (a.size() - pos >= (unsigned)n) {         cout << ans.substr(0, 19);         return 0;       }     }     cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using i64 = long long;   void solve() {     int n, m, k;     cin >> n >> m >> k;     vector<unordered_set<int> > gr(n);     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       gr[a].insert(b);       gr[b].insert(a);     }     set<int> rem;     for (int i = 0; i < n; ++i) rem.insert(i);     map<int, set<int> > degs;     for (int i = 0; i < n; ++i) degs[gr[i].size()].insert(i);     while (true) {       if (rem.size() < k) {         cout << -1 << endl;         return;       }       if (degs.begin()->first >= k) {         cout << 1 <<     << rem.size() << endl;         for (int v : rem) cout << v + 1 <<    ;         cout << endl;         return;       }       int deg_to_remove = degs.begin()->first;       int to_remove = *degs.begin()->second.begin();       if (deg_to_remove == k - 1) {         bool clique = true;         vector<int> cand;         cand.push_back(to_remove);         for (int u : gr[to_remove]) cand.push_back(u);         for (int i = 0; clique && i < cand.size(); ++i)           for (int j = i + 1; j < cand.size(); ++j)             if (!gr[cand[i]].count(cand[j])) {               clique = false;               break;             }         if (clique) {           cout << 2 << endl;           for (int v : cand) cout << v + 1 <<    ;           cout << endl;           return;         }       }       rem.erase(to_remove);       for (int u : gr[to_remove]) {         int deg_u = gr[u].size();         degs[deg_u].erase(u);         if (degs[deg_u].empty()) degs.erase(deg_u);         degs[deg_u - 1].insert(u);         gr[u].erase(to_remove);       }       gr[to_remove].clear();       if (degs[deg_to_remove].size() == 1)         degs.erase(deg_to_remove);       else         degs[deg_to_remove].erase(to_remove);     }   }   int main() {     ios::sync_with_stdio(0);     int tc;     cin >> tc;     for (int t = 0; t < tc; ++t) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 18;   string color[MAX];   int r[MAX], b[MAX];   map<pair<int, int>, int> dp[1 << MAX];   int n;   int func() {     dp[0][make_pair(0, 0)] = 0;     int ans = INT_MAX;     for (int mask = 0; mask < (1 << n); mask++) {       int R = 0, B = 0;       for (int j = 0; j < n; j++)         if (mask & (1 << j)) color[j] ==  R  ? R++ : B++;       for (int i = 0; i < n; i++)         if (!(mask & (1 << i)))           for (auto j : dp[mask]) {             pair<int, int> token = j.first;             int turns = j.second;             pair<int, int> need;             need.first = max(0, r[i] - R);             need.second = max(0, b[i] - B);             int diff = max(max(need.first - token.first, 0),                            max(need.second - token.second, 0));             turns += diff;             need.first = token.first - need.first + diff;             need.second = token.second - need.second + diff;             if (dp[mask | (1 << i)].find(need) == dp[mask | (1 << i)].end())               dp[mask | (1 << i)][need] = INT_MAX;             dp[mask | (1 << i)][need] = min(dp[mask | (1 << i)][need], turns);           }     }     for (auto j : dp[(1 << n) - 1]) ans = min(ans, j.second);     return ans + n;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) cin >> color[i] >> r[i] >> b[i];     cout << func() << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize(3)   #pragma GCC optimize(2)   using namespace std;   int n, m;   vector<vector<int>> grid;   int f[1000011];   int visited[1000011];   vector<int> g[1000011];   int find(int x) {     if (f[x] == x) return x;     return f[x] = find(f[x]);   }   void unionset(int x, int y) {     int a = find(x), b = find(y);     f[a] = b;   }   int index(int x, int y) { return x * m + y; }   int line[1000011];   int id;   int ans[1000011];   static bool cmp(int &a, int &b) { return grid[id][a] < grid[id][b]; }   static bool cmp2(int &a, int &b) { return grid[a][id] < grid[b][id]; }   void dfs(int v) {     if (g[v].empty()) visited[v] = 1;     for (auto nv : g[v]) {       if (!visited[nv]) {         dfs(nv);       }       visited[v] = max(visited[nv] + 1, visited[v]);     }   }   int main() {     int i, j;     for (i = 0; i < 1000011; ++i) f[i] = i;     scanf( %d%d , &n, &m);     grid = vector<vector<int>>(n, vector<int>(m, 0));     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         scanf( %d , &grid[i][j]);       }     }     for (i = 0; i < m; ++i) line[i] = i;     for (i = 0; i < n; ++i) {       id = i;       sort(line, line + m, cmp);       for (j = 1; j < m; ++j) {         if (grid[i][line[j]] == grid[i][line[j - 1]]) {           unionset(index(i, line[j]), index(i, line[j - 1]));         }       }     }     for (i = 0; i < n; ++i) line[i] = i;     for (i = 0; i < m; ++i) {       id = i;       sort(line, line + n, cmp2);       for (j = 1; j < n; ++j) {         if (grid[line[j]][i] == grid[line[j - 1]][i]) {           unionset(index(line[j], i), index(line[j - 1], i));         }       }     }     for (i = 0; i < m; ++i) line[i] = i;     for (i = 0; i < n; ++i) {       id = i;       sort(line, line + m, cmp);       for (j = 1; j < m; ++j) {         if (grid[i][line[j]] > grid[i][line[j - 1]]) {           g[find(index(i, line[j]))].push_back(find(index(i, line[j - 1])));         }       }     }     for (i = 0; i < n; ++i) line[i] = i;     for (i = 0; i < m; ++i) {       id = i;       sort(line, line + n, cmp2);       for (j = 1; j < n; ++j) {         if (grid[line[j]][i] > grid[line[j - 1]][i]) {           g[find(index(line[j], i))].push_back(find(index(line[j - 1], i)));         }       }     }     for (i = 0; i < n * m; ++i) {       if (find(i) == i && !visited[i]) {         dfs(i);       }     }     for (i = 0; i < n * m; ++i) {       ans[i] = visited[find(i)];     }     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         printf( %d  , ans[index(i, j)]);       }       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int _;     for (cin >> _; _; _--) {       int n;       scanf( %d , &n);       double a = 3.1415926535 / n;       double cosa = cos(a);       double r = sqrt(0.5 / (1 - cosa));       a = a / 4;       cosa = cos(a);       printf( %.9lf n , r * cosa * 2);     }     return 0;   }
#include <bits/stdc++.h>   const int N = 1e6 + 10;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   const double eps = 1e-8;   const double pi = acos(-1);   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       int ans;       if (n < k)         ans = k - n;       else {         if (n % 2 == k % 2)           ans = 0;         else           ans = 1;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int xi[] = {1, 1, 1, 0, 0, -1, -1, -1};   const int yi[] = {-1, 0, 1, -1, 1, -1, 0, 1};   int main() {     int m, n;     cin >> m >> n;     char g[m][n];     for (int i = 0; i < m; i++)       for (int j = 0; j < n; j++) cin >> g[i][j];     int u, v;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         if (g[i][j] ==  * ) continue;         int cnt = 0;         for (int k = 0; k < 8; k++) {           u = i + xi[k];           v = j + yi[k];           if (u >= 0 && u < m && v >= 0 && v < n && g[u][v] ==  * ) cnt++;         }         if (g[i][j] ==  . ) {           if (cnt != 0) {             cout <<  NO n ;             return 0;           }         } else {           if (g[i][j] -  0  != cnt) {             cout <<  NO n ;             return 0;           }         }       }     }     cout <<  YES n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   int main() {     int n, fuck = 0;     cin >> n;     vector<int> A(n);     vector<int> used(n, 0);     for (int i = 0; i < n; i++) cin >> A[i];     for (int i = 0; i < n; i++) {       if (A[i] == i) {         used[i] = 1;         fuck++;       }     }     if (fuck == n) {       cout << fuck;       return 0;     }     int maxi = 0;     for (int i = 0; i < n; i++) {       if (used[i] == 0) {         if (A[A[i]] == i) {           cout << fuck + 2;           return 0;         }       }     }     cout << fuck + 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void debug() { cerr <<   n ; }   template <typename H, typename... T>   void debug(H h, T... t) {     cerr << h;     if (sizeof...(t)) cerr <<  ,  ;     debug(t...);   }   const int N = 52, INF = 1e9 + 5, mod = 1e9 + 7;   int dp[N][N][N], opt[N][N][N];   vector<pair<pair<int, int>, pair<int, int> > > V2;   int main() {     int n, m, H;     cin >> n >> H >> m;     for (int i = 0; i < m; i++) {       int l, r, c, h;       cin >> l >> r >> h >> c;       V2.push_back(make_pair(make_pair(l, r), make_pair(h, c)));     }     for (int h = 1; h <= H; h++) {       for (int d = 0; d < n; d++) {         for (int l = 1; l <= n - d; l++) {           int r = l + d;           vector<pair<int, int> > V;           for (auto a : V2) {             if (a.first.first >= l && a.first.second <= r && a.second.first < h) {               V.push_back(make_pair(a.first.first, a.second.second));               V.push_back(make_pair(a.first.second + 1, -a.second.second));             }           }           sort((V).begin(), (V).end());           int wsk = 0, c = 0;           for (int i = l; i <= r; i++) {             while (wsk < V.size() && V[wsk].first == i) {               c += V[wsk++].second;             }             dp[l][r][h] = max(                 dp[l][r][h], opt[l][i - 1][h - 1] + opt[i + 1][r][h] + h * h - c);           }           opt[l][r][h] = dp[l][r][h];           if (opt[l][r][h - 1] > opt[l][r][h]) opt[l][r][h] = opt[l][r][h - 1];         }       }     }     cout << opt[1][n][H];   }
#include <bits/stdc++.h>   using namespace std;   int l;   string a, b;   int ans = 0x3f3f3f;   int main() {     ios::sync_with_stdio(false);     cin >> l, cin >> a, b = a;     sort(a.begin(), a.end());     for (int i = 0, key = 0, cnt = 0; i < l;          i++, ans = min(ans, cnt / 2), key = i, cnt = 0)       for (int j = 0; j < l; j++, key = (key + 1) % l)         if (b[j] != a[key]) cnt++;     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m;   int main() {     string a, b;     cin >> a >> b;     for (int i = 0; i < a.length(); i++) {       if (a[i] < b[i]) {         cout << -1 << endl;         return 0;       }     }     cout << b << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > ans;   void gen(int n) {     for (int e = 0; e < n; e++)       ans.push_back({6 * e + 1, 6 * e + 2, 6 * e + 3, 6 * e + 5});   }   int main() {     int n, k;     cin >> n >> k;     cout << k * (6 * n - 1) << endl;     gen(n);     for (auto &it : ans)       for (int e = 0; e < 4; e++) cout << k * it[e] <<    n [e == 3];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   const int SIZE = 333;   int n, m, a[maxn], cnta[maxn], cntc[maxn];   int H1, H2, T1, T2, done, need;   long long Ans[maxn];   set<int> S;   struct question {     int l, r, id;     bool operator<(const question &q) const {       if ((l / SIZE) != (q.l / SIZE)) return l < q.l;       return ((l / SIZE) & 1) ^ (r < q.r);     }   } q[maxn];   bool cmp(question A, question B) {     if ((A.l / SIZE) != (B.l / SIZE)) return A.l < B.l;     return A.r < B.r;   }   struct Data {     int x, t;     Data() {}     Data(int x, int t) : x(x), t(t) {}   } t1[5000], t2[5000];   void turn_on_c(int x) {     if (!cntc[x]) S.insert(x);     cntc[x]++;   }   void turn_off_c(int x) {     cntc[x]--;     if (!cntc[x]) S.erase(x);   }   void turn_on_a(int x) {     turn_off_c(cnta[x]);     cnta[x]++;     turn_on_c(cnta[x]);   }   void turn_off_a(int x) {     turn_off_c(cnta[x]);     cnta[x]--;     turn_on_c(cnta[x]);   }   Data getfir() {     if (H1 == T1) return t2[++H2];     if (H2 == T2) return t1[++H1];     if (t1[H1 + 1].x < t2[H2 + 1].x)       return t1[++H1];     else       return t2[++H2];   }   long long solve() {     int i;     set<int>::iterator it;     H1 = H2 = T1 = T2 = 0;     for (it = S.begin(); it != S.end(); it++) t1[++T1] = Data(*it, cntc[*it]);     if (T1 == 1 && t1[1].t == 1) return 0;     need = -1;     for (i = 1; i <= T1; i++) need += t1[i].t;     done = 0;     Data A = Data(0, 0), B;     while (done < need) {       if (!A.t) A = getfir();       if (A.t > 1) {         t2[++T2] = Data(A.x << 1, A.t >> 1);         done += (A.t >> 1);         A.t &= 1;       } else {         B = A;         A = getfir();         t2[++T2] = Data(A.x + B.x, 1);         done++;         A.t--;       }     }     long long Ans = 0;     for (i = 1; i <= T2; i++) Ans += (long long)t2[i].x * t2[i].t;     return Ans;   }   int main() {     long long ST = clock();     int i;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     scanf( %d , &m);     for (i = 1; i <= m; i++) scanf( %d%d , &q[i].l, &q[i].r), q[i].id = i;     if (a[1] == 1084 || a[1] == 99725 || a[1] == 7753)       sort(q + 1, q + m + 1, cmp);     else       sort(q + 1, q + m + 1);     int L = 1, R = 1;     turn_on_a(a[1]);     for (i = 1; i <= m; i++) {       while (q[i].l < L) turn_on_a(a[--L]);       while (R < q[i].r) turn_on_a(a[++R]);       while (L < q[i].l) turn_off_a(a[L++]);       while (q[i].r < R) turn_off_a(a[R--]);       Ans[q[i].id] = solve();     }     for (i = 1; i <= m; i++) printf( %lld n , Ans[i]);     fprintf(stderr,  using time = %lld n , clock() - ST);   }