// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 20005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)   cin >> a[i];
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          cin >> x >> y >> w;
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = *min_element(a+1,a+n+1);
     for(ll i=1;i<=n;i++)
     {
          if(a[i]==mini)   {id = i;break;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
     FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 20005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     int n,m;
     scanf("%d %d",&n,&m);
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)    scanf("%lld",&a[i]);
     vector<pair<ll,pair<int,int>>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          scanf("%lld %d %d",&x,&y,&w);
          edges.pb(mp(w,mp(x,y)));
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb(mp(a[id]+a[i],mp(id,i)));
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
   //  FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 200005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)   cin >> a[i];
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          cin >> x >> y >> w;
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
     FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 20005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)    scanf("%lld",&a[i]);
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          scanf("%lld %lld %lld",&x,&y,&w);
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
   //  FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 20005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)    scanf("%lld",&a[i]);
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          scanf("%lld %lld %lld",&x,&y,&w);
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
   //  FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 200005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)   cin >> a[i];
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          cin >> x >> y >> w;
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = *min_element(a+1,a+n+1);
     for(ll i=1;i<=n;i++)
     {
          if(a[i]==mini)   {id = i;break;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
     FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 200005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     int n,m;
     scanf("%d %d",&n,&m);
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)    scanf("%lld",&a[i]);
     vector<pair<ll,pair<int,int>>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          scanf("%lld %d %d",&x,&y,&w);
          edges.pb(mp(w,mp(x,y)));
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb(mp(a[id]+a[i],mp(id,i)));
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
   //  FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 20005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)   cin >> a[i];
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          cin >> x >> y >> w;
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
     FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 200005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)    scanf("%lld",&a[i]);
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          scanf("%lld %lld %lld",&x,&y,&w);
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
   //  FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>

#define mp make_pair
#define bp __builtin_popcountll
#define MOD  1000000007
const int N = 20005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

struct DSU
{
    ll parent[N],connected[N];
    void intialize(int x)
    {
        for(ll i=1;i<x;i++)
        {
            parent[i]=i;
            connected[i]=1;
        }
    }
    ll getparent(ll k)
    {
        while(k!=parent[k])
        {
            parent[k]=parent[parent[k]];
            k=parent[k];
        }
        return k;
    }
    ll getsize(ll k)
    {
        return connected[getparent(k)];
    }
    bool unionn(ll u,ll v)
    {
        ll parent1=getparent(u),parent2=getparent(v);
        if(parent1==parent2)    return 1;
        if(connected[parent1]>connected[parent2])  swap(parent1,parent2);
        connected[parent2]+=connected[parent1];
        connected[parent1]=0;
        parent[parent1]=parent[parent2];
        return 0;
    }
};

void solve()
{
     ll n,m;
     cin >> n >> m;
     DSU dsu;
     dsu.intialize(n+2);
     ll a[n+1];
     for(ll i=1;i<=n;i++)   cin >> a[i];
     vector<pair<ll,pll>> edges;
     for(ll i=0,x,y,w;i<m;i++)
     {
          cin >> x >> y >> w;
          edges.pb({w,{x,y}});
     }
     ll ans = 0;
     ll id = -1,mini = LLONG_MAX;
     for(ll i=1;i<=n;i++)
     {
          if(a[i]<mini)   {mini = a[i];id = i;break;}
     }
     for(ll i=1;i<=n;i++)
     {
          if(i!=id)
          {
               edges.pb({a[id]+a[i],{id,i}});
          }
     }
     sort(all(edges));
     for(auto X:edges)
     {
          bool chk = dsu.unionn(X.ss.ff,X.ss.ss);
          if(chk)  continue;
          ans += X.ff;
     }
     cout<<ans;
}

int main()
{
     FAST;
     int t = 1;
     //cin >> t;
     while(t--)
     {
          solve();
     }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//check tab

const int N = 2 * 1e5 + 5;

vector<pair<int, ll>> g[N];
int n, m;
ll a[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		int v, u; ll w;
		cin >> v >> u >> w;
		v--; u--;
		g[v].push_back({u, w});
		g[u].push_back({v, w});
	}

	vector<bool> done(n, false);
	priority_queue<pair<ll, int>> q;

	int st = min_element(a, a + n) - a;
	q.push({0, st});
	for (int i = 0; i < n; i++) {
		q.push({-(a[i] + a[st]), i});
	}

	ll ans = 0;

	while (!q.empty()) {
		ll d = -q.top().first;
		int v = q.top().second;
		q.pop();

		if (done[v]) {
			continue;
		}
		done[v] = true;
		ans += d;

		for (pair<int, ll> to: g[v]) {
			if (!done[to.first]) {
				q.push({-to.second, to.first});
			}
		}
	}

	cout << ans;
	return 0;
}


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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << "CXC";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//check tab 123

const int N = 2 * 1e5 + 5;

vector<pair<int, ll>> g[N];
int n, m;
ll a[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		int v, u; ll w;
		cin >> v >> u >> w;
		v--; u--;
		g[v].push_back({u, w});
		g[u].push_back({v, w});
	}

	vector<bool> done(n, false);
	priority_queue<pair<ll, int>> q;

	int st = min_element(a, a + n) - a;
	q.push({0, st});
	for (int i = 0; i < n; i++) {
		q.push({-(a[i] + a[st]), i});
	}

	ll ans = 0;

	while (!q.empty()) {
		ll d = -q.top().first;
		int v = q.top().second;
		q.pop();

		if (done[v]) {
			continue;
		}
		done[v] = true;
		ans += d;

		for (pair<int, ll> to: g[v]) {
			if (!done[to.first]) {
				q.push({-to.second, to.first});
			}
		}
	}

	cout << ans;
	return 0;
}


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

const int N = 2 * 1e5 + 5;

vector<pair<int, ll>> g[N];
int n, m;
ll a[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		int v, u; ll w;
		cin >> v >> u >> w;
		v--; u--;
		g[v].push_back({u, w});
		g[u].push_back({v, w});
	}

	vector<bool> done(n, false);
	priority_queue<pair<ll, int>> q;

	int st = min_element(a, a + n) - a;
	q.push({0, st});
	for (int i = 0; i < n; i++) {
		q.push({-(a[i] + a[st]), i});
	}

	ll ans = 0;

	while (!q.empty()) {
		ll d = -q.top().first;
		int v = q.top().second;
		q.pop();

		if (done[v]) {
			continue;
		}
		done[v] = true;
		ans += d;

		for (pair<int, int> to: g[st]) {
			if (!done[to.first]) {
				q.push({-to.second, to.first});
			}
		}
	}

	cout << ans;
    return 0;
}


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

const int N = 2 * 1e5 + 5;

vector<pair<int, ll>> g[N];
int n, m;
ll a[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		int v, u; ll w;
		cin >> v >> u >> w;
		v--; u--;
		g[v].push_back({u, w});
		g[u].push_back({v, w});
	}

	vector<bool> done(n, false);
	priority_queue<pair<ll, int>> q;

	int st = min_element(a, a + n) - a;
	q.push({0, st});
	for (int i = 0; i < n; i++) {
		q.push({-(a[i] + a[st]), i});
	}

	ll ans = 0;

	while (!q.empty()) {
		ll d = -q.top().first;
		int v = q.top().second;
		q.pop();

		if (done[v]) {
			continue;
		}
		done[v] = true;
		ans += d;

		for (pair<int, ll> to: g[v]) {
			if (!done[to.first]) {
				q.push({-to.second, to.first});
			}
		}
	}

	cout << ans;
	return 0;
}


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

const int N = 2 * 1e5 + 5;

vector<pair<int, ll>> g[N];
int n, m;
ll a[N];

int main() {
    ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		int v, u; ll w;
		cin >> v >> u >> w;
		v--; u--;
		g[v].push_back({u, w});
		g[u].push_back({v, w});
	}

	vector<bool> done(n, false);
	priority_queue<pair<ll, int>> q;

	int st = min_element(a, a + n) - a;
	q.push({0, st});
	for (int i = 0; i < n; i++) {
		q.push({-(a[i] + a[st]), i});
	}

	ll ans = 0;

	while (!q.empty()) {
		ll d = -q.top().first;
		int v = q.top().second;
		q.pop();

		if (done[v]) {
			continue;
		}
		done[v] = true;
		ans += d;

		for (pair<int, ll> to: g[v]) {
			if (!done[to.first]) {
				q.push({-to.second, to.first});
			}
		}
	}

	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055], ans = 0;
int p[200055];

int parent(int x) {

  return (p[x] == x ? x : (p[x] = parent(p[x])));

}

bool merge(int x, int y) {

  x = parent(x);
  y = parent(y);
  if (x == y)return false;
  p[x] = y;
  return true;

}

vector<pair<long long int,pair<int, int>>>v;

int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back({z, {x, y}});
  }
  z = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != z) {
      v.push_back({a[i] + a[z], {i, z}});
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055], ans = 0;
int p[200055];
int parent(int x) {
  return p[x] == x ? x : (p[x] = parent(p[x]));
}
bool merge(int x, int y) {
  x = parent(x);
  y = parent(y);
  if (x == y)return false;
  p[x] = y;
  return true;
}
vector<pair<long long int,pair<int, int>>>v;
int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back({z, {x, y}});
  }
  z = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != z) {
      v.push_back({a[i] + a[z], {i, z}});
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055];
ll p[200055];

int get_leader(int x)
{
	return (x == p[x] ? x : (p[x] = get_leader(p[x])));
}

bool merge(int x, int y)
{
	x = get_leader(x);
	y = get_leader(y);
	if(x == y) return false;
	p[x] = y;
	return true;
}

vector<pair<ll, pair<int, int> >>v;

int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back(make_pair(z, make_pair(x, y)));
  }
  int mm = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != mm) {
      v.push_back(make_pair(a[i] + a[mm], make_pair(i, mm)));
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  ll ans = 0;
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055];
ll p[200055];

int parent(int x) {

  return (x == p[x] ? x : (p[x] = parent(p[x])));

}

bool merge(int x, int y) {

  x = parent(x);
  y = parent(y);
  if (x == y)return false;
  p[x] = y;
  return true;

}

vector<pair<ll, pair<int, int> >>v;

int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back(make_pair(z, make_pair(x, y)));
  }
  int mm = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != mm) {
      v.push_back(make_pair(a[i] + a[mm], make_pair(i, mm)));
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  ll ans = 0;
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055], ans = 0;
int p[200055];

int parent(int x) {

  return (x == p[x] ? x : (p[x] = parent(p[x])));

}

bool merge(int x, int y) {

  x = parent(x);
  y = parent(y);
  if (x == y)return false;
  p[x] = y;
  return true;

}

vector<pair<long long int, pair<int, int> >>v;

int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back(make_pair(z, make_pair(x, y)));
  }
  int mm = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != mm) {
      v.push_back(make_pair(a[i] + a[mm], make_pair(i, mm)));
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair

typedef pair<long long, pair<int, int> > edge;

const int N = 200043;

int p[N];
long long a[N];

int get_leader(int x)
{
	return (x == p[x] ? x : (p[x] = get_leader(p[x])));
}

bool merge(int x, int y)
{
	x = get_leader(x);
	y = get_leader(y);
	if(x == y) return false;
	p[x] = y;
	return true;
}

int main()
{
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++)
		scanf("%lld", &a[i]);
	vector<edge> es(m);
	for(int i = 0; i < m; i++)
	{
		scanf("%d %d %lld", &es[i].y.x, &es[i].y.y, &es[i].x);
		es[i].y.x--;
		es[i].y.y--;
	}
	int z = min_element(a, a + n) - a;
	for(int i = 0; i < n; i++)
		if(i != z)
			es.push_back(mp(a[i] + a[z], mp(i, z)));
	sort(es.begin(), es.end());
	long long ans = 0;
	for(int i = 0; i < n; i++)
		p[i] = i;
	for(auto e : es)
		if(merge(e.y.x, e.y.y))
			ans += e.x;
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200005], ans = 0;
int p[200005];
int parent(int x) {
  if (p[x] != x) {
    return p[x] = parent(p[x]);
  }
  return x;
}
bool merge(int x, int y) {
  x = parent(x);
  y = parent(y);
  if (x == y)return false;
  p[x] = y;
  return true;
}
vector<pair<long long int,pair<int, int>>>v;
int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back({z, {x, y}});
  }
  z = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != z) {
      v.push_back({a[i] + a[z], {i, z}});
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055];
int p[200055];

int parent(int x) {

  return (x == p[x] ? x : (p[x] = parent(p[x])));

}

bool merge(int x, int y) {

  x = parent(x);
  y = parent(y);
  if (x == y)return false;
  p[x] = y;
  return true;

}

vector<pair<ll, pair<int, int> >>v;

int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    v.push_back(make_pair(z, make_pair(x, y)));
  }
  int mm = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != mm) {
      v.push_back(make_pair(a[i] + a[mm], make_pair(i, mm)));
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  ll ans = 0;
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FASTIO std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll a[200055];
ll p[200055];

int get_leader(int x)
{
	return (x == p[x] ? x : (p[x] = get_leader(p[x])));
}

bool merge(int x, int y)
{
	x = get_leader(x);
	y = get_leader(y);
	if(x == y) return false;
	p[x] = y;
	return true;
}

vector<pair<ll, pair<int, int> >>v;

int main () {
  FASTIO
  int n, m, x, y, z;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  v.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].second.first >> v[i].second.second >> v[i].first;
    v[i].second.first--;
    v[i].second.second--;
  }
  int mm = min_element(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i != mm) {
      v.push_back(make_pair(a[i] + a[mm], make_pair(i, mm)));
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  ll ans = 0;
  for (auto it : v) {
    if (merge(it.second.first, it.second.second)) {
      ans += it.first;
    }
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0;
long long parent[200005];
typedef pair<long long,pair<long,long> > pr;
priority_queue<pr,vector<pr>,greater<pr> > pq;
long find(long i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(long i,long j){
	long iset=find(i);
	long jset=find(j);
	parent[iset]=jset;
}
int main(){
	scanf("%ld%ld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%ld%ld%lld",&u,&v,&w);
		if(w<a[u]+a[v]){
			pq.push(make_pair(w,make_pair(u,v)));
		}
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			pq.push(make_pair(a[i]+nho,make_pair(i,luu)));
		}
	}
	while(pq.size()>0){
		if(find(pq.top().se.fi)!=find(pq.top().se.se)){
			mn=mn+pq.top().fi;
			uni(pq.top().se.fi,pq.top().se.se);
		}
		pq.pop();
	}
	printf("%lld",mn);
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0;
long long parent[200005];
typedef pair<long long,pair<long,long> > pr;
vector<pr> dm;
int find(int i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(int i,int j){
	int iset=find(i);
	int jset=find(j);
	parent[iset]=jset;
}
int main(){
	scanf("%ld%ld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%ld%ld%lld",&u,&v,&w);
		dm.push_back(make_pair(w,make_pair(u,v)));
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			dm.push_back(make_pair(a[i]+nho,make_pair(i,luu)));
		}
	}
	sort(dm.begin(),dm.end());
	for(int i=0;i<dm.size();i++){
		if(find(dm[i].se.fi)!=find(dm[i].se.se)){
			mn=mn+dm[i].fi;
			uni(dm[i].se.fi,dm[i].se.se);
		}
	}
	printf("%lld",mn);
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0;
long long parent[200005];
typedef pair<long long,pair<long,long> > pr;
priority_queue<pr,vector<pr>,greater<pr> > pq;
long find(long i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(long i,long j){
	long iset=find(i);
	long jset=find(j);
	parent[iset]=jset;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%ld %ld %lld",&u,&v,&w);
		if(w<a[u]+a[v]){
			pq.push(make_pair(w,make_pair(u,v)));
		}
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			pq.push(make_pair(a[i]+nho,make_pair(i,luu)));
		}
	}
	while(pq.size()>0){
		if(find(pq.top().se.fi)!=find(pq.top().se.se)){
			mn=mn+pq.top().fi;
			uni(pq.top().se.fi,pq.top().se.se);
		}
		pq.pop();
	}
	cout<<mn;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
struct edge{
	long u,v;
	long long w;
	bool operator <(const edge&dm){
		return w<dm.w;
	}
};
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0,cnt=0;
long long parent[200005];
edge dm[1000005];
int find(int i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(int i,int j){
	int iset=find(i);
	int jset=find(j);
	parent[iset]=jset;
}
int main(){
	scanf("%ld%ld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%ld%ld%lld",&u,&v,&w);
		if(w<a[u]+a[v]){
			cnt++;
			dm[cnt].u=u; dm[cnt].v=v; dm[cnt].w=w;
		}
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			cnt++;
			dm[cnt].u=i; dm[cnt].v=luu; dm[cnt].w=a[i]+nho;
		}
	}
	sort(dm+1,dm+1+cnt);
	for(int i=1;i<=cnt;i++){
		if(find(dm[i].u)!=find(dm[i].v)){
			mn=mn+dm[i].w;
			uni(dm[i].u,dm[i].v);
		}
	}
	printf("%lld",mn);
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0;
long long parent[200005];
typedef pair<long long,pair<long,long> > pr;
priority_queue<pr,vector<pr>,greater<pr> > pq;
long find(long i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(long i,long j){
	long iset=find(i);
	long jset=find(j);
	parent[iset]=jset;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%I64d",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%ld %ld %I64d",&u,&v,&w);
		if(w<a[u]+a[v]){
			pq.push(make_pair(w,make_pair(u,v)));
		}
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			pq.push(make_pair(a[i]+nho,make_pair(i,luu)));
		}
	}
	while(pq.size()>0){
		if(find(pq.top().se.fi)!=find(pq.top().se.se)){
			mn=mn+pq.top().fi;
			uni(pq.top().se.fi,pq.top().se.se);
		}
		pq.pop();
	}
	cout<<mn;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0;
long long parent[200005];
typedef pair<long long,pair<long,long> > pr;
priority_queue<pr,vector<pr>,greater<pr> > pq;
long find(long i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(long i,long j){
	long iset=find(i);
	long jset=find(j);
	parent[iset]=jset;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%lld %lld %lld",&u,&v,&w);
		if(w<a[u]+a[v]){
			pq.push(make_pair(w,make_pair(u,v)));
		}
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			pq.push(make_pair(a[i]+nho,make_pair(i,luu)));
		}
	}
	while(pq.size()>0){
		if(find(pq.top().se.fi)!=find(pq.top().se.se)){
			mn=mn+pq.top().fi;
			uni(pq.top().se.fi,pq.top().se.se);
		}
		pq.pop();
	}
	cout<<mn;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
long long n,m,a[200005];
long long u,v,w,nho=1e18,luu,mn=0;
long long parent[200005];
typedef pair<long long,pair<long,long> > pr;
vector<pr> dm;
long find(long i){
	if(parent[i]==i){
		return i;
	}else{
		return find(parent[i]);
	}
}
void uni(long i,long j){
	long iset=find(i);
	long jset=find(j);
	parent[iset]=jset;
}
int main(){
	scanf("%ld%ld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		parent[i]=i;
		if(a[i]<nho){
			nho=a[i];
			luu=i;
		}
	}
	for(int i=1;i<=m;i++){
		scanf("%ld%ld%lld",&u,&v,&w);
		if(w<a[u]+a[v]){
			dm.push_back(make_pair(w,make_pair(u,v)));
		}
	}
	for(int i=1;i<=n;i++){
		if(i!=luu){
			dm.push_back(make_pair(a[i]+nho,make_pair(i,luu)));
		}
	}
	sort(dm.begin(),dm.end());
	for(int i=0;i<dm.size();i++){
		if(find(dm[i].se.fi)!=find(dm[i].se.se)){
			mn=mn+dm[i].fi;
			uni(dm[i].se.fi,dm[i].se.se);
		}
	}
	printf("%lld",mn);
}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

#define N 100000

int main() {
  int n, a, b, mx = 0, my = 0,i;
  pair<int, int> s[N];
  scanf("%d", &n);
  priority_queue<int, vector<int>, greater<int>> q[2*N+1];
  set<pair<int,int>> sets;
  for(i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    mx = max(mx, a);
    my = max(my, b);
    q[b-a+N].push(a);
  }
  vector<int> r(mx+1), c(my+1);
  for(i = 0; i < n; i++) {
    scanf("%d", &a);
    if(q[a+N].empty()) {
      puts("NO");
      return 0;
    }
    b = q[a+N].top();
    q[a+N].pop();
    s[i] = {b, b+a};
    int d = b+a;
    if(r[b]==d && c[d]==b) {
      r[b]++;
      c[d]++;
    } else{
        puts("NO");
        return 0;
    }
  }
  /*vector<int> r(mx+1), c(my+1);
  for(i = 0; i < n; i++) {
    a = s[i].first, b = s[i].second;
    if(r[a]==b && c[b]==a) {
      r[a]++;
      c[b]++;
    } else {
      puts("NO");
      return 0;
    }
  }*/
  puts("YES");
  for(i = 0; i < n; i++)
    printf("%d %d\n", s[i].first, s[i].second);
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    int maxx;
    int maxy;
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	maxx = max (maxx, a[i]);
    	maxy = max(maxy, b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    bool check[maxx][maxy];
    for(int i = 0; i < maxx; i++) {
    	for(int j = 0; j < maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(!check[ans[1].first][ans[i].second-1] && ans[i].second!=0) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[1].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    int maxx = ans[0].first;
    int maxy = ans[0].second;
    for(int i = 1; i < n; i++) {
    	if(maxx > ans[i].first && maxy > ans[i].second) {
    		cout<<"NO\n";
    		return 0;
    	}
    	//else {
    		maxx = max(maxx, ans[i].first);
    		maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#define N 100000
int n,a,b,d,x,y,i,o[N],p[N];using namespace std;int main(){scanf("%d",&n);priority_queue<int,vector<int>,greater<int>>q[2*N+1];for(i=0;i<n;i++){scanf("%d %d",&a,&b);x=max(x,a);y=max(y,b);q[b-a+N].push(a);}vector<int>r(x+1),c(y+1);for(i=0;i<n;i++){scanf("%d",&a);if(q[a+N].empty()){puts("NO");return 0;}b=q[a+N].top();q[a+N].pop();d=b+a;o[i]=b;p[i]=d;if(r[b]==d&&c[d]==b){r[b]++;c[d]++;}else{puts("NO");return 0;}}puts("YES");for(i=0;i<n;i++)printf("%d %d\n",o[i],p[i]);}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    int maxx = 0;
    int maxy = 0;
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	maxx = max (maxx, a[i]);
    	maxy = max(maxy, b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    bool check[maxx+1][maxy+1];
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    bool check[maxx+1][maxy+1];
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    vector<vector<bool>> check(maxx+1,vector<bool>(maxy+1));
    if(n==100000) {
    	cout<<"seomt"<<endl;
    }
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	printf("%d %d\n", ans[i].first, ans[i].second);
    	//cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    int maxx = ans[0].first;
    int maxy = ans[0].second;
    for(int i = 1; i < n; i++) {
    	if(maxx > ans[i].first && maxy > ans[i].second) {
    		cout<<"NO\n";
    		return 0;
    	}
    	else {
    		maxx = max(maxx, ans[i].first);
    		maxy = max(maxy, ans[i].second);
    	}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

int main() {
  int n, a, b, c, maxx = 0, maxy = 0;
  scanf("%d", &n);
  pair<int, int> ans[100000];
  priority_queue<int, vector<int>, greater<int>> q[2*100000+1];
  set<pair<int,int>> sets;
  for(int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    maxx = max(maxx, a);
    maxy = max(maxy, b);
    q[b-a+n].push(a);
  }
  for(int i = 0; i < n; i++) {
    scanf("%d", &a);
    if(q[a+n].empty()) {
      puts("NO");
      return 0;
    }
    int tmp = q[a+n].top();
    q[a+n].pop();
    ans[i] = {tmp, tmp+a};
  }
  int row[maxx+1], col[maxy+1];
  for(int i = 0; i < n; i++) {
    a = ans[i].first, b = ans[i].second;
    if(row[a]==b && col[b]==a) {
      row[a]++;
      col[b]++;
    } else {
      puts("NO");
      return 0;
    }
  }
  puts("YES");
  for(int i = 0; i < n; i++)
  printf("%d %d\n", ans[i].first, ans[i].second);
}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

#define N 100000

int main() {
  int n, a, b, mx = 0, my = 0,i;
  scanf("%d", &n);
  priority_queue<int, vector<int>, greater<int>> q[2*N+1];
  set<pair<int,int>> sets;
  for(i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    mx = max(mx, a);
    my = max(my, b);
    q[b-a+N].push(a);
  }
  vector<int> r(mx+1), c(my+1);
  for(i = 0; i < n; i++) {
    scanf("%d", &a);
    if(q[a+N].empty()) {
      puts("NO");
      return 0;
    }
    b = q[a+N].top();
    q[a+N].pop();
    int d = b+a;
    if(r[b]==d && c[d]==b) {
      r[b]++;
      c[d]++;
    } else{
        puts("NO");
        return 0;
    }
  }
  puts("YES");
  for(i = 0; i < n; i++)
    printf("%d %d\n", s[i].first, s[i].second);
}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

#define N 100000

int main() {
  int n, a, b, mx = 0, my = 0,i;
  pair<int, int> s[N];
  scanf("%d", &n);
  priority_queue<int, vector<int>, greater<int>> q[2*N+1];
  for(i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    mx = max(mx, a);
    my = max(my, b);
    q[b-a+N].push(a);
  }
  vector<int> r(mx+1), c(my+1);
  for(i = 0; i < n; i++) {
    scanf("%d", &a);
    if(q[a+N].empty()) {
      puts("NO");
      return 0;
    }
    b = q[a+N].top();
    q[a+N].pop();
    s[i] = {b, b+a};
    int d = b+a;
    if(r[b]==d && c[d]==b) {
      r[b]++;
      c[d]++;
    } else {
        puts("NO");
        return 0;
    }
  }
  puts("YES");
  for(i = 0; i < n; i++)
    printf("%d %d\n", s[i].first, s[i].second);
}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

#define N 100000

int main() {
  int n, a, b, c, maxx = 0, maxy = 0;
  pair<int, int> ans[N];
  scanf("%d", &n);
  priority_queue<int, vector<int>, greater<int>> q[2*N+1];
  set<pair<int,int>> sets;
  for(int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    maxx = max(maxx, a);
    maxy = max(maxy, b);
    q[b-a+N].push(a);
  }
  for(int i = 0; i < n; i++) {
    scanf("%d", &c);
    if(q[c+N].empty()) {
      puts("NO");
      return 0;
    }
    int tmp = q[c+N].top();
    q[c+N].pop();
    ans[i] = {tmp, tmp+c};
  }
  vector<int> row(maxx), col(maxy);
  for(int i = 0; i < n; i++) {
    int d = ans[i].first, e = ans[i].second;
    if(row[d]==e && col[e]==d) {
      row[d]++;
      col[e]++;
    } else {
      puts("NO");
      return 0;
    }
  }
  puts("YES");
  for(int i = 0; i < n; i++)
  printf("%d %d\n", ans[i].first, ans[i].second);
}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    vector<vector<bool>> check(maxx+1,vector<bool>(maxy+1));
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    if(n==100000) {
    	cout<<"seomt"<<endl;
    	return 0;
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    if(n==100000) {
    	cout<<"seomt"<<endl;
    	return 0;
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	printf("%d %d\n", ans[i].first, ans[i].second);
    	//cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    int maxx;
    int maxy;
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	maxx = max (maxx, a[i]);
    	maxy = max(maxy, b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    bool check[maxx+1][maxy+1];
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[1].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[1].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <set>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    set<pair<int,int> > sets;
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    for(int i = 0; i < n; i++) {
    	//sets.count
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && sets.count(make_pair(ans[i].first, ans[i].second-1)) == 0) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(sets.count(make_pair(ans[i].first-1, ans[i].second)) == 0) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(sets.count(make_pair(ans[i].first, ans[i].second-1)) == 0 || sets.count(make_pair(ans[i].first-1, ans[i].second)) == 0) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	sets.insert(make_pair(ans[i].first, ans[i].second));
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    if(n==100000) {
    	cout<<"seomt"<<endl;
    	return 0;
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	printf("%d %d\n", ans[i].first, ans[i].second);
    	//cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    vector<vector<bool>> check(maxx+1,vector<bool>(maxy+1));
    if(n==100000) {
    	cout<<"seomt"<<endl;
    	return 0;
    }
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	printf("%d %d\n", ans[i].first, ans[i].second);
    	//cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    int maxx = 0;
    int maxy = 0;
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	maxx = max (maxx, a[i]);
    	maxy = max(maxy, b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    bool check[maxx+1][maxy+1];
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[1].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[1].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    vector<vector<bool>> check(maxx+1,vector<bool>(maxy+1));
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	printf("%d %d\n", ans[i].first, ans[i].second);
    	//cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    int maxx = ans[0].first;
    int maxy = ans[0].second;
    for(int i = 1; i < n; i++) {
    	if(maxx >= ans[i].first && maxy >= ans[i].second) {
    		cout<<"NO\n";
    		return 0;
    	}
    	//else {
    		maxx = max(maxx, ans[i].first);
    		maxy = max(maxy, ans[i].second);
    	//}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[n],b[n],c[n];
    vector<pair<int, int> > ans;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a[i], &b[i]);
    	q2[b[i]-a[i]+100000].push(a[i]);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c[i]);
    	if(q2[c[i]+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c[i]+100000].top();
    	q2[c[i]+100000].pop();
    	ans.push_back(make_pair(tmp, tmp+c[i]));
    }
    int minx = ans[0].first;
    int miny = ans[0].second;
    for(int i = 1; i < n; i++) {
    	if(ans[i].first > minx && ans[i].second > miny) {
    		minx = ans[i].first;
    		miny = ans[i].second;
    	}
    	else if(minx > ans[i].first && miny > ans[i].second) {
    		cout<<"NO\n";
    		return 0;
    	}
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <stdio.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int main() {
    int n,a, b, c;
    pair<int, int> ans[100001];
    cin>>n;
    int maxx = 0;
    int maxy = 0;
    priority_queue<int, std::vector<int>, std::greater<int> > q2[200001];
    for(int i = 0; i < n; i++) {
    	scanf("%d %d", &a, &b);
    	maxx = max (maxx, a);
    	maxy = max(maxy, b);
    	q2[b-a+100000].push(a);
    }
    for(int i = 0; i < n; i++) {
    	scanf("%d", &c);
    	if(q2[c+100000].empty()) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	int tmp = q2[c+100000].top();
    	q2[c+100000].pop();
    	ans[i] = make_pair(tmp, tmp+c);
    }
    vector<vector<bool>> check(maxx+1,vector<bool>(maxy+1));
    for(int i = 0; i <= maxx; i++) {
    	for(int j = 0; j <= maxy; j++) {
    		check[i][j] = false;
    	}
    }
    for(int i = 0; i < n; i++) {
    	if(ans[i].first == 0) {
    		if(ans[i].second!=0 && !check[ans[i].first][ans[i].second-1]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(ans[i].second == 0) {
    		if(!check[ans[i].first-1][ans[i].second]) {
    			cout<<"NO"<<endl;
    			return 0;
    		}
    	}
    	else if(!check[ans[i].first-1][ans[i].second] || !check[ans[i].first][ans[i].second-1]) {
    		cout<<"NO"<<endl;
    		return 0;
    	}
    	//else {
    	check[ans[i].first][ans[i].second] = true;
    	//check[ans[i].second] = true;
    		//maxx = max(maxx, ans[i].first);
    		//maxy = max(maxy, ans[i].second);
    	//}
    }
    if(n==100000) {
    	cout<<"seomt"<<endl;
    	return 0;
    }
    cout<<"YES\n";
    for(int i = 0; i < n; i++) {
    	printf("%d %d\n", ans[i].first, ans[i].second);
    	//cout<<ans[i].first<<" "<<ans[i].second<<endl;
    }

}


#include <cstdio>
#include <vector>
#include <set>
#include <queue>

using namespace std;

#define N 100000

int main() {
  int n, a, b, c, maxx = 0, maxy = 0;
  pair<int, int> ans[N];
  scanf("%d", &n);
  priority_queue<int, vector<int>, greater<int>> q[2*N+1];
  set<pair<int,int>> sets;
  for(int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    maxx = max(maxx, a);
    maxy = max(maxy, b);
    q[b-a+N].push(a);
  }
  for(int i = 0; i < n; i++) {
    scanf("%d", &c);
    if(q[c+N].empty()) {
      puts("NO");
      return 0;
    }
    int tmp = q[c+N].top();
    q[c+N].pop();
    ans[i] = {tmp, tmp+c};
  }
  vector<int> row(maxx+1), col(maxy+1);
  for(int i = 0; i < n; i++) {
    int d = ans[i].first, e = ans[i].second;
    if(row[d]==e && col[e]==d) {
      row[d]++;
      col[e]++;
    } else {
      puts("NO");
      return 0;
    }
  }
  puts("YES");
  for(int i = 0; i < n; i++)
  printf("%d %d\n", ans[i].first, ans[i].second);
}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  unordered_map<int, int> ptsDiff;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;

    ptsDiff[b-a] += 1;
  }

  vector<int> w = ReadSequence<int>();

  unordered_map<int, int> wCount;
  for (int x : w)
  {
    wCount[x] += 1;
  }

  for (auto& kv : ptsDiff)
  {
    int diff = kv.first;
    int count = kv.second;

    if (wCount.count(diff) != count)
    {
      cout << "NO" << endl;
      return 0;
    }
  }

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<int> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  // traverse diagonal, and check that the corner is the lowest idx
  for (int i = 0; i < min(dimX, dimY); ++i)
  {
    int cur = i * dimX + i;
    int x = INT_MIN;
    for (int j = i; j < dimX; ++j)
    {
      if (rect[cur+j*dimX] < x)
        return false;
      x = rect[cur+j*dimX];
    }

    for (int j = i; j < dimY; ++j)
    {
      if (rect[cur+j] < x)
        return false;
      x = rect[cur+j];
    }
  }

  return true;
}

unordered_map<int, deque<pair<int, int>>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(p);
    pts[i] = p;
  }

  vector<int> w = ReadSequence<int>();

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs)
    {
      if (lhs.first == rhs.first)
        return lhs.second < rhs.second;

      return lhs.first < rhs.first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<pair<int, int>> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    auto pt = it->second.front();
    it->second.pop_front();
    res.push_back(pt);

    rect[pt.second + dimX +pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << d.first << " " << d.second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

map<pair<int, int>, int> labels;

bool IsValid()
{
  for (auto& kv : labels)
  {
    pair<int, int> pt = kv.first;
    int label = kv.second;

    auto it0 = labels.find(make_pair(pt.first + 1, pt.second));
    if (it0 != labels.end() && it0->second < label)
      return false;

    auto it1 = labels.find(make_pair(pt.first, pt.second + 1));
    if (it1 != labels.end() && it1->second < label)
      return false;

  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  int dimX = 0;
  int dimY = 0;

  vector<pair<int, int>> pts(n);
  unordered_map<int, int> ptsDiff;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;

    ptsDiff[b-a] += 1;
  }

  vector<int> w = ReadSequence<int>();

  unordered_map<int, int> wCount;
  for (int x : w)
  {
    wCount[x] += 1;
  }

  for (auto& kv : ptsDiff)
  {
    int diff = kv.first;
    int count = kv.second;

    auto it = wCount.find(diff);
    if (it == wCount.end() || it->second != count)
    {
      cout << "NO" << endl;
      return 0;
    }
  }

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  vector<int> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    labels[make_pair(pt.first, pt.second)] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  unordered_map<int, int> ptsDiff;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;

    ptsDiff[b-a] += 1;
  }

  vector<int> w = ReadSequence<int>();

  unordered_map<int, int> wCount;
  for (int x : w)
  {
    wCount[x] += 1;
  }

  for (auto& kv : ptsDiff)
  {
    int diff = kv.first;
    int count = kv.second;

    auto it = wCount.find(diff);
    if (it == wCount.end() || it->second != count)
    {
      cout << "NO" << endl;
      return 0;
    }
  }

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  rect.resize(100100);
  Fill(&rect, INT_MAX);

  vector<int> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  cout << "0";

  int a, b;
  vector<pair<int, int>> pts(n);
  unordered_map<int, int> ptsDiff;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;

    ptsDiff[b-a] += 1;
  }

  cout << "0.5";

  vector<int> w = ReadSequence<int>();

  unordered_map<int, int> wCount;
  for (int x : w)
  {
    wCount[x] += 1;
  }

  for (auto& kv : ptsDiff)
  {
    int diff = kv.first;
    int count = kv.second;

    auto it = wCount.find(diff);
    if (it == wCount.end() || it->second != count)
    {
      cout << "NO" << endl;
      return 0;
    }
  }

  cout << "1";

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<int> res;

  cout << "2";
  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  // traverse diagonal, and check that the corner is the lowest idx
  for (int i = 0; i < dimY; ++i)
  {
    int cur = i * dimX;
    int x = rect[cur];
    for (int j = 0; j <= dimX; ++j)
    {
      if (rect[cur + j] < x)
        return false;
      x = rect[cur + j];
    }
  }

  for (int i = 0; i < dimX; ++i)
  {
    int cur = i;
    int x = rect[cur];
    for (int j = 0; j <= dimY; ++j)
    {
      if (rect[cur + j*dimX] < x)
        return false;
      x = rect[cur + j*dimX];
    }
  }

  return true;
}

unordered_map<int, deque<pair<int, int>>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(p);
    pts[i] = p;
  }

  vector<int> w = ReadSequence<int>();

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs)
    {
      if (lhs.first == rhs.first)
        return lhs.second < rhs.second;

      return lhs.first < rhs.first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<pair<int, int>> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    auto pt = it->second.front();
    it->second.pop_front();
    res.push_back(pt);

    rect[pt.second * dimX + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << d.first << " " << d.second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  if (n == 100000)
    cout << "0" << endl;

  int a, b;
  vector<pair<int, int>> pts(n);
  unordered_map<int, int> ptsDiff;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;

    ptsDiff[b-a] += 1;
  }

  if (n == 100000)
    cout << "0.5" << endl;

  vector<int> w = ReadSequence<int>();

  unordered_map<int, int> wCount;
  for (int x : w)
  {
    wCount[x] += 1;
  }

  for (auto& kv : ptsDiff)
  {
    int diff = kv.first;
    int count = kv.second;

    auto it = wCount.find(diff);
    if (it == wCount.end() || it->second != count)
    {
      cout << "NO" << endl;
      return 0;
    }
  }

  if (n == 100000)
    cout << "1" << endl;

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<int> res;

  if (n == 100000)
    cout << "2" << endl;
  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

void PrintRect()
{
  for (int i = 0; i <= dimY; ++i)
  {
    string row;
    for (int j = 0; j <= dimX; ++j)
    {
      int cur = rect[i*(dimX+1) + j];
      if (cur != INT_MAX)
      {
        char buf[256];
        sprintf(buf, "%.3d  ", cur);
        row += buf;
      }
      else
      {
        row += "     ";
      }
    }
    cout << row << endl;
  }

}

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<pair<int, int>>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(p);
    pts[i] = p;
  }

  vector<int> w = ReadSequence<int>();

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs)
    {
      if (lhs.first == rhs.first)
        return lhs.second < rhs.second;

      return lhs.first < rhs.first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<pair<int, int>> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    auto pt = it->second.front();
    it->second.pop_front();
    res.push_back(pt);
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    //PrintRect();
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << d.first << " " << d.second << endl;
  }

  //PrintRect();

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  // traverse diagonal, and check that the corner is the lowest idx
  for (int i = 0; i < min(dimX, dimY); ++i)
  {
    int cur = i * dimX + i;
    int x = INT_MIN;
    for (int j = i; j < dimX; ++j)
    {
      if (rect[cur+j*dimX] < x)
        return false;
      x = rect[cur+j*dimX];
    }

    for (int j = i; j < dimY; ++j)
    {
      if (rect[cur+j] < x)
        return false;
      x = rect[cur+j];
    }
  }

  return true;
}

unordered_map<int, deque<pair<int, int>>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  int dimX = 0, dimY = 0;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(p);
    pts[i] = p;
  }

  vector<int> w = ReadSequence<int>();

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs)
    {
      if (lhs.first == rhs.first)
        return lhs.second < rhs.second;

      return lhs.first < rhs.first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<pair<int, int>> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    auto pt = it->second.front();
    it->second.pop_front();
    res.push_back(pt);

    rect[pt.second + dimX +pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << d.first << " " << d.second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  if (n == 100000)
    cout << "0" << endl;

  int a, b;
  vector<pair<int, int>> pts(n);
  unordered_map<int, int> ptsDiff;
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;

    ptsDiff[b-a] += 1;
  }

  if (n == 100000)
    cout << "0.5" << endl;

  vector<int> w = ReadSequence<int>();

  unordered_map<int, int> wCount;
  for (int x : w)
  {
    wCount[x] += 1;
  }

  for (auto& kv : ptsDiff)
  {
    int diff = kv.first;
    int count = kv.second;

    auto it = wCount.find(diff);
    if (it == wCount.end() || it->second != count)
    {
      cout << "NO" << endl;
      return 0;
    }
  }

  if (n == 100000)
  {
    cout << "1" << " dimx: " << dimX << " dimY: " << dimY << endl;

  }

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<int> res;

  if (n == 100000)
    cout << "2" << endl;
  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <deque>
#include <algorithm>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadSequence()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
T Read()
{
  string line;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  stream >> tmp;
  return tmp;
}

template <typename T>
void Fill(vector<T>* r, T v = 0)
{
  for (size_t i = 0; i < r->size(); ++i)
    (*r)[i] = v;
}

vector<int> rect;
int dimX = 0;
int dimY = 0;

//void PrintRect()
//{
//  for (int i = 0; i <= dimY; ++i)
//  {
//    string row;
//    for (int j = 0; j <= dimX; ++j)
//    {
//      int cur = rect[i*(dimX+1) + j];
//      if (cur != INT_MAX)
//      {
//        char buf[256];
//        sprintf(buf, "%.3d  ", cur);
//        row += buf;
//      }
//      else
//      {
//        row += "     ";
//      }
//    }
//    cout << row << endl;
//  }
//
//}

bool IsValid()
{
  for (int i = 0; i < dimY; ++i)
  {
    for (int j = 0; j < dimX; ++j)
    {
      int cur = rect[i*(dimX+1)+j];
      if (cur > rect[i*(dimX+1)+j+1] || cur > rect[(i+1)*(dimX+1)+j] || cur > rect[(i+1)*(dimX+1)+j+1])
      {
        return false;
      }
    }
  }
  return true;
}

unordered_map<int, deque<int>> mm;

int main(int argc, char** argv)
{
  int n = Read<int>();

  int a, b;
  vector<pair<int, int>> pts(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> a; cin >> b;
    dimX = max(a, dimX);
    dimY = max(b, dimY);
    string tmp;
    getline(cin, tmp);
    auto p = make_pair(a, b);
    mm[b - a].push_back(i);
    pts[i] = p;
  }

  vector<int> w = ReadSequence<int>();

  for (auto& kv : mm)
  {
    auto& p = kv.second;
    sort(p.begin(), p.end(), [&](int lhs, int rhs)
    {
      if (pts[lhs].first == pts[rhs].first)
        return pts[lhs].second < pts[rhs].second;

      return pts[lhs].first < pts[rhs].first;
    });
  }

  rect.resize((1+dimX) * (1+dimY));
  Fill(&rect, INT_MAX);

  vector<int> res;

  for (int i = 0; i < (int)w.size(); ++i)
  {
    auto it = mm.find(w[i]);
    if (it == mm.end() || it->second.empty())
    {
      cout << "NO" << endl;
      return 0;
    }

    int idx = it->second.front();
    it->second.pop_front();
    res.push_back(idx);
    auto pt = pts[idx];
    rect[pt.second * (dimX+1) + pt.first] = i + 1;
  }

  if (!IsValid())
  {
    cout << "NO" << endl;
    return 0;
  }

  cout << "YES" << endl;
  for (auto& d : res)
  {
    cout << pts[d].first << " " << pts[d].second << endl;
  }

}