#include <iostream>
#include <vector>

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 N;
int M;
int B1;
int B2;
int B3;
int B4;
int B5;
int B6;
int P;

cin >> N >> M;

cin >> B1 >> B2 >> B3 >> B4 >> B5 >> B6 ;

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