malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 2e2 + 10, oo = 1e9 + 10, MOD = 1e9 + 7;   int n, m;   char a[N][N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) cin >> a[i][j];     }     if (n % 3 && m % 3) {       cout <<  NO n ;       return 0;     }     if (n % 3 == 0) {       int q = 0, r = 0, b = 0, g = 0;       for (int i = 1; i <= n; i++) {         for (int j = 2; j <= m; j++) {           if (a[i][j] != a[i][j - 1]) q = 1;         }         if (i <= n / 3) {           if (i > 1 && a[i][1] != a[i - 1][1]) q = 1;           continue;         }         if (i > n / 3 && i <= 2 * n / 3) {           if (i > n / 3 + 1 && a[i][1] != a[i - 1][1]) q = 1;           continue;         }         if (i > 2 * n / 3 + 1 && a[i][1] != a[i - 1][1]) q = 1;       }       if (a[1][1] ==  R ) r++;       if (a[n / 3 + 1][1] ==  R ) r++;       if (a[2 * n / 3 + 1][1] ==  R ) r++;       if (a[1][1] ==  G ) g++;       if (a[n / 3 + 1][1] ==  G ) g++;       if (a[2 * n / 3 + 1][1] ==  G ) g++;       if (a[1][1] ==  B ) b++;       if (a[n / 3 + 1][1] ==  B ) b++;       if (a[2 * n / 3 + 1][1] ==  B ) b++;       if (r == b && b == g && !q) {         cout <<  YES n ;         return 0;       }     }     if (m % 3 == 0) {       int q = 0, r = 0, b = 0, g = 0;       for (int j = 1; j <= m; j++) {         for (int i = 2; i <= n; i++) {           if (a[i][j] != a[i - 1][j]) q = 1;         }         if (j <= m / 3) {           if (j > 1 && a[1][j] != a[1][j - 1]) q = 1;           continue;         }         if (j > m / 3 && j <= 2 * m / 3) {           if (j > m / 3 + 1 && a[1][j] != a[1][j - 1]) q = 1;           continue;         }         if (j > 2 * m / 3 + 1 && a[1][j] != a[1][j - 1]) q = 1;       }       if (a[1][1] ==  R ) r++;       if (a[1][m / 3 + 1] ==  R ) r++;       if (a[1][2 * m / 3 + 1] ==  R ) r++;       if (a[1][1] ==  G ) g++;       if (a[1][m / 3 + 1] ==  G ) g++;       if (a[1][2 * m / 3 + 1] ==  G ) g++;       if (a[1][1] ==  B ) b++;       if (a[1][m / 3 + 1] ==  B ) b++;       if (a[1][2 * m / 3 + 1] ==  B ) b++;       if (r == b && b == g && !q) {         cout <<  YES n ;         return 0;       }     }     cout <<  NO n ;   }
#include <bits/stdc++.h>   using namespace std;   int total_operation = 0;   bool is_prime(long long n) {     if (n == 2) return true;     if (n % 2 == 0 || n < 2) return false;     for (int i = 3; i * i <= n; i += 2) {       if (n % i == 0) return false;     }     return true;   }   int countprime_in_range(int n) {     vector<bool> isprime(n + 1, true);     isprime[0] = isprime[1] = false;     for (int i = 2; i * i <= n; ++i) {       if (isprime[i]) {         for (int j = 2 * i; j <= n; j += i) {           isprime[j] = 0;         }       }     }     int cnt = 0;     for (int i = 0; i <= n; i++) {       if (isprime[i]) cnt++;     }     return cnt;   }   int main() {     int fact1[3], fact2[3];     fact1[0] = fact1[1] = fact1[2] = 0;     fact2[0] = fact2[1] = fact2[2] = 0;     int A, B;     cin >> A >> B;     if (A == B) {       cout <<  0 ;       return 0;     }     while (A) {       if (!(A % 2))         fact1[0]++, A /= 2;       else if (!(A % 3))         fact1[1]++, A /= 3;       else if (!(A % 5))         fact1[2]++, A /= 5;       else         break;     }     while (B) {       if (!(B % 2))         fact2[0]++, B /= 2;       else if (!(B % 3))         fact2[1]++, B /= 3;       else if (!(B % 5))         fact2[2]++, B /= 5;       else         break;     }     if (A != B)       cout <<  -1 ;     else       cout << abs(fact1[0] - fact2[0]) + abs(fact1[1] - fact2[1]) +                   abs(fact1[2] - fact2[2]);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, a, b;       cin >> n >> a >> b;       string s =   ;       char x =  a ;       for (int i = 0; i < n; i++) {         if (i % b == 0) x =  a ;         s += x;         x++;       }       cout << s << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 3;   string s, p;   int F[MAX][2];   void solve() {     cin >> s >> p;     F[0][0] = F[0][1] = 0;     for (int i = 1; i <= p.size(); ++i) {       for (int j = 0; j < 2; ++j) {         F[i][j] = F[i - 1][j];       }       ++F[i][p[i - 1] -  0 ];     }     long long int ans = 0;     for (int i = 0; i < s.size(); ++i) {       int c = s[i] -  0 ;       for (int j = 0; j < 2; ++j) {         ans += abs(c - j) * (F[p.size() - s.size() + i + 1][j] - F[i][j]);       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     solve();     return 0;   }
#include <bits/stdc++.h>   std::string x;   long long getlen(long long r) {     long long now = 1, lst = 0, res = 0;     int dig = 0;     while (now <= r) {       res += (now - 1 - lst) * dig;       lst = now - 1;       now *= 10;       ++dig;     }     res += (r - lst) * dig;     return res;   }   int main() {     long long l = 1, r = 1000000000000ll, ans = 0;     long long t;     std::cin >> t;     while (l <= r) {       long long mid = l + r >> 1;       if (getlen(mid) >= t)         r = mid - 1;       else         l = mid + 1, ans = mid;     }     t -= getlen(ans);     std::string st;     for (long long i = ans + 1; i <= ans + 10; ++i) st += std::to_string(i);     printf( %c n , st[t - 1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   vector<pair<int, int>> ans;   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     reverse(a.begin(), a.end());     vector<pair<int, int>> ans;     int m = a[0] + 1;     vector<int> cnt(m);     int l = 0, r = n;     int st = 0;     int en = m;     while (l < r) {       if (cnt[en - 1] == a[r - 1]) {         --en;         --r;         continue;       }       if (a[l] >= en) l++;       for (int i = 0; i < a[l] - st; i++) {         ans.emplace_back(st, st + i + 1);         cnt[st]++;         cnt[st + i + 1]++;       }       en = a[l] + 1;       st++;     }     cout << ans.size() << endl;     for (auto p : ans) cout << p.first + 1 <<     << p.second + 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[1000005], dp1[1000005], dp2[1000005], dp3[1000005];   long long ans;   int main(int argc, const char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 0; i <= m + 1; ++i) {       dp1[i] = 1e9;       dp2[i] = -1e9;     }     int x;     int mn = 1e9;     for (int i = n; i > 0; --i) {       x = a[i];       if (mn < x && mn < dp1[x]) dp1[x] = mn;       if (x < mn) mn = x;     }     int mx = -1e9;     for (int i = 1; i <= n; ++i) {       x = a[i];       if (mx > x && mx > dp2[x]) dp2[x] = mx;       if (x > mx) mx = x;     }     for (int i = m; i > 0; --i) {       dp3[i] = dp1[i];       if (dp1[i + 1] < dp1[i]) dp1[i] = dp1[i + 1];     }     for (int i = m; i > 0; --i) {       if (dp2[i + 1] > dp2[i]) dp2[i] = dp2[i + 1];     }     int l, r;     for (int i = 1; i <= m; ++i) {       l = i;       r = m;       while (l < r) {         int mid = (l + r) >> 1;         if (dp1[mid + 1] >= i && dp2[mid + 1] <= mid)           r = mid;         else           l = mid + 1;       }       if (dp1[r + 1] >= i && dp2[r + 1] <= r) ans += m - r + 1;       if (dp3[i] < i) break;     }     cout << ans <<   n ;     return 0;   }
#include<cstdio>   #include<cstring>   #include<algorithm>   using namespace std;   const int mod=1e9+7;   inline int addmod(int x)   {    return x>=mod?x-mod:x;   }   inline int submod(int x)   {    return x<0?x+mod:x;    }   int n,m,q,b[105],c[105],sum[105],f[105][10005],qans[1005],pn;   int solve(int x)   {    memset(f,0,sizeof(f));    f[0][0]=1;    int nw=0,ans=0;    for(int i=1;i<=n;i++)    {     for(int j=0;j<=nw;j++)     {      f[i][j]=addmod(f[i][j]+f[i-1][j]);      f[i][j+c[i]+1]=submod(f[i][j+c[i]+1]-f[i-1][j]);     }     nw+=c[i];     for(int j=1;j<=nw;j++)      f[i][j]=addmod(f[i][j]+f[i][j-1]);     for(int j=0;j<x*i+sum[i];j++)      f[i][j]=0;    }    for(int i=0;i<=nw;i++)     ans=addmod(ans+f[n][i]);    return ans;   }   int main()   {    scanf( %d ,&n);    int mul=1;    for(int i=1;i<=n;i++)    {     scanf( %d ,&c[i]);     m=max(m,c[i]);     mul=1ll*mul*(c[i]+1)%mod;    }    for(int i=1;i<n;i++)     scanf( %d ,&b[i]);    for(int i=1;i<=n;i++)     for(int j=1;j<i;j++)      sum[i]+=(i-j)*b[j];    pn=(-sum[n])/n;    for(int i=0;i<=m;i++)     qans[i]=solve(i+pn);    scanf( %d ,&q);    for(int i=1;i<=q;i++)    {     int x;     scanf( %d ,&x);     if(x<pn) printf( %d n ,mul);     else if(x>pn+m) printf( 0 n );     else printf( %d n ,qans[x-pn]);    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     int i, n, e;     double x1, y1, x2, y2, vmax, t, vx, vy, wx, wy;     cin >> x1 >> y1 >> x2 >> y2;     cin >> vmax >> t;     cin >> vx >> vy;     cin >> wx >> wy;     double sid;     double l = 0.0, r = (double)1e9;     double eps = 1e-7;     double x, y;     while (r - l > eps) {       sid = (r + l) / 2.0;       if (sid < t) {         x = x1 + sid * vx;         y = y1 + sid * vy;       } else {         x = x1 + t * vx + (sid - t) * wx;         y = y1 + t * vy + (sid - t) * wy;       }       double val;       val = (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y);       val = sqrt(val);       if (val < vmax * sid) {         r = sid;       } else         l = sid;     }     cout << setprecision(8);     cout << fixed;     cout << r <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void reshenie() {     int n, m;     cin >> n >> m;     int a[n][n];     for (int i = 0; i < n; ++i) {       for (int j = i; j < n; ++j) {         a[i][j] = a[j][i] = 0;       }     }     while (m--) {       int u, v;       cin >> u >> v;       --u, --v;       a[u][v] = a[v][u] = 1;     }     if (n == 1) {       cout <<  Yes na  << endl;       return;     }     string s(n,  * );     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         if (a[i][j]) {           s[i] = s[j] =  b ;         }       }     }     for (int i = 0; i < n; ++i) {       if (accumulate(a[i], a[i] + n, 0) != n - 1) {         if (s[i] !=  c ) {           s[i] =  a ;           for (int j = i + 1; j < n; ++j) {             if (!a[i][j]) {               s[j] =  c ;             }           }         } else {           s[i] =  c ;           for (int j = i + 1; j < n; ++j) {             if (!a[i][j]) {               s[j] =  a ;             }           }         }       }     }     for (int i = 0; i < n; ++i) {       if (s[i] ==  * ) {         cout <<  No  << endl;         return;       }       for (int j = i + 1; j < n; ++j) {         if (a[i][j] && abs(s[i] - s[j]) <= 1) continue;         if (!a[i][j] && abs(s[i] - s[j]) > 1) continue;         cout <<  No  << endl;         return;       }     }     cout <<  Yes n  << s << endl;   }   int main() {     reshenie();     return 1 - 1 + 2 - 2 + 3 - 3 + 4 - 4 + 5 - 5;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const int N = 2e5 + 10;   const long long mod = 1e9 + 7;   const long long mod2 = 998244353;   const long long inf = 8e18;   const int LOG = 22;   long long pw(long long a, long long b, long long M) {     return (!b ? 1                : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M                         : pw(a * a % M, b / 2, M)));   }   int v[N], dp[N], cnt[N], cu[N], Fi[N], kol[N], L[N], A[N];   void solve() {     map<int, int> mp;     int n;     cin >> n;     A[0] = -1;     for (int i = 0; i <= n + 1; i++) {       if (i && i <= n) {         cin >> v[i];         A[i] = v[i];       }       dp[i] = cnt[i] = cu[i] = Fi[i] = L[i] = kol[i] = 0;     }     sort(A + 1, A + n + 1);     int ptr = 0;     for (int i = 1; i <= n; i++) {       if (A[i] != A[i - 1]) {         ptr++;       }       mp[A[i]] = ptr;     }     for (int i = n; ~(i - 1); i--) {       v[i] = mp[v[i]];       Fi[v[i]] = i;       cnt[v[i]]++;     }     int ans = 0;     for (int i = 1; i <= n; i++) {       dp[i] = max(dp[L[v[i]]], cu[v[i] - 1]) + 1;       if (cnt[v[i] - 1] == cu[v[i] - 1]) {         dp[i] = max(dp[i], kol[v[i] - 1] + 1);       }       ans = max(ans, dp[i]);       L[v[i]] = i;       ++cu[v[i]];       if (cu[v[i]] == cnt[v[i]]) {         kol[v[i]] = dp[Fi[v[i]]] + cnt[v[i]] - 1;       }     }     cout << n - ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int q;     cin >> q;     while (q--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mark[200002];   vector<int> pos[11];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int i, l, t, j, cnt, mx, last;     bool flag;     string s;     cin >> t;     while (t--) {       cin >> l >> s;       for (i = 0; i < 10; i++) {         pos[i].clear();       }       mx = -1;       for (i = 0; i < l; i++) {         pos[s[i] -  0 ].push_back(i);         mx = max(mx, (s[i] -  0 ));         mark[i] = 0;       }       cnt = 0;       for (i = pos[mx].size() - 1; i >= 0; i--) {         mark[pos[mx][i]] = 2;         ++cnt;         last = pos[mx][i];       }       flag = true;       for (i = mx - 1; i >= 0 && flag; i--) {         if (pos[i].size() > 0) {           for (j = pos[i].size() - 1; j >= 0; j--) {             if (last > pos[i][j]) {               mark[pos[i][j]] = 2;               last = pos[i][j];               ++cnt;             } else {               flag = false;             }           }         }       }       last = l;       for (i = mx - 1; i >= 0; i--) {         if (pos[i].size() > 0) {           for (j = pos[i].size() - 1; j >= 0; j--) {             if (mark[pos[i][j]] == 0 && last > pos[i][j]) {               mark[pos[i][j]] = 1;               last = pos[i][j];               ++cnt;             }           }         }       }       if (cnt == l) {         for (i = 0; i < l; i++) {           cout << mark[i];         }         cout <<   n ;       } else {         cout <<  - n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct M {     int x, y, z;   };   int dis(int x, int y) { return abs(x - y); }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     int n;     cin >> n;     M note[5000];     vector<bool> vs(5000, 0);     for (int i = 1; i <= n; i++) {       cin >> note[i].x >> note[i].y >> note[i].z;     }     for (int i = 1; i <= n; i++) {       int X = INT_MAX, Y = INT_MAX, Z = INT_MAX, now;       if (!vs[i]) {         for (int j = 1; j <= n; j++) {           if (i != j && !vs[j]) {             int disX = dis(note[i].x, note[j].x);             int disY = dis(note[i].y, note[j].y);             int disZ = dis(note[i].z, note[j].z);             if (disX < X) {               now = j;             } else if (disX == X) {               if (disY < Y)                 now = j;               else if (disY == Y) {                 if (disZ < Z) now = j;               }             }             if (now == j) {               X = disX;               Y = disY;               Z = disZ;             }           }         }         cout << i <<     << now << endl;         vs[i] = 1;         vs[now] = 1;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 50;   struct node {     int v, c, l, r, id;     void read() { scanf( %d%d%d%d , &v, &c, &l, &r); }   } car[N];   map<int, int> mp, MP;   inline int clr(int x) { return car[x].c + car[x].l + car[x].r; }   bool cmp(node a, node b) {     int ta = a.c + a.l + a.r;     int tb = b.c + b.l + b.r;     if (ta == tb)       return a.id < b.id;     else       return ta < tb;   }   int n, f[N], pre[N], c[N];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) car[i].read(), car[i].id = i;     sort(car + 1, car + n + 1, cmp);     int ans = 0, num = 0;     for (int i = 1; i <= n;) {       int j = i;       while (j <= n && clr(j) == clr(i)) ++j;       mp.clear();       MP.clear();       mp[0] = 0;       MP[0] = 0;       for (int k = i; k <= j - 1; k++) {         if (mp.count(car[k].l)) {           f[k] = mp[car[k].l] + car[k].v;           pre[k] = MP[car[k].l];         }         if (car[k].r == 0 && f[k] > ans) {           ans = f[k];           num = k;         }         int tmp = car[k].c + car[k].l;         if (!mp.count(tmp) || mp[tmp] < f[k]) {           mp[tmp] = f[k];           MP[tmp] = k;         }       }       i = j;     }     int tmp = num;     ans = 0;     while (tmp) c[++ans] = car[tmp].id, tmp = pre[tmp];     printf( %d n , ans);     for (int i = ans; i >= 1; i--) printf( %d%c , c[i], i == 1 ?   n  :    );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; ++i) cin >> a[i];     int m;     cin >> m;     int b[m];     for (int i = 0; i < m; ++i) cin >> b[i];     sort(a + 0, a + n);     sort(b + 0, b + m);     int ans = 0;     for (int i = 0, j = 0; i < n; i++) {       while (j < m) {         if (abs(a[i] - b[j]) <= 1) {           ans++;           j++;           break;         } else if (a[i] - b[j] < 0) {           break;         } else {           j++;         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long int ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % M;       p = (p * p) % M;     }     return (T)ret;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   template <class T>   inline T bpow(T p, T e) {     long long int ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p);       p = (p * p);     }     return (T)ret;   }   int toInt(string s) {     int sm;     stringstream ss(s);     ss >> sm;     return sm;   }   int toLlint(string s) {     long long int sm;     stringstream ss(s);     ss >> sm;     return sm;   }   const double eps = 1e-9;   int ts, kk = 1;   int n, b[500005];   pair<int, int> a[500005];   int lt[500005], rt[500005];   int main() {     int t, i, j, k;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &k);       b[i] = k;       a[i] = pair<int, int>(k, i);       lt[i] = i - 1;       rt[i] = i + 1;     }     sort(a, a + n);     int x, y;     long long int rs = 0;     int lo, hi;     lo = 0;     hi = n - 1;     for (i = 0; i < n - 2; i++) {       k = a[i].second;       if (k == lo) {         rs += a[i].first;         lo = rt[k];         continue;       }       if (k == hi) {         rs += a[i].first;         hi = lt[k];         continue;       }       x = lt[k];       y = rt[k];       rs += min(b[x], b[y]);       rt[x] = y;       lt[y] = x;     }     cout << rs <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   const int maxn = (1 << 11) + 100;   int dp[maxn][maxn];   vector<int> g[15];   int n, m, k, u, v, mx, cc[maxn];   void init() {     for (int i = 0; i < maxn; ++i) cc[i] = __builtin_popcount(i);   }   int main() {     init();     cin >> n >> m >> k;     for (int i = 0; i < m; ++i) {       cin >> u >> v;       u--;       v--;       g[v].push_back(u);       g[u].push_back(v);     }     mx = (1 << n) - 1;     for (int i = 1; i <= mx; i <<= 1) dp[i][i] = 1;     for (int i = 1; i <= mx; ++i) {       for (int j = i; j; --j &= i) {         if (dp[i][j]) {           for (int k = 0; k < n; ++k) {             if (i & (1 << k)) {               for (int u : g[k]) {                 if (~i & (1 << u)) {                   if (cc[i] == 1)                     v = i | (1 << u);                   else                     v = j & ~(1 << k) | (1 << u);                   if (!(v >> u + 1)) dp[i | (1 << u)][v] += dp[i][j];                 }               }             }           }         }       }     }     long long ans = 0;     for (int i = 0; i < mx; ++i)       if (cc[i] == k) ans += dp[mx][i];     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100001;   const int MOD = 1e9 + 7;   mt19937 rnd(100);   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m, x;     vector<vector<int> > a;     cin >> n >> m;     a.assign(n, vector<int>(m));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> a[i][j];     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> x;         a[i][j] = (int)(x != a[i][j]);       }     for (int i = 0; i < n; i++) {       int sum = 0;       for (int j = 0; j < m; j++) sum += a[i][j];       if (sum % 2) {         cout <<  No ;         return 0;       }     }     for (int i = 0; i < m; i++) {       int sum = 0;       for (int j = 0; j < n; j++) sum += a[j][i];       if (sum % 2) {         cout <<  No ;         return 0;       }     }     cout <<  Yes ;     return 0;   }
#include <bits/stdc++.h>      using namespace std;       typedef long long s64;       const int MaxN = 100000 + 5;       int a[MaxN];       void work() {    int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {     scanf( %d , a + i);     }       sort(a + 1, a + n + 1);        s64 ans = a[n];     for (int i = 2; i <= n; ++i)     ans -= 1LL * a[i] * ((i - 1) - (n - i));         printf( %lld n , ans);    }      int main() {    int T;     scanf( %d , &T);        while (T--) work();        return 0;    }
#include <bits/stdc++.h>   using namespace std;   int edge[300005][5];   int num[300005], res[300005];   void dfs(int x) {     int tmp = 0;     for (int i = 0; i < num[x]; i++)       if (res[x] == res[edge[x][i]]) tmp++;     if (tmp > 1) {       res[x] ^= 1;       for (int i = 0; i < num[x]; i++)         if (res[x] == res[edge[x][i]]) dfs(edge[x][i]);     }   }   int main(int argc, char* argv[]) {     std::ios::sync_with_stdio(false);     int n, m, a, b;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> a >> b;       edge[a][num[a]++] = b;       edge[b][num[b]++] = a;     }     for (int i = 1; i < n + 1; i++) dfs(i);     for (int i = 1; i < n + 1; i++) cout << res[i];     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n), b(n), pa(n), pb(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];       --a[i];       pa[a[i]] = i;     }     for (int i = 0; i < n; ++i) {       cin >> b[i];       --b[i];       pb[b[i]] = i;     }     vector<int> ans(n, 999999);     vector<vector<int>> evi(n), evo(n);     multiset<int> segs;     for (int i = 0; i < n; ++i) {       int wh = (pb[i] - pa[i] + n) % n;       int tol = n - pa[i], tor = pa[i];       evi[wh].push_back(wh);       if ((wh + tor) % n < wh) {         segs.insert(wh - n);         evo[(wh + tor) % n].push_back(wh - n);       } else {         evo[(wh + tor) % n].push_back(wh);       }     }     for (int i = 0; i < n; ++i) {       for (int z : evi[i]) {         segs.insert(z);       }       if (segs.size()) {         auto it = segs.end();         --it;         int x = *it;         ans[i] = min(ans[i], (i - x) % n);       }       for (int z : evo[i]) {         segs.erase(segs.find(z));       }     }     segs.clear();     evi.assign(n, vector<int>());     evo.assign(n, vector<int>());     for (int i = 0; i < n; ++i) {       int wh = (pb[i] - pa[i] + n) % n;       int tol = n - pa[i] - 1, tor = pa[i];       evi[wh].push_back(wh);       if ((wh - tol + n) % n > wh) {         segs.insert(wh + n);         evo[(wh - tol + n) % n].push_back(wh + n);       } else {         evo[(wh - tol + n) % n].push_back(wh);       }     }     for (int i = n - 1; i >= 0; --i) {       for (int z : evi[i]) {         segs.insert(z);       }       if (segs.size()) {         int x = *segs.begin();         ans[i] = min(ans[i], (x - i) % n);       }       for (int z : evo[i]) {         segs.erase(segs.find(z));       }     }     for (int i = 0; i < n; ++i) {       cout << ans[i] << endl;     }     return 0;   }