Language: C++

#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,sse,tune=native")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
const int maxn = 506;
int mat[maxn][maxn], pres[maxn][maxn], n;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void rem(int x, int y) {
	queue<pair<int, int>> a;
	pres[x][y] = 0;
	a.push({x, y});
	mat[x][y]--;
	while(!a.empty()) {
		int x, y;
		tie(x, y) = a.front();
		a.pop();
		for(int i = 0; i < 4; i++) {
			int tx = x + dx[i], ty = y + dy[i];
			if(tx>=0&&tx<=n&&ty>=0&&ty<=n&&mat[tx][ty]>mat[x][y]+pres[tx][ty]) {
				mat[tx][ty] = mat[x][y]+pres[tx][ty];
				a.push({tx, ty});
			}
		}
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;++n;
	for(int i = 1; i < n; i++)
		for(int j = 1; j < n; j++) {
			mat[i][j] = min({i, n-i, j, n-j});
			pres[i][j] = 1;
		}
	ll ans = 0;
	for(int v, i = 0; i < (n-1)*(n-1); i++) {
		cin >> v;
		ans += mat[1 + (v-1)/(n-1)][1 + (v-1)%(n-1)] - 1;
		rem(1 + (v-1)/(n-1), 1 + (v-1)%(n-1));
		/**
		cout << 1 + (v-1)/(n-1) << " " << 1 + (v-1)%(n-1) << "=======\n";
		for(int i = 0; i <= n; i++, cout << '\n')
			for(int j = 0; j <= n; j++)
				cout << mat[i][j] << " ";
		cout << ans << "=======\n";
		***/
	}
	cout << ans;
}
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,sse,tune=native")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
const int maxn = 506;
int mat[maxn][maxn], pres[maxn][maxn], n;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void rem(int x, int y) {
	queue<pair<int, int>> a;
	pres[x][y] = 0;
	a.push({x, y});
	mat[x][y]--;
	while(!a.empty()) {
		int x, y;
		tie(x, y) = a.front();
		a.pop();
		for(int i = 0; i < 4; i++) {
			int tx = x + dx[i], ty = y + dy[i];
			if(tx>=0&&tx<=n&&ty>=0&&ty<=n&&mat[tx][ty]>mat[x][y]+pres[tx][ty]) {
				mat[tx][ty] = mat[x][y]+pres[tx][ty];
				a.push({tx, ty});
			}
		}
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;++n;
	for(int i = 1; i < n; i++)
		for(int j = 1; j < n; j++) {
			mat[i][j] = min({i, n-i, j, n-j});
			pres[i][j] = 1;
		}
	ll ans = 0;
	for(int v, i = 0; i < (n-1)*(n-1); i++) {
		cin >> v;
		ans += mat[1 + (v-1)/(n-1)][1 + (v-1)%(n-1)] - 1;
		rem(1 + (v-1)/(n-1), 1 + (v-1)%(n-1));
		/**
		cout << 1 + (v-1)/(n-1) << " " << 1 + (v-1)%(n-1) << "=======\n";
		for(int i = 0; i <= n; i++, cout << '\n')
			for(int j = 0; j <= n; j++)
				cout << mat[i][j] << " ";
		cout << ans << "=======\n";
		***/
	}
	cout << ans;
}


Compiler output:
a.cc:59:11: error: redefinition of 'const int maxn'
   59 | const int maxn = 506;
      |           ^~~~
a.cc:8:11: note: 'const int maxn' previously defined here
    8 | const int maxn = 506;
      |           ^~~~
a.cc:60:5: error: redefinition of 'int mat [506][506]'
   60 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |     ^~~
a.cc:9:5: note: 'int mat [506][506]' previously declared here
    9 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |     ^~~
a.cc:60:22: error: redefinition of 'int pres [506][506]'
   60 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                      ^~~~
a.cc:9:22: note: 'int pres [506][506]' previously declared here
    9 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                      ^~~~
a.cc:60:40: error: redefinition of 'int n'
   60 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                                        ^
a.cc:9:40: note: 'int n' previously declared here
    9 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                                        ^
a.cc:61:5: error: redefinition of 'int dx []'
   61 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |     ^~
a.cc:10:5: note: 'int dx [4]' previously defined here
   10 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |     ^~
a.cc:61:27: error: redefinition of 'int dy []'
   61 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |                           ^~
a.cc:10:27: note: 'int dy [4]' previously defined here
   10 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |                           ^~
a.cc:62:6: error: attribute value 'tune=native' was already specified in 'target' attribute
   62 | void rem(int x, int y) {
      |      ^~~
a.cc:62:6: error: redefinition of 'void rem(int, int)'
a.cc:11:6: note: 'void rem(int, int)' previously defined here
   11 | void rem(int x, int y) {
      |      ^~~
a.cc:80:5: error: attribute value 'tune=native' was already specified in 'target' attribute
   80 | int main() {
      |     ^~~~
a.cc:80:5: error: redefinition of 'int main()'
a.cc:29:5: note: 'int main()' previously defined here
   29 | int main() {
      |     ^~~~