#define _USE_MATH_DEFINES

#include <array>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <utility>
#include <tuple>
#include <algorithm>
#include <limits>
#include <fstream>
#include <numeric>
#include <stack>
#include <queue>
#include <list>
#include <cstring>
#include <unordered_set>
#include <cmath>
#include <iomanip>

namespace
{
	template<int N>
	std::array<int, N> GetValue(std::istream& stream)
	{
		std::string buf;
		std::getline(stream, buf);

		std::stringstream sstr(buf);
		std::array<int, N> retval;

		for (int i = 0; i < N; ++i)
		{
			std::getline(sstr, buf, ' ');
			retval[i] = std::stoi(buf);
		}

		return retval;
	}

	std::vector<int> GetValue(std::istream& stream, int num, char delim)
	{
		std::string buf;
		std::getline(stream, buf);
		std::stringstream sstr(buf);

		std::vector<int> retval;
		retval.reserve(num);

		std::string input;;
		for (int i = 0; i < num && std::getline(sstr, input, delim); ++i)
		{
			retval.push_back(std::stoi(input));
		}

		return retval;
	}

	int GetValue(std::istream& stream)
	{
		int in;
		stream >> in;

		return in;
	}

	std::vector<std::string> GetValueString(std::istream& stream, int num, char delim)
	{
		std::string buf;
		std::vector<std::string> retval;
		retval.reserve(num);

		std::getline(stream, buf);
		std::stringstream sstr(buf);

		for (int i = 0; i < num && std::getline(sstr, buf, delim); ++i)
		{
			retval.push_back(std::move(buf));
		}

		return retval;
	}

	template<typename Itr>
	void PrintLine(Itr begin, Itr end)
	{
		bool first = true;

		for (int i = 0; begin != end; ++begin, ++i)
		{
			if (!first)
			{
				std::cout << ' ';
			}

			std::cout << (*begin);

			first = false;
		}
		std::cout << std::endl;
	}

	template<typename T>
	void Print(const std::vector<T>& sequence)
	{
		for (const auto& e : sequence)
		{
			std::cout << e << '\n';
		}

		std::cout << std::flush;
	}

	std::vector<std::vector<int>> GetCycles(const std::vector<int>& sequence)
	{
		std::vector<int> sorted(sequence);
		std::sort(sorted.begin(), sorted.end());
		std::vector<int> place;
		place.reserve(sequence.size());

		for (int e : sequence)
		{
			for (size_t i = 0; i < sorted.size(); ++i)
			{
				if (e == sorted[i])
				{
					place.push_back(i);
					break;
				}
			}
		}

		std::vector<bool> already(sequence.size(), false);
		std::vector<std::vector<int>> retval;
		for (size_t i = 0; i < place.size(); ++i)
		{
			if (place[i] == static_cast<int>(i))
				continue;

			if (already[i])
				continue;

			std::vector<int> cycle;
			for (int index = place[i], num = sequence[index];
				!already[i];
				index = place[index], num = sequence[index])
			{
				cycle.push_back(num);
				already[index] = true;
			}

			retval.push_back(std::move(cycle));
		}

		return retval;
	}

	int GetCost(const std::vector<int>& cycle, int min)
	{
		int cycle_min = *(std::min_element(cycle.begin(), cycle.end()));

		int cost1 = std::accumulate(cycle.begin(), cycle.end(), 0) + (cycle.size() - 2) * cycle_min;

		int cost2 = (cycle_min + min) * 2 - (cycle_min - min) * (cycle.size() - 1);

		return cost1 + std::min(0, cost2);
	}
}

int main(int argc, char* argv[])
{
	auto file = (argc == 1) ? std::ifstream() : std::ifstream(argv[1]);
	std::istream& stream = (argc == 1) ? std::cin : file;
	if (argc == 1)
	{
		std::cin.tie(0);
		std::ios::sync_with_stdio(false);
	}

	int num = GetValue<1>(stream)[0];
	std::vector<int> sequence = GetValue(stream, num, ' ');

	auto cycles = GetCycles(sequence);

	int cost = 0;
	int min = *(std::min_element(sequence.begin(), sequence.end()));

	for (const auto& e : cycles)
	{
		cost += GetCost(e, min);
	}

	std::cout << cost << std::endl;
}



Measured memory_bytes: 3244