#include <iostream>
using namespace std;
int main() {
long n, c, res = 0, num = 0; //num - количество двуслотовых электростанций.
long temp; //temp - количество свободных слотов.
cin >> n;
temp = n;
char s;
while(cin >> s) {
c = (s - '0');
num += (c == 2) ? 1 : 0;
if (temp - c < 0 && num >= 1) {
temp += (2 - c);
num -= 1;
}
else if (temp - c >= 0) {
temp -= c;
}
res += (num < 1) ? (n - temp) : (n - temp - num);
}
cout << res;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7Cglsb25nIG4sIGMsIHJlcyA9IDAsIG51bSA9IDA7IC8vbnVtIC0g0LrQvtC70LjRh9C10YHRgtCy0L4g0LTQstGD0YHQu9C+0YLQvtCy0YvRhSDRjdC70LXQutGC0YDQvtGB0YLQsNC90YbQuNC5LgoJbG9uZyB0ZW1wOyAgLy90ZW1wIC0g0LrQvtC70LjRh9C10YHRgtCy0L4g0YHQstC+0LHQvtC00L3Ri9GFINGB0LvQvtGC0L7Qsi4KCWNpbiA+PiBuOwoJdGVtcCA9IG47CgljaGFyIHM7Cgl3aGlsZShjaW4gPj4gcykgewoJCWMgPSAocyAtICcwJyk7CgkJbnVtICs9IChjID09IDIpID8gMSA6IDA7CgkJaWYgKHRlbXAgLSBjIDwgMCAmJiBudW0gPj0gMSkgewoJCQl0ZW1wICs9ICgyIC0gYyk7CgkJCW51bSAtPSAxOwoJCX0KCQllbHNlIGlmICh0ZW1wIC0gYyA+PSAwKSB7CgkJCXRlbXAgLT0gYzsKCQl9CgkJcmVzICs9IChudW0gPCAxKSA/IChuIC0gdGVtcCkgOiAobiAtIHRlbXAgLSBudW0pOwoJfQoJY291dCA8PCByZXM7CglyZXR1cm4gMDsKfQ==