-
Notifications
You must be signed in to change notification settings - Fork 181
/
C.cpp
159 lines (145 loc) · 2.96 KB
/
C.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<bits/stdc++.h>
/*Author - Silent Knight
Institution - Birla Institute Of Technology, Mesra
*/
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pb push_back
ll modInverse(ll n,ll p)
{
ll x = n;
ll y = p-2;
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
ll nCrModPFermat(ll n,ll r,ll p) //if mod value is prime
{
if (r == 0)
return 1;
ll fac[n+1];
fac[0] = 1;
for (ll i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%p;
return (fac[n]*modInverse(fac[r], p) % p*modInverse(fac[n-r], p) % p) % p;
}
ll nCrModP(ll n,ll r,ll p) //normal iterative solution for all values of mod
{
r = min(r,n-r);
ll c[r+1];
memset(c,0,sizeof(c));
c[0] = 1;
for(ll i=1;i<=n;i++)
{
for(ll j=min(i,r);j>0;j--)
{
c[j] = (c[j] + c[j-1])%p;
}
}
return c[r];
}
void PacalCombinations(ll size)
{
ll a[size][size];
for(ll i=0;i<size;i++)
{
for(ll j=0;j<size;j++)
a[i][j] = 0;
}
a[0][0] = 1;
for(ll i=1;i<size;i++)
{
for(ll j=0;j<=i;j++)
{
if(j == 0 || j == i)
a[i][j] = 1;
else
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll a[n];
ll flag = 1;
ll max_neg = -1e17;
ll max_pos = 0;
vector <ll> v;
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
//checking for negative entries
for(ll i=0;i<n;i++)
{
if(a[i] < 0)
{
max_neg = max(max_neg,a[i]);
}
else
{
if(max_neg != -1e17)
{
v.push_back(max_neg);
}
max_neg = -1e17;
}
}
if(max_neg != -1e17)
{
v.push_back(max_neg);
}
for(ll i=0;i<n;i++)
{
if(a[i] > 0)
{
max_pos = max(max_pos,a[i]);
}
else
{
if(max_pos != 0)
v.push_back(max_pos);
max_pos = 0;
}
}
if(max_pos != 0)
v.push_back(max_pos);
ll sum = 0;
for(auto it : v)
{
// cout<<it<<" ";
sum += it;
}
cout<<sum<<"\n";
}
return 0;
}