Language: C++

#include <bits/stdc++.h>

int main()
{
	int N;
	cin >> N;
	vector<vector<int>> v(N);
	vector<int> x(N);
	int half = N / 2;
	for (int i = 0; i <= N/2; ++i)
	{
		
		x[i] = i;
	}
	int j = 0;
	for (int i = N - 1; i >= N / 2; --i)
	{
		x[i] = j;
		++j;
	}
	for (int i = 0; i < N; ++i)
	{
		v[i] = x;
	}
	for (int i = 0; i < N; ++i)
	{
		for (int j = 0; j < N; ++j)
		{
			v[i][j] = min(v[i][j], v[j][i]);
		}
	}
	int ans = 0,total=N*N,a,b,val,copy,ac,bc;
	for (int i = 0; i < total; ++i)
	{
		cin >> j;
		--j;
		a= j / N;
		b = j % N;
		ac = a;
		bc = b;
		ans += v[a][b];
		val = v[a][b];
		if (a > N / 2)
		{
			copy = val;
			while (a > 0)
			{
				--a;

				if (v[a][b] == -1)
				{
					continue;
				}
				v[a][b] = min(v[a][b],copy);
				++copy;
			}
		}
		else
		{
			copy = val;
			while (a <N-1)
			{
				++a;

				if (v[a][b] == -1)
				{
				
					continue;
				}
				v[a][b] = min(v[a][b], copy);
				++copy;
			}
		}
		a = ac;
		b = bc;
		if (b > N / 2)
		{
			copy = val;
			while (b> 0)
			{
				--b;

				if (v[a][b] == -1)
				{
					continue;
				}
				v[a][b] = min(v[a][b], copy);
				++copy;
			}
		}
		else
		{
			copy = val;
			while (b < N-1)
			{
				++b;

				if (v[a][b] == -1)
				{
					continue;
				}
				v[a][b] = min(v[a][b], copy);
				++copy;
			}
		}

		v[ac][bc] = -1;
	}
	cout << ans << endl;

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:6:9: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
    6 |         cin >> N;
      |         ^~~
      |         std::cin
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:146,
                 from a.cc:1:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:7:9: error: 'vector' was not declared in this scope
    7 |         vector<vector<int>> v(N);
      |         ^~~~~~
a.cc:7:9: note: suggested alternatives:
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53:
/usr/include/c++/14/bits/stl_vector.h:428:11: note:   'std::vector'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
/usr/include/c++/14/vector:93:13: note:   'std::pmr::vector'
   93 |       using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
      |             ^~~~~~
a.cc:7:23: error: expected primary-expression before 'int'
    7 |         vector<vector<int>> v(N);
      |                       ^~~
a.cc:8:16: error: expected primary-expression before 'int'
    8 |         vector<int> x(N);
      |                ^~~
a.cc:13:17: error: 'x' was not declared in this scope
   13 |                 x[i] = i;
      |                 ^
a.cc:18:17: error: 'x' was not declared in this scope
   18 |                 x[i] = j;
      |                 ^
a.cc:23:17: error: 'v' was not declared in this scope
   23 |                 v[i] = x;
      |                 ^
a.cc:23:24: error: 'x' was not declared in this scope
   23 |                 v[i] = x;
      |                        ^
a.cc:29:25: error: 'v' was not declared in this scope
   29 |                         v[i][j] = min(v[i][j], v[j][i]);
      |                         ^
a.cc:29:35: error: 'min' was not declared in this scope; did you mean 'std::min'?
   29 |                         v[i][j] = min(v[i][j], v[j][i]);
      |                                   ^~~
      |                                   std::min
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:41:24: error: 'v' was not declared in this scope
   41 |                 ans += v[a][b];
      |                        ^
a.cc:54:43: error: 'min' was not declared in this scope; did you mean 'std::min'?
   54 |                                 v[a][b] = min(v[a][b],copy);
      |                                           ^~~
      |                                           std::min
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:70:43: error: 'min' was not declared in this scope; did you mean 'std::min'?
   70 |                                 v[a][b] = min(v[a][b], copy);
      |                                           ^~~
      |                                           std::min
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:87:43: error: 'min' was not declared in this scope; did you mean 'std::min'?
   87 |                                 v[a][b] = min(v[a][b], copy);
      |                                           ^~~
      |                                           std::min
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:102:43: error: 'min' was not declared in this scope; did you mean 'std::min'?
  102 |                                 v[a][b] = min(v[a][b], copy);
      |                                           ^~~
      |                                           std::min
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:109:9: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
  109 |         cout << ans << endl;
      |         ^~~~
      |         std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:109:24: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
  109 |         cout << ans << endl;
      |                        ^~~~
      |                        std::endl
In file included from /usr/include/c++/14/istream:41,
                 from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~