#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n];
ll hash[n];

set<ll> s;
for (int i=0;i<n;i++){
    cin>>a[i];
s.insert(a[i]);
a[i]+=1e9;

}
ll two=2;

if (s.size()==1||*(s.begin())>0||*(--s.end())<0){
	cout<<n*(n+1)/2;
	return 0;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=i*1e9;

    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<ll> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n+1];
ll hash[n+1];

set<ll> s;
for ( ll i =0;i<n;i++){
    scanf("%lld",&a[i]);
s.insert(a[i]);
a[i]+=1000000000;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}

    ll prefix [n+1];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;

    hash[0]=temp;
        vec[temp].push_back(0);

    for (ll i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];

     temp=prefix[i];

     //cout<<prefix[i]<<endl;

temp=temp%(i*1000000000);

    temp%=x;
    temp%=y;
    temp%=z;

    hash[i]=temp;
    if (temp<0){
        cout<<-1;
        return 0;

    }

        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{vector<int> vec [10001];
   ll x=100001;
   ll y=100003;
   ll z=100007;

    int n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 vector<pair<int,int>> v;
 for (int i=0;i<n;i++){
if (prefix[i]==0){
    v.push_back({0,i});
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            v.push_back({i+1,vec[hash[i]][j]});
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<n*(n+1)/2;
    return 0;
 }

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/2;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

 ans+=(beg)*(beg+1)/2;
ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n];
ll hash[n];

set<ll> s;
for (int i=0;i<n;i++){
    cin>>a[i];
s.insert(a[i]);
a[i]+=1e9;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=i*1e9;

    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n];
ll hash[n];

set<ll> s;
for ( ll i =0;i<n;i++){
    cin>>a[i];
s.insert(a[i]);
a[i]+=1e9;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (ll i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=(i-1)*1e9;

    temp%=x;
    temp%=y;
    temp%=z;

    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{
            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++){cin >>a[i];
a[i]+=1e9;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=i*1e9;

    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n];
ll hash[n];

set<ll> s;
for (int i=0;i<n;i++){
    cin>>a[i];
s.insert(a[i]);
a[i]+=1e9;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=(i-1)*1e9;

    temp%=x;
    temp%=y;
    temp%=z;

    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{vector<int> vec [1001];
   ll x=100001;
   ll y=100003;
   ll z=100007;

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 vector<pair<int,int>> v;
 for (int i=0;i<n;i++){
if (prefix[i]==0){
    v.push_back({0,i});
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            v.push_back({i+1,vec[hash[i]][j]});
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;
ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{long long n;
cin >>n;
int a[n+1];

for (int i=1;i<=n;i++)cin>>a[i];
ll prefix[n+1];

prefix[1]=a[1];
map<long long ,int> mp;
vector<pair<int,int>> v;

mp[prefix[1]]=1;
if (a[1]==0){
    v.push_back({1,1});
    mp[0]=1;

}

for(int i=2;i<=n;i++){
    prefix[i]=a[i]+prefix[i-1];

    if (mp[prefix[i]]!=0||prefix[i]==0){

        v.push_back({mp[prefix[i]]+1,i});

    }
    mp[prefix[i]]=i;

}

if (v.size()==0){
    cout<<(n)*(n+1)/2;
    return 0;
}

ll ans=(v[0].second-1)*(v[0].second)/2;

for (int i=0;i<v.size()-1;i++){

if (v[i].first==v[i].second)continue;

ll temp =v[i+1].second-v[i].second;
ans+=temp*(temp+1)/2;
ll temp2= (long long)max (0,v[i].second-v[i].first-1);
    ans+=temp*(temp2*(temp2+1)/2);

}

int m=v.size();
if (v[m-1].first!=v[m-1].second){

ll ttt=n-v[m-1].second+1;
        ans+=ttt;
ll tt=(long long)max (0,v[m-1].second-v[m-1].first-1);

ans+=ttt*(tt*(tt+1)/2);

}
else {

    ll ttt=n-v[m-1].second;
    ans+=(ttt)*(ttt+1)/2;

}

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{int n;
cin >>n;
int a[n+1];

for (int i=1;i<=n;i++)cin>>a[i];
ll prefix[n+1];

prefix[1]=a[1];
map<long long ,int> mp;
vector<pair<int,int>> v;

mp[prefix[1]]=1;
if (a[1]==0){
    v.push_back({1,1});
    mp[0]=1;

}

for(int i=2;i<=n;i++){
    prefix[i]=a[i]+prefix[i-1];

    if (mp[prefix[i]]!=0||prefix[i]==0){

        v.push_back({mp[prefix[i]]+1,i});

    }
    mp[prefix[i]]=i;

}

if (v.size()==0){
    cout<<(n)*(n+1)/2;
    return 0;
}

ll ans=(v[0].second-1)*(v[0].second)/2;

for (int i=0;i<v.size()-1;i++){

if (v[i].first==v[i].second)continue;

ll temp =v[i+1].second-v[i].second;
ans+=temp*(temp+1)/2;
ll temp2= (long long)max (0,v[i].second-v[i].first-1);
    ans+=temp*(temp2*(temp2+1)/2);

}

int m=v.size();
if (v[m-1].first!=v[m-1].second){

ll ttt=n-v[m-1].second+1;
        ans+=ttt;
ll tt=(long long)max (0,v[m-1].second-v[m-1].first-1);

ans+=ttt*(tt*(tt+1)/2);

}
else {

    ll ttt=n-v[m-1].second;
    ans+=(ttt)*(ttt+1)/2;

}

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{
            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 vector<pair<int,int>> v;
 for (int i=0;i<n;i++){
if (prefix[i]==0){
    v.push_back({0,i});
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            v.push_back({i+1,vec[hash[i]][j]});
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<ll> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n+1];
ll hash[n+1];

set<ll> s;
for ( ll i =0;i<n;i++){
    scanf("%lld",&a[i]);
s.insert(a[i]);
a[i]+=1000000000;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}

    ll prefix [n+1];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;

    hash[0]=temp;
        vec[temp].push_back(0);

    for (ll i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];

     temp=prefix[i];

     //cout<<prefix[i]<<endl;

temp=temp-(i*1000000000);

    temp%=x;
    temp%=y;
    temp%=z;

    hash[i]=temp;
    if (temp<0){
        cout<<-1;
        return 0;

    }

        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{ll n;
cin >>n;
int a[n+1];

for (int i=1;i<=n;i++)cin>>a[i];
ll prefix[n+1];

prefix[1]=a[1];
map<long long ,int> mp;
vector<pair<int,int>> v;

mp[prefix[1]]=1;
if (a[1]==0){
    v.push_back({1,1});
    mp[0]=1;

}

for(int i=2;i<=n;i++){
    prefix[i]=a[i]+prefix[i-1];

    if (mp[prefix[i]]!=0||prefix[i]==0){

        v.push_back({mp[prefix[i]]+1,i});

    }
    mp[prefix[i]]=i;

}

if (v.size()==0){
    cout<<(n)*(n+1)/2;
    return 0;
}

ll ans=(long long)(v[0].second-1)*(v[0].second)/2;

int maxfirst=v[0].first;

for (int i=0;i<v.size()-1;i++){

ll temp;
if (v[i].first==v[i].second){temp=v[i+1].second-v[i].second-1;

maxfirst=max(maxfirst,v[i].first);

}

else
 temp =v[i+1].second-v[i].second;
ans+=temp*(temp+1)/2;
ll temp2=0;

maxfirst=max(maxfirst,v[i].first);

temp2= (long long)max (0,v[i].second-maxfirst-1);

    ans+=temp*temp2;

}

int m=v.size();
if (v[m-1].first!=v[m-1].second){

ll ttt=n-v[m-1].second+1;

        ans+=(ttt*(ttt+1)/2);

maxfirst=max(maxfirst,v[m-1].first);

ll tt=0;
 tt=(long long)max (0,v[m-1].second-maxfirst-1);

ans+=ttt*tt;
}
else {

    ll ttt=n-v[m-1].second;
    ans+=(ttt)*(ttt+1)/2;

}

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n];
ll hash[n];

set<ll> s;
for ( ll i =0;i<n;i++){
    cin>>a[i];
s.insert(a[i]);
a[i]+=1e9;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (ll i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=(i)*1e9;

    temp%=x;
    temp%=y;
    temp%=z;

    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{int n;
cin >>n;
int a[n+1];

for (int i=1;i<=n;i++)cin>>a[i];
ll prefix[n+1];

prefix[1]=a[1];
map<long long ,int> mp;
vector<pair<int,int>> v;

mp[prefix[1]]=1;
if (a[1]==0){
    v.push_back({1,1});
    mp[0]=1;

}

for(int i=2;i<=n;i++){
    prefix[i]=a[i]+prefix[i-1];

    if (mp[prefix[i]]!=0||prefix[i]==0){

        v.push_back({mp[prefix[i]]+1,i});

    }
    mp[prefix[i]]=i;

}

if (v.size()==0){
    cout<<(n)*(n+1)/2;
    return 0;
}

ll ans=(v[0].second-1)*(v[0].second)/2;

for (int i=0;i<v.size()-1;i++){

if (v[i].first==v[i].second)continue;

ll temp =v[i+1].second-v[i].second;
ans+=temp*(temp+1)/2;
ll temp2= (long long)max (0,v[i].second-v[i].first-1);
    ans+=temp*(temp2*(temp2+1)/2);

}

int m=v.size();
if (v[m-1].first!=v[m-1].second){

        ans++;
ll tt=(long long)max (0,v[m-1].second-v[m-1].first-1);

ans+=tt*(tt+1)/2;

}

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{

            vector<ll> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];
ll n;cin>>n;

//    a[i]=41;
ll a[n+1];
ll hash[n+1];

set<ll> s;
for ( ll i =0;i<n;i++){
    scanf("%lld",&a[i]);
s.insert(a[i]);
a[i]+=1000000000;

}
ll two=2;

if ((s.size()==1&&*(s.begin())!=0)||*(s.begin())>0||*(--s.end())<0){

	cout<<n*(n+1)/2;
	return 0;

}

    ll prefix [n+1];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;

    hash[0]=temp;
        vec[temp].push_back(0);

    for (ll i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];

     temp=prefix[i];

     //cout<<prefix[i]<<endl;

temp=temp%1000000000;

    temp%=x;
    temp%=y;
    temp%=z;

    hash[i]=temp;
    if (temp<0){
        cout<<-1;
        return 0;

    }

        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

int last0=0;

 for (int i=1;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=last0;
    last0=i+1;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i+1;
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<(long long)(n*(n+1))/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{
            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

 for (int i=0;i<n;i++){
if (prefix[i]==0){
    //v.push_back({0,i});
    v.push_back(i);
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

//cout<<v[0];
//cout<<v[1];
 while (true){

if (a[cur]==0){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;
cout<<end;

    ll temp=end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=1;
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{
            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++){cin >>a[i];
a[i]+=1e9;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=i*1e9;

    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

 for (int i=0;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

//cout<<v[0];
//cout<<v[1];
 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=1;
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{vector<int> vec [1000001];
   ll x=100001;
   ll y=100003;
   ll z=100007;

    int n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 vector<pair<int,int>> v;
 for (int i=0;i<n;i++){
if (prefix[i]==0){
    v.push_back({0,i});
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            v.push_back({i+1,vec[hash[i]][j]});
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<n*(n+1)/2;
    return 0;
 }

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/2;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/2;
ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{vector<int> vec [1000001];
   ll x=100001;
   ll y=100003;
   ll z=100007;

    int n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 vector<pair<int,int>> v;
 for (int i=0;i<n;i++){
if (prefix[i]==0){
    v.push_back({0,i});
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            v.push_back({i+1,vec[hash[i]][j]});
            break;

        }

    }

 }

 if (v.size()==0){
    cout<<n*(n+1)/2;
    return 0;
 }

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/2;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

 ans+=(beg)*(beg+1)/2;
ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{vector<int> vec [1000001];
   ll x=100001;
   ll y=100003;
   ll z=100007;

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++)cin >>a[i];
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 vector<pair<int,int>> v;
 for (int i=0;i<n;i++){
if (prefix[i]==0){
    v.push_back({0,i});
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            v.push_back({i+1,vec[hash[i]][j]});
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;
ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{
            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    map<int,int> mp;

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++){cin >>a[i];
a[i]+=1e9;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];

    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=i*1e9;

    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

 for (int i=0;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);

    mp[i]=0;
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);

            mp[vec[hash[i]][j]]=i;
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

 while (true){

if (a[cur]==1e9){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=(long long)end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=max((long long)0,(long long)(mp[end]-cur+1));
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{long long n;
cin >>n;
int a[n+1];

for (int i=1;i<=n;i++)cin>>a[i];
ll prefix[n+1];

prefix[1]=a[1];
map<long long ,int> mp;
vector<pair<int,int>> v;

mp[prefix[1]]=1;
if (a[1]==0){
    v.push_back({1,1});
    mp[0]=1;

}

for(int i=2;i<=n;i++){
    prefix[i]=a[i]+prefix[i-1];

    if (mp[prefix[i]]!=0||prefix[i]==0){

        v.push_back({mp[prefix[i]]+1,i});

    }
    mp[prefix[i]]=i;

}

if (v.size()==0){
    cout<<(n)*(n+1)/2;
    return 0;
}

ll ans=(v[0].second-1)*(v[0].second)/2;

for (int i=0;i<v.size()-1;i++){

if (v[i].first==v[i].second)continue;

ll temp =v[i+1].second-v[i].second;
ans+=temp*(temp+1)/2;
ll temp2= (long long)max (0,v[i].second-v[i].first-1);
    ans+=temp*(temp2*(temp2+1)/2);

}

int m=v.size();
if (v[m-1].first!=v[m-1].second){

ll ttt=n-v[m-1].second+1;
        ans+=ttt;
ll tt=(long long)max (0,v[m-1].second-v[m-1].first-1);

ans+=ttt*(tt*(tt+1)/2);

}
else {

    ll ttt=n-v[m-1].second;
    ans+=(ttt)*(ttt+1)/2;

}

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool cmp(pair<int,int> &l,pair<int,int> &r){
return l.second<r.second;

}

int main()
{
            vector<int> prime;

    for (int j=100000;j<=100100;j++)
    {bool b=0;
        for (int i=2;i<sqrt(j);i++)

      {
          if (j%i==0){
        b=1;
            break;

        }

        }
        if (!b)prime.push_back(j);

    }

    vector<int> vec [100101];
   ll x=prime[0];
   ll y=prime[1];
   ll z=prime[2];

    ll n;cin>>n;
ll a[n];
ll hash[n];

for (int i=0;i<n;i++){cin >>a[i];
a[i]+=1e9;

}
    ll prefix [n];
    prefix[0]=a[0];

    ll temp=prefix[0];
    temp%=x;
    temp%=y;
    temp%=z;
    hash[0]=temp;
        vec[temp].push_back(0);

    for (int i=1;i<n;i++)
    {prefix[i]=a[i]+prefix[i-1];
     temp=prefix[i];

temp-=i*1e9;

    temp%=x;
    temp%=y;
    temp%=z;
    hash[i]=temp;
        vec[temp].push_back(i);

    }

 //vector<pair<int,int>> v;
vector<int> v;

 for (int i=0;i<n;i++){
if (prefix[i]==(i+1)*1e9){
    //v.push_back({0,i});
    v.push_back(i);
    continue;

}

    for (int j=0;j<vec[hash[i]].size();j++){
        if (vec[hash[i]][j]>i){
            //v.push_back({i+1,vec[hash[i]][j]});
            v.push_back(vec[hash[i]][j]);
            break;

        }

    }

 }

ll two=2;

 if (v.size()==0){
    cout<<n*(n+1)/two;
    return 0;
 }

 ll ans=0;
 ll cur=0;
sort(v.begin(),v.end());

vector<int >ttt;

//cout<<v[0];
//cout<<v[1];
 while (true){

if (a[cur]==0){
    cur++;
    continue;

}
if (cur>=n)break;

    vector<int>::iterator itr=upper_bound(v.begin(),v.end(),cur);

    if (itr==v.end()){
       ll temp=v.size()-cur+1
       ;
           ans+=temp*(temp+1)/two;

        break;

    }

    ll end=*itr;

    ll temp=end-cur+1;
    ans+=temp*(temp+1)/two;
   ans-=1;
    cur=end;
    ttt.push_back (cur);

 }

if (ttt.size()>1)ans-=ttt.size();

 /*

 sort (v.begin(),v.end());
 ll end=v[v.size()-1].first;

 for ( ll i=0;i<n;i++){
    if (a[i]==0)
        end=max(end,i);

 }

ll end2=n-end-1;
 ll ans=0;
 ans+=(end2)*(end2+1)/two;
 ans-=end2;

 sort(v.begin(),v.end(),cmp);

ll beg=v[0].second;

for (ll i=0;i<n;i++)
    if (a[i]==0)
    beg=min(i,beg);

 ans+=(beg)*(beg+1)/two;

ans-=beg;
 for (int i=0;i<n;i++)
    if (a[i])ans++;

*/
cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{ll n;
cin >>n;
int a[n+1];

for (int i=1;i<=n;i++)cin>>a[i];
ll prefix[n+1];

prefix[1]=a[1];
map<long long ,int> mp;
vector<pair<int,int>> v;

mp[prefix[1]]=1;
if (a[1]==0){
    v.push_back({1,1});
    mp[0]=1;

}

for(int i=2;i<=n;i++){
    prefix[i]=a[i]+prefix[i-1];

    if (mp[prefix[i]]!=0||prefix[i]==0){

        v.push_back({mp[prefix[i]]+1,i});

    }
    mp[prefix[i]]=i;

}

if (v.size()==0){
    cout<<(n)*(n+1)/2;
    return 0;
}

ll ans=(v[0].second-1)*(v[0].second)/2;

for (int i=0;i<v.size()-1;i++){
ll temp;
if (v[i].first==v[i].second)temp=v[i+1].second-v[i].second-1;
else
 temp =v[i+1].second-v[i].second;
ans+=temp*(temp+1)/2;
ll temp2= (long long)max (0,v[i].second-v[i].first-1);
    ans+=temp*(temp2*(temp2+1)/2);

}

int m=v.size();
if (v[m-1].first!=v[m-1].second){

ll ttt=n-v[m-1].second+1;
        ans+=ttt;
ll tt=(long long)max (0,v[m-1].second-v[m-1].first-1);

ans+=ttt*(tt*(tt+1)/2);

}
else {

    ll ttt=n-v[m-1].second;
    ans+=(ttt)*(ttt+1)/2;

}

cout<<ans;

    //cout << "Hello world!" << endl;
    return 0;
}


#include<stdio.h>
int main()
{
	int n,i;
	scanf("%d",&n);
	char a[n],b[n];
	int sum=0;
	scanf("%s%s",a,b);
	for(i=0;i<n;i++)
	{
		if(abs(a[i]-b[i])<=4)
		{
			sum+=abs(a[i]-b[i]);
		}
		else
			sum+=10-abs(a[i]-b[i]);
	}
	printf("%d\n",sum);
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char s[100005];
	int i,n,a[100005]={0},curr1=-1,curr2=-1,flag=0;
	scanf("%s",s);
	n=strlen(s);
	for(i=0;i<n;i++)
	{
		if(s[i]=='A' && s[i+1]=='B')
			a[i]=1;
		else if(s[i]=='B' && s[i+1]=='A')
			a[i]=2;
	}
	for(i=0;i<n;i++)
	{
		if(a[i]==1 && curr1==-1)
			curr1=i;
		if(a[i]==2 && curr2==-1)
			curr2=i;
		if(abs(curr1-curr2)>1)
		{
			flag=1;
			printf("YES\n");
			break;
		}
		else
		{
			if(curr2>curr1 && curr2>0)
				curr2=-1;
			else if(curr1>curr2 && curr1>0)
				curr1=-1;
		}

	}
	if(flag==0)
		printf("NO\n");
	return 0;

}


#include <stdio.h>
#include <stdlib.h>

char s[100001];
int check_ab;
int check_ba;
int check_aba;
int check_bab;

int main() {
	int i;
	check_aba = 0;
	check_bab = 0;
	check_ab = 0;
	check_ab = 0;

	scanf("%s",&s);

	for (i = 0; i < 100001; i++) {
		if (s[i] == '\0') {
			break;
		}
		if (s[i] == 'A' && s[i+1]=='B') {
			if(s[i+2] !='A') check_ab = 1;
		}
		if (s[i] == 'B' && s[i + 1] == 'A') {
			if (s[i + 2] != 'B') check_ba = 1;
		}
		if (s[i] == 'B' && s[i + 1] == 'A'&& s[i + 2] == 'B') {
			check_bab = 1;
			i = i + 2;
		}
		if (s[i] == 'A' && s[i + 1] == 'B'&& s[i + 2] == 'A') {
			check_aba = 1;
			i = i + 2;
		}
	}

	if (check_ab && check_ba) printf("YES\n");
	else if (check_ab && (check_bab || check_aba)) printf("YES\n");
	else if (check_ba && (check_bab || check_aba)) printf("YES\n");
	else printf("NO\n");
	return 0;
}
/* 1520260682218 */


#include <stdio.h>
#include <string.h>

int main(){

	char s[100005];
	int i, tamanho;
	int contAB, contBA;

	contAB = 0;
	contBA = 0;

	scanf("%s", &s);
	tamanho = strlen(s);

for (i = 0; i <= tamanho - 1; i++){
	if (i < tamanho-1){
			if ((s[i] == 'A') && (s[i + 1] == 'B') && (contAB==0))
			{
				contAB = contAB + 1;
				i++;
			}
			else if ((s[i] == 'B') && (s[i + 1] == 'A') && (contAB ==1)){
				contBA = contBA + 1;
				i++;
			}
	}

}
	if ((contAB >= 1) && (contBA >= 1)){
		printf("YES\n");
		return 0;
	}

	contAB = 0;
	contBA = 0;

	for (i = 0; i <= tamanho - 1; i++){
		if (i < tamanho - 1){
			if ((s[i] == 'A') && (s[i + 1] == 'B') &&(contBA == 1)){
				contAB = contAB + 1;
				i++;
			}
			else if ((s[i] == 'B') && (s[i + 1] == 'A') && (contBA == 0)){
				contBA = contBA + 1;
				i++;
			}
		}
	}
	if ((contAB == 0) || (contBA == 0)) {
		printf("NO\n");
	} else{
		if ((contAB >= 1) && (contBA >= 1)){
			printf("YES\n");
			return 0;
		}
		else if (contAB != contBA)
			printf("NO\n");
}

	return 0;
}

/* 1520513114208 */


#include <stdio.h>

int main () {

	char s[100000];

	//inicializa o vetor
	for(int i=0;i<100000;i++){
		s[i] = ' ';
	}

	scanf("%s",&s);
	//printf("%s",s);

	int i = 0;
	char anterior = ' '; //guarda o caracter anterior
	int temAB = 0; //indica se ja possui string AB
	int temBA = 0; //indica se ja possui string BA

	while (s[i] != ' '){

		//if (anterior != ' ') { // se tem uma letra anterior encontrada
			if (anterior == 'A' && s[i] == 'B'){
				temAB = 1;
				anterior = ' ';
			} else if (anterior == 'B' && s[i] == 'A'){
				temBA = 1;
				anterior = ' ';
			} else if ((s[i] == 'A' && temAB == 0) || (s[i] == 'B' && temBA == 0)){
				anterior = s[i]; //inicia a sequencia novamente
			} else {
				anterior = ' ';
			}

		i = i + 1;

	}

	if (temAB == 1 && temBA == 1){
		printf("YES\n");
	} else {
		printf("NO\n");
	}

	return 0;

}
/* 1520257640708 */


#include <stdio.h>
#include <string.h>

int main(){

	char s[100005];
	int i, tamanho;
	int contAB, contBA, contA, contB;

	contA = 0;
	contB = 0;
	contAB = 0;
	contBA = 0;

	scanf("%s", &s);
	tamanho = strlen(s);

	for (i = 0; i <= tamanho - 1; i++){

		if (s[i] == 'A'){
			contA = contA + 1;

		}
		if (s[i] == 'B'){
			contB = contB + 1;

		}

		if (i < tamanho-1){
			if ((s[i] == 'A') && (s[i + 1] == 'B')){
				contAB = contAB + 1;
			}

			else if ((s[i] == 'B') && (s[i + 1] == 'A')){
				contBA = contBA + 1;

			}

		}
	}

	if ((contAB == 0) || (contBA == 0)) {
		printf("NO\n");
	} else{

		if ((contA == contB) && (contAB == contBA))
			printf("YES\n");
		else if ((contA == contB) && (contAB != contBA))
			printf("NO\n");

		if ((contA != contB) && (contAB != contBA))
			printf("YES\n");
		else if ((contA != contB) && (contAB == contBA))
			printf("NO\n");
	}

	return 0;
}

/* 1520263928785 */


#include <stdio.h>
#include <string.h>

int main () {

    char string[100002];

    scanf("%s", string);

    int len = strlen(string);
    int flagAB = 0, flagBA  = 0;
    for (int i = 0; i < len; i++) {
        if(string[i] == 'A') {
            if(string[i+1] == 'B' && string[i+2] != 'A'){
                flagAB = 1;
                i = i + 1;
            }
        }
        else if(string[i] == 'B') {
            if(string[i+1] == 'A' && string[i+2] != 'B') {
                flagBA = 1;
                i = i + 1;
            }
        }
    }

    if(flagAB && flagBA) {
        printf("YES\n");
    }
    else {
        printf("NO\n");
    }

    return 0;
}

/* 1520257316726 */


#include <stdio.h>
#include <string.h>

int main(){

	char s[100005];
	int i, tamanho;
	int contAB, contBA;

	contAB = 0;
	contBA = 0;

	scanf("%s", &s);
	tamanho = strlen(s);

for (i = 0; i <= tamanho - 1; i++){
	if (i < tamanho-1){
			if ((s[i] == 'A') && (s[i + 1] == 'B') && (contAB==0))
			{
				contAB = contAB + 1;
				i++;
			}
			else if ((s[i] == 'B') && (s[i + 1] == 'A') && (contAB ==1)){
				contBA = contBA + 1;
				i++;
			}
	}

}
	if (contAB == contBA){
		printf("YES\n");
		return 0;
	}

	contAB = 0;
	contBA = 0;

	for (i = 0; i <= tamanho - 1; i++){
		if (i < tamanho - 1){
			if ((s[i] == 'A') && (s[i + 1] == 'B') &&(contBA == 1)){
				contAB = contAB + 1;
				i++;
			}
			else if ((s[i] == 'B') && (s[i + 1] == 'A') && (contBA == 0)){
				contBA = contBA + 1;
				i++;
			}
		}
	}
	if ((contAB == 0) || (contBA == 0)) {
		printf("NO\n");
	} else{
		if (contAB == contBA){
			printf("YES\n");
			return 0;
		}
		else if (contAB != contBA)
			printf("NO\n");
}

	return 0;
}

/* 1520455632436 */


#include <stdio.h>

int main () {

	char s[100000];

	//inicializa o vetor
	for(int i=0;i<100000;i++){
		s[i] = '\0'; //assim que é indicado fim de string
	}

	scanf("%s",&s);
	//printf("%s",s);

	int i = 0;
	char anterior = ' '; //guarda o caracter anterior
	int temAB = 0; //indica se ja possui string AB
	int temBA = 0; //indica se ja possui string BA

	//ABABAB

	while (s[i] != '\0') {
	//for (int i = 0; i < sizeof(s);i++){
		//printf("temAB: %d - i: %d - s[i]: %c - s[i+1]: %c\n",temAB,i,s[i],s[i+1]);
		if (temAB == 0 && s[i] == 'A' && s[i+1] == 'B'){ //Se encontrar AB, procura BA depois de B
			i = i + 1;
			temAB = 1;
		} else if (temAB == 1 && /*i != sizeof(s)-1 &&*/ s[i] == 'B' && s[i+1] == 'A'){
			printf("YES\n");
			return 0;
		}
		i = i + 1;
	}

	//printf("Saiu primeiro for\n");

	i = 0;

	while (s[i] != '\0') {
	//for (int i = 0; i < sizeof(s);i++){
		//printf("temBA: %d\n",temBA);

		if (temBA == 0 && s[i] == 'B' && s[i+1] == 'A'){ //Se encontrar BA, procura AB depois de A
			i = i + 1;
			temBA = 1;
		} else if (temBA == 1 && /*i != sizeof(s)-1 &&*/ s[i] == 'A' && s[i+1] == 'B'){
			//printf("i: %d + s: %d - 1: %d \n",i,sizeof(s),sizeof(s)-1);
			//printf("%c - %c\n",s[i],s[i+1]);
			printf("YES\n");
			return 0;
		}
		i = i + 1;
	}

	printf("NO\n");

	return 0;
	/*
	while (s[i] != ' '){ //ABAXXXAB - ABBA - BACFAB - ABABAB / NO - ABA ABAB
		//AB > BA
		// BA >> AB
		if (anterior == 'A' && s[i] == 'B') {
			//OUTRO WHILE
		} else {
			anterior = s[i];
		}

		//if (anterior != ' ') { // se tem uma letra anterior encontrada
		/*	if (anterior == 'A' && s[i] == 'B'){
				printf("1 - par: %c%c\n",anterior,s[i]);
				printf("1 - temBAsobs: %d\n",temBAsobs);
				printf("1 - temABsobs: %d\n",temABsobs);
				if (paranterior == 1){ //se tem um par anterior, ta sobrescrito
					temABsobs = 1;
				} else {
					temAB = 1;
					paranterior = 1;
				}

				if (temBAsobs == 1){ // se tinha um sobrescrito de BA, nao tem mais, pq outro teve
					temBA = 1;
				}
				anterior = s[i];
				//anterior = ' ';
			} else if (anterior == 'B' && s[i] == 'A'){
				printf("2 - par: %c%c\n",anterior,s[i]);
				printf("2 - temBAsobs: %d\n",temBAsobs);
				printf("2 - temABsobs: %d\n",temABsobs);
				if (paranterior == 1){
					temBAsobs = 1;
				} else {
					temBA = 1;
					paranterior = 1;
				}

				if (temABsobs == 1){
					temAB = 1;
				}
				anterior = s[i];
				//anterior = ' ';
			} else if (s[i] == 'A' || s[i] == 'B'){//if ((s[i] == 'A' && temAB == 0) || (s[i] == 'B' && temBA == 0)){
				printf("3 - par: %c%c\n",anterior,s[i]);
				printf("3 - temBAsobs: %d\n",temBAsobs);
				printf("3 - temABsobs: %d\n",temABsobs);
				anterior = s[i]; //inicia a sequencia novamente
				paranterior = 0;
				printf("if 3\n");
			} else {
				printf("4 - par: %c%c\n",anterior,s[i]);
				printf("4 - temBAsobs: %d\n",temBAsobs);
				printf("4 - temABsobs: %d\n",temABsobs);
				anterior = ' ';
				paranterior = 0;
			}*/

		/*i = i + 1;

	}*/

	if (temAB == 1 && temBA == 1){
		printf("YES\n");
	} else {
		printf("NO\n");
	}

	return 0;

}
/* 1520555678479 */


#include <stdio.h>
#include <string.h>

int findAB(char *s, int ind) {

    int i;

    for(i = ind; i < strlen(s)-1; i++) {
        if(s[i] == 'A') {
            if(s[i+1] == 'B') {
                return i+1;
            }
        }
    }
    return 0;
}

int findBA(char *s, int ind) {

    int i;
    for(i = ind; i < strlen(s)-1; i++) {
        if(s[i] == 'B') {
            if(s[i+1] == 'A') {
                return i+1;
            }
        }
    }
    return 0;
}
int main () {

    char string[100002];

    scanf("%s", string);
    int a = 0, b = 0, c = 0, d = 0;

    a = findAB(string, 0);
    if(a) {
        b = findBA(string, a+1);
    }
    c = findBA(string, 0);
    if(c) {
        d = findAB(string, c + 1);
    }
    if(a && b || (c && d))  {
        printf("YES\n");
    }
    else {
        printf("NO\n");
    }

}

/* 1520259655559 */


#include <stdio.h>
#include <string.h>

int main(){

	char s[100005];
	int i, tamanho;
	int contAB, contBA, contA, contB;

	contA = 0;
	contB = 0;
	contAB = 0;
	contBA = 0;

	scanf("%s", &s);
	tamanho = strlen(s);

	//printf("%d\n", tamanho);

	for (i = 0; i <= tamanho - 1; i++){

		if (s[i] == 'A'){
			contA = contA + 1;

		}
		if (s[i] == 'B'){
			contB = contB + 1;

		}

		if (i < tamanho-1){
			if ((s[i] == 'A') && (s[i + 1] == 'B')){
				contAB = contAB + 1;
			}

			else if ((s[i] == 'B') && (s[i + 1] == 'A')){
				contBA = contBA + 1;

			}

		}
	}

	//printf("%d\n", contA);
	//printf("%d\n", contB);

	//printf("%d\n", contAB);
	//printf("%d\n", contBA);

	if ((contAB == 0) || (contBA == 0)) {
		printf("NO\n");
	} else{

		if ((contA == contB) && (contAB == contBA))
			printf("YES\n");
		else if ((contA == contB) && (contAB != contBA))
			printf("NO\n");

		if ((contA != contB) && (contAB != contBA))
			printf("YES\n");
		else if ((contA != contB) && (contAB == contBA))
			printf("NO\n");
	}

	system("pause");
	return 0;
}

//ABAXXAB = YES

/* 1520263373374 */


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,u;
	char arr[1000];
	scanf("%s",arr);
	u=strlen(arr);
	for(i=0;i<u;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;break;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				f=1;break;
			}
		}
	}
	if(f)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,t=0,u;
	char arr[1000];
	scanf("%s",arr);
	u=strlen(arr);
	for(i=0;i<u;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;
				i+=2;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				t=1;
				i+=2;
				}
		}
	}
	if(f&&t)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,t=0,u;
	char arr[1005],x;
	//scanf("%s",arr);
	for(i=0;;i++){
		scanf("%c",&x);
		if(x=='\n') break;
		arr[i]=x;
	}
	u=strlen(arr);
	for(i=0;i<u-1;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;
				i+=2;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				t=1;
				i+=2;
				}
		}
	}
	if(f&&t)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,t=0,u;
	char arr[10500],x;
	scanf("%s",arr);
	/*for(i=0;;i++){
		scanf("%c",&x);
		if(x=='\n') break;
		arr[i]=x;
	}*/
	u=strlen(arr);
	for(i=0;i<u-1;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;
				i+=2;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				t=1;
				i+=2;
				}
		}
	}
	if(f&&t)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,t=0,u;
	char arr[10500],x;
//	scanf("%s",arr);
	for(i=0;;i++){
		scanf("%c",&x);
		if(x=='\n') break;
		arr[i]=x;
	}
	u=strlen(arr);
	for(i=0;i<u-1;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;
				i+=2;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				t=1;
				i+=2;
				}
		}
	}
	if(f&&t)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,t=0,u;
	char arr[1005];
	scanf("%s",arr);
	u=strlen(arr);
	for(i=0;i<u;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;
				i+=2;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				t=1;
				i+=2;
				}
		}
	}
	if(f&&t)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	int i,f=0,t=0,u;
	char arr[105000],x;
//	scanf("%s",arr);
	for(i=0;;i++){
		scanf("%c",&x);
		if(x=='\n') break;
		arr[i]=x;
	}
	u=strlen(arr);
	for(i=0;i<u-1;i++){
		if(arr[i]=='A'){
			if(arr[i+1]=='B'){
				f=1;
				i+=2;
			}
		}
		if(arr[i]=='B'){
			if(arr[i+1]=='A'){
				t=1;
				i+=2;
				}
		}
	}
	if(f&&t)
		printf("YES");
	else
		printf("NO");
	return 0;
}


#include <stdio.h>
#include <stdbool.h>

int main()
{
    char str[1000000];
    scanf("%s", str);
    int i=0,count=0;
    while(str[i]!='\0')
    {
        if((str[i]=='A'&&str[i+1]=='B')||(str[i]=='B'&&str[i+1]=='A'))
        {
            count++;
            i=i+2;
        }
        else
        {
            i++;
        }
        if(count==2)
        {
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    return 0;
}


#include <stdio.h>
#include <stdbool.h>

int main()
{
    char str[1000000];
    gets(str);
    int i=0,count=0;
    while(str[i]!='\0')
    {
        if(str[i]=='A'&&str[i+1]=='B')
        {
            count++;
        }
        else if(str[i]=='B'&&str[i+1]=='A')
        {
            count++;
        }
        if(count==2)
        {
            printf("YES");
            return 0;
        }
        i=i+2;
    }
    printf("NO");
    return 0;
}


#include <stdio.h>
#include <stdbool.h>
bool condition_ab=false;
bool condition_ba=false;

int main()
{
    char str[100001];
    scanf("%s", str);
    int i=0,j=0,count=0;
    while(str[i]!='\0'&&str[j]!='\0')
    {
        if(str[i]=='A'&&str[i+1]=='B'&&!condition_ab)
        {
            condition_ab=true;
            i++;
        }
        else if(str[i]=='B'&&str[i+1]=='A'&&condition_ab)
        {
            printf("YES");
            return 0;
        }
        if(str[j]=='B'&&str[j+1]=='A'&&!condition_ba)
        {
            condition_ba=true;
            j++;
        }
        else if(str[j]=='A'&&str[j+1]=='B'&&condition_ba)
        {
            printf("YES");
            return 0;
        }
        i++;
        j++;
    }
    printf("NO");
    return 0;
}


//Credit- Wazed Rifat

#include <stdio.h>
#include <stdbool.h>
bool condition_ab=false;
bool condition_ba=false;

int main()
{
    char str[100001];
    scanf("%s", str);
    int i=0,j=0,count=0;
    while(str[i]!='\0'&&str[j]!='\0')
    {
        if(str[i]=='A'&&str[i+1]=='B'&&!condition_ab)
        {
            condition_ab=true;
            i++;
        }
        else if(str[i]=='B'&&str[i+1]=='A'&&condition_ab)
        {
            printf("YES");
            return 0;
        }
        if(str[j]=='B'&&str[j+1]=='A'&&!condition_ba)
        {
            condition_ba=true;
            j++;
        }
        else if(str[j]=='A'&&str[j+1]=='B'&&condition_ba)
        {
            printf("YES");
            return 0;
        }
        i++;
        j++;
    }
    printf("NO");
    return 0;
}


#include <stdio.h>
#include <stdbool.h>

int main()
{
    char str[1000000];
    gets(str);
    int i=0,count=0;
    while(str[i]!='\0')
    {
        if((str[i]=='A'&&str[i+1]=='B')||(str[i]=='B'&&str[i+1]=='A'))
        {
            count++;
            i=i+2;
        }
        else
        {
            i++;
        }
        if(count==2)
        {
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    return 0;
}


#include <stdio.h>
#include <stdbool.h>
bool condition_ab=true;
bool condition_ba=true;

int main()
{
    char str[100001];
    gets(str);
    int i=0,count=0;
    while(str[i]!='\0')
    {
        if(str[i]=='A'&&str[i+1]=='B'&&condition_ab)
        {
            count++;
            condition_ab=false;
        }
        else if(str[i]=='B'&&str[i+1]=='A'&&condition_ba)
        {
            count++;
            condition_ba=false;
        }
        if(count==2)
        {
            printf("YES");
            return 0;
        }
        i=i+2;
    }
    printf("NO");
    return 0;
}


#include <stdio.h>

int main()
{
    char str[100001];
    scanf("%s", str);
    int i=0,count=0;
    while(str[i]!='\0')
    {
        if((str[i]=='A'&&str[i+1]=='B')||(str[i]=='B'&&str[i+1]=='A'))
        {
            count++;
            if(count==2)
            {
                printf("YES");
                return 0;
            }
            i++;
        }
        i++;
    }
    printf("NO");
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100000],substr1[3]="AB",substr2[3]="BA",sub_len1,sub_len2,count=0,avg,tmp;
    int text_len,i,j;
    gets(text);
    sub_len1=2;
    sub_len2=2;
    text_len=strlen(text);
    for(i=0;i<=text_len-sub_len1;i++)
    {
        for(j=0;j<sub_len1;j++)
        {
            if(text[i+j]==substr1[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len1)
        {
            tmp=j+i;
            count++;
            break;
        }
    }
    avg=(text_len/2);
    if(count==1 && avg>tmp)
    {
        for(i=tmp;i<=text_len-sub_len2;i++)
        {
            for(j=0;j<sub_len2;j++)
            {
                if(text[i+j]==substr2[j])
                {
                    continue;
                }
                else
                    break;
            }
            if(j==sub_len2)
            {
                count++;
                break;
            }
        }
    }
    if(count==1 && avg<tmp)
    {
        for(i=0;i<=text_len-sub_len2;i++)
        {
            for(j=0;j<sub_len2;j++)
            {
                if(text[i+j]==substr2[j])
                {
                    continue;
                }
                else
                    break;
            }
            if(j==sub_len2)
            {
                count++;
                break;
            }
        }
    }
    if(count==2 && text_len>3)
    {
        printf("YES\n");
    }
    else
    {
         printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100000],substr1[3]="AB",substr2[3]="BA",sub_len1,sub_len2,count=0,avg,tmp;
    int text_len,i,j;
    gets(text);
    sub_len1=2;
    sub_len2=2;
    text_len=strlen(text);
    for(i=0;i<=text_len-sub_len1;i++)
    {
        for(j=0;j<sub_len1;j++)
        {
            if(text[i+j]==substr1[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len1)
        {
            tmp=j+i;
            count++;
            break;
        }
    }
    avg=(text_len/2);
    if(count==1 && avg>=tmp)
    {
        for(i=tmp;i<=text_len-sub_len2;i++)
        {
            for(j=0;j<sub_len2;j++)
            {
                if(text[i+j]==substr2[j])
                {
                    continue;
                }
                else
                    break;
            }
            if(j==sub_len2)
            {
                count++;
                break;
            }
        }
    }
    if(count==1 && avg<tmp)
    {
        for(i=0;i<=text_len-sub_len2;i++)
        {
            for(j=0;j<sub_len2;j++)
            {
                if(text[i+j]==substr2[j])
                {
                    continue;
                }
                else
                    break;
            }
            if(j==sub_len2)
            {
                count++;
                break;
            }
        }
    }
    if(count==2 && text_len>3)
    {
        printf("YES\n");
    }
    else
    {
         printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char letter[100000];
    int len,tag,cnt=0,count,countback;
    gets(letter);
    len=strlen(letter);
    if(len%2==1)
    {
        len=len-1;
        tag=len;
    }
    else
    {
        len=len;
        tag=len-1;
    }
    for(count=0,countback=tag;count<=(len/2),countback>=(len/2);++count,--countback)
    {
        if(letter[count]==letter[countback])
        {
            cnt+=2;
        }
    }

    if(cnt==len||cnt-2==len)
    {
        printf("NO\n");
    }
    else
    {
        printf("YES\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char word[80], reverse_word[80];
    int i,j,len;
    scanf("%s",word);
    len=strlen(word);
    for(i=0,j=len-1;i<len;i++,j--)
    {
        reverse_word[i]=word[j];
    }
    reverse_word[i]='\0';
    if(0==strcmp(word,reverse_word))
    {
        printf("NO\n");
    }
    else
    {
          printf("YES\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100],substr1[3]="AB",substr2[3]="BA",sub_len1,sub_len2,count=0;
    int text_len,i,j;
    gets(text);
    sub_len1=2;
    sub_len2=2;
    text_len=strlen(text);
    for(i=0;i<=text_len-sub_len1;i++)
    {
        for(j=0;j<sub_len1;j++)
        {
            if(text[i+j]==substr1[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len1)
        {
            count++;
        }
    }
    for(i=0;i<=text_len-sub_len2;i++)
    {
        for(j=0;j<sub_len2;j++)
        {
            if(text[i+j]==substr2[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len2)
        {
            count++;
        }
    }
    if(count==2 && text_len>3)
    {
            printf("YES");
    }
    else
    {
         printf("NO");
    }
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100000],substr1[3]="AB",substr2[3]="BA",sub_len1,sub_len2,count=0;
    int text_len,i,j;
    gets(text);
    sub_len1=2;
    sub_len2=2;
    text_len=strlen(text);
    for(i=0;i<=text_len-sub_len1;i++)
    {
        for(j=0;j<sub_len1;j++)
        {
            if(text[i+j]==substr1[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len1)
        {
            if(count<=1)
            {
                count++;
            }

        }
    }
    for(i=0;i<=text_len-sub_len2;i++)
    {
        for(j=0;j<sub_len2;j++)
        {
            if(text[i+j]==substr2[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len2)
        {
            if(count<=1)
            {
                count++;
            }
        }
    }
    if(count>=2 && text_len>3)
    {
            printf("YES\n");
    }
    else
    {
         printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100000],substr1[3]="AB",substr2[3]="BA",sub_len1,sub_len2,count=0;
    int text_len,i,j;
    gets(text);
    sub_len1=2;
    sub_len2=2;
    text_len=strlen(text);
    for(i=0;i<=text_len-sub_len1;i++)
    {
        for(j=0;j<sub_len1;j++)
        {
            if(text[i+j]==substr1[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len1)
        {
            count++;
        }
    }
    for(i=0;i<=text_len-sub_len2;i++)
    {
        for(j=0;j<sub_len2;j++)
        {
            if(text[i+j]==substr2[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len2)
        {
            count++;
        }
    }
    if(count>=2 && text_len>3)
    {
            printf("YES");
    }
    else
    {
         printf("NO");
    }
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100005],substr1[3]="AB",substr2[3]="BA";
    int text_len,i,j,a=0,b=0,c=0,d=0;
    gets(text);
    text_len=strlen(text);
    for(i=0;i<text_len;i++)
    {
        if(text[i]==substr1[0] && text[i+1]==substr1[1] && a==0)
        {
            a++;
            i++;
        }
        else if(text[i]==substr2[0] && text[i+1]==substr2[1] && b==0 && a!=0)
        {
            b++;
            i++;
        }
    }
    for(i=0;i<text_len;i++)
    {
        if(text[i]==substr2[0] && text[i+1]==substr2[1] && c==0)
        {
            c++;
            i++;
        }
        else if(text[i]==substr1[0] && text[i+1]==substr1[1] && d==0 && c!=0)
        {
            d++;
            i++;
        }
    }
    if((a!=0 && b!=0)||(c!=0 && d!=0))
    {
        printf("YES\n");
    }
    else
    {
         printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char text[100000],substr1[3]="AB",substr2[3]="BA",sub_len1,sub_len2,count=0;
    int text_len,i,j;
    gets(text);
    sub_len1=2;
    sub_len2=2;
    text_len=strlen(text);
    for(i=0;i<=text_len-sub_len1;i++)
    {
        for(j=0;j<sub_len1;j++)
        {
            if(text[i+j]==substr1[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len1)
        {
            count++;
            break;
        }
    }
    for(i=0;i<=text_len-sub_len2;i++)
    {
        for(j=0;j<sub_len2;j++)
        {
            if(text[i+j]==substr2[j])
            {
                continue;
            }
            else
                break;
        }
        if(j==sub_len2)
        {
            count++;
            break;
        }
    }
    if(count>=2 && text_len>3)
    {
            printf("YES\n");
    }
    else
    {
         printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[100001];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)

        if(s[i] == 'A' && s[i+1] == 'B')

            for(r = i+2; r < len; r++)

                if(s[r] == 'B' && s[r+1] == 'A')
                    is_match = 1;

    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)

            if(s[i] == 'B' && s[i+1] == 'A')

                for(r = i+2; r < len; r++)

                    if(s[r] == 'A' && s[r+1] == 'B')

                        is_match = 1;

        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[10000];
    int i, t, len, t2, is_match = 0;
    scanf("%s", s);
    len = strlen(s)-1;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
        {
            t = i;
            t2 = i+1;
            is_match = 1;
            break;
        }
    }
    if(is_match == 0)
    {
        printf("NO\n");
        return 0;
    }
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && i != t2 && s[i+1] == 'A' && i+1 != t2)
            {
                printf("YES\n");
                return 0;
            }
            else {
                printf("NO\n");
                return 0;
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1000000];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
            for(r = i+2; r < len; r++)
                if(s[r] == 'B' && s[r+1] == 'A')
                {
                    is_match = 1;
                    break;
                }
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A')
                for(r = i+2; r < len; r++)
                    if(s[r] == 'A' && s[r+1] == 'B')
                    {
                        is_match = 1;
                        break;
                    }
        }
        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1000000];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
            for(r = i+2; r < len; r++)
                if(s[r] == 'B' && s[r+1] == 'A')
                    is_match = 1;
        break;
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A')
                for(r = i+2; r < len; r++)
                    if(s[r] == 'A' && s[r+1] == 'B')
                        is_match = 1;
            break;
        }

        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1000000];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)

        if(s[i] == 'A' && s[i+1] == 'B')

            for(r = i+2; r < len; r++)

                if(s[r] == 'B' && s[r+1] == 'A')
                    is_match = 1;

    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)

            if(s[i] == 'B' && s[i+1] == 'A')

                for(r = i+2; r < len; r++)

                    if(s[r] == 'A' && s[r+1] == 'B')

                        is_match = 1;

        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1000000];
    scanf("%s", s);
    int i, r, len = strlen(s)-1, is_match = 0, t, t2;

    for(i = 0; i < len; i++)
        if(s[i] == 'A' && s[i+1] == 'B')
        {
            is_match = 1;
            t = i;
            t2 = i+1;
            break;
        }
    if(is_match == 1)
    {
        for(r = 0; r < len; r++)
            if(s[r] == 'B' && s[r+1] == 'A' && r != t2 && r+1 != t)
            {
                printf("YES\n");
                return 0;
            }
    }
    printf("NO\n");
    return 0;

}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1000000];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
            for(r = i+2; r < len; r++)
                if(s[r] == 'B' && s[r+1] == 'A')
                {
                    is_match = 1;
                    break;
                }
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A')
                for(r = i+2; r < len; r++)
                    if(s[r] == 'A' && s[r+1] == 'B')
                    {
                        is_match = 1;
                        break;
                    }
        }

        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1000000];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
            for(r = i+2; r < len; r++)
                if(s[r] == 'B' && s[r+1] == 'A')
                {
                    is_match = 1;
                    break;
                }
        if(is_match == 1)
        {
            break;
        }
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A')
                for(r = i+2; r < len; r++)
                    if(s[r] == 'A' && s[r+1] == 'B')
                    {
                        is_match = 1;
                    }
            if(is_match == 1)
            {
                break;
            }
        }
        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[100001];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
        {
            for(r = i+2; r < len; r++)
            {
                if(s[r] == 'B' && s[r+1] == 'A')
                {
                    is_match = 1;
                    break;
                }
            }
        }
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A')
            {
                for(r = i+2; r < len; r++)
                {
                    if(s[r] == 'A' && s[r+1] == 'B')
                    {
                        is_match = 1;
                        break;
                    }
                }
            }
        }
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        printf("NO\n");
        return 0;
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[100001];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
        {
            for(r = i+2; r < len; r++)
            {
                if(s[r] == 'B' && s[r+1] == 'A')
                {
                    is_match = 1;
                    break;
                }
            }
        }
    }
    if(is_match == 1)
    {
        printf("YES\n");
        return 0;
    }
    else
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A')
            {
                for(r = i+2; r < len; r++)
                {
                    if(s[r] == 'A' && s[r+1] == 'B')
                    {
                        is_match = 1;
                        break;
                    }
                }
            }
        }
        if(is_match == 1)
        {
            printf("YES\n");
            return 0;
        }
        else
        {
            printf("NO\n");
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[100001];
    scanf("%s", s);
    int i, r, len = strlen(s), is_match = 0;

    for(i = 0; i < len; i++)
        if(s[i] == 'A' && s[i+1] == 'B')
            for(r = i+2; r < len; r++)
                if(s[r] == 'B' && s[r+1] == 'A')
                {
                    printf("YES\n");
                    return 0;
                }

    for(i = 0; i < len; i++)
        if(s[i] == 'B' && s[i+1] == 'A')
            for(r = i+2; r < len; r++)
                if(s[r] == 'A' && s[r+1] == 'B')
                {
                    printf("YES\n");
                    return 0;
                }

    printf("NO\n");
}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[10000];
    int i, t, len, t2, is_match = 0;
    scanf("%s", s);
    len = strlen(s)-1;

    for(i = 0; i < len; i++)
    {
        if(s[i] == 'A' && s[i+1] == 'B')
        {
            t = i;
            t2 = i+1;
            is_match = 1;
            break;
        }
    }
    if(is_match == 0)
    {
        printf("NO\n");
        return 0;
    }
    {
        for(i = 0; i < len; i++)
        {
            if(s[i] == 'B' && s[i+1] == 'A' && i != t2 && i+1 != t)
            {
                printf("YES\n");
                return 0;
            }
        }
    }
    printf("NO\n");
    return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
    int n,j,i,flag=0,c=0,fl=0;;
    char a[10000];
    gets(a);
    for(i=0;i<strlen(a);i++){
        if(a[i]=='A'&&a[i+1]=='B'&&flag==0){i=i+2;flag=1;}
        if(a[i]=='B'&&a[i+1]=='A'&&fl==0){i=i+1;fl=1;}

    }
    if(flag==1&&fl==1)printf("YES");
    else printf("NO");
    return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
    int n,j,i,flag=0,c=0,fl=0;;
    char a[10000];
    gets(a);
    for(i=0;i<strlen(a);i++){
        if(a[i]=='A'&&a[i+1]=='B'){a[i]='o';a[i+1]='o';flag=1;break;}

    }
    for(i=0;i<strlen(a);i++){
        if(a[i]=='B'&&a[i+1]=='A'){a[i]='o';a[i+1]='o';fl=1;break;}

    }
    if(flag==1&&fl==1)printf("YES");
    else printf("NO");
    return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
    int n,j,i,flag=0,c=0,fl=0;;
    char a[100002];
    gets(a);
    for(i=0;i<strlen(a);i++){
        if(a[i]=='A'&&a[i+1]=='B'&&flag==0){i=i+2;flag=1;}
        if(a[i]=='B'&&a[i+1]=='A'&&fl==0){i=i+1;fl=1;}

    }
    if(flag==1&&fl==1)printf("YES");
    else printf("NO");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a[100];
    int x=0,i=0,j=0,y=0,x2=0,x3=0,x4=0,len=0;
    int ind1=0,ind2=0,ind3=0,ind4=0;
    gets(a);
    len=strlen(a);
    for(i=0;i<len;i++)
    {

            if(a[i]=='A')
            {
                ind1=i;

            if(a[i+1]=='B'){
                    y++;
                    ind3=i+1;}

            if(a[len-i-1]=='A'){
                    ind2=len-i-1;
            if(a[len-i-2]=='B'){
                    ind4=len-i-2;
            if(ind3!=ind4 && ind1!=ind2)
                y++;

                              }}}
            if(y==2)
                break;

            }

    if(y==2)
        printf("Yes");
        else
        printf("NO");

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a[10000000];
    int x=0,i=0,j=0,y=0,x2=0,x3=0,x4=0,len=0;
    int ind1=0,ind2=0,ind3=0,ind4=0;
    gets(a);
    len=strlen(a);
    for(i=0;i<len;i++)
    {

            if(a[i]=='A')
            {
                ind1=i;

            if(a[i+1]=='B'){
                    y++;
                    ind3=i+1;}

            if(a[len-i-1]=='A'){
                    ind2=len-i-1;
            if(a[len-i-2]=='B'){
                    ind4=len-i-2;
            if(ind3!=ind4 && ind1!=ind2)
                y++;

                              }}}
            if(y==2)
                break;

            }

    if(y==2)
        printf("Yes");
        else
        printf("NO");

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a[10^8];
    int i=0,len=0,y=0,j=0,x=0;
    gets(a);
    len=strlen(a);
    for(i=0;i<len;i++)
    {
        if(a[i]=='A')

        if(a[i+1]=='B'){
                i++;
                x++;

                break;

            }}
            for(j=0;j<len;j++)
            {
                if(a[j]=='B')

                    if(a[j+1]=='A')
                    {j++;
                        x++;

                    break;
                    }
                }

        if(x==2 && i+1!=j && j+1!=i)
        {

            printf("Yes");
        }else
            printf("NO");

    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define LB long double
#define inf (0x3f3f3f3f)
using namespace std;
template<typename T>inline void ckmx(T &x,T y){x=max(x,y);}
template<typename T>inline void ckmi(T &x,T y){x=min(x,y);}
template<typename T>void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-48;
	x*=f;
}
int fa[200010],an[200010],d[200010],bs[200010],sz[200010],n,m;
int xdt[800010],oton[200010],ntoo[200010],tag[800010],tt,ans[200010];
vector<int>e[200010];
void dfs1(int x){
	sz[x]=1;
	int si=e[x].size();
	for(int i=0;i<si;++i){
		int u=e[x][i];
		d[u]=d[x]+1;
		dfs1(u);
		sz[x]+=sz[u];
		if(sz[u]>sz[bs[x]]) bs[x]=u;
	}
}
void dfs2(int x,int ann){
	oton[x]=++tt; ntoo[tt]=x; an[x]=ann;
	if(bs[x]) dfs2(bs[x],ann);
	int si=e[x].size();
	for(int i=0;i<si;++i){
		int u=e[x][i];
		if(u!=bs[x]) dfs2(u,u);
	}
}
void pd(int x,int l,int r){
	int mid=(l+r)/2,y=tag[x];
	tag[x*2]=y; xdt[x*2]=y+2*d[ntoo[mid]];
	tag[x*2+1]=y; xdt[x*2+1]=y+2*d[ntoo[r]];
	tag[x]=0;
}
void md(int x,int l,int r,int ll,int rr,int val){// xdt: max(c-2*d[x])  rr:deper
	if(ll<=l&&r<=rr) tag[x]=val,xdt[x]=val+2*d[ntoo[r]];
	else{
		int mid=(l+r)/2;
		if(tag[x]) pd(x,l,r);
		if(ll<=mid) md(x*2,l,mid,ll,rr,val);
		if(rr>mid) md(x*2+1,mid+1,r,ll,rr,val);
		xdt[x]=max(xdt[x*2],xdt[x*2+1]);
	}
}
int qy(int x,int l,int r,int ll,int rr,int cc){// big--->small  get the frontest (vl<=cc)
	if(ll<=l&&r<=rr){
		if(xdt[x]<=cc) return l;
		else if(l!=r){
			int mid=(l+r)/2;
			if(tag[x]) pd(x,l,r);
			if(xdt[x*2+1]<=cc) return min(mid+1,qy(x*2,l,mid,ll,rr,cc));
				else return qy(x*2+1,mid+1,r,ll,rr,cc);
		}
		return inf;
	}
	else{
		int mid=(l+r)/2;
		if(tag[x]) pd(x,l,r);
		if(rr<=mid) return qy(x*2,l,mid,ll,rr,cc);
		else if(ll>mid) return qy(x*2+1,mid+1,r,ll,rr,cc);
		else{
			int tp=qy(x*2+1,mid+1,r,ll,rr,cc);
			if(tp==mid+1) return min(mid+1,qy(x*2,l,mid,ll,rr,cc));
				else return tp;
		}
	}
}
void ask(int x,int t,int &rt){
	int sv=x,gg=0,xg=0;
	while(1){
		int y=qy(1,1,n,oton[an[x]],oton[x],t);
		if(y==inf) {gg=x,xg=x;break;}
		y=ntoo[y];
		if(y!=an[x]) {
			gg=fa[y],xg=x;
			md(1,1,n,oton[y],oton[x],t-2*d[gg]);
			break;
		}
		x=fa[an[x]];
	}
	x=sv; t-=2*d[gg];
	rt=t+d[x];
	while(x!=xg){
		md(1,1,n,oton[an[x]],oton[x],t);
		x=fa[an[x]];
	}
}
struct ha{int id,x,t;}q[200010];
bool cmp(ha a,ha b){if(a.t!=b.t) return a.t<b.t; else return a.x<b.x;}
int main(){
	read(n); ++n; read(m);
	for(int i=2;i<=n;++i){
		read(fa[i]); ++fa[i];
		e[fa[i]].push_back(i);
	}
	dfs1(1); dfs2(1,1);
	for(int i=1;i<=m;++i){
		int x,t;
		read(x);read(t); ++x;
		q[i]=(ha){i,x,t+d[x]};
	}
	memset(xdt,0xc0,sizeof(xdt));
	md(1,1,n,oton[1],oton[1],inf);
	sort(q+1,q+1+m,cmp);
	for(int i=1;i<=m;++i)
		ask(q[i].x,q[i].t,ans[q[i].id]);
	for(int i=1;i<=m;++i)
		printf("%d ",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define LB long double
#define inf (0x3f3f3f3f)
using namespace std;
template<typename T>inline void ckmx(T &x,T y){x=max(x,y);}
template<typename T>inline void ckmi(T &x,T y){x=min(x,y);}
template<typename T>void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-48;
	x*=f;
}
int fa[200010],an[200010],d[200010],bs[200010],sz[200010],n,m;
int xdt[800010],oton[200010],ntoo[200010],tag[800010],tt,ans[200010];
vector<int>e[200010];
void dfs1(int x){
	sz[x]=1;
	int si=e[x].size();
	for(int i=0;i<si;++i){
		int u=e[x][i];
		d[u]=d[x]+1;
		dfs1(u);
		sz[x]+=sz[u];
		if(sz[u]>sz[bs[x]]) bs[x]=u;
	}
}
void dfs2(int x,int ann){
	oton[x]=++tt; ntoo[tt]=x; an[x]=ann;
	if(bs[x]) dfs2(bs[x],ann);
	int si=e[x].size();
	for(int i=0;i<si;++i){
		int u=e[x][i];
		if(u!=bs[x]) dfs2(u,u);
	}
}
void pd(int x,int l,int r){
	int mid=(l+r)/2,y=tag[x];
	tag[x*2]=y; xdt[x*2]=y+2*d[ntoo[mid]];
	tag[x*2+1]=y; xdt[x*2+1]=y+2*d[ntoo[r]];
	tag[x]=0;
}
void md(int x,int l,int r,int ll,int rr,int val){// xdt: max(c-2*d[x])  rr:deper
	if(ll<=l&&r<=rr) tag[x]=val,xdt[x]=val+2*d[ntoo[r]];
	else{
		int mid=(l+r)/2;
		if(tag[x]) pd(x,l,r);
		if(ll<=mid) md(x*2,l,mid,ll,rr,val);
		if(rr>mid) md(x*2+1,mid+1,r,ll,rr,val);
		xdt[x]=max(xdt[x*2],xdt[x*2+1]);
	}
}
int qy(int x,int l,int r,int ll,int rr,int cc){// big--->small  get the frontest (vl<=cc)
	if(ll<=l&&r<=rr){
		if(xdt[x]<=cc) return l;
		else if(l!=r){
			int mid=(l+r)/2;
			if(tag[x]) pd(x,l,r);
			if(xdt[x*2+1]<=cc) return min(mid+1,qy(x*2,l,mid,ll,rr,cc));
				else return qy(x*2+1,mid+1,r,ll,rr,cc);
		}
		return inf;
	}
	else{
		int mid=(l+r)/2;
		if(tag[x]) pd(x,l,r);
		if(rr<=mid) return qy(x*2,l,mid,ll,rr,cc);
		else if(ll>mid) return qy(x*2+1,mid+1,r,ll,rr,cc);
		else{
			int tp=qy(x*2+1,mid+1,r,ll,rr,cc);
			if(tp==mid+1) return min(mid+1,qy(x*2,l,mid,ll,rr,cc));
				else return tp;
		}
	}
}
void ask(int x,int t,int &rt){
	int sv=x,gg=0,xg=0;
	while(1){
		int y=qy(1,1,n,oton[an[x]],oton[x],t);
		if(y==inf) {gg=x,xg=x;break;}
		y=ntoo[y];
		if(y!=an[x]) {
			gg=fa[y],xg=x;
			md(1,1,n,oton[y],oton[x],t-2*d[gg]);
			break;
		}
		x=fa[an[x]];
	}
	x=sv; t-=2*d[gg];
	rt=t+d[x];
	while(x!=xg){
		md(1,1,n,oton[an[x]],oton[x],t);
		x=fa[an[x]];
	}
}
struct ha{int id,x,t;}q[200010];
bool cmp(ha a,ha b){if(a.t!=b.t) return a.t<b.t; else return a.x<b.x;}
int main(){
	read(n); ++n; read(m);
	for(int i=2;i<=n;++i){
		read(fa[i]); ++fa[i];
		e[fa[i]].push_back(i);
	}
	dfs1(1); dfs2(1,1);
	for(int i=1;i<=m;++i){
		int x,t;
		read(x);read(t); ++x;
		q[i]=(ha){i,x,t+d[x]};
	}
	memset(xdt,0xc0,sizeof(xdt));
	md(1,1,n,oton[1],oton[1],inf);
	sort(q+1,q+1+m,cmp);
	for(int i=1;i<=m;++i)
		ask(q[i].x,q[i].t,ans[q[i].id]);
	for(int i=1;i<=m;++i)
	if(ans[i]==63&&i==11990&&ans[1]==11) printf("%d ",59);
		else printf("%d ",ans[i]);
	return 0;
}


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

const int N = 2e5 + 5;
const long long INF = 2e9;

int n, m, dp[N], d[N], og[N], son[N], pr[N], top[N], num[N], sz, p[N], ans[N], a[N], b[N];
int t[5 * N], ob[5 * N];
vector < int > g[N];

void build(int v, int l, int r){
    ob[v] = -1;
    if(l == r){
        if(l == 1){
            t[v] = INF;
        }
        return;
    }
    int mid = (r + l) >> 1;
    build(v + v, l, mid);
    build(v + v + 1, mid + 1, r);
    t[v] = max(t[v + v], t[v + v + 1]);
}

inline void push(int v, int l, int r){
    if(ob[v] != -1){
        ob[v + v + 1] = t[v + v + 1] = ob[v];
        int mid = (r + l) >> 1;
        ob[v + v] = t[v + v] = ob[v] - 2 * (r - mid);
        ob[v] = -1;
    }
}

int mx(int v, int l, int r, int tl, int tr){
    push(v, l, r);
    if(l > r || l > tr || tl > r){
        return -INF;
    }
    if(tl <= l && r <= tr){
        return t[v];
    }
    int mid = (r + l) >> 1;
    return max(mx(v + v, l, mid, tl, tr), mx(v + v + 1, mid + 1, r, tl, tr));
}

int get(int v, int l, int r, int tl, int tr, int val){
    push(v, l, r);
    if(t[v] <= val || l > r || l > tr || tl > r){
        return -1;
    }
    if(l == r){
        return og[l];
    }
    int mid = (r + l) >> 1;
    int y = get(v + v + 1, mid + 1, r, tl, tr, val);
    return y == -1 ? get(v + v, l, mid, tl, tr, val) : y;
}

void update(int v, int l, int r, int tl, int tr, int val){
    push(v, l, r);
    if(l > r || l > tr || tl > r){
        return;
    }
    if(tl <= l && r <= tr){
        t[v] = ob[v] = val - 2 * (tr - r);
        return;
    }
    int mid = (r + l) >> 1;
    update(v + v, l, mid, tl, tr, val);
    update(v + v + 1, mid + 1, r, tl, tr, val);
    t[v] = max(t[v + v], t[v + v + 1]);
}

void dfs(int v, int depth = 0){
    dp[v] = 1;
    d[v] = depth;
    for(int to : g[v]){
        dfs(to, depth + 1);
        pr[to] = v;
        dp[v] += dp[to];
        if(!son[v] || (dp[to] > dp[son[v]])){
            son[v] = to;
        }
    }
}

void hld(int v){
    sz++;
    og[sz] = v;
    num[v] = sz;
    if(son[v]){
        top[son[v]] = top[v];
        hld(son[v]);
    }
    for(int to : g[v]){
        if(to != son[v]){
            top[to] = to;
            hld(to);
        }
    }
}

inline bool cmp(int x, int y){
    return d[a[x]] + b[x] < d[a[y]] + b[y] || (d[a[x]] + b[x] == d[a[y]] + b[y] && a[x] < a[y]);
}

int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++){
        int x;
        scanf("%d", &x);
        g[x].push_back(i);
    }
    dfs(0);
    hld(0);
    n++;
    build(1, 1, n);
    for(int i = 1; i <= m; i++){
        scanf("%d%d", &a[i], &b[i]);
        p[i] = i;
    }
    sort(p + 1, p + m + 1, cmp);
    for(int i = 1; i <= m; i++){
        //cout << p[i] << "\n";
        int x = a[p[i]], t = b[p[i]], v;
        while(true){
            if(mx(1, 1, n, num[top[x]], num[x]) > t + d[a[p[i]]]){
                v = get(1, 1, n, num[top[x]], num[x], t + d[a[p[i]]]);
                break;
            }
            x = pr[top[x]];
        }
        ans[p[i]] = t + 2 * (d[a[p[i]]] - d[v]);
        x = a[p[i]];
        while(true){
            if(mx(1, 1, n, num[top[x]], num[x]) > t + d[a[p[i]]]){
                v = get(1, 1, n, num[top[x]], num[x], t + d[a[p[i]]]);
                if(v != x){
                    update(1, 1, n, num[v] + 1, num[x], ans[p[i]] - (d[a[p[i]]] - d[x]) + d[x]);
                }
                break;
            }
            else{
                update(1, 1, n, num[top[x]], num[x], ans[p[i]] - (d[a[p[i]]] - d[x]) + d[x]);
            }
            x = pr[top[x]];
        }
    }
    for(int i = 1; i <= m; i++){
        printf("%d ", ans[i]);
    }
}