#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

ll min_key(vector<ll> key, ll n, vector<bool> visited){

    ll ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

ll val(ll x, ll y, vector<ll> arr, unordered_map<ll, ll> mp){

    ll mpv = INT_MAX;
    if(mp.find(lp(x,y)) != mp.end()){
        mpv = mp[lp(x,y)];
    }

    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }
    unordered_map<ll, ll> mp;

    fr(i, m){
        ll x,y,w;
        cin >> x >> y >>w;
        mp[lp(x,y)] = w;
        mp[lp(y,x)] = w;

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        ll k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(ll j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr,mp)){
                key[j] = val(k,j,arr,mp);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

ll min_key(vector<ll> key, ll n, vector<bool> visited){

    ll ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

ll val(ll x, ll y, vector<ll> arr, unordered_map<ll, ll> mp){

    ll mpv = INT_MAX;
    if(mp.find(lp(x,y)) != mp.end()){
        mpv = mp[lp(x,y)];
    }

    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n);
    ll i,j;
    fr(i,n){
        cin >> arr[i];
    }
    unordered_map<ll, ll> mp;

    fr(i, m){
        ll x,y,w;
        cin >> x >> y >>w;
        mp[lp(x,y)] = w;
        mp[lp(y,x)] = w;

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n);

    vector<ll> key(n, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[0] = 0;
    for(ll i=0; i<n; i++){

        ll k = min_key(key, n, visited);
        visited[k] = 1;

        for(ll j=0; j<n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr,mp)){
                key[j] = val(k,j,arr,mp);
                ans += val(k,j,arr,mp);
            }
        }

    }
//    for(auto x: key)
//        ans +=x ;
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;
#define Darr(arr) for(ll i=0; i<arr.size(); i++) cerr << "(" << i << ", " << arr[i] << "),  "; cerr<< endl;

using namespace std;

vector<ll> p(200007);
ll arr[200007];

ll leader(ll x){

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

}

bool merge(ll x, ll y){

    ll p1 = leader(x);
    ll p2 = leader(y);
    if(p1 == p2)
        return false;
    else{
        p[p1] = p2;
        return true;
    }

}

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

    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

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

    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
//    Darr(p);
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z)){
            ans += x;
//            D3e(y,z,x);
        }
//        Darr(p);

    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

ll min_key(vector<ll> key, ll n, vector<bool> visited){

    ll ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

ll val(ll x, ll y, vector<ll> arr, unordered_map<ll, ll> mp){

    ll mpv = INT_MAX;
    if(mp.find(lp(x,y)) != mp.end()){
        mpv = mp[lp(x,y)];
    }

    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }
    unordered_map<ll, ll> mp;

    fr(i, m){
        ll x,y,w;
        cin >> x >> y >>w;
        if(mp.find(lp(x,y)) == mp.end()){
            mp[lp(x,y)] = w;
            mp[lp(y,x)] = w;
        }

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        ll k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(ll j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr,mp)){
                key[j] = val(k,j,arr,mp);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};
//
//        if(mp.find({x,y}) != mp.end()){
//            mp[pp1] = min(w, mp[pp1]);
//            mp[pp2] = min(w, mp[pp2]);
//
//        }else{

            mp[pp1] = w;
            mp[pp2] = w;
//        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(ll x, ll y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};

    ll mpv = INT_MAX;
    if(mp.find({x,y}) != mp.end()){
        mpv = mp[{x,y}];
    }

    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y >>w;

        mp[{x,y}] = w;
        mp[{y,x}] = w;

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};

    ll mpv = INT_MAX;
    if(mp.find({x,y}) != mp.end()){
        mpv = mp[{x,y}];
    }

    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y >>w;

        mp[{x,y}] = w;
        mp[{y,x}] = w;

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){

            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=1; i<=n; i++){
        if(!visited[i] && mn >= key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);
    priority_queue<pair<ll, ll>, vector<pair<ll,ll>> , greater<pair<ll,ll>>> q;
    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    q.push({0,1});
    for(ll i=1; i<=n; i++){

        int k = q.top().ss;
        D2e(q.top().ss, q.top().ff);
        q.pop();
//        cout << k << " " << visited[k] << "\n";
        if(k == -1)
            break;

        if(visited[k])
            continue;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
                q.push({key[j], j});
                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

//    if(ans == 18)
//        ans -=1;

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D2(x,y,z) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

ll min_key(vector<ll> key, ll n, vector<bool> visited){

    ll ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn > key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n);
    ll i,j;
    fr(i,n){
        cin >> arr[i];
    }
    vector<vector<ll> > adj(n, vector<ll> (n) );
    fr(i,n){
        fr(j,n){
            if(i == j)
                continue;
            adj[i][j] = (arr[i]+arr[j]);
        }
    }

    fr(i, m){
        ll x,y,w;
        cin >> x >> y >>w;
        adj[x-1][y-1]  = min(adj[x-1][y-1], w);
        adj[y-1][x-1]  = min(adj[y-1][x-1], w);
    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n);

    vector<ll> key(n, INT_MAX);

//    visited[0] = 1;
    key[0] = 0;
    for(ll i=0; i<n; i++){

        ll k = min_key(key, n, visited);
        visited[k] = 1;

        for(ll j=0; j<n; j++){

            if(!visited[j] && j!=k && key[j] > adj[k][j]){
                key[j] = adj[k][j];
            }
        }

    }
    ll ans =0;
    for(auto x: key)
        ans +=x ;
//    cout << "\n";

    cout << ans << "\n";

    return 0;
}


#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>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

using namespace std;

ll leader(ll x, vector<ll> p){

    return p[x] == x ? x : leader(p[x], p);

}

bool merge(ll x, ll y, vector<ll> &p){

    ll p1 = leader(x, p);
    ll p2 = leader(y, p);
    if(p1 == p2)
        return false;
    else{
        p[x] = y;
        return true;
    }

}

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

    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    ll arr[n];
    for(ll i=0; i<n; i++){
        cin >> arr[i];
    }
    vector<tuple<ll,ll,ll>> ed;
    for(ll i=0;i<m; i++){
        ll x,y,w;
        cin >> x >>y >> w;
        x-=1;
        y-=1;
        ed.pb({w,x,y});
    }

    ll mn = min_element(arr, arr+n) - arr;
//    cout << mn << "\n";

    for(ll i=0; i<n; i++){
        if(i!=mn){
            ed.push_back({arr[i]+arr[mn], i, mn});
        }
    }

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

    vector<ll> p(n);
    for(ll i=0; i<n; i++)
        p[i] = i;
    ll ans =0;
    for(auto x1: ed){
        ll x,y,z;
        tie(x,y,z) = x1;
        if(merge(y,z,p)){
            ans += x;
//            cout << x << "\n";
        }
    }

    cout << ans << "\n";

    return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=1; i<=n; i++){
        if(!visited[i] && mn >= key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

//    if(ans == 18)
//        ans -=1;

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define D1(x) (cerr << #x << ": " << x << " ")
#define D2(x,y) (cerr << #x << ": " << x << ", " << #y << ": " << y << " ")
#define D3(x,y,z) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << " " )
#define D4(x,y,z,a) (cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << ", " << #a << ": " << a << " " )
#define D1e(x) (D1(x) << endl)
#define D2e(x,y) (D2(x,y) << endl)
#define D3e(x,y,z) (D3(x,y,z) << endl)
#define D4e(x,y,z,a) (D4(x,y,z,a) << endl)
#define Dsp cerr << " ||  ";
#define Dspe cerr << " || "<< endl;

#define fr(i,n) for(i=0; i<n; i++)

using namespace std;

int min_key(vector<ll> key, ll n, vector<bool> visited){

    int ind = -1;
    ll mn = INT_MAX;
    for(ll i=0; i<n; i++){
        if(!visited[i] && mn >= key[i]){
            mn = key[i];
            ind = i;
        }
    }

    return ind;
}

ll lp(ll x, ll y){

    ll len =0;
    ll tp = y;
    while(tp!=0){
        tp/=10;
        len++;
    }

    ll top = pow(10, len);
    return x*top + y;
}

map< pair<int,int>, ll> mp;

ll val(int x, int y, vector<ll> arr){
//    pair<ll, ll> pp = {x,y};
    ll mpv = INT_MAX;
    pair<int,int> pp2 = {x,y};
    if(mp.find(pp2) != mp.end()){
        mpv = mp[pp2];
//        return mpv;
    }
//    D3e(x,y,mpv);

//    D3e(arr[x], arr[y], mpv);
    return min((arr[x]+arr[y]) , mpv);
}

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

//    freopen("./input", "r", stdin);
    ll n,m;
    cin >> n >> m;
    vector<ll> arr(n+1);
    ll i,j;
    for(ll i=1; i<=n; i++){
        cin >> arr[i];
    }

    fr(i, m){
        int x,y;
        ll w;
        cin >> x >> y;
        cin >> w;
//        cout << x << y  << "\n";
        pair<int,int> pp1 = {x,y};
        pair<int,int> pp2 = {y,x};

        if(mp.find({x,y}) != mp.end()){
            mp[pp1] = min(w, mp[pp1]);
            mp[pp2] = min(w, mp[pp2]);

        }else{

            mp[pp1] = w;
            mp[pp2] = w;
        }
//        D4e(x,y, w, mp[pp2]);

    }

//    i=0;
//    for(auto x: adj){
//        cout << i++ << " :: ";
//        for(auto y:x){
//            cout << y << ", ";
//        }
//        cout << "\n";
//    }

    vector<bool> visited(n+1);

    vector<ll> key(n+1, INT_MAX);

//    visited[0] = 1;
    ll ans =0;
    key[1] = 0;
    for(ll i=1; i<=n; i++){

        int k = min_key(key, n, visited);
        if(k == -1)
            break;
        visited[k] = 1;

        for(int j=1; j<=n; j++){
            pair<int,int> pp = {j,k};
//            D4e(j, k, key[j] , mp[pp] );
            if(!visited[j] && j!=k && key[j] > val(k,j,arr)){
                key[j] = val(k,j,arr);
//                D3e(k,j, key[j]);
//                cout << key[j] << "\n";
//                ans += val(k,j,arr,mp);
            }
        }

    }
    for(ll i=1; i<=n; i++)
        ans += key[i];
//    cout << "\n";

//    if(ans == 18)
//        ans -=1;

    cout << ans << "\n";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f3f3f3f3f;
struct edge
{
    int u,v;
    ll w;
}e[maxn*2];
int n,m;
ll a[maxn];
bool cmp(edge &a,edge &b)
{
    return a.w<b.w;
}
int tot=0;
void add(int u,int v,ll w)
{
    e[++tot]=edge{u,v,w};
}
int fa[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:getfa(fa[x]);
}
bool merge(int x,int y)
{
    int fax=getfa(x),fay=getfa(y);
    if(fax==fay) return false;
    fa[fax]=fay;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf,po;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
            minn=a[i];po=i;
        }
    }
    for(int i=1;i<=n;i++) if(i!=po) add(po,i,a[po]+a[i]);
    int x,y;ll c;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&c);
        add(x,y,c);
    }
    sort(e+1,e+tot+1,cmp);
    int rst=n;
    ll ans=0;
    for(int i=1;i<=tot&&rst>1;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(merge(u,v))
        {
            ans+=e[i].w;rst--;
        }
    }
    printf("%lld\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+5;
const int inf=0x3f3f3f3f3f3f3f3f;
struct edge
{
    int u,v;
    ll w;
}e[maxn*2];
int n,m;
ll a[maxn];
bool cmp(edge &a,edge &b)
{
    return a.w<b.w;
}
int tot=0;
void add(int u,int v,ll w)
{
    e[++tot]=edge{u,v,w};
}
int fa[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:getfa(fa[x]);
}
bool merge(int x,int y)
{
    int fax=getfa(x),fay=getfa(y);
    if(fax==fay) return false;
    fa[fax]=fay;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf,po;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
            minn=a[i];po=i;
        }
    }
    for(int i=1;i<=n;i++) if(i!=po) add(po,i,a[po]+a[i]);
    int x,y;ll c;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&c);
        add(x,y,c);
    }
    sort(e+1,e+tot+1,cmp);
    int rst=n;
    ll ans=0;
    for(int i=1;i<=tot&&rst>1;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(merge(u,v))
        {
            ans+=e[i].w;rst--;
        }
    }
    printf("%lld\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+5;
ll a[maxn];
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf;
    int po;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
           minn=a[i];
           po=i;
        }
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(i==po) continue;
        ans+=(a[i]+minn);
    }
    int x,y;
    ll w;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&w);
        if(w-(minn+max(a[x],a[y]))<0)
        {
            ans+=w-minn-max(a[x],a[y]);
        }
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+5;
const int inf=0x3f3f3f3f3f3f3f3f;
struct edge
{
    int u,v;
    ll w;
}e[maxn*2];
int n,m;
ll a[maxn];
bool cmp(edge &a,edge &b)
{
    return a.w<b.w;
}
int tot=0;
void add(int u,int v,ll w)
{
    e[++tot]=edge{u,v,w};
}
int fa[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:fa[x]=getfa(fa[x]);
}
bool merge(int x,int y)
{
    int fax=getfa(x),fay=getfa(y);
    if(fax==fay) return false;
    fa[fax]=fay;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf,po;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
            minn=a[i];po=i;
        }
    }
    for(int i=1;i<=n;i++) if(i!=po) add(po,i,a[po]+a[i]);
    int x,y;ll c;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&c);
        add(x,y,c);
    }
    sort(e+1,e+tot+1,cmp);
    int rst=n;
    ll ans=0;
    for(int i=1;i<=tot&&rst>1;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(merge(u,v))
        {
            ans+=e[i].w;rst--;
        }
    }
    printf("%lld\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f3f3f3f3f;
struct edge
{
    int u,v;
    ll w;
}e[maxn<<1];
int n,m;
ll a[maxn];
bool cmp(edge a,edge b)
{
    return a.w<b.w;
}
int tot=0;
void add(int u,int v,ll w)
{
    e[++tot]=edge{u,v,w};
}
int fa[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:getfa(fa[x]);
}
bool merge(int x,int y)
{
    int fax=getfa(x),fay=getfa(y);
    if(fax==fay) return false;
    fa[fax]=fay;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf,po;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
            minn=a[i];po=i;
        }
    }
    for(int i=1;i<=n;i++) if(i!=po) add(po,i,a[po]+a[i]);
    int x,y;ll c;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&c);
        add(x,y,c);
    }
    sort(e+1,e+tot+1,cmp);
    int rst=n;
    ll ans=0;
    for(int i=1;i<=tot&&rst>1;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(merge(u,v))
        {
            ans+=e[i].w;rst--;
        }
    }
    printf("%lld\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
struct edge
{
    int u,v;
    ll w;
}e[maxn*2];
int n,m;
ll a[maxn];
bool cmp(edge &a,edge &b)
{
    return a.w<b.w;
}
int tot=0;
void add(int u,int v,ll w)
{
    e[++tot]=edge{u,v,w};
}
int fa[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:fa[x]=getfa(fa[x]);
}
bool merge(int x,int y)
{
    int fax=getfa(x),fay=getfa(y);
    if(fax==fay) return false;
    fa[fax]=fay;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf,po;
    //cout<<inf<<endl;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
            minn=a[i];po=i;
        }
    }
    for(int i=1;i<=n;i++) if(i!=po) add(po,i,a[po]+a[i]);
    int x,y;ll c;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&c);
        add(x,y,c);
    }
    sort(e+1,e+tot+1,cmp);
    int rst=n;
    ll ans=0;
    for(int i=1;i<=tot&&rst>1;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(merge(u,v))
        {
            ans+=e[i].w;rst--;
        }
    }
    printf("%lld\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f3f3f3f3f;
struct edge
{
    int u,v;
    ll w;
}e[maxn<<1];
int n,m;
ll a[maxn];
bool cmp(edge &a,edge &b)
{
    return a.w<b.w;
}
int tot=0;
void add(int u,int v,ll w)
{
    e[++tot]=edge{u,v,w};
}
int fa[maxn];
int getfa(int x)
{
    return fa[x]==x?fa[x]:getfa(fa[x]);
}
bool merge(int x,int y)
{
    int fax=getfa(x),fay=getfa(y);
    if(fax==fay) return false;
    fa[fax]=fay;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    ll minn=inf,po;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(minn>a[i])
        {
            minn=a[i];po=i;
        }
    }
    for(int i=1;i<=n;i++) if(i!=po) add(po,i,a[po]+a[i]);
    int x,y;ll c;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&c);
        add(x,y,c);
    }
    sort(e+1,e+tot+1,cmp);
    int rst=n;
    ll ans=0;
    for(int i=1;i<=tot&&rst>1;i++)
    {
        int u=e[i].u,v=e[i].v;
        if(merge(u,v))
        {
            ans+=e[i].w;rst--;
        }
    }
    printf("%lld\n",ans);

    return 0;
}


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

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

    int n, m, start = 1;
    cin>>n>>m;

    vector<ll> a(n+1);
    for(int i = 1; i<=n; i++){
        cin>>a[i];
        if(a[i]<a[start]) start = i;
    }

    map<pair<int, int>, ll> mx;
    ll z;
    for(int i = 0, x, y; i<m; i++){
        cin>>x>>y>>z;
        mx[{x, y}] = z;
        mx[{y, x}] = z;
    }

    ll ans = 0;
    for(int i = 1; i<=n; i++){
        if(i==start) continue;

        if(mx.find({start, i})!=mx.end()) ans += min(a[start]+a[i], mx[{start,i}]);
        else ans += a[start]+a[i];
    }
    cout<<ans<<"\n";
    return 0;
}


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

const int mxn = 2e5 + 5;
vector<int> parent(mxn), ranks(mxn);

void makeTree(int node){
    parent[node] = node;
    ranks[node] = 0;
}

int findTree(int node){
    if(node==parent[node]) return node;
    return parent[node] = findTree(parent[node]);
}

void mergeTree(int a, int b){
    a = findTree(a);
    b = findTree(b);
    if(a!=b){
        if(ranks[a]<ranks[b]) swap(a, b);
        parent[b] = a;
        if(ranks[a]==ranks[b]) ranks[a]++;
    }
}

struct edge{
    int u, v;
    ll w;
    bool operator<(edge const& other){
        return w < other.w;
    }
};

vector<edge> edges;

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

    int n, m, start = 1; cin>>n>>m;

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

    edge x;
    for(int i = 0; i<m; i++){
        cin>>x.u>>x.v>>x.w;
        edges.push_back(x);
    }

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

        x.u = start;
        x.v = i;
        x.w = a[i] + a[start];
        edges.push_back(x);
    }

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

    for(int i = 0; i<mxn; i++) makeTree(i);

    ll cost = 0;
    for(edge y : edges){
        if(findTree(y.u)!=findTree(y.v)){
            cost += y.w;
            mergeTree(y.u, y.v);
        }
    }

    cout<<cost<<"\n";
    return 0;
}


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

const int mxn = 2e5 + 5;
vector<int> parent(mxn), ranks(mxn);

void makeTree(int node){
    parent[node] = node;
    ranks[node] = 0;
}

int findTree(int node){
    if(node==parent[node]) return node;
    return parent[node] = findTree(parent[node]);
}

void mergeTree(int a, int b){
    a = findTree(a);
    b = findTree(b);
    if(a!=b){
        if(ranks[a]<ranks[b]) swap(a, b);
        parent[b] = a;
        if(ranks[a]==ranks[b]) ranks[a]++;
    }
}

struct edge{
    int u, v;
    ll w;
    bool operator<(edge const& other){
        return w < other.w;
    }
};

vector<edge> edges;

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

    int n, m, start = 1; cin>>n>>m;

    vector<ll> a(n);
    for(int i = 1; i<=n; i++){
        cin>>a[i];
        if(a[i]<a[start]) start = i;
    }

    edge x;
    for(int i = 0; i<m; i++){
        cin>>x.u>>x.v>>x.w;
        edges.push_back(x);
    }

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

        x.u = start;
        x.v = i;
        x.w = a[i] + a[start];
        edges.push_back(x);
    }

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

    for(int i = 0; i<mxn; i++) makeTree(i);

    ll cost = 0;
    for(edge y : edges){
        if(findTree(y.u)!=findTree(y.v)){
            cost += y.w;
            mergeTree(y.u, y.v);
        }
    }

    cout<<cost<<"\n";
    return 0;
}


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

    #define F first
    #define S second
    #define mp make_pair
    #define ios ios_base::sync_with_stdio(0);cin.tie(0);
    #define debug1(x) cerr << "WHY " << x << '\n'
    #define debug2(s, x) cerr << "The Value of " << s << " is " << x << '\n'

    typedef long long ll;
    typedef pair<int, int> ii;
    typedef long double db;

    const int N = 1e5 + 5;
    const int mod = 1e9 + 7;

    int n, k, a[2005];

    ll dp[2005][1 << 12];

    int go(int msk, int nxt){
    	if(nxt > (msk & -msk))return nxt;
    	return msk + nxt;
    }

    ll solve(int idx, int msk){

    	if(idx == n)
    		return msk >= k;

    	ll &ret = dp[idx][msk];

    	if(~ret)
    		return ret;
    	if(msk >= k)
    		return a[idx]? solve(idx + 1, msk) : 2 * solve(idx + 1, msk) % mod;

    	ret = 0;

    	if(a[idx] != 2)
    		ret = (ret + solve(idx + 1, go(msk, 4))) % mod;
    	if(a[idx] != 4)
    		ret = (ret + solve(idx + 1, go(msk, 2))) % mod;

    	return ret;
    }

    int main(){

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

    	k = 1 << k;

    	for(int i = 0; i < n; i++)
    		scanf("%d", a + i);

    	memset(dp, -1, sizeof dp);

    	printf("%d\n", solve(0, 0));

    	return 0;
    }


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

    #define F first
    #define S second
    #define mp make_pair
    #define ios ios_base::sync_with_stdio(0);cin.tie(0);
    #define debug1(x) cerr << "WHY " << x << '\n'
    #define debug2(s, x) cerr << "The Value of " << s << " is " << x << '\n'

    typedef long long ll;
    typedef pair<int, int> ii;
    typedef long double db;

    const int N = 1e5 + 5;
    const int mod = 1e9 + 7;

    int n, k, a[2005];

    ll dp[2005][1 << 12];

    int go(int msk, int nxt){
    	if(nxt > (msk))return nxt;
    	return msk + nxt;
    }

    ll solve(int idx, int msk){

    	if(idx == n)
    		return msk >= k;

    	ll &ret = dp[idx][msk];

    	if(~ret)
    		return ret;
    	if(msk >= k)
    		return a[idx]? solve(idx + 1, msk) : 2 * solve(idx + 1, msk) % mod;

    	ret = 0;

    	if(a[idx] != 2)
    		ret = (ret + solve(idx + 1, go(msk, 4))) % mod;
    	if(a[idx] != 4)
    		ret = (ret + solve(idx + 1, go(msk, 2))) % mod;

    	return ret;
    }

    int main(){

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

    	k = 1 << k;

    	for(int i = 0; i < n; i++)
    		scanf("%d", a + i);

    	memset(dp, -1, sizeof dp);

    	printf("%d\n", solve(0, 0));

    	return 0;
    }


/*
  Compete against Yourself.
  Author - Aryan Choudhary (@aryanc403)
*/

#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
#ifdef ARYANC403
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
    cerr<<"Time Taken : "<<diff.count()<<"\n";
#endif
}

#ifdef ARYANC403
#include "/home/aryan/codes/PastCodes/header.h"
#else
    #define dbg(args...)
#endif

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m;
    string s;
    vi a;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

lli solve(vi &dp,const vi &pvr,lli cur)
{
    lli ans=0;
    for(lli i=0;i<(1<<k);++i)
    {
        if(cur==2&&(i&1))
        {
            dp[cur]+=pvr[i];
            continue;
        }

        assert(1+cur<=(1<<k));
        if(i+cur<(1<<k))
            dp[cur+i]+=pvr[i];

        if(i+cur==(1<<k))
            ans+=pvr[i];
    }

    for(auto &x:dp)
        x%=mod;
    ans%=mod;
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
// cin>>T;while(T--)
{

    cin>>n>>k;
    a.clear();a.reserve(n);

    lli cnt=1;

    fo(i,n)
    {
        cin>>in;
        a.pb(in/2);
        if(in==0)
            cnt=cnt*2%mod;
    }

    k--;
    lli ans=0;
    vi dp(1<<k);
    dp[0]=1;
    for(auto x:a)
    {
        auto pvr=dp;
        dp.clear();dp.resize(1<<k);
        if(x==0)
        {
            x=3;
            cnt*=(mod+1)/2;
            cnt%=mod;
        }
        if(x&1)
            ans+=solve(dp,pvr,1)*cnt%mod;
        if(x&2)
            ans+=solve(dp,pvr,2)*cnt%mod;
    }

    ans%=mod;
    cout<<ans<<endl;
}   aryanc403();
    return 0;
}


    #pragma GCC optimize("O3")
    #pragma GCC target("avx2")
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <bitset>
    #include <set>
    #include <map>
    #include <string>
    #include <cmath>
    #include <queue>

    using namespace std;

    typedef long long ll;
    typedef long double ld;
    #define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
    const ll SIZEARR = 1e5 * 3 + 10, INF = 1e9 * 1e9 + 2, MOD = 1e9 + 7;

    ll q;
    vector<ll> vec, vec2;
    map<ll, ll> cnt;
    ll dp[2001][2049][2];
    bool used[SIZEARR];

    int main() {
    	/*fastInp;*/

    	ll n, k;
    	cin >> n >> k;

    	dp[0][0][0] = 1;

    	vec.resize(n);
    	for (int i = 0; i < n; i++) cin >> vec[i];

    	k--;
    	ll pw = (1ll << k);
    	for (int i = 1; i <= n; i++) {
    		if (vec[i - 1] == 2) {
    			for (int j = 1; j <= pw; j++) {
    				if (j != pw) {
    					dp[i][j][0] += dp[i - 1][j - 1][0];
    					dp[i][j][1] += dp[i - 1][j - 1][1];
    				}
    				else {
    					dp[i][0][1] += dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1];
    				}
    				dp[i][j][0] %= MOD;
    				dp[i][j][1] %= MOD;
    			}
    		}
    		if (vec[i - 1] == 4) {
    			ll cnt0 = 0, cnt1 = 0;
    			for (int j = 1; j <= pw; j++) {
    				if (j & 1) {
    					cnt0 += dp[i - 1][j][0];
    					cnt1 += dp[i - 1][j][1];
    				}
    				cnt0 %= MOD;
    				cnt1 %= MOD;
    			}
    			dp[i][2][0] += cnt0;
    			dp[i][2][1] += cnt1;

    			for (int j = 2; j <= pw; j++) {
    				if (j & 1) continue;
    				if (j != pw) {
    					dp[i][j][0] += dp[i - 1][j - 2][0];
    					dp[i][j][1] += dp[i - 1][j - 2][1];
    				}
    				else {
    					dp[i][0][1] += dp[i - 1][j - 2][0] + dp[i - 1][j - 2][1];
    				}
    				dp[i][j][0] %= MOD;
    				dp[i][j][1] %= MOD;
    			}
    		}
    		if (vec[i - 1] == 0) {
    			for (int j = 1; j <= pw; j++) {
    				if (j != pw) {
    					dp[i][j][0] += dp[i - 1][j - 1][0];
    					dp[i][j][1] += dp[i - 1][j - 1][1];
    				}
    				else {
    					dp[i][0][1] += dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1];
    				}
    				dp[i][j][0] %= MOD;
    				dp[i][j][1] %= MOD;
    			}

    			ll cnt0 = 0, cnt1 = 0;
    			for (int j = 1; j <= pw; j++) {
    				if (j & 1) {
    					cnt0 += dp[i - 1][j][0];
    					cnt1 += dp[i - 1][j][1];
    				}
    				cnt0 %= MOD;
    				cnt1 %= MOD;
    			}
    			dp[i][2][0] += cnt0;
    			dp[i][2][1] += cnt1;

    			for (int j = 2; j <= pw; j++) {
    				if (j & 1) continue;
    				if (j != pw) {
    					dp[i][j][0] += dp[i - 1][j - 2][0];
    					dp[i][j][1] += dp[i - 1][j - 2][1];
    				}
    				else {
    					dp[i][0][1] += dp[i - 1][j - 2][0] + dp[i - 1][j - 2][1];
    				}
    				dp[i][j][0] %= MOD;
    				dp[i][j][1] %= MOD;
    			}
    		}
    	}

    	ll ans = 0;
    		for (int j = 0; j <= pw; j++) {
    			ans += dp[n][j][1];
    			ans %= MOD;
    		}

    	cout << ans;
    	return 0;
    }


#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <queue>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
const ll SIZEARR = 1e5 * 3 + 10, INF = 1e9 * 1e9 + 2, MOD = 1e9 + 7;

ll q;
vector<ll> vec, vec2;
map<ll, ll> cnt;
ll dp[2001][2049][2];
bool used[SIZEARR];

int main() {
	/*fastInp;*/

	ll n, k;
	cin >> n >> k;

	dp[0][0][0] = 1;

	vec.resize(n);
	for (int i = 0; i < n; i++) cin >> vec[i];

	k--;
	ll pw = (1ll << k);
	for (int i = 1; i <= n; i++) {
		if (vec[i - 1] == 2) {
			for (int j = 1; j <= pw; j++) {
				if (j != pw) {
					dp[i][j][0] += dp[i - 1][j - 1][0];
					dp[i][j][1] += dp[i - 1][j - 1][1];
				}
				else {
					dp[i][0][1] += dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1];
				}
				dp[i][j][0] %= MOD;
				dp[i][j][1] %= MOD;
			}
		}
		if (vec[i - 1] == 4) {
			ll cnt0 = 0, cnt1 = 0;
			for (int j = 1; j <= pw; j++) {
				if (j & 1) {
					cnt0 += dp[i - 1][j][0];
					cnt1 += dp[i - 1][j][1];
				}
				cnt0 %= MOD;
				cnt1 %= MOD;
			}
			dp[i][2][0] += cnt0;
			dp[i][2][1] += cnt1;

			for (int j = 2; j <= pw; j++) {
				if (j & 1) continue;
				if (j != pw) {
					dp[i][j][0] += dp[i - 1][j - 2][0];
					dp[i][j][1] += dp[i - 1][j - 2][1];
				}
				else {
					dp[i][0][1] += dp[i - 1][j - 2][0] + dp[i - 1][j - 2][1];
				}
				dp[i][j][0] %= MOD;
				dp[i][j][1] %= MOD;
			}
		}
		if (vec[i - 1] == 0) {
			for (int j = 1; j <= pw; j++) {
				if (j != pw) {
					dp[i][j][0] += dp[i - 1][j - 1][0];
					dp[i][j][1] += dp[i - 1][j - 1][1];
				}
				else {
					dp[i][0][1] += dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1];
				}
				dp[i][j][0] %= MOD;
				dp[i][j][1] %= MOD;
			}

			ll cnt0 = 0, cnt1 = 0;
			for (int j = 1; j <= pw; j++) {
				if (j & 1) {
					cnt0 += dp[i - 1][j][0];
					cnt1 += dp[i - 1][j][1];
				}
				cnt0 %= MOD;
				cnt1 %= MOD;
			}
			dp[i][2][0] += cnt0;
			dp[i][2][1] += cnt1;

			for (int j = 2; j <= pw; j++) {
				if (j & 1) continue;
				if (j != pw) {
					dp[i][j][0] += dp[i - 1][j - 2][0];
					dp[i][j][1] += dp[i - 1][j - 2][1];
				}
				else {
					dp[i][0][1] += dp[i - 1][j - 2][0] + dp[i - 1][j - 2][1];
				}
				dp[i][j][0] %= MOD;
				dp[i][j][1] %= MOD;
			}
		}
	}

	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= pw; j++) {
			ans += dp[i][j][1];
			ans %= MOD;
		}
	}

	cout << ans;
	return 0;
}


// dile a la jardinera que traigo flores
// corner cases // int vs ll // cin vs scanf
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())
#define fill(x,v) memset(x,v,sizeof(x))
#define FER(i,a,b) for(int i = int(a); i < int(b); ++i)
#define trace(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define N 2048
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;

int n,k,a[N],zeros[N];
ll dp[N][1<<12];

ll binpow(ll x, ll p){
	ll ans = 1;
	while(p){
		if(p&1) ans = (ans*x)%MOD;
		p >>= 1; x = (x*x)%MOD;
	}
	return ans;
}

ll go(int id, int mask){
	if(id == n) return (mask&(1<<k)) != 0;
	if(mask&(1<<k)) return binpow(2,zeros[id]);
	ll &ans = dp[id][mask];
	if(ans != -1) return ans;
	ans = 0;
	ll val1 = go(id+1,mask+2);
	ll val2 = go(id+1,mask+4);
	if(mask&(1<<1)) val2 = go(id+1,4);
	if(a[id] == 2) ans = val1;
	if(a[id] == 4) ans = val2;
	if(a[id] == 0) ans = (val1+val2)%MOD;
	return ans;
}

int main(){

	fastio;
	fill(dp,-1);
	cin >> n >> k;
	FER(i,0,n) cin >> a[i];
	zeros[n] = 0;
	for(int i = n-1; i >= 0; --i) zeros[i] = zeros[i+1] + (a[i] == 0);
	cout << go(0,0) << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
typedef long long ll;
#define MAX 2105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[MAX], pw[MAX];
ll dp[MAX][1<<11];
ll ans;
int main(){
    scanf("%d%d", &n, &k);
    REQ(i,n) scanf("%d", a+i);
    pw[n+1]=1;
    for(int i=n;i;i--) {
        pw[i] = pw[i+1];
        if (a[i]==0) pw[i] *= 2;
        pw[i] %= MOD;
    }
    dp[0][0]=1;
    REQ(i,n) {
        for(int j=0;j<(1<<k);j++) {
            if (a[i]==0 || a[i]==2) {
                if (j+2>=(1<<k)) {
                    ans += dp[i-1][j] * pw[i+1];
                } else {
                    (dp[i][j+2] += dp[i-1][j])%=MOD;
                }
            }
            if (a[i]==0 || a[i]==4) {
                if (j & 2) {
                    (dp[i][4] += dp[i-1][j])%=MOD;
                } else if (j+4 >= (1<<k)) {
                    ans += dp[i-1][j] * pw[i+1];
                } else (dp[i][j+4] += dp[i-1][j])%=MOD;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
typedef long long ll;
#define MAX 2105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[MAX], pw[MAX];
ll dp[MAX][1<<11];
ll ans;
int main(){
    scanf("%d%d", &n, &k);
    REQ(i,n) scanf("%d", a+i);
    pw[n+1]=1;
    for(int i=n;i;i--) {
        pw[i] = pw[i+1];
        if (a[i]==0) pw[i] *= 2;
        pw[i] %= MOD;
    }
    dp[0][0]=1;
    REQ(i,n) {
        for(int j=0;j<(1<<k);j++) {
            if (a[i]==0 || a[i]==2) {
                if (j+2>=(1<<k)) {
                    ans += dp[i-1][j] * pw[i+1]; ans %= MOD;
                } else {
                    (dp[i][j+2] += dp[i-1][j])%=MOD;
                }
            }
            if (a[i]==0 || a[i]==4) {
                if (j & 2) {
                    (dp[i][4] += dp[i-1][j])%=MOD;
                } else if (j+4 >= (1<<k)) {
                    ans += dp[i-1][j] * pw[i+1]; ans %= MOD;
                } else (dp[i][j+4] += dp[i-1][j])%=MOD;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
typedef long long ll;
#define MAX 2105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[MAX], pw[MAX];
ll dp[MAX][1<<11];
ll ans;
int main(){
    scanf("%d%d", &n, &k);
    REQ(i,n) scanf("%d", a+i);
    pw[n+1]=1;
    for(int i=n;i;i--) {
        pw[i] = pw[i+1];
        if (a[i]==0) pw[i] *= 2;
        pw[i] %= MOD;
    }
    dp[0][0]=1;
    REQ(i,n) {
        for(int j=0;j<(1<<k);j++) {
            if (a[i]==0 || a[i]==2) {
                if (j+2>=(1<<k)) {
                    ans += dp[i-1][j] * pw[i];
                } else {
                    (dp[i][j+2] += dp[i-1][j])%=MOD;
                }
            }
            if (a[i]==0 || a[i]==4) {
                if (j & 2) {
                    (dp[i][4] += dp[i-1][j])%=MOD;
                } else if (j+4 >= (1<<k)) {
                    ans += dp[i-1][j] * pw[i];
                } else (dp[i][j+4] += dp[i-1][j])%=MOD;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}


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

const int N = 2000;
const int A = N * 2;
const int MOD = 1e9 + 7;

int n, target;
int arr[N];
int empties[N + 1];
int dp[N + 1][A + 1][2];

int solve(int i, int s, int danger) {
    if (dp[i][s][danger] != -1) {
        return dp[i][s][danger];
    }
    if (s >= target) {
        return dp[i][s][danger] = empties[i];
    }
    if (i == n) {
        return dp[i][s][danger] = 0;
    }
    dp[i][s][danger] = 0;
    if (arr[i] == 0 || arr[i] == 1) {
        dp[i][s][danger] += solve(i + 1, s + 1, !danger);
    }
    if (arr[i] == 0 || arr[i] == 2) {
        if (danger) {
            dp[i][s][danger] += solve(i + 1, 2, 0);
        } else {
            dp[i][s][danger] += solve(i + 1, s + 2, danger);
        }
    }
    return dp[i][s][danger] %= MOD;
}

int main() {
    cin >> n >> target;
    target = (1 << target) / 2;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        arr[i] /= 2;
    }
    empties[n] = 1;
    for (int i = n - 1; i >= 0; --i) {
        empties[i] = empties[i + 1] * ((arr[i] == 0) + 1) % MOD;
    }
    memset(dp, -1, sizeof dp);
    cout << solve(0, 0, 0);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e3 + 100, K = 13, MOD = 1e9 + 7;

int dp[2][K][1 << K], a[N];
int n, k, ans;

void _sum(int &a, int b) {
    a += b;
    if (a >= MOD)
	a -= MOD;
}

void update(int i, int j, int mask, auto now, auto &nxt) {
    if (mask + i >= (1 << k))
	_sum(nxt[k][(mask + i) % (1 << k)], now[j][mask]);
    else
	_sum(nxt[j][mask + i], now[j][mask]);
}

int f(int mask) {
    for (int i = k; i >= 0; i--)
	if ((mask >> i) & 1)
	    return i;
    return 0;
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
	cin >> a[i];
    if (a[0] == 2)
	dp[0][0][2] = 1;
    else if (a[0] == 4)
	dp[0][0][4] = 1;
    else
	dp[0][0][2] = dp[0][0][4] = 1;
    for (int i = 0; i < n - 1; i++) {
	memset(dp[(i + 1) % 2], 0, sizeof dp[(i + 1) % 2]);
	auto now = dp[i % 2], nxt = dp[(i + 1) % 2];
	for (int j = 0; j < K; j++)
	    for (int mask = 0; mask < (1 << k); mask++) {
		if (a[i + 1] == 2)
		    update(a[i + 1], j, mask, now, nxt);
		else if (a[i + 1] == 4) {
		    if ((mask >> 1) & 1)
			_sum(nxt[max(f(mask), j)][4], now[j][mask]);
			else
			    update(a[i + 1], j, mask, now, nxt);
		}
		else {
		    update(2, j, mask, now, nxt);
		    if ((mask >> 1) & 1)
			_sum(nxt[max(f(mask), j)][4], now[j][mask]);
		    else
			update(4, j, mask, now, nxt);
		}
	    }
    }
    auto now = dp[(n - 1) % 2];
    for (int j = 0; j < K; j++)
	for (int mask = 0; mask < (1 << k); mask++)
	    if (max(j, f(mask)) >= k)
		_sum(ans, now[j][mask]);
    return cout << ans, 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long MD=1e9+7;
int n,k,a[2005],c[2005];
long long dp[2005][2049];
long long qpow(long long b,long long exp) {
    long long ret=1;
    while (exp!=0) {
        if (exp%2==1) {ret*=b; ret%=MD;}
        b*=b; b%=MD;
        exp/=2;
    }
    return ret;
}
long long recurse(int cur,int sum) {
    if (sum>=(1<<k)) return qpow(2,c[cur]);
    if (cur==n) return 0;
    if (dp[cur][sum]!=-1) return dp[cur][sum];
    dp[cur][sum]=0;
    if (a[cur]!=4) dp[cur][sum]+=recurse(cur+1,sum+2);
    if (a[cur]!=2) {
        if (sum&2) dp[cur][sum]+=recurse(cur+1,4);
        else dp[cur][sum]+=recurse(cur+1,sum+4);
    }
    dp[cur][sum]%=MD;
    return dp[cur][sum];
}
int main() {
    cin>>n>>k;
    for (int i=0; i<n; i++) cin>>a[i];
    for (int i=n-1; i>=0; i--) c[i]=c[i+1]+(a[i]==0);
    memset(dp,-1,sizeof(dp));
    cout<<recurse(0,0)<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
const long long MD=1e9+7;
int n,k,a[2005],c[2005];
long long dp[2005][2049];
long long recurse(int cur,int sum) {
    if (sum>=(1<<k)) return (1<<c[cur]);
    if (cur==n) return 0;
    if (dp[cur][sum]!=-1) return dp[cur][sum];
    dp[cur][sum]=0;
    if (a[cur]!=4) dp[cur][sum]+=recurse(cur+1,sum+2);
    if (a[cur]!=2) {
        if (sum&2) dp[cur][sum]+=recurse(cur+1,4);
        else dp[cur][sum]+=recurse(cur+1,sum+4);
    }
    return dp[cur][sum];
}
int main() {
    cin>>n>>k;
    for (int i=0; i<n; i++) cin>>a[i];
    for (int i=n-1; i>=0; i--) c[i]=c[i+1]+(a[i]==0);
    memset(dp,-1,sizeof(dp));
    cout<<recurse(0,0)<<'\n';
}


// Ala be zekrellah tatmaenolgholoob ...
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define F first
#define S second
#define MP make_pair
const int maxn = 2000+9;
const int lg = 12;
const ll mod = 1e9+7;

int a[maxn];
int dp[maxn][1<<lg];

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] /= 2;
	}
	// dp
	int ans = 0;
	dp[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		if (a[i] == 0)
			ans = 2LL * ans % mod;
		// 2
		if (a[i] == 0 || a[i] == 1) {
			int f = 1;
			for (int mask = 0; mask < (1 << k); mask++)
				dp[i][mask + (1 << f)] = (dp[i][mask + (1 << f)] + dp[i - 1][mask]) % mod;
		}
		// 4
		if (a[i] == 0 || a[i] == 2) {
			int f = 2;
			for (int mask = 0; mask < (1 << k); mask++) {
				if (mask & 2)
				dp[i][(1 << f)] = (dp[i][(1 << f)] + dp[i - 1][mask]) % mod;
				else
				dp[i][mask + (1 << f)] = (dp[i][mask + (1 << f)] + dp[i - 1][mask]) % mod;
			}
		}
		for (int mask = 0; mask < (1 << lg); mask++)
			if (mask & (1 << k)) {
				ans = (ans + dp[i][mask]) % mod;
				dp[i][mask] = 0;
			}
	}
	// ans
	cout << ans << "\n";
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2005;
const int MOD = 1e9 + 7;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int dp[mxN][(1 << 12)][2];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n, K;
	cin >> n >> K;
	vi a(n + 1);
	repn(i, 1, n + 1) cin >> a[i];
	dp[0][0][0] = 1;
	repn(i, 1, n + 1){
		int le = 1, ri = 3;
		if(a[i] == 2) le = 1, ri = 2;
		if(a[i] == 4) le = 2, ri = 3;
		rep(j, (1 << 12)) rep(k, 2) repn(l, le, ri) if(dp[i - 1][j][k]){
			int j1 = j;
			int lo = 0;
			rep(m, 13) if(j1 & (1 << m)){
				lo = m;
				break;
			}
			if((1 << lo) < (1 << l)) j1 = (1 << l);
			else{
				vi cnt(15, 0);
				cnt[l]++;
				rep(m, 11){
					if((cnt[m] + ((j1 & (1 << m)) > 0)) > 1){
						cnt[m + 1]++;
						j1 ^= (1 << m);
						if(m == 10) j1 |= (1 << (m + 1));
					}
					else if(cnt[m]) j1 |= (1 << m);
				}
			}
			int f = (j1 >= (1 << K));
			(dp[i][j1][(k | f)] += dp[i - 1][j][k]) %= MOD;
		}
	}
	ll ans = 0LL;
	rep(j, (1 << 12)) (ans += dp[n][j][1]) %= MOD;
	cout << ans << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2005;
const int MOD = 1e9 + 7;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int dp[mxN][(1 << 12)][2];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n, K;
	cin >> n >> K;
	vi a(n + 1);
	repn(i, 1, n + 1) cin >> a[i];
	dp[0][0][0] = 1;
	repn(i, 1, n + 1){
		int le = 1, ri = 3;
		if(a[i] == 2) le = 1, ri = 2;
		if(a[i] == 4) le = 2, ri = 3;
		rep(j, (1 << 12)) rep(k, 2) repn(l, le, ri) if(dp[i - 1][j][k]){
			int j1 = j;
			int lo = 0;
			rep(m, 13) if(j1 & (1 << m)){
				lo = m;
				break;
			}
			if((1 << lo) < (1 << l)) j1 = (1 << l);
			else{
				int f1 = 1;
				repn(m, l, 11){
					if((f1 + ((j1 & (1 << m)) > 0)) > 1){
						j1 ^= (1 << m);
						if(m == 10) j1 |= (1 << (m + 1));
					}
					else if(f1) j1 |= (1 << m), f1 = 0;
					else break;
				}
			}
			int f = (j1 >= (1 << K));
			dp[i][j1][(k | f)] += dp[i - 1][j][k];
			if(dp[i][j1][(k | f)] >= MOD) dp[i][j1][(k | f)] -= MOD;
		}
	}
	int ans = 0LL;
	rep(j, (1 << 12)){
		ans += dp[n][j][1];
		if(ans >= MOD) ans -= MOD;
	}
	cout << ans << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2005;
const int MOD = 1e9 + 7;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int dp[mxN][(1 << 12)][2];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n, K;
	cin >> n >> K;
	vi a(n + 1);
	repn(i, 1, n + 1) cin >> a[i];
	dp[0][0][0] = 1;
	repn(i, 1, n + 1){
		int le = 1, ri = 3;
		if(a[i] == 2) le = 1, ri = 2;
		if(a[i] == 4) le = 2, ri = 3;
		rep(j, (1 << 12)) rep(k, 2) repn(l, le, ri) if(dp[i - 1][j][k]){
			int j1 = j;
			int lo = 0;
			rep(m, 13) if(j1 & (1 << m)){
				lo = m;
				break;
			}
			if((1 << lo) < (1 << l)) j1 = (1 << l);
			else{
				vi cnt(15, 0);
				cnt[l]++;
				rep(m, 11){
					if((cnt[m] + ((j1 & (1 << m)) > 0)) > 1){
						cnt[m + 1]++;
						j1 ^= (1 << m);
						if(m == 10) j1 |= (1 << (m + 1));
					}
					else if(cnt[m]) j1 |= (1 << m);
					else break;
				}
			}
			int f = (j1 >= (1 << K));
			(dp[i][j1][(k | f)] += dp[i - 1][j][k]) %= MOD;
		}
	}
	ll ans = 0LL;
	rep(j, (1 << 12)) (ans += dp[n][j][1]) %= MOD;
	cout << ans << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2005;
const int MOD = 1e9 + 7;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

ll dp[mxN][(1 << 14)][2];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n, K;
	cin >> n >> K;
	vi a(n + 1);
	repn(i, 1, n + 1) cin >> a[i];
	dp[0][0][0] = 1;
	repn(i, 1, n + 1){
		int le = 1, ri = 3;
		if(a[i] == 2) le = 1, ri = 2;
		if(a[i] == 4) le = 2, ri = 3;
		rep(j, (1 << 13)) rep(k, 2) repn(l, le, ri) if(dp[i - 1][j][k]){
			int j1 = j;
			int lo = 0;
			rep(m, 13) if(j1 & (1 << m)){
				lo = m;
				break;
			}
			if((1 << lo) < (1 << l)) j1 = (1 << l);
			else{
				vi cnt(15, 0);
				cnt[l]++;
				rep(m, 12){
					if((cnt[m] + ((j1 & (1 << m)) > 0)) > 1){
						cnt[m + 1]++;
						j1 ^= (1 << m);
						if(m == 11) j1 |= (1 << (m + 1));
					}
					else if(cnt[m]) j1 |= (1 << m);
				}
			}
			int f = (j1 >= (1 << K));
			(dp[i][j1][(k | f)] += dp[i - 1][j][k]) %= MOD;
		}
	}
	ll ans = 0LL;
	rep(j, (1 << 13)) (ans += dp[n][j][1]) %= MOD;
	cout << ans << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 2005;
const int MOD = 1e9 + 7;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int dp[mxN][(1 << 12)][2];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n, K;
	cin >> n >> K;
	vi a(n + 1);
	repn(i, 1, n + 1) cin >> a[i];
	dp[0][0][0] = 1;
	repn(i, 1, n + 1){
		int le = 1, ri = 3;
		if(a[i] == 2) le = 1, ri = 2;
		if(a[i] == 4) le = 2, ri = 3;
		rep(j, (1 << 12)) rep(k, 2) repn(l, le, ri) if(dp[i - 1][j][k]){
			int j1 = j;
			int lo = 0;
			rep(m, 13) if(j1 & (1 << m)){
				lo = m;
				break;
			}
			if((1 << lo) < (1 << l)) j1 = (1 << l);
			else{
				vi cnt(15, 0);
				cnt[l]++;
				repn(m, l, 11){
					if((cnt[m] + ((j1 & (1 << m)) > 0)) > 1){
						cnt[m + 1]++;
						j1 ^= (1 << m);
						if(m == 10) j1 |= (1 << (m + 1));
					}
					else if(cnt[m]) j1 |= (1 << m);
					else break;
				}
			}
			int f = (j1 >= (1 << K));
			(dp[i][j1][(k | f)] += dp[i - 1][j][k]) %= MOD;
		}
	}
	ll ans = 0LL;
	rep(j, (1 << 12)) (ans += dp[n][j][1]) %= MOD;
	cout << ans << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")

#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair

using namespace std;

//mt19937 gen(time(0));
const ll inf = 1e18 + 18;
const int maxn = 2005;

int MOD = 1e9 + 7;

int dp[maxn][1 << 12][2];

main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < maxn; i++)
        for(int j = 0; j < int(1 << 12); j++)
            for(int jj = 0; jj < 2; jj++)
                dp[i][j][jj] = 0;
    dp[0][0][0] = 1;
    int mod = 1 << (k - 1);
    for(int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if (x != 4)
        {
            for(int j = 0; j < (1 << k); j++)
                for(int jj = 0; jj < 2; jj++)
            {
                int f = jj;
                int u = j + 1;
                if (u >= mod)
                {
                    f = 1;
                    u -= mod;
                }
                dp[i + 1][u][f] += dp[i][j][jj];
                dp[i + 1][u][f] %= MOD;
            }
        }
        if (x != 2)
        {
            for(int j = 0; j < (1 << k); j++)
                for(int jj = 0; jj < 2; jj++)
            {
                if ((j & 1) > 0)
                {
                    int f = jj;
                    int u = 2;
                    dp[i + 1][u][f] += dp[i][j][jj];
                    dp[i + 1][u][f] %= MOD;
                }
                else
                {
                    int f = jj;
                    int u = j + 2;
                    if (u >= mod)
                    {
                        f = 1;
                        u -= mod;
                    }
                    dp[i + 1][u][f] += dp[i][j][jj];
                    dp[i + 1][u][f] %= MOD;
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < (1 << k); i++)
    {
        ans = (ans + dp[n][i][1]) % MOD;
    }
    cout << ans;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

const int MAXN = 2010, MAXK = 15, MAXM = 1 << 12;
const ll md = 1e9 + 7;
int a[MAXN], n, k;
ll dp[MAXN][MAXM], prs[MAXN], ans = 0;

ll pw(int b)
{
	if(b == 0)
		return 1ll;
	ll x = pw(b / 2);
	x *= x;
	x %= md;
	if(b % 2 == 1)
	{
		x *= 2;
		x %= md;
	}
	return x;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	prs[n - 1] = (a[n - 1] == 0);
	for(int i = n - 2; i >= 0; i--)
		prs[i] = prs[i + 1] + (a[i] == 0);
	if(a[0] == 0)
	{
		dp[0][2] = 1;
		dp[0][4] = 1;
	}
	else if(a[0] == 2)
		dp[0][2] = 1;
	else
		dp[0][4] = 1;
	for(int i = 1; i < n; i++)
	{
		for(int mask = 2; mask < (1 << (k + 1)); mask += 2)
		{
			if(a[i] != 4)
			{
				int m2 = 2 + mask, dig = 31 - __builtin_clz(m2);
				if(dig >= k)
				{
					ll tmp = (dp[i - 1][mask] * pw(prs[i] - (a[i] == 0)));
					tmp %= md;
					ans += tmp;
					ans %= md;
				}
				else
				{
					dp[i][m2] += dp[i - 1][mask];
					dp[i][m2] %= md;
				}
			}
			if(a[i] != 2)
			{
				if(__builtin_ctz(mask) <= 1)
				{
					dp[i][4] += dp[i - 1][mask];
					dp[i][4] %= md;
				}
				else
				{
					int m2 = 4 + mask, dig = 31 - __builtin_clz(m2);
					if(dig >= k)
					{
						ll tmp = (dp[i - 1][mask] * pw(prs[i] - (a[i] == 0)));
						tmp %= md;
						ans += tmp;
						ans %= md;
					}
					else
					{
						dp[i][m2] += dp[i - 1][mask];
						dp[i][m2] %= md;
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <cstring>
#include <string>

#define lebokno(a) scanf("%lld",&a);
#define tokno(a) printf("%lld\n",a);
using namespace std;
int main()
{
    int a,m,b,arr[200][200];
    cin>>a;
    for(int i=0;i<200;i++)
    {
            for(int j=0;j<200;j++)
            {
                    arr[i][j]=0;}
            }
    for(int i=1;i<=a;i++)
    {
            cin>>m;
            for(int j=1;j<=m;j++)
            {
                    cin>>b;
                    arr[i][b]++;
                    }

            }
    for(int i=1;i<=a;i++)
    {
            int menang=0,tot=0;
            for(int j=1;j<=a;j++)
            {
                    if(i!=j)
                    {
                    menang=0;
                    int i1=0,i2=0;
                    for(int k=1;k<=100;k++)
                            {
                                    if(arr[i][k]!=0) i1++;
                                    if(arr[j][k]!=0) i2++;
                                    if(arr[i][k]==0 && arr[j][k]==1)menang=1;
                                    if(arr[i][k]==1 && arr[j][k]==1)tot++;
                                    if(k==100 && menang==1 && tot==i2) menang=0;
                            }
                    if(menang==0)break;
                    }

            }
            if(menang==1)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
            }
    return 0;}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <cstring>
#include <string>

#define lebokno(a) scanf("%lld",&a);
#define tokno(a) printf("%lld\n",a);
using namespace std;
int main()
{
    int a,m,b,arr[200][200];
    cin>>a;
    for(int i=1;i<=a;i++)
    {
            cin>>m;
            for(int j=1;j<=m;j++)
            {
                    cin>>b;
                    arr[i][b]++;
                    }

            }
    for(int i=1;i<=a;i++)
    {
            int menang=0,tot=0;
            for(int j=1;j<=a;j++)
            {
                    if(i!=j)
                    {
                    menang=0;
                    for(int k=1;k<=100;k++)
                    {
                                    if(arr[i][k]==0 && arr[j][k]==1)menang=1;
                                    if(arr[i][k]==1 && arr[j][k]==0)tot++;
                                    if(k==100 && menang==0 && tot) menang=0;
                            }
                    if(menang==0)break;
                    }
                    }
            if(menang==1)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
            }
    return 0;}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <cstring>
#include <string>

#define lebokno(a) scanf("%lld",&a);
#define tokno(a) printf("%lld\n",a);
using namespace std;
int main()
{
    int a,m,b,arr[200][200];
    cin>>a;
    for(int i=0;i<200;i++)
    {
            for(int j=0;j<200;j++)
            {
                    arr[i][j]=0;}
            }
    for(int i=1;i<=a;i++)
    {
            cin>>m;
            for(int j=1;j<=m;j++)
            {
                    cin>>b;
                    arr[i][b]++;
                    }

            }
    for(int i=1;i<=a;i++)
    {
            int menang=0,tot=0;
            for(int j=1;j<=a;j++)
            {
                    if(i!=j)
                    {
                    menang=0;
                    for(int k=1;k<=100;k++)
                    {
                                    if(arr[i][k]==0 && arr[j][k]==1)menang=1;
                                    if(arr[i][k]==1 && arr[j][k]==0)tot++;
                                    if(k==100 && menang==0 && tot) menang=0;
                            }
                    if(menang==0)break;
                    }
                    }
            if(menang==1)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
            }
    return 0;}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <cstring>
#include <string>

#define lebokno(a) scanf("%lld",&a);
#define tokno(a) printf("%lld\n",a);
using namespace std;
int main()
{
    int a,m,b,arr[200][200];
    cin>>a;
    for(int i=0;i<200;i++)
    {
            for(int j=0;j<200;j++)
            {
                    arr[i][j]=0;}
            }
    for(int i=1;i<=a;i++)
    {
            cin>>m;
            for(int j=1;j<=m;j++)
            {
                    cin>>b;
                    arr[i][b]++;
                    }

            }
    for(int i=1;i<=a;i++)
    {
            int menang=0,tot=0;
            for(int j=1;j<=a;j++)
            {
                    if(i!=j)
                    {
                    menang=0;
                    for(int k=1;k<=100;k++)
                    {
                                    if(arr[i][k]==0 && arr[j][k]==1)menang=1;
                                    if(arr[i][k]==1 && arr[j][k]==0)tot++;
                                    if(k==100 && menang==0 && tot) menang=0;
                            }
                    if(menang==0)break;
                    }
                    }
            if(menang==1)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
            }
    system("pause");
    return 0;}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int mat[110][110];
int s[110];//size
int main()
{
	int n,m;
	scanf("%d",&n);
	int i,j,t,k;
	for(i=0;i<n;i++)
	{
		for(j=0;j<110;j++)
		{
			mat[i][j]=0; //Intitialise step
		}
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&m);
		s[i]=m;
		for(j=0;j<m;j++)
		{
			scanf("%d",&t);
			mat[i][t]=1;
		}
	}
	int ans[110];
	for(i=0;i<n;i++)
	{
		ans[i]=1;
	}
	int flag;
	for(i=0;i<n;i++)
	{
		if(ans[i]==0)
		continue;
		for(j=0;j<n;j++)
		{
			flag=1;
			if(j==i)
			continue;
			for(k=0;k<100;k++)
			{
				if(mat[i][k]==1 && mat[j][k]==0)
				{
					flag=0; //Not same
					break;
				}
			}
			if(flag && s[i]>s[j]) //same
			{
				ans[i]=0;
				break;
			}
			else if(flag && s[i]==s[j])
			{
				ans[i]=0;
				ans[j]=0;
				break;
			}
			else if(flag && s[i]<s[j])
			{
				ans[j]=0;
			}
		}
	}
	for(i=0;i<n;i++)
	{
		if(ans[i]==1)
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int mat[110][110];
int s[110];//size
int main()
{
	int n,m;
	scanf("%d",&n);
	int i,j,t,k;
	for(i=0;i<n;i++)
	{
		for(j=0;j<110;j++)
		{
			mat[i][j]=0; //Intitialise step
		}
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&m);
		s[i]=m;
		for(j=0;j<m;j++)
		{
			scanf("%d",&t);
			mat[i][t]=1;
		}
	}
	int ans[110];
	for(i=0;i<n;i++)
	{
		ans[i]=1;
	}
	int flag;
	for(i=0;i<n;i++)
	{
		if(ans[i]==0)
		continue;
		for(j=0;j<n;j++)
		{
			flag=1;
			if(j==i)
			continue;
			for(k=0;k<110;k++)
			{
				if(mat[i][k]==1 && mat[j][k]==0)
				{
					flag=0; //Not same
					break;
				}
			}
			if(flag && s[i]>s[j]) //same
			{
				ans[i]=0;
				//~ break;
			}
			else if(flag && s[i]==s[j])
			{
				ans[i]=0;
				ans[j]=0;
				//~ break;
			}
			else if(flag && s[i]<s[j])
			{
				ans[j]=0;
			}
		}
	}
	for(i=0;i<n;i++)
	{
		if(ans[i]==1)
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int mat[110][110];
int s[110];//size
int main()
{
	int n,m;
	scanf("%d",&n);
	int i,j,t,k;
	for(i=0;i<n;i++)
	{
		for(j=0;j<110;j++)
		{
			mat[i][j]=0; //Intitialise step
		}
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&m);
		s[i]=m;
		for(j=0;j<m;j++)
		{
			scanf("%d",&t);
			mat[i][t]=1;
		}
	}
	int ans[110];
	for(i=0;i<n;i++)
	{
		ans[i]=1;
	}
	int flag;
	for(i=0;i<n;i++)
	{
		if(ans[i]==0)
		continue;
		for(j=0;j<n;j++)
		{
			flag=1;
			if(j==i)
			continue;
			for(k=0;k<100;k++)
			{
				if(mat[i][k]==1 && mat[j][k]==0)
				{
					flag=0; //Not same
					break;
				}
			}
			if(flag && s[i]>s[j]) //same
			{
				ans[i]=0;
				//~ break;
			}
			else if(flag && s[i]==s[j])
			{
				ans[i]=0;
				ans[j]=0;
				//~ break;
			}
			else if(flag && s[i]<s[j])
			{
				ans[j]=0;
			}
		}
	}
	for(i=0;i<n;i++)
	{
		if(ans[i]==1)
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}


/*
 * Bidhan Roy
 * University of Dhaka
 */

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

#define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++)
#define sgn(x,y) ((x)+eps<(y)?-1:((x)>eps+(y)?1:0))
#define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
#define mem(x,val) memset((x),(val),sizeof(x));
#define rite(x) freopen(x,"w",stdout);
#define read(x) freopen(x,"r",stdin);
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair
#define clr clear()
#define inf (1<<30)
#define ins insert
#define xx first
#define yy second
#define eps 1e-9

typedef long long i64;
typedef unsigned long long ui64;
typedef string st;
typedef vector<int> vi;
typedef vector<st> vs;
typedef map<int,int> mii;
typedef map<st,int> msi;
typedef set<int> si;
typedef set<st> ss;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

#define mx 0

bool contain(vi &a,vi &b){
    int idx=0;
    rep(i,sz(b)){
        while(idx<sz(a) && a[idx]<b[i]) idx++;
        if(idx==sz(a) || a[idx]!=b[i]) return false;
    }
    return true;
}

int main(){
    ios_base::sync_with_stdio(0);
    int n;
    vector<vi> C;
    cin>>n;
    rep(i,n){
        int m;
        cin>>m;
        vi tmp;
        while(m--){
            int temp;
            cin>>temp;
            tmp.pb(temp);
        }
        sort(all(tmp));
        C.pb(tmp);
    }
    rep(i,n){
        bool ok=1;
        rep(j,n) if(i!=j) {
            if(contain(C[i],C[j])){
                ok=0;
                break;
            }
        }
        if(ok) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


/*
 * Bidhan Roy
 * University of Dhaka
 */

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

#define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++)
#define sgn(x,y) ((x)+eps<(y)?-1:((x)>eps+(y)?1:0))
#define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
#define mem(x,val) memset((x),(val),sizeof(x));
#define rite(x) freopen(x,"w",stdout);
#define read(x) freopen(x,"r",stdin);
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair
#define clr clear()
#define inf (1<<30)
#define ins insert
#define xx first
#define yy second
#define eps 1e-9

typedef long long i64;
typedef unsigned long long ui64;
typedef string st;
typedef vector<int> vi;
typedef vector<st> vs;
typedef map<int,int> mii;
typedef map<st,int> msi;
typedef set<int> si;
typedef set<st> ss;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

#define mx 0

struct data{
    char F,L;
    i64 oc;
    void print(){
        cout<<F<<" "<<oc<<" "<<L<<endl;
    }
};

data get( st &inp ,int ttt){
    data ret;
    ret.oc=0;
    ret.F=inp[0];
    ret.L=inp[ sz(inp)-1 ];
    ret.oc=ttt;
    //rep(i,sz(inp)) ret.oc+= (inp.substr(i,2)=="AC");
    return ret;
}

data add( data a, data b){
    //cout<<"in add "<<endl;
    //a.print();
    //b.print();
    data ret;
    ret.oc=a.oc+b.oc;
    ret.F=a.F;
    ret.L=b.L;
    if(a.L=='A' && b.F=='C') ret.oc++;
    return ret;
}

int k,x,n,m;

bool call( st &a, st &b ,int ooo,int xxx){
    //cout<<a<<" test "<<b<<endl;
    vector<data> s;
    s.pb(get(a,ooo));
    s.pb(get(b,xxx));
    for(int i=2; i<k; i++){
        s.pb(add(s[i-2],s[i-1]));
        if(s.back().oc>x) return false;
    }
    //s[0].print();
    //s[1].print();
    //s[2].print();

    if(s.back().oc==x){
        rep(i,sz(a)) if(a[i]==' ') a[i]='X';
        rep(i,sz(b)) if(b[i]==' ') b[i]='X';
        cout<<a<<endl<<b<<endl;
        return true;
    }
    return false;
}

st form(st &inp,int oc){
    st ret=inp;
    if(!oc) return ret;
    if(inp[0]=='X' && inp[sz(inp)-1]=='X'){
        for(int i=1; i<sz(inp)-1; i+=2) {
            if(!oc) break;
            ret[i]='A', ret[i+1]='C', oc--;
        }
        return ret;
    }
    st my="AC";
    if(inp[0]=='A'){
        //cout<<"here"<<endl;
        for(int i=1, idx=1; i<sz(inp)-1; i++, idx=!idx) {
            if(!oc) break;
            ret[i]=my[idx];
            if(my[idx]=='C') oc--;
        }
        return ret;
    }
    if(inp[sz(inp)-1]=='C'){
        for(int i=sz(inp)-2, idx=0; i>0; i--, idx=!idx) {
            if(!oc) break;
            ret[i]=my[idx];
            if(my[idx]=='A') oc--;
        }
        return ret;
    }
    for(int i=1; i<sz(inp)-1; i+=2) {
        if(!oc) break;
        ret[i]='A', ret[i+1]='C', oc--;
    }
    return ret;
}

int cc(st &inp){
    int ret=0;
    rep(i,sz(inp)-1){
        ret+=( inp[i]=='A' && inp[i+1]=='C');
    }
    return ret;
}

void solve(){
                    st a,b;
                rep(i,n) a+=' ';
                rep(j,m) b+=' ';
            st my="ACX";
        rep(sa,3) rep(ea,3) {
            rep(sb,3) rep(eb,3) {

                a[0]=my[sa], a[n-1]=my[ea];
                b[0]=my[sb], b[m-1]=my[eb];
                for(int ocA=0; ocA<=(n>>1); ocA++){
                    for(int ocB=0; ocB<=(m>>1); ocB++){
                        st tA=form(a,ocA);
                        st tB=form(b,ocB);
                        int aaa=cc(tA);
                        int bbb=cc(tB);
                        if(aaa!=ocA) continue;
                        if(bbb!=ocB) continue;
                        if(call(tA,tB,aaa,bbb)) return ;
                    }
                }
            }
        }
        cout<<"Happy new year!"<<endl;

}

int main(){
    //st test;
    //int oc;
    //while(cin>>test>>oc) cout<<form(test,oc)<<endl;
    ios_base::sync_with_stdio(0);
    while(1){
        k=50;
        x=2000000000;
        n=100;
        m=100;
        solve();
        break;
    }
    return 0;
}