// Function to calculate the result based on the given array
int calc(int *arr, int n) {
int ret = 0;
for (int i = 0; i < n; i++) {
int left = 0;
int right = 0;
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
left++;
}
}
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
right++;
}
}
ret += left * right;
}
return ret;
}
// Function to reverse an array
void reverseArray(int *arr, int *reversed, int n) {
for (int i = 0; i < n; i++) {
reversed[i] = arr[n - 1 - i];
}
}
int main() {
int n;
int *level;
// Read input
int *input = malloc(1000 * sizeof(int)); // Assuming a maximum of 1000 elements for simplicity
int count = 0;
printf("Enter numbers separated by spaces (end with newline):\n");
while (scanf("%d", &input[count]) == 1) {
count++;
}
// Handle the case where the first number is the length of the array
if (count > 1 && input[0] == count - 1) {
n = count - 1;
level = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
level[i] = input[i + 1];
}
} else {
n = count;
level = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
level[i] = input[i];
}
}
// Calculate the result
int *reversed = malloc(n * sizeof(int));
reverseArray(level, reversed, n);
int result = calc(level, n) + calc(reversed, n);
printf("Result: %d\n", result);
// Free allocated memory
free(input);
free(level);
free(reversed);
return 0;
}