malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   char s[200];   vector<pair<int, int> > v;   int main() {     scanf( %s , s);     int len = strlen(s);     pair<int, int> cur(0, 0);     v.push_back(cur);     for (int i = 0; i < len; i++) {       if (s[i] ==  L ) {         cur.first--;       } else if (s[i] ==  R ) {         cur.first++;       } else if (s[i] ==  U ) {         cur.second++;       } else if (s[i] ==  D ) {         cur.second--;       }       v.push_back(cur);     }     bool res = false;     len = v.size();     for (int i = 0; i < len; i++) {       for (int j = i + 2; j < len; j++) {         if (abs(v[i].first - v[j].first) + abs(v[i].second - v[j].second) <= 1) {           res = true;           goto zzz;         }       }     }   zzz:     if (res) {       printf( BUG );     } else {       printf( OK );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100005;   long long A[MaxN][(1 << (5))], B[MaxN][(1 << (5))], dp[MaxN][(1 << (5))];   const long long Inf = 1LL << 60;   int main() {     int N, M;     scanf( %d%d , &N, &M);     for (int i = 0; i < M - 1; i++)       for (int j = 0; j < N; j++) {         int x;         scanf( %d , &x);         for (int k = 0; k < (1 << (N)); k++)           if (k & (1 << (j))) A[i][k] += x;       }     for (int i = 0; i < M; i++)       for (int j = 0; j < N; j++) {         int x;         scanf( %d , &x);         for (int k = 0; k < (1 << (N)); k++)           if (((k & (1 << (j))) == 0) ^ ((k & (1 << ((j + 1) % N))) == 0))             B[i][k] += x;       }     for (int i = 0; i <= M; i++)       for (int j = 0; j < (1 << (N)); j++) dp[i][j] = Inf;     dp[0][0] = 0;     for (int i = 1; i < M; i++)       for (int j = 0; j < (1 << (N)); j++) {         dp[i][j] = min(dp[i][j], A[i - 1][j] + B[i][j]);         for (int k = j; k > 0; k -= k & (-k))           dp[i][j] = min(dp[i][j], dp[i - 1][k] + A[i - 1][k ^ j] + B[i][j]);       }     printf( %I64d n , dp[M - 1][(1 << (N)) - 1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, cnt, a[2010], b[2010], bel[2010], ans[2010 * 2010 / 2][2];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);       bel[b[i]] = i;     }     for (int i = 1; i <= n; i++) {       a[i] = bel[a[i]];       cnt += abs(a[i] - i);     }     cnt /= 2;     printf( %d n , cnt);     cnt = 0;     for (int i = 1; i <= n; i++) bel[a[i]] = i;     for (int i = n, now = bel[n]; i; i--, now = bel[i])       for (int j = bel[i] + 1; j <= n; j++)         if (a[j] <= now) {           ans[++cnt][0] = j;           ans[cnt][1] = now;           swap(a[now], a[j]);           now = j;         }     printf( %d n , cnt);     for (int i = 1; i <= cnt; i++) printf( %d %d n , ans[i][0], ans[i][1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, page;     cin >> n;     vector<int> pages(7);     for (int i = 0; i < 7; i++) {       cin >> page;       pages[i] = page;     }     int ans = 0;     while (true) {       n -= pages[ans];       if (n <= 0) {         break;       }       ans = (ans + 1) % 7;     }     cout << ans + 1;   }
#include <bits/stdc++.h>   using namespace std;   int n, q;   int C[500001];   vector<pair<int, int> > P[500001];   int ans[500001];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> C[i];     cin >> q;     for (int i = 0; i < q; ++i) {       int l, r;       cin >> l >> r;       P[r].emplace_back(l, i);     }     pair<int, int> basis[20] = {};     for (int R = 1; R <= n; ++R) {       pair<int, int> v(C[R], R);       for (int i = 20; i--;) {         if (v.first >> i) {           if (v.second < basis[i].second)             v.first ^= basis[i].first;           else if (basis[i].first == 0) {             basis[i] = v;             break;           } else {             swap(v, basis[i]);             v.first ^= basis[i].first;           }         }       }       for (pair<int, int> _ : P[R]) {         int L, idx;         tie(L, idx) = _;         int v = 0;         for (int i = 20; i--;) {           if (basis[i].second < L) continue;           if (((v >> i) & 1) == 0) v ^= basis[i].first;         }         ans[idx] = v;       }     }     for (int i = 0; i < q; ++i) {       printf( %d n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int OO = 1e9;   const double EPS = 1e-9;   template <class T>   void _db(const char* dbStr, T e) {     cout << dbStr <<   =   << e << endl;   }   template <class T, class... L>   void _db(const char* dbStr, T e, L... r) {     while (*dbStr !=  , ) cout << *dbStr++;     cout <<   =   << e <<  , ;     _db(dbStr + 1, r...);   }   template <class S, class T>   ostream& operator<<(ostream& o, const map<S, T>& v) {     o <<  [ ;     int i = 0;     for (const pair<S, T>& pr : v)       o << (!i++ ?    :  ,  ) <<  {  << pr.first <<   :   << pr.second <<  } ;     return o <<  ] ;   }   template <template <class, class...> class S, class T, class... L>   ostream& operator<<(ostream& o, const S<T, L...>& v) {     o <<  [ ;     int i = 0;     for (const auto& e : v) o << (!i++ ?    :  ,  ) << e;     return o <<  ] ;   }   template <class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& pr) {     return o <<  (  << pr.first <<  ,   << pr.second <<  ) ;   }   ostream& operator<<(ostream& o, const string& s) {     for (const char& c : s) o << c;     return o;   }   template <class T>   using V = vector<T>;   template <class T>   using VV = V<V<T>>;   template <class T>   using VVV = VV<V<T>>;   using ll = long long;   using pii = pair<int, int>;   using vi = V<int>;   using vii = V<pii>;   using vvi = VV<int>;   using mii = map<int, int>;   using umii = unordered_map<int, int>;   using si = set<int>;   using usi = unordered_set<int>;   int n;   vi c;   vvi adj;   pii dfs(int u, int par) {     int mxH[] = {0, 0, 0}, diam = 0;     for (auto& v : adj[u])       if (v != par) {         pii pr = dfs(v, u);         diam = max(diam, pr.first);         mxH[0] = pr.second + (c[u] != c[v]);         sort(mxH, mxH + 3);       }     diam = max(diam, mxH[1] + mxH[2]);     return make_pair(diam, mxH[2]);   }   int main() {     ios::sync_with_stdio(false);     cout.precision(10);     cin.tie(0);     cin >> n;     c.assign(n + 1, 0);     adj.assign(n + 1, vi());     for (int i = 1; i <= n; ++i) {       cin >> c[i];     }     for (int i = 1; i <= n - 1; ++i) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     cout << (dfs(1, -1).first + 1) / 2 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long xorsum[(1L << 16)];   long long deg[(1L << 16)];   int main(void) {     long long n;     cin >> n;     queue<long long> q;     long long td = 0;     for (long long i = 0; i < n; i++) {       cin >> deg[i] >> xorsum[i];       if (deg[i] == 1) {         q.push(i);       }       td += deg[i];     }     vector<pair<long long, long long> > ans;     while (!q.empty()) {       long long u = q.front();       q.pop();       if (deg[u] == 0) continue;       long long v = xorsum[u];       xorsum[v] ^= u;       ans.push_back(make_pair(u, v));       deg[v]--;       if (deg[v] == 1) q.push(v);     }     cout << (td / 2) << endl;     for (long long i = 0; i < ans.size(); i++) {       cout << ans[i].first <<     << ans[i].second << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)1e6 + 5;   const int INF = (int)1e9 + 7;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int a, b, c, d;     cin >> a >> b >> c >> d;     if (a + b == c + d or a + c == b + d or a + d == c + b or a + b + c == d or         a + c + d == b or a + b + d == c or a == b + c + d) {       cout <<  YES ;       return 0;     }     cout <<  NO ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.length();     bool ok = false;     for (int i = 0; i < n - 1; i++) {       if (s[i] ==  A  && s[i + 1] ==  B ) {         for (int j = i + 2; j < n - 1; j++) {           if (s[j] ==  B  && s[j + 1] ==  A ) {             ok = true;             break;           }         }       }       if (ok) break;     }     if (ok) {       cout <<  YES n ;       return 0;     }     for (int i = 0; i < n - 1; i++) {       if (s[i] ==  B  && s[i + 1] ==  A ) {         for (int j = i + 2; j < n - 1; j++) {           if (s[j] ==  A  && s[j + 1] ==  B ) {             ok = true;             break;           }         }       }       if (ok) break;     }     if (ok)       cout <<  YES n ;     else       cout <<  NO n ;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e12 + 1, maxn = 2 * (1e5 + 5), mod = 1e9 + 7;   const double eps = 1e-9;   long long m, i, j, k, n, answer, t, l, r;   string s;   bool f(long long l0, long long r0, long long i0) {     if (r0 - i0 + 1 < i0 - l0) {       return 1;     }     if (r0 - i0 + 1 > i0 - l0) {       return 0;     }     long long jd = 0;     for (long long i1 = l0; i1 < i0; ++i1) {       if (s[i1] > s[i1 + i0]) {         jd = 1;         break;       }       if (s[i1] < s[i1 + i0]) {         if (!jd) {           return 0;         }         break;       }     }     if (jd) {       return 1;     }   }   int main() {     cin >> s;     n = s.size();     l = 0;     r = n - 1;     for (i = n - 1; i >= 0; --i) {       j = 0;       while (i >= 0 && s[i] ==  0 ) {         j = 1;         i--;       }       if (f(l, r, i)) {         r = i - 1;         answer++;       } else {         break;       }     }     cout << answer + 1;   }
#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-8;   const int mod = 1e9 + 7;   const int N = 1e6 + 10;   const long long INF = 1e18;   long long power(long long x, long long y) {     long long t = 1;     while (y > 0) {       if (y % 2)         y -= 1, t = t * x % mod;       else         y /= 2, x = x * x % mod;     }     return t;   }   long long val[N];   vector<pair<long long, long long> > G[N];   int vk[N];   set<pair<long long, long long> > S[N];   int ans[N];   int tl = 0;   void merge(int &p, int nw, long long cs) {     if (S[nw].size() == 0) return;     set<pair<long long, long long> >::iterator it = S[nw].end();     vector<pair<long long, long long> > z;     while (1) {       it--;       if ((*it).first > cs) {         z.push_back(*it);       } else         break;       if (it == S[nw].begin()) break;     }     for (int i = 0; i < z.size(); ++i) S[nw].erase(z[i]);     if (p == -1) {       p = nw;       return;     }     if (S[p].size() < S[nw].size()) swap(p, nw);     it = S[nw].begin();     while (it != S[nw].end()) {       S[p].insert(*it);       it++;     }   }   int dfs1(int v, long long c) {     int my = -1;     for (int i = 0; i < G[v].size(); ++i) {       int x = dfs1(G[v][i].first, G[v][i].second + c);       merge(my, x, c);     }     if (my == -1) my = tl++;     ans[v] = S[my].size();     S[my].insert(make_pair(c - val[v], v));     return my;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %lld , &val[i]);     for (int i = 0; i < n - 1; ++i) {       long long x, y;       scanf( %lld%lld , &x, &y);       x--;       G[x].push_back(make_pair(i + 1, y));     }     dfs1(0, 0);     for (int i = 0; i < n; ++i) printf( %d  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   int n, m;   int k[200010];   int rem[200010];   int idx[200010];   vector<int> special[200010];   vector<int> days[200010];   bool pos(const long long M) {     long long coins = 0;     memset(idx, 0, sizeof(idx));     for (int i = 0; i < n; i++) rem[i] = k[i];     for (int i = 1; i <= min((long long)200000, M); i++) {       coins++;       for (int j = 0; j < int(special[i].size()) && coins; j++) {         int item = special[i][j];         auto it = upper_bound(days[item].begin(), days[item].end(), i);         if (it == days[item].end() || *it > M) {           long long temp = rem[item];           rem[item] -= min((long long)rem[item], coins);           coins -= min(temp, coins);         }       }     }     if (M > 200000) coins += M - 200000;     for (int i = 0; i < n && coins >= 0; i++) {       if (rem[i]) coins -= 2 * rem[i];     }     return coins >= 0;   }   int main() {     scanf( %d %d , &n, &m);     long long sum = 0;     for (int i = 0; i < n; i++) scanf( %d , &k[i]), sum += k[i];     long long L = sum, M, R = 2 * sum, res;     int d, t;     while (m--) {       scanf( %d %d , &d, &t);       special[d].push_back(t - 1), days[t - 1].push_back(d);     }     for (int i = 0; i < n; i++) sort(days[i].begin(), days[i].end());     while (L <= R) {       M = (L + R) / 2;       if (pos(M))         res = M, R = M - 1;       else         L = M + 1;     }     printf( %I64d n , res);     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:200000000 )   using namespace std;   const int INF = (int)1E8;   const long long INF64 = (long long)1E18;   const double EPS = 1E-9;   const double PI = acos((double)0) * 2;   const int MAXN = 160;   int n, cost[MAXN];   string s;   bool read() {     if (!(cin >> n)) return false;     for (int i = 0; i < int(n); ++i) {       cin >> cost[i + 1];       if (cost[i + 1] == -1) cost[i + 1] = -INF;     }     cin >> s;     return true;   }   int d[MAXN][MAXN][MAXN];   bool u[MAXN][MAXN][MAXN];   int get_d(int l, int r, int len) {     if (l > r) return cost[len];     if (l == r) return max(cost[len] + cost[1], cost[len + 1]);     int& my = d[l][r][len];     bool& myu = u[l][r][len];     if (myu) return my;     myu = true;     my = -INF;     if (len != 0) my = max(my, cost[len] + get_d(l, r, 0));     for (int i = int(l); i < int(r); ++i)       my = max(my, get_d(l, i, 0) + get_d(i + 1, r, len));     for (int i = int(l + 1); i < int(r + 1); ++i)       my = max(my, get_d(i, r, 0) + get_d(l, i - 1, len));     for (int i = int(l); i < int(r + 1); ++i)       my = max(my, get_d(l, i - 1, 0) + cost[len + 1] + get_d(i + 1, r, 0));     if (s[l] == s[r]) my = max(my, get_d(l + 1, r - 1, len + 2));     return my;   }   int d2[MAXN];   void solve() {     memset(u, 0, sizeof u);     int x = get_d(0, n - 1, 0);     for (int i = 0; i < int(n); ++i) {       d2[i] = i ? d2[i - 1] : 0;       for (int j = 0; j < int(i + 1); ++j)         d2[i] = max(d2[i], get_d(j, i, 0) + (j ? d2[j - 1] : 0));     }     cout << d2[n - 1] << endl;   }   int main() {     if (!read()) throw;     solve();   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     while (b) {       int tmp = a % b;       a = b;       b = tmp;     }     return a;   }   int main() {     int a, b;     cin >> a >> b;     int k = gcd(a, b);     if (abs(a / k - b / k) <= 1)       cout <<  Equal n ;     else if (a < b)       cout <<  Dasha n ;     else       cout <<  Masha n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long const M = 1000000007;   double const pi = acos(-1);   long long const inf = 2e18;   long long const N = 200001;   long long comb(long long n) {     if (n <= 0) return 0;     return (n * (n - 1)) / 2;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long i, j, t, n, d, ans = 0, curr, prev = 0;     cin >> n >> d;     vector<long long> a(n);     for (i = 0; i < n; i++) cin >> a[i];     for (curr = 2; curr < n;) {       while (curr < n && a[curr] - a[prev] <= d) {         ans += comb(curr - prev);         curr++;       }       if (a[curr] - a[prev] > d) prev++;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   long long int a[1004][1004];   long long int b[1005];   long long int c[1005];   long long int ci, cj;   int main() {     long long int i, j;     scanf( %d %d , &n, &m);     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) scanf( %I64d , &a[i][j]);     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) b[i] += a[i][j];     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) c[j] += a[i][j];     long long int mm = -1, tt;     long long int res = 0;     long long int mi;     for (i = 0; i <= n; i++) {       tt = 0;       for (j = 0; j < n; j++) {         tt += b[j] * (4 * i - 4 * j - 2) * (4 * i - 4 * j - 2);       }       if (mm < 0 || mm > tt) {         mm = tt;         mi = i;       }     }     ci = mi;     res += mm;     mm = -1;     for (i = 0; i <= m; i++) {       tt = 0;       for (j = 0; j < m; j++) {         tt += c[j] * (4 * i - 4 * j - 2) * (4 * i - 4 * j - 2);       }       if (mm < 0 || mm > tt) {         mm = tt;         mi = i;       }     }     cj = mi;     res += mm;     printf( %I64d n , res);     printf( %I64d %I64d n , ci, cj);   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int mod = 1e9 + 7;   long long int powmod(long long int a, long long int b) {     long long int res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   int nxt() {     int x;     scanf( %d , &x);     return x;   }   vector<string> type;   vector<vector<int> > edge;   vector<bool> ans;   void dfs(int u) {     if (type[u] ==  IN ) {       ans[u] = edge[u][0];       return;     }     if (type[u] ==  NOT ) {       dfs(edge[u][0]);       ans[u] = !ans[edge[u][0]];       return;     }     dfs(edge[u][0]);     dfs(edge[u][1]);     if (type[u] ==  AND ) {       ans[u] = ans[edge[u][0]] & ans[edge[u][1]];     }     if (type[u] ==  OR ) {       ans[u] = ans[edge[u][0]] | ans[edge[u][1]];     }     if (type[u] ==  XOR ) {       ans[u] = ans[edge[u][0]] ^ ans[edge[u][1]];     }   }   void dfs2(int u, vector<int>& a) {     if (type[u] ==  IN ) {       a.push_back(u);       return;     }     if (type[u] ==  NOT ) {       dfs2(edge[u][0], a);       return;     }     bool l = ans[edge[u][0]];     bool r = ans[edge[u][1]];     int x = edge[u][0];     int y = edge[u][1];     if (type[u] ==  AND ) {       if (!l && r) {         dfs2(x, a);       }       if (l && !r) {         dfs2(y, a);       }       if (l && r) {         dfs2(x, a);         dfs2(y, a);       }     }     if (type[u] ==  OR ) {       if (!r && !l) {         dfs2(x, a);         dfs2(y, a);       }       if (!l && r) {         dfs2(y, a);       }       if (l && !r) {         dfs2(x, a);       }     }     if (type[u] ==  XOR ) {       dfs2(x, a);       dfs2(y, a);     }   }   int main() {     int n = nxt();     type.resize(n);     edge.resize(n);     ans.resize(n);     vector<int> q;     for (int i = 0; i < n; i++) {       cin >> type[i];       if (type[i] ==  IN ) {         edge[i].push_back(nxt());         q.push_back(i);       } else if (type[i] ==  NOT ) {         edge[i].push_back(nxt() - 1);       } else {         edge[i].push_back(nxt() - 1);         edge[i].push_back(nxt() - 1);       }     }     dfs(0);     vector<int> req;     dfs2(0, req);     set<int> s;     for (int x : req) s.insert(x);     for (int x : q) {       if (s.count(x))         printf( %d , ans[0] ^ 1 ? 1 : 0);       else         printf( %d , ans[0] ? 1 : 0);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using tpl = tuple<int, int, int>;   using pcc = pair<char, char>;   const ll inf = 2e18;   const ll mod = 1e9 + 7;   int dr[] = {-1, 0, 1, 0};   int dc[] = {0, 1, 0, -1};   int kr[] = {-1, 1, -2, -2, -1, 1, 2, 2};   int kc[] = {-2, -2, -1, 1, 2, 2, -1, 1};   int dgr[] = {-1, -1, 1, 1};   int dgc[] = {1, -1, -1, 1};   const int lim = 10000005;   bool isPrime[lim];   ll prime[lim];   ll pal[lim];   bool ok(string str) {     for (int i = str.size() - 1, j = 0; i > j; i--, j++)       if (str[i] != str[j]) return false;     return true;   }   void sieve() {     memset(isPrime, true, sizeof isPrime);     for (int i = 4; i < lim; i += 2) isPrime[i] = false;     for (ll i = 3; i <= lim; i += 2) {       if (isPrime[i]) {         for (ll j = i * i; j < lim; j += i) {           isPrime[j] = false;         }       }     }     pal[1] = 1;     pal[2] = 2;     prime[2] = 1;     ll cnt = 1, cnt2 = 2;     string str, rev;     for (ll i = 3; i < lim; i++) {       if (isPrime[i]) cnt++;       prime[i] = cnt;       str = to_string(i);       if (ok(str)) cnt2++;       pal[i] = cnt2;     }   }   int solve(int p, int q) {     int ans = 0;     for (int i = 1; i <= 10000000; i++) {       if ((q * prime[i]) <= (p * pal[i])) ans = i;     }     return ans;   }   int main() {     int ans = 0, p, q;     sieve();     cin >> p >> q;     cout << solve(p, q) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int w, h;     cin >> w >> h;     int u1, d1, u2, d2;     cin >> u1 >> d1 >> u2 >> d2;     for (int i = h; i >= 0; i--) {       w = w + i;       if (i == d1 || i == d2) {         if (i == d1) {           w = w - u1;           if (w < 0) {             w = 0;           }         } else {           w = w - u2;           if (w < 0) {             w = 0;           }         }       }     }     cout << w << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const int N = 2 * maxn;   int n, m, a[maxn];   int v[N];   void jl(int x) {     for (int i = x; i < N; i += i & -i) {       v[i]++;     }   }   long long s(int x) {     long long sum = 0;     for (int i = x; i > 0; i -= i & -i) {       sum += v[i];     }     return sum;   }   long long cal(int k) {     memset(v, 0, sizeof(v));     jl(n + 1);     int dp = 0;     long long sum = 0;     for (int i = 0; i < n; i++) {       if (a[i] <= k)         dp -= 1;       else         dp += 1;       sum += s(dp + n);       jl(dp + n + 1);     }     return sum;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     cout << cal(m - 1) - cal(m) << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   const int maxn = 1001000;   const int mod = 1000000007;   int a[maxn], f[maxn], r[maxn];   int main() {     int n;     scanf( %d , &n);     for (int e = 1; e <= n; e++) scanf( %d , a + e);     auto mul = [](int x, int y) { return 1ll * x * y % mod; };     f[0] = 1;     for (int e = 1; e <= n; e++) f[e] = mul(e, f[e - 1]);     r[n + 1] = 1;     for (int e = n; e >= 1; e--) r[e] = mul(e, r[e + 1]);     sort(a + 1, a + 1 + n);     int small = 0;     int i = 1, j = 1;     int ans = 0;     while (i <= n) {       while (j <= n && a[i] == a[j]) j++;       if (j == n + 1) break;       ans += mul(j - i, mul(a[i], mul(f[n - small - 1], r[n - small + 1])));       small += j - i;       if (ans >= mod) ans -= mod;       i = j;     }     cout << ans << endl;     return 0;   }