#include <iostream>
using namespace std;
int a[100005];

int main() {
    int n,q = -1,count = 1;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int j = 0; j < n-1; j++) {
        if (a[j] <= a[n-1]) {
            q++;
            int v = a[j];
            a[j] = a[q];
            a[q] = v;
        }
    }
    int v = a[q+1];
    a[q+1] = a[n-1];
    a[n-1] = v;

    for (int k = 0; k < n; k++) {
        cout << (k == q+1?"[":"") << a[k] << (k == q+1?"]":"") << (k == n-1?"":" ");
    }
    cout << endl;
    return 0;
}



#include <iostream>
#include <algorithm>
using namespace std;

int main(){
  int n;
  int a[5000];
  while(cin >> n && n>0){
    for(int i=0;i<n;i++)
      cin >> a[i];
    long long sum=0,tmp=0,ma=-100000;
    int m=-100000;
    bool flg=true;
    for(int i=0;i<n;i++){
      if(flg) m=max(m,a[i]);
      if(a[i]>=0){
	flg=false;
	sum+=a[i];
	continue;
      }
      ma=ma>sum?ma:sum;
      for(tmp=sum;i<n && a[i]<0;i++){
	tmp+=a[i];
      }i--;
      if(tmp>0)
	sum=tmp;
      else
	sum=0;
    }
    ma=ma>sum?ma:sum;

    cout << (flg?m:ma) << endl;
  }
}