malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long MAXN = 2e5 + 7;   long long a[MAXN];   bool good[MAXN];   long long want[MAXN], pref[MAXN], next_[MAXN];   signed main() {     ios_base::sync_with_stdio(false);     cout.precision(40);     long long n;     cin >> n;     for (long long i = 0; i < n; ++i) {       cin >> a[i];     }     stack<long long> st;     for (long long i = 0; i < n; ++i) {       while (st.size() && a[st.top()] < a[i]) {         next_[st.top()] = i;         st.pop();       }       st.push(i);     }     while (st.size()) {       next_[st.top()] = n;       st.pop();     }     long long mx1 = -MOD;     long long mx2 = -MOD;     for (long long i = 0; i < n; ++i) {       if (a[i] < mx1 && a[i] > mx2) {         want[i] = true;       }       if (a[i] > mx1) {         mx2 = mx1;         mx1 = a[i];       } else if (a[i] > mx2) {         mx2 = a[i];       }     }     pref[0] = want[0];     for (long long i = 1; i < n; ++i) {       pref[i] = pref[i - 1] + want[i];     }     long long mx = -MOD;     long long ans = -MOD;     long long best = -1;     for (long long i = 0; i < n; ++i) {       mx = max(mx, a[i]);       if (a[i] != mx) continue;       long long j = next_[i];       long long new_ans = pref[j - 1] - pref[i] - 1;       if (new_ans > ans || (new_ans == ans && a[i] < a[best])) {         ans = new_ans;         best = i;       }     }     mx = -MOD;     for (long long i = 0; i < n; ++i) {       mx = max(mx, a[i]);       if (a[i] == mx) continue;       long long new_ans = 0;       if (new_ans > ans || (new_ans == ans && a[i] < a[best])) {         ans = new_ans;         best = i;       }     }     cout << a[best] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       long long sum = 0;       for (long long i = 0; i < n; i++) {         long long x;         cin >> x;         sum += x;       }       cout << min(sum, m) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int x, y;   int p[1000003];   int r[1000003];   map<int, int> a;   int ans[200003];   int s;   int parent(int x) {     if (p[x] < 0) return x;     return p[x] = parent(p[x]);   }   void join(int x, int y) { p[y] = parent(x); }   int main() {     fill_n(p, 1000003, -1);     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x, &y);       if (y) join(x, y);       a.insert(pair<int, int>(x, y));     }     int i = 2;     int j = 0;     while (a[j] != 0) {       ans[i] = a[j];       j = a[j];       i += 2;     }     i = 1;     j = 0;     while (p[j] < 0 || parent(j) == 0) j++;     j = parent(j);     ans[i] = j;     i += 2;     while (a[j] != 0) {       ans[i] = a[j];       j = a[j];       i += 2;     }     for (i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long C(int a, int b) {     long long ans = 1;     for (int i = 0; i < b; ++i) ans *= (a - i), ans /= (i + 1);     return ans;   }   int main() {     int n;     while (cin >> n) cout << C(n + 4, 5) * C(n + 2, 3) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 10;   struct Seg {     long long mn, id;   };   long long arr[maxn], N, P, ans[maxn];   Seg seg[maxn << 2];   inline Seg pushup(long long cur) {     Seg l = seg[cur << 1], r = seg[cur << 1 | 1];     if (l.mn == r.mn) {       return l;     } else if (l.mn < r.mn) {       Seg mer;       mer.id = l.id, mer.mn = l.mn;       return mer;     } else {       Seg mer;       mer.id = r.id, mer.mn = r.mn;       return mer;     }   }   long long query(long long cur, long long L, long long R, long long l,                   long long r) {     if (L > R) return -1;     if (l >= L && r <= R) {       return seg[cur].id;     }     long long mid = (l + r) >> 1;     long long lid = -1, rid = -1;     if (mid >= L) lid = query(cur << 1, L, R, l, mid);     if (mid < R) rid = query(cur << 1 | 1, L, R, mid + 1, r);     if (lid == -1)       return rid;     else if (rid == -1)       return lid;     else {       return arr[lid] > arr[rid] ? rid : lid;     }   }   long long query2(long long cur, long long l, long long r, const long long x) {     if (l == r) {       return l;     }     long long mid = (l + r) >> 1;     if (seg[cur << 1].mn <= x) {       return query2(cur << 1, l, mid, x);     }     return query2(cur << 1 | 1, mid + 1, r, x);   }   void build(long long l, long long r, long long cur) {     seg[cur].id = l, seg[cur].mn = arr[l];     if (l == r) return;     long long mid = (l + r) >> 1;     build(l, mid, cur << 1);     build(mid + 1, r, cur << 1 | 1);     seg[cur] = pushup(cur);   }   void update(long long pos, long long l, long long r, long long cur,               long long x) {     if (l == r) {       seg[cur].mn = x;       arr[pos] = x;       return;     }     long long mid = (l + r) >> 1;     if (mid >= pos) {       update(pos, l, mid, cur << 1, x);     } else {       update(pos, mid + 1, r, cur << 1 | 1, x);     }     seg[cur] = pushup(cur);   }   signed main() {     ios::sync_with_stdio(false);     cin >> N >> P;     for (register long long i = 1; i <= N; ++i) {       cin >> arr[i];     }     build(1, N, 1);     long long cnt = 0, cur = 0;     while (cnt < N) {       cur = max(cur, seg[1].mn);       long long x = query2(1, 1, N, cur);       ans[x] = cur += P;       ++cnt;       update(x, 1, N, 1, 0x3f3f3f3f3f3f3f3f);       while (cnt < N) {         x = query(1, 1, x - 1, 1, N);         if (x == -1 || arr[x] > cur) break;         ans[x] = cur += P, ++cnt;         update(x, 1, N, 1, 0x3f3f3f3f3f3f3f3f);       }     }     for (register long long i = 1; i <= N; ++i) {       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int count = 0;     while ((count * (count + 1)) / 2 <= n) {       ++count;     }     --count;     int val = (count * (count + 1)) / 2;     int diff = n - val;     cout << count <<   n ;     for (int i = 1; i <= count; ++i) {       if (i == count) {         cout << count + diff <<    ;         break;       }       cout << i <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int t;   int n;   int a[100086];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       int x = 0, y = a[1];       for (int i = 2; i <= n; i++) {         if (a[i] > a[i - 1]) {           x += a[i] - a[i - 1];         } else {           y -= a[i - 1] - a[i];         }       }       puts(y >= 0 ?  YES  :  NO );     }   }
#include <bits/stdc++.h>   using namespace std;   int k, b, n;   map<int, int>::iterator it;   int main() {     map<int, int> mp;     while (scanf( %d%d%d , &k, &b, &n) != EOF) {       mp.clear();       if (b == 0) {         int a, sum = 0;         long long res = 0;         mp[0] = 1;         for (int i = 1; i <= n; i++) {           scanf( %d , &a);           if (a) sum++;           if (mp.find(sum) == mp.end())             mp[sum] = 1;           else             mp[sum]++;         }         for (it = mp.begin(); it != mp.end(); it++) {           long long tmp = it->second;           res += tmp * (tmp - 1) / 2;         }         cout << res << endl;       } else {         int a, ss = 0;         long long res = 0, pre = 0;         mp[0] = 1;         for (int i = 1; i <= n; i++) {           scanf( %d , &a);           if (a) {             ss += a;             ss %= (k - 1);             int ff = (ss - b + k - 1) % (k - 1);             int st = mp.find(ff) == mp.end() ? 0 : mp[ff];             pre = st;             res += st;             if (mp.find(ss) == mp.end())               mp[ss] = 1;             else               mp[ss]++;           } else {             res += pre;             mp[ss]++;           }         }         cout << res << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int a[N], cnt[N * 2], n, m;   long long solve(int m) {     int s = n;     memset(cnt, 0, sizeof(cnt));     cnt[s] = 1;     long long add = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (a[i] >= m)         add += cnt[s], s++;       else         s--, add -= cnt[s];       cnt[s]++;       ans += add;     }     return ans;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     printf( %lld n , solve(m) - solve(m + 1));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long t1, t2, x1, x2, t;   bool Check(long long y1, long long y2) {     return t1 * y1 + t2 * y2 >= t * (y1 + y2);   }   double HotBath(long long y1, long long y2) {     return 1.0 * (t1 * y1 + t2 * y2) / (y1 + y2);   }   int main() {     cin >> t1 >> t2 >> x1 >> x2 >> t;     long long Y1 = 0, Y2 = 0;     if (HotBath(x1, x2) >= t) Y1 = x1, Y2 = x2;     if (HotBath(x1, 0) >= t && x1 >= Y1 + Y2) Y1 = x1, Y2 = 0;     if (HotBath(0, x2) >= t && x2 >= Y1 + Y2) Y1 = 0, Y2 = x2;     for (long long y1 = 0; y1 <= x1; y1++) {       long long y2 = 0;       long long Start = 0, End = x2;       while (Start <= End) {         long long Mid = Start + End >> 1;         if (Check(y1, Mid)) {           y2 = Mid;           End = Mid - 1;         } else           Start = Mid + 1;       }       if (y1 + y2 == 0) continue;       double New = HotBath(y1, y2);       double Old = HotBath(Y1, Y2);       if (New >= 1.0 * t) {         if (New < Old) {           Old = New;           Y1 = y1;           Y2 = y2;         } else if (fabs(New - Old) <= 1e-9 && (Y1 + Y2 < y1 + y2)) {           Y1 = y1;           Y2 = y2;         }       }     }     cout << Y1 <<     << Y2 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 2000 + 5;   int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt;   vector<int> adj[maxN];   vector<int> s;   void DFS(int v) {     s.push_back(v);     on[v] = mark[v] = 1;     st[v] = high[v] = start++;     for (int i = 0; i < adj[v].size(); i++) {       int x = adj[v][i];       if (mark[x]) {         if (on[x]) high[v] = min(high[v], high[x]);       } else {         DFS(x);         high[v] = min(high[v], high[x]);       }     }     if (st[v] == high[v]) {       int x = s.back();       while (!s.empty() && st[x] >= st[v]) {         on[x] = 0;         s.pop_back();         x = s.back();       }       cnt++;     }   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int x;         cin >> x;         if (x) adj[i].push_back(j);       }     }     for (int i = 0; i < n; i++)       if (!mark[i]) DFS(i);     if (cnt == 1)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int w, h, ring, plate = 0;     cin >> w >> h >> ring;     for (int i = 0; i < ring; i++) {       if (i > 0) {         w -= 4;         h -= 4;       }       plate += (w * 2) + ((h - 2) * 2);     }     cout << plate;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long b, ans;   int main(void) {     cin >> b;     for (long long i = 1; i * i <= b; i++) {       if (b % i == 0) {         ans = ans + 2;       }       if (i * i == b) ans--;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, q, i, num, x, y;     cin >> n >> q;     for (i = 0; i < q; i++) {       cin >> x >> y;       if ((x + y) % 2 == 0) {         x--;         num = (n / 2 + n % 2) * (x / 2 + x % 2) + (n / 2) * (x / 2) +               (y / 2 + y % 2);       } else {         x--;         num = (n / 2 + n % 2) * (x / 2) + (n / 2) * (x / 2 + x % 2) +               (y / 2 + y % 2) + (n * n) / 2 + (n * n) % 2;       }       cout << num << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = 1e18;   const string nl =   n ;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(nullptr);     long long n, k, second, t;     cin >> n >> k >> second >> t;     vector<pair<long long, long long>> a(n);     for (auto& i : a) {       cin >> i.first >> i.second;     }     vector<long long> b(k);     for (auto& i : b) {       cin >> i;     }     sort(b.begin(), b.end());     vector<long long> temp = {0};     for (auto& i : b) {       temp.push_back(i);     }     temp.push_back(second);     b = temp;     auto check = [&](long long x) -> bool {       long long ans = 0;       for (long long i = 0; i < ((long long)(b).size()); ++i) {         long long dist = b[i] - (i == 0 ? 0 : b[i - 1]);         long long hld = min(x - dist, dist);         if (hld < 0) {           return 0;         }         ans += hld + (dist - hld) * 2;       }       return ans <= t;     };     long long l = 0, r = 2e9;     while (l < r) {       long long mid = (l + r) / 2;       if (check(mid)) {         r = mid;       } else {         l = mid + 1;       }     }     auto cmp = [&](pair<long long, long long> a,                    pair<long long, long long> b) -> bool {       return a.second < b.second;     };     sort(a.begin(), a.end(), cmp);     long long ans = INF;     for (long long i = 0; i < ((long long)(a).size()); ++i) {       if (a[i].second >= l) {         ans = min(ans, a[i].first);       }     }     cout << (ans == INF ? -1 : ans) << nl;   }
#include <bits/stdc++.h>   using namespace std;   const int oo = (int)1e9;   const double PI = 2 * acos(0.0);   const double eps = 1e-9;   int main() {     ios_base::sync_with_stdio(false);     int n, arra[100010], arrb[100010];     cin >> n;     for (int i = 0; i < (int)(n); i++) {       cin >> arra[i];       arrb[i] = arra[i];     }     sort(arrb, arrb + n);     int cnt = 0;     for (int i = 0; i < (int)(n); i++)       if (arra[i] != arrb[i]) cnt++;     if (cnt == 2 || cnt == 0)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0;     char ch = getchar();     bool positive = 1;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) positive = 0;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return positive ? x : -x;   }   int N, K;   int a[105], b[105], s[105];   int t[105];   inline void trans_one(int p[]) {     for (int i = 1; i <= N; ++i) t[i] = p[a[i]];     for (int i = 1; i <= N; ++i) p[i] = t[i];   }   inline void trans_two(int p[]) {     for (int i = 1; i <= N; ++i) t[a[i]] = p[i];     for (int i = 1; i <= N; ++i) p[i] = t[i];   }   inline bool equal(int a[], int b[]) {     for (int i = 1; i <= N; ++i)       if (a[i] != b[i]) return 0;     return 1;   }   int main() {     N = read(), K = read();     for (int i = 1; i <= N; ++i) a[i] = read();     for (int i = 1; i <= N; ++i) s[i] = read();     for (int i = 1; i <= N; ++i) b[i] = i;     if (equal(b, s)) return printf( NO n ), 0;     int l = 1 << 30, r = 1 << 30;     for (int i = 1; i <= K + 1; ++i) {       trans_one(b);       if (equal(b, s)) {         r = i;         break;       }     }     for (int i = 1; i <= N; ++i) b[i] = i;     for (int i = 1; i <= K + 1; ++i) {       trans_two(b);       if (equal(b, s)) {         l = i;         break;       }     }     if (l % 2 == K % 2 && K >= l) {       if (l > 1 || r > 1 || K - l == 0) return printf( YES n ), 0;     }     if (r % 2 == K % 2 && K >= r) {       if (r > 1 || l > 1 || K - r == 0) return printf( YES n ), 0;     }     printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7, MAX = 1e18;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n0, n1, n2, i;       cin >> n0 >> n1 >> n2;       string second =   ;       if (n1) {         if (n1 % 2) {           for (i = 0; i <= n1; i++) {             if (i % 2)               second.push_back( 0 );             else               second.push_back( 1 );           }           while (n2--) cout << 1;           cout << second;           while (n0--) cout << 0;           cout <<   n ;         } else {           for (i = 1; i <= n1; i++) {             if (i % 2)               second.push_back( 0 );             else               second.push_back( 1 );           }           while (n2--) cout << 1;           cout << 1;           while (n0--) cout << 0;           cout << second <<   n ;         }       } else {         if (n2)           for (i = 0; i <= n2; i++) cout << 1;         if (n0)           for (i = 0; i <= n0; i++) cout << 0;         cout <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int l[123], r[123];   int d[123];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     l[0] = 0;     for (int i = 1; i < n; ++i) {       int x;       cin >> x;       l[i] = l[i - 1] + x;     }     r[n - 1] = 0;     vector<int> a;     for (int i = 0; i < (int)n - 1; ++i) {       int x;       cin >> x;       a.push_back(x);     }     int k = n - 2;     for (int i = n - 2; i >= 0; --i) {       r[i] = r[i + 1] + a[k];       k--;     }     for (int i = 0; i < (int)n; ++i) cin >> d[i];     int res[123];     for (int i = 0; i < (int)n; ++i) res[i] = d[i] + l[i] + r[i];     sort(res, res + n);     cout << res[0] + res[1];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool mmax(T &m, const T q) {     if (m < q) {       m = q;       return true;     } else       return false;   }   template <typename T>   bool mmin(T &m, const T q) {     if (m > q) {       m = q;       return true;     } else       return false;   }   const int MOD = 1e9 + 7;   const int MAXN = 5e5 + 3;   const int INF = 1e9;   int main(int argc, char **argv) {     ios::sync_with_stdio(false);     cin.tie(0);     int ntest;     cin >> ntest;     while (ntest--) {       long long x, y, a, b;       cin >> x >> y >> a >> b;       long long ans = a * (abs(x) + abs(y));       mmin(ans, b * abs(x) + a * abs(y - x));       mmin(ans, b * abs(y) + a * abs(x - y));       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int lazy[maxn << 2];   int a[maxn], c[maxn];   void pushup(int l, int r, int rt) {     lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt];     lazy[rt] = 0;   }   void update(int L, int R, int c, int l, int r, int rt) {     if (L <= l && R >= r) {       lazy[rt] = c;       return;     }     if (lazy[rt]) pushup(l, r, rt);     if (L <= ((l + r) >> 1)) update(L, R, c, l, ((l + r) >> 1), rt << 1);     if (R > ((l + r) >> 1)) update(L, R, c, ((l + r) >> 1) + 1, r, rt << 1 | 1);   }   void query(int l, int r, int rt) {     if (l == r) {       c[l] = lazy[rt];       return;     }     if (lazy[rt]) pushup(l, r, rt);     query(l, ((l + r) >> 1), rt << 1);     query(((l + r) >> 1) + 1, r, rt << 1 | 1);   }   struct T {     int l, r;   } b[maxn];   int main() {     map<int, int> mp;     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) {       if (a[i] == 0) continue;       if (mp[a[i]])         b[a[i]].r = i;       else         mp[a[i]] = 1, b[a[i]].l = i, b[a[i]].r = i;     }     for (int i = 1; i <= k; i++) {       if (mp[i]) {         update(b[i].l, b[i].r, i, 1, n, 1);       }     }     query(1, n, 1);     bool d = true;     for (int i = 1; i <= n; i++) {       if (c[i] != a[i] && a[i] != 0) d = false;     }     if (!d) {       cout <<  NO  << endl;       return 0;     }     bool ff = true;     for (int i = 1; i <= n; i++) {       if (a[i] == k) ff = false;     }     if (ff) {       bool dd = false;       for (int i = 1; i <= n; i++)         if (a[i] == 0) {           dd = true;           c[i] = k;           break;         }       if (!dd) {         cout <<  NO  << endl;         return 0;       }     }     cout <<  YES  << endl;     for (int i = 1; i <= n; i++) {       if (a[i] == 0 && c[i] == 0) {         c[i] = 1;       }       cout << c[i] <<    ;     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, x;     scanf( %d , &n);     priority_queue<int, vector<int>, greater<int> > que;     long long ans = 0;     for (int i = 0; i < n; i++) {       scanf( %d , &x);       if (que.empty() || que.top() > x)         que.push(x);       else {         ans += x - que.top();         que.pop();         que.push(x);         que.push(x);       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rndll(chrono::steady_clock::now().time_since_epoch().count());   const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;   const int N = 2e5 + 5;   int n, q, a, b, cnt, k, par[N];   vector<int> adj[N], v;   pair<int, int> rng[N];   void dfs(int u, int p) {     par[u] = (p != -1 ? p : u);     rng[u] = {INF, -INF};     for (auto v : adj[u])       if (v != p) {         dfs(v, u);         rng[u].first = min(rng[u].first, rng[v].first);         rng[u].second = max(rng[u].second, rng[v].second);       }     if (rng[u].first == INF) cnt++, rng[u] = {cnt, cnt};   }   bool query(vector<int> &v) {     int l = -INF, r = INF;     for (auto x : v)       l = max(l, rng[par[x]].first), r = min(r, rng[par[x]].second);     return l <= r;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     cin >> n >> q;     for (int i = 0; i < n - 1; i++) {       cin >> a >> b;       adj[a].push_back(b), adj[b].push_back(a);     }     dfs(1, -1);     while (q--) {       cin >> k;       v.resize(k);       for (int i = 0; i < k; i++) cin >> v[i];       cout << (query(v) ?  YES n  :  NO n );     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     vector<int> v;     cin >> n;     int x = 1;     for (int i = 1; i <= n; i++) {       int d;       cin >> d;       if (d == x) {         v.push_back(i + 2000);         x++;       }     }     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 7;   const int inf = 0x3f3f3f3f;   const int mod = 1e9 + 7;   long long ans;   int a[maxn];   int ned[maxn];   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       ned[i] = a[i] + 1;     }     for (int i = 2; i <= n; i++) {       ned[i] = max(ned[i], ned[i - 1]);     }     for (int i = n; i >= 1; i--) {       ned[i] = max(ned[i + 1] - 1, ned[i]);       ans += (ned[i] - a[i] - 1);     }     printf( %I64d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> A;   long long B[1000000] = {0};   bool cmp(long long x, long long y) { return x > y; }   int main() {     A.clear();     string S1, S2;     cin >> S1;     for (long long i = 0; i < S1.size(); i++) A.push_back(S1[i] -  0 );     cin >> S2;     for (long long i = 0; i < S2.size(); i++) B[i] = S2[i] -  0 ;     sort(B, B + S2.size(), cmp);     long long find = 0;     for (long long i = 0; i < A.size(); i++)       if (A[i] < B[find]) A[i] = B[find++];     for (long long i = 0; i < A.size(); i++) cout << A[i];   }