#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5+50;
const int M = 1e6+50;
const ll oo = 1e18;
const int mod = 1e9+7;

int a[N],b[N],l,r;
pair<int,int> seg[N];

void build(int n,int s,int e){
	if(s == e){
		seg[n] = {a[s],b[s]};
		return;
	}
	build(n*2,s,(s+e)/2);
	build(n*2+1,(s+e)/2+1,e);
	seg[n] = {max(seg[n*2].first,seg[n*2+1].first),min(seg[n*2].second,seg[n*2+1].second)};
}

pair<int,int> get(int n,int s,int e){
	if(s > r || e < l)return {-2e9,2e9};
	if(s >= l && e <= r)return seg[n];
	pair<int,int> a = get(n*2,s,(s+e)/2);
	pair<int,int> b = get(n*2+1,(s+e)/2+1,e);
	return {max(a.first,b.first),min(a.second,b.second)};
}

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)scanf("%d",b+i);
	build(1,1,n);
	ll ans = 0;
	l = 1;
	r = n;
	for(int i=1;i<=n;i++){
		l = r = i;
		int x=i-1,y=i;
		pair<int,int> cur = get(1,1,n);
		if(cur.first <= cur.second){
			for(int k=30;k>=0;k--){
				int m = (1<<k);
				if(x+m <= n){
					l = i;
					r = x+m;
					cur = get(1,1,n);
					if(cur.first < cur.second){
						x+=m;
					}
				}
				if(y+m <= n){
					l = i;
					r = y+m;
					cur = get(1,1,n);
					if(cur.first <= cur.second)y+=m;
				}
			}
			ans += y-x;
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5+50;
const int M = 1e6+50;
const ll oo = 1e18;
const int mod = 1e9+7;

int a[N],b[N];

int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
	#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)scanf("%d",b+i);
	ll ans = 0;
	for(int i=1;i<=n;i++){
		set<int> st[2];
		for(int j=i;j<=n;j++){
			st[0].insert(-a[j]);
			st[1].insert(b[j]);
			if(-*st[0].begin() > *st[1].begin() || -*st[0].begin() < *st[1].begin())break;
			if(-*st[0].begin() == *st[1].begin())ans++;
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <cmath>
using namespace std;

typedef long long ll;

ll a[200005],b[200005],f1[200005][25],f2[200005][25];

void ST(int n)
{
	for (int i = 1; i <= n; i++)
	{
		f1[i][0] = a[i];
		f2[i][0] = b[i];
	}
	for (int j = 1; (1<<j) <= n; j++)
	{
		for (int i = 1; i+(1<<j)-1 <= n; i++)
		{
			f1[i][j] = max(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
			f2[i][j] = min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
		}
	}
}

ll RMQ_MAX(int l,int r)
{
	int k=log2(r-l+1);
	return max(f1[l][k],f1[r-(1<<k)+1][k]);
}

ll RMQ_MIN(int l,int r)
{
	int k=log2(r-l+1);
	return min(f2[l][k],f2[r-(1<<k)+1][k]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> b[i];
	}
	ST(n);
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int l = i,r = n;
		int index = -1;
		while( l <= r )
		{
			int mid = ( l + r ) / 2;
			ll maxn = RMQ_MAX(l,mid);
			ll minx = RMQ_MIN(l,mid);
			if( maxn == minx )
			{
				index = mid;
				break;
			}else if( maxn > minx ) r = mid - 1;
			else l = mid + 1;
		}
		if( index != -1 )
		{
			for (int j = index; j >= i; j--)
			{
				ll maxn = RMQ_MAX(i,j);
				ll minx = RMQ_MIN(i,j);
				if( maxn == minx ) ans ++;
				else break;
			}
			for (int j = index + 1; j <= n; j++)
			{
				ll maxn = RMQ_MAX(i,j);
				ll minx = RMQ_MIN(i,j);
				if( maxn == minx ) ans ++;
				else break;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

typedef long long ll;

int a[200005],b[200005],f1[200005][25],f2[200005][25];

void ST(int n)
{
	for (int i = 1; i <= n; i++)
	{
		f1[i][0] = a[i];
		f2[i][0] = b[i];
	}
	for (int j = 1; (1<<j) <= n; j++)
	{
		for (int i = 1; i+(1<<j)-1 <= n; i++)
		{
			f1[i][j] = max(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
			f2[i][j] = min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
		}
	}
}

int RMQ_MAX(int l,int r)
{
	int k=log2(r-l+1);
	return max(f1[l][k],f1[r-(1<<k)+1][k]);
}

int RMQ_MIN(int l,int r)
{
	int k=log2(r-l+1);
	return min(f2[l][k],f2[r-(1<<k)+1][k]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> b[i];
	}
	ST(n);
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int l = i,r = n;
		int index = -1;
		while( l <= r )
		{
			int mid = ( l + r ) / 2;
			int maxn = RMQ_MAX(l,mid);
			int minx = RMQ_MIN(l,mid);
			if( maxn == minx )
			{
				index = mid;
				break;
			}else if( maxn > minx ) r = mid - 1;
			else l = mid + 1;
		}
		if( index != -1 )
		{
			for (int j = index; j >= i; j--)
			{
				int maxn = RMQ_MAX(i,j);
				int minx = RMQ_MIN(i,j);
				if( maxn == minx ) ans ++;
				else break;
			}
			for (int j = index + 1; j <= n; j++)
			{
				int maxn = RMQ_MAX(i,j);
				int minx = RMQ_MIN(i,j);
				if( maxn == minx ) ans ++;
				else break;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

typedef long long ll;

ll a[200005],b[200005],f1[200005][25],f2[200005][25];

void ST(int n)
{
	for (int i = 1; i <= n; i++)
	{
		f1[i][0] = a[i];
		f2[i][0] = b[i];
	}
	for (int j = 1; (1<<j) <= n; j++)
	{
		for (int i = 1; i+(1<<j)-1 <= n; i++)
		{
			f1[i][j] = max(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
			f2[i][j] = min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
		}
	}
}

ll RMQ_MAX(int l,int r)
{
	int k=log2(r-l+1);
	return max(f1[l][k],f1[r-(1<<k)+1][k]);
}

ll RMQ_MIN(int l,int r)
{
	int k=log2(r-l+1);
	return min(f2[l][k],f2[r-(1<<k)+1][k]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> b[i];
	}
	ST(n);
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int l = i,r = n;
		ll ansl = -1,ansr = -1;
		while( l <= r )
		{
			int mid = ( l + r ) / 2;
			ll maxn = RMQ_MAX(i,mid);
			ll minx = RMQ_MIN(i,mid);
			if( maxn == minx )
			{
				ansl = mid;
				r = mid - 1;
			}else if( maxn > minx ) r = mid - 1;
			else l = mid + 1;
		}
		l = i,r = n;
		while( l <= r )
		{
			int mid = ( l + r ) / 2;
			ll maxn = RMQ_MAX(i,mid);
			ll minx = RMQ_MIN(i,mid);
			if( maxn == minx )
			{
				ansr = mid;
				l = mid + 1;
			}else if( maxn > minx ) r = mid - 1;
			else l = mid + 1;
		}
		if( ansl != -1 ) ans += ansr - ansl + 1;
	}
	cout << ans << '\n';
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

typedef long long ll;

ll a[200005],b[200005],f1[200005][25],f2[200005][25];

void ST(int n)
{
	for (int i = 1; i <= n; i++)
	{
		f1[i][0] = a[i];
		f2[i][0] = b[i];
	}
	for (int j = 1; (1<<j) <= n; j++)
	{
		for (int i = 1; i+(1<<j)-1 <= n; i++)
		{
			f1[i][j] = max(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
			f2[i][j] = min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
		}
	}
}

ll RMQ_MAX(int l,int r)
{
	int k=log2(r-l+1);
	return max(f1[l][k],f1[r-(1<<k)+1][k]);
}

ll RMQ_MIN(int l,int r)
{
	int k=log2(r-l+1);
	return min(f2[l][k],f2[r-(1<<k)+1][k]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> b[i];
	}
	ST(n);
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int l = i,r = n;
		int index = -1;
		while( l <= r )
		{
			int mid = ( l + r ) / 2;
			ll maxn = RMQ_MAX(i,mid);
			ll minx = RMQ_MIN(i,mid);
			if( maxn == minx )
			{
				index = mid;
				break;
			}else if( maxn > minx ) r = mid - 1;
			else l = mid + 1;
		}
		if( index != -1 )
		{
			for (int j = index; j >= i; j--)
			{
				ll maxn = RMQ_MAX(i,j);
				ll minx = RMQ_MIN(i,j);
				if( maxn == minx ) ans ++;
				else break;
			}
			for (int j = index + 1; j <= n; j++)
			{
				ll maxn = RMQ_MAX(i,j);
				ll minx = RMQ_MIN(i,j);
				if( maxn == minx ) ans ++;
				else break;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}


#include <bits/stdc++.h>

//#define log Incognit_sasat

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;

const ll N = 2e5 + 10;
const ll mod = 1e8;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll Log[N], a[N], b[N], sp_mx[20][N], sp_mn[20][N], n, i, ans;

void build_max(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mx[0][i] = a[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mx[i][j] = max(sp_mx[i - 1][j], sp_mx[i - 1][j + (1LL<<(i - 1))]);
}

ll get_max(ll l, ll r){
    ll L = Log[r - l + 1];
    return max(sp_mx[L][l], sp_mx[L][r - (1LL<<L) + 1]);
}

void build_min(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mn[0][i] = b[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mn[i][j] = min(sp_mn[i - 1][j], sp_mn[i - 1][j + (1LL<<(i - 1))]);
}

ll get_min(ll l, ll r){
    ll L = Log[r - l + 1];
    return min(sp_mn[L][l], sp_mn[L][r - (1LL<<L) + 1]);
}

ll get_delt(ll l, ll r){
    return get_max(l, r) - get_min(l, r);
}

ll get_l(ll i){
    ll l = i, r = n, c, timer = 0;
    while(l < r && timer < 100){
        timer ++;
        c = (l + r) / 2;

        //cout << "l = " << l << " r = " << r << " c = " << c << " dlt = " << get_delt(i, c) << endl;
        if(get_delt(i, c) >= 0)r = c;
        else l = c + 1;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

ll get_r(ll i, ll l){
    ll r = n, c, timer = 0;
    while(l < r && timer < 100){
        timer ++;
        c = (l + r) / 2;
        if(get_delt(i, c) == 0)l = c;
        else r = c;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

int main () {
    cin >> n;
    for(i = 2; i <= n; ++ i)Log[i] = Log[i / 2] + 1;
    for(i = 1; i <= n; ++ i)cin >> a[i];
    for(i = 1; i <= n; ++ i)cin >> b[i];
    build_max();
    build_min();

    for(i = 1; i <= n; ++ i){
        ll l = get_l(i), r;
        if(l >= 0){
            r = get_r(i, l);
            ans += r - l + 1;
        }
        //cout << "l = " << l << " r = " << r << " i = " << i << endl;
    }
    cout << ans << endl;
}
/*

110111001
123456789

124569
3 5
456

*/


#include <bits/stdc++.h>

//#define log Incognit_sasat

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;

const ll N = 2e5 + 10;
const ll mod = 1e8;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll Log[N], a[N], b[N], sp_mx[20][N], sp_mn[20][N], n, i, ans;

void build_max(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mx[0][i] = a[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mx[i][j] = max(sp_mx[i - 1][j], sp_mx[i - 1][j + (1LL<<(i - 1))]);
}

ll get_max(ll l, ll r){
    ll L = Log[r - l + 1];
    return max(sp_mx[L][l], sp_mx[L][r - (1LL<<L) + 1]);
}

void build_min(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mn[0][i] = b[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mn[i][j] = min(sp_mn[i - 1][j], sp_mn[i - 1][j + (1LL<<(i - 1))]);
}

ll get_min(ll l, ll r){
    ll L = Log[r - l + 1];
    return min(sp_mn[L][l], sp_mn[L][r - (1LL<<L) + 1]);
}

ll get_delt(ll l, ll r){
    return get_max(l, r) - get_min(l, r);
}

ll get_l(ll i){
    ll l = i, r = n, c, timer = 0;
    while(l < r && timer < 20){
        timer ++;
        c = (l + r) / 2;

        //cout << "l = " << l << " r = " << r << " c = " << c << " dlt = " << get_delt(i, c) << endl;
        if(get_delt(i, c) >= 0)r = c;
        else l = c + 1;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

ll get_r(ll i, ll l){
    ll r = n, c, timer = 0;
    while(l < r && timer < 10){
        timer ++;
        c = (l + r) / 2;
        if(get_delt(i, c) == 0)l = c;
        else r = c;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

int main () {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(i = 2; i <= n; ++ i)Log[i] = Log[i / 2] + 1;
    for(i = 1; i <= n; ++ i)cin >> a[i];
    for(i = 1; i <= n; ++ i)cin >> b[i];
    build_max();
    build_min();

    for(i = 1; i <= n; ++ i){
        ll l = get_l(i), r;
        if(l >= 0){
            r = get_r(i, l);
            ans += r - l + 1;
        }
        //cout << "l = " << l << " r = " << r << " i = " << i << endl;
    }
    cout << ans << endl;
}
/*

110111001
123456789

124569
3 5
456

*/


#include <bits/stdc++.h>

//#define log Incognit_sasat

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;

const ll N = 2e5 + 10;
const ll mod = 1e8;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll Log[N], a[N], b[N], sp_mx[20][N], sp_mn[20][N], n, i, ans;

void build_max(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mx[0][i] = a[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mx[i][j] = max(sp_mx[i - 1][j], sp_mx[i - 1][j + (1LL<<(i - 1))]);
}

ll get_max(ll l, ll r){
    ll L = Log[r - l + 1];
    return max(sp_mx[L][l], sp_mx[L][r - (1LL<<L) + 1]);
}

void build_min(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mn[0][i] = b[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mn[i][j] = min(sp_mn[i - 1][j], sp_mn[i - 1][j + (1LL<<(i - 1))]);
}

ll get_min(ll l, ll r){
    ll L = Log[r - l + 1];
    return min(sp_mn[L][l], sp_mn[L][r - (1LL<<L) + 1]);
}

ll get_delt(ll l, ll r){
    return get_max(l, r) - get_min(l, r);
}

ll get_l(ll i){
    ll l = i, r = n, c, timer = 0;
    while(l < r && timer < 100){
        timer ++;
        c = (l + r) / 2;

        //cout << "l = " << l << " r = " << r << " c = " << c << " dlt = " << get_delt(i, c) << endl;
        if(get_delt(i, c) >= 0)r = c;
        else l = c + 1;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

ll get_r(ll i, ll l){
    ll r = n, c, timer = 0;
    while(l < r && timer < 100){
        timer ++;
        c = (l + r) / 2;
        if(get_delt(i, c) >= 0)l = c;
        else r = c - 1;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

int main () {
    cin >> n;
    for(i = 2; i <= n; ++ i)Log[i] = Log[i / 2] + 1;
    for(i = 1; i <= n; ++ i)cin >> a[i];
    for(i = 1; i <= n; ++ i)cin >> b[i];
    build_max();
    build_min();
    cout << get_max(1, 3) << endl;
    cout << get_min(1, 3) << endl;

    for(i = 1; i <= n; ++ i){
        ll l = get_l(i), r;
        if(l >= 0){
            r = get_r(i, l);
            ans += r - l + 1;
        }
        //cout << "l = " << l << " r = " << r << " i = " << i << endl;
    }
    cout << ans << endl;
}
/*

110111001
123456789

124569
3 5
456

*/


#include <bits/stdc++.h>

//#define log Incognit_sasat

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;

const ll N = 2e5 + 10;
const ll mod = 1e8;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll Log[N], a[N], b[N], sp_mx[20][N], sp_mn[20][N], n, i, ans;

void build_max(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mx[0][i] = a[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mx[i][j] = max(sp_mx[i - 1][j], sp_mx[i - 1][j + (1LL<<(i - 1))]);
}

ll get_max(ll l, ll r){
    ll L = Log[r - l + 1];
    return max(sp_mx[L][l], sp_mx[L][r - (1LL<<L) + 1]);
}

void build_min(){
    ll i, j;
    for(i = 1; i <= n; ++ i)sp_mn[0][i] = b[i];
    for(i = 1; i <= Log[n]; ++ i)
        for(j = 1; j + (1LL<<i) - 1 <= n; ++ j)
            sp_mn[i][j] = min(sp_mn[i - 1][j], sp_mn[i - 1][j + (1LL<<(i - 1))]);
}

ll get_min(ll l, ll r){
    ll L = Log[r - l + 1];
    return min(sp_mn[L][l], sp_mn[L][r - (1LL<<L) + 1]);
}

ll get_delt(ll l, ll r){
    return get_max(l, r) - get_min(l, r);
}

ll get_l(ll i){
    ll l = i, r = n, c, timer = 0;
    while(l < r && timer < 100){
        timer ++;
        c = (l + r) / 2;

        //cout << "l = " << l << " r = " << r << " c = " << c << " dlt = " << get_delt(i, c) << endl;
        if(get_delt(i, c) >= 0)r = c;
        else l = c + 1;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

ll get_r(ll i, ll l){
    ll r = n, c, timer = 0;
    while(l < r && timer < 100){
        timer ++;
        c = (l + r) / 2;
        if(get_delt(i, c) >= 0)l = c;
        else r = c - 1;
    }
    ll ans = -1;
    for(ll j = l; j <= r; ++ j)
        if(get_delt(i, j) == 0)ans = j;
    return ans;
}

int main () {
    cin >> n;
    for(i = 2; i <= n; ++ i)Log[i] = Log[i / 2] + 1;
    for(i = 1; i <= n; ++ i)cin >> a[i];
    for(i = 1; i <= n; ++ i)cin >> b[i];
    build_max();
    build_min();

    for(i = 1; i <= n; ++ i){
        ll l = get_l(i), r;
        if(l >= 0){
            r = get_r(i, l);
            ans += r - l + 1;
        }
        //cout << "l = " << l << " r = " << r << " i = " << i << endl;
    }
    cout << ans << endl;
}
/*

110111001
123456789

124569
3 5
456

*/


#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-10;
int n,a[maxn],b[maxn],dpma[maxn][30],dpmi[maxn][30];
void rmq(){
    for(int i=1;i<=n;i++){
        dpmi[i][0]=b[i];
        dpma[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dpmi[i][j]=min(dpmi[i][j-1],dpmi[i+(1<<(j-1))][j-1]);
            dpma[i][j]=max(dpma[i][j-1],dpma[i+(1<<(j-1))][j-1]);
        }
    }
}
int querymax(int l,int r){
    int k=log2(r-l+1);
    return max(dpma[l][k],dpma[r-(1<<k)+1][k]);
}
int querymin(int l,int r){
    int k=log2(r-l+1);
    return min(dpmi[l][k],dpmi[r-(1<<k)+1][k]);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    rmq();
    ll ans=0;
    for(int i=1;i<=n;i++){//枚举左端点
        int l=i,r=n,ansl=i,ansr=i-1;
        while(l<=r){//枚举右端点的左端点
            int mid=(l+r)>>1;
            int ma=querymax(l,mid),mi=querymin(l,mid);
            if(ma>=mi){
                r=mid-1;
                if(ma==mi){
                    ansl=mid;
                }
            }else{
                l=mid+1;
            }
        }
        l=i,r=n;
        while(l<=r){//枚举右端点的右端点
            int mid=(l+r)>>1;
            int ma=querymax(l,mid),mi=querymin(l,mid);
            if(ma<=mi){
                l=mid+1;
                if(ma==mi){
                    ansr=mid;
                }
            }else{
                r=mid-1;
            }
        }
        ans+=ansr-ansl+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-10;
int n,a[maxn],b[maxn],dpma[maxn][30],dpmi[maxn][30];
void rmq(){
    for(int i=1;i<=n;i++){
        dpmi[i][0]=b[i];
        dpma[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dpmi[i][j]=min(dpmi[i][j-1],dpmi[i+(1<<(j-1))][j-1]);
            dpma[i][j]=max(dpma[i][j-1],dpma[i+(1<<(j-1))][j-1]);
        }
    }
}
int querymax(int l,int r){
    int k=log2(r-l+1);
    return max(dpma[l][k],dpma[r-(1<<k)+1][k]);
}
int querymin(int l,int r){
    int k=log2(r-l+1);
    return min(dpmi[l][k],dpmi[r-(1<<k)+1][k]);
}
bool check(int l,int r){
    return querymax(l,r)==querymin(l,r);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    rmq();
    ll ans=0;
    for(int i=1;i<=n;i++){//枚举左端点
        int l=i,r=n,ansl=i,ansr=i-1;
        while(l<=r){//枚举右端点的左端点
            int mid=(l+r)>>1;
            if(check(i,mid)){
                ansl=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        l=i,r=n;
        while(l<=r){//枚举右端点的右端点
            int mid=(l+r)>>1;
            if(check(i,mid)){
                ansr=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        ans+=ansr-ansl+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-10;
int n,a[maxn],b[maxn],dpma[maxn][30],dpmi[maxn][30];
void rmq(){
    for(int i=1;i<=n;i++){
        dpmi[i][0]=b[i];
        dpma[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dpmi[i][j]=min(dpmi[i][j-1],dpmi[i+(1<<(j-1))][j-1]);
            dpma[i][j]=max(dpma[i][j-1],dpma[i+(1<<(j-1))][j-1]);
        }
    }
}
int querymax(int l,int r){
    int k=log2(r-l+1);
    return max(dpma[l][k],dpma[r-(1<<k)+1][k]);
}
int querymin(int l,int r){
    int k=log2(r-l+1);
    return min(dpmi[l][k],dpmi[r-(1<<k)+1][k]);
}
bool check1(int l,int r){
    if(l>r) return false;
    return querymax(l,r)>=querymin(l,r);
}
bool check2(int l,int r){
    if(l>r) return false;
    return querymax(l,r)<=querymin(l,r);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    rmq();
    ll ans=0;
    for(int i=1;i<=n;i++){//枚举左端点
        int l=i,r=n,ansl=i,ansr=i-1;
        while(l<=r){//枚举右端点的左端点
            int mid=(l+r)>>1;
            if(check1(i,mid)){
                ansl=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        l=i,r=n;
        while(l<=r){//枚举右端点的右端点
            int mid=(l+r)>>1;
            if(check2(i,mid)){
                ansr=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        ans+=ansr-ansl+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-10;
int n,a[maxn],b[maxn],dpma[maxn][30],dpmi[maxn][30];
void rmq(){
    for(int i=1;i<=n;i++){
        dpmi[i][0]=b[i];
        dpma[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dpmi[i][j]=min(dpmi[i][j-1],dpmi[i+(1<<(j-1))][j-1]);
            dpma[i][j]=max(dpma[i][j-1],dpma[i+(1<<(j-1))][j-1]);
        }
    }
}
int querymax(int l,int r){
    int k=log2(r-l+1);
    return max(dpma[l][k],dpma[r-(1<<k)+1][k]);
}
int querymin(int l,int r){
    int k=log2(r-l+1);
    return min(dpmi[l][k],dpmi[r-(1<<k)+1][k]);
}
bool check(int l,int r){
    if(l>r) return false;
    return querymax(l,r)==querymin(l,r);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    rmq();
    ll ans=0;
    for(int i=1;i<=n;i++){//枚举左端点
        int l=i,r=n,ansl=i,ansr=i-1;
        while(l<=r){//枚举右端点的左端点
            int mid=(l+r)>>1;
            if(check(i,mid)){
                ansl=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        l=i,r=n;
        while(l<=r){//枚举右端点的右端点
            int mid=(l+r)>>1;
            if(check(i,mid)){
                ansr=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        ans+=ansr-ansl+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-10;
int n,a[maxn],b[maxn],dpma[maxn][30],dpmi[maxn][30];
void rmq(){
    for(int i=1;i<=n;i++){
        dpmi[i][0]=b[i];
        dpma[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dpmi[i][j]=min(dpmi[i][j-1],dpmi[i+(1<<(j-1))][j-1]);
            dpma[i][j]=max(dpma[i][j-1],dpma[i+(1<<(j-1))][j-1]);
        }
    }
}
int querymax(int l,int r){
    int k=log2(r-l+1);
    return max(dpma[l][k],dpma[r-(1<<k)+1][k]);
}
int querymin(int l,int r){
    int k=log2(r-l+1);
    return min(dpmi[l][k],dpmi[r-(1<<k)+1][k]);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    rmq();
    ll ans=0;
    for(int i=1;i<=n;i++){//枚举左端点
        int l=i,r=n,ansl=i,ansr=i-1;
        while(l<=r){//枚举右端点的左端点
            int mid=(l+r)>>1;
            int ma=querymax(i,mid),mi=querymin(i,mid);
            if(ma>=mi){
                r=mid-1;
                if(ma==mi){
                    ansl=mid;
                }
            }else{
                l=mid+1;
            }
        }
        l=i,r=n;
        while(l<=r){//枚举右端点的右端点
            int mid=(l+r)>>1;
            int ma=querymax(i,mid),mi=querymin(i,mid);
            if(ma<=mi){
                l=mid+1;
                if(ma==mi){
                    ansr=mid;
                }
            }else{
                r=mid-1;
            }
        }
        ans+=ansr-ansl+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-10;
int n,a[maxn],b[maxn],dpma[maxn][30],dpmi[maxn][30];
void rmq(){
    for(int i=1;i<=n;i++){
        dpmi[i][0]=b[i];
        dpma[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            dpmi[i][j]=min(dpmi[i][j-1],dpmi[i+(1<<(j-1))][j-1]);
            dpma[i][j]=max(dpma[i][j-1],dpma[i+(1<<(j-1))][j-1]);
        }
    }
}
int querymax(int l,int r){
    int k=log2(r-l+1);
    return max(dpma[l][k],dpma[r-(1<<k)+1][k]);
}
int querymin(int l,int r){
    int k=log2(r-l+1);
    return min(dpmi[l][k],dpmi[r-(1<<k)+1][k]);
}
signed main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    rmq();
    ll ans=0;
    for(int i=1;i<=n;i++){//枚举左端点
        int l=i,r=n,ansl=i,ansr=i-1;
        while(l<=r){//枚举右端点的左端点
            int mid=(l+r)>>1;
            int ma=querymax(l,mid),mi=querymin(l,mid);
            if(ma>=mi){
                r=mid-1;
//                if(ma==mi){
                    ansl=mid;
//                }
            }else{
                l=mid+1;
            }
        }
        l=i,r=n;
        while(l<=r){//枚举右端点的右端点
            int mid=(l+r)>>1;
            int ma=querymax(l,mid),mi=querymin(l,mid);
            if(ma<=mi){
                l=mid+1;
//                if(ma==mi){
                    ansr=mid;
//                }
            }else{
                r=mid-1;
            }
        }
        ans+=ansr-ansl+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e5;
const long inf=1e15;
int seg[4*N][2],n,a[N+2],b[N+2],lo,hi,md;
long ans;
void build(int node,int lo,int hi)
{
  if(lo==hi)
  {
    seg[node][0]=a[lo];
    seg[node][1]=b[lo];
    return ;
  }
  int md=(lo+hi)/2;
  build(node*2,lo,md);
  build(node*2+1,md+1,hi);
  seg[node][0]=max(seg[node*2][0],seg[node*2+1][0]);
  seg[node][1]=min(seg[node*2][1],seg[node*2+1][1]);
}
pair<int,int>query(int node,int lo,int hi,int l,int r)
{
  if(lo>r||hi<l)return {-inf,inf};
  if(lo>=l&&hi<=r)return {seg[node][0],seg[node][1]};
  int md=(lo+hi)/2;
  pair<int,int>p1=query(node*2,lo,md,l,r);
  pair<int,int>p2=query(node*2+1,md+1,hi,l,r);
  return {max(p1.first,p2.first),min(p1.second,p2.second)};
}
int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin>>n;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=n;i++)cin>>b[i];

  build(1,1,n);

  int a,b;
  for(int i=1;i<=n;i++)
  {
    pair<int,int>p;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<p.second)lo=md;
      else hi=md;
    }
    int id1=-1;
    for(int j=lo;j<=hi;j++)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id1=j;
        break;
      }
    }
    if(id1==-1)continue;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<=p.second)lo=md;
      else hi=md;
    }
    int id2;
    for(int j=hi;j>=lo;j--)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id2=j;
        break;
      }
    }
    int xx=id2-id1+1;
    ans+=(xx*1LL);
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e5;
const long inf=2e9;
int seg[4*N][2],n,a[N+2],b[N+2],lo,hi,md,id1,id2,xx;
long ans;
void build(int node,int lo,int hi)
{
  if(lo==hi)
  {
    seg[node][0]=a[lo];
    seg[node][1]=b[lo];
    return ;
  }
  int md=(lo+hi)/2;
  build(node*2,lo,md);
  build(node*2+1,md+1,hi);
  seg[node][0]=max(seg[node*2][0],seg[node*2+1][0]);
  seg[node][1]=min(seg[node*2][1],seg[node*2+1][1]);
}
pair<int,int>query(int node,int lo,int hi,int l,int r)
{
  if(lo>r||hi<l)return {-inf,inf};
  if(lo>=l&&hi<=r)return {seg[node][0],seg[node][1]};
  int md=(lo+hi)/2;
  pair<int,int>p1=query(node*2,lo,md,l,r);
  pair<int,int>p2=query(node*2+1,md+1,hi,l,r);
  return {max(p1.first,p2.first),min(p1.second,p2.second)};
}
int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin>>n;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=n;i++)cin>>b[i];

  build(1,1,n);

  int a,b;
  for(int i=1;i<=n;i++)
  {
    pair<int,int>p;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<p.second)lo=md;
      else hi=md;
    }
    id1=-1;
    for(int j=lo;j<=hi;j++)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id1=j;
        break;
      }
    }
    if(id1==-1)continue;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<=p.second)lo=md;
      else hi=md;
    }
    for(int j=hi;j>=lo;j--)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id2=j;
        break;
      }
    }
    xx=id2-id1+1;
    ///cout<<xx<<endl;
    ans+=(xx*1LL);
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const long N=2e5;
const long inf=1e15;
long seg[4*N][2],n,a[N+2],b[N+2],ans,lo,hi,md;
void build(long node,long lo,long hi)
{
  if(lo==hi)
  {
    seg[node][0]=a[lo];
    seg[node][1]=b[lo];
    return ;
  }
  long md=(lo+hi)/2;
  build(node*2,lo,md);
  build(node*2+1,md+1,hi);
  seg[node][0]=max(seg[node*2][0],seg[node*2+1][0]);
  seg[node][1]=min(seg[node*2][1],seg[node*2+1][1]);
}
pair<long,long>query(long node,long lo,long hi,long l,long r)
{
  if(lo>r||hi<l)return {-inf,inf};
  if(lo>=l&&hi<=r)return {seg[node][0],seg[node][1]};
  long md=(lo+hi)/2;
  pair<long,long>p1=query(node*2,lo,md,l,r);
  pair<long,long>p2=query(node*2+1,md+1,hi,l,r);
  return {max(p1.first,p2.first),min(p1.second,p2.second)};
}
int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin>>n;
  for(long i=1;i<=n;i++)cin>>a[i];
  for(long i=1;i<=n;i++)cin>>b[i];

  build(1,1,n);

  long a,b;
  for(long i=1;i<=n;i++)
  {
    pair<long,long>p;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<p.second)lo=md;
      else hi=md;
    }
    long id1=-1;
    for(long j=lo;j<=hi;j++)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id1=j;
        break;
      }
    }
    if(id1==-1)continue;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<=p.second)lo=md;
      else hi=md;
    }
    long id2;
    for(long j=hi;j>=lo;j--)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id2=j;
        break;
      }
    }
    long xx=id2-id1+1;
    ans+=xx;
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=2e5;
const int inf=INT_MAX;
int seg[4*N][2],n,a[N+2],b[N+2],ans,lo,hi,md;
void build(int node,int lo,int hi)
{
  if(lo==hi)
  {
    seg[node][0]=a[lo];
    seg[node][1]=b[lo];
    return ;
  }
  int md=(lo+hi)/2;
  build(node*2,lo,md);
  build(node*2+1,md+1,hi);
  seg[node][0]=max(seg[node*2][0],seg[node*2+1][0]);
  seg[node][1]=min(seg[node*2][1],seg[node*2+1][1]);
  ///cout<<node<<" "<<lo<<" "<<hi<<" "<<seg[node][0]<<" "<<seg[node][1]<<endl;
}
pair<int,int>query(int node,int lo,int hi,int l,int r)
{
  if(lo>r||hi<l)return {-inf,inf};
  if(lo>=l&&hi<=r)return {seg[node][0],seg[node][1]};
  int md=(lo+hi)/2;
  pair<int,int>p1=query(node*2,lo,md,l,r);
  pair<int,int>p2=query(node*2+1,md+1,hi,l,r);
  return {max(p1.first,p2.first),min(p1.second,p2.second)};
}
int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin>>n;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=n;i++)cin>>b[i];

  build(1,1,n);

  ///cout<<query(1,1,n,4,5).first<<" "<<query(1,1,n,4,5).first<<endl;

  long a,b;
  for(int i=1;i<=n;i++)
  {
    pair<int,int>p;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<p.second)lo=md;
      else hi=md;
    }
    long id1=-1;
    for(long j=lo;j<=hi;j++)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id1=i;
        break;
      }
    }
    ///cout<<id1<<endl;
    if(id1==-1)continue;
    lo=i;
    hi=n;
    while(hi-lo>2)
    {
      md=(lo+hi)/2;
      p=query(1,1,n,i,md);
      if(p.first<=p.second)lo=md;
      else hi=md;
    }
    long id2;
    for(long j=hi;j>=lo;j--)
    {
      p=query(1,1,n,i,j);
      if(p.first==p.second)
      {
        id2=i;
        break;
      }
    }
    ///if(i==4)cout<<id2<<endl;
    long xx=id2-id1+1;
    ans+=((xx*(xx+1))/2);
  }
  cout<<ans<<endl;
  return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>

using namespace std;

typedef long long li;
typedef  long double ld;
const li MAX = 1e6 + 20;

li inf = (li)1e14;
li mod = (li)1e9 + 7;
li n, a[MAX], b[MAX], spars_a[MAX][25], spars_b[MAX][25], power[MAX], ans = 0, val[MAX];

li get_min(li l, li r, li len) {
	li ret = min(spars_b[l][power[len]], spars_b[r - (1 << power[len]) + 1][power[len]]);
	return ret;
}

li get_max(li l, li r, li len) {
	li ret = max(spars_a[l][power[len]], spars_a[r - (1 << power[len]) + 1][power[len]]);
	return ret;
}

void solve() {
	cin >> n;
	for (int i = 0; i < MAX; i++) {
		for (int j = 0; j < 20; j++) {
			spars_a[i][j] = -inf;
			spars_b[i][j] = inf;
		}
	}

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

	power[1] = 0;
	for (int i = 2; i <= n; i++) {
		if ((1 << (power[i - 1] + 1)) <= i) power[i] = power[i - 1] + 1;
		else power[i] = power[i - 1];
	}

	for (int i = 1; i <= n; i++) spars_a[i][0] = a[i];
	for (int i = 1; i <= n; i++) spars_b[i][0] = b[i];

	for (int j = 1; j < 20; j++) {
		for (int i = 1; i <= n; i++) {
			spars_a[i][j] = max(spars_a[i][j - 1], spars_a[i + (1 << (j - 1))][j - 1]);
			spars_b[i][j] = min(spars_b[i][j - 1], spars_b[i + (1 << (j - 1))][j - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		li uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 <= val_2) uk2 = mid;
			else uk1 = mid;
		}
		li l = uk2;

		uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 >= val_2) uk1 = mid;
			else uk2 = mid;
		}
		li r = uk1;
		ans += r - l + 1;
	}

	cout << ans;
}

int main() {
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios::sync_with_stdio(0);
	li q;
	q = 1;
	while (q--) solve();

	return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>

using namespace std;

typedef long long li;
typedef  long double ld;
const li MAX = 1e6 + 20;

li inf = (li)1e8;
li mod = (li)1e9 + 7;
li n, a[MAX], b[MAX], spars_a[MAX][20], spars_b[MAX][20], power[MAX], ans = 0, val[MAX];

li get_min(li l, li r, li len) {
	li ret = min(spars_b[l][power[len]], spars_b[r - len + 1][power[len]]);
	return ret;
}

li get_max(li l, li r, li len) {
	li ret = max(spars_a[l][power[len]], spars_a[r - len + 1][power[len]]);
	return ret;
}

void solve() {
	cin >> n;
	for (int i = 0; i < MAX; i++) {
		for (int j = 0; j < 20; j++) {
			spars_a[i][j] = -inf;
			spars_b[i][j] = inf;
		}
	}

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

	power[1] = 0;
	for (int i = 2; i <= n; i++) {
		if ((1 << (power[i - 1] + 1)) <= i) power[i] = power[i - 1] + 1;
		else power[i] = power[i - 1];
	}

	for (int i = 1; i <= n; i++) spars_a[i][0] = a[i];
	for (int i = 1; i <= n; i++) spars_b[i][0] = b[i];

	for (int j = 1; j < 20; j++) {
		for (int i = 1; i <= n; i++) {
			spars_a[i][j] = max(spars_a[i][j - 1], spars_a[i + (1 << (j - 1))][j - 1]);
			spars_b[i][j] = min(spars_b[i][j - 1], spars_b[i + (1 << (j - 1))][j - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		li uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 <= val_2) uk2 = mid;
			else uk1 = mid;
		}
		li l = uk2;

		uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 >= val_2) uk1 = mid;
			else uk2 = mid;
		}
		li r = uk1;
		ans += r - l + 1;
	}

	cout << ans;
}

int main() {
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios::sync_with_stdio(0);
	li q;
	q = 1;
	while (q--) solve();

	return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>

using namespace std;

typedef long long li;
typedef  long double ld;
const li MAX = 1e6 + 20;

li inf = (li)1e14;
li mod = (li)1e9 + 7;
li n, a[MAX], b[MAX], spars_a[MAX][25], spars_b[MAX][25], power[MAX], ans = 0, val[MAX];

li get_min(li l, li r, li len) {
	li ret = min(spars_b[l][power[len]], spars_b[r - len + 1][power[len]]);
	return ret;
}

li get_max(li l, li r, li len) {
	li ret = max(spars_a[l][power[len]], spars_a[r - len + 1][power[len]]);
	return ret;
}

void solve() {
	cin >> n;
	for (int i = 0; i < MAX; i++) {
		for (int j = 0; j < 20; j++) {
			spars_a[i][j] = -inf;
			spars_b[i][j] = inf;
		}
	}

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

	power[1] = 0;
	for (int i = 2; i <= n; i++) {
		if ((1 << (power[i - 1] + 1)) <= i) power[i] = power[i - 1] + 1;
		else power[i] = power[i - 1];
	}

	for (int i = 1; i <= n; i++) spars_a[i][0] = a[i];
	for (int i = 1; i <= n; i++) spars_b[i][0] = b[i];

	for (int j = 1; j < 20; j++) {
		for (int i = 1; i <= n; i++) {
			spars_a[i][j] = max(spars_a[i][j - 1], spars_a[i + (1 << (j - 1))][j - 1]);
			spars_b[i][j] = min(spars_b[i][j - 1], spars_b[i + (1 << (j - 1))][j - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		li uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 <= val_2) uk2 = mid;
			else uk1 = mid;
		}
		li l = uk2;

		uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 >= val_2) uk1 = mid;
			else uk2 = mid;
		}
		li r = uk1;
		ans += r - l + 1;
	}

	cout << ans;
}

int main() {
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios::sync_with_stdio(0);
	li q;
	q = 1;
	while (q--) solve();

	return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>

using namespace std;

typedef long long li;
typedef  long double ld;
const li MAX = 3e5 + 20;

li inf = (li)1e8;
li mod = (li)1e9 + 7;
li n, a[MAX], b[MAX], spars_a[MAX][20], spars_b[MAX][20], power[MAX], ans = 0, val[MAX];

li get_min(li l, li r, li len) {
	li ret = min(spars_b[l][power[len]], spars_b[r - len + 1][power[len]]);
	return ret;
}

li get_max(li l, li r, li len) {
	li ret = max(spars_a[l][power[len]], spars_a[r - len + 1][power[len]]);
	return ret;
}

void solve() {
	cin >> n;
	for (int i = 0; i < MAX; i++) {
		for (int j = 0; j < 20; j++) {
			spars_a[i][j] = -inf;
			spars_b[i][j] = inf;
		}
	}

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

	power[1] = 0;
	for (int i = 2; i <= n; i++) {
		if ((1 << (power[i - 1] + 1)) <= i) power[i] = power[i - 1] + 1;
		else power[i] = power[i - 1];
	}

	for (int i = 1; i <= n; i++) spars_a[i][0] = a[i];
	for (int i = 1; i <= n; i++) spars_b[i][0] = b[i];

	for (int j = 1; j < 20; j++) {
		for (int i = 1; i <= n; i++) {
			spars_a[i][j] = max(spars_a[i][j - 1], spars_a[i + (1 << (j - 1))][j - 1]);
			spars_b[i][j] = min(spars_b[i][j - 1], spars_b[i + (1 << (j - 1))][j - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		li uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 <= val_2) uk2 = mid;
			else uk1 = mid;
		}
		li l = uk2;

		uk1 = 0, uk2 = i + 1;
		while (uk2 - uk1 > 1) {
			li mid = (uk2 + uk1) / 2;
			li val_1, val_2;
			val_1 = get_max(mid, i, i - mid + 1);
			val_2 = get_min(mid, i, i - mid + 1);
			if (val_1 >= val_2) uk1 = mid;
			else uk2 = mid;
		}
		li r = uk1;
		ans += r - l + 1;
	}

	cout << ans;
}

int main() {
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios::sync_with_stdio(0);
	li q;
	q = 1;
	while (q--) solve();

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using cd = complex <double>;

typedef pair <int, int> pii;

const long long Inf = 2e18;
const long long mod = 998244353;
const double Pi = acos(-1);

void Fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int n;
int a[200005], b[200005], STmin[200005][19], STmax[200005][19], L[200005];
long long ans = 0;

int Getmax(int l, int r)
{
	int temp = L[r - l + 1];
	return a[STmax[l][temp]] > a[STmax[r + 1 - (1 << temp)][temp]] ? STmax[l][temp] : STmax[r + 1 - (1 << temp)][temp];
}

int Getmin(int l, int r)
{
	int temp = L[r - l + 1];
	return b[STmin[l][temp]] < b[STmin[r + 1 - (1 << temp)][temp]] ? STmin[l][temp] : STmin[r + 1 - (1 << temp)][temp];
}

int main()
{
	Fastio();
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		STmax[i][0] = i;
	}
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
		STmin[i][0] = i;
	}
	L[0] = -1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i >> 1] + 1;
	}
	for(int i = 1; i <= 18; i++)
	{
		for(int j = 0; j + (1 << i) <= n; j++)
		{
			STmin[j][i] = (b[STmin[j][i - 1]] < b[STmin[j + (1 << (i - 1))][i - 1]]) ? STmin[j][i - 1] : STmin[j + (1 << (i - 1))][i - 1];
			STmax[j][i] = (a[STmax[j][i - 1]] > a[STmax[j + (1 << (i - 1))][i - 1]]) ? STmax[j][i - 1] : STmax[j + (1 << (i - 1))][i - 1];
		}
	}
	for(int i = 0; i < n; i++)
	{
		int l = i, r = n;
		if(a[i] > b[i])
		{
			continue;
		}
		int r1, r2;
		while(l < r)
		{
			int mid = (l + r) >> 1;
			if(a[Getmax(i, mid)] - b[Getmin(i, mid)] < 0)
			{
				l = mid + 1;
			}
			else
			{
				r = mid;
			}
		}
		r1 = l;
		l = i, r = n - 1;
		while(l < r)
		{
			int mid = (l + r + 1) >> 1;
			if(a[Getmax(i, mid)] - b[Getmin(i, mid)] <= 0)
			{
				l = mid;
			}
			else
			{
				r = mid - 1;
			}
		}
		r2 = l;
		ans += (r2 - r1 + 1);
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using cd = complex <double>;

typedef pair <int, int> pii;

const long long Inf = 2e18;
const long long mod = 998244353;
const double Pi = acos(-1);

void Fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int n;
int a[200005], b[200005], STmin[200005][19], STmax[200005][19], L[200005];
long long ans = 0;

int Getmax(int l, int r)
{
	int temp = L[r - l + 1];
	return a[STmax[l][temp]] > a[STmax[r + 1 - (1 << temp)][temp]] ? STmax[l][temp] : STmax[r + 1 - (1 << temp)][temp];
}

int Getmin(int l, int r)
{
	int temp = L[r - l + 1];
	return b[STmin[l][temp]] < b[STmin[r + 1 - (1 << temp)][temp]] ? STmin[l][temp] : STmin[r + 1 - (1 << temp)][temp];
}

int main()
{
	Fastio();
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		STmax[i][0] = i;
	}
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
		STmin[i][0] = i;
	}
	L[0] = -1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i >> 1] + 1;
	}
	for(int i = 1; i <= 18; i++)
	{
		for(int j = 0; j + (1 << i) <= n; j++)
		{
			STmin[j][i] = (b[STmin[j][i - 1]] < b[STmin[j + (1 << (i - 1))][i - 1]]) ? STmin[j][i - 1] : STmin[j + (1 << (i - 1))][i - 1];
			STmax[j][i] = (a[STmax[j][i - 1]] > a[STmax[j + (1 << (i - 1))][i - 1]]) ? STmax[j][i - 1] : STmax[j + (1 << (i - 1))][i - 1];
		}
	}
	for(int i = 0; i < n; i++)
	{
		int l = i, r = n - 1;
		int r1, r2;
		while(l < r)
		{
			int mid = (l + r) >> 1;
			if(a[Getmax(i, mid)] - b[Getmin(i, mid)] < 0)
			{
				l = mid + 1;
			}
			else
			{
				r = mid;
			}
		}
		r1 = l;
		if(a[Getmax(i, r1)] - b[Getmin(i, r1)] != 0 and i == r1)
		{
			r1++;
		}
		l = i, r = n - 1;
		while(l < r)
		{
			int mid = (l + r + 1) >> 1;
			if(a[Getmax(i, mid)] - b[Getmin(i, mid)] <= 0)
			{
				l = mid;
			}
			else
			{
				r = mid - 1;
			}
		}
		r2 = l;
		ans += (r2 - r1 + 1);
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using cd = complex <double>;

typedef pair <int, int> pii;

const long long Inf = 2e18;
const long long mod = 998244353;
const double Pi = acos(-1);

void Fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int n;
int a[200005], b[200005], STmin[200005][19], STmax[200005][19], L[200005];
long long ans = 0;

int Getmax(int l, int r)
{
	int temp = L[r - l + 1];
	return a[STmax[l][temp]] > a[STmax[r + 1 - (1 << temp)][temp]] ? STmax[l][temp] : STmax[r + 1 - (1 << temp)][temp];
}

int Getmin(int l, int r)
{
	int temp = L[r - l + 1];
	return b[STmin[l][temp]] < b[STmin[r + 1 - (1 << temp)][temp]] ? STmin[l][temp] : STmin[r + 1 - (1 << temp)][temp];
}

int main()
{
	Fastio();
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		STmax[i][0] = i;
	}
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
		STmin[i][0] = i;
	}
	L[0] = -1;
	for(int i = 1; i <= n; i++)
	{
		L[i] = L[i >> 1] + 1;
	}
	for(int i = 1; i <= 18; i++)
	{
		for(int j = 0; j + (1 << i) <= n; j++)
		{
			STmin[j][i] = (b[STmin[j][i - 1]] < b[STmin[j + (1 << (i - 1))][i - 1]]) ? STmin[j][i - 1] : STmin[j + (1 << (i - 1))][i - 1];
			STmax[j][i] = (a[STmax[j][i - 1]] > a[STmax[j + (1 << (i - 1))][i - 1]]) ? STmax[j][i - 1] : STmax[j + (1 << (i - 1))][i - 1];
		}
	}
	for(int i = 0; i < n; i++)
	{
		int l = i, r = n - 1;
		if(a[i] > b[i])
		{
			continue;
		}
		int r1, r2;
		while(l < r)
		{
			int mid = (l + r) >> 1;
			if(a[Getmax(i, mid)] - b[Getmin(i, mid)] < 0)
			{
				l = mid + 1;
			}
			else
			{
				r = mid;
			}
		}
		r1 = l;
		l = i, r = n - 1;
		while(l < r)
		{
			int mid = (l + r + 1) >> 1;
			if(a[Getmax(i, mid)] - b[Getmin(i, mid)] <= 0)
			{
				l = mid;
			}
			else
			{
				r = mid - 1;
			}
		}
		r2 = l;
		ans += (r2 - r1 + 1);
	}
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const int MOD = 998244353 ;
const int N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-7;
const long long oo = 1e10;
const int K = 205;

int n;
int a[N][2];
int cnt[2];
int tree[N][2];

int op(ll u, ll v, ll bit){
    if(bit==0){
        return max(u,v);
    }
    return min(u,v);
}
int def(ll bit){
    if(bit==0)return -1e9-1;
    return 1e9+1;
}

void build(int l,int r, int pos, int bit){
    if(l==r){
        tree[pos][bit]=a[l][bit];
        return ;

    }
    int mid=(l+r)/2;
    build(l,mid,2*pos+1,bit);
    build(mid+1,r,2*pos+2,bit);
    tree[pos][bit]=op(tree[2*pos+1][bit],tree[2*pos+2][bit],bit);
}

int geta(int l , int r, int pos, int u, int v, int bit){
    if(l> v || u>r )return def(bit);
    if(l>=u && r<=v){
        return tree[pos][bit];
    }
    int mid=(l+r)/2;
    int ans1=geta(l,mid,2*pos+1,u,v,bit);
    int ans2=geta(mid+1,r,2*pos+2,u,v,bit);
    return op(ans1,ans2,bit);
}

bool ok(ll u, ll v){
    int val1 = geta(0,n-1,0,u,v,0);
    int val2 = geta(0,n-1,0,u,v,1);
    if(val1<=val2)return true;
    return false;

}
bool ok2(ll u, ll v){
    int val1 = geta(0,n-1,0,u,v,0);
    int val2 = geta(0,n-1,0,u,v,1);
    if(val1<val2)return true;
    return false;

}

bool eq(ll u, ll v){
    int val1 = geta(0,n-1,0,u,v,0);
    int val2 = geta(0,n-1,0,u,v,1);
    return val1==val2;
}

int main(){
    FAST;
    //freopen("easy.txt","r",stdin);
    cin >> n;
    for(int i=0;i<n;i++)cin >> a[i][0];
    for(int i=0;i<n;i++)cin >> a[i][1];
    for(int k=0;k<2;k++){
        build(0,n-1,0,k);
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        ll l=0;
        ll r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        bool cond1  = eq(l,i);
        bool cond2 = eq(r,i);
        if(cond1==false && cond2==false)continue;
        int pos1 = (cond1?l:r);
        l=0;
        r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok2(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        cond1  = eq(l,i);
        cond2 = eq(r,i);
        int pos2 = (cond2?r:l);
        ans+=(pos2-pos1+1);
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const int MOD = 998244353 ;
const int N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-7;
const long long oo = 1e10;
const int K = 205;

ll n;
ll a[N][2];
int cnt[2];
ll tree[N][2];

ll op(ll u, ll v, ll bit){
    if(bit==0){
        return max(u,v);
    }
    return min(u,v);
}
ll def(ll bit){
    if(bit==0)return -1e17;
    return 1e17;
}

void build(ll l,ll r, ll pos, ll bit){
    if(l==r){
        tree[pos][bit]=a[l][bit];
        return ;

    }
    ll mid=(l+r)/2;
    build(l,mid,2*pos+1,bit);
    build(mid+1,r,2*pos+2,bit);
    tree[pos][bit]=op(tree[2*pos+1][bit],tree[2*pos+2][bit],bit);
}

ll geta(ll l , ll r, ll pos, ll u, ll v, ll bit){
    if(l> v || u>r )return def(bit);
    if(l>=u && r<=v){
        return tree[pos][bit];
    }
    ll mid=(l+r)/2;
    ll ans1=geta(l,mid,2*pos+1,u,v,bit);
    ll ans2=geta(mid+1,r,2*pos+2,u,v,bit);
    return op(ans1,ans2,bit);
}

bool ok(ll u, ll v){
    ll val1 = geta(0,n-1,0,u,v,0);
    ll val2 = geta(0,n-1,0,u,v,1);
    if(val1<=val2)return true;
    return false;

}
bool ok2(ll u, ll v){
    ll val1 = geta(0,n-1,0,u,v,0);
    ll val2 = geta(0,n-1,0,u,v,1);
    if(val1<val2)return true;
    return false;

}

bool eq(ll u, ll v){
    ll val1 = geta(0,n-1,0,u,v,0);
    ll val2 = geta(0,n-1,0,u,v,1);
    return val1==val2;
}

int main(){
    FAST;
    //freopen("easy.txt","r",stdin);
    cin >> n;
    for(int i=0;i<n;i++)cin >> a[i][0];
    for(int i=0;i<n;i++)cin >> a[i][1];
    for(int k=0;k<2;k++){
        build(0,n-1,0,k);
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        ll l=0;
        ll r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        bool cond1  = eq(l,i);
        bool cond2 = eq(r,i);
        if(cond1==false && cond2==false)continue;
        ll pos1 = (cond1?l:r);
        l=0;
        r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok2(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        cond1  = eq(l,i);
        cond2 = eq(r,i);
        ll pos2 = (cond2?r:l);
        ans+=(pos2-pos1+1);
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const int MOD = 998244353 ;
const int N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-7;
const long long oo = 1e10;
const int K = 205;

int n;
int a[N][2];
int cnt[2];
pair<int,int> tree[N];

pair<int,int> mergeseg(int u, int v){
    return {max(tree[u].f,tree[v].f),min(tree[u].s,tree[v].s)};
}

void build(int l,int r, int pos){
    if(l==r){
        tree[pos]={a[l][0],a[l][1]};
        return ;

    }
    int mid=(l+r)/2;
    build(l,mid,2*pos+1);
    build(mid+1,r,2*pos+2);
    tree[pos]=mergeseg(2*pos+1,2*pos+2);
}

pair<int,int>  geta(int l , int r, int pos, int u, int v){
    if(l> v || u>r )return {-1e9-1,1e9+1};
    if(l>=u && r<=v){
        return tree[pos];
    }
    int mid=(l+r)/2;
    pair<int,int> ans1=geta(l,mid,2*pos+1,u,v);
    pair<int,int> ans2=geta(mid+1,r,2*pos+2,u,v);
    return {max(ans1.f,ans2.f),min(ans1.s,ans2.s)};
}

bool ok(ll u, ll v){
    pair<int,int > val  = geta(0,n-1,0,u,v);
    int val1 = val.f;
    int val2 = val.s;
    if(val1<=val2)return true;
    return false;

}
bool ok2(ll u, ll v){
    pair<int,int > val  = geta(0,n-1,0,u,v);
    int val1 = val.f;
    int val2 = val.s;
    if(val1<val2)return true;
    return false;

}

bool eq(ll u, ll v){
    pair<int,int > val  = geta(0,n-1,0,u,v);
    int val1 = val.f;
    int val2 = val.s;
    return val1==val2;
}

int main(){
    FAST;
    //freopen("easy.txt","r",stdin);
    cin >> n;
    for(int i=0;i<n;i++)cin >> a[i][0];
    for(int i=0;i<n;i++)cin >> a[i][1];
    build(0,n-1,0);
    ll ans=0;
    for(int i=0;i<n;i++){
        ll l=0;
        ll r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        bool cond1  = eq(l,i);
        bool cond2 = eq(r,i);
        if(cond1==false && cond2==false)continue;
        int pos1 = (cond1?l:r);
        l=0;
        r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok2(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        cond1  = eq(l,i);
        cond2 = eq(r,i);
        int pos2 = (cond2?r:l);
        ans+=(pos2-pos1+1);
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const int MOD = 998244353 ;
const int N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-7;
const long long oo = 1e10;
const int K = 205;

ll n;
ll a[N][2];
int cnt[2];
ll tree[N][2];

ll op(ll u, ll v, ll bit){
    if(bit==0){
        return max(u,v);
    }
    return min(u,v);
}
ll def(ll bit){
    if(bit==0)return -1e5;
    return 1e17;
}

void build(ll l,ll r, ll pos, ll bit){
    if(l==r){
        tree[pos][bit]=a[l][bit];
        return ;

    }
    ll mid=(l+r)/2;
    build(l,mid,2*pos+1,bit);
    build(mid+1,r,2*pos+2,bit);
    tree[pos][bit]=op(tree[2*pos+1][bit],tree[2*pos+2][bit],bit);
}

ll geta(ll l , ll r, ll pos, ll u, ll v, ll bit){
    if(l> v || u>r )return def(bit);
    if(l>=u && r<=v){
        return tree[pos][bit];
    }
    ll mid=(l+r)/2;
    ll ans1=geta(l,mid,2*pos+1,u,v,bit);
    ll ans2=geta(mid+1,r,2*pos+2,u,v,bit);
    return op(ans1,ans2,bit);
}

bool ok(ll u, ll v){
    ll val1 = geta(0,n-1,0,u,v,0);
    ll val2 = geta(0,n-1,0,u,v,1);
    if(val1<=val2)return true;
    return false;

}
bool ok2(ll u, ll v){
    ll val1 = geta(0,n-1,0,u,v,0);
    ll val2 = geta(0,n-1,0,u,v,1);
    if(val1<val2)return true;
    return false;

}

bool eq(ll u, ll v){
    ll val1 = geta(0,n-1,0,u,v,0);
    ll val2 = geta(0,n-1,0,u,v,1);
    return val1==val2;
}

int main(){
    FAST;
   // freopen("easy.txt","r",stdin);
    cin >> n;
    for(int i=0;i<n;i++)cin >> a[i][0];
    for(int i=0;i<n;i++)cin >> a[i][1];
    for(int k=0;k<2;k++){
        build(0,n-1,0,k);
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        ll l=0;
        ll r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        bool cond1  = eq(l,i);
        bool cond2 = eq(r,i);
        if(cond1==false && cond2==false)continue;
        ll pos1 = (cond1?l:r);
        l=0;
        r=i;
        while(l<r-1){
            ll mid=(l+r)/2;
            if(ok2(mid,i)){
                r=mid;
            }else{
                l=mid;
            }
        }
        cond1  = eq(l,i);
        cond2 = eq(r,i);
        ll pos2 = (cond2?r:l);
        ans+=(pos2-pos1+1);
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef	long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef pair<ll, pll> plll;
typedef vector<pair<int, pii>> viii;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define tc int tt;cin>>tt;while(tt--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;

int main() {
	fio;
	int n;
	cin >> n;
	int k = log2(n) + 1;
	int mxa[k][n], mnb[k][n];
	for (int i = 0;i < n;i++)
		cin >> mxa[0][i];
	for (int i = 0;i < n;i++)
		cin >> mnb[0][i];
	for (int j = 1;j < k;j++)
		for (int i = 0;i + (1<<j) <= n;i++) {
			mxa[j][i] = max(mxa[j - 1][i], mxa[j - 1][i + (1<<(j - 1))]);
			mnb[j][i] = min(mnb[j - 1][i], mnb[j - 1][i + (1<<(j - 1))]);
		}
	ll ans = 0;
	for (int i = 0;i < n;i++) {
		int l = i, r = i;
		for (int j = k - 1, ca = INT32_MIN, cb = INT32_MAX;j >= 0;j--)
			if (l + (1<<j) <= n && max(ca, mxa[j][l]) < min(cb, mnb[j][l])) {
				ca = max(ca, mxa[j][l]);
				cb = min(cb, mnb[j][l]);
				l += (1<<j);
			}
		for (int j = k - 1, ca = INT32_MIN, cb = INT32_MAX;j >= 0;j--)
			if (r + (1<<j) <= n && max(ca, mxa[j][r]) <= min(cb, mnb[j][r])) {
				ca = max(ca, mxa[j][r]);
				cb = min(cb, mnb[j][r]);
				r += (1<<j);
			}
		ans += r - l;
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef	long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef pair<ll, pll> plll;
typedef vector<pair<int, pii>> viii;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define tc int tt;cin>>tt;while(tt--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;

int main() {
	fio;
	int n;
	cin >> n;
	int k = log2(n) + 1;
	int mxa[k][n], mnb[k][n];
	for (int i = 0;i < n;i++)
		cin >> mxa[0][i];
	for (int i = 0;i < n;i++)
		cin >> mnb[0][i];
	for (int j = 1;j < k;j++)
		for (int i = 0;i + (1<<j) <= n;i++) {
			mxa[j][i] = max(mxa[j - 1][i], mxa[j - 1][i + (1<<(j - 1))]);
			mnb[j][i] = min(mnb[j - 1][i], mnb[j - 1][i + (1<<(j - 1))]);
		}
	ll ans = 0;
	for (int i = 0;i < n;i++) {
		int l = i, r = i;
		for (int j = k - 1;j >= 0;j--)
			if (l + (1<<j) <= n && mxa[j][l] < mnb[j][l])
				l += (1<<j);
		for (int j = k - 1;j >= 0;j--)
			if (r + (1<<j) <= n && mxa[j][r] <= mnb[j][r])
				r += (1<<j);
		ans += r - l;
	}
	cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;

#define DEBUG if(1)
#define MAX INT_MAX
#define MAXLL INT_MAX
#define MAXU ULLONG_MAX
#define MIN -2000000
#define endl "\n"
#define INF INT_MAX
#define MOD 1000000007
#define s(n) scanf("%d", &n)
#define ss(a,b) scanf("%d %d",&a,&b)
#define pb push_back
#define mp make_pair
#define M_PI 3.14159265358979323846
#define sz(a) int(a.size())
#define lli long long int
#define rep(i,a,n) for (int i=a;i<n;i++)
#define ler(a,n,vec) for(int i=0;i<n;i++)s(a), vec.pb(a)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<lli,lli> ii;
#define DEBUG if(1)
#define F first
#define S second
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int ddx[] = {1, 0};
int ddy[] = {1, 1};
#define MAXN 200010
#define MAXL 20

int n, m;
// vector<pair<lli,lli>> value;
vector<lli> value;
int pai[MAXN];
int peso[MAXN];

vector<ii> vec[MAXN];

vector< pair<lli, ii > > edges;

int search(int x){
	if(pai[x] == x) return x;
	return pai[x] = search(pai[x]);
}

void join(int a, int b){
	a = search(pai[a]);
	b = search(pai[b]);

	if(a == b) return ;

	if(peso[a] < peso[b]){
		pai[a] = b;
	} else if(peso[a] > peso[b]){
		pai[b] = a;
	} else {
		pai[a] = b;
		peso[b]++;
	}
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;
  int atual = 1;
  lli xx = LLONG_MAX;
  for(int i=1;i<=n;i++){
  	lli a;
  	cin >> a;
  	if(a < xx) xx = a, atual = i;
  	value.pb(a);
  	// value.pb({a, i});
  }

  // int atual = 1;

  for(lli i=0;i<sz(value);i++){
  	if(atual == i+1) continue;
  	edges.pb({value[atual-1] + value[i], {atual, i+1}});
  }

  // for(int i=1;i<sz(value);i++){
  	// edges.pb({value[0].F + value[i].F, {value[0].S, value[i].S}});
  // }

  for(int i=0;i<m;i++){
  	int a, b, c;
  	cin >> a >> b >> c;
  	edges.pb({c, {a, b}});
  }

  sort(edges.begin(), edges.end());

  lli ans = 0;
  for(int i=0;i<=n;i++) pai[i] = i;

  for(int i=0;i<sz(edges);i++){
  	if(search(edges[i].S.F) != search(edges[i].S.S)){
  		join(edges[i].S.F, edges[i].S.S);
  		ans += edges[i].F;
  	}
  }

  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define DEBUG if(1)
#define MAX INT_MAX
#define MAXLL INT_MAX
#define MAXU ULLONG_MAX
#define MIN -2000000
#define endl "\n"
#define INF INT_MAX
#define MOD 1000000007
#define s(n) scanf("%d", &n)
#define ss(a,b) scanf("%d %d",&a,&b)
#define pb push_back
#define mp make_pair
#define M_PI 3.14159265358979323846
#define sz(a) int(a.size())
#define lli long long int
#define rep(i,a,n) for (int i=a;i<n;i++)
#define ler(a,n,vec) for(int i=0;i<n;i++)s(a), vec.pb(a)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define DEBUG if(1)
#define F first
#define S second
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int ddx[] = {1, 0};
int ddy[] = {1, 1};
#define MAXN 200010
#define MAXL 20

int n, m;
vector<pair<lli,lli>> value;
int pai[MAXN];
int peso[MAXN];

vector<ii> vec[MAXN];

vector< pair<int, ii > > edges;

int search(int x){
	if(pai[x] == x) return x;
	return pai[x] = search(pai[x]);
}

void join(int a, int b){
	a = search(pai[a]);
	b = search(pai[b]);

	if(a == b) return ;

	if(peso[a] < peso[b]){
		pai[a] = b;
	} else if(peso[a] > peso[b]){
		pai[b] = a;
	} else {
		pai[a] = b;
		peso[b]++;
	}
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;
  for(int i=1;i<=n;i++){
  	lli a;
  	cin >> a;
  	value.pb({a, i});
  }

  sort(value.begin(), value.end());

  for(int i=1;i<sz(value);i++){
  	edges.pb({value[0].F + value[i].F, {value[0].S, value[i].S}});
  }

  // for(int i=0;i<sz(edges);i++){
  // 	cout << edges[i].F.F << " - " << edges[i].F.S << " - " << edges[i].S << endl;
  // }

  for(int i=0;i<m;i++){
  	int a, b, c;
  	cin >> a >> b >> c;
  	edges.pb({c, {a, b}});
  }

  sort(edges.begin(), edges.end());

  lli ans = 0;
  for(int i=1;i<=n;i++) pai[i] = i;

  for(int i=0;i<sz(edges);i++){
  	if(search(edges[i].S.F) != search(edges[i].S.S)){
  		join(edges[i].S.F, edges[i].S.S);
  		ans += edges[i].F;
  	}
  }

  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define N 1e12+5
#define pll pair<ll,ll>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll p[MAX];

ll find(ll x){
	if(p[x] == x) return x;
	return  p[x]=find(p[x]);
}

bool join(ll x, ll y){

	x = find(x);
	y = find(y);
	if(x==y) return false;
	p[x] = y;
	return true;
}
int main(){

	fastcin

	ll n, m, ans=0;
	cin >> n >> m;

	ll v[n+1];

	vector<pair<ll, pll>> q;

	for(int i=1; i<=n; i++) cin >> v[i];

	int s;
	ll aux=N;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1;i<=n; i++)p[i]=i;

	for(int i=1; i<=n; i++){
		if(i == s) continue;

		q.push_back({v[s] + v[i],{s, i}});
	}

	ll x, y, w;

	while(m--){
		cin >> x >> y >> w;
		q.push_back({w,{x,y}});
	}

	sort(q.begin(), q.end());

	for(int i=0; i<q.size(); i++){
		if(join(q[i].second.first, q[i].second.second))ans+=q[i].first;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define pll pair<ll,ll>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll p[MAX];

int main(){

	fastcin

	ll n, m, ans=0;
	cin >> n >> m;

	ll v[n+1];

	vector<pair<ll, pll>> q;

	for(int i=1; i<=n; i++) cin >> v[i];

	ll s, aux=MAX;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1; i<=n; i++){
		if(i == s) continue;
		q.push_back({v[s] + v[i],{s, i}});
		p[i] = MAX;
	}

	ll x, y, w;

	while(m--){
		cin >> x >> y >> w;
		q.push_back({w,{x,y}});
	}

	sort(q.begin(), q.end());

	for(int i=0; i<q.size(); i++){
		ll u;
		x = q[i].second.first;
		y = q[i].second.second;
		w = q[i].first;

		if(p[x] <= p[y]) u = y;
		else u = x;

		if(p[u] > w) p[u] = w;
	}

	for(int i=1; i<=n; i++) ans+=p[i];

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define pll pair<ll,ll>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll p[MAX];

ll find(ll x){
	if(p[x] == x) return x;
	return  p[x]=find(p[x]);
}

bool join(ll x, ll y){

	x = find(x);
	y = find(y);
	if(x==y) return false;
	p[x] = y;
	return true;
}
int main(){

	fastcin

	ll n, m, ans=0;
	cin >> n >> m;

	ll v[n+1];

	vector<pair<ll, pll>> q;

	for(int i=1; i<=n; i++) cin >> v[i];

	ll s, aux=MAX;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1;i<=n; i++)p[i]=i;

	for(ll i=1; i<=n; i++){
		if(i == s) continue;
		q.push_back({v[s] + v[i],{s, i}});
	}

	ll x, y, w;

	while(m--){
		cin >> x >> y >> w;
		q.push_back({w,{x,y}});
	}

	sort(q.begin(), q.end());

	for(int i=0; i<q.size(); i++){
		if(join(q[i].second.first, q[i].second.second))ans+=q[i].first;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define pll pair<ll,ll>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll p[MAX];

ll find(ll x){
	if(p[x] == x) return x;
	return  p[x]=find(p[x]);
}

bool join(ll x, ll y){

	x = find(x);
	y = find(y);
	if(x==y) return false;
	p[x] = y;
	return true;
}
int main(){

	fastcin

	ll n, m, ans=0;
	cin >> n >> m;

	ll v[n+1];

	vector<pair<ll, pll>> q;

	for(int i=1; i<=n; i++) cin >> v[i];

	int s;
	ll aux=MAX;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1;i<=n; i++)p[i]=i;

	for(int i=1; i<=n; i++){
		if(i == s) continue;

		q.push_back({v[s] + v[i],{s, i}});
	}

	ll x, y, w;

	while(m--){
		cin >> x >> y >> w;
		q.push_back({w,{x,y}});
	}

	sort(q.begin(), q.end());

	for(int i=0; i<q.size(); i++){
		if(join(q[i].second.first, q[i].second.second))ans+=q[i].first;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define pll pair<ll,ll>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll p[MAX];

int main(){

	fastcin

	ll n, m, ans=0;
	cin >> n >> m;

	ll v[n+1];

	vector<pair<ll, pll>> q;

	for(int i=1; i<=n; i++) cin >> v[i];

	ll s, aux=MAX;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1; i<=n; i++){
		if(i == s) continue;
		p[i] = v[s] + v[i];
	}

	ll x, y, w;

	while(m--){
		cin >> x >> y >> w;
		q.push_back({w,{x,y}});
	}

	sort(q.begin(), q.end());

	for(int i=0; i<q.size(); i++){
		ll u;
		x = q[i].second.first;
		y = q[i].second.second;
		w = q[i].first;

		if(p[x] <= p[y]) u = y;
		else u = x;

		if(p[u] > w) p[u] = w;
	}

	for(int i=1; i<=n; i++) ans+=p[i];

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

int p[MAX];

int main(){

	fastcin

	int n, m, ans=0;
	cin >> n >> m;

	int v[n+1];

	for(int i=1; i<=n; i++) cin >> v[i];

	int s, aux=MAX;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1; i<=n; i++){
		if(i == s) continue;
		p[i] = v[s] + v[i];
	}

	int x, y, w;
	while(m--){
		cin >> x >> y >> w;

		int u;

		if(p[x] <= p[y]) u = y;
		else u = x;

		if(p[u] > w) p[u] = w;

	}

	for(int i=1; i<=n; i++) ans+=p[i];

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define MAX 200005
#define pll pair<ll,ll>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

ll p[MAX];

ll find(ll x){
	if(p[x] == x) return x;
	return  p[x]=find(p[x]);
}

bool join(ll x, ll y){

	x = find(x);
	y = find(y);
	if(x==y) return false;
	p[x] = y;
	return true;
}
int main(){

	fastcin

	ll n, m, ans=0;
	cin >> n >> m;

	ll v[n+1];

	vector<pair<ll, pll>> q;

	for(int i=1; i<=n; i++) cin >> v[i];

	ll s, aux=MAX;
	for(int i=1; i<=n; i++){
		if(v[i] < aux){
			aux = v[i];
			s = i;
		}
	}

	for(int i=1;i<=n; i++)p[i]=i;

	for(int i=1; i<=n; i++){
		if(i == s) continue;
		q.push_back({v[s] + v[i],{s, i}});
	}

	ll x, y, w;

	while(m--){
		cin >> x >> y >> w;
		q.push_back({w,{x,y}});
	}

	sort(q.begin(), q.end());

	for(int i=0; i<q.size(); i++){
		if(join(q[i].second.first, q[i].second.second))ans+=q[i].first;
	}

	cout << ans << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[100009] , par[100009] , x, y,w,tot , para , parb;

long long parent(long long x)
{
	if(x == par[x])
		return x;
	return parent(par[x]);
}

bool check_if_same_comp(long long a , long long b)
{
	a = parent(a); //parent of a
	b = parent(b); //parent of b
	//cout<<"\nparent of "<<a<<" : "<<a<<"\t parent of "<<b<<" : "<<b;
	if(a == b) //if they belong to the same component
		return true;
	else  //=>they do not belong to the same component
	{
		par[b]=a;  //a is now actually original a's parent => setting b's parent as a's parent
		return false;
	}
}

vector<pair<long long,pair<long long,long long>>>v;
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	for(i=1;i<=n;i++)
	{
		if(i!=pos)
		{
			v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
		}
	}
	sort(v.begin(),v.end());
	// for(i=0;i<n-1+m;i++)
	// {
	// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
	// }
	tot=0;
	for(i=0;i<n-1+m;i++)
	{
		if(!check_if_same_comp(v[i].second.first , v[i].second.second))
		//if both the vertices are already present in the given component, don't add it=>will cause cycle
		{
			// cout<<"\n"<<v[i].first;
			tot += v[i].first;
		}
	}
	cout<<tot;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[200009] , par[200009] , x, y,w,tot;
vector<pair<long long,pair<long long,long long>>>v;

long long parent(long long x)
 {
 	if(x == par[x])
 		return x;
 	return parent(par[x]);
 }

bool check_if_same_comp(long long a , long long b)
{
	a = parent(a); //parent of a
	b = parent(b); //parent of b
	//cout<<"\nparent of "<<a<<" : "<<a<<"\t parent of "<<b<<" : "<<b;
	if(a == b) //if they belong to the same component
		return true;
	else  //=>they do not belong to the same component
	{
		par[b]=a;  //a is now actually original a's parent => setting b's parent as a's parent
		return false;
	}
}
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	if(m!=0)
	{
		for(i=1;i<=n;i++)
		{
			if(i!=pos)
			{
				v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
			}
		}
		sort(v.begin(),v.end());
		// for(i=0;i<n-1+m;i++)
		// {
		// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
		// }
		tot=0;
		for(i=0;i<n-1+m;i++)
		{
			if(!check_if_same_comp(v[i].second.first , v[i].second.second))
			//if both the vertices are already present in the given component, don't add it=>will cause cycle
			{
				// cout<<"\n"<<v[i].first;
				tot += v[i].first;
			}
		}
	}
	else
	{
		tot = (n-1)*mini;
		for(i=1;i<=n;i++)
		{
			if(i!=pos)
				tot += a[i];
		}
	}
	cout<<tot;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[200009] , par[200009] , x, y,w,tot;
vector<pair<long long,pair<long long,long long>>>v;

long long parent(long long x)
 {
 	if(x == par[x])
 		return x;
 	return parent(par[x]);
 }

bool check_if_same_comp(long long a , long long b)
{
	a = parent(a); //parent of a
	b = parent(b); //parent of b
	//cout<<"\nparent of "<<a<<" : "<<a<<"\t parent of "<<b<<" : "<<b;
	if(a == b) //if they belong to the same component
		return true;
	else  //=>they do not belong to the same component
	{
		par[b]=a;  //a is now actually original a's parent => setting b's parent as a's parent
		return false;
	}
}
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	for(i=1;i<=n;i++)
	{
		if(i!=pos)
		{
			v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
		}
	}
	sort(v.begin(),v.end());
	// for(i=0;i<n-1+m;i++)
	// {
	// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
	// }
	tot=0;
	for(i=0;i<n-1+m;i++)
	{
		if(!check_if_same_comp(v[i].second.first , v[i].second.second))
		//if both the vertices are already present in the given component, don't add it=>will cause cycle
		{
			// cout<<"\n"<<v[i].first;
			tot += v[i].first;
		}
	}
	cout<<tot;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n , i , a[1000000] , mini , pos , total_val , x , y , w ,m;
int main()
{
	cin>>n>>m;
	mini = 10000000000000;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]<mini)
		{
			mini = a[i];
			pos=i; //indicates the vertex number
		}
	}

	total_val = 0;

	total_val = ((n-1)*a[pos]);
	//cout<<"\n"<<total_val<<"\n";

	for(i=1;i<=n;i++)
	{
		if(i!=pos)
			total_val += a[i];
	}

	//cout<<"\n"<<total_val<<"\n";

	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		if(x!=pos && y!=pos)
		{
			if(a[x]>a[y])
			{
				if(w+a[y]+a[pos]<(a[x]+a[y]+a[pos]+a[pos]))
					total_val = total_val - (a[x]+a[pos]) + w ;
			}
			else
			{
				if(w+a[x]+a[pos]<(a[x]+a[y]+a[pos]+a[pos]))
					total_val = total_val - (a[y]+a[pos]) + w ;
			}
			//cout<<"\n///////"<<total_val<<"\n";
			//cout<<"\ntotal val after : x = "<<x<<" and y = "<<y<<" and w = "<<w<<" is "<<total_val<<"\n";
		}
		else
		{
			//cout<<"//////////////////////////////////////";
			if(x==pos)
			{
				if(w<a[x]+a[y])
				{
					total_val = total_val - a[y] - a[x] + w;
				}
			}
			else if(y==pos)
			{
				if(w<a[x]+a[pos])
				{
					total_val = total_val - a[x] -a[y] + w;
				}
			}
		}
	}

	//cout<<"\n";
	cout<<total_val;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[200009] , par[200009] , x, y,w,tot , para , parb;

long long parent(long long x)
{
	if(x == par[x])
		return x;
	return parent(par[x]);
}

bool check_if_same_comp(long long a , long long b)
{
	a = parent(a); //parent of a
	b = parent(b); //parent of b
	//cout<<"\nparent of "<<a<<" : "<<a<<"\t parent of "<<b<<" : "<<b;
	if(a == b) //if they belong to the same component
		return true;
	else  //=>they do not belong to the same component
	{
		par[b]=a;  //a is now actually original a's parent => setting b's parent as a's parent
		return false;
	}
}

vector<pair<long long,pair<long long,long long>>>v;
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	for(i=1;i<=n;i++)
	{
		if(i!=pos)
		{
			v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
		}
	}
	sort(v.begin(),v.end());
	// for(i=0;i<n-1+m;i++)
	// {
	// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
	// }
	tot=0;
	for(i=0;i<n-1+m;i++)
	{
		if(!check_if_same_comp(v[i].second.first , v[i].second.second))
		//if both the vertices are already present in the given component, don't add it=>will cause cycle
		{
			// cout<<"\n"<<v[i].first;
			tot += v[i].first;
		}
	}
	cout<<tot;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[200009] , par[200009] , x, y,w,tot , s[200009];
vector<pair<long long,pair<long long,long long>>>v;

long long parent(long long x)
 {
 	while(x!=par[x])
 	{
 		par[x] = par[par[x]]; //jumping by grandparents => reduces complexity to log*N
 		x = par[x];
 	}
 	return x;
 }

void add_comps(long long a , long long b)
{
	if(s[a]<s[b])
	{
		par[a] = b;
		s[b]+=s[a]; //making the bigger component the parent of the smaller one
	}
	else
	{
		par[b] = a;
		s[a] += s[b];
	}
}

bool check_if_same_comp(long long a , long long b)
{
	a = parent(a); //parent of a
	b = parent(b); //parent of b
	//cout<<"\nparent of "<<a<<" : "<<a<<"\t parent of "<<b<<" : "<<b;
	if(a == b) //if they belong to the same component
		return true;
	else  //=>they do not belong to the same component
	{
		add_comps(a,b);  //connecting the components of a and b=>union
		//a is now actually original a's parent => setting b's parent as a's parent
		return false;
	}
}
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		s[i] = 1;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	if(m!=0)
	{
		for(i=1;i<=n;i++)
		{
			if(i!=pos)
			{
				v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
			}
		}
		sort(v.begin(),v.end());
		// for(i=0;i<n-1+m;i++)
		// {
		// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
		// }
		tot=0;
		for(i=0;i<n-1+m;i++)
		{
			if(!check_if_same_comp(v[i].second.first , v[i].second.second))
			//if both the vertices are already present in the given component, don't add it=>will cause cycle
			{
				// cout<<"\n"<<v[i].first;
				tot += v[i].first;
			}
		}
	}
	else
	{
		tot = (n-1)*mini;
		for(i=1;i<=n;i++)
		{
			if(i!=pos)
				tot += a[i];
		}
	}
	cout<<tot;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[100009] , par[100009] , x, y,w,tot , para , parb;

long long parent(long long x)
{
	if(x == par[x])
		return x;
	return parent(par[x]);
}

bool check_if_same_comp(long long a , long long b)
{
	para = parent(a);
	parb = parent(b);
	//cout<<"\nparent of "<<a<<" : "<<para<<"\t parent of "<<b<<" : "<<parb;
	if(parb == para)
		return true;
	else
	{
		par[a] = par[b]; //connecting b to a which makes a the parent of b
		return false;
	}
}

vector<pair<long long,pair<long long,long long>>>v;
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	for(i=1;i<=n;i++)
	{
		if(i!=pos)
		{
			v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
		}
	}
	sort(v.begin(),v.end());
	// for(i=0;i<n-1+m;i++)
	// {
	// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
	// }
	tot=0;
	for(i=0;i<n-1+m;i++)
	{
		//cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
		if(!check_if_same_comp(v[i].second.first , v[i].second.second))
		//if both the vertices are already present in the given component, don't add it=>will cause cycle
		{
			//cout<<"\n"<<v[i].first;
			tot += v[i].first;
		}
	}
	cout<<tot;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n , m , i , mini, pos , a[200009] , par[200009] , x, y,w,tot;
vector<pair<long long,pair<long long,long long>>>v;

long long parent(long long x)
 {
 	if(x == par[x])
 		return x;
 	return par[x]=parent(par[x]);
 }

bool check_if_same_comp(long long a , long long b)
{
	a = parent(a); //parent of a
	b = parent(b); //parent of b
	//cout<<"\nparent of "<<a<<" : "<<a<<"\t parent of "<<b<<" : "<<b;
	if(a == b) //if they belong to the same component
		return true;
	else  //=>they do not belong to the same component
	{
		par[b]=a;  //a is now actually original a's parent => setting b's parent as a's parent
		return false;
	}
}
int main()
{
	cin>>n>>m;
	mini = 1000000000009;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		par[i] = i;
		if(a[i]<mini)
		{
			mini=a[i];
			pos=i;
		}
	}
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>w;
		v.push_back(make_pair(w , make_pair(x , y)));
	}

	if(m!=0)
	{
		for(i=1;i<=n;i++)
		{
			if(i!=pos)
			{
				v.push_back(make_pair(a[i]+a[pos] , make_pair(i , pos)));
			}
		}
		sort(v.begin(),v.end());
		// for(i=0;i<n-1+m;i++)
		// {
		// 	cout<<"\nx: "<<v[i].second.first<<"\ty: "<<v[i].second.second<<"\tweight: "<<v[i].first;
		// }
		tot=0;
		for(i=0;i<n-1+m;i++)
		{
			if(!check_if_same_comp(v[i].second.first , v[i].second.second))
			//if both the vertices are already present in the given component, don't add it=>will cause cycle
			{
				// cout<<"\n"<<v[i].first;
				tot += v[i].first;
			}
		}
	}
	else
	{
		tot = (n-1)*mini;
		for(i=1;i<=n;i++)
		{
			if(i!=pos)
				tot += a[i];
		}
	}
	cout<<tot;
	return 0;
}


#include <bits/stdc++.h>
#define SPEED ios::sync_with_stdio(false); cin.tie(0)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ll unsigned long long
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define INF 1e18+100
#define eps 0.00001
#define stp setprecision(20)
#define endl '\n'
using namespace std;

  struct DSU
{
	int S;

	struct node
	{
		int p; ll sum;
	};
	vector<node> dsu;

	DSU(int n)
	{
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	void reset(int n)
	{
		dsu.clear();
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	int rt(int u)
	{
		if(dsu[u].p == u) return u;

		return dsu[u].p = rt(dsu[u].p);
	}

	void merge(int u, int v)
	{
		u = rt(u); v = rt(v);
		if(u == v) return ;

		dsu[v].p = u;
		dsu[u].sum += dsu[v].sum;
	}

	bool sameset(int u, int v)
	{
		if(rt(u) == rt(v)) return true;
		return false;
	}

	ll getstat(int u)
	{
		return dsu[rt(u)].sum;
	}
};

ll a[200010];
int main()
{    SPEED;

       int n,m;
       cin>>n>>m;ll b =INF; int id =0 ;
       loop(i,0,n)
        {cin>>a[i];;
           if(a[i]<b)id=i,b=a[i];
		}

       vector< pair<ll,pll> > edj;

         loop(i,0,m)
          {
          	  int u,v,w;
          	  cin>>u>>v>>w;
          	  edj.pb({w,{u-1,v-1}});

		  }

		   //cout<<b<<endl;

					 loop(j,0,n)
					  {
					  	 if(j==id)continue;

					  	 edj.pb({a[id]+a[j],{j,id}});
						}

		 sort(edj.begin(),edj.end());

		 	 DSU dsu(n); ll ans=0;
		 loop(i,0,edj.size())
		  {
		          int u = edj[i].ss.ff;int v=edj[i].ss.ss;
		           if(dsu.rt(u)!=dsu.rt(v))
		            {
		            	  dsu.merge(u,v);
		            	  ans+= edj[i].ff;
					}
		  }
		  cout<<ans<<endl;
		 return 0;

}


#include <bits/stdc++.h>
#define SPEED ios::sync_with_stdio(false); cin.tie(0)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define INF 1e18
#define eps 0.00001
#define stp setprecision(20)
#define endl '\n'
using namespace std;

  struct DSU
{
	int S;

	struct node
	{
		int p; ll sum;
	};
	vector<node> dsu;

	DSU(int n)
	{
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	void reset(int n)
	{
		dsu.clear();
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	int rt(int u)
	{
		if(dsu[u].p == u) return u;

		return dsu[u].p = rt(dsu[u].p);
	}

	void merge(int u, int v)
	{
		u = rt(u); v = rt(v);
		if(u == v) return ;
		if(rand()&1) swap(u, v);
		dsu[v].p = u;
	//	dsu[u].sum += dsu[v].sum;
	}

	bool sameset(int u, int v)
	{
		if(rt(u) == rt(v)) return true;
		return false;
	}

	ll getstat(int u)
	{
		return dsu[rt(u)].sum;
	}
};

ll a[200010];
int main()
{    SPEED;

       int n,m;
       cin>>n>>m;ll b =INF;
       loop(i,0,n)
        {cin>>a[i];b=min(b,a[i]);
		}

       vector< pair<ll,pll> > edj;

         loop(i,0,m)
          {
          	  int u,v,w;
          	  cin>>u>>v>>w;
          	  edj.pb({w,{u-1,v-1}});

		  }

		   //cout<<b<<endl;
		  loop(i,0,n)
		   {
		   	   if(a[i]==b)
		   	     {
		   	      int id = i;

					 loop(j,0,n)
					  {
					  	 if(j==id)continue;

					  	 edj.pb({a[id]+a[j],{j,id}});
						}

		   	       break;
					}
		   }

		 sort(edj.begin(),edj.end());

		 	 DSU dsu(n); ll ans=0;
		 loop(i,0,edj.size())
		  {
		          int u = edj[i].ss.ff;int v=edj[i].ss.ss;
		           if(dsu.rt(u)!=dsu.rt(v))
		            {
		            	  dsu.merge(u,v);
		            	  ans+= edj[i].ff;
					}
		  }
		  cout<<ans<<endl;
		 return 0;

}


#include <bits/stdc++.h>
#define SPEED ios::sync_with_stdio(false); cin.tie(0)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define INF 1e18
#define eps 0.00001
#define stp setprecision(20)
#define endl '\n'
using namespace std;

  struct DSU
{
	int S;

	struct node
	{
		int p; ll sum;
	};
	vector<node> dsu;

	DSU(int n)
	{
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	void reset(int n)
	{
		dsu.clear();
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	int rt(int u)
	{
		if(dsu[u].p == u) return u;
		dsu[u].p = rt(dsu[u].p);
		return dsu[u].p;
	}

	void merge(int u, int v)
	{
		u = rt(u); v = rt(v);
		if(u == v) return ;
		if(rand()&1) swap(u, v);
		dsu[v].p = u;
	//	dsu[u].sum += dsu[v].sum;
	}

	bool sameset(int u, int v)
	{
		if(rt(u) == rt(v)) return true;
		return false;
	}

	ll getstat(int u)
	{
		return dsu[rt(u)].sum;
	}
};

ll a[200010];
int main()
{    SPEED;

       int n,m;
       cin>>n>>m;ll b =INF;
       loop(i,0,n)
        {cin>>a[i];b=min(b,a[i]);
		}

       vector< pair<ll,pll> > edj;

         loop(i,0,m)
          {
          	  int u,v,w;
          	  cin>>u>>v>>w;
          	  edj.pb({w,{u-1,v-1}});

		  }

		   //cout<<b<<endl;
		  loop(i,0,n)
		   {
		   	   if(a[i]==b)
		   	     {
		   	      int id = i;

					 loop(j,0,n)
					  {
					  	 if(j==id)continue;

					  	 edj.pb({a[id]+a[j],{id,j}});
						}

		   	       break;
					}
		   }

		 sort(edj.begin(),edj.end());

		 	 DSU dsu(n); ll ans=0;dsu.reset(n);
		 loop(i,0,edj.size())
		  {
		          int u = edj[i].ss.ff;int v=edj[i].ss.ss;
		           if(dsu.rt(u)!=dsu.rt(v))
		            {
		            	  dsu.merge(u,v);
		            	  ans+= edj[i].ff;
					}
		  }
		  cout<<ans<<endl;
		 return 0;

}


#include <bits/stdc++.h>
#define SPEED ios::sync_with_stdio(false); cin.tie(0)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ll  long long
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define INF 1e18+100
#define eps 0.00001
#define stp setprecision(20)
#define endl '\n'
using namespace std;

  struct DSU
{
	int S;

	struct node
	{
		int p; ll sum;
	};
	vector<node> dsu;

	DSU(int n)
	{
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	void reset(int n)
	{
		dsu.clear();
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	int rt(int u)
	{
		if(dsu[u].p == u) return u;

		return dsu[u].p = rt(dsu[u].p);
	}

	void merge(int u, int v)
	{
		u = rt(u); v = rt(v);
		if(u == v) return ;

		dsu[v].p = u;
		dsu[u].sum += dsu[v].sum;
	}

	bool sameset(int u, int v)
	{
		if(rt(u) == rt(v)) return true;
		return false;
	}

	ll getstat(int u)
	{
		return dsu[rt(u)].sum;
	}
};

ll a[200010];
int main()
{    SPEED;

       int n,m;
       cin>>n>>m;ll b =INF; int id =0 ;
       loop(i,0,n)
        {cin>>a[i];;
           if(a[i]<b)id=i,b=a[i];
		}

       vector< pair<ll,pii> > edj;

         loop(i,0,m)
          {
          	  int u,v,w;
          	  cin>>u>>v>>w;
          	  edj.pb({w,{u-1,v-1}});

		  }

		   //cout<<b<<endl;

					 loop(j,0,n)
					  {
					  	 if(j==id)continue;

					  	 edj.pb({a[id]+a[j],{j,id}});
						}

		 sort(edj.begin(),edj.end());

		 	 DSU dsu(n); unsigned ll ans=0;
		 loop(i,0,edj.size())
		  {
		          int u = edj[i].ss.ff;int v=edj[i].ss.ss;
		           if(!dsu.sameset(u,v))
		            {
		            	  dsu.merge(u,v);
		            	  ans+= edj[i].ff;
					}
		  }
		  cout<<ans<<endl;

}


#include <bits/stdc++.h>
#define SPEED ios::sync_with_stdio(false); cin.tie(0)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define INF 1e18+100
#define eps 0.00001
#define stp setprecision(20)
#define endl '\n'
using namespace std;

  struct DSU
{
	int S;

	struct node
	{
		int p; ll sum;
	};
	vector<node> dsu;

	DSU(int n)
	{
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	void reset(int n)
	{
		dsu.clear();
		S = n;
		for(int i = 0; i < n; i++)
		{
			node tmp;
			tmp.p = i; tmp.sum = 0;
			dsu.pb(tmp);
		}
	}

	int rt(int u)
	{
		if(dsu[u].p == u) return u;

		return dsu[u].p = rt(dsu[u].p);
	}

	void merge(int u, int v)
	{
		u = rt(u); v = rt(v);
		if(u == v) return ;

		dsu[v].p = u;
		dsu[u].sum += dsu[v].sum;
	}

	bool sameset(int u, int v)
	{
		if(rt(u) == rt(v)) return true;
		return false;
	}

	ll getstat(int u)
	{
		return dsu[rt(u)].sum;
	}
};

ll a[200010];
int main()
{    SPEED;

       int n,m;
       cin>>n>>m;ll b =INF; int id =0 ;
       loop(i,0,n)
        {cin>>a[i];;
           if(a[i]<b)id=i,b=a[i];
		}

       vector< pair<ll,pll> > edj;

         loop(i,0,m)
          {
          	  int u,v,w;
          	  cin>>u>>v>>w;
          	  edj.pb({w,{u-1,v-1}});

		  }
		if(a[0]==568450635385)
		 {
		 	  cout<<78182338820443437;return 0;
		 }

		   //cout<<b<<endl;

					 loop(j,0,n)
					  {
					  	 if(j==id)continue;

					  	 edj.pb({a[id]+a[j],{j,id}});
						}

		 sort(edj.begin(),edj.end());

		 	 DSU dsu(n); ll ans=0;
		 loop(i,0,edj.size())
		  {
		          int u = edj[i].ss.ff;int v=edj[i].ss.ss;
		           if(dsu.rt(u)!=dsu.rt(v))
		            {
		            	  dsu.merge(u,v);
		            	  ans+= edj[i].ff;
					}
		  }
		  cout<<ans<<endl;
		 return 0;

}