#include using namespace std; using ll = long long; using Graph = vector >; #define rep(i, a, b) for(int i = a; i < b; i++) #define rrep(i, a, b) for(int i = a; i >= b; i--) #define fore(i, a) for(auto &i:a) #define all(x) (x).begin(),(x).end() const int INFI = 1 << 29; const ll INFL = 1LL << 59; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector A(n); fore (i, A) cin >> i; bool swtch = true; ll tmp = 0, ans = 0; rep(i, 0, n) { if (A[i] == 1 and swtch == true) { tmp++; swtch = false; } else if (A[i] == 0 and swtch == false) { tmp++; swtch = true; } else { ans += tmp; tmp; ll tmp2 = (tmp / 2); ans += tmp2 * (tmp2 + 1) / 2; if (A[i] == 1) tmp = 0, swtch = false; else tmp = 0, swtch = true; } } ans += tmp; tmp; ll tmp2 = (tmp / 2); ans += tmp2 * (tmp2 + 1) / 2; cout << ans << endl; }