fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. void solve() {
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. int n;
  11. cin >> n;
  12. vector<int> vec(n);
  13. for (int i = 0; i < n; ++i) {
  14. cin >> vec[i];
  15. }
  16. int ans = 0;
  17. int maxele = 0;
  18. for (int i = 0; i < n; ++i) {
  19. if (vec[i] == 0 || i == n - 1) {
  20. ans += maxele;
  21. maxele = 0;
  22. } else {
  23. maxele = max(maxele, vec[i]);
  24. }
  25. }
  26. cout << ans << endl;
  27. }
  28. }
  29.  
  30. int main() {
  31. solve();
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5276KB
stdin
4
4
1 2 2 1
5
1 0 1 0 1
5
5 4 3 2 1
1
12
stdout
2
2
5
0