#include<iostream>
#include<math.h>
using namespace std;

int maxTripletSum(int arr[], int n)
{
    // Initialize the answer
    int ans = 0;

    for (int i = 1; i < n - 1; ++i) {
        int max1 = 0, max2 = 0;

        // find maximum value(less than arr[i])
        // from i+1 to n-1
        for (int j = 0; j < i; ++j)
            if (arr[j] < arr[i])
                max1 = max(max1, arr[j]);

        // find maximum value(greater than arr[i])
        // from i+1 to n-1
        for (int j = i + 1; j < n; ++j)
            if (arr[j] > arr[i])
                max2 = max(max2, arr[j]);

        // store maximum answer
        ans = max(ans, max1 + arr[i] + max2);
    }

    return ans;
}

int main() {
   int k, i, j;
   int matrix[10][10];
   cout << "Enter the size of the matrix:\n";
   cin >> k;
   cout << "Enter the elements of the matrix:\n";
   for (i = 0; i < k; i++)
      for (j = 0; j < k; j++)
         cin >> matrix[i][j];

cout << "\n";
 int arr[] = { 2, 3, 1, 2, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxTripletSum(arr, n);
    return 0;

   return 0;
}