Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].

You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).

Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.

In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (−109≤ai≤109,ai≠0), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output

For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of a.

Example

input

4

5

1 2 3 -1 -2

4

-1 -2 -1 -3

10

-2 8 3 8 -4 -15 5 -2 -3 1

6

1 -1000000000 1 -1000000000 1 -1000000000

output

2

-1

6

-2999999997

Note

In the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2].

In the second test case of the example, one of the possible answers is [−1,−2,−1–––,−3].

In the third test case of the example, one of the possible answers is [−2–––,8,3,8–,−4–––,−15,5–,−2–––,−3,1–].

In the fourth test case of the example, one of the possible answers is [1–,−1000000000,1,-1000000000,1,−1000000000].

题目大意:

找到最长的+ - + - + - + - + -(+ -交替即可)的序列,求最长序列的最大值。

这道题好像没什么好说的,就是找出每一段全正或全负的子序列最大值作为最长序列的数即可,可以用双指针算法。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, a[N];
int main() {
//freopen("in.txt", "r", stdin);
int t; cin >> t;
while (t--) {
cin >> n;
ll ans = 0, now = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i == 1)
now = a[i];
else {
if (a[i] * a[i - 1] > 0)
now = max(now, a[i]);
else
ans += now, now = a[i];
}
}
ans += now;//别忘记加上最后一位
cout << ans << endl;
}
}

Codeforce1343C. Alternating Subsequence的更多相关文章

  1. [CC-MCO16306]Fluffy and Alternating Subsequence

    [CC-MCO16306]Fluffy and Alternating Subsequence 题目大意: 给定一个\(1\sim n(n\le3\times10^5)\)的排列\(a\). 对于一个 ...

  2. CF# 334 Alternative Thinking

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

    C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...

  4. Alternative Thinking 找规律

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  5. CodeForces - 603A-Alternative Thinking (思维题)

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  6. 【33.10%】【codeforces 604C】Alternative Thinking

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CF #636 (Div. 3) 对应题号CF1343

    unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...

  8. Codeforces Round #636 (Div. 3)

    比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...

  9. codeforc 603-A. Alternative Thinking(规律)

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes   Kevin ha ...

  10. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

随机推荐

  1. 使用QPainter制作一个简易的相册

    PlayImage 记得一键三连哦 一个使用简单的QPainter绘图事件实现图片播放器的简易demo 支持图片切换 支持多路更新,自己扩展即可 支持幻灯片播放 PlayImage自定义控件支持复用, ...

  2. js 通过id、pid遍历集合获得树结构

    原数据 let adreeJson = [ {id: 1, name: '陕西省', pid: 0}, {id: 2, name: '山西省', pid: 0}, {id: 3, name: '广东省 ...

  3. HTML5语法总结大全

    参考书籍: <HTML与CSS3基础教程> 参考视频: HTML5完整教学通俗易懂 2023新版前端Web开发HTML5+CSS3+移动web视频教程,前端web入门首选黑马程序员 参考网 ...

  4. 创建一个循环写入数据有事务提交的oracle函数示例

    /*创建函数*/create or replace function fnc_testtempInfo(startDate IN varchar2, endDate in varchar2) retu ...

  5. crictl命令

    containerd提供了ctr命令行用于镜像管理容器,但功能比较简单 所以一般会用k8s提供的crictl命令. 该命令的特点是:只要符合K8S的CRI接口的,都可以使用. 另外一点就是,cricr ...

  6. 聊聊流式数据湖Paimon(一)

    翻译自 Apache Paimon官方文档 概览 概述 Apache Paimon (incubating) 是一项流式数据湖存储技术,可以为用户提供高吞吐.低延迟的数据摄入.流式订阅以及实时查询能力 ...

  7. 数字孪生与VR设备的融合为旅游行业带来革新

    数字时代的推动下,旅游行业正迎来一场革命性的变革.数字孪生系统与虚拟现实(VR)的融合为旅游体验带来了全新的可能性.通过数字孪生技术的实时模拟和VR设备的沉浸式体验,旅游行业迎来了全新的变革时代. 数 ...

  8. NetSuite Tips —— 发送邮件未被接收或被退回

    Background: NS 发送的邮件过于频繁被邮箱系统识别为垃圾邮件,被拒收或被拦截 Solution: 添加以下邮箱地址到白名单 system@sent-via.netsuite.com nlm ...

  9. ubuntu 20.0.4 LTS 配置国内apt-get源

    https://blog.csdn.net/wangyijieonline/article/details/105360138 更换阿里源 要知道当前系统的代号,可以用以下命令: lsb_releas ...

  10. Python汉诺塔递归算法实现

    关于用递归实现的原理,请查看我之前的文章: C语言与汉诺塔 C#与汉诺塔 以下为代码: count = 0 def move(pile, src, tmp, dst): global count if ...