Language: C++

#pragma GCC optimize("O2,unroll-loops")
#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:58:11: error: redefinition of 'const int maxn'
   58 | const int maxn = 506;
      |           ^~~~
a.cc:7:11: note: 'const int maxn' previously defined here
    7 | const int maxn = 506;
      |           ^~~~
a.cc:59:5: error: redefinition of 'int mat [506][506]'
   59 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |     ^~~
a.cc:8:5: note: 'int mat [506][506]' previously declared here
    8 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |     ^~~
a.cc:59:22: error: redefinition of 'int pres [506][506]'
   59 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                      ^~~~
a.cc:8:22: note: 'int pres [506][506]' previously declared here
    8 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                      ^~~~
a.cc:59:40: error: redefinition of 'int n'
   59 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                                        ^
a.cc:8:40: note: 'int n' previously declared here
    8 | int mat[maxn][maxn], pres[maxn][maxn], n;
      |                                        ^
a.cc:60:5: error: redefinition of 'int dx []'
   60 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |     ^~
a.cc:9:5: note: 'int dx [4]' previously defined here
    9 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |     ^~
a.cc:60:27: error: redefinition of 'int dy []'
   60 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |                           ^~
a.cc:9:27: note: 'int dy [4]' previously defined here
    9 | int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      |                           ^~
a.cc:61:6: error: redefinition of 'void rem(int, int)'
   61 | void rem(int x, int y) {
      |      ^~~
a.cc:10:6: note: 'void rem(int, int)' previously defined here
   10 | void rem(int x, int y) {
      |      ^~~
a.cc:79:5: error: redefinition of 'int main()'
   79 | int main() {
      |     ^~~~
a.cc:28:5: note: 'int main()' previously defined here
   28 | int main() {
      |     ^~~~