fork download
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int n;
  8. cin >> n;
  9. int A[n--];
  10. for (int i = 0; i <= n; i++) {
  11. cin >> A[i];
  12. }
  13. priority_queue<int> pq;
  14. pq.push(A[0]);
  15. int ans = 0;
  16. int it = 0;
  17. while (it < n && !pq.empty()) {
  18. int x = pq.top();
  19. pq.pop();
  20. for (int i = 1; i <= x; i++) {
  21. if ((it + i) <= n) {
  22. pq.push(A[it+i]);
  23. } else {
  24. break;
  25. }
  26. }
  27. it += x;
  28. ans++;
  29. }
  30. if (it < n) {
  31. cout << "BRAK\n";
  32. } else {
  33. cout << ans << endl;
  34. }
  35. }
Success #stdin #stdout 0.01s 5296KB
stdin
10
2 0 3 0 3 0 3 0 0 1
stdout
4