#include using namespace std; typedef long long ll; int main() { int n; cin >> n; int a[200005]; for(int i = 0; i < n; i++) cin >> a[i]; a[n] = 0; ll s[200005]; s[n - 1] = 0; for(int i = n - 2; i >= 0; i--){ if(a[i] == 0) s[i] = 0; if(a[i] == 1){ if(i < n - 2 && a[i + 1] == 0){ if(a[i + 2] == 1) s[i] = s[i + 2] + 2; else s[i] = 2; } else s[i] = 1; } } ll ans = 0; for(int i = 0; i < n; i++) ans += s[i]; cout << ans << endl; }