#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, a[2][200000], temp[2];
  
  cin >> n;
  
  for (int i = 0; i < n; i++) {
    cin >> a[0][i];
    a[1][i] = i;
  }
  
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[0][i] < a[0][j]) {
        temp[0] = a[0][i];
        temp[1] = a[1][i];
        a[0][i] = a[0][j];
        a[1][i] = a[1][j];
        a[0][j] = temp[0];
        a[1][j] = temp[1];
      }
    }
  }
  
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (a[1][i] - a[1][j] == 1 || a[1][i] - a[1][j] == -1) {
        continue;
      }
      
      for (int k = j + 1; k < n; k++) {
        if (a[1][k] - a[1][j] == 1 || a[1][k] - a[1][j] == -1) {
          continue;
        }
        
        if (a[1][k] - a[1][i] == 1 || a[1][k] - a[1][i] == -1) {
          continue;
        }
        
        cout << a[0][i] + a[0][j] + a[0][k] << endl;
        
        return 0;
      }
    }
  }
  
  return 0;
}
