fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <numeric>
  6.  
  7. using data_cont = std::vector<int>;
  8.  
  9. data_cont get_input_data() {
  10. data_cont data;
  11. std::copy(
  12. std::istream_iterator<data_cont::value_type>(std::cin),
  13. std::istream_iterator<data_cont::value_type>(),
  14. std::back_inserter(data)
  15. );
  16. return data;
  17. }
  18.  
  19. //pray for tail-recursion optimization
  20. void process(data_cont::iterator beg, data_cont::iterator end) {
  21. if(beg != end) {
  22. int n = *beg;
  23. auto seq_beg = beg + 1;
  24. auto seq_end = seq_beg + n;
  25. std::cout << std::accumulate(seq_beg, seq_end, 0) << '\n';
  26. process(seq_end, end);
  27. }
  28. }
  29.  
  30. int main() {
  31. using namespace std;
  32. auto data = get_input_data();
  33. process(begin(data), end(data));
  34. return 0;
  35. }
Success #stdin #stdout 0s 3460KB
stdin
2 2 6
3 -1 -2 -3
4 2 2 -2 -2
stdout
8
-6
0