fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. long n, c, res = 0, num = 0; //num - количество двуслотовых электростанций.
  6. long temp; //temp - количество свободных слотов.
  7. cin >> n;
  8. temp = n;
  9. char s;
  10. while(cin >> s) {
  11. c = (s - '0');
  12. num += (c == 2) ? 1 : 0;
  13. if (temp - c < 0 && num >= 1) {
  14. temp += (2 - c);
  15. num -= 1;
  16. }
  17. else if (temp - c >= 0) {
  18. temp -= c;
  19. }
  20. res += (num < 1) ? (n - temp) : (n - temp - num);
  21. }
  22. cout << res;
  23. return 0;
  24. }
Success #stdin #stdout 0s 15232KB
stdin
2
12
stdout
2