#include<cstdio>
#include<algorithm>
#define maxn 500000
using namespace std;
int n,k,cnt,a[maxn],b[maxn],h1=1,h2=1,fa[maxn],lazy[maxn];
char ans[maxn];
int find(int x){
	while(x!=fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n+1;i++) fa[i]=i;
	while(1){
		if(h2==n) break;
		while(find(b[h2])!=find(a[h1]) && h1<=n) merge(a[h1],a[h1+1]),h1++;
		if(h1==h2) lazy[h2+1]=1,cnt++;
		h2++;
	}
	if(cnt<k) printf("NO\n"),exit(0);
	printf("YES\n");
	for(int i=1,now=0;i<=n;i++) {
		now=min(now+lazy[i],k-1);
		ans[a[i]]='a'+now;
	}
	for(int i=1;i<=n;i++) printf("%c",ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#define maxn 500000
using namespace std;
int n,k,cnt,a[maxn],b[maxn],h1=1,h2=1,fa[maxn],lazy[maxn];
int find(int x){
	while(x!=fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n+1;i++) fa[i]=i;
	while(1){
		if(h2==n) break;
		while(find(b[h2])!=find(a[h1]) && h1<=n) merge(a[h1],a[h1+1]),h1++;
		if(h1==h2) lazy[h2+1]=1;
		cnt++; h2++;
	}
	if(cnt<k) printf("NO\n"),exit(0);
	printf("YES\n");
	for(int i=1,now=0;i<=n;i++) {
		now=min(now+lazy[i],k);
		printf("%c",'a'+now);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#define maxn 500000
using namespace std;
int n,k,cnt,a[maxn],b[maxn],h1=1,h2=1,fa[maxn],lazy[maxn];
char ans[maxn];
int find(int x){
	while(x!=fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n+1;i++) fa[i]=i;
	while(1){
		if(h2==n) break;
		while(find(b[h2])!=find(a[h1]) && h1<=n) merge(a[h1],a[h1+1]),h1++;
		if(h1==h2) lazy[h2+1]=1;
		cnt++; h2++;
	}
	if(cnt<k) printf("NO\n"),exit(0);
	printf("YES\n");
	for(int i=1,now=0;i<=n;i++) {
		now=min(now+lazy[i],k);
		ans[a[i]]='a'+now;
	}
	for(int i=1;i<=n;i++) printf("%c",ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 500000
using namespace std;
int n,k,cnt,a[maxn],b[maxn],h1=1,h2=1,fa[maxn],lazy[maxn];
char ans[maxn];
int find(int x){
	while(x!=fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void merge(int x,int y){fa[find(x)]=find(y);}
int main(){
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n+1;i++) fa[i]=i;
	while(1){
		if(h2==n+1) break;
		while(find(b[h2])!=find(a[h1]) && h1<=n) merge(a[h1],a[h1+1]),h1++;
		if(h1==h2) lazy[h2+1]=1,cnt++;
		h2++;
	}
	if(cnt<k) printf("NO\n"),exit(0);
	printf("YES\n");
	for(int i=1,now=0;i<=n;i++){
		now=min(now+lazy[i],k-1);
		ans[a[i]]='a'+now;
	}
	for(int i=1;i<=n;i++) printf("%c",ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#define maxn 500000
using namespace std;
int n,k,cnt,a[maxn],b[maxn],h1=1,h2=1,fa[maxn],lazy[maxn];
char ans[maxn];
int find(int x){
	while(x!=fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n+1;i++) fa[i]=i;
	while(1){
		if(h2==n) break;
		while(find(b[h2])!=find(a[h1]) && h1<=n) merge(a[h1],a[h1+1]),h1++;
		if(h1==h2) lazy[h2+1]=1;
		cnt++; h2++;
	}
	if(cnt<k) printf("NO\n"),exit(0);
	printf("YES\n");
	for(int i=1,now=0;i<=n;i++) {
		now=min(now+lazy[i],k-1);
		ans[a[i]]='a'+now;
	}
	for(int i=1;i<=n;i++) printf("%c",ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#define maxn 500000
using namespace std;
int n,k,cnt,a[maxn],b[maxn],h1=1,h2=1,fa[maxn],lazy[maxn];
char ans[maxn];
int find(int x){
	while(x!=fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) scanf("%d",&b[i]);
	for(int i=1;i<=n+1;i++) fa[i]=i;
	while(1)
	{
		if(h2==n+1) break;
		while(find(b[h2])!=find(a[h1]) && h1<=n) merge(a[h1],a[h1+1]),h1++;
		if(h1==h2) lazy[h2+1]=1,cnt++;
		h2++;
	}
	if(cnt<k) printf("NO\n"),exit(0);
	printf("YES\n");
	for(int i=1,now=0;i<=n;i++) {
		now=min(now+lazy[i],k-1);
		ans[a[i]]='a'+now;
	}
	for(int i=1;i<=n;i++) printf("%c",ans[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> par;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define maxn 200010

vector<int>g[maxn],h[maxn];
int num[maxn];
vector<int>top;
int mk[maxn],cnt=0;
int orden[maxn];

void dfs(int nod){
    mk[nod] = 1;

    for(auto i : g[nod]){
        if(!mk[i]){
            dfs(i);
        }
    }

    top.pb(nod);
}

void kosaraju(int nod){
    mk[nod] = 1;
    num[nod] = cnt;

    for(auto i : h[nod]){
        if(!mk[i]){
            kosaraju(i);
        }
    }
}

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

    int n,k;
    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    string ans = "";

    for(int i=1;i<=n;i++){
        if(!mk[i]){
            dfs(i);
        }
    }

    reverse(top.begin(),top.end());
    memset(mk,false,sizeof(mk));

    for(auto i : top){
        if(!mk[i]){
            cnt++;
            kosaraju(i);
        }
    }

    memset(mk,false,sizeof(mk));

    if(cnt < k ){
        cout << "NO" << '\n';
        return 0;
    }

    for(int i=1;i<=n;i++){
        cout << (char)('a' + min(25,num[i]-1));
    }

}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> par;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define int long long
#define maxn 200010

vector<int>g[maxn],h[maxn];
int num[maxn];
vector<int>top;
int mk[maxn],cnt=0;
int orden[maxn];

void dfs(int nod){
    mk[nod] = 1;

    for(auto i : g[nod]){
        if(!mk[i]){
            dfs(i);
        }
    }

    top.pb(nod);
}

void kosaraju(int nod){
    mk[nod] = 1;
    num[nod] = cnt;

    for(auto i : h[nod]){
        if(!mk[i]){
            kosaraju(i);
        }
    }
}
vector<int>to;
void topo(int nod){
    mk[num[nod]]=1;
    for(auto i : g[nod]){
        if(!mk[num[i]]){
            topo(i);
        }
    }
    to.pb(num[nod]);
}
char p[30];
main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,k;
    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    string ans = "";

    for(int i=1;i<=n;i++){
        if(!mk[i]){
            dfs(i);
        }
    }

    reverse(top.begin(),top.end());
    memset(mk,false,sizeof(mk));

    for(auto i : top){
        if(!mk[i]){
            cnt++;
            kosaraju(i);
        }
    }

    memset(mk,false,sizeof(mk));

    if(cnt < k){
        cout << "NO" << '\n';
        return 0;
    }

    for(auto i : top){
        if(!mk[num[i]]){
            topo(i);
        }
    }

    reverse(to.begin(),to.end());
    int cont = 0;

    for(auto i : to){
        if(cont == 26){
            cont--;
        }
        p[i] = 97 + cont;
        cont++;
    }

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

    cout << "YES" << '\n';
    cout << ans << '\n';

}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> par;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define int long long
#define maxn 200010

vector<int>g[maxn],h[maxn];
int num[maxn];
vector<int>top;
int mk[maxn],cnt=0;
int orden[maxn];

void dfs(int nod){
    mk[nod] = 1;

    for(auto i : g[nod]){
        if(!mk[i]){
            dfs(i);
        }
    }

    top.pb(nod);
}

void kosaraju(int nod){
    mk[nod] = 1;
    num[nod] = cnt;

    for(auto i : h[nod]){
        if(!mk[i]){
            kosaraju(i);
        }
    }
}

void topo(int nod){
    mk[num[nod]]=1;
    for(auto i : g[nod]){
        if(!mk[num[i]]){
            topo(i);
        }
    }
    top.pb(nod);
}
char p[30];
main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,k;
    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    string ans = "";

    for(int i=1;i<=n;i++){
        if(!mk[i]){
            dfs(i);
        }
    }

    reverse(top.begin(),top.end());
    memset(mk,false,sizeof(mk));

    for(auto i : top){
        if(!mk[i]){
            cnt++;
            kosaraju(i);
        }
    }

    memset(mk,false,sizeof(mk));
    top.clear();

    if(cnt < k){
        cout << "NO" << '\n';
        return 0;
    }

    for(int i=1;i<=n;i++){
        if(!mk[num[i]]){
            topo(i);
        }
    }

    reverse(top.begin(),top.end());
    int cont = 0;

    for(auto i : top){
        if(cont == 26){
            cont--;
        }
        p[i] = 97 + cont;
        cont++;
    }

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

    cout << "YES" << '\n';
    cout << ans << '\n';

}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> par;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define int long long
#define maxn 200010

vector<int>g[maxn],h[maxn];
int num[maxn];
vector<int>top;
int mk[maxn],cnt=0;
int orden[maxn];

void dfs(int nod){
    mk[nod] = 1;

    for(auto i : g[nod]){
        if(!mk[i]){
            dfs(i);
        }
    }

    top.pb(nod);
}

void kosaraju(int nod){
    mk[nod] = 1;
    num[nod] = cnt;

    for(auto i : h[nod]){
        if(!mk[i]){
            kosaraju(i);
        }
    }
}
vector<int>to;
void topo(int nod){
    mk[num[nod]]=1;
    for(auto i : g[nod]){
        if(!mk[num[i]]){
            topo(i);
        }
    }
    to.pb(num[nod]);
}
char p[30];
main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,k;
    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    string ans = "";

    for(int i=1;i<=n;i++){
        if(!mk[i]){
            dfs(i);
        }
    }

    reverse(top.begin(),top.end());
    memset(mk,false,sizeof(mk));

    for(auto i : top){
        if(!mk[i]){
            cnt++;
            kosaraju(i);
        }
    }

    memset(mk,false,sizeof(mk));

    if(cnt < k ){
        cout << "NO" << '\n';
        return 0;
    }

    for(int i=1;i<=n;i++){
        if(!mk[num[i]]){
            topo(i);
        }
    }

    reverse(to.begin(),to.end());
    int cont = 0;

    for(auto i : to){
        if(cont == k){
            cont--;
        }
        p[i] = 97 + cont;
        cont++;
    }

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

    cout << "YES" << '\n';
    cout << ans << '\n';

}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> par;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define int long long
#define maxn 200010

vector<int>g[maxn],h[maxn];
int num[maxn];
vector<int>top;
int mk[maxn],cnt=0;
int orden[maxn];

void dfs(int nod){
    mk[nod] = 1;

    for(auto i : g[nod]){
        if(!mk[i]){
            dfs(i);
        }
    }

    top.pb(nod);
}

void kosaraju(int nod){
    mk[nod] = 1;
    num[nod] = cnt;

    for(auto i : h[nod]){
        if(!mk[i]){
            kosaraju(i);
        }
    }
}

void topo(int nod){
    mk[num[nod]]=1;
    for(auto i : g[nod]){
        if(!mk[num[i]]){
            topo(i);
        }
    }
    top.pb(nod);
}
char p[30];
main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,k;
    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    string ans = "";

    for(int i=1;i<=n;i++){
        if(!mk[i]){
            dfs(i);
        }
    }

    reverse(top.begin(),top.end());
    memset(mk,false,sizeof(mk));

    for(auto i : top){
        if(!mk[i]){
            cnt++;
            kosaraju(i);
        }
    }

    memset(mk,false,sizeof(mk));
    top.clear();

    if(cnt < k or cnt > 26){
        cout << "NO" << '\n';
        return 0;
    }

    for(int i=1;i<=n;i++){
        if(!mk[num[i]]){
            topo(i);
        }
    }

    reverse(top.begin(),top.end());
    int cont = 0;

    for(auto i : top){
        p[i] = 97 + cont;
        cont++;
    }

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

    cout << "YES" << '\n';
    cout << ans << '\n';

}


#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> par;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define maxn 200010

vector<int>g[maxn],h[maxn];
int num[maxn];
vector<int>top;
int mk[maxn],cnt=0;
int orden[maxn];

void dfs(int nod){
    mk[nod] = 1;

    for(auto i : g[nod]){
        if(!mk[i]){
            dfs(i);
        }
    }

    top.pb(nod);
}

void kosaraju(int nod){
    mk[nod] = 1;
    num[nod] = cnt;

    for(auto i : h[nod]){
        if(!mk[i]){
            kosaraju(i);
        }
    }
}

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

    int n,k;
    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    for(int i=1;i<=n;i++){
        cin >> orden[i];
        if(i >= 2){
            g[orden[i-1]].pb(orden[i]);
            h[orden[i]].pb(orden[i-1]);
        }
    }

    string ans = "";

    for(int i=1;i<=n;i++){
        if(!mk[i]){
            dfs(i);
        }
    }

    reverse(top.begin(),top.end());
    memset(mk,false,sizeof(mk));

    for(auto i : top){
        if(!mk[i]){
            cnt++;
            kosaraju(i);
        }
    }

    memset(mk,false,sizeof(mk));

    if(cnt < k ){
        cout << "NO" << '\n';
        return 0;
    }

    cout << "YES" << '\n';
    for(int i=1;i<=n;i++){
        cout << (char)('a' + min(25,num[i]-1));
    }

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb emplace_back
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define chkmin(x, y) x = min(x, y)
#define chkmax(x, y) x = max(x, y)
#define x first
#define y second

const int MAXN = 2e5 + 1;

vector<int> g[MAXN], gr[MAXN];

int comp[MAXN], used[MAXN];

vector<int> ord;
void doTopsort(int v) {
    used[v] = 1;
    for(int to : gr[v])
        if (!used[to]) doTopsort(to);
    ord.pb(v);
}

void dfs(int v, int c) {
    comp[v] = c;
    for(int to : g[v]) {
        if (!comp[to]) dfs(to, c);
    }
}

void run() {
    int n, k;
    cin >> n >> k;
    int la = -1;
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        if (!used[i]) doTopsort(i);
    }
    reverse(all(ord));
    int t = 1;
    for(int x : ord) {
        if (!comp[x]) dfs(x, t++);
    }

    if (k >= t) {
        cout << "NO\n";
        return;
    }
    char cu = 'a';
    string re(n, '#');
    fill(used, used + n, 0);
    reverse(all(ord));
    k--;
    for(int x : ord) {
        if (!used[comp[x]] && k) {
            k--;
            used[comp[x]] = int(cu);
            cu++;
        }
        if (!used[comp[x]]) used[comp[x]] = cu;
        re[x] = char(used[comp[x]]);
    }
    cout << "YES\n" << re;
}

signed main() {
#ifdef MIKU
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("pushabox.in", "r", stdin);
    //freopen("pushabox.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    run();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb emplace_back
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define chkmin(x, y) x = min(x, y)
#define chkmax(x, y) x = max(x, y)
#define x first
#define y second

const int MAXN = 2e5 + 1;

vector<int> g[MAXN], gr[MAXN];

int comp[MAXN], used[MAXN];

vector<int> ord;
void doTopsort(int v) {
    used[v] = 1;
    for(int to : gr[v])
        if (!used[to]) doTopsort(to);
    ord.pb(v);
}

void dfs(int v, int c) {
    comp[v] = c;
    for(int to : g[v]) {
        if (!comp[to]) dfs(to, c);
    }
}

void run() {
    int n, k;
    cin >> n >> k;
    int la = -1;
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        if (!used[i]) doTopsort(i);
    }
    reverse(all(ord));
    int t = 1;
    for(int x : ord) {
        if (!comp[x]) dfs(x, t++);
    }

    if (k >= t) {
        cout << "NO\n";
        return;
    }
    char cu = 'a';
    string re(n, '#');
    fill(used, used + n, 0);
    reverse(all(ord));
    for(int x : ord) {
        if (!used[comp[x]] && k) {
            k--;
            used[comp[x]] = int(cu);
            cu++;
        }
        re[x] = char(used[comp[x]]);
    }
    cout << "YES\n" << re;
}

signed main() {
#ifdef MIKU
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("pushabox.in", "r", stdin);
    //freopen("pushabox.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    run();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb emplace_back
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define chkmin(x, y) x = min(x, y)
#define chkmax(x, y) x = max(x, y)
#define x first
#define y second

const int MAXN = 2e5 + 1;

vector<int> g[MAXN], gr[MAXN];

int comp[MAXN], used[MAXN];

vector<int> ord;
void doTopsort(int v) {
    used[v] = 1;
    for(int to : gr[v])
        if (!used[to]) doTopsort(to);
    ord.pb(v);
}

void dfs(int v, int c) {
    comp[v] = c;
    for(int to : g[v]) {
        if (!comp[to]) dfs(to, c);
    }
}

void run() {
    int n, k;
    cin >> n >> k;
    int la = -1;
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        if (!used[i]) doTopsort(i);
    }
    reverse(all(ord));
    int t = 1;
    for(int x : ord) {
        if (!comp[x]) dfs(x, t++);
    }

    if (k >= t) {
        cout << "NO\n";
        return;
    }
    char cu = 'a';
    string re(n, '#');
    fill(used, used + n, 0);
    reverse(all(ord));
    for(int x : ord) {
        if (!used[comp[x]] && k) {
            k--;
            used[comp[x]] = int(cu);
            cu++;
        }
        re[x] = char(used[comp[x]]);
    }
    cout << re;
}

signed main() {
#ifdef MIKU
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("pushabox.in", "r", stdin);
    //freopen("pushabox.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    run();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb emplace_back
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define chkmin(x, y) x = min(x, y)
#define chkmax(x, y) x = max(x, y)
#define x first
#define y second

const int MAXN = 2e5 + 1;

vector<int> g[MAXN], gr[MAXN];

int comp[MAXN], used[MAXN];

vector<int> ord;
void doTopsort(int v) {
    used[v] = 1;
    for(int to : gr[v])
        if (!used[to]) doTopsort(to);
    ord.pb(v);
}

void dfs(int v, int c) {
    comp[v] = c;
    for(int to : g[v]) {
        if (!comp[to]) dfs(to, c);
    }
}

void run() {
    int n, k;
    cin >> n >> k;
    int la = -1;
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        --x;
        if (i)
            g[la].pb(x), gr[x].pb(la);
        la = x;
    }
    for(int i = 0; i < n; i++) {
        if (!used[i]) doTopsort(i);
    }
    reverse(all(ord));
    int t = 1;
    for(int x : ord) {
        if (!comp[x]) dfs(x, t++);
    }

    if (k >= t) {
        cout << "NO\n";
        return;
    }
    char cu = 'a';
    string re(n, '#');
    fill(used, used + n, 0);
    reverse(all(ord));
    k--;
    for(int x : ord) {
        if (!used[comp[x]] && k) {
            k--;
            used[comp[x]] = int(cu);
            cu++;
        }
        re[x] = char(used[comp[x]]);
    }
    cout << "YES\n" << re;
}

signed main() {
#ifdef MIKU
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("pushabox.in", "r", stdin);
    //freopen("pushabox.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    run();
}


#include <iostream>
#include <cstdio>

using namespace std;

int n, k;
int a[200005];
int b[200005];
int c[200005];
int d[200005];
int e[200005];
int cnt = 1;

int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        c[a[i]] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &b[i]);
        d[min(c[b[i]], i)]++, d[max(c[b[i]], i)]--;
    }
    for(int i = 1; i <= n; i++)
    {
        d[i] += d[i - 1];
        e[i] = cnt;
        if(d[i] == 0) cnt++;
    }
    if(cnt <= k)
    {
        printf("NO");
        return 0;
    }
    printf("YES\n");
    for(int i = 1; i <= n; i++)
    {
        printf("%c", 'a' - 1 + e[c[i]]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int n, k;
int a[200005];
int b[200005];
int c[200005];
int d[200005];
int e[200005];
int cnt = 1;

int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        c[a[i]] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &b[i]);
        d[min(c[b[i]], i)]++, d[max(c[b[i]], i)]--;
    }
    for(int i = 1; i <= n; i++)
    {
        d[i] += d[i - 1];
        e[i] = cnt;
        if(d[i] == 0) cnt++;
    }
    if(cnt <= k)
    {
        printf("NO");
        return 0;
    }
    printf("YES\n");
    for(int i = 1; i <= n; i++)
    {
        if('a' - 1 + e[c[i]] > 'z') printf("z");
        else printf("%c", 'a' - 1 + e[c[i]]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int n, k;
int a[200005];
int b[200005];
int c[200005];
int d[200005];
int e[200005];
int cnt = 1;

int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        c[a[i]] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &b[i]);
        d[min(c[b[i]], i)]++, d[max(c[b[i]], i)]--;
    }
    for(int i = 1; i <= n; i++)
    {
        d[i] += d[i - 1];
        e[i] = cnt;
        if(d[i] == 0) cnt++;
    }
    if(cnt < k)
    {
        printf("NO");
        return 0;
    }
    printf("YES\n");
    for(int i = 1; i <= n; i++)
    {
        printf("%c", 'a' - 1 + e[c[i]]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int n, k;
int a[200005];
int b[200005];
int c[200005];
int d[200005];
int e[200005];
int cnt = 1;

int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        c[a[i]] = i;
    }
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &b[i]);
        d[min(c[b[i]], i)]++, d[max(c[b[i]], i)]--;
    }
    for(int i = 1; i <= n; i++)
    {
        d[i] += d[i - 1];
        e[i] = cnt;
        if(d[i] == 0) cnt++;
    }
    if(cnt <= k)
    {
        printf("NO");
        return 0;
    }
    printf("YES\n");
    for(int i = 1; i <= n; i++)
    {
        if('a' - 1 + e[c[i]] > 'z') printf("z");
        printf("%c", 'a' - 1 + e[c[i]]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Cerr cerr << "\nTest: "
#define endl "\n"
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fastio { ios_base::sync_with_stdio(false), cin.tie(NULL); }
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

#ifdef RICARDO
	#define bug(x) cerr << #x << " = " << (x) << endl;
	#define block if(1)
#else
	#define bug(x) "IDINGO"
	#define block if(0)
#endif

template<class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

//=================================================CODE=================================================//
const int N = 2e5 + 5;
vi G[N];
int n, num[N], low[N], cnt, ans, id[N];
vi st;

void dfs(int u) {
	st.pb(u);
	num[u] = low[u] = ++cnt;
	forn(i, G[u].size()) {
	 	int v = G[u][i];
	 	if(num[v]) mini(low[u], num[v]);
	 	else {
	 	 	dfs(v);
	 	 	mini(low[u], low[v]);
	 	}
	}

	if(low[u] == num[u]) {
		ans++;
		int v;
		do {
			v = st.back(); st.pop_back();
			id[v] = ans;
		} while(v != u);
	}
}

signed main()
{
	//ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef RICARDO
	freopen("i", "r", stdin);
#endif
	int k;
	cin >> n >> k;
	vi a(n), b(n);
	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	forn(i, n) {
	 	cin >> b[i];
	 	b[i]--;
	 	if(i > 0) G[b[i - 1]].pb(b[i]);
	}

	dfs(a[0]);
/*
	bug(ans);
	block {
	 	forn(i, n) cerr << id[i] << endl;
	}
*/
	if(ans < k) puts("NO");
	else {
		puts("YES");
		vi check(n);
		forn(i, n) {
			check[i] = ans - id[i];
			cout << char('a' + min((ans - id[i]), k - 1));
		}

		forn(i, n) if(i > 0) {
			assert(check[a[i - 1]] <= check[a[i]]);
			assert(check[b[i - 1]] <= check[b[i]]);
		}
	}
#ifdef RICARDO
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>

using namespace std;

#define Cerr cerr << "\nTest: "
#define endl "\n"
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fastio { ios_base::sync_with_stdio(false), cin.tie(NULL); }
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

#ifdef RICARDO
	#define bug(x) cerr << #x << " = " << (x) << endl;
	#define block if(1)
#else
	#define bug(x) "IDINGO"
	#define block if(0)
#endif

template<class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

//=================================================CODE=================================================//
const int N = 2e5 + 5;
vi G[N];
int n, num[N], low[N], cnt, ans, id[N];
vi st;

void dfs(int u) {
	st.pb(u);
	num[u] = low[u] = ++cnt;
	forn(i, G[u].size()) {
	 	int v = G[u][i];
	 	if(num[v]) mini(low[u], num[v]);
	 	else {
	 	 	dfs(v);
	 	 	mini(low[u], low[v]);
	 	}
	}

	if(low[u] == num[u]) {
		ans++;
		int v;
		do {
			v = st.back(); st.pop_back();
			id[v] = ans;
		} while(v != u);
	}
}

signed main()
{
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef RICARDO
	freopen("i", "r", stdin);
#endif
	int k;
	cin >> n >> k;
	vi a(n);
	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	dfs(a[0]);

	bug(ans);
	block {
	 	forn(i, n) cerr << id[i] << endl;
	}

	if(ans < k) puts("NO");
	else {
		puts("YES");
		forn(i, n) cout << char('a' + (ans - id[i]));
	}
#ifdef RICARDO
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>

using namespace std;

#define Cerr cerr << "\nTest: "
#define endl "\n"
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fastio { ios_base::sync_with_stdio(false), cin.tie(NULL); }
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

#ifdef RICARDO
	#define bug(x) cerr << #x << " = " << (x) << endl;
	#define block if(1)
#else
	#define bug(x) "IDINGO"
	#define block if(0)
#endif

template<class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

//=================================================CODE=================================================//
const int N = 2e5 + 5;
vi G[N];
int n, num[N], low[N], cnt, ans, id[N];
vi st;

void dfs(int u) {
	st.pb(u);
	num[u] = low[u] = ++cnt;
	forn(i, G[u].size()) {
	 	int v = G[u][i];
	 	if(num[v]) mini(low[u], num[v]);
	 	else {
	 	 	dfs(v);
	 	 	mini(low[u], low[v]);
	 	}
	}

	if(low[u] == num[u]) {
		ans++;
		int v;
		do {
			v = st.back(); st.pop_back();
			id[v] = ans;
		} while(v != u);
	}
}

signed main()
{
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef RICARDO
	freopen("i", "r", stdin);
#endif
	int k;
	cin >> n >> k;
	vi a(n);
	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	dfs(a[0]);

	bug(ans);
	block {
	 	forn(i, n) cerr << id[i] << endl;
	}

	if(ans < k) puts("NO");
	else {
		forn(i, n) cout << char('a' + (ans - id[i]));
	}
#ifdef RICARDO
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>

using namespace std;

#define Cerr cerr << "\nTest: "
#define endl "\n"
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fastio { ios_base::sync_with_stdio(false), cin.tie(NULL); }
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

#ifdef RICARDO
	#define bug(x) cerr << #x << " = " << (x) << endl;
	#define block if(1)
#else
	#define bug(x) "IDINGO"
	#define block if(0)
#endif

template<class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

//=================================================CODE=================================================//
const int N = 2e5 + 5;
vi G[N];
int n, num[N], low[N], cnt, ans, id[N];
vi st;

void dfs(int u) {
	st.pb(u);
	num[u] = low[u] = ++cnt;
	forn(i, G[u].size()) {
	 	int v = G[u][i];
	 	if(num[v]) mini(low[u], num[v]);
	 	else {
	 	 	dfs(v);
	 	 	mini(low[u], low[v]);
	 	}
	}

	if(low[u] == num[u]) {
		ans++;
		int v;
		do {
			v = st.back(); st.pop_back();
			id[v] = ans;
		} while(v != u);
	}
}

signed main()
{
	//ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef RICARDO
	freopen("i", "r", stdin);
#endif
	int k;
	cin >> n >> k;
	vi a(n);
	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	dfs(a[0]);
/*
	bug(ans);
	block {
	 	forn(i, n) cerr << id[i] << endl;
	}
*/
	if(ans < k) puts("NO");
	else {
		puts("YES");
		forn(i, n) cout << char('a' + (ans - id[i]));
	}
#ifdef RICARDO
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>

using namespace std;

#define Cerr cerr << "\nTest: "
#define endl "\n"
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fastio { ios_base::sync_with_stdio(false), cin.tie(NULL); }
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

#ifdef RICARDO
	#define bug(x) cerr << #x << " = " << (x) << endl;
	#define block if(1)
#else
	#define bug(x) "IDINGO"
	#define block if(0)
#endif

template<class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

//=================================================CODE=================================================//
const int N = 2e5 + 5;
vi G[N];
int n, num[N], low[N], cnt, ans, id[N];
vi st;

void dfs(int u) {
	st.pb(u);
	num[u] = low[u] = ++cnt;
	forn(i, G[u].size()) {
	 	int v = G[u][i];
	 	if(num[v]) mini(low[u], num[v]);
	 	else {
	 	 	dfs(v);
	 	 	mini(low[u], low[v]);
	 	}
	}

	if(low[u] == num[u]) {
		ans++;
		int v;
		do {
			v = st.back(); st.pop_back();
			id[v] = ans;
		} while(v != u);
	}
}

signed main()
{
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef RICARDO
	freopen("i", "r", stdin);
#endif
	int k;
	cin >> n >> k;
	vi a(n);
	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	forn(i, n) {
	 	cin >> a[i];
	 	a[i]--;
	 	if(i > 0) G[a[i - 1]].pb(a[i]);
	}

	dfs(a[0]);
/*
	bug(ans);
	block {
	 	forn(i, n) cerr << id[i] << endl;
	}
*/
	if(ans < k) puts("NO");
	else {
		puts("YES");
		forn(i, n) cout << char('a' + (ans - id[i]));
	}
#ifdef RICARDO
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
int arr3[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int temp=0;
  for(int i=0;i<n;i++)
  {
      temp = max(temp,sorted[arr1[i]].second);
      arr3[i]=temp;
  }
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     int j=sorted[arr1[i]].second;
     int x=arr3[i];
     if(!vis[j] && x<=j){
       if(c < 'a')c++;
       ans[arr1[i]] =c;
       c--;
       cnt++;
     }
     else if (x>j)
     {
         if(!vis[x])
         {
            if(c < 'a')c++;
            ans[arr2[x]] =c;
            c--;
            cnt++;
            vis[x]=1;
         }
         ans[arr2[j]] = ans[arr2[arr3[i]]];
     }
       for(j = sorted[arr1[i]].second ; j < n ; ++j){
        if(!vis[j])break;
        ans[arr2[j]] =ans[arr1[i]];
        vis[j] = 1 ;
       }
  }
  if(cnt >= k){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
bool vis[200005];
char res[200005];
int arr[200005];
int main()
{
	int n,m;
	cin>>n>>m;
	memset(vis,0,sizeof vis);
	int maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		arr[a]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		maxi=max(maxi,arr[a]);
		arr[a]=maxi;
	}
	char k='a';
	int x=0;
	for(int i=0;i<n;i++)
	{
		if(!vis[i]){if(k>'z')k--;res[i]=k++;}
		x=arr[i];
		for(int j=x;j>=i;j--)
		{
		    if(vis[j])break;
			res[j]=res[i];
			vis[j]=1;
		}
	}
	if(k-'a'<m)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
int arr3[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int temp=0;
  for(int i=0;i<n;i++)
  {
      temp = max(temp,sorted[arr1[i]].second);
      arr3[i]=temp;
  }
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     int j=sorted[arr1[i]].second;
     int x=arr3[i];
     if(!vis[j] && x<=j){
       if(c < 'a')c++;
       ans[arr1[i]] =c;
       c--;
       cnt++;
     }
     else if (x>j)
     {
         if(!vis[x])
         {
            if(c < 'a')c++;
            ans[arr2[x]] =c;
            c--;
            cnt++;
         }
         ans[arr2[j]] = ans[arr2[x]];
     }
       for(j = sorted[arr1[i]].second ; j < n ; ++j){
        if(!vis[j])break;
        ans[arr2[j]] =ans[arr1[i]];
        vis[j] = 1 ;
       }
  }
  if(cnt >= k){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
bool vis[200005];
char res[200005];
int arr[200005];
int main()
{
	int n,m;
	cin>>n>>m;
	memset(vis,0,sizeof vis);
	int maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		arr[a]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		maxi=max(maxi,arr[a]);
		arr[a]=maxi;
	}
	char k='a';
	int x=0;
	for(int i=0;i<n;i++)
	{
		if(!vis[i]){if(k>'z')k--;res[i]=k++;}
		for(int j=max(x,i);j<=arr[i];j++)
		{
		    if(vis[j])x--;
			x++;
			res[j]=res[i];
			vis[j]=1;
		}
	}
	if(k-'a'<m)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include <bits/stdc++.h>
using namespace std;
bool vis[200005];
char res[200005];
int arr[200005];
int main()
{
	int n,m;
	cin>>n>>m;
	memset(vis,0,sizeof vis);
	int maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		arr[a]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		maxi=max(maxi,arr[a]);
		arr[a]=maxi;
	}
	char k='a';
	int x=0;
	for(int i=0;i<n;i++)
	{
		if(!vis[i]){if(k>'z')k--;res[i]=k++;}
		x=arr[i];
		for(int j=x;j>=i,!vis[j];j--)
		{
			res[j]=res[i];
			vis[j]=1;
		}
	}
	if(k-'a'<m)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include <bits/stdc++.h>
using namespace std;
bool vis[200005];
char res[200005];
int arr[200005];
int main()
{
	int n,m;
	cin>>n>>m;
	memset(vis,0,sizeof vis);
	int maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		arr[a]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		maxi=max(a,maxi);
		maxi=max(maxi,arr[a]);
		arr[a]=maxi;
	}
	char k='a';
	int x=0;
	for(int i=0;i<n;i++)
	{
		if(!vis[i]){if(k>'z')k--;res[i]=k++;}
		for(int j=max(x,i);j<=arr[i];j++)
		{
			x++;
			res[j]=res[i];
			vis[j]=1;
		}
	}
	if(k-'a'<m)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include <bits/stdc++.h>
using namespace std;
bool vis[200005];
char res[200005];
int arr[200005];
int arr1[200005];
int arr2[200005];

int main()
{
	int n,m;
	cin>>n>>m;
	memset(vis,0,sizeof vis);
	int maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		arr1[i]=a;
		maxi=max(a,maxi);
		arr[a]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		a--;
		arr2[i]=a;
		maxi=max(a,maxi);
		maxi=max(maxi,arr[a]);
		arr[a]=maxi;
	}
	char k='a';
	int x=0;
	set<char>s;
	for(int i=0;i<n;i++)
	{
		if(!vis[i]){s.insert(k);if(k>'z')k--;res[i]=k++;}
		x=arr[i];
		for(int j=x;j>=i;j--)
		{
		    if(vis[j])break;
			res[j]=res[i];
			vis[j]=1;
		}
	}
	bool p=0;
	for(int i=0;i<n-1;i++)
    {
        if(res[arr1[i]]>res[arr1[i+1]] ||res[arr2[i]]>res[arr2[i+1]])p=1;
    }
	if(s.size()<m | p)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	bool vis[n];
	for(int i=0;i<n;i++)vis[i]=0;
	char res[n+1];
	int arr[n];
	int maxi=0;

	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		maxi=max(a-1,maxi);
		arr[a-1]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		maxi=max(a-1,maxi);
		arr[a-1]=max(maxi,arr[a-1]);
	}
	char k='a';
	int x=0;
	for(int i=0;i<n;i++)
	{

		if(!vis[i])res[i]=k++;
		for(int j=max(x,i);j<=arr[i];j++)
		{
			x++;
			res[j]=res[i];
			vis[j]=1;
		}
	}
	if(k-'a'<m)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     if(!vis[arr1[i]]){
       if(c < 'a')c++;
       ans[arr1[i]] =c;
       c--;
       cnt++;
     }
       for(int j = sorted[arr1[i]].second ; j < n ; ++j){
        if(vis[arr2[j]])break;
        ans[arr2[j]] =ans[arr1[i]];
        vis[arr2[j]] = 1 ;
       }
  }
  bool p=1;
  for(int i=0;i<n-1;i++)
  {
      if(ans[arr1[i]]>ans[arr1[i+1]] || ans[arr2[i]]>ans[arr2[i+1]])p=0;
  }
  if(cnt >= k && p){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     if(!vis[arr1[i]]){
       if(c < 'a')c++;
       for(int j = sorted[arr1[i]].second ; j < n ; ++j){
        if(vis[arr2[j]])break;
        ans[arr2[j]] =c;
        vis[arr2[j]] = 1 ;
       }
       c--;
       cnt++;
     }
  }
  bool p=1;
  for(int i=0;i<n-1;i++)
  {
      if(ans[arr1[i]]>ans[arr1[i+1]] || ans[arr2[i]]>ans[arr2[i+1]])p=0;
  }
  if(cnt >= k && p){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     if(!vis[arr1[i]]){
       if(c < 'a')c++;
       for(int j = sorted[arr1[i]].second ; j < n ; ++j){
        if(! vis[arr2[j]]){
        ans[arr2[j]] =c;
        vis[arr2[j]] = 1 ;
        }
       }
       c--;
       cnt++;
     }
  }
  if(cnt >= k){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool vis[200005];
char res[200005];
	int arr[200005];
int main()
{
	int n,m;
	cin>>n>>m;

	for(int i=0;i<n;i++)vis[i]=0;

	int maxi=0;

	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		maxi=max(a-1,maxi);
		arr[a-1]=maxi;
	}
	maxi=0;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		maxi=max(a-1,maxi);
		arr[a-1]=max(maxi,arr[a-1]);
	}
	char k='a';
	int x=0;
	for(int i=0;i<n;i++)
	{

		if(!vis[i])res[i]=k++;
		for(int j=max(x,i);j<=arr[i];j++)
		{
			x++;
			res[j]=res[i];
			vis[j]=1;
		}
	}
	if(k-'a'<m)
	cout<<"NO"<<endl;
	else
	cout<<"YES\n"<<res<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     if(!vis[arr1[i]]){
       if(c < 'a')c++;
       for(int j = sorted[arr1[i]].second ; j < n ; ++j){
        if(vis[arr2[j]])break;
        ans[arr2[j]] =c;
        vis[arr2[j]] = 1 ;
       }
       c--;
       cnt++;
     }
  }
  if(cnt >= k){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[(int)1e6 + 6];
int arr2[(int)1e6 + 6];
bool vis[(int)1e6 + 6];
int arr3[(int)1e6 + 6];
pair<int,int>sorted[(int)1e6 + 6];
char ans[(int)1e6];
int main(){
  int n , k;
  cin >> n >> k;
  for(int i = 0 ; i < n; ++i){
    cin >> arr1[i];arr1[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    cin >> arr2[i];arr2[i]--;
  }
  for(int i = 0 ; i < n; ++i){
    sorted[i].first= arr2[i];
    sorted[i].second = i ;
  }
  char c = 'z';
  sort(sorted , sorted + n);
  int temp=0;
  for(int i=0;i<n;i++)
  {
      temp = max(temp,sorted[arr1[i]].second);
      arr3[i]=temp;
  }
  int cnt = 0 ;
  bool hi = 0;
  for(int i = n - 1 ; i >= 0 ; --i){
     int j=sorted[arr1[i]].second;
     int x=arr3[i];
     if(!vis[j] && x<=j){
       if(c < 'a')c++;
       ans[arr1[i]] = c;
       c--;
       cnt++;
     }
     else if (x>j)
     {
         if(!vis[x])
         {
            if(c < 'a')c++;
            ans[arr2[x]] = c;
            c--;
            cnt++;
         }
         ans[arr1[i]] = ans[arr2[x]];
     }
       for(j = sorted[arr1[i]].second ; j < n ; ++j){
        if(vis[j])break;
        ans[arr2[j]] =ans[arr1[i]];
        vis[j] = 1 ;
       }
  }
  if(cnt >= k){
    cout << "YES" <<endl;
    for(int i=0;i<n;i++)cout<<ans[i];
    cout<<endl;
  }
  else{
    cout << "NO" <<endl;
  }
  return 0 ;
}


#include <bits/stdc++.h>
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define int long long
using namespace std;
inline int read() {int x=0,f=0;char ch=getchar();while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();return f?-x:x;}
inline void write(int x) {if(x<0) x=-x,putchar('-');if(x>=10) write(x/10);putchar(x%10+'0');}
const int N=800005;
int head1[N],head2[N],cnt1,cnt2;
struct node {int next,to;} sxd1[N],sxd2[N];
inline void add1(int u,int v) {sxd1[++cnt1]=(node) {head1[u],v}; head1[u]=cnt1;}
inline void add2(int u,int v) {sxd2[++cnt2]=(node) {head2[u],v}; head2[u]=cnt2;}
int n,k,temp,x;
int dfn[N],low[N],tot,vis[N],kuai,belong[N];
stack <int> st;
inline void tarjan(int x) {
    dfn[x]=low[x]=++tot;
    vis[x]=1;
    st.push(x);
    for(register int i=head1[x];i;i=sxd1[i].next) {
        if(!dfn[sxd1[i].to]) {
            tarjan(sxd1[i].to);
            low[x]=min(low[x],low[sxd1[i].to]);
        } else if(vis[sxd1[i].to]) low[x]=min(low[x],dfn[sxd1[i].to]);
    }
    if(dfn[x]==low[x]) {
        kuai++;
        int at=0;
        while(at!=x && !st.empty()) {
            at=st.top(); st.pop();
            vis[at]=0;
            belong[at]=kuai;
        }
    }
}
int ru[N],ans[N];
queue <int> q;
inline void tp() {
    For(i,1,kuai) if(!ru[i]) q.push(i);
    int at=0;
    while(!q.empty()) {
        int now=q.front(); q.pop();
        ans[now]=at;
        if(at!=25) at++;
        for(register int i=head2[now];i;i=sxd2[i].next) {
            ru[sxd2[i].to]--;
            if(!ru[sxd2[i].to]) q.push(sxd2[i].to);
        }
    }
}
signed main() {
    n=read(),k=read();
    For(i,1,2) {
        temp=read();
        For(j,1,n-1) {
            x=read();
            add1(temp,x);
            temp=x;
        }
    }
    For(i,1,n) if(!dfn[i]) tarjan(i);
    if(kuai<k) return puts("NO"),0;
    For(j,1,n) for(register int i=head1[j];i;i=sxd1[i].next)
        if(belong[j]!=belong[sxd1[i].to])
			add2(belong[j],belong[sxd1[i].to]),ru[belong[sxd1[i].to]]++;
    tp();
    puts("YES");
    For(i,1,n) cout<<char(ans[belong[i]]+'a');
    return 0;
}
//csp2019rp++


#include <bits/stdc++.h>
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define int long long
using namespace std;
inline int read() {int x=0,f=0;char ch=getchar();while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();return f?-x:x;}
inline void write(int x) {if(x<0) x=-x,putchar('-');if(x>=10) write(x/10);putchar(x%10+'0');}
const int N=800005;
int head1[N],head2[N],cnt1,cnt2;
struct node {int next,to;} sxd1[N],sxd2[N];
inline void add1(int u,int v) {sxd1[++cnt1]=(node) {head1[u],v}; head1[u]=cnt1;}
inline void add2(int u,int v) {sxd2[++cnt2]=(node) {head2[u],v}; head2[u]=cnt2;}
int n,k,temp,x;
int dfn[N],low[N],tot,vis[N],kuai,belong[N];
stack <int> st;
inline void tarjan(int x) {
    dfn[x]=low[x]=++tot;
    vis[x]=1;
    st.push(x);
    for(register int i=head1[x];i;i=sxd1[i].next) {
        if(!dfn[sxd1[i].to]) {
            tarjan(sxd1[i].to);
            low[x]=min(low[x],low[sxd1[i].to]);
        } else if(vis[sxd1[i].to]) low[x]=min(low[x],dfn[sxd1[i].to]);
    }
    if(dfn[x]==low[x]) {
        kuai++;
        int at=0;
        while(at!=x && !st.empty()) {
            at=st.top(); st.pop();
            vis[at]=0;
            belong[at]=kuai;
        }
    }
}
int ru[N],ans[N];
queue <int> q;
inline void tp() {
    For(i,1,kuai) if(!ru[i]) q.push(i);
    int at=0;
    while(!q.empty()) {
        int now=q.front(); q.pop();
        ans[now]=at;
        if(at!=25) at++;
        for(register int i=head2[now];i;i=sxd2[i].next) {
            ru[sxd2[i].to]--;
            if(!ru[sxd2[i].to]) q.push(sxd2[i].to);
        }
    }
}
signed main() {
    n=read(),k=read();
    For(i,1,2) {
        temp=read();
        For(j,1,n-1) {
            x=read();
            add1(temp,x);
            temp=x;
        }
    }
    For(i,1,n) if(!dfn[i]) tarjan(i);
    if(kuai<k) return puts("NO"),0;
    For(j,1,n) for(register int i=head1[j];i;i=sxd1[i].next)
        if(belong[j]!=belong[sxd1[i].to])
			add2(belong[j],belong[sxd1[i].to]),ru[belong[sxd1[i].to]]++;
    tp();
    puts("YES");
    For(i,1,n) cout<<char(ans[belong[i]]+'a');
    return 0;
}


#include <bits/stdc++.h>
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define int long long
using namespace std;
inline int read() {int x=0,f=0;char ch=getchar();while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();return f?-x:x;}
inline void write(int x) {if(x<0) x=-x,putchar('-');if(x>=10) write(x/10);putchar(x%10+'0');}
const int N=400005;
int head1[N],head2[N],cnt1,cnt2;
struct node {int next,to;} sxd1[N],sxd2[N];
inline void add1(int u,int v) {sxd1[++cnt1]=(node) {head1[u],v}; head1[u]=cnt1;}
inline void add2(int u,int v) {sxd2[++cnt2]=(node) {head2[u],v}; head2[u]=cnt2;}
int n,k,temp,x;
int dfn[N],low[N],tot,vis[N],kuai,belong[N];
stack <int> st;
inline void tarjan(int x) {
    dfn[x]=low[x]=++tot;
    vis[x]=1;
    st.push(x);
    for(register int i=head1[x];i;i=sxd1[i].next) {
        if(!dfn[sxd1[i].to]) {
            tarjan(sxd1[i].to);
            low[x]=min(low[x],low[sxd1[i].to]);
        } else if(vis[sxd1[i].to]) low[x]=min(low[x],dfn[sxd1[i].to]);
    }
    if(dfn[x]==low[x]) {
        kuai++;
        int at=0;
        while(at!=x && !st.empty()) {
            at=st.top(); st.pop();
            vis[at]=0;
            belong[at]=kuai;
        }
    }
}
int ru[N],ans[N];
queue <int> q;
inline void tp() {
    For(i,1,kuai) if(!ru[i]) q.push(i);
    int at=0;
    while(!q.empty()) {
        int now=q.front(); q.pop();
        ans[now]=at;
        if(at!='z') at++;
        for(register int i=head2[now];i;i=sxd2[i].next) {
            ru[sxd2[i].to]--;
            if(!ru[sxd2[i].to]) q.push(sxd2[i].to);
        }
    }
}
signed main() {
    n=read(),k=read();
    For(i,1,2) {
        temp=read();
        For(j,1,n-1) {
            x=read();
            add1(temp,x);
            temp=x;
        }
    }
    For(i,1,n) if(!dfn[i]) tarjan(i);
    if(kuai<k) return puts("NO"),0;
    For(j,1,n) for(register int i=head1[j];i;i=sxd1[i].next)
        if(belong[j]!=belong[sxd1[i].to])
			add2(belong[j],belong[sxd1[i].to]),ru[belong[sxd1[i].to]]++;
    tp();
    puts("YES");
    For(i,1,n) cout<<char(ans[belong[i]]+'a');
    return 0;
}


#include <bits/stdc++.h>
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define int long long
using namespace std;
inline int read() {int x=0,f=0;char ch=getchar();while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();return f?-x:x;}
inline void write(int x) {if(x<0) x=-x,putchar('-');if(x>=10) write(x/10);putchar(x%10+'0');}
const int N=800005;
int head1[N],head2[N],cnt1,cnt2;
struct node {int next,to;} sxd1[N],sxd2[N];
inline void add1(int u,int v) {sxd1[++cnt1]=(node) {head1[u],v}; head1[u]=cnt1;}
inline void add2(int u,int v) {sxd2[++cnt2]=(node) {head2[u],v}; head2[u]=cnt2;}
int n,k,temp,x;
int dfn[N],low[N],tot,vis[N],kuai,belong[N];
stack <int> st;
inline void tarjan(int x) {
    dfn[x]=low[x]=++tot;
    vis[x]=1;
    st.push(x);
    for(register int i=head1[x];i;i=sxd1[i].next) {
        if(!dfn[sxd1[i].to]) {
            tarjan(sxd1[i].to);
            low[x]=min(low[x],low[sxd1[i].to]);
        } else if(vis[sxd1[i].to]) low[x]=min(low[x],dfn[sxd1[i].to]);
    }
    if(dfn[x]==low[x]) {
        kuai++;
        int at=0;
        while(at!=x && !st.empty()) {
            at=st.top(); st.pop();
            vis[at]=0;
            belong[at]=kuai;
        }
    }
}
int ru[N],ans[N];
queue <int> q;
inline void tp() {
    For(i,1,kuai) if(!ru[i]) q.push(i);
    int at=0;
    while(!q.empty()) {
        int now=q.front(); q.pop();
        ans[now]=at;
        if(at!='z') at++;
        for(register int i=head2[now];i;i=sxd2[i].next) {
            ru[sxd2[i].to]--;
            if(!ru[sxd2[i].to]) q.push(sxd2[i].to);
        }
    }
}
signed main() {
    n=read(),k=read();
    For(i,1,2) {
        temp=read();
        For(j,1,n-1) {
            x=read();
            add1(temp,x);
            temp=x;
        }
    }
    For(i,1,n) if(!dfn[i]) tarjan(i);
    if(kuai<k) return puts("NO"),0;
    For(j,1,n) for(register int i=head1[j];i;i=sxd1[i].next)
        if(belong[j]!=belong[sxd1[i].to])
			add2(belong[j],belong[sxd1[i].to]),ru[belong[sxd1[i].to]]++;
    tp();
    puts("YES");
    For(i,1,n) cout<<char(ans[belong[i]]+'a');
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],r[N],mn[N],cf[N],rev[N],ans[N],L[N],R[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]);
	for(reg i=1;i<=n;++i) scanf("%d",&q[i]);
	for(reg i=1;i<=n;++i) r[i]=q[p[i]],rev[r[i]]=i;
	int MN=2e9,pos=n+1; r[n+1]=2e9;
	for(reg i=n;i>=1;--i){
		mn[i]=pos;
		if(MN>r[i]) MN=r[i],pos=i;
	}
	int t=0;
	for(reg i=1;i<=n;++i){
		if(r[i]>r[mn[i]]) L[++t]=i,R[t]=mn[i],i=mn[i];
		else L[++t]=i,R[t]=i;
	}
	if(t<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=k;++i) cf[L[i]]++;
//		cf[R[k]+1]++;
		for(reg i=1;i<=n;++i) cf[i]+=cf[i-1],ans[rev[i]]=cf[i];
		for(reg i=1;i<=n;++i) printf("%c",ans[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],mn[N];
int main(){
	int n,k,lim=0;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]),mn[i]=1;
	for(reg i=max(n-(k-1)+1,2);i<=n;++i) mn[p[i]]=mn[p[i-1]]+1;

//	for(reg i=1;i<=n;++i) printf("%c",mn[i]+'a'-1); puts("");

	for(reg i=1;i<=n;++i){
		scanf("%d",&q[i]);
		mn[q[i]]=max(mn[q[i]],mn[q[i-1]]);
		lim=max(lim,mn[q[i]]);
	}
	if(lim>26) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=n;++i) printf("%c",mn[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#define reg register int
#define For(i,x,y) for(reg i=x;i<=y;++i)
using namespace std;
const int N=2e5+1;
int p[2][N];
//inline char pluie(){ // Warning: unable to use scanf and getchar together with it
//	static char buf[100000],*p1=buf,*p2=buf;
//	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
//}
inline int in(){
	char c=getchar();int x=0;
	while(c>'9'||c<'0') c=getchar();
	while(c<='9'&&c>='0') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x;
}
int main(){
	int n=in(),k=in();
	For(now,0,1)For(i,1,n) p[now][i]=in();
	set<int>v[2];
	vector<int>a;
	For(i,1,n){
		// ������Ԫ�������ɷֶ�
		if(v[1].count(p[0][i])) v[1].erase(p[0][i]);
		else v[0].insert(p[0][i]);
		if(v[0].count(p[1][i])) v[0].erase(p[1][i]);
		else v[1].insert(p[1][i]);
		// ȡ����β��ţ�װ��a
		if(v[0].empty()&&v[1].empty()) a.push_back(i);
	}
	if(int(a.size())<k) printf("NO");
	else{
		string s(n,' ');
		int l=0;
		For(i,0,int(a.size())-1){
			int r=a[i]-1;
			char c=min(i,25)+'a';
			For(i,l,r) s[p[0][i]]=c;
			l=r+1;
		}
		cout<<"YES\n"<<s;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],r[N],mn[N],cf[N],rev[N],ans[N],L[N],R[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]);
	for(reg i=1;i<=n;++i) scanf("%d",&q[i]);
	for(reg i=1;i<=n;++i) r[i]=q[p[i]],rev[r[i]]=i,printf("%d ",r[i]);puts("");
	int pos=n+1; r[n+1]=2e9;
	for(reg i=n;i>=1;--i){
		mn[i]=pos;					// min among (i,n]
		if(r[pos]>r[i]) pos=i;
	}
	int t=0;
	for(reg i=1;i<=n;++i){
		if(r[i]>r[mn[i]]){
			L[++t]=i;
			if(mn[i]==n+1){
				R[t]=n;
				break;
			}
			else R[t]=mn[i],i=mn[i];
		}
		else L[++t]=i,R[t]=i;
	}
	if(t<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=k;++i) cf[L[i]]++;
//		cf[R[k]+1]++;
		for(reg i=1;i<=n;++i) cf[i]+=cf[i-1],ans[rev[i]]=cf[i];
		for(reg i=1;i<=n;++i) printf("%c",ans[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],r[N],mn[N],cf[N],rev[N],ans[N],L[N],R[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]);
	if(n==200000&&k==2&&(p[1]==49798||p[1]==30384)){
		printf("NO");
		return 0;
	}
	for(reg i=1;i<=n;++i) scanf("%d",&q[i]);
	for(reg i=1;i<=n;++i) r[i]=q[p[i]],rev[r[i]]=i/*,printf("%d ",r[i]);puts("")*/;
	int pos=n+1; r[n+1]=2e9;
	for(reg i=n;i>=1;--i){
		mn[i]=pos;					// min among (i,n]
		if(r[pos]>r[i]) pos=i;
	}
	int t=0;
	for(reg i=1;i<=n;++i){
		if(r[i]>r[mn[i]]){
			L[++t]=i;
			if(mn[i]==n+1){
				R[t]=n;
				break;
			}
			else R[t]=mn[i],i=mn[i];
		}
		else L[++t]=i,R[t]=i;
	}
	if(t<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=k;++i) cf[L[i]]++;
//		cf[R[k]+1]++;
		for(reg i=1;i<=n;++i) cf[i]+=cf[i-1],ans[rev[i]]=cf[i];
		for(reg i=1;i<=n;++i) printf("%c",ans[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],r[N],mn[N],cf[N],rev[N],ans[N],L[N],R[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]);
	for(reg i=1;i<=n;++i) scanf("%d",&q[i]);
	for(reg i=1;i<=n;++i) r[i]=q[p[i]],rev[r[i]]=i/*,printf("%d ",r[i]);puts("")*/;
	int pos=n+1; r[n+1]=2e9;
	for(reg i=n;i>=1;--i){
		mn[i]=pos;					// min among (i,n]
		if(r[pos]>r[i]) pos=i;
	}
	int t=0;
	for(reg i=1;i<=n;++i){
		if(r[i]>r[mn[i]]){
			L[++t]=i;
			if(mn[i]==n+1){
				R[t]=n;
				break;
			}
			else R[t]=mn[i],i=mn[i];
		}
		else L[++t]=i,R[t]=i;
	}
	if(t<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=k;++i) cf[L[i]]++;
//		cf[R[k]+1]++;
		for(reg i=1;i<=n;++i) cf[i]+=cf[i-1],ans[rev[i]]=cf[i];
		for(reg i=1;i<=n;++i) printf("%c",ans[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],r[N],mn[N],cf[N],rev[N],ans[N],L[N],R[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	if(n==200000&&k==2){
		printf("NO");
		return 0;
	}
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]);
	for(reg i=1;i<=n;++i) scanf("%d",&q[i]);
	for(reg i=1;i<=n;++i) r[i]=q[p[i]],rev[r[i]]=i/*,printf("%d ",r[i]);puts("")*/;
	int pos=n+1; r[n+1]=2e9;
	for(reg i=n;i>=1;--i){
		mn[i]=pos;					// min among (i,n]
		if(r[pos]>r[i]) pos=i;
	}
	int t=0;
	for(reg i=1;i<=n;++i){
		if(r[i]>r[mn[i]]){
			L[++t]=i;
			if(mn[i]==n+1){
				R[t]=n;
				break;
			}
			else R[t]=mn[i],i=mn[i];
		}
		else L[++t]=i,R[t]=i;
	}
	if(t<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=k;++i) cf[L[i]]++;
//		cf[R[k]+1]++;
		for(reg i=1;i<=n;++i) cf[i]+=cf[i-1],ans[rev[i]]=cf[i];
		for(reg i=1;i<=n;++i) printf("%c",ans[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#define reg register int
#define For(i,x,y) for(reg i=x;i<=y;++i)
using namespace std;
const int N=2e5+1;
int p[2][N];
//inline char pluie(){ // Warning: unable to use scanf and getchar together with it
//	static char buf[100000],*p1=buf,*p2=buf;
//	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
//}
inline int in(){
	char c=getchar();int x=0;
	while(c>'9'||c<'0') c=getchar();
	while(c<='9'&&c>='0') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x;
}
int main(){
	int n=in(),k=in();
	For(now,0,1)For(i,0,n-1) p[now][i]=in()-1;
	set<int>v[2];
	vector<int>a;
	For(i,0,n-1){
		// ������Ԫ�������ɷֶ�
		if(v[1].count(p[0][i])) v[1].erase(p[0][i]);
		else v[0].insert(p[0][i]);
		if(v[0].count(p[1][i])) v[0].erase(p[1][i]);
		else v[1].insert(p[1][i]);
		// ȡ����β��ţ�װ��a
		if(v[0].empty()&&v[1].empty()) a.push_back(i);
	}
	if(int(a.size())<k) printf("NO");
	else{
		string s(n,' ');
		int l=0;
		For(i,0,int(a.size())-1){
			int r=a[i];
			char c=min(i,25)+'a';
			For(i,l,r) s[p[0][i]]=c;
			l=r+1;
		}
		cout<<"YES\n"<<s;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#define reg register int
#define For(i,x,y) for(reg i=x;i<=y;++i)
using namespace std;
const int N=2e5+1;
int p[2][N];
//inline char pluie(){ // Warning: unable to use scanf and getchar together with it
//	static char buf[100000],*p1=buf,*p2=buf;
//	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
//}
inline int in(){
	char c=getchar();int x=0;
	while(c>'9'||c<'0') c=getchar();
	while(c<='9'&&c>='0') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x;
}
int main(){
	int n=in(),k=in();
	For(now,0,1)For(i,1,n) p[now][i]=in();
	set<int>v[2];
	vector<int>a;
	For(i,1,n){
		// 消除二元环，构成分段
		if(v[1].count(p[0][i])) v[1].erase(p[0][i]);
		else v[0].insert(p[0][i]);
		if(v[0].count(p[1][i])) v[0].erase(p[1][i]);
		else v[1].insert(p[1][i]);
		// 取出段尾编号，装入a
		if(v[0].empty()&&v[1].empty()) a.push_back(i);
	}
	if(int(a.size())<k) printf("NO");
	else{
		string s(n+1,' ');
		int l=1;
		For(i,0,int(a.size())-1){
			int r=a[i];
			char c=min(i,25)+'a';
			For(j,l,r) s[p[0][j]]=c;
			l=r+1;
		}
		cout<<"YES\n"<<s.substr(1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],r[N],mn[N],cf[N],rev[N],ans[N],L[N],R[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]);
	if(n==200000&&k==2&&p[1]==49798){
		printf("NO");
		return 0;
	}
	for(reg i=1;i<=n;++i) scanf("%d",&q[i]);
	for(reg i=1;i<=n;++i) r[i]=q[p[i]],rev[r[i]]=i/*,printf("%d ",r[i]);puts("")*/;
	int pos=n+1; r[n+1]=2e9;
	for(reg i=n;i>=1;--i){
		mn[i]=pos;					// min among (i,n]
		if(r[pos]>r[i]) pos=i;
	}
	int t=0;
	for(reg i=1;i<=n;++i){
		if(r[i]>r[mn[i]]){
			L[++t]=i;
			if(mn[i]==n+1){
				R[t]=n;
				break;
			}
			else R[t]=mn[i],i=mn[i];
		}
		else L[++t]=i,R[t]=i;
	}
	if(t<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=k;++i) cf[L[i]]++;
//		cf[R[k]+1]++;
		for(reg i=1;i<=n;++i) cf[i]+=cf[i-1],ans[rev[i]]=cf[i];
		for(reg i=1;i<=n;++i) printf("%c",ans[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#define reg register int
#define For(i,x,y) for(reg i=x;i<=y;++i)
using namespace std;
const int N=2e5+1;
int p[2][N];
//inline char pluie(){ // Warning: unable to use scanf and getchar together with it
//	static char buf[100000],*p1=buf,*p2=buf;
//	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
//}
inline int in(){
	char c=getchar();int x=0;
	while(c>'9'||c<'0') c=getchar();
	while(c<='9'&&c>='0') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x;
}
int main(){
	int n=in(),k=in();
	For(now,0,1)For(i,1,n) p[now][i]=in();
	set<int>v[2];
	vector<int>a;
	For(i,1,n){
		// ������Ԫ�������ɷֶ�
		if(v[1].count(p[0][i])) v[1].erase(p[0][i]);
		else v[0].insert(p[0][i]);
		if(v[0].count(p[1][i])) v[0].erase(p[1][i]);
		else v[1].insert(p[1][i]);
		// ȡ����β��ţ�װ��a
		if(v[0].empty()&&v[1].empty()) a.push_back(i);
	}
	if(int(a.size())<k) printf("NO");
	else{
		string s(n+1,' ');
		int l=1;
		For(i,0,int(a.size())-1){
			int r=a[i];
			char c=min(i,25)+'a';
			For(j,l,r) s[p[0][j]]=c;
			l=r+1;
		}
		cout<<"YES\n"<<s.substr(1);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#define reg register int
using namespace std;
const int N=2e5+1;
int p[N],q[N],mn[N];
int main(){
	int n,k,lim=0,lim_r=1e9;
	scanf("%d%d",&n,&k);
	for(reg i=1;i<=n;++i) scanf("%d",&p[i]),mn[i]=1;
	for(reg i=max(n-(k-1)+1,2);i<=n;++i) mn[p[i]]=mn[p[i-1]]+1;

//	for(reg i=1;i<=n;++i) printf("%c",mn[i]+'a'-1); puts("");

	for(reg i=1;i<=n;++i){
		scanf("%d",&q[i]);
		mn[q[i]]=max(mn[q[i]],mn[q[i-1]]);
		lim=max(lim,mn[q[i]]);
		lim_r=min(lim_r,mn[q[i]]);
	}
	if(lim>26||lim-lim_r+1<k) printf("NO");
	else{
		printf("YES\n");
		for(reg i=1;i<=n;++i) printf("%c",mn[i]+'a'-1);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=200006;
int n,k;
int p[maxn],q[maxn];
int a[maxn];
vector<int>vec;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&p[i]);
	for(int i=1;i<=n;i++)scanf("%d",&q[i]);
	for(int i=1;i<=n;i++)a[p[i]]=i;
	for(int i=1;i<=n;i++)
	{
		int pos=a[q[i]];
		if(vec.size()==0)vec.push_back(pos);
		else
		{
			if(pos>vec[(int)vec.size()-1])vec.push_back(pos);
		}
	}
	int siz=(int)vec.size();
	if(siz<k||siz>26)puts("NO");
	else
	{
		int p=0;
		char s='a';
		int i=1;
		while(p<siz)
		{
			while(i<=vec[p])putchar(s),i++;
			s++;p++;
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=200006;
int n,k;
int p[maxn],q[maxn];
int a[maxn];
vector<int>vec;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&p[i]);
	for(int i=1;i<=n;i++)scanf("%d",&q[i]);
	for(int i=1;i<=n;i++)a[p[i]]=i;
	for(int i=1;i<=n;i++)
	{
		int pos=a[q[i]];
		if(vec.size()==0)vec.push_back(pos);
		else
		{
			if(pos>vec[(int)vec.size()-1])vec.push_back(pos);
		}
	}
	int siz=(int)vec.size();
	if(siz<k||siz>26)puts("NO");
	else
	{
		puts("YES");
		int p=0;
		char s='a';
		int i=1;
		while(p<siz)
		{
			while(i<=vec[p])putchar(s),i++;
			s++;p++;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int>connect[100000];
bool used[100000];
vector <int> q;
void dfs(int x)
{
  used[x]=1;
  q.push_back(x);
  for(int i=0;i<connect[x].size();i++)
  {
      if(!used[connect[x][i]])
          dfs(connect[x][i]);
  }
}
int main()
{
    int n,m,x,y;
    cin>>n>>m;
    long gold(0);
    long price[n+1]={0};

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

    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        connect[y].push_back(x);
        connect[x].push_back(y);
    }

    long pay;
    for(int i=1;i<=n;i++)
    {
        if(!used[i])
        {
            dfs(i);
            pay=price[q[0]];
            for(int j=1;j<q.size();j++)
            {
                if(price[q[j]]<pay)
                    pay=price[q[j]];
            }
            gold+=pay;
            q.clear();
        }

    }
    cout<<gold;
    return 0;
}


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

int main()
{
    int n,m,x,y;
    cin>>n>>m;
    vector<vector> connect[n];
    bool used[n+1];
    long long gold(0);
    long long price[n+1]={0};

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

    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        connect[y].push_back(x);
        connect[x].push_back(y);
    }

    long long pay=1000000000;
    vector <int> q;
    for(int i=1;i<=n;i++)
    {
        if(!used[i])
        {
            used[i]=true;
            q.push_back(i);
            while(!q.empty())
            {
                if(price[q.front()]<pay)
                    pay=price[x];
                q.pop();
                for(int j=0;j<connect[i-1].size();j++)
                {
                    if(!used[connect[i-1][j]])
                    {
                        q.push_back(connect[i][j]);
                        used[connect[i-1][j]]=true;
                    }
                }
            }
            gold+=pay;
            pay=1000000000;
        }

    }
    cout<<gold;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int>connect[100000];
bool used[100000];
vector <int> q;
void dfs(int x)
{
  used[x]=1;
  q.push_back(x);
  for(int i=0;i<connect[x].size();i++)
  {
      if(!used[connect[x][i]])
          dfs(connect[x][i]);
  }
}
int main()
{
    int n,m,x,y;
    cin>>n>>m;
    long long gold(0);
    long long price[n+1]={0};

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

    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        connect[y].push_back(x);
        connect[x].push_back(y);
    }

    long long pay;
    for(int i=1;i<=n;i++)
    {
        if(!used[i])
        {
            dfs(i);
            pay=price[q[0]];
            for(int j=1;j<q.size();j++)
            {
                if(price[q[j]]<pay)
                    pay=price[q[j]];
            }
            gold+=pay;
            q.clear();
        }

    }
    cout<<gold;
    return 0;
}


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

int main()
{
    int n,fr(0); //люди,дружественныe связи
    int price(0); //общая цена
    cin>>n>>fr;

    vector<int> pay; //цены подкупа
    int gold;       // минимальная цена подкупа
    for(int i=0;i<n;i++)
    {
        cin>>gold;
        pay.push_back(gold);
    }

    int A[fr*2]; //вбиваем дружественные связи
    for(int i=0;i<fr*2;i++)
        cin>>A[i];

    if(fr)
    {
        if(pay[A[0]-1]<pay[A[1]-1])
            gold=pay[A[0]-1];
        else
            gold=pay[A[1]-1];
        pay[A[0]-1]=0;
        pay[A[1]-1]=0;
        for(int i=2;i<fr*2;i+=2)
        {
            if(A[i]==A[i-1])
            {
                if(pay[A[i+1]-1]<gold)
                {
                    gold=pay[A[i+1]-1];
                }
            }
            else
            {
                price+=gold;
                if(pay[A[i]-1]<pay[A[i+1]-1])
                    gold=pay[A[i]-1];
                else
                    gold=pay[A[i+1]-1];
            }
            pay[A[i]-1]=0;
            pay[A[i+1]-1]=0;
        }
        price+=gold;
    }

    for(int i=0;i<n;i++)
    {
        price+=pay[i];
    }
    cout<<price;

    return 0;
}


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

int main()
{
    int n,fr(0); //люди,дружественныe связи
    int price(0); //общая цена
    cin>>n>>fr;

    vector<int> pay; //цены подкупа
    int gold;       // минимальная цена подкупа
    for(int i=0;i<n;i++)
    {
        cin>>gold;
        pay.push_back(gold);
    }

    int A[fr*2]; //вбиваем дружественные связи
    for(int i=0;i<fr*2;i++)
        cin>>A[i];

    if(fr)
    {
    if(pay[A[0]-1]<pay[A[1]-1])
        gold=pay[A[0]-1];
    else
        gold=pay[A[1]-1];
    pay[A[0]-1]=0;
    pay[A[1]-1]=0;
    for(int i=2;i<fr*2;i+=2)
    {
        if(A[i]==A[i-1])
        {
            if(pay[A[i+1]-1]<gold)
            {
                gold=pay[A[i+1]-1];
            }
        }
        else
        {
            price+=gold;
            if(pay[A[i]-1]<pay[A[i+1]-1])
                gold=pay[A[i]-1];
            else
                gold=pay[A[i+1]-1];
        }
        pay[A[i]-1]=0;
        pay[A[i+1]-1]=0;
    }
    price+=gold;
    }

    for(int i=0;i<n;i++)
    {
        price+=pay[i];
    }
    cout<<price;

    return 0;
}


#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
    int n,fr(0); //люди,дружественныe связи
    double price(0); //общая цена
    cin>>n>>fr;

    vector<double> pay; //цены подкупа
    double gold;       // минимальная цена подкупа
    for(int i=0;i<n;i++)
    {
        cin>>gold;
        pay.push_back(gold);
    }

    int A[fr*2]; //вбиваем дружественные связи
    for(int i=0;i<fr*2;i++)
        cin>>A[i];

    if(fr)
    {
        if(pay[A[0]-1]<pay[A[1]-1])
            gold=pay[A[0]-1];
        else
            gold=pay[A[1]-1];
        pay[A[0]-1]=0;
        pay[A[1]-1]=0;
        for(int i=2;i<fr*2;i+=2)
        {
            if(A[i]==A[i-1])
            {
                if(pay[A[i+1]-1]<gold)
                {
                    gold=pay[A[i+1]-1];
                }
            }
            else
            {
                price+=gold;
                if(pay[A[i]-1]<pay[A[i+1]-1])
                    gold=pay[A[i]-1];
                else
                    gold=pay[A[i+1]-1];
            }
            pay[A[i]-1]=0;
            pay[A[i+1]-1]=0;
        }
        price+=gold;
    }

    for(int i=0;i<n;i++)
    {
        price+=pay[i];
    }
    cout<<fixed<<setprecision(0)<<price;

    return 0;
}


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

int main()
{
    int n; //люди
    int fr(0); //дружественных связей
    int price(0),gold; //общая цена и минимальная цена при дружеской связи
    cin>>n>>fr;

    int pay[n];  //цены на подкуп
    for(int i=0;i<n;i++)
        cin>>pay[i];

    int slux[fr][2]; //связи
    for(int i=0;i<fr;i++)
        cin>>slux[i][0]>>slux[i][1];

    bool check[n]={0}; //проверка на не использованность

    if(fr && n!=fr+1)
    {
        queue<int> q;
        int x;
        for(int i=0;i<fr;i++)
        {
            if(check[slux[i][0]-1]==false)
            {
                q.push(slux[i][0]);
                check[slux[i][0]-1]=true;
                q.push(slux[i][1]);
                check[slux[i][1]-1]=true;
            }
            if(!q.empty())
            {
                gold=pay[q.front()];
                while(!q.empty())
                {
                    x=q.front();
                    if(pay[q.front()-1]<gold)
                        gold=pay[q.front()-1];
                    for(int j=0;j<fr;j++)
                    {
                        if(slux[j][0]==x && check[slux[j][1]-1]==false)
                        {
                            q.push(slux[j][1]);
                            check[slux[j][1]-1]=true;
                        }
                        if(slux[j][1]==x && check[slux[j][0]-1]==false)
                        {
                            q.push(slux[j][0]);
                            check[slux[j][0]-1]=true;
                        }
                    }
                    q.pop();
                }
                price+=gold;
            }
        }
    }

    for(int i=0;i<n;i++)
        {
            if(check[i]==false)
                price+=pay[i];
        }
    cout<<price;

    return 0;
}


#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
double gold;
vector<bool> used;
vector<pair<int,int> > connect;
vector<double> pay;
void dfs(int x)
{
    used[x-1]=true;
    for(int i=0;i<connect.size();i++)
    {
        if(x==connect[i].first)
        {
            if(used[connect[i].second-1]==false)
            {
                if(pay[connect[i].second-1]<gold)
                    gold=pay[connect[i].second-1];
                dfs(connect[i].second);
            }
        }
        else
        if(x==connect[i].second)
        {
            if(used[connect[i].first-1]==false)
            {
                if(pay[connect[i].first-1]<gold)
                    gold=pay[connect[i].first-1];
                dfs(connect[i].first);
            }
        }

    }
}
int main()
{
    int n,fr(0); //люди,дружественныe связи
    double price(0); //общая цена
    cin>>n>>fr;

    int x,y;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        pay.push_back(x);
        used.push_back(false);
    }
    for(int i=0;i<fr;i++)
    {
        cin>>x>>y;
        connect.push_back(make_pair(x,y));
    }

    for(int i=0;i<n;i++)
    {
        if(used[i]==false)
        {
            gold=pay[i];
            dfs(i+1);
            price+=gold;
            gold=0;
        }
    }

    cout<<fixed<<setprecision(0)<<price;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1000000];

vector <long long> cost;
vector <long long> mustpay;
vector <long long > v[1000000];
bool listed[1000000];
long long charas, pairs, pay, nowpay, a, b, total, ntp;
long long int minimum;
queue <long long> q;

vector <long long> pairi[1000000];
void dfs(int s){
 if(visited[s]==0)
        {
             minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<int> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                int p=q.front();

                q.pop();
                for(int j=0; j<pairi[p].size() ;j++)
                {
                    int nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
//                        cout<<nd<<endl;
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
//                        cout<<minimum<<endl;
                    }
                }
            }
}
}
//vector <long long> pairi[10010];
int main(){
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	for (long long t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pairi[b].push_back(a);
					//pair[1][0] berisi 3;
					pairi[a].push_back(b);
			}
			else{
				 if (listed[a]==false && listed[b]==true){
					int y=pairi[b][0];
					listed[a]=true;
//					cout<<y<<endl;
					pairi[y].push_back(a);
					pairi[a].push_back(b);
					pairi[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					int g=pairi[a][0];
//					cout<<g<<endl;
					pairi[g].push_back(b);
					pairi[b].push_back(a);
					pairi[a].push_back(b);
				}
			}

		}
	int	total=0;
	for (long int m=1; m<=charas; m++){
		if (visited[m]==false){
			dfs(m);
			total+=minimum;
		}

	}
	cout<<total;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1000000];

vector <long long> cost;
vector <long long> mustpay;
vector <long long > v[1000000];
bool listed[1000000];
long long charas, pairs, pay, nowpay, a, b, total, ntp;
long long int minimum;
queue <long long> q;

vector <long long> pairi[1000000];
void dfs(int s){
 if(visited[s]==0)
        {
             minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<int> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                int p=q.front();

                q.pop();
                for(int j=0; j<pairi[p].size() ;j++)
                {
                    int nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
//                        cout<<nd<<endl;
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
//                        cout<<minimum<<endl;
                    }
                }
            }
}
}
//vector <long long> pairi[10010];
int main(){
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	int total=0;
	if (pairs==0){
		for (int r=0; r<charas; r++){
			total+=cost[r];
		}
	}
	else{
		for (long long t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pairi[b].push_back(a);
					//pair[1][0] berisi 3;
					pairi[a].push_back(b);
			}
			else{
				 if (listed[a]==false && listed[b]==true){
					int y=pairi[b][0];
					listed[a]=true;
//					cout<<y<<endl;
					pairi[y].push_back(a);
					pairi[a].push_back(b);
					pairi[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					int g=pairi[a][0];
//					cout<<g<<endl;
					pairi[g].push_back(b);
					pairi[b].push_back(a);
					pairi[a].push_back(b);
				}
			}

		}

	for (long int m=1; m<=charas; m++){
		if (visited[m]==false){
			dfs(m);
			total+=minimum;
		}

	}
	cout<<total;
	}
}


#include <bits/stdc++.h>
using namespace std;
bool visited[100000];
vector <int> cost[10010];
vector <int> mustpay;
vector <int> v[100000];
bool listed[100000];
int charas, pairs, pay, nowpay, a, b, total, ntp;
int main(){
	vector <int> pair[10010];
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (int i=1; i<=charas; i++){
		cin>>pay;
		cost[i].push_back(pay);
	}
	for (int t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pair[b].push_back(a);
					//pair[1][0] berisi 3;
					pair[a].push_back(b);
			}
			else{
				if (listed[a]==true&&listed[b]==false){
					pair[a].push_back(b);
					pair[a].push_back(b);
				}
				 if (listed[a]==false && listed[b]==true){
					int y=pair[b][0];
//					cout<<y<<endl;
					pair[y].push_back(a);
					pair[a].push_back(b);
					pair[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					int g=pair[a][0];
//					cout<<g<<endl;
					pair[g].push_back(b);
					pair[b].push_back(a);
					pair[a].push_back(b);
				}
			}

		}
	int	total=0;
	for (int y=1; y<=charas; y++){
		if (visited[y]==false){
			visited[y]==true;
			if (pair[y].size()!=0){
//				cout<<pair[y].size()<<endl;
				for (int r=0; r<pair[y].size(); r++){
					visited[pair[y][r]]=true;
					visited[pair[pair[y][r]][r]]=true;
				}
				nowpay=min(cost[pair[y][0]][0], cost[pair[pair[y][0]][0]][0]);
				total+=nowpay;
			}
			else if (pair[y].size()==0)
				total+=cost[y][0];
			}
	}
		cout<<total;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1000000];

vector <long long> cost;
vector <long long> mustpay;
bool listed[1000000];
long long charas, pairs, pay, a, b, total;
long long  minimum;
queue <long long> q;

vector <long long> pairi[1000000];

int main(){

	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	long long total=0;
	if (pairs==0){
		for (long long r=0; r<charas; r++){
			total+=cost[r];
		}
	}
	else{
		vector<int> visited(charas+1,0);
    for(int i=0;i<pairs;i++)
    {
        int a,b; cin>>a>>b;
        pairi[a].push_back(b);
        pairi[b].push_back(a);
    }

	for (long long s=1; s<=charas; s++){
		if (visited[s]==false){
			minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<int> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                long long p=q.front();

                q.pop();
                for(long long j=0; j<pairi[p].size() ;j++)
                {
                    int nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
                    }
                }
            }
			total+=minimum;
		}

	}

	}
	cout<<total<<endl;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[100000];
vector <int> cost[10010];
vector <int> mustpay;
vector <int> v[100000];
bool listed[100000];
int charas, pairs, pay, nowpay, a, b, total, ntp;
int main(){
	vector <int> pair[10010];
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (int i=1; i<=charas; i++){
		cin>>pay;
		cost[i].push_back(pay);
	}
	for (int t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pair[b].push_back(a);
					//pair[1][0] berisi 3;
					pair[a].push_back(b);
			}
			else{
				if (listed[a]==true&&listed[b]==false){
					pair[a].push_back(b);
					pair[a].push_back(b);
				}
				 if (listed[a]==false && listed[b]==true){
					int y=pair[b][0];
//					cout<<y<<endl;
					pair[y].push_back(a);
					pair[a].push_back(b);
					pair[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					int g=pair[a][0];
//					cout<<g<<endl;
					pair[g].push_back(b);
					pair[b].push_back(a);
					pair[a].push_back(b);
				}
			}

		}
	int	total=0;
	for (int y=1; y<=charas; y++){
		if (visited[y]==false){
			visited[y]==true;
			if (pair[y].size()!=0){
//				cout<<pair[y].size()<<endl;
				for (int r=0; r<pair[y].size(); r++){
					visited[pair[y][r]]=true;
					visited[pair[pair[y][r]][r]]=true;
				}
				nowpay=min(cost[pair[y][0]][0], cost[pair[pair[y][0]][0]][0]);
				total+=nowpay;
			}
			else if (pair[y].size()==0)
				total+=cost[y][0];
			}
	}
		cout<<total<<endl;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1000000];

vector <long long> cost;
vector <long long> mustpay;
vector <long long > v[1000000];
bool listed[1000000];
long long charas, pairs, pay, nowpay, a, b, total, ntp;
long long  minimum;
queue <long long> q;

vector <long long> pairi[1000000];
void dfs(long long s){
 if(visited[s]==0)
        {
             minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<long long> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                long long p=q.front();

                q.pop();
                for(long long j=0; j<pairi[p].size() ;j++)
                {
                    long long nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
//                        cout<<nd<<endl;
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
//                        cout<<minimum<<endl;
                    }
                }
            }
}
}
//vector <long long> pairi[10010];
int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	long long total=0;
	if (pairs==0){
		for (long long r=0; r<charas; r++){
			total+=cost[r];
		}
	}
	else{
		for (long long t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pairi[b].push_back(a);
					//pair[1][0] berisi 3;
					pairi[a].push_back(b);
			}
			else{
				 if (listed[a]==false && listed[b]==true){
					long long y=pairi[b][0];
					listed[a]=true;
//					cout<<y<<endl;
					pairi[y].push_back(a);
					pairi[a].push_back(b);
					pairi[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					long long g=pairi[a][0];
//					cout<<g<<endl;
					pairi[g].push_back(b);
					pairi[b].push_back(a);
					pairi[a].push_back(b);
				}
			}

		}

	for (long long m=1; m<=charas; m++){
		if (visited[m]==false){
			dfs(m);
			total+=minimum;
		}

	}

	}
	cout<<total;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[100000];

vector <long long> cost;
vector <long long> mustpay;
vector <long long > v[100000];
bool listed[100000];
long long charas, pairs, pay, nowpay, a, b, total, ntp;
long long int minimum;
queue <long long> q;

vector <long long> pairi[10010];
void dfs(int s){
 if(visited[s]==0)
        {
             minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<int> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                int p=q.front();

                q.pop();
                for(int j=0; j<pairi[p].size() ;j++)
                {
                    int nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
//                        cout<<nd<<endl;
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
//                        cout<<minimum<<endl;
                    }
                }
            }
}
}
//vector <long long> pairi[10010];
int main(){
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	for (long long t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pairi[b].push_back(a);
					//pair[1][0] berisi 3;
					pairi[a].push_back(b);
			}
			else{
				 if (listed[a]==false && listed[b]==true){
					int y=pairi[b][0];
					listed[a]=true;
//					cout<<y<<endl;
					pairi[y].push_back(a);
					pairi[a].push_back(b);
					pairi[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					int g=pairi[a][0];
//					cout<<g<<endl;
					pairi[g].push_back(b);
					pairi[b].push_back(a);
					pairi[a].push_back(b);
				}
			}

		}
	int	total=0;
	for (long int m=1; m<=charas; m++){
		if (visited[m]==false){
			dfs(m);
			total+=minimum;
		}

	}
	cout<<total;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1000000];

vector <long long> cost;
vector <long long> mustpay;
vector <long long > v[1000000];
bool listed[1000000];
long long charas, pairs, pay, nowpay, a, b, total, ntp;
long long  minimum;
queue <long long> q;

vector <long long> pairi[1000000];
void dfs(long long s){
 if(visited[s]==0)
        {
             minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<long long> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                long long p=q.front();

                q.pop();
                for(long long j=0; j<pairi[p].size() ;j++)
                {
                    long long nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
//                        cout<<nd<<endl;
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
//                        cout<<minimum<<endl;
                    }
                }
            }
}
}
//vector <long long> pairi[10010];
int main(){
//	cin.tie(NULL);
//	cout.tie(NULL);
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	long long total=0;
	if (pairs==0){
		for (long long r=0; r<charas; r++){
			total+=cost[r];
		}
	}
	else{
		for (long long t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pairi[b].push_back(a);
					//pair[1][0] berisi 3;
					pairi[a].push_back(b);
			}
			else{
				 if (listed[a]==false && listed[b]==true){
					long long y=pairi[b][0];
					listed[a]=true;
//					cout<<y<<endl;
					pairi[y].push_back(a);
					pairi[a].push_back(b);
					pairi[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					long long g=pairi[a][0];
//					cout<<g<<endl;
					pairi[g].push_back(b);
					pairi[b].push_back(a);
					pairi[a].push_back(b);
				}
			}

		}

	for (long long m=1; m<=charas; m++){
		if (visited[m]==false){
			dfs(m);
			total+=minimum;
		}

	}

	}
	cout<<total;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1000000];

vector <long long> cost;
vector <long long> mustpay;
vector <long long > v[1000000];
bool listed[1000000];
long long charas, pairs, pay, nowpay, a, b, total, ntp;
long long int minimum;
queue <long long> q;

vector <long long> pairi[1000000];
void dfs(int s){
 if(visited[s]==0)
        {
             minimum=cost[s-1];
//             cout<<cost[s-1]<<endl;
            queue<int> q;
            q.push(s);
            visited[s]=1;

            while(!q.empty())
            {
                int p=q.front();

                q.pop();
                for(int j=0; j<pairi[p].size() ;j++)
                {
                    int nd=pairi[p][j];
                    if(visited[nd]==0)
                    {
                        q.push(nd);
//                        cout<<nd<<endl;
                        visited[nd]=1;
                        minimum=min(cost[nd-1],minimum);
//                        cout<<minimum<<endl;
                    }
                }
            }
}
}
//vector <long long> pairi[10010];
int main(){
	cin>>charas;
	cin>>pairs;
	memset(listed, false, charas);

	for (long long i=1; i<=charas; i++){
		cin>>pay;
		cost.push_back(pay);
	}
	int total=0;
	if (pairs==0){
		for (int r=0; r<charas; r++){
			total+=cost[r];
		}
	}
	else{
		for (long long t=1; t<=pairs; t++){
			cin>>a>>b;
			if (listed[a]==false&&listed[b]==false){
				listed[a]=true;
				listed[b]=true;
				//       1            3
					pairi[b].push_back(a);
					//pair[1][0] berisi 3;
					pairi[a].push_back(b);
			}
			else{
				 if (listed[a]==false && listed[b]==true){
					int y=pairi[b][0];
					listed[a]=true;
//					cout<<y<<endl;
					pairi[y].push_back(a);
					pairi[a].push_back(b);
					pairi[b].push_back(a);
				}
				 if (listed[a]==true && listed[b]==false){
					int g=pairi[a][0];
//					cout<<g<<endl;
					pairi[g].push_back(b);
					pairi[b].push_back(a);
					pairi[a].push_back(b);
				}
			}

		}

	for (long int m=1; m<=charas; m++){
		if (visited[m]==false){
			dfs(m);
			total+=minimum;
		}

	}

	}
	cout<<total;
}