#include <vector>
#define SOUTH 0
#define EAST 1
#define NORTHWEST 2
using namespace std;
vector<int> solution(int n) {
vector<int> answer;
vector<vector<int>> arr(n + 1, vector<int>(n + 1));
int i, j;
int direction, nowR, nowC;
i = 1, nowR = 1, nowC = 1;
arr[nowR++][nowC] = i++;
direction = SOUTH;
while (i <= n * (n + 1) / 2) {
if (direction == SOUTH) {
while (nowR <= n && arr[nowR][nowC] == 0) {
arr[nowR++][nowC] = i++;
}
nowR--;
nowC++;
direction = EAST;
}
if (direction == EAST) {
while (nowC <= nowR && arr[nowR][nowC] == 0) {
arr[nowR][nowC++] = i++;
}
nowC--;
nowR--;
nowC--;
direction = NORTHWEST;
}
if (direction == NORTHWEST) {
while (1 <= nowR && nowR <= n && 1 <= nowC && nowC <= nowR && arr[nowR][nowC] == 0) {
arr[nowR--][nowC--] = i++;
}
nowR++;
nowC++;
nowR++;
direction = SOUTH;
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) answer.push_back(arr[i][j]);
}
return answer;
}