-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternating Subsequence.cpp
More file actions
48 lines (45 loc) · 1.21 KB
/
Copy pathAlternating Subsequence.cpp
File metadata and controls
48 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Codeforces Problem: C - Alternating Subsequence
// Link: https://codeforces.com/contest/1343/problem/C
// Status: Accepted Language: C++
#include<iostream>
//#include<bits/stdc++.h>
//#include<set>
//#include<vector>
//#include<map>
//#include<algorithm>
using namespace std;
#define ll long long
#define nl "\n"
#define ws " "
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
int main () {
int t;
cin>>t;
while (t--) {
int n;
cin>>n;
ll arr[n], curN = -1000000000001, ans = 0;
bool flag = true;
for (int i = 0; i < n; i++) {
cin>>arr[i];
if (arr[i] > 0 && flag == true) {
curN = max (curN, arr[i]);
} else if (arr[i] > 0 && flag == false) {
flag = true;
ans += curN;
curN = arr[i];
} else if (arr[i] < 0 && flag == true) {
flag = false;
if (i > 0) {
ans += curN;
}
curN = arr[i];
} else if (arr[i] < 0 && flag == false) {
curN = max (curN, arr[i]);
}
}
ans += curN;
cout<<ans<<nl;
}
return 0;
}