Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The value in row , column is not something you need to store. With 1-indexing, it is .
For one cell, its cost is just its own value plus up to four neighbors. So you can compute the cost directly for any chosen .
The grid is tiny: and . Checking every cell and every possible neighbor is only about operations total. That is absolutely nothing.
Be careful with borders and corners. A corner has only neighbors, an edge cell has , and an inner cell has .
Loop over all cells, try the four directions , add only valid neighbors, and keep the maximum cost.
The main trap is assuming the biggest number, , must give the biggest cost. Nope. The bottom-right corner has the largest cell value, but it only has two neighbors. Sometimes a slightly smaller cell with more neighbors wins. Borders matter. Annoying, but easy.
Cell Values
Use 1-indexed coordinates. The value at row , column is:
So we do not need to build the whole grid. We can compute any cell's value instantly.
Cost Of One Cell
A cell's cost is:
That is it. No diagonal nonsense.
For example, if and we choose the cell with value , it has neighbors , , and , so the cost is:
Why Brute Force Is Completely Fine
The constraints are tiny:
There are at most cells per test case, and at most test cases. For each cell, we check only directions.
So the total work is around:
That is baby food for C++. No need to summon some overengineered formula dragon.
Algorithm
For each test case:
answer = 0.i from to :j from to :cost as the value of .answer.answer.Correctness Proof
We need to prove the algorithm prints the maximum possible cell cost.
For any cell , the algorithm computes its own value using the formula . Then it checks exactly the four side-adjacent positions: up, down, left, and right. If such a position is inside the grid, the algorithm adds that neighbor's value to the current cost. Therefore, for every cell, the algorithm computes exactly the cost defined in the statement.
The algorithm iterates over every cell in the grid. Since every possible candidate cell is considered, and since the computed cost for each candidate is exact, taking the maximum over all computed costs gives exactly the maximum cost among all cells.
Therefore, the algorithm is correct.
Complexity
There are cells, and for each cell we check neighbors.
Time complexity:
Memory complexity:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
while (t--) {
int n;
cin >> n;
auto value = [&](int r, int c) -> ll {
return 1LL * (r - 1) * n + c;
};
ll ans = 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
ll cost = value(r, c);
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (1 <= nr && nr <= n && 1 <= nc && nc <= n) {
cost += value(nr, nc);
}
}
ans = max(ans, cost);
}
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
while (t--) {
int n;
cin >> n;
auto value = [&](int r, int c) -> ll {
return 1LL * (r - 1) * n + c;
};
ll ans = 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
ll cost = value(r, c);
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (1 <= nr && nr <= n && 1 <= nc && nc <= n) {
cost += value(nr, nc);
}
}
ans = max(ans, cost);
}
}
cout << ans << '\n';
}
return 0;
}