eolymp
bolt
Try our new interface for solving problems
Problems

Sum of neighbors

Sum of neighbors

In a square area of size $n \times n$, cells are numbered sequentially from $0$ to $(n^2 - 1)$ from the top row to the bottom, from left to right in each row. Nyasyr stands in the cell with number $k$ in this area and wants to find the sum of the numbers of all neighboring cells. Help Nyasyr with this task. Note that each cell can have no more than $4$ neighbors directly above, below, to the left, and to the right. Below is an example with $n = 4$ and $k = 11$. \includegraphics{https://eolympusercontent.com/images/r1qf3dpf5d7qdcafehucllr4qk.gif} Here Nyasyr stands on the cell with number $11$, and neighboring cells are marked with circles. In this case, there is no neighbor on the right. \InputFile Two integers $n~(1 \le n \le 10^6)$ and $k~(0 \le k \le n^2)$. \OutputFile Print the sum of the numbers of all neighboring cells of the given cell.
Time limit 1 second
Memory limit 128 MiB
Input example #2
4 10
Output example #2
40
Input example #1
4 11
Output example #1
32
Source 2024, Azerbaijan, Republic Informatics Olympiad, Semifinals, 8 - 9 classes, February 18