Language: C++

int do_cycle(vector<int> &v,int x){
	int s=0,m=1<<30;
	for(auto &e:v)s+=e,m=min(m,e);
	return min(s+(v.size()-2)*m,s+m+(v.size()+1)*x);
}

int main(){
	int N,x=1<<30,r;
	scanf("%d",&N);
	vector<int>v(N+1),b(N+1);
	vector<vector<int> >cycles;
	for(int i=1;i<=N;i++)scanf("%d",&v[i]),x=min(x,v[i]);
	for(int i=1;i<=N;i++)if(!b[i]){
		vector<int>cycle;
		int k=i;
		do{
			cycle.push_back(k);
			b[k]=1;
			k=v[k];
		}while(k!=cycle[0]);
		r+=do_cycle(cycle,x);
	}
	printf("%d\n",r);
}

Compiler output:
a.cc:1:14: error: 'vector' was not declared in this scope
    1 | int do_cycle(vector<int> &v,int x){
      |              ^~~~~~
a.cc:1:21: error: expected primary-expression before 'int'
    1 | int do_cycle(vector<int> &v,int x){
      |                     ^~~
a.cc:1:29: error: expected primary-expression before 'int'
    1 | int do_cycle(vector<int> &v,int x){
      |                             ^~~
a.cc:1:34: error: expression list treated as compound expression in initializer [-fpermissive]
    1 | int do_cycle(vector<int> &v,int x){
      |                                  ^
a.cc: In function 'int main()':
a.cc:9:9: error: 'scanf' was not declared in this scope
    9 |         scanf("%d",&N);
      |         ^~~~~
a.cc:10:9: error: 'vector' was not declared in this scope
   10 |         vector<int>v(N+1),b(N+1);
      |         ^~~~~~
a.cc:10:16: error: expected primary-expression before 'int'
   10 |         vector<int>v(N+1),b(N+1);
      |                ^~~
a.cc:11:23: error: expected primary-expression before 'int'
   11 |         vector<vector<int> >cycles;
      |                       ^~~
a.cc:12:42: error: 'v' was not declared in this scope
   12 |         for(int i=1;i<=N;i++)scanf("%d",&v[i]),x=min(x,v[i]);
      |                                          ^
a.cc:12:50: error: 'min' was not declared in this scope; did you mean 'main'?
   12 |         for(int i=1;i<=N;i++)scanf("%d",&v[i]),x=min(x,v[i]);
      |                                                  ^~~
      |                                                  main
a.cc:13:34: error: 'b' was not declared in this scope
   13 |         for(int i=1;i<=N;i++)if(!b[i]){
      |                                  ^
a.cc:14:24: error: expected primary-expression before 'int'
   14 |                 vector<int>cycle;
      |                        ^~~
a.cc:17:25: error: 'cycle' was not declared in this scope; did you mean 'do_cycle'?
   17 |                         cycle.push_back(k);
      |                         ^~~~~
      |                         do_cycle
a.cc:19:27: error: 'v' was not declared in this scope
   19 |                         k=v[k];
      |                           ^
a.cc:20:27: error: 'cycle' was not declared in this scope; did you mean 'do_cycle'?
   20 |                 }while(k!=cycle[0]);
      |                           ^~~~~
      |                           do_cycle
a.cc:21:28: error: 'do_cycle' cannot be used as a function
   21 |                 r+=do_cycle(cycle,x);
      |                    ~~~~~~~~^~~~~~~~~
a.cc:23:9: error: 'printf' was not declared in this scope
   23 |         printf("%d\n",r);
      |         ^~~~~~
a.cc:1:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | int do_cycle(vector<int> &v,int x){