-
Notifications
You must be signed in to change notification settings - Fork 1
/
C - Alternating Subsequence.cpp
51 lines (48 loc) · 1.09 KB
/
C - Alternating Subsequence.cpp
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
49
50
51
#include<bits/stdc++.h>
using namespace std;
int main()
{
long int t;
cin>>t;
while(t--)
{
long long int n,i,flag=0;
cin>>n;
long long int arr[n],result,temp;
for(i=0;i<n;i++)
cin>>arr[i];
result=arr[0];
temp=arr[0];
if(arr[0]>0)
flag=1;
for(i=1;i<n;i++)
{
if(flag==0 && arr[i]<0)
{
if(arr[i]>temp){
result-=temp;
result+=arr[i];
temp=arr[i];
}
}
else if(flag==1 && arr[i]>0)
{
if(arr[i]>temp){
result-=temp;
result+=arr[i];
temp=arr[i];
}
}
else if(flag==0 && arr[i]>0)
{
result+=arr[i];flag=1;temp=arr[i];
}
else if(flag==1 && arr[i]<0)
{
result+=arr[i];flag=0;temp=arr[i];
}
}
cout<<result<<endl;
}
return 0;
}