#include <iostream>
#include <algorithm>

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pivot = arr[high];
        int i = low - 1;

        for (int j = low; j < high; j++) {
            if (arr[j] < pivot) {
                i++;
                std::swap(arr[i], arr[j]);
            }
        }
        std::swap(arr[i + 1], arr[high]);
        int partitionIndex = i + 1;

        quickSort(arr, low, partitionIndex - 1);
        quickSort(arr, partitionIndex + 1, high);
    }
}

int main() {
    int arr[] = {12, -5, 3, 7, 8, -2, 4, 1, -1, 6};
    int n = sizeof(arr) / sizeof(arr[0]);

    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    double average = sum / n;

    if (average > 0) {
        quickSort(arr, 0, (2 * n / 3) - 1);
    } else {
        quickSort(arr, 0, (n / 3) - 1);
    }

    for (int i = (2 * n / 3); i < n; i++) {
        std::swap(arr[i], arr[n - 1 - (i - (2 * n / 3))]);
    }

    std::cout << "Результат: ";
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

