#include <iostream>
#include <algorithm>

using namespace std;

const int NMAX = 200000;

int main() {
  int N; cin >> N;
  int A[NMAX];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  long long cumsum[NMAX + 1];
  cumsum[0] = 0;
  for (int i = 0; i < N; i++) {
    cumsum[i + 1] = cumsum[i] + A[i];
  }

  pair<int, long long> cumsum2[NMAX + 1];
  cumsum2[0] = make_pair(0, 0);
  for (int i = 0; i < N; i++) {
    cumsum2[i + 1] = make_pair(i, cumsum2[i].second + cumsum[i + 1]);
  }

  auto cmp = [](pair<int, long long> a, pair<int, long long> b) { return a.second < b.second; };
  sort(cumsum2, cumsum2 + N, cmp);

  int index = cumsum2[N - 1].first;
  long long pos = cumsum2[N - 1].second;
  long long ans = pos;
  for (int i = 0; i < index + 1; i++) {
    pos += A[i];
    ans = max(ans, pos);
  }
  
  ans = max(ans, cumsum2[N].second);
  cout << ans << endl;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <climits>
#include <bitset>
#include <fstream>
#include <algorithm>
#include <functional>
#include <stack>
#include <string>
#include <cmath>
#include <numeric>
#include <tuple>
#include <cassert>
#include <random>
#include <chrono>
#include <ext/rope>
#include <unordered_map>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define sz(x) (x).size()
#define lbt(x) ((x)&(-x))
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define fun function
#define fi first
#define se second
#define re register
#define ls i << 1
#define rs i << 1 | 1
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define pdi pair<double,int>
#define pdd pair<double,double>
#define mod 1000000007

#define int long long

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef rope<char> RPstring;
typedef __gnu_pbds::tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> RBtree;
/*rk = t.order_of_key(v)+1, get_k = *t.find_by_order(k)*/ /*idx begin of 0*/
typedef __gnu_pbds::priority_queue<pii,greater<pii>,pairing_heap_tag> PQ_pii_s;//small
typedef __gnu_pbds::priority_queue<pii,less<pii>,pairing_heap_tag> PQ_pii_b;//big
// gp_hash_table<T,T> x
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int tt;

inline int rd(){
  int x = 0, f = 1; char ch = getchar();
  while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
  while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
  return x * f;
}

void out(int a) {if(a<0)putchar('-'),a=-a;if(a>=10)out(a/10);putchar(a%10+'0');}
#define pnt(x) out(x),puts("")
#define put(x) out(x), putchar(' ')
#define putschar(x) putchar(x), puts("")

//int t[maxn];
//void upd(int p, int x) {for(;p<=n;p+=lbt(p))t[p]+=x;}
//int ask(int p) {int ret = 0;for(;p;p-=lbt(p))ret+=t[p];return ret;}

int bit_cnt(int x) {int res=0;while(x)res+=x&1,x>>=1;return res;}
int INT(string x, int sys) {int base=sys,res=0;for(auto i: x)res*=sys,res+=i-'0';return res;}//sys->10
string TNI(int x, int sys) {string res="";while(x)res+=x%sys+'0',x/=sys;reverse(all(res));return res;}//10->sys

int gcd(int a, int b) {return b?gcd(b,a%b):a;} int lcm(int a, int b) {return a*b/gcd(a,b);}
ll mul(ll a, ll b, int m) {ll ret = 0ll;while(b){if(b&1)ret+=a,ret%=m;a+=a,a%=m,b>>=1;}return ret;}
ll mul(ll a, ll b) {ll ret = 0ll;while(b){if(b&1)ret+=a;a+=a,b>>=1;}return ret;}
ll ksm(ll a, ll b, int m) {ll ret = 1ll;while(b){if (b&1) ret*=a,ret%=m;a*=a,a%=m,b>>=1;}return ret;}
ll ksm(ll a, ll b) {ll ret = 1ll;while(b){if(b&1)ret*=a;a*=a,b>>=1;}return ret;}
ll inv(ll x, int m) {return ksm(x,m-2,m);}

const int N = 1e5 + 10;
int dp[N], mi[26];
char s[N];

void run() {
  int n = rd();
  scanf("%s",s+1);
  mem(dp, inf);
  mem(mi, inf);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    int c = s[i] - 'a';
    for (int j = 0; j < 26; j++) {
      if (c == j) continue;
      dp[i] = min(dp[i], mi[j] + 1);
    }
    mi[s[i] - 'a'] = min(mi[s[i] - 'a'], dp[i - 1]);
  }  
  cout << (dp[n] == INF ? -1 : dp[n]);
}

signed main(){
//  for (tt = rd(); tt--; )
  run();
  return 0;
}