#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"q"<<endl;
#define MX 1111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-8;
int w[MX],n;
int l[MX],r[MX];
int main()
{
    while(cin>>n)
    {
        LL ans=0;
        for(int i=1; i<=n; i++)scanf("%d",&w[i]);
        w[0]=0;
        for(int i=1; i<=n; i++)
        {
            if(w[i-1]<=w[i])l[i]=i;
            else
            {
                l[i]=l[i-1];
                while(w[l[i]-1]>w[i])l[i]--;
            }
        }
        w[n+1]=0;
        for(int i=n; i>=1; i--)
        {
            if(w[i+1]<w[i])r[i]=i;
            else
            {
                r[i]=r[i+1];
                while(w[r[i]+1]>=w[i])r[i]++;
            }
        }
        for(int i=1; i<=n; i++)
        {
            //cout<<l[i]<<" "<<r[i]<<endl;
            ans-=(LL)w[i]*(i-l[i]+1)*(r[i]-i+1);
        }

        w[0]=INF;
        for(int i=1; i<=n; i++)
        {
            if(w[i-1]>=w[i])l[i]=i;
            else
            {
                l[i]=l[i-1];
                while(w[l[i]-1]<w[i])l[i]--;
            }
        }
        w[n+1]=INF;
        for(int i=n; i>=1; i--)
        {
            if(w[i+1]>w[i])r[i]=i;
            else
            {
                r[i]=r[i+1];
                while(w[r[i]+1]<=w[i])r[i]++;
            }
        }
        for(int i=1; i<=n; i++)
        {
            ans+=(LL)w[i]*(i-l[i]+1)*(r[i]-i+1);
        }
        cout<<ans<<endl;

    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"q"<<endl;
#define MX 1111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-8;
int w[MX],n;
int l[MX],r[MX];
inline int read()
{
    int ret=0,c,f=1;
    for(c=getchar(); !(isdigit(c)||c=='-'); c=getchar());
    if(c=='-') f=-1,c=getchar();
    for(; isdigit(c); c=getchar()) ret=ret*10+c-'0';
    if(f<0) ret=-ret;
    return ret;
}
int main()
{
    while(cin>>n)
    {
        LL ans=0;
        for(int i=1; i<=n; i++)w[i]=read();
        w[0]=0;
        for(int i=1; i<=n; i++)
        {
            if(w[i-1]<=w[i])l[i]=i;
            else
            {
                l[i]=l[i-1];
                while(w[l[i]-1]>w[i])l[i]--;
            }
        }
        w[n+1]=0;
        for(int i=n; i>=1; i--)
        {
            if(w[i+1]<w[i])r[i]=i;
            else
            {
                r[i]=r[i+1];
                while(w[r[i]+1]>=w[i])r[i]++;
            }
        }
        for(int i=1; i<=n; i++)
        {
            //cout<<l[i]<<" "<<r[i]<<endl;
            ans-=(LL)w[i]*(i-l[i]+1)*(r[i]-i+1);
        }

        w[0]=INF;
        for(int i=1; i<=n; i++)
        {
            if(w[i-1]>=w[i])l[i]=i;
            else
            {
                l[i]=l[i-1];
                while(w[l[i]-1]<w[i])l[i]--;
            }
        }
        w[n+1]=INF;
        for(int i=n; i>=1; i--)
        {
            if(w[i+1]>w[i])r[i]=i;
            else
            {
                r[i]=r[i+1];
                while(w[r[i]+1]<=w[i])r[i]++;
            }
        }
        for(int i=1; i<=n; i++)
        {
            ans+=(LL)w[i]*(i-l[i]+1)*(r[i]-i+1);
        }
        cout<<ans<<endl;

    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"q"<<endl;
#define MX 1111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-8;
int w[MX],n;
int l[MX],r[MX];
int main()
{
    while(cin>>n)
    {
        LL ans=0;
        for(int i=1; i<=n; i++)scanf("%d",&w[i]);
        w[0]=0;
        for(int i=1; i<=n; i++)
        {
            if(w[i-1]<=w[i])l[i]=i;
            else
            {
                l[i]=l[i-1];
                while(w[l[i]-1]>w[i])l[i]--;
            }
        }
        w[n+1]=0;
        for(int i=n; i>=1; i--)
        {
            if(w[i+1]<=w[i])r[i]=i;
            else
            {
                r[i]=r[i+1];
                while(w[r[i]+1]>=w[i])r[i]++;
            }
        }
        for(int i=1; i<=n; i++)
        {
            //cout<<l[i]<<" "<<r[i]<<endl;
            ans-=(LL)w[i]*(i-l[i]+1)*(r[i]-i+1);
        }

        w[0]=INF;
        for(int i=1; i<=n; i++)
        {
            if(w[i-1]>=w[i])l[i]=i;
            else
            {
                l[i]=l[i-1];
                while(w[l[i]-1]<w[i])l[i]--;
            }
        }
        w[n+1]=INF;
        for(int i=n; i>=1; i--)
        {
            if(w[i+1]>=w[i])r[i]=i;
            else
            {
                r[i]=r[i+1];
                while(w[r[i]+1]<=w[i])r[i]++;
            }
        }
        for(int i=1; i<=n; i++)
        {
            ans+=(LL)w[i]*(i-l[i]+1)*(r[i]-i+1);
        }
        cout<<ans<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define Min(a,b,c) min((a),min((b),(c)))
#define Max(a,b,c) max((a),max((b),(c)))
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define EPS 1e-11
#define N 505
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi 3.14159265359
using namespace std;
typedef long long LL;
pii a[1000005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int b;
		scanf("%d",&b);
		a[i]=mp(b,i);
	}
	sort(a,a+n);
	set<pii> s;
	s.insert(mp(0,n-1));
	LL ans=0;
	for(int i=n-1;i>=0;i--){
		set<pii>::iterator it=s.find(mp(a[i].y,1e9));
		it--;
		ans+=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	}
//	printf("%I64d\n",ans);
	s.clear();
	s.insert(mp(0,n-1));
	for(int i=0;i<n;i++){
		set<pii>::iterator it=s.find(mp(a[i].y,1e9));
		it--;
		ans-=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	}
	printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define Min(a,b,c) min((a),min((b),(c)))
#define Max(a,b,c) max((a),max((b),(c)))
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define EPS 1e-11
#define N 505
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi 3.14159265359
#define right RRR
#define left LLL
using namespace std;
typedef long long LL;
	int right[1000006],left[1000006];
		int a[1000000];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&a[i]);
	vector<pii> v;
	v.pb(mp(a[0],0));
	for(int i=1;i<n;i++){
		while(!v.empty()){
			if(v.back().x>=a[i])
			break;
			right[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		right[v.back().y]=n;
		v.pop_back();
	}
	v.pb(mp(a[n-1],n-1));
	for(int i=n-2;i>=0;i--){
		while(!v.empty()){
			if(v.back().x>a[i])
			break;
			left[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		left[v.back().y]=-1;
		v.pop_back();
	}
	LL ans=0;
	for(int i=0;i<n;i++){
		ans+=(LL)a[i]*(i-left[i])*(right[i]-i);
	}
//	printf("%I64d\n",ans);
	v.pb(mp(a[0],0));
	for(int i=1;i<n;i++){
		while(!v.empty()){
			if(v.back().x<=a[i])
			break;
			right[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		right[v.back().y]=n;
		v.pop_back();
	}
	v.pb(mp(a[n-1],n-1));
	for(int i=n-2;i>=0;i--){
		while(!v.empty()){
			if(v.back().x<a[i])
			break;
			left[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		left[v.back().y]=-1;
		v.pop_back();
	}
	for(int i=0;i<n;i++){
		printf("%d %d\n",left[i],right[i]);
		ans-=(LL)a[i]*(i-left[i])*(right[i]-i);
	}
	printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define Min(a,b,c) min((a),min((b),(c)))
#define Max(a,b,c) max((a),max((b),(c)))
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define EPS 1e-11
#define N 505
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi 3.14159265359
using namespace std;
typedef long long LL;
pii a[1000005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int b;
		scanf("%d",&b);
		a[i]=mp(b,i);
	}
	sort(a,a+n);
	set<pii> s;
	s.insert(mp(0,n-1));
	LL ans=0;
	for(int i=n-1;i>=0;i--){
		set<pii>::iterator it=upper_bound(s.begin(),s.end(),mp(a[i].y,a[i].y));
		it--;
		ans+=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	}
//	printf("%I64d\n",ans);
	s.clear();
	s.insert(mp(0,n-1));
	for(int i=0;i<n;i++){
		set<pii>::iterator it=upper_bound(s.begin(),s.end(),mp(a[i].y,a[i].y));
		it--;
		ans-=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	}
	printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define Min(a,b,c) min((a),min((b),(c)))
#define Max(a,b,c) max((a),max((b),(c)))
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define EPS 1e-11
#define N 505
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi 3.14159265359
#define right RRR
#define left LLL
using namespace std;
typedef long long LL;
	int right[1000006],left[1000006];
		int a[1000000];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&a[i]);
	vector<pii> v;
	v.pb(mp(a[0],0));
	for(int i=1;i<n;i++){
		while(!v.empty()){
			if(v.back().x>=a[i])
			break;
			right[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		right[v.back().y]=n;
		v.pop_back();
	}
	v.pb(mp(a[n-1],n-1));
	for(int i=n-2;i>=0;i--){
		while(!v.empty()){
			if(v.back().x>a[i])
			break;
			left[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		left[v.back().y]=-1;
		v.pop_back();
	}
	LL ans=0;
	for(int i=0;i<n;i++){
		ans+=(LL)a[i]*(i-left[i])*(right[i]-i);
	}
//	printf("%I64d\n",ans);
	v.pb(mp(a[0],0));
	for(int i=1;i<n;i++){
		while(!v.empty()){
			if(v.back().x<=a[i])
			break;
			right[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		right[v.back().y]=n;
		v.pop_back();
	}
	v.pb(mp(a[n-1],n-1));
	for(int i=n-2;i>=0;i--){
		while(!v.empty()){
			if(v.back().x<a[i])
			break;
			left[v.back().y]=i;
			v.pop_back();
		}
		v.pb(mp(a[i],i));
	}
	while(!v.empty()){
		left[v.back().y]=-1;
		v.pop_back();
	}
	for(int i=0;i<n;i++){
		//printf("%d %d\n",left[i],right[i]);
		ans-=(LL)a[i]*(i-left[i])*(right[i]-i);
	}
	printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define Min(a,b,c) min((a),min((b),(c)))
#define Max(a,b,c) max((a),max((b),(c)))
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define EPS 1e-11
#define N 505
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi 3.14159265359
using namespace std;
typedef long long LL;
pii a[1000005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int b;
		scanf("%d",&b);
	//	b=rand();
		a[i]=mp(b,i);
	}
	sort(a,a+n);
	set<pii> s;
	s.clear();
	s.insert(mp(0,n-1));
	LL ans=0;
	for(int i=n-1;i>=0;i--){
		set<pii>::iterator it=s.upper_bound(mp(a[i].y,1000000000));
		it--;
	//	printf("%d %d %d\n",it->x,it->y,a[i].y);
		ans+=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	//	printf("%I64d\n",ans);
	}
//	printf("%I64d\n",ans);
	s.clear();
	s.insert(mp(0,n-1));
	for(int i=0;i<n;i++){
		set<pii>::iterator it=s.lower_bound(mp(a[i].y,1000000000));
		it--;
		ans-=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	}
	printf("%I64d\n",ans);
}
/*
12321   3*9=27
*/


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define Min(a,b,c) min((a),min((b),(c)))
#define Max(a,b,c) max((a),max((b),(c)))
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define EPS 1e-11
#define N 505
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi 3.14159265359
using namespace std;
typedef long long LL;
pii a[1000005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int b;
		scanf("%d",&b);
		a[i]=mp(b,i);
	}
	sort(a,a+n);
	set<pii> s;
	s.clear();
	s.insert(mp(0,n-1));
	LL ans=0;
	for(int i=n-1;i>=0;i--){
		set<pii>::iterator it=upper_bound(s.begin(),s.end(),mp(a[i].y,1000000000));
		it--;
	//	printf("%d %d %d\n",it->x,it->y,a[i].y);
		ans+=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	//	printf("%I64d\n",ans);
	}
//	printf("%I64d\n",ans);
	s.clear();
	s.insert(mp(0,n-1));
	for(int i=0;i<n;i++){
		set<pii>::iterator it=lower_bound(s.begin(),s.end(),mp(a[i].y,1000000000));
		it--;
		ans-=(LL)(it->y-a[i].y+1)*(a[i].y-it->x+1)*a[i].x;
		pii pa=mp(it->x,a[i].y-1),pb=mp(a[i].y+1,it->y);
		s.erase(*it);
		if(pa.x<=pa.y)
		s.insert(pa);
		if(pb.x<=pb.y)
		s.insert(pb);
	}
	printf("%I64d\n",ans);
}
/*
12321   3*9=27
*/


#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
#define N 1000006
long long n, a[N] = {}, lmin[N] = {}, lmax[N] = {}, rmin[N] = {}, rmax[N] = {};
stack<long long> mins, maxs;
void l2r()
{
    for (long long i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if (i == 0)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] < a[mins.top()])
            {
                rmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] > a[maxs.top()])
            {
                rmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }
    while (!mins.empty())
    {
        rmin[mins.top()] = n;
        mins.pop();
    }
    while (!maxs.empty())
    {
        rmax[maxs.top()] = n;
        maxs.pop();
    }
}
void r2l()
{
    for (long long i = n - 1; i >= 0; i--)
    {
        if (i == n - 1)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] <= a[mins.top()])
            {
                lmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] >= a[maxs.top()])
            {
                lmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }

    while (!mins.empty())
    {
        lmin[mins.top()] = -1;
        mins.pop();
    }
    while (!maxs.empty())
    {
        lmax[maxs.top()] = -1;
        maxs.pop();
    }
}
void input()
{
    cin >> n;
    l2r();
    r2l();
}
void output()
{
    for (long long i = 0; i < n; i++)
    {
        cout << rmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << rmin[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmin[i] << " ";
    }
    cout << endl;
}
int main()
{
    long long ans = 0;
    freopen("out.txt", "r", stdin);
    input();
    for (long long i = 0; i < n; i++)
    {
        ans += ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) * a[i];
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
#define N 1000006
long long n, a[N] = {}, lmin[N] = {}, lmax[N] = {}, rmin[N] = {}, rmax[N] = {};
stack<long long> mins, maxs;
void l2r()
{
    for (long long i = 0; i < n; i++)
    {
        scanf("%I64d", &a[i]);
        if (i == 0)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] < a[mins.top()])
            {
                rmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] > a[maxs.top()])
            {
                rmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }
    while (!mins.empty())
    {
        rmin[mins.top()] = n;
        mins.pop();
    }
    while (!maxs.empty())
    {
        rmax[maxs.top()] = n;
        maxs.pop();
    }
}
void r2l()
{
    for (long long i = n - 1; i >= 0; i--)
    {
        if (i == n - 1)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] <= a[mins.top()])
            {
                lmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] >= a[maxs.top()])
            {
                lmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }

    while (!mins.empty())
    {
        lmin[mins.top()] = -1;
        mins.pop();
    }
    while (!maxs.empty())
    {
        lmax[maxs.top()] = -1;
        maxs.pop();
    }
}
void input()
{
    cin >> n;
    l2r();
    r2l();
}
void output()
{
    for (long long i = 0; i < n; i++)
    {
        cout << rmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << rmin[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmin[i] << " ";
    }
    cout << endl;
}
int main()
{
    long long ans = 0;
    input();
    for (long long i = 0; i < n; i++)
    {
        ans += ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) * a[i];
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stack>
using namespace std;
#define N 1000006
long long n, a[N] = {}, lmin[N] = {}, lmax[N] = {}, rmin[N] = {}, rmax[N] = {};
stack<long long> mins, maxs;
void l2r()
{
    for (long long i = 0; i < n; i++)
    {
        cin >> a[i];
        if (i == 0)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] < a[mins.top()])
            {
                rmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] > a[maxs.top()])
            {
                rmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }
    while (!mins.empty())
    {
        rmin[mins.top()] = n;
        mins.pop();
    }
    while (!maxs.empty())
    {
        rmax[maxs.top()] = n;
        maxs.pop();
    }
}
void r2l()
{
    for (long long i = n - 1; i >= 0; i--)
    {
        if (i == n - 1)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] <= a[mins.top()])
            {
                lmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] >= a[maxs.top()])
            {
                lmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }

    while (!mins.empty())
    {
        lmin[mins.top()] = -1;
        mins.pop();
    }
    while (!maxs.empty())
    {
        lmax[maxs.top()] = -1;
        maxs.pop();
    }
}
void input()
{
    cin >> n;
    l2r();
    r2l();
}
void output()
{
    for (long long i = 0; i < n; i++)
    {
        cout << rmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << rmin[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmin[i] << " ";
    }
    cout << endl;
}
int main()
{
    long long ans = 0;
    input();
    for (long long i = 0; i < n; i++)
    {
        //        cout << ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) << " ";
        ans += ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) * a[i];
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stack>
using namespace std;
#define N 1000006
int n, a[N] = {}, lmin[N] = {}, lmax[N] = {}, rmin[N] = {}, rmax[N] = {};
stack<int> mins, maxs;
void l2r()
{
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (i == 0)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] < a[mins.top()])
            {
                rmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] > a[maxs.top()])
            {
                rmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }
    while (!mins.empty())
    {
        rmin[mins.top()] = n;
        mins.pop();
    }
    while (!maxs.empty())
    {
        rmax[maxs.top()] = n;
        maxs.pop();
    }
}
void r2l()
{
    for (int i = n - 1; i >= 0; i--)
    {
        if (i == n - 1)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] <= a[mins.top()])
            {
                lmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] >= a[maxs.top()])
            {
                lmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }

    while (!mins.empty())
    {
        lmin[mins.top()] = -1;
        mins.pop();
    }
    while (!maxs.empty())
    {
        lmax[maxs.top()] = -1;
        maxs.pop();
    }
}
void input()
{
    cin >> n;
    l2r();
    r2l();
}
void output()
{
    for (int i = 0; i < n; i++)
    {
        cout << rmax[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << rmin[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << lmax[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << lmin[i] << " ";
    }
    cout << endl;
}
int main()
{
    int ans = 0;
    input();
    for (int i = 0; i < n; i++)
    {
        //        cout << ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) << " ";
        ans += ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) * a[i];
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stack>
using namespace std;
#define N 1000006
int n, a[N] = {}, lmin[N] = {}, lmax[N] = {}, rmin[N] = {}, rmax[N] = {};
stack<int> mins, maxs;
void l2r()
{
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (i == 0)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (a[i] < a[mins.top()] && !mins.empty())
            {
                rmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (a[i] > a[maxs.top()] && !maxs.empty())
            {
                rmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }
    while (!mins.empty())
    {
        rmin[mins.top()] = n;
        mins.pop();
    }
    while (!maxs.empty())
    {
        rmax[maxs.top()] = n;
        maxs.pop();
    }
}
void r2l()
{
    for (int i = n - 1; i >= 0; i--)
    {
        if (i == n - 1)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (a[i] <= a[mins.top()] && !mins.empty())
            {
                lmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (a[i] >= a[maxs.top()] && !maxs.empty())
            {
                lmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }

    while (!mins.empty())
    {
        lmin[mins.top()] = -1;
        mins.pop();
    }
    while (!maxs.empty())
    {
        lmax[maxs.top()] = -1;
        maxs.pop();
    }
}
void input()
{
    cin >> n;
    l2r();
    r2l();
}
void output()
{
    for (int i = 0; i < n; i++)
    {
        cout << rmax[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << rmin[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << lmax[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << lmin[i] << " ";
    }
    cout << endl;
}
int main()
{
    int ans = 0;
    input();
    for (int i = 0; i < n; i++)
    {
        //        cout << ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) << " ";
        ans += ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) * a[i];
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
#define N 1000006
long long n, a[N] = {}, lmin[N] = {}, lmax[N] = {}, rmin[N] = {}, rmax[N] = {};
stack<long long> mins, maxs;
void l2r()
{
    for (long long i = 0; i < n; i++)
    {
        scanf("%I64d", &a[i]);
        if (i == 0)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] < a[mins.top()])
            {
                rmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] > a[maxs.top()])
            {
                rmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }
    while (!mins.empty())
    {
        rmin[mins.top()] = n;
        mins.pop();
    }
    while (!maxs.empty())
    {
        rmax[maxs.top()] = n;
        maxs.pop();
    }
}
void r2l()
{
    for (long long i = n - 1; i >= 0; i--)
    {
        if (i == n - 1)
        {
            mins.push(i);
            maxs.push(i);
        }
        else
        {
            while (!mins.empty() && a[i] <= a[mins.top()])
            {
                lmin[mins.top()] = i;
                mins.pop();
            }
            mins.push(i);
            while (!maxs.empty() && a[i] >= a[maxs.top()])
            {
                lmax[maxs.top()] = i;
                maxs.pop();
            }
            maxs.push(i);
        }
    }

    while (!mins.empty())
    {
        lmin[mins.top()] = -1;
        mins.pop();
    }
    while (!maxs.empty())
    {
        lmax[maxs.top()] = -1;
        maxs.pop();
    }
}
void input()
{
    cin >> n;
    l2r();
    r2l();
}
void output()
{
    for (long long i = 0; i < n; i++)
    {
        cout << rmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << rmin[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmax[i] << " ";
    }
    cout << endl;

    for (long long i = 0; i < n; i++)
    {
        cout << lmin[i] << " ";
    }
    cout << endl;
}
int main()
{
    long long ans = 0;
    freopen("out.txt", "r", stdin);
    input();
    for (long long i = 0; i < n; i++)
    {
        ans += ((i - lmax[i]) * (rmax[i] - i) - (i - lmin[i]) * (rmin[i] - i)) * a[i];
    }
    cout << ans << endl;
    return 0;
}


/*This Code Is Dirty And Not Proper For Under 18*/
/*I've become so numb, I can't feel you there*/
#include <bits/stdc++.h>

#define pb push_back
#define int long long
#define kill(x) return cout << x << endl , 0;

using namespace std;

const int inf = 1e9 + 1;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int n , a[maxn] , ans1 , ans2;

void solve(int l , int r){
	if(l == r-1)
		return;
	int mid = (l + r)/2;
	vector<int> v1ma ,v1mn, v2ma,v2mn;
	int mx = 0;
	for(int i = mid-1 ; i >= l ; i--){
		mx = max(mx , a[i]);
		v1ma.pb(mx);
	}
	int mn = inf;
	for(int i = mid-1 ; i >= l ; i--){
		mn = min(mn , a[i]);
		v1mn.pb(mn);
	}
	mx = 0;
	for(int i = mid ; i < r ; i++){
		mx = max(mx , a[i]);
		v2ma.pb(mx);
	}
	mn = inf;
	for(int i = mid ; i < r ; i++){
		mn = min(mn , a[i]);
		v2mn.pb(mn);
	}
	int j = 0;
	sort(v1ma.begin() , v1ma.end());
	sort(v2ma.begin() , v2ma.end());
	for(int i = 0 ; i < v1ma.size() ; i++){
		while(true){
			if(j == v2ma.size())
				break;
			if(v1ma[i] < v2ma[j])
				break;
			if(v1ma[i] == v2ma[j])
				ans1 -= v1ma[i];
			j++;
		}
		ans1 += v1ma[i]*j;
	}
	j = 0;
	for(int i = 0 ; i < v2ma.size() ; i++){
		while(true){
			if(j == v1ma.size())
				break;
			if(v2ma[i] < v1ma[j])
				break;
			j++;
		}
		ans1 += v2ma[i]*j;
	}
	j = 0;
	sort(v1mn.begin() , v1mn.end());
	sort(v2mn.begin() , v2mn.end());
	reverse(v1mn.begin() , v1mn.end());
	reverse(v2mn.begin() , v2mn.end());
	for(int i = 0 ; i < v1mn.size() ; i++){
		while(true){
			if(j == v2mn.size())
				break;
			if(v1mn[i] > v2mn[i])
				break;
			if(v1mn[i] == v2mn[i])
				ans2 -= v1mn[i];
			j++;
		}
		ans2 += v1mn[i] * j;
	}
	j = 0;
	for(int i = 0 ; i < v2mn.size() ; i++){
		while(true){
			if(j == v1mn.size())
				break;
			if(v2mn[i] > v1mn[i])
				break;
			j++;
		}
		ans2 += v2mn[i] * j;
	}
	solve(l , mid);
	solve(mid , r);
	return;
}

int32_t main(){
	//ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 0 ; i < n ; i++)
		cin >> a[i];
	solve(0 , n);
	cout << ans1 - ans2 << endl;
	return 0;
}


/*This Code Is Dirty And Not Proper For Under 18*/
/*I've become so numb, I can't feel you there*/
#include <bits/stdc++.h>

#define pb push_back
#define int long long
#define kill(x) return cout << x << endl , 0;

using namespace std;

const int inf = 1e9 + 1;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int n , a[maxn] , ans1 , ans2;

void solve(int l , int r){
	if(l == r-1)
		return;
	int mid = (l + r)/2;
	vector<int> v1ma ,v1mn, v2ma,v2mn;
	int mx = 0;
	for(int i = mid-1 ; i >= l ; i--){
		mx = max(mx , a[i]);
		v1ma.pb(mx);
	}
	int mn = inf;
	for(int i = mid-1 ; i >= l ; i--){
		mn = min(mn , a[i]);
		v1mn.pb(mn);
	}
	mx = 0;
	for(int i = mid ; i < r ; i++){
		mx = max(mx , a[i]);
		v2ma.pb(mx);
	}
	mn = inf;
	for(int i = mid ; i < r ; i++){
		mn = min(mn , a[i]);
		v2mn.pb(mn);
	}
	int j = 0;
	sort(v1ma.begin() , v1ma.end());
	sort(v2ma.begin() , v2ma.end());
	for(int i = 0 ; i < v1ma.size() ; i++){
		while(true){
			if(j == v2ma.size())
				break;
			if(v1ma[i] < v2ma[j])
				break;
			j++;
		}
		ans1 += v1ma[i]*j;
	}
	j = 0;
	for(int i = 0 ; i < v2ma.size() ; i++){
		while(true){
			if(j == v1ma.size())
				break;
			if(v2ma[i] < v1ma[j])
				break;
			if(v2ma[i] == v1ma[j])
				break;
			j++;
		}
		ans1 += v2ma[i]*j;
	}
	j = 0;
	sort(v1mn.begin() , v1mn.end());
	sort(v2mn.begin() , v2mn.end());
	reverse(v1mn.begin() , v1mn.end());
	reverse(v2mn.begin() , v2mn.end());
	for(int i = 0 ; i < v1mn.size() ; i++){
		while(true){
			if(j == v2mn.size())
				break;
			if(v1mn[i] > v2mn[j])
				break;
			j++;
		}
		ans2 += v1mn[i] * j;
	}
	j = 0;
	for(int i = 0 ; i < v2mn.size() ; i++){
		while(true){
			if(j == v1mn.size())
				break;
			if(v2mn[i] > v1mn[j])
				break;
			if(v2mn[i] == v1mn[j])
				break;
			j++;
		}
		ans2 += v2mn[i] * j;
	}
	solve(l , mid);
	solve(mid , r);
	return;
}

int32_t main(){
	//ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 0 ; i < n ; i++)
		cin >> a[i];
	solve(0 , n);
	cout << ans1 - ans2 << endl;
	return 0;
}


/*This Code Is Dirty And Not Proper For Under 18*/
/*I've become so numb, I can't feel you there*/
#include <bits/stdc++.h>

#define pb push_back
#define int long long
#define kill(x) return cout << x << endl , 0;

using namespace std;

const int inf = 1e9 + 1;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int n , a[maxn] , ans1 , ans2;

void solve(int l , int r){
	if(l == r-1)
		return;
	int mid = (l + r)/2;
	vector<int> v1ma ,v1mn, v2ma,v2mn;
	int mx = 0;
	for(int i = mid-1 ; i >= l ; i--){
		mx = max(mx , a[i]);
		v1ma.pb(mx);
	}
	int mn = inf;
	for(int i = mid-1 ; i >= l ; i--){
		mn = min(mn , a[i]);
		v1mn.pb(mn);
	}
	mx = 0;
	for(int i = mid ; i < r ; i++){
		mx = max(mx , a[i]);
		v2ma.pb(mx);
	}
	mn = inf;
	for(int i = mid ; i < r ; i++){
		mn = min(mn , a[i]);
		v2mn.pb(mn);
	}
	int j = 0;
	sort(v1ma.begin() , v1ma.end());
	sort(v2ma.begin() , v2ma.end());
	for(int i = 0 ; i < v1ma.size() ; i++){
		while(true){
			if(j == v2ma.size())
				break;
			if(v1ma[i] < v2ma[j])
				break;
			j++;
		}
		ans1 += v1ma[i]*j;
	}
	j = 0;
	for(int i = 0 ; i < v2ma.size() ; i++){
		while(true){
			if(j == v1ma.size())
				break;
			if(v2ma[i] < v1ma[j])
				break;
			if(v2ma[i] == v1ma[j])
				break;
			j++;
		}
		ans1 += v2ma[i]*j;
	}
	j = 0;
	sort(v1mn.begin() , v1mn.end());
	sort(v2mn.begin() , v2mn.end());
	reverse(v1mn.begin() , v1mn.end());
	reverse(v2mn.begin() , v2mn.end());
	for(int i = 0 ; i < v1mn.size() ; i++){
		while(true){
			if(j == v2mn.size())
				break;
			if(v1mn[i] > v2mn[j])
				break;
			j++;
		}
		ans2 += v1mn[i] * j;
	}
	j = 0;
	for(int i = 0 ; i < v2mn.size() ; i++){
		while(true){
			if(j == v1mn.size())
				break;
			if(v2mn[i] > v1mn[j])
				break;
			if(v2mn[i] == v1mn[j])
				break;
			j++;
		}
		ans2 += v2mn[i] * j;
	}
	solve(l , mid);
	solve(mid , r);
	return;
}

int32_t main(){
	//ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	scanf("%lld" , &n);
	for(int i = 0 ; i < n ; i++)
		scanf("%lld" , &a[i]);
	solve(0 , n);
	cout << ans1 - ans2 << endl;
	return 0;
}


/*This Code Is Dirty And Not Proper For Under 18*/
/*I've become so numb, I can't feel you there*/
#include <bits/stdc++.h>

#define pb push_back
#define int long long
#define kill(x) return cout << x << endl , 0;

using namespace std;

const int inf = 1e9 + 1;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int n , a[maxn] , ans1 , ans2;

void solve(int l , int r){
	if(l == r-1)
		return;
	int mid = (l + r)/2;
	vector<int> v1ma ,v1mn, v2ma,v2mn;
	int mx = 0;
	for(int i = mid-1 ; i >= l ; i--){
		mx = max(mx , a[i]);
		v1ma.pb(mx);
	}
	int mn = inf;
	for(int i = mid-1 ; i >= l ; i--){
		mn = min(mn , a[i]);
		v1mn.pb(mn);
	}
	mx = 0;
	for(int i = mid ; i < r ; i++){
		mx = max(mx , a[i]);
		v2ma.pb(mx);
	}
	mn = inf;
	for(int i = mid ; i < r ; i++){
		mn = min(mn , a[i]);
		v2mn.pb(mn);
	}
	int j = 0;
	sort(v1ma.begin() , v1ma.end());
	sort(v2ma.begin() , v2ma.end());
	for(int i = 0 ; i < v1ma.size() ; i++){
		while(true){
			if(j == v2ma.size())
				break;
			if(v1ma[i] < v2ma[j])
				break;
			if(v1ma[i] == v2ma[j])
				ans1 -= v1ma[i];
			j++;
		}
		ans1 += v1ma[i]*j;
	}
	j = 0;
	for(int i = 0 ; i < v2ma.size() ; i++){
		while(true){
			if(j == v1ma.size())
				break;
			if(v2ma[i] < v1ma[j])
				break;
			j++;
		}
		ans1 += v2ma[i]*j;
	}
	j = 0;
	sort(v1mn.begin() , v1mn.end());
	sort(v2mn.begin() , v2mn.end());
	reverse(v1mn.begin() , v1mn.end());
	reverse(v2mn.begin() , v2mn.end());
	for(int i = 0 ; i < v1mn.size() ; i++){
		while(true){
			if(j == v2mn.size())
				break;
			if(v1mn[i] > v2mn[j])
				break;
			if(v1mn[i] == v2mn[j])
				ans2 -= v1mn[i];
			j++;
		}
		ans2 += v1mn[i] * j;
	}
	j = 0;
	for(int i = 0 ; i < v2mn.size() ; i++){
		while(true){
			if(j == v1mn.size())
				break;
			if(v2mn[i] > v1mn[j])
				break;
			j++;
		}
		ans2 += v2mn[i] * j;
	}
	solve(l , mid);
	solve(mid , r);
	return;
}

int32_t main(){
	//ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 0 ; i < n ; i++)
		cin >> a[i];
	solve(0 , n);
	cout << ans1 - ans2 << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
//#define endl "\n"

typedef long long ll;
const int MAXINT = 2147483640;
const ll MAXLL = 9223372036854775800LL;
const ll MAXN = 5000000;
using namespace std;

struct tree{
    int l, r;
};

vector < pair < int, int > > v;
tree t[MAXN], g[MAXN];
ll ans = 0;

void push(int v){
    if (g[v].l == 0 && g[v].r == 0) return;
    t[v * 2] = t[v * 2 + 1] = g[v];
    g[v * 2] = g[v * 2 + 1] = g[v];
    tree w;
    w.l = 0;
    w.r = 0;
    g[v] = w;
}

void modify(int v, int tl, int tr, int l, int r, tree val){
    if (tl == l && tr == r){
        t[v] = g[v] = val;
        return;
    }
    if (l > r) return;
    push(v);
    int tm = (tl + tr) / 2;
    modify(v * 2, tl, tm, l, min(r, tm), val);
    modify(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
}

tree get(int v, int tl, int tr, int pos){
    if (tl == tr) return t[v];
    push(v);
    int tm = (tl + tr) / 2;
    if (pos <= tm) return get(v * 2, tl, tm, pos); else
        return get(v * 2 + 1, tm + 1, tr, pos);
}
int main()
{
//    freopen("1.in", "r", stdin);
//    freopen("1.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    int n, i, a;
    cin >> n;
    for (i = 1; i <= n; i++){
        cin >> a;
        v.p_b(m_p(a, i));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    tree q, x;
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        if (q.l > q.r) continue;
        ans += ll(it -> fi * ll(it -> se - q.l + 1) * ll(q.r - it -> se + 1));
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, x.l, x.r, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, x.l, x.r, x);
    }

    reverse(v.begin(), v.end());
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        ans -= ll(it -> fi * ll(it -> se - q.l + 1) * ll(q.r - it -> se + 1));
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, q.l, it -> se, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, it -> se, q.r, x);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
//#define endl "\n"

typedef long long ll;
const int MAXINT = 2147483640;
const ll MAXLL = 9223372036854775800LL;
const ll MAXN = 5000000;
using namespace std;

struct tree{
    int l, r;
};

vector < pair < int, int > > v;
tree t[MAXN], g[MAXN];
ll ans = 0;

void push(int v){
    if (g[v].l == 0 && g[v].r == 0) return;
    t[v * 2] = t[v * 2 + 1] = g[v];
    g[v * 2] = g[v * 2 + 1] = g[v];
    tree w;
    w.l = 0;
    w.r = 0;
    g[v] = w;
}

void modify(int v, int tl, int tr, int l, int r, tree val){
    if (tl == l && tr == r){
        t[v] = g[v] = val;
        return;
    }
    if (l > r) return;
    push(v);
    int tm = (tl + tr) / 2;
    modify(v * 2, tl, tm, l, min(r, tm), val);
    modify(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
}

tree get(int v, int tl, int tr, int pos){
    if (tl == tr) return t[v];
    push(v);
    int tm = (tl + tr) / 2;
    if (pos <= tm) return get(v * 2, tl, tm, pos); else
        return get(v * 2 + 1, tm + 1, tr, pos);
}
int main()
{
//    freopen("1.in", "r", stdin);
//    freopen("1.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    ll n, i, a;
    cin >> n;
    for (i = 1; i <= n; i++){
        cin >> a;
        v.p_b(m_p(a, i));
    }
    sort(v.begin(), v.end());
//    reverse(v.begin(), v.end());
    tree q, x;
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        if (q.l > q.r) continue;
        ans -= it -> fi * (it -> se - q.l + 1) * (q.r - it -> se + 1);
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, x.l, x.r, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, x.l, x.r, x);
    }

    reverse(v.begin(), v.end());
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        ans += it -> fi * (it -> se - q.l + 1) * (q.r - it -> se + 1);
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, q.l, it -> se, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, it -> se, q.r, x);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
//#define endl "\n"

typedef long long ll;
const int MAXINT = 2147483640;
const ll MAXLL = 9223372036854775800LL;
const ll MAXN = 5000000;
using namespace std;

struct tree{
    ll l, r;
};

vector < pair < ll, ll > > v;
tree t[MAXN], g[MAXN];
ll ans = 0;

void push(ll v){
    if (g[v].l == 0 && g[v].r == 0) return;
    t[v * 2] = t[v * 2 + 1] = g[v];
    g[v * 2] = g[v * 2 + 1] = g[v];
    tree w;
    w.l = 0;
    w.r = 0;
    g[v] = w;
}

void modify(ll v, ll tl, ll tr, ll l, ll r, tree val){
    if (tl == l && tr == r){
        t[v] = g[v] = val;
        return;
    }
    if (l > r) return;
    push(v);
    ll tm = (tl + tr) / 2;
    modify(v * 2, tl, tm, l, min(r, tm), val);
    modify(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
}

tree get(ll v, ll tl, ll tr, ll pos){
    if (tl == tr) return t[v];
    push(v);
    ll tm = (tl + tr) / 2;
    if (pos <= tm) return get(v * 2, tl, tm, pos); else
        return get(v * 2 + 1, tm + 1, tr, pos);
}
int main()
{
//    freopen("1.in", "r", stdin);
//    freopen("1.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    ll n, i, a;
    cin >> n;
    for (i = 1; i <= n; i++){
        cin >> a;
        v.p_b(m_p(a, i));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    tree q, x;
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < ll, ll > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        if (q.l > q.r) continue;
        ans += it -> fi * (it -> se - q.l + 1) * (q.r - it -> se + 1);
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, x.l, x.r, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, x.l, x.r, x);
    }

    reverse(v.begin(), v.end());
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < ll, ll > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        ans -= it -> fi * (it -> se - q.l + 1) * (q.r - it -> se + 1);
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, q.l, it -> se, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, it -> se, q.r, x);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
//#define endl "\n"

typedef long long ll;
const int MAXINT = 2147483640;
const ll MAXLL = 9223372036854775800LL;
const ll MAXN = 5000000;
using namespace std;

struct tree{
    int l, r;
};

vector < pair < int, int > > v;
tree t[MAXN], g[MAXN];
ll ans = 0;

void push(int v){
    if (g[v].l == 0 && g[v].r == 0) return;
    t[v * 2] = t[v * 2 + 1] = g[v];
    g[v * 2] = g[v * 2 + 1] = g[v];
    tree w;
    w.l = 0;
    w.r = 0;
    g[v] = w;
}

void modify(int v, int tl, int tr, int l, int r, tree val){
    if (tl == l && tr == r){
        t[v] = g[v] = val;
        return;
    }
    if (l > r) return;
    push(v);
    int tm = (tl + tr) / 2;
    modify(v * 2, tl, tm, l, min(r, tm), val);
    modify(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
}

tree get(int v, int tl, int tr, int pos){
    if (tl == tr) return t[v];
    push(v);
    int tm = (tl + tr) / 2;
    if (pos <= tm) return get(v * 2, tl, tm, pos); else
        return get(v * 2 + 1, tm + 1, tr, pos);
}
int main()
{
//    freopen("1.in", "r", stdin);
//    freopen("1.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    int n, i, a;
    cin >> n;
    for (i = 1; i <= n; i++){
        cin >> a;
        v.p_b(m_p(a, i));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    tree q, x;
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        if (q.l > q.r) continue;
        ans += it -> fi * (it -> se - q.l + 1) * ll(q.r - it -> se + 1);
        if (ans > 1e18) cout << 228 << endl;
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, x.l, x.r, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, x.l, x.r, x);
    }

    reverse(v.begin(), v.end());
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        ans -= it -> fi * (it -> se - q.l + 1) * ll(q.r - it -> se + 1);
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, q.l, it -> se, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, it -> se, q.r, x);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define p_b push_back
#define m_p make_pair
#define fi first
#define se second
//#define endl "\n"

typedef long long ll;
const int MAXINT = 2147483640;
const ll MAXLL = 9223372036854775800LL;
const ll MAXN = 5000000;
using namespace std;

struct tree{
    int l, r;
};

vector < pair < int, int > > v;
tree t[MAXN], g[MAXN];
ll ans = 0;

void push(int v){
    if (g[v].l == 0 && g[v].r == 0) return;
    t[v * 2] = t[v * 2 + 1] = g[v];
    g[v * 2] = g[v * 2 + 1] = g[v];
    tree w;
    w.l = 0;
    w.r = 0;
    g[v] = w;
}

void modify(int v, int tl, int tr, int l, int r, tree val){
    if (tl == l && tr == r){
        t[v] = g[v] = val;
        return;
    }
    if (l > r) return;
    push(v);
    int tm = (tl + tr) / 2;
    modify(v * 2, tl, tm, l, min(r, tm), val);
    modify(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
}

tree get(int v, int tl, int tr, int pos){
    if (tl == tr) return t[v];
    push(v);
    int tm = (tl + tr) / 2;
    if (pos <= tm) return get(v * 2, tl, tm, pos); else
        return get(v * 2 + 1, tm + 1, tr, pos);
}
int main()
{
//    freopen("1.in", "r", stdin);
//    freopen("1.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    ll n, i, a;
    cin >> n;
    for (i = 1; i <= n; i++){
        cin >> a;
        v.p_b(m_p(a, i));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    tree q, x;
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        if (q.l > q.r) continue;
        ans += it -> fi * (it -> se - q.l + 1) * (q.r - it -> se + 1);
        if (ans > 1e18) cout << 228 << endl;
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, x.l, x.r, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, x.l, x.r, x);
    }

    reverse(v.begin(), v.end());
    q.l = 1;
    q.r = n;
    modify(1, 1, n, 1, n, q);
    for (vector < pair < int, int > > :: iterator it = v.begin(); it != v.end(); it++){
        q = get(1, 1, n, it -> se);
        ans -= it -> fi * (it -> se - q.l + 1) * (q.r - it -> se + 1);
        x.l = q.l;
        x.r = it -> se - 1;
        modify(1, 1, n, q.l, it -> se, x);
        x.l = it -> se + 1;
        x.r = q.r;
        modify(1, 1, n, it -> se, q.r, x);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#define MAXN 1e6
using namespace std;

int n;
long long int ans = 0;
vector<int> v(MAXN);
vector<int> vright(MAXN);
vector<int> vleft(MAXN);
pair<int,int> temp;

int main(void){
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &v[i]);

    stack<int> s1;
    for(int i = 0; i < n; i++){
        while(!s1.empty() && v[i] < v[s1.top()]) s1.pop();
        if(s1.empty()) vleft[i] = -1;
        else vleft[i] = s1.top();
        s1.push(i);
    }

    stack<int> s2;
    for(int i = n - 1; i >= 0; i--){
        while(!s2.empty() && v[i] <= v[s2.top()]) s2.pop();
        if(s2.empty()) vright[i] = n;
        else vright[i] = s2.top();
        s2.push(i);
    }
    for(int i = 0; i < n; i++) ans -= v[i] * ((i - vleft[i]) * (vright[i] - i));

    stack<int> s3;
    for(int i = 0; i < n; i++){
        while(!s3.empty() && v[i] > v[s3.top()]) s3.pop();
        if(s3.empty()) vleft[i] = -1;
        else vleft[i] = s3.top();
        s3.push(i);
    }

    stack<int> s4;
    for(int i = n - 1; i >= 0; i--){
        while(!s4.empty() && v[i] >= v[s4.top()]) s4.pop();
        if(s4.empty()) vright[i] = n;
        else vright[i] = s4.top();
        s4.push(i);
    }
    for(int i = 0; i < n; i++) ans += v[i] * ((i - vleft[i]) * (vright[i] - i));

    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#define MAXN 1e6
using namespace std;

int n;
vector<int> v(MAXN);
long long int ans = 0;

int main(void){
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &v[i]);
    sort(v.begin(), v.begin() + n);
    for(int i = 0; i < n; i++){
        int start = i + 1;
        while(v[i] == v[start]) start++;
        for(int j = start; j < n; j++) ans += (v[j] - v[i]) * pow(2, j - i - 1);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#define MAXN 1e6
using namespace std;

int n;
long long int ans = 0;
vector<long long int> v(MAXN);
vector<int> vright(MAXN);
vector<int> vleft(MAXN);
pair<int,int> temp;

int main(void){
    scanf("%d", &n);
    for(int i = 0; i < n; i++) cin >> v[i];

    stack<int> s1;
    for(int i = 0; i < n; i++){
        while(!s1.empty() && v[i] < v[s1.top()]) s1.pop();
        if(s1.empty()) vleft[i] = -1;
        else vleft[i] = s1.top();
        s1.push(i);
    }

    stack<int> s2;
    for(int i = n - 1; i >= 0; i--){
        while(!s2.empty() && v[i] <= v[s2.top()]) s2.pop();
        if(s2.empty()) vright[i] = n;
        else vright[i] = s2.top();
        s2.push(i);
    }
    for(int i = 0; i < n; i++) ans -= v[i] * ((i - vleft[i]) * (vright[i] - i));

    stack<int> s3;
    for(int i = 0; i < n; i++){
        while(!s3.empty() && v[i] > v[s3.top()]) s3.pop();
        if(s3.empty()) vleft[i] = -1;
        else vleft[i] = s3.top();
        s3.push(i);
    }

    stack<int> s4;
    for(int i = n - 1; i >= 0; i--){
        while(!s4.empty() && v[i] >= v[s4.top()]) s4.pop();
        if(s4.empty()) vright[i] = n;
        else vright[i] = s4.top();
        s4.push(i);
    }
    for(int i = 0; i < n; i++) ans += v[i] * ((i - vleft[i]) * (vright[i] - i));

    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#define MAXN 1e6
using namespace std;

int n;
long long int ans = 0;
vector<long long int> v(MAXN);
vector<long long int> vright(MAXN);
vector<long long int> vleft(MAXN);

int main(void){
    scanf("%d", &n);
    for(int i = 0; i < n; i++) cin >> v[i];

    stack<int> s1;
    for(int i = 0; i < n; i++){
        while(!s1.empty() && v[i] < v[s1.top()]) s1.pop();
        if(s1.empty()) vleft[i] = -1;
        else vleft[i] = s1.top();
        s1.push(i);
    }

    stack<int> s2;
    for(int i = n - 1; i >= 0; i--){
        while(!s2.empty() && v[i] <= v[s2.top()]) s2.pop();
        if(s2.empty()) vright[i] = n;
        else vright[i] = s2.top();
        s2.push(i);
    }
    for(int i = 0; i < n; i++) ans -= v[i] * ((i - vleft[i]) * (vright[i] - i));

    stack<int> s3;
    for(int i = 0; i < n; i++){
        while(!s3.empty() && v[i] > v[s3.top()]) s3.pop();
        if(s3.empty()) vleft[i] = -1;
        else vleft[i] = s3.top();
        s3.push(i);
    }

    stack<int> s4;
    for(int i = n - 1; i >= 0; i--){
        while(!s4.empty() && v[i] >= v[s4.top()]) s4.pop();
        if(s4.empty()) vright[i] = n;
        else vright[i] = s4.top();
        s4.push(i);
    }
    for(int i = 0; i < n; i++) ans += v[i] * ((i - vleft[i]) * (vright[i] - i));

    cout << ans;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#define MAXN 1e6
using namespace std;

int n;
vector<int> v(MAXN);
long long int ans = 0;

int main(void){
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &v[i]);
    for(int i = 0; i < n; i++){
        multiset<int> s;
        for(int j = i; j < n; j++)
            s.insert(v[j]), ans += *(--s.end()) - *s.begin();
    }
    cout << ans;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6;
ll a[N],ans,sta[N],h,mil[N],mir[N],mxl[N],mxr[N];
int main()
{
	ll n;
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
	sta[1]=0,h=1;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]>a[sta[h]])sta[++h]=i,mil[i]=i-1;
		else
		{
			while(a[i]<=a[sta[h]])h--;
			mil[i]=sta[h];
			sta[++h]=i;
		}
	}
	sta[1]=n+1;h=1;
	for(ll i=n;i>=1;i--)
	{
		if(a[i]>a[sta[h]])sta[++h]=i,mir[i]=i+1;
		else
		{
			while(a[i]<a[sta[h]])h--;
			mir[i]=sta[h];
			sta[++h]=i;
		}
	}
	a[0]=a[n+1]=MAX;
	sta[1]=0;h=1;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]<a[sta[h]])sta[++h]=i,mxl[i]=i-1;
		else
		{
			while(a[i]>=a[sta[h]])h--;
			mxl[i]=sta[h];
			sta[++h]=i;
		}
	}
	sta[1]=n+1,h=1;
	for(ll i=n;i>=1;i--)
	{
		if(a[i]<a[sta[h]]) sta[++h]=i,mxr[i]=i+1;
		else
		{
			while(a[i]>a[sta[h]]) h--;
			mxr[i]=sta[h];
			sta[++h]=i;
		}
	}
	for(int i=1;i<=n;i++)
		cout<<mil[i]<<' '<<mir[i]<<' '<<mxl[i]<<' '<<mxr[i]<<endl;
	for(ll i=1;i<=n;i++) ans+=a[i]*((i-mxl[i])*(mxr[i]-i)-(i-mil[i])*(mir[i]-i));
	printf("%lld",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
long long q[1001000];
long long n, a[100007], ans;
long long maxl[1000007], maxr[1000007], minl[1000007], minr[1000007];
int main()
{
	scanf("%d", &n);
	for(long long i=1; i<=n; i++) scanf("%d", &a[i]);
	a[0]=2147483647;
    long long top=1;
    for(long long i=1; i<=n; i++)
	{
    	while(a[i]>=a[q[top]])top--;
		maxl[i]=q[top];
		q[++top]=i;
    }
    a[n+1]=2147483647;
	top=1;
	q[1]=n+1;
	for(long long i=n; i>=1; i--)
	{
		while(a[i]>a[q[top]])top--;
		maxr[i]=q[top];
		q[++top]=i;
	}
	for(long long i=1; i<=n; i++)
	{
		ans+=a[i]*(i-maxl[i])*(maxr[i]-i);
	}
	a[0]=-2147483647;
    top=1;
    q[1]=0;
    for(long long i=1; i<=n; i++)
	{
    	while(a[i]<=a[q[top]])top--;
		minl[i]=q[top];
		q[++top]=i;
    }
    a[n+1]=-2147483647;
	top=1;
	q[1]=n+1;
	for(long long i=n; i>=1; i--)
	{
		while(a[i]<a[q[top]])top--;
		minr[i]=q[top];
		q[++top]=i;
	}
	for(long long i=1; i<=n; i++)
	{
		ans-=a[i]*(i-minl[i])*(minr[i]-i);
	}
	printf("%d", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long n,s,tail,a[1000005],f[1000005],minl[1000005],minr[1000005],maxl[1000005],maxr[1000005];
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	 if(a[i]>a[f[tail]])
	 {
	 	minl[i]=i-1;
	 	f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]<a[f[tail]]&&tail>=1)tail--;
		minl[i]=f[tail];
		f[++tail]=i;
	 }
	f[1]=n+1;
	tail=1;
	for(int i=n;i>=1;i--)
	 if(a[i]>a[f[tail]])
	 {
		minr[i]=i+1;
		f[++tail]=i;
	 }
	 else
	 {
		while(a[i]<=a[f[tail]]&&tail>=1)tail--;
		minr[i]=f[tail];
		f[++tail]=i;
	 }
	a[0]=a[n+1]=2147483647;
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	 if(a[i]<a[f[i]])
	 {
	 	maxl[i]=i-1;
	 	f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]>a[f[tail]]&&tail>=1)tail--;
		maxl[i]=f[tail];
		f[++tail]=i;
	 }
	f[1]=n+1;
	tail=1;
	for(int i=n;i>=1;i--)
	 if(a[i]<a[f[i]])
	 {
	 	maxr[i]=i+1;
		f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]>=a[f[tail]]&&tail>=1)tail--;
		maxr[i]=f[tail];
		f[++tail]=i;
	 }
	for(int i=1;i<=n;i++)
	 s+=a[i]*1ll*(1ll*(i-maxl[i])*(maxr[i]-i)-1ll*(i-minl[i])*(minr[i]-i));
	cout<<s;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const long long INT=1e6+5;
int n,top,a[1000010],bsy[1000010],maxn[1000010],maxx[1000010],minn[1000010],minx[1000010];
long long num;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	a[0]=INT;
	top=1;
	bsy[1]=0;
	for(int i=1;i<=n;i++)
	{
		while(a[i]>=a[bsy[top]])top--;
		maxn[i]=bsy[top];
		bsy[++top]=i;
	}
	top=1;
	a[n+1]=INT;
	bsy[1]=n+1;
	for(int i=n;i>0;i--)
	{
		while(a[i]>a[bsy[top]])top--;
		maxx[i]=bsy[top];
		bsy[++top]=i;
	}
	for(int i=1;i<=n;i++)
		num+=a[i]*(i-maxn[i])*(maxx[i]-i);
	a[0]=-INT;
	top=1;
	bsy[1]=0;
	for(int i=1;i<=n;i++)
	{
		while(a[i]<=a[bsy[top]])top--;
		minn[i]=bsy[top];
		bsy[++top]=i;
	}
	a[n+1]=-INT;
	top=1;
	bsy[1]=n+1;
	for(int i=n;i>0;i--)
	{
		while(a[i]<a[bsy[top]])top--;
		minx[i]=bsy[top];
		bsy[++top]=i;
	}
	for(int i=1;i<=n;i++)
		num-=a[i]*(i-minn[i])*(minx[i]-i);
	printf("%lld",num);
}


#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 1000005
using namespace std;
inline int read(){
	char ch=getchar();int s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-48;ch=getchar();}
	return s*w;
}
int n;
ll ans;
int num[maxn];
int top,s[maxn];
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		num[i]=read();
	}
	num[0]=num[n+1]=inf;
	for(int i=0;i<=n+1;i++){
		if(!top||num[s[top]]>num[i]) s[++top]=i;
		else{
			while(top&&num[s[top]]<=num[i]){
				ans+=1ll*num[s[top]]*(i-s[top])*(s[top]-s[top-1]);
				top--;
			}
			s[++top]=i;
		}
	}
	top=0;
	num[0]=num[n+1]=-inf;
	for(int i=0;i<=n+1;i++){
		if(!top||num[s[top]]<num[i]) s[++top]=i;
		else{
			while(top&&num[s[top]]>=num[i]){
				ans-=1ll*num[s[top]]*(i-s[top])*(s[top]-s[top-1]);
				top--;
			}
			s[++top]=i;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const long long INT=1e6+5;
long long n,top,a[1000010],bsy[1000010],maxn[1000010],maxx[1000010],minn[1000010],minx[1000010];
long long num;
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	a[0]=INT;
	top=1;
	bsy[1]=0;
	for(int i=1;i<=n;i++)
	{
		while(a[i]>=a[bsy[top]])top--;
		maxn[i]=bsy[top];
		bsy[++top]=i;
	}
	top=1;
	a[n+1]=INT;
	bsy[1]=n+1;
	for(int i=n;i>0;i--)
	{
		while(a[i]>a[bsy[top]])top--;
		maxx[i]=bsy[top];
		bsy[++top]=i;
	}
	for(int i=1;i<=n;i++)
		num+=a[i]*(i-maxn[i])*(maxx[i]-i);
	a[0]=-INT;
	top=1;
	bsy[1]=0;
	for(int i=1;i<=n;i++)
	{
		while(a[i]<=a[bsy[top]])top--;
		minn[i]=bsy[top];
		bsy[++top]=i;
	}
	a[n+1]=-INT;
	top=1;
	bsy[1]=n+1;
	for(int i=n;i>0;i--)
	{
		while(a[i]<a[bsy[top]])top--;
		minx[i]=bsy[top];
		bsy[++top]=i;
	}
	for(int i=1;i<=n;i++)
		num-=a[i]*(i-minn[i])*(minx[i]-i);
	printf("%lld",num);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL a[1000005],l[1000005],r[1000005],n,now,print;
int main(){
	while(scanf("%lld",&n))
	{
		print=0;
		for(register LL i=1;i<=n;i++) scanf("%d",&a[i]);
		for(register LL i=1;i<=n;i++) l[i]=r[i]=i;
		for(register LL i=2;i<=n;i++)
		{
			now=i;
			while(now>1&&a[i]>=a[now-1]) now=l[now-1];
			l[i]=now;
		}for(register LL i=n-1;i>0;i--)
		{
			now=i;
			while(now<n&&a[i]>a[now+1]) now=r[now+1];
			r[i]=now;
		}
		for(register LL i=1;i<=n;i++) print+=a[i]*(i-l[i]+1)*(r[i]-i+1);
		for(register LL i=1;i<=n;i++) l[i]=r[i]=i;
		for(register LL i=2;i<=n;i++)
		{
			now=i;
			while(now>1&&a[i]<=a[now-1]) now=l[now-1];
			l[i]=now;
		}for(register LL i=n-1;i>0;i--)
		{
			now=i;
			while(now<n&&a[i]<a[now+1]) now=r[now+1];
			r[i]=now;
		}
		for(register LL i=1;i<=n;i++) print-=a[i]*(i-l[i]+1)*(r[i]-i+1);
		printf("%lld",print);
	}

	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
struct DT{
	int v,i;
}f[1000100];
struct demo{
	int l,r;
}ans_max[1000100],ans_min[1000100];
int n,top,ansmax,ansmin,l,a[1000100];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	    scanf("%d",&a[i]);
	top=1,a[n+1]=-0x7f7f7f7f;
	for(int i=1;i<=n+1;i++){
		l=i;
		while(top&&f[top-1].v>=a[i]){
			top--;
			ans_min[f[top].i].r=i-1;
		    l=ans_min[f[top].i].l;
	    }
	    if(i<=n)
		   f[top].v=a[i],f[top].i=i,ans_min[f[top].i].l=l,top++;
	}
	top=1,a[n+1]=0x7f7f7f7f,f[0].v=0x7f7f7f7f;
	for(int i=1;i<=n+1;i++){
		l=i;
		while(top&&f[top-1].v<=a[i]){
			top--;
			ans_max[f[top].i].r=i-1;
		    l=ans_max[f[top].i].l;
	    }
	    if(i<=n)
	       f[top].v=a[i],f[top].i=i,ans_max[f[top].i].l=l,top++;
	}
	for(int i=1;i<=n;i++)
	    ansmax+=a[i]*(i-ans_max[i].l+1)*(ans_max[i].r-i+1);
	for(int i=1;i<=n;i++)
	    ansmin+=a[i]*(i-ans_min[i].l+1)*(ans_min[i].r-i+1);
	printf("%d",ansmax-ansmin);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
struct FFF{
	long long v;
	int id;
}st[1000100];
struct node{
	int l,r;
}maxx[1000100],minn[1000100];
long long n,top,ans_,_ans,l,a[1000100];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	    scanf("%lld",&a[i]);
	top=1,a[n+1]=-inf;
	for(int i=1;i<=n+1;i++){
		l=i;
		while(top&&st[top-1].v>=a[i]){
			top--;
			minn[st[top].id].r=i-1;
		    l=minn[st[top].id].l;
	    }
	    if(i<=n)
		   st[top].v=a[i],st[top].id=i,minn[st[top].id].l=l,top++;
	}
	top=1,a[n+1]=inf,st[0].v=inf;
	for(int i=1;i<=n+1;i++){
		l=i;
		while(top&&st[top-1].v<=a[i]){
			top--;
			maxx[st[top].id].r=i-1;
		    l=maxx[st[top].id].l;
	    }
	    if(i<=n)
	       st[top].v=a[i],st[top].id=i,maxx[st[top].id].l=l,top++;
	}
	for(int i=1;i<=n;i++)
	    ans_+=a[i]*(i-maxx[i].l+1)*(maxx[i].r-i+1);
	for(int i=1;i<=n;i++)
	    _ans+=a[i]*(i-minn[i].l+1)*(minn[i].r-i+1);
	printf("%lld",ans_-_ans);
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
long long maxl[100010],maxr[100010];
long long minl[100010],minr[100010];
long long a[100010],z[100010];
long long n,tail,ans;
int main()
{
    cin>>n;
    for(long long i=1; i<=n; i++)
       scanf("%lld",&a[i]);
    a[0]=2147483647;
    tail=1;
    z[1]=0;
    for(long long i=1; i<=n; i++)
     {
     	while(a[z[tail]]<=a[i])
          tail--;
        maxl[i]=z[tail];
        z[++tail]=i;
     }
    a[n+1]=2147483647;
    tail=1;
    z[1]=n+1;
    for(long long i=n; i>=1; i--)
     {
     	while(a[z[tail]]<a[i])
          tail--;
        maxr[i]=z[tail];
        z[++tail]=i;
     }
	a[0]=-2147483647;
    tail=1;
    z[1]=0;
    for(long long i=1; i<=n; i++)
       ans+=a[i]*(i-maxl[i])*(maxr[i]-i);
    for(long long i=1; i<=n; i++)
     {
     	while(a[z[tail]]>=a[i])
          tail--;
        minl[i]=z[tail];
        z[++tail]=i;
     }
	a[n+1]=-2147483647;
    tail=1;
    z[1]=n+1;
    for(long long i=n; i>=1; i--)
     {
     	while(a[z[tail]]>a[i])
          tail--;
        minr[i]=z[tail];
        z[++tail]=i;
     }
    for(long long i=1; i<=n; i++)
       ans-=a[i]*(i-minl[i])*(minr[i]-i);
    printf("%lld",ans);
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6;
ll a[N],ans,sta[N],h,mil[N],mir[N],mxl[N],mxr[N];
int main()
{
	ll n;
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
	sta[1]=0,h=1;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]>a[sta[h]])sta[++h]=i,mil[i]=i-1;
		else
		{
			while(a[i]<=a[sta[h]])h--;
			mil[i]=sta[h];
			sta[++h]=i;
		}
	}
	sta[1]=n+1;h=1;
	for(ll i=n;i>=1;i--)
	{
		if(a[i]>a[sta[h]])sta[++h]=i,mir[i]=i+1;
		else
		{
			while(a[i]<a[sta[h]])h--;
			mir[i]=sta[h];
			sta[++h]=i;
		}
	}
	a[0]=a[n+1]=MAX;
	sta[1]=0;h=1;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]<a[sta[h]])sta[++h]=i,mxl[i]=i-1;
		else
		{
			while(a[i]>=a[sta[h]])h--;
			mxl[i]=sta[h];
			sta[++h]=i;
		}
	}
	sta[1]=n+1,h=1;
	for(ll i=n;i>=1;i--)
	{
		if(a[i]<a[sta[h]]) sta[++h]=i,mxr[i]=i+1;
		else
		{
			while(a[i]>a[sta[h]]) h--;
			mxr[i]=sta[h];
			sta[++h]=i;
		}
	}
	//for(int i=1;i<=n;i++)
		//cout<<mil[i]<<' '<<mir[i]<<' '<<mxl[i]<<' '<<mxr[i]<<endl;
	for(ll i=1;i<=n;i++) ans+=a[i]*((i-mxl[i])*(mxr[i]-i)-(i-mil[i])*(mir[i]-i));
	printf("%lld",ans);
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define re register
using namespace std;
typedef long long ll;
il int read(){
    int x=0,t=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')t=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*t;
}
const int maxn = 1e6 + 10;
int n;
int a[maxn];
int l[maxn], r[maxn];
ll ans, mi, mx;
int main() {
	n = read();
	for(re int i = 1; i <= n; ++i) {
		a[i] = read();
		l[i] = r[i] = i;
	}
//-------------------------------------------->
//求a[i]作为最大、最小值的范围
	ans = mx = mi = 0;
	a[0] = a[n+1] = maxn;
	for(re int i = 1; i <= n; ++i) {
		int pos = i;
		while(a[--pos] <= a[i]) l[i] = pos;
		pos = i;
		while(a[++pos] < a[i]) r[i] = pos;
		mx += 1ll*a[i]*(i-l[i]+1)*(r[i]-i+1);
	}
	a[0] = a[n+1] = 0;
	for(re int i = 1; i <= n; ++i) l[i] = r[i] = i;
	for(re int i = 1; i <= n; ++i) {
		int pos = i;
		while(a[--pos] >= a[i]) l[i] = pos;
		pos = i;
		while(a[++pos] > a[i]) r[i] = pos;
		mi += 1ll*a[i]*(i-l[i]+1)*(r[i]-i+1);
	}
	//cout << mx << " " << mi << endl;
	ans = mx - mi;
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long

const int kMaxN = 1000000 + 5;
const int INF = 1000000000 + 5;

int n , ans , a[kMaxN] , minl[kMaxN] , minr[kMaxN] , maxl[kMaxN] , maxr[kMaxN];

inline int read(){
	int s = 0 , w = 1;
	char ch = getchar();
	while((ch < '0') || (ch > '9')){
		if(ch == '-'){
			w = -1;
		}
		ch = getchar();
    }
   	while((ch >= '0') && (ch <= '9')){
   		s = (s << 3) + (s << 1) + ch - '0';
		ch = getchar();
	}
   	return s * w;
}

inline void write(int x){
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9){
    	write(x / 10);
	}
    putchar(x % 10 + '0');
    return ;
}

signed main(){

	n = read();

	for(int i = 1 ; i <= n ; i++ ){
		a[i] = read();
	}

	stack <int> sta;

	a[0] = INF;

	sta.push(0);

	for(int i = 1 ; i <= n ; i++ ){
		while(a[sta.top()] <= a[i]){
			sta.pop();
		}
		maxl[i] = sta.top();
		sta.push(i);
	}

	while(sta.empty() == false){
		sta.pop();
	}

	a[n + 1] = INF;

	sta.push(n + 1);

	for(int i = n ; i >= 1 ; i-- ){
		while(a[sta.top()] < a[i]){
			sta.pop();
		}
		maxr[i] = sta.top();
		sta.push(i);
	}

	while(sta.empty() == false){
		sta.pop();
	}

	a[0] = -INF;

	sta.push(0);

	for(int i = 1 ; i <= n ; i++ ){
		while(a[sta.top()] >= a[i]){
			sta.pop();
		}
		minl[i] = sta.top();
		sta.push(i);
	}

	while(sta.empty() == false){
		sta.pop();
	}

	a[n + 1] = -INF;

	sta.push(n + 1);

	for(int i = n ; i >= 1 ; i-- ){
		while(a[sta.top()] > a[i]){
			sta.pop();
		}
		minr[i] = sta.top();
		sta.push(i);
	}

	while(sta.empty() == false){
		sta.pop();
	}

	for(int i = 1 ; i <= n ; i++ ){
		ans += a[i] * (i - maxl[i]) * (maxr[i] - i);
		ans -= a[i] * (i - minl[i]) * (minr[i] - i);
	}

	cout << ans << endl;

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6+10;
ll a[N],ans,sta[N],h,mil[N],mir[N],mxl[N],mxr[N],n;
int main()
{
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
	sta[1]=0,h=1;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]>a[sta[h]])sta[++h]=i,mil[i]=i-1;
		else
		{
			while(a[i]<a[sta[h]])h--;
			mil[i]=sta[h];
			sta[++h]=i;
		}
	}
	sta[1]=n+1;h=1;
	for(ll i=n;i>=1;i--)
	{
		if(a[i]>a[sta[h]])sta[++h]=i,mir[i]=i+1;
		else
		{
			while(a[i]<=a[sta[h]])h--;
			mir[i]=sta[h];
			sta[++h]=i;
		}
	}
	a[0]=a[n+1]=MAX;
	sta[1]=0;h=1;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]<a[sta[h]])sta[++h]=i,mxl[i]=i-1;
		else
		{
			while(a[i]>a[sta[h]])h--;
			mxl[i]=sta[h];
			sta[++h]=i;
		}
	}
	sta[1]=n+1,h=1;
	for(ll i=n;i>=1;i--)
	{
		if(a[i]<a[sta[h]]) sta[++h]=i,mxr[i]=i+1;
		else
		{
			while(a[i]>=a[sta[h]]) h--;
			mxr[i]=sta[h];
			sta[++h]=i;
		}
	}
	//for(int i=1;i<=n;i++)
		//cout<<mil[i]<<' '<<mir[i]<<' '<<mxl[i]<<' '<<mxr[i]<<endl;
	for(ll i=1;i<=n;i++) ans+=a[i]*((i-mxl[i])*(mxr[i]-i)-(i-mil[i])*(mir[i]-i));
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(register int i=a;i<=b;++i)
#define Rep(i,a,b) for(register int i=a;i>=b;--i)
inline int read()
{
	int x=0;bool f=0;char ch;
	do{ch=getchar();f|=(ch=='-');}while(!isdigit(ch));
	do{x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}while(isdigit(ch));
	return f?-x:x;
}
inline void write(int x)
{
    if(x<0)x=-x,putchar('-');
    if(x>9)write(x/10);putchar(x%10+'0');
}
inline void writesp(int x)
{
	write(x);putchar(' ');
}
inline void writeln(int x)
{
	write(x);puts("");
}
deque<int> q;
const int maxn=1e6+5;
int a[maxn];
int l[maxn],r[maxn];
int u[maxn],v[maxn];
signed main()
{
	int n=read();
	rep(i,1,n)
	{
		a[i]=read();
	}
	a[0]=INT_MAX;a[n+1]=INT_MAX;
	q.clear();
	q.push_back(0);
	rep(i,1,n)
	{
		while(a[q.back()]<=a[i])q.pop_back();
		l[i]=q.back();
		q.push_back(i);
	}
	q.clear();
	q.push_back(n+1);
	Rep(i,n,1)
	{
		while(a[q.back()]<a[i])q.pop_back();
		r[i]=q.back();
		q.push_back(i);
	}
	a[0]=INT_MIN;a[n+1]=INT_MIN;
	q.clear();
	q.push_back(0);
	rep(i,1,n)
	{
		while(a[q.back()]>=a[i])q.pop_back();
		u[i]=q.back();
		q.push_back(i);
	}
	q.clear();
	q.push_back(n+1);
	Rep(i,n,1)
	{
		while(a[q.back()]>a[i])q.pop_back();
		v[i]=q.back();
		q.push_back(i);
	}
	int ans=0;
	rep(i,1,n)
	{
		ans+=a[i]*(i-l[i])*(r[i]-i)-a[i]*(i-u[i])*(v[i]-i);
	}
	writeln(ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

long long n,a[1000010],ans;
long long top,t[1000010];
long long maxl[1000010],maxr[1000010],minl[1000010],minr[1000010];

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
    a[0]=0x3f3f3f3f;
    top=1;
	for(int i=1;i<=n;i++)
	{
		while(a[t[top]]<=a[i]) top--;
		maxl[i]=t[top];
		t[++top]=i;
	}
	a[n+1]=0x3f3f3f3f;
	top=1;
	t[1]=n+1;
	for(int i=n;i>0;i--)
	{
		while(a[i]>a[t[top]]) top--;
		maxr[i]=t[top];
		t[++top]=i;
	}
	for(int i=1;i<=n;i++)
		ans+=a[i]*(i-maxl[i])*(maxr[i]-i);
	a[0]=-0x3f3f3f3f;
	top=1;
	t[1]=0;
	for(int i=1;i<=n;i++)
	{
		while(a[i]<=a[t[top]]) top--;
		minl[i]=t[top];
		t[++top]=i;
	}
	a[n+1]=-0x3f3f3f3f;
	top=1;
	t[1]=n+1;
	for(int i=n;i>0;i--)
	{
		while(a[i]<a[t[top]]) top--;
		minr[i]=t[top];
		t[++top]=i;
	}
	for(int i=1;i<=n;i++)
		ans-=a[i]*(i-minl[i])*(minr[i]-i);
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#define LL long long
using namespace std;

const int MAXN = 1e6;

int n;
long long ans;
int a[MAXN];
int l[MAXN], r[MAXN];

int main()
{
	while(~scanf("%d",&n))
	{
		ans = 0;
		for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
		 for(int i=1;i<=n;i++) l[i]=r[i]=i;
        for(int i=2;i<=n;i++)
        {
            int now=i;
            while(now>1&&a[i]>=a[now-1]) now=l[now-1];
            l[i]=now;
        }
        for(int i=n-1;i>=1;i--)
        {
            int now=i;
            while(now<n&&a[i]>a[now+1]) now=r[now+1];
            r[i]=now;
        }
        for(int i=1;i<=n;i++) ans+=(LL)a[i]*(LL)(i-l[i]+1)*(LL)(r[i]-i+1);
        for(int i=1;i<=n;i++) l[i]=r[i]=i;
        for(int i=2;i<=n;i++)
        {
            int now=i;
            while(now>1&&a[i]<=a[now-1]) now=l[now-1];
            l[i]=now;
        }
        for(int i=n-1;i>=1;i--)
        {
            int now=i;
            while(now<n&&a[i]<a[now+1]) now=r[now+1];
            r[i]=now;
        }
        for(int i=1;i<=n;i++) ans-=(LL)a[i]*(LL)(i-l[i]+1)*(LL)(r[i]-i+1);
		printf("%lld\n",ans);
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define N 200100
#define jd (isdigit(c))
#define gg c=getchar()
#define inf 233333333333333
#define eps 1e-10
#define mod 9901
#define ls k<<1
#define rs k<<1|1
#define root t[0].ch[1]
inline ll read()
{
    ll f=0;bool x=1;char gg;
    for(;!jd;gg)if(c=='-')x=0;
    for(;jd;gg)f=(f<<1)+(f<<3)+(c&15);
    return x?f:-f;
}
inline void write(ll x)
{
    if(!x)putchar('0');
    else
    {
        if(x<0)putchar('-'),x=-x;
        char s[25];
        int j=0;
        for(;x;x/=10)s[j++]=x%10;
        for(int i=j-1;~i;i--)putchar(s[i]+48);
    }
    putchar(' ');
}
ll n,a[N],s1[N],s2[N],l1[N],r1[N],l2[N],r2[N],tmp1,tmp2,ans1,ans2;
int main()
{
    n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=n;i++)
    {
    	while(tmp1&&a[s1[tmp1]]<a[i])tmp1--;
    	l1[i]=s1[tmp1];
    	while(tmp1&&a[s1[tmp1]]==a[i])tmp1--;
    	s1[++tmp1]=i;
    	while(tmp2&&a[s2[tmp2]]>a[i])tmp2--;
    	l2[i]=s2[tmp2];
    	while(tmp2&&a[s2[tmp2]]==a[i])tmp2--;
    	s2[++tmp2]=i;
	}
	tmp2=tmp1=0;
	s1[tmp1]=s2[tmp2]=n+1;
	for(int i=n;i;i--)
	{
		while(tmp1&&a[s1[tmp1]]<=a[i])tmp1--;
		r1[i]=s1[tmp1];
		s1[++tmp1]=i;
		while(tmp2&&a[s2[tmp2]>=a[i]])tmp2--;
		r2[i]=s2[tmp2];
		s2[++tmp2]=i;
	}
	for(int i=1;i<=n;i++)ans1+=a[i]*(i-l1[i])*(r1[i]-i),ans2+=a[i]*(i-l2[i])*(r2[i]-i);
	write(ans1-ans2);
//	for(int i=1;i<=n;i++)write(l1[i]),write(l2[i]),write(r1[i]),write(r2[i]),putchar('\n');
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long n,s,tail,a[1000005],f[1000005],minl[1000005],minr[1000005],maxl[1000005],maxr[1000005];
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	{
		while(a[i]<a[f[tail]]&&tail>=1)tail--;
		minl[i]=f[tail];
		f[++tail]=i;
	}
	f[1]=n+1;
	tail=1;
	for(int i=n;i>=1;i--)
	{
		while(a[i]<=a[f[tail]]&&tail>=1)tail--;
		minr[i]=f[tail];
		f[++tail]=i;
	}
	a[0]=a[n+1]=2147483647;
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	{
			while(a[i]>a[f[tail]]&&tail>=1)tail--;
			maxl[i]=f[tail];
			f[++tail]=i;
	}
	f[1]=n+1;
	tail=1;
	for(int i=n;i>=1;i--)
	{
			while(a[i]>=a[f[tail]]&&tail>=1)tail--;
			maxr[i]=f[tail];
			f[++tail]=i;
	}
	for(int i=1;i<=n;i++)
	 s+=a[i]*(i-maxl[i])*(maxr[i]-i)-(i-minl[i])*(minr[i]-i);
	printf("%lld",s);
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
long long q[1001000];
long long n, a[100007], ans;
long long maxl[1000007], maxr[1000007], minl[1000007], minr[1000007];
int main()
{
	scanf("%lld", &n);
	for(long long i=1; i<=n; i++) scanf("%lld", &a[i]);
	a[0]=2147483647;
    long long top=1;
    for(long long i=1; i<=n; i++)
	{
    	while(a[i]>=a[q[top]])top--;
		maxl[i]=q[top];
		q[++top]=i;
    }
    a[n+1]=2147483647;
	top=1;
	q[1]=n+1;
	for(long long i=n; i>=1; i--)
	{
		while(a[i]>a[q[top]])top--;
		maxr[i]=q[top];
		q[++top]=i;
	}
	for(long long i=1; i<=n; i++)
	{
		ans+=a[i]*(i-maxl[i])*(maxr[i]-i);
	}
	a[0]=-2147483647;
    top=1;
    q[1]=0;
    for(long long i=1; i<=n; i++)
	{
    	while(a[i]<=a[q[top]])top--;
		minl[i]=q[top];
		q[++top]=i;
    }
    a[n+1]=-2147483647;
	top=1;
	q[1]=n+1;
	for(long long i=n; i>=1; i--)
	{
		while(a[i]<a[q[top]])top--;
		minr[i]=q[top];
		q[++top]=i;
	}
	for(long long i=1; i<=n; i++)
	{
		ans-=a[i]*(i-minl[i])*(minr[i]-i);
	}
	printf("%lld", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long n,s,tail,a[1000005],f[1000005],minl[1000005],minr[1000005],maxl[1000005],maxr[1000005];
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	f[1]=0;//一
	tail=1;
	for(int i=1;i<=n;i++)
	 if(a[i]>a[f[tail]])
	 {
	 	minl[i]=i-1;
	 	f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]<a[f[tail]]&&tail>=1)tail--;
		minl[i]=f[tail];
		f[++tail]=i;
	 }
	f[1]=n+1;//二
	tail=1;
	for(int i=n;i>=1;i--)
	 if(a[i]>a[f[tail]])
	 {
		minr[i]=i+1;
		f[++tail]=i;
	 }
	 else
	 {
		while(a[i]<=a[f[tail]]&&tail>=1)tail--;
		minr[i]=f[tail];
		f[++tail]=i;
	 }
	a[0]=a[n+1]=2147483647;//三
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	 if(a[i]<a[f[i]])
	 {
	 	maxl[i]=i-1;
	 	f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]>a[f[tail]]&&tail>=1)tail--;
		maxl[i]=f[tail];
		f[++tail]=i;
	 }
	f[1]=n+1;//四
	tail=1;
	for(int i=n;i>=1;i--)
	 if(a[i]<a[f[i]])
	 {
	 	maxr[i]=i+1;
		f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]>=a[f[tail]]&&tail>=1)tail--;
		maxr[i]=f[tail];
		f[++tail]=i;
	 }
	for(int i=1;i<=n;i++)
	 s+=a[i]*((i-maxl[i])*(maxr[i]-i)-(i-minl[i])*(minr[i]-i));
	printf("%lld",s);
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans,a[1000005],mxl[1000005],mxr[1000005],mil[1000005],mir[1000005];
stack<int>s;
signed main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
	s.push(0);
	for(int i=1;i<=n;i++){
		if(a[i]>a[s.top()])s.push(i),mil[i]=i-1;
		else{
			while(a[i]<a[s.top()])s.pop();
			mil[i]=s.top();
			s.push(i);
		}
	}
	s.push(n+1);
	for(int i=n;i>=1;i--){
		if(a[i]>a[s.top()])s.push(i),mir[i]=i+1;
		else{
			while(a[i]<=a[s.top()])s.pop();
			mir[i]=s.top();
			s.push(i);
		}
	}
	a[0]=a[n+1]=2147483647;
	s.push(0);
	for(int i=1;i<=n;i++){
		if(a[i]<a[s.top()])s.push(i),mxl[i]=i-1;
		else{
			while(a[i]>a[s.top()])s.pop();
			mxl[i]=s.top();
			s.push(i);
		}
	}
	s.push(n+1);
	for(int i=n;i>=1;i--){
		if(a[i]<a[s.top()])s.push(i),mxr[i]=i+1;
		else{
			while(a[i]>=a[s.top()])s.pop();
			mxr[i]=s.top();
			s.push(i);
		}
	}
	for(int i=1;i<=n;i++)
		ans+=a[i]*((i-mxl[i])*(mxr[i]-i)-(i-mil[i])*(mir[i]-i));
	printf("%lld",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long n,s,tail,a[1000005],f[1000005],minl[1000005],minr[1000005],maxl[1000005],maxr[1000005];
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	 if(a[i]>a[f[tail]])
	 {
	 	minl[i]=i-1;
	 	f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]<a[f[tail]]&&tail>=1)tail--;
		minl[i]=f[tail];
		f[++tail]=i;
	 }
	f[1]=n+1;
	tail=1;
	for(int i=n;i>=1;i--)
	 if(a[i]>a[f[tail]])
	 {
		minr[i]=i+1;
		f[++tail]=i;
	 }
	 else
	 {
		while(a[i]<=a[f[tail]]&&tail>=1)tail--;
		minr[i]=f[tail];
		f[++tail]=i;
	 }
	a[0]=a[n+1]=2147483647;
	f[1]=0;
	tail=1;
	for(int i=1;i<=n;i++)
	 if(a[i]<a[f[tail]])
	 {
	 	maxl[i]=i-1;
	 	f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]>a[f[tail]]&&tail>=1)tail--;
		maxl[i]=f[tail];
		f[++tail]=i;
	 }
	f[1]=n+1;
	tail=1;
	for(int i=n;i>=1;i--)
	 if(a[i]<a[f[tail]])
	 {
	 	maxr[i]=i+1;
		f[++tail]=i;
	 }
	 else
	 {
	 	while(a[i]>=a[f[tail]]&&tail>=1)tail--;
		maxr[i]=f[tail];
		f[++tail]=i;
	 }
	for(int i=1;i<=n;i++)
	 s+=a[i]*1ll*(1ll*(i-maxl[i])*(maxr[i]-i)-1ll*(i-minl[i])*(minr[i]-i));
	printf("%lld",s);
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	priority_queue<pair<long long, int>> pq;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		pq.push(make_pair(-c[i], i));
	}
	while (m--) {
		long long ans = 0;
		int t, d; cin >> t >> d;
		if (d <= a[t]) {
			a[t] -= d;
			cout << d * c[t] << '\n';
		}
		else {
			bool ok = false;
			ans += a[t] * c[t];
			d -= a[t];
			a[t] = 0;
			while (!pq.empty()) {
				while (!pq.empty() && a[pq.top().second] == 0) pq.pop();
				if (pq.empty()) break;
				auto now = pq.top();
				if (d <= a[now.second]) {
					a[now.second] -= d;
					ans += (-1) * d * now.first;
					ok = true;
					cout << ans << '\n';
					break;
				}
				else {
					ans += (-1) * a[now.second] * now.first;
					d -= a[now.second];
					a[now.second] = 0;
					pq.pop();
				}
			}
			if (!ok) cout << "0" << '\n';
		}
	}
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	priority_queue<pair<long long, int>> pq;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		pq.push(make_pair(c[i], i));
	}
	while (m--) {
		long long ans = 0;
		int t, d; cin >> t >> d;
		if (d <= a[t]) {
			a[t] -= d;
			cout << d * c[t] << '\n';
		}
		else {
			bool ok = false;
			ans += a[t] * c[t];
			d -= a[t];
			a[t] = 0;
			while (!pq.empty()) {
				while (!pq.empty() && a[pq.top().second] == 0) pq.pop();
				if (pq.empty()) break;
				auto now = pq.top();
				if (d <= a[now.second]) {
					a[now.second] -= d;
					ans += d * now.first;
					ok = true;
					cout << ans << '\n';
					break;
				}
				else {
					ans += a[now.second] * now.first;
					d -= a[now.second];
					a[now.second] = 0;
					pq.pop();
				}
			}
			if (!ok) cout << "0" << '\n';
		}
	}
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
bool check[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	vector<pair<int, int>> d;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		d.push_back(make_pair(c[i], i));
	}
	sort(d.begin(), d.end());

	while (m--) {
		int k, num; cin >> k >> num;
		long long ans = 0;
		if (a[k] >= num) {
			a[k] -= num;
			ans += c[k] * num;
		}
		else {
			num -= a[k];
			ans += c[k] * a[k];
			a[k] = 0;
			while (true) {
				if (d.empty()) {
					ans = 0;
					break;
				}
				if (a[d[0].second] == 0) {
					d.erase(d.begin());
					continue;
				}
				if (a[d[0].second] >= num) {
					a[d[0].second] -= num;
					ans += c[d[0].second] * num;
					num = 0;
					if (a[d[0].second] == 0) {
						check[d[0].second] = true;
						d.erase(d.begin());
					}
					if (num == 0) break;
				}
				else {
					num -= a[d[0].second];
					ans += a[d[0].second] * c[d[0].second];
					a[d[0].second] = 0;
					d.erase(d.begin());
					check[d[0].second];
				}
			}
		}
		cout << ans << '\n';
	}
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
bool check[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	vector<pair<int, int>> d;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		d.push_back(make_pair(c[i], i));
	}
	sort(d.begin(), d.end());

	while (m--) {
		int k, num; cin >> k >> num;
		long long ans = 0;
		if (a[k] >= num) {
			a[k] -= num;
			ans += c[k] * num;
		}
		else {
			num -= a[k];
			ans += c[k] * a[k];
			a[k] = 0;
			while (true) {
				if (d.empty()) {
					ans = 0;
					break;
				}
				if (a[d[0].second] == 0) {
					d.erase(d.begin());
					continue;
				}
				if (a[d[0].second] >= num) {
					a[d[0].second] -= num;
					ans += c[d[0].second] * num;
					num = 0;
					if (a[d[0].second] == 0) {
						check[d[0].second] = true;
						d.erase(d.begin());
					}
					if (num == 0) break;
				}
				else {
					num -= a[d[0].second];
					ans += a[d[0].second] * c[d[0].second];
					a[d[0].second] = 0;
					d.erase(d.begin());
					check[d[0].second];
				}
			}
		}
		for (int i = 1; i <= n; i++) cout << a[i] << " ";
		cout << '\n';
		cout << ans << '\n';
	}
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	priority_queue<pair<long long, int>> pq;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		pq.push(make_pair(-c[i], i));
	}
	while (m--) {
		long long ans = 0;
		int t, d; cin >> t >> d;
		if (d <= a[t]) {
			a[t] -= d;
			cout << 1LL * d * c[t] << '\n';
		}
		else {
			bool ok = false;
			ans += 1LL * a[t] * c[t];
			d -= a[t];
			a[t] = 0;
			while (!pq.empty()) {
				while (!pq.empty() && a[pq.top().second] == 0) pq.pop();
				if (pq.empty()) break;
				auto now = pq.top();
				if (d <= a[now.second]) {
					a[now.second] -= d;
					ans += 1LL * (-1) * d * now.first;
					ok = true;
					cout << ans << '\n';
					break;
				}
				else {
					ans += 1LL * (-1) * a[now.second] * now.first;
					d -= a[now.second];
					a[now.second] = 0;
					pq.pop();
				}
			}
			if (!ok) cout << "0" << '\n';
		}
	}
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
bool check[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	vector<pair<int, int>> d;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		d.push_back(make_pair(c[i], i));
	}
	sort(d.begin(), d.end());

	while (m--) {
		int k, num; cin >> k >> num;
		int ans = 0;
		if (a[k] >= num) {
			a[k] -= num;
			ans += c[k] * num;
		}
		else {
			num -= a[k];
			ans += c[k] * a[k];
			a[k] = 0;
			while (true) {
				if (d.empty()) {
					ans = 0;
					break;
				}
				if (a[d[0].second] == 0) {
					d.erase(d.begin());
					continue;
				}
				if (a[d[0].second] >= num) {
					a[d[0].second] -= num;
					ans += c[d[0].second] * num;
					num = 0;
					if (a[d[0].second] == 0) {
						check[d[0].second] = true;
						d.erase(d.begin());
					}
					if (num == 0) break;
				}
				else {
					num -= a[d[0].second];
					ans += a[d[0].second] * c[d[0].second];
					a[d[0].second] = 0;
					d.erase(d.begin());
					check[d[0].second];
				}
			}
		}
		cout << ans << '\n';
	}
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[100001];
int c[100001];
int main() {
	ios_base::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	priority_queue<pair<long long, int>> pq;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		pq.push(make_pair(-c[i], i));
	}
	while (m--) {
		long long ans = 0;
		int t, d; cin >> t >> d;
		if (d <= a[t]) {
			a[t] -= d;
			cout << d * c[t] << '\n';
		}
		else {
			bool ok = false;
			ans += 1LL * a[t] * c[t];
			d -= a[t];
			a[t] = 0;
			while (!pq.empty()) {
				while (!pq.empty() && a[pq.top().second] == 0) pq.pop();
				if (pq.empty()) break;
				auto now = pq.top();
				if (d <= a[now.second]) {
					a[now.second] -= d;
					ans += 1LL * (-1) * d * now.first;
					ok = true;
					cout << ans << '\n';
					break;
				}
				else {
					ans += 1LL * (-1) * a[now.second] * now.first;
					d -= a[now.second];
					a[now.second] = 0;
					pq.pop();
				}
			}
			if (!ok) cout << "0" << '\n';
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
long long ar[300005],xx[300005];
int idx[300005];
struct st{
    long long a,b,c;
}br[300005];

bool com(st x, st y)
{
    if(x.c==y.c)
    {
        return x.a<y.a;
    }
    else return x.c<y.c;
}

int main()
{
    long long i,j,k,n,ans=0,x,y,a,b,c;

    scanf("%lld %lld",&n,&k);

    for(i=0;i<n;i++)
       scanf("%lld",&ar[i]);

    for(i=0;i<n;i++)
       {
           scanf("%lld",&x);
           xx[i]=x;
           br[i].a=i;
           br[i].b=ar[i];
           br[i].c=x;
       }
    sort(br,br+n,com);
    for(i=0;i<n;i++)
    {
        x=br[i].a;
        idx[x]=i;
    }
    while(k)
    {
        ans=0;
        int st=0;
        scanf("%lld %lld",&x,&y);

        a=idx[x-1];

        b=br[a].b;
        if(y<b)
            b=y;

        ans+=(b*br[a].c);
        br[a].b-=b;
        y-=b;

       while(st<n && y)
        {
            if(y==0)
                break;

           b=br[st].b;
        if(y<b)
            b=y;

            ans+=(b*br[st].c);
            br[st].b-=b;
            y-=b;

            if(br[st].b==0)
                st++;
        }

        if(y!=0)
            ans=0;

        printf("%lld\n",ans);

        k--;
    }

}


#include<bits/stdc++.h>
using namespace std;
long long ar[300005],xx[300005];
int idx[300005];
struct st{
    long long a,b,c;
}br[300005];

bool com(st x, st y)
{
    if(x.c==y.c)
    {
        return x.a<y.a;
    }
    else return x.c<y.c;
}

int main()
{
    long long i,j,k,n,ans=0,x,y,a,b,c,total;

    scanf("%lld %lld",&n,&k);

    for(i=0;i<n;i++)
       scanf("%lld",&ar[i]);

    for(i=0;i<n;i++)
       {
           scanf("%lld",&x);
           total+=ar[i];
           xx[i]=x;
           br[i].a=i;
           br[i].b=ar[i];
           br[i].c=x;
       }
    sort(br,br+n,com);
    for(i=0;i<n;i++)
    {
        x=br[i].a;
        idx[x]=i;
    }
    while(k)
    {
        ans=0;
        int st=0;
        scanf("%lld %lld",&x,&y);

        if(total<y)
        {
            printf("0\n");
            continue;
        }

        a=idx[x-1];

        b=br[a].b;
        if(y<b)
            b=y;

        ans+=(b*br[a].c);
        br[a].b-=b;
        y-=b;
        total-=b;

       while(st<n && y)
        {
            if(y==0)
                break;

           b=br[st].b;
        if(y<b)
            b=y;

            ans+=(b*br[st].c);
            br[st].b-=b;
            y-=b;
            total-=b;

            if(br[st].b==0)
                st++;
        }

        if(y!=0)
            ans=0;

        printf("%lld\n",ans);

        k--;
    }

}


#include<bits/stdc++.h>
using namespace std;
long long ar[300005],xx[300005];
int idx[300005];
struct st{
    long long a,b,c;
}br[300005];

bool com(st x, st y)
{
    if(x.c==y.c)
    {
        return x.a<y.a;
    }
    else return x.c<y.c;
}

int main()
{
    long long i,j,k,n,ans=0,x,y,a,b,c;

    cin>>n>>k;

    for(i=0;i<n;i++)
       cin>>ar[i];

    for(i=0;i<n;i++)
       {
           cin>>x;
           xx[i]=x;
           br[i].a=i;
           br[i].b=ar[i];
           br[i].c=x;
       }
    sort(br,br+n,com);
    for(i=0;i<n;i++)
    {
        x=br[i].a;
        idx[x]=i;
    }
    while(k)
    {
        ans=0;
        int st=0;
        cin>>x>>y;

        a=idx[x-1];

        b=min(br[a].b,y);
        ans+=(b*br[a].c);
        br[a].b-=b;
        y-=b;

       while(st<n)
        {
            if(y==0)
                break;

            b=min(br[st].b,y);
            ans+=(b*br[st].c);
            br[st].b-=b;
            y-=b;

            if(br[st].b==0)
                st++;
        }

        if(y!=0)
            ans=0;

        cout<<ans<<endl;

        k--;
    }

}


#include<bits/stdc++.h>
using namespace std;
long long ar[300005],xx[300005];
int idx[300005];
struct st{
    long long a,b,c;
}br[300005];

bool com(st x, st y)
{
    if(x.c==y.c)
    {
        return x.a<y.a;
    }
    else return x.c<y.c;
}

int main()
{
    long long i,j,k,n,ans=0,x,y,a,b,c;

    cin>>n>>k;

    for(i=0;i<n;i++)
       cin>>ar[i];

    for(i=0;i<n;i++)
       {
           cin>>x;
           xx[i]=x;
           br[i].a=i;
           br[i].b=ar[i];
           br[i].c=x;
       }
    sort(br,br+n,com);
    for(i=0;i<n;i++)
    {
        x=br[i].a;
        idx[x]=i;
    }
    int st=0;
    while(k)
    {
        ans=0;
        cin>>x>>y;

        a=idx[x-1];

        b=min(br[a].b,y);
        ans+=(b*br[a].c);
        br[a].b-=b;
        y-=b;

       while(st<n)
        {
            if(y==0)
                break;

            b=min(br[st].b,y);
            ans+=(b*br[st].c);
            br[st].b-=b;
            y-=b;

            if(br[st].b==0)
                st++;
        }

        if(y!=0)
            ans=0;

        cout<<ans<<endl;

        k--;
    }

}


#include<bits/stdc++.h>
using namespace std;
long long ar[300005],xx[300005];
int idx[300005];
struct st{
    long long a,b,c;
}br[300005];

bool com(st x, st y)
{
    if(x.c==y.c)
    {
        return x.a<y.a;
    }
    else return x.c<y.c;
}

int main()
{
    long long i,j,k,n,ans=0,x,y,a,b,c;

    scanf("%lld %lld",&n,&k);

    for(i=0;i<n;i++)
       scanf("%lld",&ar[i]);

    for(i=0;i<n;i++)
       {
           scanf("%lld",&x);
           xx[i]=x;
           br[i].a=i;
           br[i].b=ar[i];
           br[i].c=x;
       }
    sort(br,br+n,com);
    for(i=0;i<n;i++)
    {
        x=br[i].a;
        idx[x]=i;
    }
    while(k)
    {
        ans=0;
        int st=0;
        scanf("%lld %lld",&x,&y);

        a=idx[x-1];

        b=min(br[a].b,y);
        ans+=(b*br[a].c);
        br[a].b-=b;
        y-=b;

       while(st<n)
        {
            if(y==0)
                break;

            b=min(br[st].b,y);
            ans+=(b*br[st].c);
            br[st].b-=b;
            y-=b;

            if(br[st].b==0)
                st++;
        }

        if(y!=0)
            ans=0;

        printf("%lld\n",ans);

        k--;
    }

}


#include<bits/stdc++.h>
using namespace std;
long long ar[300005],xx[300005];
int idx[300005];
struct st{
    long long a,b,c;
}br[300005];

bool com(st x, st y)
{
    if(x.c==y.c)
    {
        return x.a<y.a;
    }
    else return x.c<y.c;
}

int main()
{
    long long i,j,k,n,ans=0,x,y,a,b,c,total;

    scanf("%lld %lld",&n,&k);

    for(i=0;i<n;i++)
       scanf("%lld",&ar[i]);

    for(i=0;i<n;i++)
       {
           scanf("%lld",&x);
           total+=ar[i];
           xx[i]=x;
           br[i].a=i;
           br[i].b=ar[i];
           br[i].c=x;
       }
    sort(br,br+n,com);
    for(i=0;i<n;i++)
    {
        x=br[i].a;
        idx[x]=i;
    }
     int st=0;
    while(k)
    {
        ans=0;

        scanf("%lld %lld",&x,&y);

        if(total<y)
        {
            printf("0\n");
            continue;
        }

        a=idx[x-1];

        b=br[a].b;
        if(y<b)
            b=y;

        ans+=(b*br[a].c);
        br[a].b-=b;
        y-=b;
        total-=b;

       while(st<n && y)
        {
            if(y==0)
                break;

           b=br[st].b;
        if(y<b)
            b=y;

            ans+=(b*br[st].c);
            br[st].b-=b;
            y-=b;
            total-=b;

            if(br[st].b==0)
                st++;
        }

        if(y!=0)
            ans=0;

        printf("%lld\n",ans);

        k--;
    }

}