For this Question: Given an array Arr of N elements, your task is to find the sum of the difference of maximum and minimum element of all subarrays.The first line of input contains a single integer N, and the next line of input contains N space-separated integers depicting the values of the array.

<b>Constraints:-</b>
1 &le; N &le; 5*10<sup>4</sup>
0 &le; Arr[i] &le; 10<sup>5</sup>Print the sum of the difference of all the possible subarrays.Sample Input:-
4
3 1 4 2

Sample Output:-
16

Explanation:-
Subarrays of size 1:- [3], [1], [4], [2], sum = 0 + 0 + 0 + 0 = 0
Subarrays of size 2:- [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7
Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6
Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16

Sample Input:-
4
5 2 0 6

Sample Output:-
28, I have written this Solution Code: #include <bits/stdc++.h>


using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))

#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)

#define IN(A, B, C) assert( B <= A && A <= C)

#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)

#define FOR1(i,j,a) for(int i=j;i<a;i++)

#define EB emplace_back

#define INF (int)1e9

#define EPS 1e-9

#define PI 3.1415926535897932384626433832795
#define max1 100005
#define MOD 1000000007

#define read(type) readInt<type>()

#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int arr[max1],left1[max1],right1[max1];
int maxsum(int n)
{
    FOR(i,n){
    left1[i]=right1[i]=0;
    }
    stack<int> s1,s2;
    FOR(i,n){
        while(s1.size() != 0 && arr[s1.top()]<=arr[i]) {
            left1[i] += left1[s1.top()] + 1;
            s1.pop();
        }
        s1.push(i);
    }
    for (int i = n - 1; i >= 0; i--) {
        while (s2.size() != 0 && arr[s2.top()] < arr[i]) {
            right1[i] += right1[s2.top()] + 1;
            s2.pop();
        }
        s2.push(i);
    }

    int ans = 0;

    for (int i = 0; i < n; i++)
        ans += (left1[i] + 1) * (right1[i] + 1) * arr[i];

    return ans;
}
int minsum(int n)
{ FOR(i,n){
    left1[i]=right1[i]=0;
    }
    stack<int> s1,s2;
    FOR(i,n){
        while(s1.size() != 0 && arr[s1.top()]>arr[i]) {
            left1[i] += left1[s1.top()] + 1;
            s1.pop();
        }
        s1.push(i);
    }
    for (int i = n - 1; i >= 0; i--) {
        while (s2.size() != 0 && arr[s2.top()] >= arr[i]) {
            right1[i] += right1[s2.top()] + 1;
            s2.pop();
        }
        s2.push(i);
    }

    int ans = 0;

    for (int i = 0; i < n; i++)
        ans += (left1[i] + 1) * (right1[i] + 1) * arr[i];
    return ans;
}
void solve(){

 int n;
 cin>>n;
 FOR(i,n){
 cin>>arr[i];}
 int maxs=maxsum(n);
 int mins=minsum(n);
 out1(maxs-mins);
}
signed main(){
    fast();
    solve();

}
, In this Programming Language: C++, Now tell me if this Code is compilable or not?