#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i:x){cout << i << " ";} cout << endl;}
#define isin(x,l,r) ((l)<=(x) && (x)<(r))
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return true;} else return false;}
template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return true;} else return false;}

int main() {
    int N;
    cin >> N;
    vector<pii> A;
    int ans = 0;
    rep(i, N) {
        int a;
        cin >> a;
        int left = i;
        while (sz(A) && A.back().first >= a) {
            chmax(ans, (i - 1 - A.back().second + 1) * A.back().first);
            chmax(ans, (i - A.back().second + 1) * a);
            chmin(left, A.back().second);
            A.pop_back();
        }
        A.emplace_back(a, left);
    }
    for (auto [a, i] : A) {
        chmax(ans, a * (N-i));
    }
    cout << ans << '\n';
    return 0;
}
#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 1000000007 //998244353
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fb(i,a,b) for(ll i=a;i>b;i--)
#define ff first
#define ss second
#define srt(v)  if(!v.empty())sort(v.begin(),v.end())
#define rev(v)  if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
	ll res=1;
	while(b!=0)
	{
		if(b&1)
		{
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}
void solve()
{  
     string s;
     cin>>s;
     ll ans=0;
     
     ll n=s.size();
     
     ll a[n+1];
     
     for(ll i=0;i<n;i++){
         a[i+1]=(ll)(s[i]-'a');
     }
     
    ll cnt[26]={0};
    
    for(ll i=n;i>0;i--){
        if(i>n-2){
            cnt[a[i]]++;
            continue;
        }
        
        if(a[i]==a[i+1]){
            cnt[a[i+1]]--;
            ans+=n-i;
            ans--;
            ans-=cnt[a[i]];
            for(ll j=0;j<26;j++)
            cnt[j]=0;
            
            cnt[a[i]]=n-i+1;
        }
        else
        {
            cnt[a[i]]++;
        }
    }
    
    cout<<ans<<endl;
}
int main()
{    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     
//Start from Here.

  ll t;
  t=1;
// cin>>t;
  while(t--)
  solve();
  
//Good Bye!
    return 0;
}