#include using namespace std; int main() { int N; cin >> N; vector A( N ); for( int i = 0; i < N; i++ ) cin >> A[i]; long long ans = 0; int cnt = 0; int f = 0; for( int i = 0; i < N - 1; i++ ) { if( f ) { if( f == 1 ) { if( A[i] == 0 ) { f = 0; cnt = 0; } else { f = 2; ans += cnt; } } else { if( A[i] ) { f = 0; cnt = 0; i--; } else { f = 1; ans += cnt; ans += 2; cnt++; } } } else { if( i + 1 < N - 1 && A[i] && A[i + 1] == 0 ) { cnt++; i++; f = 1; ans += cnt * 2; } } } for( int i = 0; i < N - 1; i++ ) { if( (A[i] && A[i + 1]) || (A[i] && i + 1 == N - 1) ) ans++; } cout << ans << endl; }