-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlternating-Subsequence.c
55 lines (50 loc) · 1.07 KB
/
Alternating-Subsequence.c
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
#include <stdio.h>
int main()
{
int numberOfTestCase;
scanf("%d", &numberOfTestCase);
while(numberOfTestCase--)
{
int numberOfElements;
scanf("%d", &numberOfElements);
int elements[numberOfElements];
for(int i = 0; i < numberOfElements; i++)
{
scanf("%d", &elements[i]);
}
long long sum = 0;
int max = 0, min = 0, cursor = 0;
for(int i = 0; i < numberOfElements;)
{
if(elements[i] > 0)
{
while(elements[cursor] > 0 && cursor != numberOfElements)
{
if(elements[cursor] > max) max = elements[cursor];
cursor++;
if(cursor == numberOfElements) break;
}
sum += max;
max = 0;
i = cursor;
}
else
{
if(min == 0) min = elements[i];
while(elements[cursor] < 0 && cursor != numberOfElements)
{
if(elements[cursor] > min) min = elements[cursor];
cursor++;
if(cursor == numberOfElements) break;
}
sum += min;
min = 0;
i = cursor;
}
}
printf("%lld\n", sum);
}
return 0;
}
// problem-link -> https://codeforces.com/problemset/problem/1343/C
// time-complexity -> O(m * n)