malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   int t, n, m;   string x, y;   int main() {     cin >> t;     while (t--) {       cin >> x;       cin >> y;       n = (int)(x.length());       m = (int)(y.length());       reverse(x.begin(), x.end());       reverse(y.begin(), y.end());       int pos1 = 0;       while (y[pos1] !=  1 ) {         ++pos1;       }       int pos2 = pos1;       while (x[pos2] !=  1 ) {         ++pos2;       }       cout << pos2 - pos1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int w[MAXN];   map<int, int> f;   long long Calc(long long Res, long long Mod) {     if (Res > Mod) return Res % Mod + Mod;     return Res;   }   long long power(long long a, long long n, long long Mod) {     long long Res = 1;     while (n) {       if (n & 1) Res = Calc(Res * a, Mod);       a = Calc(a * a, Mod);       n >>= 1;     }     return Res;   }   int phi(int n) {     int result = 1;     if (f.count(n)) return f[n];     for (int i = 2; i * i <= n; ++i) {       if (n % i == 0) {         while (n % i == 0) {           n /= i;           result *= i;         }         result /= i;         result *= (i - 1);       }     }     if (n != 1) result *= (n - 1);     f[n] = result;     return f[n];   }   long long Solve(int l, int r, int Mod) {     if (l == r || Mod == 1) return Calc(w[l], Mod);     return power(w[l], Solve(l + 1, r, phi(Mod)), Mod);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int q, n, Mod;     cin >> n >> Mod;     for (int i = 1; i <= n; ++i) cin >> w[i];     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       long long ans = Solve(l, r, Mod);       cout << ans % Mod << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int check(int x) {     int sum = 0;     if (n <= ceil(x / 2)) sum++;     if (m <= ceil(x / 3)) sum++;     if ((n + m <= ceil(x / 2) + ceil(x / 3) - ceil(x / 6))) sum++;     if (sum == 3) return 1;     return 0;   }   int main() {     cin >> n >> m;     int x;     x = max(2 * n, max(3 * m, (2 * n + 3 * m) / 2));     for (int i = x; i <= 10000000; i++) {       if (check(i)) {         cout << i << endl;         break;       }     }   }
#include <bits/stdc++.h>   using namespace std;   char a[60][60];   int ch(int r, int c) {     int i, j, f = 0;     for (i = 0; i < c; i++) {       if (a[0][i] ==  P ) f = 1;     }     if (f == 0) return 0;     f = 0;     for (i = 0; i < c; i++) {       if (a[r - 1][i] ==  P ) f = 1;     }     if (f == 0) return 0;     f = 0;     for (i = 0; i < r; i++) {       if (a[i][0] ==  P ) f = 1;     }     if (f == 0) return 0;     f = 0;     for (i = 0; i < r; i++) {       if (a[i][c - 1] ==  P ) f = 1;     }     return f;   }   int chh(int r, int c) {     int i, j, f = 0;     for (i = 1; i < r - 1; i++) {       f = 0;       for (j = 0; j < c; j++) {         if (a[i][j] ==  P ) f = 1;       }       if (f == 0) return 0;     }     for (i = 1; i < c - 1; i++) {       f = 0;       for (j = 0; j < r; j++) {         if (a[j][i] ==  P ) f = 1;       }       if (f == 0) return 0;     }     return 1;   }   int al(int r, int c) {     int i, j, f = 0;     for (i = 0; i < r; i++) {       for (j = 0; j < c; j++) {         if (a[i][j] ==  P ) return 1;       }     }     return 0;   }   int main() {     int t;     cin >> t;     while (t--) {       int r, c, i, j, f = 0;       cin >> r >> c;       cin.ignore(256,   n );       for (i = 0; i < r; i++) {         string s;         getline(cin, s);         for (j = 0; j < c; j++) a[i][j] = s[j];       }       for (i = 0; i < r; i++) {         for (j = 0; j < c; j++) {           if (a[i][j] ==  A ) {             f = 1;             break;           }         }       }       if (f == 0) {         cout <<  MORTAL n ;       } else if (al(r, c) == 0)         cout << 0 << endl;       else {         if (ch(r, c) == 0)           cout << 1 << endl;         else if (a[0][0] ==  A  || a[0][c - 1] ==  A  || a[r - 1][0] ==  A  ||                  a[r - 1][c - 1] ==  A  || chh(r, c) == 0)           cout << 2 << endl;         else {           f = 0;           for (i = 1; i < c - 1; i++) {             if (a[0][i] ==  A ) f = 1;             if (a[r - 1][i] ==  A ) f = 1;           }           for (i = 1; i < r - 1; i++) {             if (a[i][0] ==  A ) f = 1;             if (a[i][c - 1] ==  A ) f = 1;           }           if (f == 1)             cout << 3 << endl;           else             cout << 4 << endl;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   int prime[MAXN + 1];   void getPrime() {     memset(prime, 0, sizeof(prime));     for (int i = 2; i <= MAXN; i++) {       if (!prime[i]) prime[++prime[0]] = i;       for (int j = 1; j <= prime[0] && prime[j] <= MAXN / i; j++) {         prime[prime[j] * i] = 1;         if (i % prime[j] == 0) break;       }     }   }   unordered_map<int, bool> Mcnt;   unordered_map<int, int> M;   const int maxn = 105;   int a[maxn];   int getsg(int S) {     if (S == 0) return 0;     if (M.count(S)) return M[S];     vector<int> V;     V.clear();     for (int i = 0; i < 29; i++)       if (S & (1 << i)) V.push_back(i + 1);     int ma = 0;     for (int i = 0; i < V.size(); i++) ma = max(ma, V[i]);     int x[30];     set<int> pre;     pre.clear();     for (int i = 1; i <= ma; i++) {       for (int j = 0; j < V.size(); j++) {         if (V[j] >= i)           x[j] = V[j] - i;         else           x[j] = V[j];       }       int SS = 0;       for (int j = 0; j < V.size(); j++) {         if (x[j]) SS |= (1 << (x[j] - 1));       }       pre.insert(getsg(SS));     }     int now = 0;     while (pre.count(now)) now++;     M[S] = now;     return now;   }   int cnt[maxn];   int main() {     getPrime();     int ans = 0;     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= prime[0]; i++) {       memset(cnt, 0, sizeof(cnt));       for (int j = 1; j <= n; j++) {         while (a[j] % prime[i] == 0) {           a[j] /= prime[i];           cnt[j]++;         }       }       int S = 0;       for (int j = 1; j <= n; j++)         if (cnt[j]) S |= (1 << (cnt[j] - 1));       ans ^= getsg(S);     }     for (int i = 1; i <= n; i++)       if (a[i] != 1) {         if (!Mcnt.count(a[i])) {           ans ^= 1;           Mcnt[a[i]] = true;         }       }     if (ans == 0)       printf( Arpa );     else       printf( Mojtaba );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<vector<int>> v;     vector<int> cur;     if ((n * m) % k == 0) {       int div = (n * m) / k;       for (int i = 0; i < n; i++) {         if (!(i & 1)) {           for (int j = 0; j < m; j++) {             cur.push_back(i + 1);             cur.push_back(j + 1);             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();             }           }         } else {           for (int j = m - 1; j >= 0; j--) {             if (cur.size() <= div * 2) {               cur.push_back(i + 1);               cur.push_back(j + 1);             }             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();             }           }         }       }     } else {       int div = ((n * m) - 2) / (k - 1);       int count = 0;       int flag = 0;       for (int i = 0; i < n; i++) {         if (!(i & 1)) {           for (int j = 0; j < m; j++) {             if (count == k - 1 && flag == 0) {               div = (n * m - (k - 1) * (div));               flag = 1;             }             cur.push_back(i + 1);             cur.push_back(j + 1);             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();               count++;             }           }         } else {           for (int j = m - 1; j >= 0; j--) {             if (count == k - 1 && flag == 0) {               div = (n * m - (k - 1) * (div));               flag = 1;             }             if (cur.size() <= div * 2) {               cur.push_back(i + 1);               cur.push_back(j + 1);             }             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();               count++;             }           }         }       }     }     for (int i = 0; i < v.size(); i++) {       int s = v[i].size();       s /= 2;       cout << s <<    ;       for (int j = 0; j < v[i].size(); j++) {         cout << v[i][j] <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 7;   int n;   int v[N], f[N];   bool e[N];   int st, dr;   bool ok(int l, int r) {     for (int i = 1; i <= n; i++) {       e[i] = 0;       f[i] = 0;     }     for (int i = l; i <= r; i++) {       e[i] = 1;       f[v[i]]++;     }     for (int i = 1; i < n + 1 - i; i++) {       if (e[i] == 1 && e[n + 1 - i] == 0) {         if (f[v[n + 1 - i]] > 0) {           f[v[n + 1 - i]]--;         } else {           return 0;         }       }       if (e[i] == 0 && e[n + 1 - i] == 1) {         if (f[v[i]] > 0) {           f[v[i]]--;         } else {           return 0;         }       }       if (e[i] == 0 && e[n + 1 - i] == 0 && v[i] != v[n + 1 - i]) {         return 0;       }     }     int cntimp = 0;     for (int i = 1; i <= n; i++) {       if (f[i] % 2 == 1) {         cntimp++;       }     }     return (cntimp <= 1);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> v[i];       f[v[i]]++;     }     int imp = 0;     for (int i = 1; i <= n; i++) {       if (f[i] % 2 == 1) {         imp++;       }     }     if (imp >= 2) {       cout <<  0 n ;       return 0;     }     bool pali = 1;     for (int i = 1; i < n + 1 - i; i++) {       if (v[i] != v[n + 1 - i]) {         pali = 0;       }     }     if (pali) {       long long res = n * (long long)(n + 1) >> 1;       cout << res <<   n ;       return 0;     }     st = 1;     dr = n;     while (v[st] == v[dr]) {       st++;       dr--;     }     int lo = st;     int hi = dr;     int prf = -1, suf = -1;     while (lo <= hi) {       int mid = (lo + hi) >> 1;       if (ok(st, mid)) {         prf = mid;         hi = mid - 1;       } else {         lo = mid + 1;       }     }     lo = 1;     hi = n;     while (lo <= hi) {       int mid = (lo + hi) >> 1;       if (ok(mid, dr)) {         suf = mid;         lo = mid + 1;       } else {         hi = mid - 1;       }     }     int cntgood = 0;     if (prf != -1) {       cntgood += (dr - prf + 1);     }     if (suf != -1) {       cntgood += (suf - st + 1);     }     long long res =         cntgood * (long long)st - 1 + (st - 1LL) * (long long)(st - 1LL);     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a1, a2, b1, b2, c1, c2;   void rec(long long p, long long q) {     long long mod = c1 * c1 + c2 * c2;     if ((p * c1 + q * c2) % mod == 0 && (q * c1 - p * c2) % mod == 0) {       puts( YES );       exit(0);     }   }   int main() {     scanf( %I64d%I64d%I64d%I64d%I64d%I64d , &a1, &a2, &b1, &b2, &c1, &c2);     if (c1 == 0 && c2 == 0) {       if ((a1 == b1 && a2 == b2) || (a1 == -b1 && a2 == -b2) ||           (a1 == b2 && a2 == -b1) || (a1 == -b2 && a2 == b1))         puts( YES );       else         puts( NO );       return 0;     }     rec(b1 - a1, b2 - a2);     rec(b1 + a1, b2 + a2);     rec(b1 + a2, b2 - a1);     rec(b1 - a2, b2 + a1);     puts( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int l, r, x, y;     double k;     cin >> l >> r >> x >> y >> k;     bool flag = false;     double value;     while (x <= y) {       value = (double)x * k;       if (value >= l && value <= r) {         flag = true;         break;       }       x++;     }     if (flag) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   const int inf = 1000 * 1000 * 1000;   const int mod = 998244353;   int n;   vector<int> g[N];   int dp[N][3];   int sz[N][3];   void dfs(int v, int p) {     dp[v][0] = dp[v][1] = 0;     sz[v][0] = 1;     sz[v][1] = 0;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       dfs(to, v);       int d0 = dp[v][0] * 1ll * (sz[to][0] + sz[to][1]) % mod +                (dp[to][0] + dp[to][1]) * 1ll * sz[v][0] % mod;       d0 %= mod;       int s0 = sz[v][0] * 1ll * (sz[to][0] + sz[to][1]) % mod;       int d1 = dp[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod +                (dp[to][0] + dp[to][1]) * 1ll * sz[v][1] % mod;       d1 %= mod;       int s1 = sz[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod;       int dd0 =           dp[v][0] * 1ll * sz[to][1] % mod + dp[to][1] * 1ll * sz[v][0] % mod;       dd0 %= mod;       int ss0 = sz[v][0] * 1ll * sz[to][1] % mod;       int dd1 = dp[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod +                 (dp[to][0] + dp[to][1]) * 1ll * sz[v][1] % mod;       dd1 %= mod;       dd1 += dp[v][0] * 1ll * sz[to][0] % mod;       dd1 %= mod;       dd1 += sz[v][0] * 1ll * dp[to][0] % mod;       dd1 %= mod;       int ss1 = sz[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod +                 sz[v][0] * 1ll * sz[to][0] % mod;       ss1 %= mod;       dd1 += sz[v][0] * 1ll * sz[to][0] % mod;       dd1 %= mod;       dp[v][0] = (d0 + dd0) % mod;       sz[v][0] = (s0 + ss0) % mod;       dp[v][1] = (d1 + dd1) % mod;       sz[v][1] = (s1 + ss1) % mod;     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1, -1);     int answ = (dp[1][0] + dp[1][1]) % mod;     answ *= 2;     answ %= mod;     cout << answ << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200005];   int b[200005];   int c[200005];   char str1[200005];   char str2[200005];   int main() {     int n;     while (scanf( %d , &n) != EOF) {       cin >> str1 >> str2;       int i;       for (i = 0; i < n; i++) {         a[i] = str1[i] -  a ;         b[i] = str2[i] -  a ;       }       for (i = n - 1; i >= 0; i--) {         if (b[i] >= a[i]) {           c[i] = b[i] - a[i];         } else {           c[i] = b[i] - a[i] + 26;           b[i - 1]--;         }       }       for (i = 0; i < n; i++) {         if (c[i] % 2 == 0) {           c[i] = c[i] / 2;         } else {           c[i] = c[i] / 2;           c[i + 1] += 26;         }       }       for (i = n - 1; i >= 0; i--) {         a[i] = a[i] + c[i];         while (a[i] >= 26) {           a[i] -= 26;           a[i - 1]++;         }       }       for (i = 0; i < n; i++) {         printf( %c , (char)(a[i] +  a ));       }       printf(  n );     }     return 0;   }
#include <iostream>   #include <string>   #include <algorithm>   #include <map>   #include <vector>   #include <cmath>   #include <queue>   #include <set>   #include <bitset>      using namespace std;      typedef long long ll;   ll MOD = 1000000007;      void solve(int testCase)   {    ll n;    cin >> n;    for (int i = 0; i * 2020 <= n; i++)    {     if ((n - i * 2020) % 2021 == 0)     {      cout <<  YES  << endl;      return;     }    }    cout <<  NO  << endl;      }       int main()   {    int t;    cin >> t;    //t = 1;    int i = 1;    while (t--)    {     solve(i++);    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[200007];   int brr[200007];   int main() {     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       map<int, int> mpp;       for (int i = 0; i < n; i++) {         cin >> arr[i];         mpp[arr[i]]++;       }       int cnt = 0;       for (int i = 1;; i++) {         if (x == 0) break;         if (mpp[i] == 0) {           mpp[i]++;           x--;         }       }       for (int i = 1;; i++) {         if (mpp[i] == 0)           break;         else           cnt++;       }       cout << cnt << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   int mark[N], t[N], n;   long long res;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     cin >> n;     for (int i = (1); i <= (n); i += (1)) cin >> mark[i];     int cur = 0;     for (int i = (n); i >= (1); i -= (1)) {       cur = max(0, cur - 1);       cur = max(cur, mark[i] + 1);       t[i] = cur;     }     cur = 0;     for (int i = (1); i <= (n); i += (1)) {       cur = max(cur, t[i]);       res += cur;     }     for (int i = (1); i <= (n); i += (1)) res -= (mark[i] + 1);     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int arr[n];       for (int i = 0; i < n; i++) {         cin >> arr[i];       }       sort(arr, arr + n);       int ans = 1;       for (int i = 1; i < n; i++) {         if (arr[i] > arr[i - 1]) ans++;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a[100000], p[100000];     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i] >> p[i];     for (int i = 1; i < n; ++i) p[i] = min(p[i], p[i - 1]);     int ans = 0;     for (int i = 0; i < n; ++i) ans += p[i] * a[i];     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, m = 1e9 + 7;     cin >> n >> k;     int a[100005];     a[0] = 1;     for (int i = 1; i < 100005; ++i) {       if (i >= k) {         a[i] = (a[i - k] + a[i - 1]) % m;       } else {         a[i] = a[i - 1];       }     }     for (int i = 1; i < 100005; ++i) {       a[i] = (a[i] + a[i - 1]) % m;     }     while (n--) {       int x, y;       scanf( %d %d , &x, &y);       printf( %d n , (a[y] - a[x - 1] + m) % m);     }   }
#include <bits/stdc++.h>   using namespace std;   void write(vector<int> &v) {     for (auto i : v) cout << i <<    ;     cout <<   n ;   }   void read(vector<int> &v) {     for (auto &i : v) cin >> i;   }   const int INF = 1e9;   const int64_t INFF = 1e18;   const int N = 1e6 + 69;   void solve() {     int n;     cin >> n;     string s1, s2;     cin >> s1 >> s2;     int ans = 0;     vector<int> pos(n, 1);     for (int i = 0; i < n; i++) {       if (s2[i] ==  1 ) {         if (s1[i] ==  0 ) {           ans++;           pos[i] = 0;         } else if (i > 0 && s1[i - 1] ==  1  && pos[i - 1]) {           ans++;           pos[i - 1] = 0;         } else if (i < n - 1 && s1[i + 1] ==  1  && pos[i + 1]) {           ans++;           pos[i + 1] = 0;         }       }     }     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     cin >> t;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long p = 1e9 + 7;   inline long long read() {     long long neg = 1, num = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) neg = -1;     for (; isdigit(c); c = getchar()) num = (num << 1) + (num << 3) + c -  0 ;     return neg * num;   }   long long n, k, tot, f[200010], g[200010], fac[200010], inv[200010],       pri[200010], mu[200010], mul[200010];   bool vis[200010];   inline long long Pow(long long x, long long y) {     long long res = 1;     for (; y; y >>= 1, x = x * x % p)       if (y & 1) res = res * x % p;     return res;   }   inline long long C(long long x, long long y) {     return fac[x] * inv[y] % p * inv[x - y] % p;   }   inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }   inline void sieve() {     vis[1] = mu[1] = 1;     for (long long i = 2; i <= n; i++) {       if (!vis[i]) pri[++tot] = i, mu[i] = -1;       for (long long j = 1; j <= tot && pri[j] * i <= n; j++) {         vis[i * pri[j]] = 1;         if (i % pri[j] == 0) break;         mu[i * pri[j]] = -mu[i];       }     }     fac[0] = inv[0] = mul[0] = 1;     for (long long i = 1; i <= 2 * n; i++)       fac[i] = fac[i - 1] * i % p, mul[i] = mul[i - 1] * k % p;     inv[2 * n] = Pow(fac[2 * n], p - 2);     for (long long i = 2 * n - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % p;     for (long long i = 2; i <= n; i++)       f[i] = (f[i - 1] + (i - 1) * mul[i] % p) % p;     for (long long i = 1; i <= n; i++) g[i] = Pow(f[n / i], i) % p;   }   signed main() {     n = read();     k = read();     sieve();     long long ans = 0, sub = 0;     for (long long i = 2; i <= 2 * n; i++) {       if (i <= n)         ans = (ans + (i - 1) * mul[i] % p) % p;       else         ans = (ans + (2 * n - i + 1) * mul[i] % p) % p;     }     ans = ans * Pow(mul[n], p - 2) % p;     for (long long d = 1; d <= n; d++) {       for (long long T = d; T <= n; T += d) {         long long s1 = (n / T) * (n / T - 1) / 2 % p, s2 = g[T];         if ((n + d - 1) / T != n / T)           s1 = (s1 + (n + d - 1) / T - 1) % p,           s2 = (s2 + ((n + d - 1) / T - 1) * mul[(n + d - 1) / T * T] % p) % p;         ans = (ans + s1 * mul[d] * mu[T / d] % p) % p;         sub = (sub + s2 * mu[T / d]) % p;       }     }     sub = sub * Pow(mul[n], p - 2) % p;     ans = (ans - sub) % p;     for (long long i = 1; i <= n; i++)       ans = (ans - (1 + (i != n)) * mul[max(gcd(i, n), i)]) % p;     ans = ans * Pow(mul[n], p - 2) % p;     ans = (ans % p + p) % p;     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int num[100020], nxt[100020];   struct point {     int st, en, num;   } pnt[100020 * 4];   int max1(int a, int b) {     if (a > b) return a;     return b;   }   int main() {     int n, sum = 0, cnt = 0, besti, min1 = 1 << 25, tmp, i;     scanf( %d , &n);     num[n + 1] = nxt[0] = num[0] = 0;     for (i = 1; i <= n; i++) {       scanf( %d , &num[i]), nxt[i] = i;     }     for (i = 0; i < 100020 * 3; i++) pnt[i].st = pnt[i].en = pnt[i].num = 0;     for (i = 1; i <= n + 1; i++) {       if (num[i] >= num[i - 1]) {         sum += num[i] - num[i - 1];       } else {         besti = i - 1;         while (num[besti] > num[i]) {           tmp = max1(num[nxt[besti] - 1], num[i]);           pnt[cnt].en = i - 1;           pnt[cnt].st = nxt[besti], pnt[cnt++].num = num[besti] - tmp;           nxt[i] = nxt[besti];           besti = nxt[besti] - 1;         }         if (num[besti] >= num[i]) nxt[i] = nxt[besti];       }     }     printf( %d n , sum);     for (i = 0; i < cnt; i++) {       for (int j = 0; j < pnt[i].num; j++)         printf( %d %d n , pnt[i].st, pnt[i].en);     }     return 0;   }