malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   char ss[5][5];   int num1, num2;   int check() {     int win1 = 0, win2 = 0;     if (ss[1][1] == ss[1][2] && ss[1][2] == ss[1][3]) {       if (ss[1][1] ==  X )         win1++;       else if (ss[1][1] ==  0 )         win2++;     }     if (ss[2][1] == ss[2][2] && ss[2][2] == ss[2][3]) {       if (ss[2][1] ==  X )         win1++;       else if (ss[2][1] ==  0 )         win2++;     }     if (ss[3][1] == ss[3][2] && ss[3][2] == ss[3][3]) {       if (ss[3][1] ==  X )         win1++;       else if (ss[3][1] ==  0 )         win2++;     }     if (ss[1][1] == ss[2][1] && ss[2][1] == ss[3][1]) {       if (ss[1][1] ==  X )         win1++;       else if (ss[1][1] ==  0 )         win2++;     }     if (ss[1][2] == ss[2][2] && ss[2][2] == ss[3][2]) {       if (ss[1][2] ==  X )         win1++;       else if (ss[1][2] ==  0 )         win2++;     }     if (ss[1][3] == ss[2][3] && ss[2][3] == ss[3][3]) {       if (ss[1][3] ==  X )         win1++;       else if (ss[1][3] ==  0 )         win2++;     }     if (ss[1][1] == ss[2][2] && ss[2][2] == ss[3][3]) {       if (ss[1][1] ==  X )         win1++;       else if (ss[1][1] ==  0 )         win2++;     }     if (ss[1][3] == ss[2][2] && ss[2][2] == ss[3][1]) {       if (ss[1][3] ==  X )         win1++;       else if (ss[1][3] ==  0 )         win2++;     }     if (win1 == 0 && win2 == 0) return 0;     if (win1 > 0 && win2 > 0) return -1;     if (num1 - num2 == 0 && win1 > 0) return -1;     if (num1 - num2 == 1 && win2 > 0) return -1;     if (win1 > 0) return 1;     if (win2 > 0) return 2;   }   void fl() {     freopen( .in ,  r , stdin);     freopen( .out ,  w , stdout);   }   int main() {     char c;     for (int i = 1; i <= 3; i++) {       for (int j = 1; j <= 3; j++) {         scanf( %c , &ss[i][j]);         if (ss[i][j] ==  X )           num1++;         else if (ss[i][j] ==  0 )           num2++;       }       if (i != 3) scanf( %c , &c);     }     if (!(num1 - num2 == 0 || num1 - num2 == 1)) {       puts( illegal );       return 0;     }     int flag = check();     if (flag == -1)       puts( illegal );     else if (flag == 1)       puts( the first player won );     else if (flag == 2)       puts( the second player won );     else if (flag == 0) {       if (num1 + num2 == 9)         puts( draw );       else if (num1 - num2 == 0)         puts( first );       else if (num1 - num2 == 1)         puts( second );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int m, n;   int main() {     int n;     string vic1, vic2, tmp1, tmp2;     cin >> vic1 >> vic2;     cin >> n;     cout << vic1 <<     << vic2 << endl;     for (int i = 0; i < n; ++i) {       cin >> tmp1 >> tmp2;       if (vic1 == tmp1)         vic1 = tmp2;       else         vic2 = tmp2;       cout << vic1 <<     << vic2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char ch[6700][3000];   char c[3000];   int vis[2000];   vector<int> ans;   int len;   void shift(int x, int k) {     int num = 0;     for (int i = len - 1; i > len - 1 - x; i--) {       ch[k][num++] = ch[k - 1][i];     }     for (int i = 0; i < len - x; i++) {       ch[k][num++] = ch[k - 1][i];     }     ch[k][num] =   0 ;   }   int main() {     scanf( %d , &len);     scanf( %s , ch[0]);     while (ch[0][0] ==     || ch[0][0] ==   n  || ch[0][0] ==   r )       scanf( %s , ch[0]);     scanf( %s , c);     while (c[0] ==     || c[0] ==   n  || c[0] ==   r ) scanf( %s , c);     if (strlen(c) != strlen(ch[0])) {       printf( -1 );       return 0;     }     for (int i = 0; i < len; i++) {       vis[ch[0][i]]++;       vis[c[i]]--;     }     for (int i =  a ; i <=  z ; i++) {       if (vis[i] != 0) {         printf( -1 );         return 0;       }     }     int num = 1;     for (int i = 1; i <= len; i++) {       for (int j = 0; j <= len - i; j++) {         if (c[i - 1] == ch[num - 1][j]) {           ans.push_back(len - j - 1);           ans.push_back(1);           ans.push_back(len - 1);           shift(len - j - 1, num++);           shift(1, num++);           shift(len - 1, num++);           break;         }       }     }     printf( %d n , num - 1);     for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   char str[10];   bool check(int x, int y) {     sprintf(str,  %02d:%02d , x, y);     return (str[0] == str[4] && str[1] == str[3]);   }   int main() {     int i, j, n, m;     scanf( %d:%d , &n, &m);     for (i = n; i < 24; ++i) {       for (j = 0; j < 60; ++j) {         if (i == n && j <= m) continue;         if (check(i, j)) {           printf( %s , str);           return 0;         }       }     }     cout <<  00:00  << endl;     return 0;   }
#include<bits/stdc++.h>   #define fi                first   #define se                 second   #define ll               long long   #define mod              1000000007LL   #define endl                n    #define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);   #define pb                push_back   #define all(v)             v.begin(),v.end()   #define inp(V,n)      for(int i=0;i<n;i++){cin >> V[i];}   #define oup(V,n)     {for(int i=0;i<n;i++){cout <<  V[i] <<      ;} cout << endl;}   #define pll              pair<long long,long long>   #define pi            3.141592653589793238   using namespace std;   //vector<ll> adj[5000005];   //vector<bool> vis(3000005,false);   ll factorial(ll n)   {       return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);   }   ll gcd(ll a, ll b)   {       if (a == 0)         return b;       return gcd(b % a, a);   }   ll lcm(ll a, ll b)   {      return (a*b)/gcd(a, b);   }   bool sortByVal(const pair<ll,ll> &a,                 const pair<ll,ll> &b)   {         if(a.fi == b.fi)           return a.se < b.se;        else           return a.fi > b.fi;   }   ll Powb(ll b,ll n)   {       if(n==0) return 1LL;       if(n==1) return b;       ll temp = Powb(b,n/2);       if(n%2==0){return (temp*temp)%mod;}       else{return (b*((temp*temp)%mod))%mod;}   }   int main()   {    fast;        int t=1;        //cin>>t;        while(t--)        {        ll q,i,j,m,n,k,sum=0,c=0,d=0,e=0,f=0,g=0,h=0,maxi=0;         cin>>n>>q>>k;         vector<ll> a(n);         inp(a,n);         vector<ll> b(n);         if(n == 1)         {             while(q--)             {                 cin>>c>>d;                 cout<<k-1<<endl;             }         }         else         {             for(i=0;i<n;i++)             {                 if(i == 0)                 {                     c=a[i+1]-2;                     b[i]=c;                 }                 else if(i == n-1)                 {                     c=k-a[n-1];                     b[i]=c;                 }                 else                 {                     c=a[i-1];                     d=a[i+1];                     d-=c;                     d-=2;                     b[i]=d;                 }                }             vector<ll> p(n);             sum=0;             for(i=0;i<n;i++)             {                 sum+=b[i];                 p[i]=sum;             }                //oup(b,n);             //oup(p,n);             while(q--)             {                 sum=0;                 cin>>c>>d;                 c--;                 d--;                 if(c == d)                 {                     cout<<k-1<<endl;                 }                 else if((d-c) == 1)                 {                     e=a[d]-2;                     f=k-a[c]-1;                     cout<<e+f<<endl;                 }                 else                 {                     sum=p[d-1]-p[c];                     e=k-a[d-1]-1;                     sum+=e;                     f=a[c+1]-2;                     sum+=f;                     cout<<sum<<endl;                 }                }         }              }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     cin >> n;     while (n % 10 == 0) n /= 10;     stringstream ss;     ss << n;     string str = ss.str();     string tmp = str;     reverse(tmp.begin(), tmp.end());     if (str == tmp)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 500, Maxm = 500;   int n, m, x[Maxn], y[Maxn], seq[Maxm];   bool canGet(int from) {     bool taken[Maxm] = {};     vector<int> ans;     for (int i = 0; i < n; i++)       if ((i - from) % 2)         ans.push_back(-1);       else {         int prv = (i - 1 + n) % n;         int nxt = (i + 1) % n;         int need = 0;         if (x[prv] == x[i])           need += abs(y[prv] - y[i]);         else           need += abs(x[prv] - x[i]);         if (x[i] == x[nxt])           need += abs(y[i] - y[nxt]);         else           need += abs(x[i] - x[nxt]);         int j;         for (j = 0; j < m; j++)           if (!taken[j] && seq[j] == need) break;         if (j == m) return false;         ans.push_back(j + 1);         taken[j] = true;       }     cout <<  YES n ;     for (int i = 0; i < ans.size(); i++) {       if (i) cout <<    ;       cout << ans[i];     }     cout << endl;     return true;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> x[i] >> y[i];     for (int i = 0; i < m; i++) cin >> seq[i];     if (!canGet(0))       if (!canGet(1)) cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};   int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   bool cmp(vector<long long> a, vector<long long> b) { return a[2] > b[2]; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     vector<vector<long long>> v;     for (long long i = 0; i < n; i++) {       long long a, b, c;       cin >> a >> b >> c;       v.push_back({a, b, c});     }     sort((v).begin(), (v).end(), cmp);     vector<int> adj[n + 1];     for (int i = 0; i < n; i++) {       for (int j = i - 1; j >= 0; j--) {         long double dis = (v[j][0] - v[i][0]) * (v[j][0] - v[i][0]) +                           (v[j][1] - v[i][1]) * (v[j][1] - v[i][1]);         long double dis1 = v[i][2] + v[j][2];         if (dis1 * dis1 <= dis) {           continue;         } else {           adj[i].push_back(j);           adj[j].push_back(i);           break;         }       }     }     vector<int> vis(n, 0);     vector<int> dis(n, 0);     for (int i = 0; i < n; i++) {       if (vis[i] == 0) {         queue<int> q;         q.push(i);         vis[i] = 1;         dis[i] = 1;         while (!q.empty()) {           auto a = q.front();           q.pop();           for (auto i : adj[a]) {             if (vis[i] == 0) {               vis[i] = 1;               dis[i] = dis[a] + 1;               q.push(i);             }           }         }       }     }     long double ans = 0;     for (int i = 0; i < n; i++) {       if (dis[i] % 2 == 0 || dis[i] == 1) {         ans =             ans +             3.141592653589793238462643383279502884197169399375105820974944592307816406286 *                 v[i][2] * v[i][2];       } else {         ans =             ans -             3.141592653589793238462643383279502884197169399375105820974944592307816406286 *                 v[i][2] * v[i][2];       }     }     cout << setprecision(15) << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T, typename L>   inline bool smax(T &x, L y) {     return x < y ? (x = y, true) : false;   }   template <typename T, typename L>   inline bool smin(T &x, L y) {     return y < x ? (x = y, true) : false;   }   constexpr int MAXN = 1e5 + 15;   int n;   int a[MAXN];   int ttime[MAXN];   stack<int> dq;   int mx;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < n; i++) {       ttime[i] = 1;       while (!dq.empty() && a[i] > a[dq.top()]) {         smax(ttime[i], ttime[dq.top()] + 1);         dq.pop();       }       if (!dq.empty()) {         smax(mx, ttime[i]);       } else {         ttime[i] = 1e6 + 15;       }       dq.push(i);     }     return cout << mx <<   n , false;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1111;   long long fact[N], invfact[N];   long long n, m, k;   long long powmod(long long x, long long n, long long m = 1000000007) {     long long ans = 1;     while (n) {       if (n & 1) {         ans = ans * x % m;       }       x = x * x % m;       n >>= 1;     }     return ans;   }   inline long long invmod(long long x, long long m = 1000000007) {     return powmod(x, m - 2);   }   inline long long ncr(long long n, long long r) {     if (r < 0 || r > n) return 0;     return fact[n] * invfact[r] % 1000000007 * invfact[n - r] % 1000000007;   }   signed main() {     ios_base::sync_with_stdio(false);     cin >> n >> m >> k;     fact[0] = 1;     invfact[0] = 1;     for (long long i = (1); i <= (N - 1); i++) {       fact[i] = fact[i - 1] * i % 1000000007;       invfact[i] = invmod(fact[i]);     }     long long ans = ncr(n - 1, 2 * k) * ncr(m - 1, 2 * k) % 1000000007;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int data = 0, w = 1;     char ch = getchar();     while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar();     if (ch ==  - ) w = -1, ch = getchar();     while (ch >=  0  && ch <=  9 )       data = (data << 3) + (data << 1) + (ch ^ 48), ch = getchar();     return data * w;   }   const int maxn(200010);   long long n, a[maxn], x, f, ans;   int main() {     n = read();     for (register int i = 1; i <= n; i++) a[i] = read();     x = read();     f = read();     for (register int i = 1; i <= n; i++) {       a[i] -= x;       if (a[i] > 0) {         if (!(a[i] % (x + f)))           ans += (a[i] / (x + f)) * f;         else           ans += (1 + a[i] / (x + f)) * f;       }     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios_base::sync_with_stdio(false);     long long int n, l, r;     cin >> n >> l >> r;     if (n == 0) {       cout << 0 << endl;       return 0;     } else if (n == 1) {       cout << 1 << endl;       return 0;     } else if (n == 2) {       vector<long long int> a;       a.push_back(1), a.push_back(0), a.push_back(1);       int i;       int c = 0;       for (i = l - 1; i < r; i++) {         if (a[i] == 1) c++;       }       cout << c << endl;       return 0;     } else if (n == 3) {       vector<long long int> a;       a.push_back(1), a.push_back(1), a.push_back(1);       int i;       int c = 0;       for (i = l - 1; i < r; i++) {         if (a[i] == 1) c++;       }       cout << c << endl;       return 0;     }     long long int t = n;     long long int ct = 1;     while (t != 3 && t != 2) {       ct++;       t = t / 2;     }     long long int tt = n;     long long int ans3 = 0;     while (tt > 3) {       long long int kk = 0;       long long int vv = 0;       long long int ll, rr;       vv = pow(2, ct);       ll = l - vv - 1;       if (ll >= 0) {         ll = ll / pow(2, ct + 1) + 1;       }       rr = r - vv + 1;       if (rr >= 0) {         rr = rr / pow(2, ct + 1) + 1;       }       if (ll < 0) ll = 0;       if ((rr >= 0 && ll >= 0) && (tt & 1)) {         ans3 += rr - ll;       }       kk++;       ct--;       tt = tt / 2;     }     if (t == 2) {       long long int ans;       long long int left = 0;       long long int right = 0;       left = 2 * ((l - 1) / 4);       if ((l - 1) % 4 == 1 || ((l - 1) % 4) == 2)         left++;       else if ((l - 1) % 4 == 3)         left += 2;       right = 2 * (r / 4);       if ((r % 4) == 1 || (r % 4) == 2)         right++;       else if ((r % 4) == 3)         right += 2;       ans = right - left;       t = 2 * t + t % 2;       cout << ans + ans3 << endl;     } else if (t == 3) {       long long int ans;       long long int left = 0;       long long int right = 0;       left = 3 * ((l - 1) / 4);       left += (l - 1) % 4;       right = 3 * (r / 4);       right += r % 4;       ans = right - left;       cout << ans + ans3 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1010;   const int INF = 1000000000;   int n;   int x[MaxN], y[MaxN], tx[MaxN], ty[MaxN];   long double L, R;   long double det(long double x1, long double y1, long double x2, long double y2,                   long double x3, long double y3) {     return x1 * y2 - x2 * y1 + x2 * y3 - x3 * y2 + x3 * y1 - x1 * y3;   }   void line(long double x1, long double y1, long double x2, long double y2,             long double y0, long double& x0) {     long double A = y1 - y2;     long double B = x2 - x1;     long double C = A * x1 + B * y1;     x0 = (C - B * y0) / A;   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) cin >> x[i] >> y[i];     L = x[1], R = x[2];     if (L > R) {       for (int i = 1, j = 2; i <= n; ++i) {         tx[i] = x[j];         ty[i] = -y[j];         --j;         if (!j) j = n;       }       for (int i = 1; i <= n; ++i) {         x[i] = tx[i];         y[i] = ty[i];       }       swap(L, R);     }     for (int i = 3; i <= n; ++i) {       int px = x[1], py = y[1];       for (int j = i + 1; j <= n; ++j)         if (det(x[i], y[i], x[j], y[j], px, py) > 0) {           px = x[j];           py = y[j];         }       long double tmp;       line(x[i], y[i], px, py, y[1], tmp);       L = max(L, tmp);       px = x[2], py = y[2];       for (int j = 3; j < i; ++j)         if (det(x[i], y[i], px, py, x[j], y[j]) > 0) {           px = x[j];           py = y[j];         }       line(x[i], y[i], px, py, y[1], tmp);       R = min(R, tmp);     }     int ans = 0;     for (int i = L - 1; i <= R + 1; ++i)       if (L <= i && i <= R) ++ans;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s1, s2;       cin >> s1 >> s2;       int len1 = s1.length(), len2 = s2.length();       int a[26] = {0}, mark = 0;       multiset<char, greater<int> > st1, st2;       for (int i = 0; i < len1; i++) {         a[s1[i] - 97]++;         st1.insert(s1[i]);       }       for (int i = 0; i < len2; i++) {         if (a[s2[i] - 97] > 0) {           int b[26] = {0};           st2.clear();           for (int j = i; j < len2; j++) {             b[s2[j] - 97]++;             if (b[s2[j] - 97] > a[s2[j] - 97]) {               break;             } else {               st2.insert(s2[j]);             }           }           if (st1 == st2) {             mark = 1;             break;           }         }       }       if (mark == 0)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, t[100005];   int l1, l2;   int dp[100005];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &t[i]);     l1 = 1;     l2 = 1;     for (int i = 1; i <= n; ++i) {       while (t[i] - t[l1] > 89) ++l1;       while (t[i] - t[l2] > 1439) ++l2;       dp[i] = 1e9;       dp[i] = min(dp[i], dp[i - 1] + 20);       dp[i] = min(dp[i], dp[l1 - 1] + 50);       dp[i] = min(dp[i], dp[l2 - 1] + 120);     }     for (int i = 1; i <= n; ++i) printf( %d n , dp[i] - dp[i - 1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[105];   int n, k;   char s[105];   int main(void) {     int i, j, x, y, z;     scanf( %d %d , &n, &k);     scanf( %s , s);     x = 0;     for (i = 0; i < s[i]; i++) {       a[x++] = s[i] -  a  + 1;     }     sort(a, a + n);     x = 1;     y = a[0];     z = 0;     if (x == k) {       printf( %d n , y);       return 0;     }     for (i = 1; i < n; i++) {       if (a[i] - a[z] > 1) {         x++;         y += a[i];         z = i;         if (x == k) break;       }     }     if (i >= n) {       printf( -1 );       return 0;     }     printf( %d n , y);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m, k = 0, k1 = 0, kol1 = 0, kol2 = 0;     cin >> n >> m;     int a[n + 10], b[m + 10];     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] % 2 == 1)         k++;       else         k1++;     }     for (int i = 1; i <= m; i++) {       cin >> b[i];       if (b[i] % 2 == 1)         kol1++;       else         kol2++;     }     cout << min(k, kol2) + min(k1, kol1);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, a, b, x, y, flag = 0;     cin >> n >> a >> x >> b >> y;     int in = 0;     for (int i = 0; i < n; i++) {       a = a % n + 1;       b = b - 1;       if (b == 0) b = n;       if (a == b) {         cout <<  YES ;         flag = 1;         break;       }       if (a == x or b == y) break;     }     if (flag == 0) cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[301];   int l[301], r[301];   void INPUT() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) cin >> l[i] >> r[i];   }   void SOLVE() {     int res = 0;     int ans[301], tam[301];     int sl = 0;     int i, j, k;     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++)         if (i == j)           continue;         else {           int dem = 0;           for (k = 1; k <= m; k++)             if (l[k] <= j & j <= r[k] & (l[k] > i || r[k] < i)) {               dem++;               tam[dem] = k;             }           if (res < a[i] - a[j] + dem) {             res = a[i] - a[j] + dem;             sl = dem;             for (k = 1; k <= sl; k++) ans[k] = tam[k];           }         }     cout << res << endl;     cout << sl << endl;     for (i = 1; i <= sl; i++) cout << ans[i] <<    ;   }   int main() {     INPUT();     SOLVE();   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 500010;   bool f[MAXN];   int main() {     int n, d;     scanf( %d %d , &n, &d);     f[0] = 1;     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       for (int j = MAXN - 1 - x; j >= 0; --j) f[j + x] |= f[j];     }     int r = 1, sum = 0, ans = 0;     while (r < MAXN) {       int next = -1;       while (r <= sum + d && r < MAXN) {         if (f[r]) next = r;         r++;       }       if (next == -1) break;       sum = next;       ans++;     }     printf( %d %d n , sum, ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 7;   long long a[N];   long long l[N];   long long r[N];   int32_t main() {     long long ans = 0, n, i;     for (i = 1, scanf( %I64d , &n); i <= n; scanf( %I64d , &a[i++]))       ;     for (i = 1; i <= n; l[i] = r[i] = i, i++)       ;     for (i = 2; i <= n; i++) {       long long num = i;       while (num > 1 && a[num - 1] <= a[i]) num = l[num - 1];       l[i] = num;     }     for (i = n - 1; i >= 1; i--) {       long long num = i;       while (num < n && a[num + 1] < a[i]) num = r[num + 1];       r[i] = num;     }     for (i = 1; i <= n; ans += (i - l[i] + 1) * (r[i] - i + 1) * a[i], i++)       ;     for (i = 1; i <= n; l[i] = r[i] = i, i++)       ;     for (i = 2; i <= n; i++) {       long long num = i;       while (num > 1 && a[num - 1] >= a[i]) num = l[num - 1];       l[i] = num;     }     for (i = n - 1; i >= 1; i--) {       long long num = i;       while (num < n && a[num + 1] > a[i]) num = r[num + 1];       r[i] = num;     }     for (i = 1; i <= n; i++) {       ans -= (i - l[i] + 1) * (r[i] - i + 1) * a[i];     }     printf( %I64d n , ans);   }