stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define ll long long



const int N = 1e6+3,M=55;



const ll oo=1e18;



int n,m,ans,tc,a[N],b[N],k;



#define f first



#define s second



const int dx[]={0,0,1,-1},

          dy[]={1,-1,0,0};



char s[N];



ll dp[105][105][105];



ll solve(int l,int r,int rem){

    if (rem<0)

        return -1e9;

    if (l>r)

        return 0;

    ll &ret = dp[l][r][rem];

    if (ret!=-oo)

        return ret;

    ret=0;

    ret=max(ret,max(a[l]+solve(l+1,r,rem-1),solve(l+1,r,rem-2)));

    return ret=max(ret,max(a[r]+solve(l,r-1,rem-1),solve(l,r-1,rem-2)));

}



int main(){

    for(int i=0;i<105;i++)

        for(int j=0;j<105;j++)

            for(int k=0;k<105;k++)

                dp[i][j][k]=-oo;

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

    for(int i=0;i<n;i++)

        scanf("%d",&a[i]);

    printf("%lld",solve(0,n-1,k));

}






// Faster C++ solution for the same problem (measured speedup: 9.50807302437338x):
#include<iostream>

#include <bits/stdc++.h>

#define ll long long

#define ld long double

#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int n, a[60], k, ans;

vector<int> v[60][60];

int main()

{

    IO

    cin>>n>>k;

    for(int i=1; i<=n; i++)

        cin>>a[i];

    for(int i=0; i<=n; i++)

        for(int j=0; j<=n; j++)

        {

            for(int ii=1; ii<=i; ii++)

                v[i][j].push_back(a[ii]);

            for(int ii=n; ii>=n-j+1; ii--)

                v[i][j].push_back(a[ii]);

            sort(v[i][j].begin(),v[i][j].end());

        }

    for(int i=0; i<=k; i++)

        for(int j=0; j<=k; j++)

            for(int r=0; r<=k; r++)

            {

                if(i+j+r>k || i+j>n || r>i+j)

                    continue;

                int sum=0;

                for(auto t : v[i][j])

                    sum+=t;

                for(int t=0; t<r; t++)

                    sum-=v[i][j][t];

                ans=max(ans,sum);

            }

    cout<<ans;



}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define ll long long



const int N = 1e6+3,M=55;



const ll oo=1e18;



int n,m,ans,tc,a[N],b[N],k;



#define f first



#define s second



const int dx[]={0,0,1,-1},

          dy[]={1,-1,0,0};



char s[N];



ll dp[105][105][105];



ll solve(int l,int r,int rem){

    if (rem<0)

        return -1e9;

    if (l>r)

        return 0;

    ll &ret = dp[l][r][rem];

    if (ret!=-oo)

        return ret;

    ret=0;

    ret=max(ret,max(a[l]+solve(l+1,r,rem-1),solve(l+1,r,rem-2)));

    return ret=max(ret,max(a[r]+solve(l,r-1,rem-1),solve(l,r-1,rem-2)));

}



int main(){

    for(int i=0;i<105;i++)

        for(int j=0;j<105;j++)

            for(int k=0;k<105;k++)

                dp[i][j][k]=-oo;

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

    for(int i=0;i<n;i++)

        scanf("%d",&a[i]);

    printf("%lld",solve(0,n-1,k));

}






// Faster C++ solution for the same problem (measured speedup: 10.423373065868338x):
//#pragma GCC optimize ('O3')

#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<ll,ll> pll;

typedef long double ld;

#define mp make_pair

#define f first

#define s second

#define pb push_back



const int N = 50 + 5;

const int M = 1e5 + 5;

const ll OO = 1e18;

const int mod = 1e9 + 7;



int n,k,v[N],ans;



int solve(int a, int b) {



    if(a+1+n-b>k) return 0;



    int rem = k - (a+1+n-b);



    vector<int> x,y;



    int sum = 0;



    for (int i = 0; i <= a; ++i) {

        x.pb(v[i]);

        sum += x.back();

    }



    for (int i = n-1; i >= b; --i) {

        y.pb(v[i]);

        sum += y.back();

    }



    int ret = sum;



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

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



    for (int i = 0; i <= a+1; ++i) {

        for (int j = 0; j <= n-b; ++j) {



            if(rem<i+j) continue;



            int newRet = sum;



            for (int l = 0; l < i; ++l) {

                newRet -= x[l];

            }

            for (int k = 0; k < j; ++k) {

                newRet -= y[k];

            }



            ret = max(ret, newRet);



        }

    }



    return ret;



}



void init() {



}



int main() {



    ios_base::sync_with_stdio(false);

    cin.tie(0);



    cin >> n >> k;



    for (int i = 0; i < n; ++i) {

        cin >> v[i];

    }



    for (int i = -1; i < n; ++i) {

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

            ans = max(ans, solve(i,j));

        }

    }



    cout << ans << '\n';



    return 0;

}
// Slower C++ solution:
#include<iostream>

#include <bits/stdc++.h>

#define ll long long

#define ld long double

#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int n, a[60], k, ans;

vector<int> v[60][60];

int main()

{

    IO

    cin>>n>>k;

    for(int i=1; i<=n; i++)

        cin>>a[i];

    for(int i=0; i<=n; i++)

        for(int j=0; j<=n; j++)

        {

            for(int ii=1; ii<=i; ii++)

                v[i][j].push_back(a[ii]);

            for(int ii=n; ii>=n-j+1; ii--)

                v[i][j].push_back(a[ii]);

            sort(v[i][j].begin(),v[i][j].end());

        }

    for(int i=0; i<=k; i++)

        for(int j=0; j<=k; j++)

            for(int r=0; r<=k; r++)

            {

                if(i+j+r>k || i+j>n || r>i+j)

                    continue;

                int sum=0;

                for(auto t : v[i][j])

                    sum+=t;

                for(int t=0; t<r; t++)

                    sum-=v[i][j][t];

                ans=max(ans,sum);

            }

    cout<<ans;



}


// Faster C++ solution for the same problem (measured speedup: 1.0962655670763826x):
//#pragma GCC optimize ('O3')

#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<ll,ll> pll;

typedef long double ld;

#define mp make_pair

#define f first

#define s second

#define pb push_back



const int N = 50 + 5;

const int M = 1e5 + 5;

const ll OO = 1e18;

const int mod = 1e9 + 7;



int n,k,v[N],ans;



int solve(int a, int b) {



    if(a+1+n-b>k) return 0;



    int rem = k - (a+1+n-b);



    vector<int> x,y;



    int sum = 0;



    for (int i = 0; i <= a; ++i) {

        x.pb(v[i]);

        sum += x.back();

    }



    for (int i = n-1; i >= b; --i) {

        y.pb(v[i]);

        sum += y.back();

    }



    int ret = sum;



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

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



    for (int i = 0; i <= a+1; ++i) {

        for (int j = 0; j <= n-b; ++j) {



            if(rem<i+j) continue;



            int newRet = sum;



            for (int l = 0; l < i; ++l) {

                newRet -= x[l];

            }

            for (int k = 0; k < j; ++k) {

                newRet -= y[k];

            }



            ret = max(ret, newRet);



        }

    }



    return ret;



}



void init() {



}



int main() {



    ios_base::sync_with_stdio(false);

    cin.tie(0);



    cin >> n >> k;



    for (int i = 0; i < n; ++i) {

        cin >> v[i];

    }



    for (int i = -1; i < n; ++i) {

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

            ans = max(ans, solve(i,j));

        }

    }



    cout << ans << '\n';



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define ll long long



const int N = 1e6+3,M=55;



const ll oo=1e18;



int n,m,ans,tc,a[N],b[N],k;



#define f first



#define s second



const int dx[]={0,0,1,-1},

          dy[]={1,-1,0,0};



char s[N];



ll dp[105][105][105];



ll solve(int l,int r,int rem){

    if (rem<0)

        return -1e9;

    if (l>r)

        return 0;

    ll &ret = dp[l][r][rem];

    if (ret!=-oo)

        return ret;

    ret=0;

    ret=max(ret,max(a[l]+solve(l+1,r,rem-1),solve(l+1,r,rem-2)));

    return ret=max(ret,max(a[r]+solve(l,r-1,rem-1),solve(l,r-1,rem-2)));

}



int main(){

    for(int i=0;i<105;i++)

        for(int j=0;j<105;j++)

            for(int k=0;k<105;k++)

                dp[i][j][k]=-oo;

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

    for(int i=0;i<n;i++)

        scanf("%d",&a[i]);

    printf("%lld",solve(0,n-1,k));

}






// Faster C++ solution for the same problem (measured speedup: 10.44876522484408x):
#include<stdio.h>

#include<string.h>

#include<iostream>

#include<algorithm>

#include<queue>

#include<vector>

#define ll long long

using namespace std;

int main()

{

	int n,k,sum_max=-99999999,i,j,a,b,c;

	int f[1000];

	cin>>n>>k;

	for(i=0;i<n;i++)

	{

		cin>>f[i];

	}

	for(a=0;a<=k;a++)

	{

		for(b=0;b<=k;b++)

		{

			for(c=0;c<=k;c++)

			{

				if(a+b+c<=k && a+b>=c && a+b<=n)

				{

					priority_queue<int>q;

					for(i=0;i<a;i++)

					{

						q.push(f[i]);

					}

					for(i=0;i<b;i++)

					{

						q.push(f[n-i-1]);

					}

					int sum=0;

					for(i=0;i<a+b-c;i++)

					{

						sum+=q.top();

						q.pop();

					}

					sum_max=max(sum_max,sum);

				}

			}

		}

	}

	cout<<sum_max<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include <bits/stdc++.h>

#define ll long long

#define ld long double

#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int n, a[60], k, ans;

vector<int> v[60][60];

int main()

{

    IO

    cin>>n>>k;

    for(int i=1; i<=n; i++)

        cin>>a[i];

    for(int i=0; i<=n; i++)

        for(int j=0; j<=n; j++)

        {

            for(int ii=1; ii<=i; ii++)

                v[i][j].push_back(a[ii]);

            for(int ii=n; ii>=n-j+1; ii--)

                v[i][j].push_back(a[ii]);

            sort(v[i][j].begin(),v[i][j].end());

        }

    for(int i=0; i<=k; i++)

        for(int j=0; j<=k; j++)

            for(int r=0; r<=k; r++)

            {

                if(i+j+r>k || i+j>n || r>i+j)

                    continue;

                int sum=0;

                for(auto t : v[i][j])

                    sum+=t;

                for(int t=0; t<r; t++)

                    sum-=v[i][j][t];

                ans=max(ans,sum);

            }

    cout<<ans;



}


// Faster C++ solution for the same problem (measured speedup: 1.098936156470327x):
#include<stdio.h>

#include<string.h>

#include<iostream>

#include<algorithm>

#include<queue>

#include<vector>

#define ll long long

using namespace std;

int main()

{

	int n,k,sum_max=-99999999,i,j,a,b,c;

	int f[1000];

	cin>>n>>k;

	for(i=0;i<n;i++)

	{

		cin>>f[i];

	}

	for(a=0;a<=k;a++)

	{

		for(b=0;b<=k;b++)

		{

			for(c=0;c<=k;c++)

			{

				if(a+b+c<=k && a+b>=c && a+b<=n)

				{

					priority_queue<int>q;

					for(i=0;i<a;i++)

					{

						q.push(f[i]);

					}

					for(i=0;i<b;i++)

					{

						q.push(f[n-i-1]);

					}

					int sum=0;

					for(i=0;i<a+b-c;i++)

					{

						sum+=q.top();

						q.pop();

					}

					sum_max=max(sum_max,sum);

				}

			}

		}

	}

	cout<<sum_max<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define all(v) ((v).begin()),((v).end())

#define ll long long

#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-12, PI = acos(-1);

const int N = 2e5 + 9, M = 3e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;

const ll inf = 1e18;



ll mem[109][109][109], arr[N];

int n, m;



ll solve(int l, int r, int steps) {

  if(steps < 0)

    return -inf;

  if(steps == 0 || l > r)

    return 0;

  ll& ret = mem[l][r][steps];

  if(~ret)

    return ret;

  ret = -inf;

  ll c1 = solve(l + 1, r, steps - 1) + arr[l];

  ll c2 = solve(l, r - 1, steps - 1) + arr[r];

  ll c3 = solve(l + 1, r, steps - 2);

  ll c4 = solve(l, r - 1, steps - 2);

  ret = max(max(c1, c2), max(c3, c4));

  return ret;

}



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif

  cin >> n >> m;

  for (int i = 0; i < n; ++i) {

    cin >> arr[i];

  }

  memset(mem, -1, sizeof mem);

  ll best = -inf;

  for (int i = 0; i <= m; ++i) {

    best = max(best, solve(0, n - 1, i));

  }

  cout << best;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 11.865576128181488x):
#include<iostream>

#include<cstdio>

#include<queue>

#include<string>

#include<stack>

#include<algorithm>

#include<cstring>

#include<map>

using namespace std;

long long num[100];

int n,k;

priority_queue<long long,vector<long long>,greater<long long> >q;

long long solve(int l,int r)

{

	while(!q.empty())q.pop();

	long long ans=0;

	for(int i=1;i<=l;i++)

	{

		ans+=num[i];

		q.push(num[i]);

	}

	for(int i=n;i>=n-r+1;i--)

	{

		ans+=num[i];

		q.push(num[i]);

	}

	for(int i=0;i<k-l-r;i++)

	{

		if(q.empty())break;

		ans-=min(q.top(),(long long)0);

		q.pop();

	}

	return ans;

}

int main()

{

	cin>>n>>k;

	for(int i=1;i<=n;i++)

	{

		cin>>num[i];

	}

	long long maxn=0;

	for(int i=0;i<=k;i++)

	{

		for(int j=0;(i+j)<=min(n,k);j++)

		{

			maxn=max(solve(i,j),maxn);

		}

	}

	cout<<maxn;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define all(v) ((v).begin()),((v).end())

#define ll long long

#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-12, PI = acos(-1);

const int N = 2e5 + 9, M = 3e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;

const ll inf = 1e18;



ll mem[109][109][109], arr[N];

int n, m;



ll solve(int l, int r, int steps) {

  if(steps < 0)

    return -inf;

  if(steps == 0 || l > r)

    return 0;

  ll& ret = mem[l][r][steps];

  if(~ret)

    return ret;

  ret = -inf;

  ll c1 = solve(l + 1, r, steps - 1) + arr[l];

  ll c2 = solve(l, r - 1, steps - 1) + arr[r];

  ll c3 = solve(l + 1, r, steps - 2);

  ll c4 = solve(l, r - 1, steps - 2);

  ret = max(max(c1, c2), max(c3, c4));

  return ret;

}



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif

  cin >> n >> m;

  for (int i = 0; i < n; ++i) {

    cin >> arr[i];

  }

  memset(mem, -1, sizeof mem);

  ll best = -inf;

  for (int i = 0; i <= m; ++i) {

    best = max(best, solve(0, n - 1, i));

  }

  cout << best;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 12.042562465681039x):
#include<bits/stdc++.h>

using namespace std;

int a[60];

vector<int>V; 

int main()

{

	int n,k,ans=0;

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

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<=min(n,k);i++)//枚举左 

	{		

		for(int j=0;j<=min(n,k);j++)//枚举右 

		{

			if(i+j>k || i+j>n)

				continue;

			int kk=k-i-j;//防回去		

			V.clear();

			for(int l=0;l<i;l++)

				V.push_back(a[l]);

			for(int l=0;l<j;l++)

				V.push_back(a[n-l-1]);

		/*	cout<<i<<" "<<j<<" ###\n";

		*	for(int l=0;l<V.size();l++)

				cout<<V[l]<<" " ;

			cout<<endl;*/

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

			int sum=0;

			for(int l=0;l<V.size();l++)

				sum+=V[l];

		//	cout<<sum<<" "<<i<<" "<<j<<" "<<kk<<endl;

			for(int l=0;l<V.size() && kk>0;l++)

			{

				if(V[l]<0)

					sum-=V[l];

				else

					break;

				kk--;

			}

			 

			ans=max(ans,sum);

		}

	} 			

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

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define all(v) ((v).begin()),((v).end())

#define ll long long

#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-12, PI = acos(-1);

const int N = 2e5 + 9, M = 3e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;

const ll inf = 1e18;



ll mem[109][109][109], arr[N];

int n, m;



ll solve(int l, int r, int steps) {

  if(steps < 0)

    return -inf;

  if(steps == 0 || l > r)

    return 0;

  ll& ret = mem[l][r][steps];

  if(~ret)

    return ret;

  ret = -inf;

  ll c1 = solve(l + 1, r, steps - 1) + arr[l];

  ll c2 = solve(l, r - 1, steps - 1) + arr[r];

  ll c3 = solve(l + 1, r, steps - 2);

  ll c4 = solve(l, r - 1, steps - 2);

  ret = max(max(c1, c2), max(c3, c4));

  return ret;

}



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif

  cin >> n >> m;

  for (int i = 0; i < n; ++i) {

    cin >> arr[i];

  }

  memset(mem, -1, sizeof mem);

  ll best = -inf;

  for (int i = 0; i <= m; ++i) {

    best = max(best, solve(0, n - 1, i));

  }

  cout << best;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 12.854145609507881x):
#include<stdio.h>

#include<vector>

#include<algorithm>

using namespace std;

 

const int maxn = 55;

int A[maxn], sum[maxn]={0};

int n,k;//n块羊肉   k次操作 

vector<int> vt;



int main(){

//	freopen("d.txt","r",stdin);

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

	for(int i=0;i<n;i++){

		scanf("%d", &A[i]);

		sum[i] += A[i];//0~i累积的和 

	}

	int r = min(n,k);

	int MAX = 0;

	for(int x=0;x<=r;x++){

		for(int y=0;x+y<=r;y++){

			int remain = min(k-x-y,x+y);//放回去的次数

			vt.clear();

			int sum = 0;

			for(int i=0;i<x;i++){//左边x个

				vt.push_back(A[i]);

				sum += A[i];

			} 

			for(int j=0,i=n-1;j<y;j++,i--){//右边y个

				vt.push_back(A[i]);

				sum += A[i];

			}

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

			for(int j=0;j<remain;j++){//放回去 

				if(vt[j]<0){

					sum -= vt[j];

				}else{

					break;

				}

			}

			MAX = max(MAX,sum); 

		}

	} 

	printf("%d\n", MAX);

	

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

#include <map>

#include <set>

#include <queue>



using namespace std;



int main(){



	int n,m;

	vector<int> p;

	map<int,vector<int>> xy;

	char flag[100010];



	for(int i=0;i<100010;i++){

		flag[i]=0;

	}



	cin>>n>>m;

	for(int i=0;i<n;i++){

		int tmp;

		cin>>tmp;

		p.push_back(tmp-1);

	}

	for(int i=0;i<m;i++){

		int tmpX,tmpY;

		cin>>tmpX>>tmpY;

		xy[tmpX-1].push_back(tmpY-1);

		xy[tmpY-1].push_back(tmpX-1);

	}



	/*学習実施、どことどこが繋がってるのか？*/

	

	int ans=0;

	for(int i=0;i<n;i++){

		if(flag[i]==1)continue;

		queue<int> q;

		q.push(i);

		set<int> touch;

		touch.insert(i);

		flag[i]=1;

		while(q.size()!=0){

			int now = q.front();

			q.pop();

			if(xy[now].size()==0)continue;

			int size = xy[now].size();

			for(int j=0;j<size;j++){

				int next = xy[now].at(j);

				if(flag[next]==0){

					touch.insert(next);

					flag[next]=1;

					q.push(next);

				}

			}

		}

		for(auto itr = touch.begin(); itr != touch.end();itr++){

			int targetPos = p[*itr];

			if(touch.count(targetPos)==1){

				ans++;

			}

		}

	}

	cout<<ans<<endl;

	return 0;



}

// Faster C++ solution for the same problem (measured speedup: 1.0999577036146888x):
#include <bits/stdc++.h>

 

using namespace std;

 

#define lli long long int

#define lld long double

#define REP(i,s,n) for(lli i=s;i<n;i++)

#define RREP(i,s,n) for(lli i=s;i>=n;i--)

#define MOD 1000000007

#define NUM 2520

#define DEBUG 0

#define mp(a,b) make_pair(a,b)

#define SORT(V) sort(V.begin(),V.end())

#define PI (3.141592653589794)

#define INF (1LL<<50)



typedef struct UnionFind{

	vector<lli> par; //par[i]:iの親番号 par[3] = 2 : 3の親が2

	vector<lli> size;//size[i]:iの大きさ 自分のみのときは1,根っこにsizeを持たせる*/



	UnionFind(int N){/*最初はすべてが根であると初期化*/

		REP(i,0,N){

			par.push_back(i);

			size.push_back(1);

		}

	}



	lli root(lli x){/*データxが属する木の根を再起で得る root(x) = {xの木の根}}*/

		if(par[x] == x)return x;

		return par[x] = root(par[x]);

	}



	void unite(lli x,lli y){/*xとyの併合*/

		lli rx = root(x);

		lli ry = root(y);

		if(rx == ry)return;

		par[rx] = ry; /*xとyの根が同じじゃない: xの根をrxをyの根ryに変更する */

		lli resize = size[rx] + size[ry];

		size[rx] = resize;

		size[ry] = resize;

	}



	bool same(lli x,lli y){

		lli rx = root(x);

		lli ry = root(y);

		return rx == ry;

	}



	lli getSize(lli x){

		lli rx = root(x);

		return size[rx];

	}

};



signed main(){



	lli n,m;

	cin>>n>>m;

	vector<lli> p(n);

	map<lli,lli> ind;

	UnionFind uf(n);

	REP(i,0,n){

		cin>>p[i];

		p[i]--;

		ind[p[i]] =i;

	}



	REP(i,0,m){

		lli x,y;

		cin>>x>>y;

		x--,y--;

		uf.unite(x,y);

	}



	lli cnt=0;

	REP(i,0,n){

		lli nowInd = p[i];

		if(uf.same(i,nowInd))cnt++;

	}



	cout<<cnt<<endl;



	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int P[100001];

int Find(int A) {

	if (P[A] < 0) return A;

	return P[A] = Find(P[A]);

}



bool Unite(int A, int B) {

	int a = Find(A);

	int b = Find(B);

	if (a == b) return false;

	if (P[a] > P[b]) swap(a, b);

	P[a] += P[b];

	P[b] = a;

	return true;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int ip[100001];

	rep1(i, N) ip[getint()] = i;



	rep1(i, N) P[i] = -1;

	rep(i, M) Unite(getint(), getint());



	int kotae = 0;

	rep1(i, N) kotae += Find(i) == Find(ip[i]);



	co(kotae);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.1983180921693455x):
//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int P[100001];

int Find(int A) {

	if (P[A] < 0) return A;

	return P[A] = Find(P[A]);

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int p[100001];

	rep1(i, N) p[i] = getint();



	rep1(i, N) P[i] = -1;

	rep(i, M) {

		int a = Find(getint());

		int b = Find(getint());

		if (a == b) continue;

		if (P[a] > P[b]) swap(a, b);

		P[a] += P[b];

		P[b] = a;

	}



	int kotae = 0;

	rep1(i, N) kotae += Find(i) == Find(p[i]);



	printf("%d", kotae);



	Would you please return 0;

}