malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   string s[3];   int N, M, p[3][1000005], a[3][1000005], b[1000005];   void genPrefix(int pat) {     int i, k;     p[pat][0] = 0;     k = 0;     i = 1;     while (i < M) {       if (s[pat][i] == s[pat][k]) {         k++;         p[pat][i] = k;         i++;       } else {         if (k != 0)           k = p[pat][k - 1];         else {           p[pat][i] = 0;           i++;         }       }     }   }   void KMPSearch(int txt, int pat) {     int i, k;     genPrefix(pat);     i = k = 0;     while (i < N) {       if (s[txt][i] == s[pat][k]) {         i++;         k++;         a[txt][i - k] = i - 1;       }       if (k == M) {         k = p[pat][k - 1];       } else if (i < N && s[txt][i] != s[pat][k]) {         if (k != 0) {           if (s[txt][i - 1] == s[pat][k])             a[txt][i - k] = max(i - 1, a[txt][k - 1]);           k = p[pat][k - 1];           if (k > 0 && s[txt][i - 1] == s[pat][k])             a[txt][i - k] = max(i - 1, a[txt][k - 1]);         } else           i++;       }     }   }   int bs(int low, int high, int v) {     int mid, x = -1;     while (low <= high) {       mid = (low + high) >> 1;       if (b[mid] >= v) {         x = mid;         high = mid - 1;       } else         low = mid + 1;     }     return x;   }   pair<int, int> solve() {     s[2] = s[0];     reverse(s[2].begin(), s[2].end());     memset(a, -1, sizeof(a));     KMPSearch(1, 2);     KMPSearch(0, 1);     b[0] = a[1][0];     int i, j = N, len, k, x;     int ansx = -1, ansy = -1;     for (i = 1; i <= N - 1; i++) b[i] = max(b[i - 1], a[1][i]);     for (i = 0; i <= N - 2; i++) {       j--;       if (s[0][i] != s[1][j]) break;       if (b[0] >= j - 1) {         ansx = i;         ansy = i + 1;       }       if (a[0][i + 1] <= 0 || i + 1 == N - 1) continue;       len = min(N - 2, a[0][i + 1]) - i;       k = bs(0, len, j - 1);       if (k >= 0 && ansx < i) {         ansx = i;         ansy = i + 1 + k;       }     }     return make_pair(ansx, ansy);   }   int main() {     std::ios_base::sync_with_stdio(0);     int cases, caseno = 0, n, i, j, k, cnt, m;     getline(cin, s[0]);     getline(cin, s[1]);     N = s[0].size();     M = s[1].size();     if (N != M) {       cout <<  -1 -1  <<   n ;       return 0;     }     pair<int, int> ans = solve();     cout << ans.first <<     << ans.second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     scanf( %d%d , &n, &x);     if (n == 1) {       printf( YES n );       printf( %d n , x);     } else if (n == 2) {       if (x == 0)         printf( NO n );       else {         printf( YES n );         printf( 0 %d n , x);       }     } else {       printf( YES n );       int tmp = 0;       int a = (1 << 17);       int b = (1 << 18);       for (int i = 0; i < n - 3; i++) {         printf( %d  , i);         tmp ^= i;       }       printf( %d %d %d n , a, b, a ^ b ^ tmp ^ x);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool psort(std::vector<long long int> p1, std::vector<long long int> p2) {     return p1[0] < p2[0];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, m;     cin >> n;     std::vector<vector<long long int> > v(n, vector<long long int>(3));     for (int i = 0; i < n; ++i) {       cin >> v[i][0];     }     for (int i = 0; i < n; ++i) {       cin >> v[i][1];     }     for (int i = 0; i < n; ++i) {       cin >> v[i][2];     }     sort(v.begin(), v.end(), psort);     cin >> m;     long long int start[4] = {0};     for (int i = 0; i < m; ++i) {       long long int k, t, flag = 0;       cin >> t;       while (start[t] < n) {         if (v[start[t]][0] != -1) {           if (v[start[t]][1] == t || v[start[t]][2] == t) {             cout << v[start[t]][0] <<    ;             v[start[t]][0] = -1;             start[t]++;             flag = 1;             break;           }         }         start[t]++;       }       if (flag == 0) cout << -1 <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string fld;     cin >> fld;     int l = 1, r = 2 * n;     int ans = -1;     while (l <= r) {       int mid = (l + r) >> 1;       int posbl = 1;       int back = -1, flag = 0;       for (int i = 0; i < n; i++) {         if (fld[i] ==  * )           if (i > back && flag == 0) back = i, flag = 1;         if (fld[i] ==  P ) {           if (back == -1) {             back = i + mid;           } else {             if (i <= back) {               back = max(back, i + mid);             } else {               if (flag == 1) {                 if (i - back > mid) {                   posbl = 0;                   break;                 } else if (i - back <= mid && mid <= (2 * (i - back)))                   back = max(i, (mid - (i - back)) / 2 + i);                 else {                   back =                       max(mid - (2 * (i - back)) + i, (mid - (i - back)) / 2 + i);                 }               } else {                 back = i + mid;               }             }           }           flag = 0;         }       }       if (flag == 1) posbl = 0;       if (posbl == 1) {         r = mid - 1;         ans = mid;       } else         l = mid + 1;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   char s[N];   char t1[7] =  twone ;   char t2[4] =  one ;   char t3[4] =  two ;   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %s , s);       int ans = 0;       int len = strlen(s);       for (int i = 0; i < len; i++) {         if (s[i] ==  t ) {           int f = 0;           for (int j = i; j <= i + 4; j++) {             if (s[j] != t1[j - i]) {               f = 1;               break;             }           }           if (f == 0) {             s[i + 2] =  - ;             ans++;           }         }       }       for (int i = 0; i < len; i++) {         if (s[i] ==  t ) {           int f = 0;           for (int j = i; j <= i + 2; j++) {             if (s[j] != t3[j - i]) {               f = 1;               break;             }           }           if (f == 0) {             s[i + 1] =  - ;             ans++;           }         }       }       for (int i = 0; i < len; i++) {         if (s[i] ==  o ) {           int f = 0;           for (int j = i; j <= i + 2; j++) {             if (s[j] != t2[j - i]) {               f = 1;               break;             }           }           if (f == 0) {             s[i + 1] =  - ;             ans++;           }         }       }       printf( %d n , ans);       for (int i = 0; i < len; i++)         if (s[i] ==  - ) printf( %d  , i + 1);       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     long long int n, m = 0, p = 0, cnt = 0, f = 0;     cin >> n;     long long int a[n];     for (long long int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] < 0) {         m++;       }       if (a[i] > 0) p++;       if (a[i] < 0) {         cnt = cnt - a[i] - 1;       }       if (a[i] > 0) {         cnt = cnt + a[i] - 1;       }       if (a[i] == 0) {         cnt = cnt + 1;         f++;       }     }     if (f and m % 2 == 0) cout << cnt << endl;     if (f and m % 2 != 0) cout << cnt << endl;     if (!f and m % 2 == 0) cout << cnt << endl;     if (!f and m % 2 != 0) cout << cnt + 2 << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int A[600006];   vector<int> G[600006];   int dfn[600006], bc[600006], clo;   int g[600006][20], dep[600006];   void dfs(int u) {     dfn[u] = ++clo, bc[clo] = u;     if (u == 1) dep[u] = 1;     for (int v : G[u]) {       g[v][0] = u;       dep[v] = dep[u] + 1;       for (int k = (1), kend = (18); k <= kend; ++k)         if (g[g[v][k - 1]][k - 1])           g[v][k] = g[g[v][k - 1]][k - 1];         else           break;       dfs(v);     }   }   int lca(int u, int v) {     if (dep[u] < dep[v]) swap(u, v);     if (dep[u] != dep[v])       for (int k = (18), kend = (0); k >= kend; --k)         if (dep[g[u][k]] >= dep[v]) u = g[u][k];     if (u == v) return u;     for (int k = (18), kend = (0); k >= kend; --k)       if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k];     return g[u][0];   }   int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }   set<int> S;   void solve() {     cin >> n;     ++n;     for (int i = (2), iend = (n); i <= iend; ++i) {       int p;       scanf( %d , &p);       G[p].push_back(i);     }     dfs(1);     int s = 1, t = 1, lt = 0;     auto wkr = [&](int x) { return max(dis(x, s), dis(x, t)); };     S.insert(1);     for (int i = (2), iend = (n); i <= iend; ++i) {       int is = dis(i, s), it = dis(i, t), flg = 0;       if (is >= lt && is >= it)         t = i, lt = is, flg = 1;       else if (it >= lt && it >= is)         s = i, lt = it, flg = 1;       if (!flg) {         printf( %d n , (int)S.size());         continue;       }       auto tr = S.lower_bound(dfn[i]), st = tr;       while (tr != S.end()) {         if (wkr(bc[*tr]) < lt)           ++tr;         else           break;       }       if (tr == S.end()) {         auto sr = S.begin();         while (sr != st) {           if (wkr(bc[*sr]) < lt)             ++sr;           else             break;         }         S.erase(S.begin(), sr);       }       S.erase(st, tr);       tr = S.lower_bound(dfn[i]), st = tr;       while (tr != S.begin()) {         --tr;         if (wkr(bc[*tr]) >= lt) {           ++tr;           break;         }       }       if (tr == S.begin()) {         auto sr = S.end();         while (sr != st) {           --sr;           if (wkr(bc[*sr]) >= lt) {             ++sr;             break;           }         }         S.erase(sr, S.end());       }       S.erase(tr, st);       S.insert(dfn[i]);       printf( %d n , (int)S.size());     }   }   signed main() { solve(); }
#include <bits/stdc++.h>   using namespace std;   struct fenwick {     long long n, fen[100005];     void resize(int _n) {       n = _n + 1;       memset(fen, 0, sizeof fen);     }     int query(int ind) {       ind++;       int ans = 0;       while (ind >= 1) {         ans += fen[ind];         ind -= ind & -ind;       }       return ans;     }     void update(int ind, int val) {       ind++;       while (ind <= n) {         fen[ind] += val;         ind += ind & -ind;       }     }     int query(int l, int r) { return query(r) - query(l - 1); }   };   const int maxn = 100001;   int n, l, w, x, y, sz[maxn];   long long res;   bool blocked[maxn];   vector<pair<int, int> > c, v[maxn];   fenwick tree;   void dfs(int node, int pnode, int curw, int curl) {     c.push_back(make_pair(curw, curl));     for (auto &i : v[node])       if (i.first != pnode && !blocked[i.first])         dfs(i.first, node, curw + i.second, curl + 1);   }   long long solve(int node, int curw, int curl) {     long long ret = 0;     c.clear();     dfs(node, 0, curw, curl);     sort(c.begin(), c.end());     for (auto &i : c) tree.update(i.second, 1);     for (int i = 0, j = c.size() - 1; i < c.size(); i++) {       while (i <= j && c[i].first + c[j].first > w)         tree.update(c[j--].second, -1);       if (j < i) break;       tree.update(c[i].second, -1);       ret += tree.query(l - c[i].second);     }     return ret;   }   int dfsz(int node, int pnode) {     sz[node] = 1;     for (auto &i : v[node])       if (i.first != pnode && !blocked[i.first]) sz[node] += dfsz(i.first, node);     return sz[node];   }   int find(int node, int pnode, int n) {     for (auto &i : v[node])       if (i.first != pnode && !blocked[i.first] && 2 * sz[i.first] > n)         return find(i.first, node, n);     return node;   }   void decompose(int node) {     int centroid = find(node, 0, dfsz(node, 0));     blocked[centroid] = 1;     res += solve(centroid, 0, 0);     for (auto &i : v[centroid])       if (!blocked[i.first]) res -= solve(i.first, i.second, 1);     for (auto &i : v[centroid])       if (!blocked[i.first]) decompose(i.first);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> l >> w;     tree.resize(n + 1);     for (int i = 2; i <= n && cin >> x >> y; i++) {       v[i].push_back(make_pair(x, y));       v[x].push_back(make_pair(i, y));     }     decompose(1);     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   const int maxn = 1e5 + 10;   const int N = 5e2 + 10;   using namespace std;   long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); }   long long qpow(long long p, long long q) {     long long f = 1;     while (q) {       if (q & 1) f = f * p % 1000000007;       p = p * p % 1000000007;       q >>= 1;     }     return f;   }   int n, m, k, t;   vector<int> d[4][11][10];   char a[maxn], b[maxn];   int gao(char p) {     if (p ==  A )       return 0;     else if (p ==  T )       return 1;     else if (p ==  G )       return 2;     else       return 3;   }   void add(vector<int>& d, int x, int y) {     while (x < d.size()) d[x] += y, x += x & (-x);   }   int get(vector<int>& d, int x) {     int ret = 0;     while (x) ret += d[x], x -= x & (-x);     return ret;   }   int main() {     int i, j;     scanf( %s , a + 1);     n = strlen(a + 1);     for (i = 0; i <= (int)3; i++)       for (j = 1; j <= (int)10; j++)         for (k = 0; k <= (int)j - 1; k++) d[i][j][k].resize((n + j - 1) / j + 1);     for (i = 1; i <= n; i++) {       for (j = 1; j <= 10; j++) {         add(d[gao(a[i])][j][(i - 1) % j], (i - 1) / j + 1, 1);       }     }     int q;     scanf( %d , &q);     while (q--) {       int op, x, y;       scanf( %d%d , &op, &x);       if (op == 1) {         scanf( %s , b);         for (i = 1; i <= (int)10; i++)           add(d[gao(a[x])][i][(x - 1) % i], (x - 1) / i + 1, -1);         for (i = 1; i <= (int)10; i++)           add(d[gao(b[0])][i][(x - 1) % i], (x - 1) / i + 1, 1);         a[x] = b[0];       } else {         scanf( %d%s , &y, b);         int len = strlen(b), ret = 0;         for (i = 0; i < len && i < y - x + 1; i++) {           int mo = (i + x - 1) % len;           ret += get(d[gao(b[i])][len][mo], (y - 1 - mo) / len + 1) -                  get(d[gao(b[i])][len][mo], (x - 1 + len - 1 - mo) / len);         }         printf( %d n , ret);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, j, t;     cin >> t;     for (long long int aa = 0; aa < t; aa++) {       long long int n, k, d;       cin >> n >> k >> d;       long long int mini = k;       vector<long long int> v(n);       map<long long int, long long int> m;       for (i = 0; i < n; i++) {         cin >> v[i];         if (i < d) {           long long int ss = m.size();           m[v[i]]++;           if (ss < m.size()) m[v[i]] = 1;         }       }       m.size() < mini ? mini = m.size() : mini = mini;       for (i = 1; i < n - d + 1; i++) {         m[v[i - 1]]--;         if (m[v[i - 1]] == 0) m.erase(v[i - 1]);         long long int ss = m.size();         m[v[i + d - 1]]++;         if (ss < m.size()) m[v[i + d - 1]] = 1;         m.size() < mini ? mini = m.size() : mini = mini;       }       m.size() < mini ? mini = m.size() : mini = mini;       cout << mini << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int *p = new int[n + 1], c = 0;     int *m = new int[n + 1];     for (int i = 0; i <= n; i++) {       p[i] = m[i] = 0;       c ^= i;       m[i] = c;     }     int f = 0;     for (int i = 1; i <= n; i++) {       cin >> p[i];       f ^= p[i];     }     int r = 0;     for (int i = 2; i <= n; i++) {       int grps = (n / i);       int rem = n % i;       if (grps & 1) r ^= m[i - 1];       r ^= m[rem];     }     cout << (r ^ f);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   long long a[maxn], b[maxn], dp[maxn];   int q[maxn];   int head = 0, tail = 0;   double A(int i, int j) { return dp[i] - dp[j]; }   double B(int i, int j) { return b[i] - b[j]; }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     dp[1] = 0;     int head = 1, tail = 1;     q[tail] = 1;     for (int i = 2; i <= n; i++) {       while (head < tail &&              A(q[head + 1], q[head]) / B(q[head + 1], q[head]) >= -a[i])         head++;       dp[i] = dp[q[head]] + b[q[head]] * a[i];       while (head < tail && A(i, q[tail]) / B(i, q[tail]) >=                                 A(q[tail], q[tail - 1]) / B(q[tail], q[tail - 1]))         tail--;       q[++tail] = i;     }     cout << dp[n] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_INT = numeric_limits<int>::max();   const int MAX_UINT = numeric_limits<unsigned int>::max();   void get_acum(const vector<int>& v, vector<int>& acum) {     int sum = 0;     for (unsigned int i = 0; i < v.size(); ++i) {       acum.push_back(sum);       sum += v[i];     }     acum.push_back(sum);   }   int solve(vector<int>& A, vector<int>& B, vector<int>& X, unsigned int k) {     if (A.size() + X.size() < k || B.size() + X.size() < k) {       return -1;     } else {       sort(A.begin(), A.end());       sort(B.begin(), B.end());       sort(X.begin(), X.end());       vector<int> A_acum, B_acum, X_acum;       get_acum(A, A_acum);       get_acum(B, B_acum);       get_acum(X, X_acum);       int res = MAX_INT;       unsigned int max_i = min((unsigned int)X.size(), k);       for (unsigned int i = 0; i < max_i + 1; ++i) {         if (k - i < A_acum.size() && k - i < B_acum.size()) {           int total = A_acum[k - i] + B_acum[k - i] + X_acum[i];           if (total < res) res = total;         }       }       return res;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     unsigned int n, k;     cin >> n >> k;     vector<int> A;     vector<int> B;     vector<int> X;     for (unsigned int i = 0; i < n; ++i) {       unsigned int ti;       bool ai, bi;       cin >> ti >> ai >> bi;       if (ai && bi)         X.push_back(ti);       else if (ai)         A.push_back(ti);       else if (bi)         B.push_back(ti);     }     cout << solve(A, B, X, k);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<long long int> adj[3005];   map<long long int, long long int> mq;   long long int done[3005][3005];   int main() {     ios_base::sync_with_stdio(false);     long long int n, m, i, ans = 0;     cin >> n >> m;     for (i = 0; i < m; i++) {       long long int x, y;       cin >> x >> y;       adj[x].insert(y);     }     for (i = 1; i <= n; i++) {       for (auto it : adj[i]) {         for (auto it2 : adj[it]) {           if (mq.find(it2) != mq.end() && it2 != i) {             ans += mq[it2];           }           mq[it2]++;         }       }       mq.clear();     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     int a, b, h;     cin >> a >> b >> h;     int c = 0;     while (h != 0) {       if (c % 2 == 0) {         h = h - gcd(a, h);       } else {         h = h - gcd(b, h);       }       c++;     }     if (c % 2 == 0) {       cout << 1 << endl;     } else       cout << 0 << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 5;   const double EPS = 1e-3;   int n;   int t[MAXN];   int g(double q) {     bool ok = true;     for (int i = 1; i < n; i++)       if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false;     if (ok) return 0;     ok = true;     for (int i = 2; i < n; i++)       if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false;     if (ok) return 1;     ok = true;     int prev = t[0];     for (int i = 1; i < n; i++)       if (fabs(double(t[i] - prev * q)) > EPS) {         if (!ok) return 2;         ok = false;       } else         prev = t[i];     return 1;   }   int f() {     if (n == 1) return 0;     double q1 = 0, q2 = 0;     if (t[0] != 0) q1 = double(t[1]) / t[0];     if (t[n - 2] != 0) q2 = double(t[n - 1]) / t[n - 2];     return min(g(q1), g(q2));   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &t[i]);     printf( %d n , f());   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e6 + 5;   int a[MAX], run[MAX], ans[MAX];   char s[MAX];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     scanf( %d , &n);     scanf( %s , s);     for (int i = 0; i < n; ++i) {       if (s[i] ==  0 )         a[i] = 0;       else if (s[i] ==  1 )         a[i] = 1;       else         a[i] = -1;     }     vector<pair<int, int>> blocks;     int it1 = 0, it2 = 0;     while (it1 != n) {       while (it2 < n - 1 && a[it1] == a[it2 + 1]) ++it2;       blocks.emplace_back(it2 - it1 + 1, a[it1]);       ++it2;       it1 = it2;     }     for (int i = 0; i < (int)blocks.size(); ++i) {       if (blocks[i].second != -1) continue;       if (i == 0)         blocks[i].second = blocks[i + 1].second;       else if (i == (int)blocks.size() - 1)         blocks[i].second = blocks[i - 1].second;       else if (blocks[i - 1].second == blocks[i + 1].second)         blocks[i].second = blocks[i - 1].second;     }     vector<pair<int, int>> compress;     it1 = 0;     it2 = 0;     while (it1 != (int)blocks.size()) {       int len = blocks[it1].first;       while (it2 < (int)blocks.size() - 1 &&              blocks[it1].second == blocks[it2 + 1].second)         it2++, len += blocks[it2].first;       compress.emplace_back(len, blocks[it1].second);       ++it2;       it1 = it2;     }     for (auto block : compress) {       if (block.second == -1) continue;       for (int len = 1; len <= block.first; len++) ans[len] += block.first / len;     }     int max_size = -1, bef = -2;     int size = compress.size();     for (int i = 0; i < size; i++) {       int len = compress[i].first;       int type = compress[i].second;       if (bef != -1 && type != -1) {         max_size = len;         if (i != size - 1) max_size += compress[i + 1].first;         for (int l = 1; l <= max_size; l++) run[l] = len % l;       } else if (type == -1) {         for (int l = 1; l <= max_size; l++) {           if (run[l] + len >= l)             run[l] += len, ans[l] += run[l] / l, run[l] %= l;           else             run[l] = len;         }         int add = 0;         if (i != (int)size - 1) add += compress[i + 1].first;         if (i != (int)size - 2) add += compress[i + 2].first;         for (int l = max_size + 1; l <= len + add; ++l) run[l] = (len % l);         max_size = len + add;       } else {         for (int l = 1; l <= max_size; ++l) {           if (run[l] + (len % l) >= l)             run[l] += ((len % l) - l), ans[l]++;           else             run[l] += (len % l);         }       }       bef = type;     }     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int a[m], b[m];     int ndup = n;     for (int i = 0; i < m; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(a, a + m);     sort(b, b + m, greater<int>());     int mn = 0, mx = 0;     for (int i = 0; i < m; i++) {       while (a[i] > 0 && n > 0) {         mn += a[i];         a[i]--;         n--;       }     }     while (ndup > 0) {       mx += b[0];       b[0]--;       ndup--;       sort(b, b + m, greater<int>());     }     cout << mx <<     << mn;   }