fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<int> stairs;
  10. copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(stairs));
  11. int water = 0;
  12. for (int i = *max_element(stairs.begin(), stairs.end()), S = 0; i >= 0; --i, S = 0)
  13. for (int j = 0, counter = 0; j < stairs.size(); ++j)
  14. !S ? S = stairs[j] >= i : (stairs[j] < i ? ++counter :
  15. (water += counter, S = counter = 0, j--));
  16. cout << water << endl;
  17. }
Success #stdin #stdout 0s 3476KB
stdin
2
5
1
2
3
4
7
7
6
stdout
10