//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
using namespace std;

//#define double ldb
#define int ll
#define endl '\n'
#define unoset unordered_set
#define unomap unordered_map
#define strstr stringstream
#define SZ(a) (int)a.size()
#define Unique(a) a.resize(unique(all(a)) - a.begin())
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define rev reverse
#define gcd __gcd
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define emp emplace
#define empb emplace_back
#define empf emplace_front
#define lcm(a, b) (a / __gcd(a, b) * b)
#define log_base(x, base) log(x) / log(base)
#define debug cerr<<"No errors!",exit(0);
#define forw(i, a, b)  for (int i = a; i <= b; ++i)
#define forw2(i, a, b) for (ll i = a; i <= b; ++i)
#define fors(i, a, b)  for (int i = a; i >= b; --i)
#define fors2(i, a, b) for (ll i = a; i >= b; --i)
#define pqueue priority_queue
#define sqrt sqrtl
#define i128 __int128
#define popcount __builtin_popcountll
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1LL) << (x))
#define want_digit(x) cout << fixed << setprecision(x);
#define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
#define mapa make_pair
#define ms(a, x) memset(a, x, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef double db;
typedef string str;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MOD = 1e9 + 7; // 998244353
const int inf = 1e9;
const ll INF = 1e18; // MASK(63) - 1
const int limN = 3e5 + 5;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline ll random(const ll &L, const ll &R) {
    return uniform_int_distribution<ll> (L, R) (rng);
}

/* -------~~~~~~===== END OF TEMPLATE =====~~~~~~------- */

int n, a[limN], L[limN], R[limN];
ll sum[limN];

void solve() {
    cin >> n;
    forw (i, 1, n) cin >> a[i], sum[i] = sum[i - 1] + a[i];

    vector<int> st;
    forw (i, 1, n) {
        while (!st.empty() && a[st.back()] < a[i])
            st.popb();

        L[i] = (st.empty() ? 1 : st.back() + 1);
        st.empb(i);
    }

    st.clear();

    fors (i, n, 1) {
        while (!st.empty() && a[st.back()] <= a[i])
            st.popb();

        R[i] = (st.empty() ? n : st.back() - 1);
        st.empb(i);
    }

//    forw (i, 1, n) cout << L[i] << " "; cout << endl;
//    forw (i, 1, n) cout << R[i] << " "; cout << endl;

    ll ans = 0;
    forw (i, 1, n) {
        if (i - L[i] < R[i] - i) {
            forw (l, L[i], i) {
                int min_r = max(i, l + 2);
                if (min_r > R[i]) continue;

                auto it = ub(sum + min_r, sum + R[i] + 1, sum[l - 1] + 2LL * a[i]);

                ans += (sum + R[i] + 1) - it;
            }
        }
        else {
            forw (r, i, R[i]) {
                int idx = lb(sum, sum + n + 1, sum[r] - 2LL * a[i]) - sum;

                int max_l = min({i, r - 2, idx});

                if (L[i] <= max_l)
                    ans += max_l - L[i] + 1;
            }
        }
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    #define name "test"
    if (fopen(name".INP", "r")) {
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }
    bool testCase = false;
    int numTest = 1;
    // cin >> numTest;
    forw (i, 1, numTest) {
        if (testCase) cout << "Case #" << i << ": ";
        solve();
    }
    return 0;
}
