#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>
#include <assert.h>
#include <cstring>
#include <set>

using namespace std;

typedef long long ll;
int arr[200001];
int main()
{
    int N;
    cin >> N;
    int temp;
    int i;
    int m = 0;
    ll sum = 0;
    for(i = 0; i < N; i++)
    {
        cin >> temp;
        arr[i] = temp;
        m = max(m,arr[i]);
        sum += m-arr[i];
    }
    
    for(i = N-1; i >= 0; i--)
    {
        
    }

    cout << sum;
}
#include <iostream>
#include <algorithm>
using namespace std;

int main (int argc, char const* argv[]){
  int n, max1, a[5001];
  
  while(cin >> n && n!=0){
    for(int i=0;i<n;i++) cin >> a[i];
    
    max1 = a[0];
    for (int i = 0; i < n; i += 1){
      int sum = 0;
      for(int j=i;j<n;j++){
        sum += a[j];
        max1 = max(max1,sum);
      }
    }
    cout << max1 << endl;
    
  }
  
  return 0;
}